summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/image/qpixmapfilter/tst_qpixmapfilter.cpp
blob: 91ba2332d1a0093bef6dc84085d466cc0c13c750 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <qpixmap.h>
#include <private/qpixmapfilter_p.h>
#include <qpainter.h>

class tst_QPixmapFilter : public QObject
{
    Q_OBJECT

public:
    tst_QPixmapFilter();
    virtual ~tst_QPixmapFilter();


public slots:
    void init();
    void cleanup();

private slots:
    void colorizeSetColor();
    void colorizeSetStrength();
    void colorizeProcess();
    void colorizeDraw();
    void colorizeDrawStrength();
    void colorizeDrawSubRect();
    void colorizeProcessSubRect();
    void convolutionBoundingRectFor();
    void convolutionDrawSubRect();
    void dropShadowBoundingRectFor();
    void blurIndexed8();

    void testDefaultImplementations();
};

class CustomFilter : public QPixmapFilter
{
public:
    enum { Type = QPixmapFilter::UserFilter + 1 };

    CustomFilter() : QPixmapFilter((QPixmapFilter::FilterType) Type, 0) { };

    void draw(QPainter *p, const QPointF &pt, const QPixmap &src, const QRectF &srcRect = QRectF()) const {
        p->drawPixmap(QRectF(pt, srcRect.size()), src, srcRect);
    }
};

tst_QPixmapFilter::tst_QPixmapFilter()
{
}

tst_QPixmapFilter::~tst_QPixmapFilter()
{
}

void tst_QPixmapFilter::init()
{
}

void tst_QPixmapFilter::cleanup()
{
}

void tst_QPixmapFilter::testDefaultImplementations()
{
    CustomFilter filter;
    QCOMPARE(filter.type(), (QPixmapFilter::FilterType) CustomFilter::Type);

    QCOMPARE(filter.boundingRectFor(QRectF(1, 2, 4, 8)), QRectF(1, 2, 4, 8));

    QPixmap src(10, 10);
    src.fill(Qt::blue);

    QPixmap test(src.size());
    QPainter p(&test);
    filter.draw(&p, QPointF(0, 0), src, src.rect());
    p.end();

    QCOMPARE(test.toImage().pixel(0, 0), 0xff0000ff);
}

void tst_QPixmapFilter::colorizeSetColor()
{
    QPixmapColorizeFilter filter;
    filter.setColor(QColor(50, 100, 200));
    QCOMPARE(filter.color(), QColor(50, 100, 200));
}

void tst_QPixmapFilter::colorizeSetStrength()
{
    QPixmapColorizeFilter filter;
    QCOMPARE(filter.strength(), qreal(1));
    filter.setStrength(0.5);
    QCOMPARE(filter.strength(), qreal(0.5));
    filter.setStrength(0.0);
    QCOMPARE(filter.strength(), qreal(0.0));
}

void tst_QPixmapFilter::colorizeProcess()
{
    QPixmapColorizeFilter filter;
    filter.setColor(QColor(100, 100, 100));

    QCOMPARE(filter.boundingRectFor(QRectF(0, 0, 50, 50)), QRectF(0, 0, 50, 50));
    QCOMPARE(filter.boundingRectFor(QRectF(30, 20, 10, 40)), QRectF(30, 20, 10, 40));
    QCOMPARE(filter.boundingRectFor(QRectF(2.2, 6.3, 11.4, 47.5)), QRectF(2.2, 6.3, 11.4, 47.5));

    QPixmap source("noise.png");
    QImage result(source.size(), QImage::Format_ARGB32_Premultiplied);
    result.fill(0);
    QPainter p(&result);
    filter.draw(&p, QPointF(0, 0), source);
    p.end();
    QImage resultImg = result;
    for(int y = 0; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width(); x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QCOMPARE(qRed(pixel), qGreen(pixel));
            QCOMPARE(qGreen(pixel), qBlue(pixel));
        }
    }
}

