/***************************************************************************
 *   Copyright (C) 2005 by Ian Reinhart Geiser                             *
 *   geiseri@sourcextreme.com                                              *
 *                                                                         *
 *   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 <QApplication>
#include <QImage>
#include <QPainter>
#include <QLabel>
#include <QMatrix>

#include <qdebug.h>

#include <QtTest/QtTest>


#include <dhwpage.h>

#include "drawpath.h"

QTEST_MAIN(TestDrawPath)

char PageBytes[] = {	0x41, 0x43, 0x45, 0x43, 0x41, 0x44, 0x5f, 0x44, 0x49, 0x47, 0x49,
			0x4d, 0x45, 0x4d, 0x4f, 0x5f, 0x48, 0x41, 0x4e, 0x44, 0x57, 0x52,
			0x49, 0x54, 0x49, 0x4e, 0x47, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x01,
			0x38, 0x17, 0x10, 0x21, 0x01, 0x00, 0x00,
			0x81, 0x4e, 0x1f, 0x19, 0x19, 0x52, 0x1f, 0x1e, 0x19, 0x80, 0x6b, 0x10, 0x1b, 0x21, 0x88, 0x7f,
			0x81, 0x12, 0x20, 0x11, 0x1a, 0x15, 0x20, 0x16, 0x1a, 0x80, 0x17, 0x20, 0x1c, 0x1a, 0x90, 0x01 };

class MockDHWPage : public DHWPage
{
public:
	virtual QList<DHWLayer> readLayerData() const;
	virtual QSizeF readPageSize(  ) const;
	virtual DHWPage splitPage( int layerIndexBegin, int polyIndexBegin, int layerIndexEnd, int polyIndexEnd )  const;
};

QList<DHWLayer> MockDHWPage::readLayerData( ) const
{
	QList<DHWLayer> layers;
	DHWLayer layer;
	QPolygonF poly;
	poly << QPointF(100,100);
	poly << QPointF(100,200);
	poly << QPointF(200,200);
	layer.polyList << poly;
	QPolygonF border;
	border << QPointF(0,0);
	border << QPointF(1023,0);
	border << QPointF(1023,767);
	border << QPointF(0,767);
	border << QPointF(0,0);
	layer.polyList << border;
	layers << layer;
	return layers;
}

QSizeF MockDHWPage::readPageSize(  ) const
{
	return QSizeF(1024,768);
}

DHWPage MockDHWPage::splitPage( int layerIndexBegin, int polyIndexBegin, int layerIndexEnd, int polyIndexEnd )  const
{
	return *this;
}

void TestDrawPath::testScaleSize()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);
	QCOMPARE( page.scaleTo( QSizeF(800,632) ).toSize(), QSize(800,600) );
}

void TestDrawPath::testDrawPath()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);

	QImage img(page.readPageSize().toSize(), QImage::Format_RGB32);
	img.fill(qRgb(0xff, 0xff, 0xff));

	QPainter painter(&img);
	page.drawLayer(page.readPageSize(), &painter );
	painter.end();

	img.save("test_draw_path.png","PNG");

	QCOMPARE( QColor(img.pixel(100,100)), QColor(Qt::black) );
	QCOMPARE( QColor(img.pixel(100,200)), QColor(Qt::black) );
}

void TestDrawPath::testDrawScaledPage()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);

	QSizeF pgSize = page.readPageSize();
	pgSize.scale(640,480, Qt::KeepAspectRatioByExpanding);
	QImage img(pgSize.toSize(), QImage::Format_RGB32);
	img.fill(qRgb(0xff, 0xff, 0xff));

	QPainter painter(&img);
	page.drawLayer(pgSize, &painter  );
	painter.end();

	img.save("test_draw_scaled_page.png","PNG");

	QCOMPARE( img.width(), 640);
	QCOMPARE( img.height(), 480);

	QCOMPARE( QColor(img.pixel(62,62)),  QColor(Qt::black) );
	QCOMPARE( QColor(img.pixel(62,125)),  QColor(Qt::black) );
	QCOMPARE( QColor(img.pixel(125,125)),  QColor(Qt::black) );
}

void TestDrawPath::testDrawSection()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);

	QImage img(QSize(120,120), QImage::Format_RGB32);
	img.fill(qRgb(0xff, 0xff, 0xff));

	QPainter painter(&img);
	page.drawSection(page.readPageSize(), QRectF(90,90,120,120), &painter );
	painter.end();

	img.save("test_draw_section.png","PNG");

	QCOMPARE( img.size(), QSize(120,120) );
	QCOMPARE( QColor(img.pixel(10,10)),  QColor(Qt::black) );
	QCOMPARE( QColor(img.pixel(10,90)),  QColor(Qt::black) );
}

void TestDrawPath::testDrawScaledSection()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);


	QImage img(QSize(100,100), QImage::Format_RGB32);
	img.fill(qRgb(0xff, 0xff, 0xff));

	QPainter painter(&img);
	page.drawSection(QSizeF(640,480), QRectF(57,120,100,100), &painter );
	painter.end();

	img.save("test_draw_scaled_section.png","PNG");

	QCOMPARE( QColor(img.pixel(5,5)), QColor(Qt::black) );
}

void TestDrawPath::testDrawSubsetOfPage()
{
	QByteArray pageBytes(PageBytes, sizeof(PageBytes) );
	MockDHWPage page;
	page.setBytes(pageBytes);
	DHWPage newPage = page.splitPage( 0, 0, 0, 1 );
	
	QImage img(QSize(100,100), QImage::Format_RGB32);
	img.fill(qRgb(0xff, 0xff, 0xff));
	
	QPainter painter(&img);
	page.drawLayer(newPage.readPageSize(), &painter );
	painter.end();
	
	img.save("test_draw_subset_page.png","PNG");
	
	QCOMPARE( QColor(img.pixel(5,5)), QColor(Qt::black) );
}


