~ubuntu-branches/ubuntu/oneiric/gwenview/oneiric-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Gwenview: an image viewer
Copyright 2007 Aurélien Gâteau <agateau@kde.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
#include <qtest_kde.h>

#include "imagescalertest.moc"

#include "../lib/imagescaler.h"
#include "../lib/document/documentfactory.h"

#include "testutils.h"

QTEST_KDEMAIN( ImageScalerTest, GUI )

using namespace Gwenview;


static void waitUntilMetaInfoLoaded(Document::Ptr doc) {
	while (doc->loadingState() < Document::MetaInfoLoaded) {
		QTest::qWait(100);
	}
}


/**
 * Scale whole image in one pass
 */
void ImageScalerTest::testScaleFullImage() {
	const qreal zoom = 2;
	KUrl url = urlForTestFile("test.png");
	Document::Ptr doc = DocumentFactory::instance()->load(url);

	ImageScaler scaler;
	ImageScalerClient client(&scaler);
	scaler.setDocument(doc);
	scaler.setZoom(zoom);

	// FIXME: Load full image for now, because ImageScalerClient does not wait
	// when ImageScaler request a full image to be loaded asynchronously.
	doc->startLoadingFullImage();
	doc->waitUntilLoaded();

	scaler.setDestinationRegion(QRect(QPoint(0,0), doc->size() * zoom));

	QImage scaledImage = client.createFullImage();
	QCOMPARE(doc->loadingState(), Document::Loaded);

	QImage expectedImage = doc->image().scaled( doc->size() * zoom);
	QCOMPARE(scaledImage, expectedImage);
}


#if 0
/**
 * Scale parts of an image
 * In this test, the result image should be missing its bottom-right corner
 */
void ImageScalerTest::testScalePartialImage() {
	QImage image(10, 10, QImage::Format_ARGB32);
	const int zoom = 2;
	{
		QPainter painter(&image);
		painter.fillRect(image.rect(), Qt::white);
		painter.drawText(0, image.height(), "X");
	}

	Gwenview::ImageScaler scaler;
	ImageScalerClient client(&scaler);
	scaler.setImage(&image);
	scaler.setZoom(zoom);
	QRegion region;
	region |= QRect(
		0, 0,
		image.width() * zoom / 2, image.height() * zoom);

	region |= QRect(
		0, 0,
		image.width() * zoom, image.height() * zoom / 2);
	scaler.setDestinationRegion(region);

	QImage expectedImage(image.size() * zoom, image.format());
	expectedImage.fill(0);
	{
		QPainter painter(&expectedImage);
		QImage tmp;
		tmp= image.copy(0, 0,
			expectedImage.width() / zoom / 2,
			expectedImage.height() / zoom);
		painter.drawImage(0, 0, tmp.scaled(tmp.size() * zoom));
		tmp= image.copy(0, 0,
			expectedImage.width() / zoom,
			expectedImage.height() / zoom / 2);
		painter.drawImage(0, 0, tmp.scaled(tmp.size() * zoom));
	}

	QImage scaledImage = client.createFullImage();

	QCOMPARE(scaledImage, expectedImage);
}



/**
 * Scale whole image in two passes, not using exact pixel boundaries
 */
void ImageScalerTest::testScaleFullImageTwoPasses() {
	QFETCH(qreal, zoom);
	QImage image(10, 10, QImage::Format_ARGB32);
	{
		QPainter painter(&image);
		painter.fillRect(image.rect(), Qt::white);
		painter.drawLine(0, 0, image.width(), image.height());
	}

	Gwenview::ImageScaler scaler;
	ImageScalerClient client(&scaler);

	scaler.setImage(&image);
	scaler.setZoom(zoom);
	int zWidth = int(image.width() * zoom);
	int zHeight = int(image.width() * zoom);
	int partialZWidth = zWidth / 3;
	scaler.setDestinationRegion(
		QRect(
			0, 0,
			partialZWidth, zHeight)
		);

	scaler.setDestinationRegion(
		QRect(
			partialZWidth, 0,
			zWidth - partialZWidth, zHeight)
		);

	QImage expectedImage = image.scaled(image.size() * zoom);

	QImage scaledImage = client.createFullImage();
	QCOMPARE(expectedImage, scaledImage);
}


void ImageScalerTest::testScaleFullImageTwoPasses_data() {
	QTest::addColumn<qreal>("zoom");

	QTest::newRow("0.5") << 0.5;
	QTest::newRow("2.0") << 2.0;
	QTest::newRow("4.0") << 4.0;
}


/**
 * When zooming out, make sure that we don't crash when scaling an area which
 * have one dimension smaller than one pixel in the destination. 
 */
void ImageScalerTest::testScaleThinArea() {
	QImage image(10, 10, QImage::Format_ARGB32);
	image.fill(0);

	Gwenview::ImageScaler scaler;

	const qreal zoom = 0.25;
	scaler.setImage(&image);
	scaler.setZoom(zoom);
	scaler.setDestinationRegion(QRect(0, 0, image.width(), 2));
}


/**
 * Test instantiating a scaler without setting an image won't crash
 */
void ImageScalerTest::testDontCrashWithoutImage() {
	Gwenview::ImageScaler scaler;
	scaler.setZoom(1.0);
	scaler.setDestinationRegion(QRect(0, 0, 10, 10));
}


/**
 * Test that scaling down a big image (==bigger than MAX_CHUNK_AREA) does not
 * produce any gap
 */
void ImageScalerTest::testScaleDownBigImage() {
	QImage image(1704, 2272, QImage::Format_RGB32);
	image.fill(255);

	Gwenview::ImageScaler scaler;
	ImageScalerClient client(&scaler);

	const qreal zoom = 0.28125;
	scaler.setImage(&image);
	scaler.setZoom(zoom);
	scaler.setDestinationRegion(QRect( QPoint(0, 0), image.size() * zoom));

	QImage scaledImage = client.createFullImage();

	QImage expectedImage = image.scaled(scaledImage.size());
	QCOMPARE(expectedImage, scaledImage);
}
#endif