void tst_QPixmapFilter::colorizeDraw()
{
    QPixmapColorizeFilter filter;
    filter.setColor(QColor(100, 100, 100));

    QPixmap pixmap("noise.png");
    QImage result(pixmap.size(), QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&result);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(result.rect(), QColor(128, 0, 0, 0));
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    filter.draw(&painter, QPointF(0, 0), pixmap);
    painter.end();

    QImage resultImg = result;
    for(int y = 0; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width(); x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QCOMPARE(qRed(pixel), qGreen(pixel));
            QCOMPARE(qGreen(pixel), qBlue(pixel));
        }
    }
}

void tst_QPixmapFilter::colorizeDrawStrength()
{
    QPixmapColorizeFilter filter;
    filter.setColor(Qt::blue);
    filter.setStrength(0.3);

    QImage source(256, 128, QImage::Format_ARGB32);
    source.fill(qRgb(255, 0, 0));
    QPixmap pixmap = QPixmap::fromImage(source);

    QImage result(pixmap.size(), QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&result);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    filter.draw(&painter, QPointF(0, 0), pixmap);
    painter.end();

    QImage resultImg = result;
    for(int y = 0; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width(); x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QCOMPARE(qRed(pixel), 206);
            QCOMPARE(qGreen(pixel), 26);
            QCOMPARE(qBlue(pixel), 75);
        }
    }
}

void tst_QPixmapFilter::colorizeDrawSubRect()
{
    QPixmapColorizeFilter filter;
    filter.setColor(QColor(255, 255, 255));

    QPixmap pixmap("noise.png");
    QImage result(pixmap.size(), QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&result);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(result.rect(), QColor(128, 0, 0, 255));
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    filter.draw(&painter, QPointF(16, 16), pixmap, QRectF(16, 16, 16, 16));
    painter.end();

    QImage resultImg = result;
    QImage sourceImg = pixmap.toImage();
    for(int y = 0; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width(); x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            if(x>=16 && x<32 && y>=16 && y<32) {
                QCOMPARE(qRed(pixel), qGreen(pixel));
                QCOMPARE(qGreen(pixel), qBlue(pixel));
            } else  {
                QCOMPARE(qRed(pixel), 128);
                QCOMPARE(qGreen(pixel), 0);
                QCOMPARE(qBlue(pixel), 0);
                QCOMPARE(qAlpha(pixel), 255);
            }
        }
    }
}

void tst_QPixmapFilter::colorizeProcessSubRect()
{
    QPixmapColorizeFilter filter;
    filter.setColor(QColor(200, 200, 200));

    QPixmap source("noise.png");
    QImage result(QSize(16, 16), QImage::Format_ARGB32_Premultiplied);
    result.fill(0);
    QPainter p(&result);
    filter.draw(&p, QPointF(0, 0), source, QRectF(16, 16, 16, 16));
    p.end();

    QImage resultImg = result;
    for(int y = 0; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width(); x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QCOMPARE(qRed(pixel), qGreen(pixel));
            QCOMPARE(qGreen(pixel), qBlue(pixel));
        }
    }
}

