/***************************************************************************
 *   Copyright (C) 2007 by Sébastien Laoût                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "tools.h"

#include <qimage.h>

QPixmap Tools::addTransparentPixelsOnTop(const QPixmap &source, int nbPixels)
{
	// Verify if it is possible:
	if (nbPixels <= 0 || source.isNull())
		return source;

	// Create the images:
	QImage resultImage(source.width(), nbPixels + source.height(), 32/*bits*/);
	QImage sourceImage = source.convertToImage();
	resultImage.setAlphaBuffer(true);

	// Clear the top part by making it fully transparent:
	uint *p;
	for (int row = 0; row < nbPixels; ++row) {
		for (int column = 0; column < resultImage.width(); ++column) {
			p = (uint *)resultImage.scanLine(row) + column;
			*p = 0; // qRgba(0, 0, 0, 0)
		}
	}

	// Copy the source image byte per byte to the right part:
	uint *q;
	for (int row = 0; row < sourceImage.height(); ++row) {
		for (int column = 0; column < sourceImage.width(); ++column) {
			p = (uint *)resultImage.scanLine(row + nbPixels) + column;
			q = (uint *)sourceImage.scanLine(row) + column;
			*p = *q;
		}
	}

	// And return the result:
	QPixmap result;
	result.convertFromImage(resultImage);
	return result;
}

QPixmap Tools::addTransparentPixelsOnLeftRight(const QPixmap &source, int nbPixelsLeft, int nbPixelsRight)
{
	// Verify if it is possible:
	if (nbPixelsLeft + nbPixelsRight <= 0 || source.isNull())
		return source;

	// Create the images:
	QImage resultImage(nbPixelsLeft + source.width() + nbPixelsRight, source.height(), 32/*bits*/);
	QImage sourceImage = source.convertToImage();
	resultImage.setAlphaBuffer(true);

	// Clear the top part by making it fully transparent:
	uint *p;
	for (int row = 0; row < resultImage.height(); ++row) {
		for (int column = 0; column < nbPixelsLeft; ++column) {
			p = (uint *)resultImage.scanLine(row) + column;
			*p = 0; // qRgba(0, 0, 0, 0)
		}
		for (int column = 0; column < nbPixelsRight; ++column) {
			p = (uint *)resultImage.scanLine(row) + resultImage.width() - 1 - column;
			*p = 0; // qRgba(0, 0, 0, 0)
		}
	}

	// Copy the source image byte per byte to the right part:
	uint *q;
	for (int row = 0; row < sourceImage.height(); ++row) {
		for (int column = 0; column < sourceImage.width(); ++column) {
			p = (uint *)resultImage.scanLine(row) + column + nbPixelsLeft;
			q = (uint *)sourceImage.scanLine(row) + column;
			*p = *q;
		}
	}

	// And return the result:
	QPixmap result;
	result.convertFromImage(resultImage);
	return result;
}
