~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to tests/auto/imagescalertest.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Gwenview: an image viewer
 
3
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
 
4
 
 
5
This program is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU General Public License
 
7
as published by the Free Software Foundation; either version 2
 
8
of the License, or (at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
#include <qtest_kde.h>
 
21
 
 
22
#include "imagescalertest.moc"
 
23
 
 
24
#include "../lib/imagescaler.h"
 
25
#include "../lib/document/documentfactory.h"
 
26
 
 
27
#include "testutils.h"
 
28
 
 
29
QTEST_KDEMAIN(ImageScalerTest, GUI)
 
30
 
 
31
using namespace Gwenview;
 
32
 
 
33
static void waitUntilMetaInfoLoaded(Document::Ptr doc)
 
34
{
 
35
    while (doc->loadingState() < Document::MetaInfoLoaded) {
 
36
        QTest::qWait(100);
 
37
    }
 
38
}
 
39
 
 
40
/**
 
41
 * Scale whole image in one pass
 
42
 */
 
43
void ImageScalerTest::testScaleFullImage()
 
44
{
 
45
    const qreal zoom = 2;
 
46
    KUrl url = urlForTestFile("test.png");
 
47
    Document::Ptr doc = DocumentFactory::instance()->load(url);
 
48
 
 
49
    ImageScaler scaler;
 
50
    ImageScalerClient client(&scaler);
 
51
    scaler.setDocument(doc);
 
52
    scaler.setZoom(zoom);
 
53
 
 
54
    // FIXME: Load full image for now, because ImageScalerClient does not wait
 
55
    // when ImageScaler request a full image to be loaded asynchronously.
 
56
    doc->startLoadingFullImage();
 
57
    doc->waitUntilLoaded();
 
58
 
 
59
    scaler.setDestinationRegion(QRect(QPoint(0, 0), doc->size() * zoom));
 
60
 
 
61
    QImage scaledImage = client.createFullImage();
 
62
    QCOMPARE(doc->loadingState(), Document::Loaded);
 
63
 
 
64
    QImage expectedImage = doc->image().scaled(doc->size() * zoom);
 
65
    QCOMPARE(scaledImage, expectedImage);
 
66
}
 
67
 
 
68
#if 0
 
69
/**
 
70
 * Scale parts of an image
 
71
 * In this test, the result image should be missing its bottom-right corner
 
72
 */
 
73
void ImageScalerTest::testScalePartialImage()
 
74
{
 
75
    QImage image(10, 10, QImage::Format_ARGB32);
 
76
    const int zoom = 2;
 
77
    {
 
78
        QPainter painter(&image);
 
79
        painter.fillRect(image.rect(), Qt::white);
 
80
        painter.drawText(0, image.height(), "X");
 
81
    }
 
82
 
 
83
    Gwenview::ImageScaler scaler;
 
84
    ImageScalerClient client(&scaler);
 
85
    scaler.setImage(&image);
 
86
    scaler.setZoom(zoom);
 
87
    QRegion region;
 
88
    region |= QRect(
 
89
                  0, 0,
 
90
                  image.width() * zoom / 2, image.height() * zoom);
 
91
 
 
92
    region |= QRect(
 
93
                  0, 0,
 
94
                  image.width() * zoom, image.height() * zoom / 2);
 
95
    scaler.setDestinationRegion(region);
 
96
 
 
97
    QImage expectedImage(image.size() * zoom, image.format());
 
98
    expectedImage.fill(0);
 
99
    {
 
100
        QPainter painter(&expectedImage);
 
101
        QImage tmp;
 
102
        tmp = image.copy(0, 0,
 
103
                         expectedImage.width() / zoom / 2,
 
104
                         expectedImage.height() / zoom);
 
105
        painter.drawImage(0, 0, tmp.scaled(tmp.size() * zoom));
 
106
        tmp = image.copy(0, 0,
 
107
                         expectedImage.width() / zoom,
 
108
                         expectedImage.height() / zoom / 2);
 
109
        painter.drawImage(0, 0, tmp.scaled(tmp.size() * zoom));
 
110
    }
 
111
 
 
112
    QImage scaledImage = client.createFullImage();
 
113
 
 
114
    QCOMPARE(scaledImage, expectedImage);
 
115
}
 
116
 
 
117
/**
 
118
 * Scale whole image in two passes, not using exact pixel boundaries
 
119
 */
 
120
void ImageScalerTest::testScaleFullImageTwoPasses()
 
121
{
 
122
    QFETCH(qreal, zoom);
 
123
    QImage image(10, 10, QImage::Format_ARGB32);
 
124
    {
 
125
        QPainter painter(&image);
 
126
        painter.fillRect(image.rect(), Qt::white);
 
127
        painter.drawLine(0, 0, image.width(), image.height());
 
128
    }
 
129
 
 
130
    Gwenview::ImageScaler scaler;
 
131
    ImageScalerClient client(&scaler);
 
132
 
 
133
    scaler.setImage(&image);
 
134
    scaler.setZoom(zoom);
 
135
    int zWidth = int(image.width() * zoom);
 
136
    int zHeight = int(image.width() * zoom);
 
137
    int partialZWidth = zWidth / 3;
 
138
    scaler.setDestinationRegion(
 
139
        QRect(
 
140
            0, 0,
 
141
            partialZWidth, zHeight)
 
142
    );
 
143
 
 
144
    scaler.setDestinationRegion(
 
145
        QRect(
 
146
            partialZWidth, 0,
 
147
            zWidth - partialZWidth, zHeight)
 
148
    );
 
149
 
 
150
    QImage expectedImage = image.scaled(image.size() * zoom);
 
151
 
 
152
    QImage scaledImage = client.createFullImage();
 
153
    QCOMPARE(expectedImage, scaledImage);
 
154
}
 
155
 
 
156
void ImageScalerTest::testScaleFullImageTwoPasses_data()
 
157
{
 
158
    QTest::addColumn<qreal>("zoom");
 
159
 
 
160
    QTest::newRow("0.5") << 0.5;
 
161
    QTest::newRow("2.0") << 2.0;
 
162
    QTest::newRow("4.0") << 4.0;
 
163
}
 
164
 
 
165
/**
 
166
 * When zooming out, make sure that we don't crash when scaling an area which
 
167
 * have one dimension smaller than one pixel in the destination.
 
168
 */
 
169
void ImageScalerTest::testScaleThinArea()
 
170
{
 
171
    QImage image(10, 10, QImage::Format_ARGB32);
 
172
    image.fill(0);
 
173
 
 
174
    Gwenview::ImageScaler scaler;
 
175
 
 
176
    const qreal zoom = 0.25;
 
177
    scaler.setImage(&image);
 
178
    scaler.setZoom(zoom);
 
179
    scaler.setDestinationRegion(QRect(0, 0, image.width(), 2));
 
180
}
 
181
 
 
182
/**
 
183
 * Test instantiating a scaler without setting an image won't crash
 
184
 */
 
185
void ImageScalerTest::testDontCrashWithoutImage()
 
186
{
 
187
    Gwenview::ImageScaler scaler;
 
188
    scaler.setZoom(1.0);
 
189
    scaler.setDestinationRegion(QRect(0, 0, 10, 10));
 
190
}
 
191
 
 
192
/**
 
193
 * Test that scaling down a big image (==bigger than MAX_CHUNK_AREA) does not
 
194
 * produce any gap
 
195
 */
 
196
void ImageScalerTest::testScaleDownBigImage()
 
197
{
 
198
    QImage image(1704, 2272, QImage::Format_RGB32);
 
199
    image.fill(255);
 
200
 
 
201
    Gwenview::ImageScaler scaler;
 
202
    ImageScalerClient client(&scaler);
 
203
 
 
204
    const qreal zoom = 0.28125;
 
205
    scaler.setImage(&image);
 
206
    scaler.setZoom(zoom);
 
207
    scaler.setDestinationRegion(QRect(QPoint(0, 0), image.size() * zoom));
 
208
 
 
209
    QImage scaledImage = client.createFullImage();
 
210
 
 
211
    QImage expectedImage = image.scaled(scaledImage.size());
 
212
    QCOMPARE(expectedImage, scaledImage);
 
213
}
 
214
#endif