void tst_QPixmapFilter::convolutionBoundingRectFor()
{
    QPixmapConvolutionFilter filter;
    QCOMPARE(filter.boundingRectFor(QRectF(0, 0, 50, 50)), QRectF(0, 0, 50, 50));
    QCOMPARE(filter.boundingRectFor(QRectF(30, 20, 10, 40)), QRectF(30, 20, 10, 40));
    QCOMPARE(filter.boundingRectFor(QRectF(2.2, 6.3, 11.4, 47.5)), QRectF(2.2, 6.3, 11.4, 47.5));
    qreal kernel[] = {
        0, 0, 0, 0,
        0, 0, 0, 0,
        0, 0, 0, 0,
        0, 0, 0, 0
    };
    filter.setConvolutionKernel(kernel, 2, 2);
    QCOMPARE(filter.boundingRectFor(QRectF(0, 0, 50, 50)), QRectF(-1, -1, 51, 51));
    QCOMPARE(filter.boundingRectFor(QRectF(30, 20, 10, 40)), QRectF(29, 19, 11, 41));
    QCOMPARE(filter.boundingRectFor(QRectF(2.2, 6.3, 11.4, 47.5)), QRectF(1.2, 5.3, 12.4, 48.5));

    filter.setConvolutionKernel(kernel, 3, 3);
    QCOMPARE(filter.boundingRectFor(QRectF(0, 0, 50, 50)), QRectF(-1, -1, 52, 52));
    QCOMPARE(filter.boundingRectFor(QRectF(30, 20, 10, 40)), QRectF(29, 19, 12, 42));
    QCOMPARE(filter.boundingRectFor(QRectF(2.2, 6.3, 11.4, 47.5)), QRectF(1.2, 5.3, 13.4, 49.5));

    filter.setConvolutionKernel(kernel, 4, 4);
    QCOMPARE(filter.boundingRectFor(QRectF(0, 0, 50, 50)), QRectF(-2, -2, 53, 53));
    QCOMPARE(filter.boundingRectFor(QRectF(30, 20, 10, 40)), QRectF(28, 18, 13, 43));
    QCOMPARE(filter.boundingRectFor(QRectF(2.2, 6.3, 11.4, 47.5)), QRectF(0.2, 4.3, 14.4, 50.5));
}

void tst_QPixmapFilter::convolutionDrawSubRect()
{
    QPixmapConvolutionFilter filter;
    qreal kernel[] = {
        0, 0, 0,
        0, 0, 0,
        0, 0, 1
    };
    filter.setConvolutionKernel(kernel, 3, 3);

    QPixmap pixmap("noise.png");
    QImage result(pixmap.size(), QImage::Format_ARGB32_Premultiplied);
    QPainter painter(&result);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(result.rect(), QColor(128, 0, 0, 255));
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    filter.draw(&painter, QPointF(16, 16), pixmap, QRectF(16, 16, 16, 16));
    painter.end();

    QImage resultImg = result;
    QImage sourceImg = pixmap.toImage();
    for(int y = 0; y < resultImg.height()-1; y++)
    {
        for(int x = 0; x < resultImg.width()-1; x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QRgb srcPixel = sourceImg.pixel(x+1,y+1);
            if(x>=15 && x<33 && y>=15 && y<33) {
                QCOMPARE(pixel, srcPixel);
            } else  {
                QCOMPARE(qRed(pixel), 128);
                QCOMPARE(qGreen(pixel), 0);
                QCOMPARE(qBlue(pixel), 0);
                QCOMPARE(qAlpha(pixel), 255);
            }
        }
    }


    kernel[2] = 1;
    kernel[8] = 0;
    filter.setConvolutionKernel(kernel, 3, 3);

    QPainter painter2(&result);
    painter2.setCompositionMode(QPainter::CompositionMode_Source);
    painter2.fillRect(result.rect(), QColor(128, 0, 0, 255));
    painter2.setCompositionMode(QPainter::CompositionMode_SourceOver);
    filter.draw(&painter2, QPointF(16, 16), pixmap, QRectF(16, 16, 16, 16));
    painter2.end();

    resultImg = result;
    sourceImg = pixmap.toImage();
    for(int y = 1; y < resultImg.height(); y++)
    {
        for(int x = 0; x < resultImg.width()-1; x++)
        {
            QRgb pixel = resultImg.pixel(x,y);
            QRgb srcPixel = sourceImg.pixel(x+1,y-1);
            if(x>=15 && x<33 && y>=15 && y<33) {
                QCOMPARE(pixel, srcPixel);
            } else  {
                QCOMPARE(qRed(pixel), 128);
                QCOMPARE(qGreen(pixel), 0);
                QCOMPARE(qBlue(pixel), 0);
                QCOMPARE(qAlpha(pixel), 255);
            }
        }
    }

}

void tst_QPixmapFilter::dropShadowBoundingRectFor()
{
    QPixmapDropShadowFilter filter;
    filter.setBlurRadius(0);

    QCOMPARE(filter.blurRadius(), 0.);

    const QRectF rect1(0, 0, 50, 50);
    const QRectF rect2(30, 20, 10, 40);
    const QRectF rect3(2.2, 6.3, 11.4, 47.5);

    filter.setOffset(QPointF(0,0));
    QCOMPARE(filter.boundingRectFor(rect1), rect1);
    QCOMPARE(filter.boundingRectFor(rect2), rect2);
    QCOMPARE(filter.boundingRectFor(rect3), rect3);

    filter.setOffset(QPointF(1,1));
    QCOMPARE(filter.offset(), QPointF(1, 1));
    QCOMPARE(filter.boundingRectFor(rect1), rect1.adjusted(0, 0, 1, 1));
    QCOMPARE(filter.boundingRectFor(rect2), rect2.adjusted(0, 0, 1, 1));
    QCOMPARE(filter.boundingRectFor(rect3), rect3.adjusted(0, 0, 1, 1));

    filter.setOffset(QPointF(-1,-1));
    QCOMPARE(filter.boundingRectFor(rect1), rect1.adjusted(-1, -1, 0, 0));
    QCOMPARE(filter.boundingRectFor(rect2), rect2.adjusted(-1, -1, 0, 0));
    QCOMPARE(filter.boundingRectFor(rect3), rect3.adjusted(-1, -1, 0, 0));

    filter.setBlurRadius(2);
    filter.setOffset(QPointF(0,0));
    qreal delta = 2;
    QCOMPARE(filter.boundingRectFor(rect1), rect1.adjusted(-delta, -delta, delta, delta));
    QCOMPARE(filter.boundingRectFor(rect2), rect2.adjusted(-delta, -delta, delta, delta));
    QCOMPARE(filter.boundingRectFor(rect3), rect3.adjusted(-delta, -delta, delta, delta));

    filter.setOffset(QPointF(1,1));
    QCOMPARE(filter.boundingRectFor(rect1), rect1.adjusted(-delta + 1, -delta + 1, delta + 1, delta + 1));
    QCOMPARE(filter.boundingRectFor(rect2), rect2.adjusted(-delta + 1, -delta + 1, delta + 1, delta + 1));
    QCOMPARE(filter.boundingRectFor(rect3), rect3.adjusted(-delta + 1, -delta + 1, delta + 1, delta + 1));

    filter.setOffset(QPointF(-10,-10));
    QCOMPARE(filter.boundingRectFor(rect1), rect1.adjusted(-delta - 10, -delta - 10, 0, 0));
    QCOMPARE(filter.boundingRectFor(rect2), rect2.adjusted(-delta - 10, -delta - 10, 0, 0));
    QCOMPARE(filter.boundingRectFor(rect3), rect3.adjusted(-delta - 10, -delta - 10, 0, 0));
}

QT_BEGIN_NAMESPACE
void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);
QT_END_NAMESPACE

void tst_QPixmapFilter::blurIndexed8()
{
    QImage img(16, 32, QImage::Format_Indexed8);
    img.setColorCount(256);
    for (int i = 0; i < 256; ++i)
        img.setColor(i, qRgb(i, i, i));

    img.fill(255);

    QImage original = img;
    qt_blurImage(img, 10, true, false);
    QCOMPARE(original.size(), img.size());

    original = img;
    qt_blurImage(img, 10, true, true);
    QCOMPARE(original.size(), QSize(img.height(), img.width()));
}

QTEST_MAIN(tst_QPixmapFilter)
#include "tst_qpixmapfilter.moc"