Debian Codegraph @

tests/auto/gui/painting/qpainter/tst_qpainter.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29
30 #include <QtTest/QtTest>
31 #include <qpainter.h>
32 #ifndef QT_NO_WIDGETS
33 #include <qdrawutil.h>
34 #include <qwidget.h>
35 #endif
36 #include <qguiapplication.h>
37 #include <qfontmetrics.h>
38 #include <qbitmap.h>
39 #include <qimage.h>
40 #include <qthread.h>
41 #include <limits.h>
42 #include <math.h>
43 #include <qpaintengine.h>
44 #include <qpixmap.h>
45 #include <qrandom.h>
46
47 #include <private/qdrawhelper_p.h>
48 #include <qpainter.h>
49 #include <qpainterpath.h>
50 #include <qqueue.h>
51 #include <qscreen.h>
52
53 #ifndef QT_NO_WIDGETS
54 #include <qgraphicsview.h>
55 #include <qgraphicsscene.h>
56 #include <qgraphicsproxywidget.h>
57 #endif
58 #include <qfontdatabase.h>
59
60 Q_DECLARE_METATYPE(QGradientStops)
61 Q_DECLARE_METATYPE(QPainterPath)
62 Q_DECLARE_METATYPE(QImage::Format)
63 Q_DECLARE_METATYPE(QPainter::CompositionMode)
64
65 class tst_QPainter : public QObject
66 {
67 Q_OBJECT
68
69 public:
70 tst_QPainter();
71
72 private slots:
73 void cleanupTestCase();
74 void getSetCheck();
75 #ifndef QT_NO_WIDGETS
76 void drawPixmap_comp_data();
77 void drawPixmap_comp();
78 #endif
79 void saveAndRestore_data();
80 void saveAndRestore();
81
82 #ifndef QT_NO_WIDGETS
83 void drawBorderPixmap();
84 #endif
85 void drawPixmapFragments();
86 void drawPixmapNegativeScale();
87 void drawPixmapRounding();
88
89 void drawLine_data();
90 void drawLine();
91 void drawLine_clipped();
92 void drawLine_task121143();
93 void drawLine_task216948();
94
95 void drawLine_task190634();
96 void drawLine_task229459();
97 void drawLine_task234891();
98 void drawLineEndPoints();
99
100 void drawRect_data() { fillData(); }
101 void drawRect();
102 void drawRect2();
103
104 void fillRect_data();
105 void fillRect();
106 void fillRect2_data();
107 void fillRect2();
108 void fillRect3_data() { fillRect2_data(); }
109 void fillRect3();
110 void fillRect4_data() { fillRect2_data(); }
111 void fillRect4();
112 void fillRectNonPremul_data();
113 void fillRectNonPremul();
114
115 void fillRectRGB30_data();
116 void fillRectRGB30();
117
118 void drawEllipse_data();
119 void drawEllipse();
120 void drawClippedEllipse_data();
121 void drawClippedEllipse();
122
123 void drawPath_data();
124 void drawPath();
125 void drawPath2();
126 void drawPath3();
127
128 #if QT_DEPRECATED_SINCE(5, 13)
129 void drawRoundRect_data() { fillData(); }
130 void drawRoundRect();
131 #endif
132 void drawRoundedRect_data() { fillData(); }
133 void drawRoundedRect();
134
135 void qimageFormats_data();
136 void qimageFormats();
137 void textOnTransparentImage();
138
139 #if !defined(QT_NO_WIDGETS) && QT_DEPRECATED_SINCE(5, 13)
140 void initFrom();
141 #endif
142
143 void setWindow();
144
145 void combinedMatrix();
146 void renderHints();
147
148 void disableEnableClipping();
149 void setClipRect();
150 void clipRect();
151 void setEqualClipRegionAndPath_data();
152 void setEqualClipRegionAndPath();
153
154 void clipRectSaveRestore();
155 void clipStateSaveRestore();
156
157 void clippedFillPath_data();
158 void clippedFillPath();
159 void clippedLines_data();
160 void clippedLines();
161 void clippedPolygon_data();
162 void clippedPolygon();
163 void clippedText();
164
165 void clipBoundingRect();
166 void transformedClip();
167
168 void setOpacity_data();
169 void setOpacity();
170
171 void drawhelper_blend_untransformed_data();
172 void drawhelper_blend_untransformed();
173 void drawhelper_blend_tiled_untransformed_data();
174 void drawhelper_blend_tiled_untransformed();
175
176 void porterDuff_warning();
177
178 void drawhelper_blend_color();
179
180 #ifndef QT_NO_WIDGETS
181 void childWidgetViewport();
182 #endif
183
184 void fillRect_objectBoundingModeGradient();
185 void fillRect_stretchToDeviceMode();
186 void monoImages();
187
188 void linearGradientSymmetry_data();
189 void linearGradientSymmetry();
190 void gradientInterpolation();
191
192 void gradientPixelFormat_data();
193 void gradientPixelFormat();
194
195 #if QT_CONFIG(raster_64bit)
196 void linearGradientRgb30_data();
197 void linearGradientRgb30();
198 void radialGradientRgb30_data();
199 void radialGradientRgb30();
200 #endif
201
202 void fpe_pixmapTransform();
203 void fpe_zeroLengthLines();
204 void fpe_divByZero();
205
206 void fpe_steepSlopes_data();
207 void fpe_steepSlopes();
208 void fpe_rasterizeLine_task232012();
209
210 void fpe_radialGradients();
211
212 void rasterizer_asserts();
213 void rasterizer_negativeCoords();
214
215 void blendOverFlow_data();
216 void blendOverFlow();
217
218 void largeImagePainting_data();
219 void largeImagePainting();
220
221 void imageScaling_task206785();
222
223 void outlineFillConsistency();
224
225 void drawImage_task217400_data();
226 void drawImage_task217400();
227 void drawImage_1x1();
228 void drawImage_task258776();
229 void drawImage_QTBUG28324();
230 void drawRect_task215378();
231 void drawRect_task247505();
232
233 #if defined(Q_OS_MAC)
234 void drawText_subPixelPositionsInRaster_qtbug5053();
235 #endif
236
237 void drawImage_data();
238 void drawImage();
239
240 void clippedImage();
241
242 void stateResetBetweenQPainters();
243
244 void imageCoordinateLimit();
245 void imageBlending_data();
246 void imageBlending();
247 void imageBlending_clipped();
248
249 void paintOnNullPixmap();
250 void checkCompositionMode();
251
252 void drawPolygon();
253
254 void inactivePainter();
255
256 void extendedBlendModes();
257
258 void zeroOpacity();
259 void clippingBug();
260 void emptyClip();
261
262 void taskQT4444_dontOverflowDashOffset();
263
264 void painterBegin();
265 void setPenColorOnImage();
266 void setPenColorOnPixmap();
267
268 #ifndef QT_NO_WIDGETS
269 void QTBUG5939_attachPainterPrivate();
270 #endif
271
272 void drawPointScaled();
273
274 void QTBUG14614_gradientCacheRaceCondition();
275 void drawTextOpacity();
276
277 void QTBUG17053_zeroDashPattern();
278
279 void QTBUG38781_NoBrushAndQBitmap();
280
281 void drawTextOutsideGuiThread();
282
283 void drawTextWithComplexBrush();
284 void QTBUG26013_squareCapStroke();
285 void QTBUG25153_drawLine();
286
287 void cosmeticStrokerClipping_data();
288 void cosmeticStrokerClipping();
289
290 void blendARGBonRGB_data();
291 void blendARGBonRGB();
292
293 void RasterOp_NotDestination();
294 void drawTextNoHinting();
295
296 void drawPolyline_data();
297 void drawPolyline();
298
299 void QTBUG50153_drawImage_assert();
300
301 void rotateImage_data();
302 void rotateImage();
303
304 void QTBUG56252();
305
306 void blendNullRGB32();
307 void toRGB64();
308
309 void fillPolygon();
310
311 void drawImageAtPointF();
312 void scaledDashes();
313
314 private:
315 void fillData();
316 void setPenColor(QPainter& p);
317 QColor baseColor( int k, int intensity=255 );
318 QImage getResImage( const QString &dir, const QString &addition, const QString &extension );
319 QBitmap getBitmap( const QString &dir, const QString &filename, bool mask );
320 };
321
322 // Testing get/set functions
323 void tst_QPainter::getSetCheck()
324 {
325 QImage img(QSize(10, 10), QImage::Format_ARGB32_Premultiplied);
326 QPainter obj1;
327 obj1.begin(&img);
328 // CompositionMode QPainter::compositionMode()
329 // void QPainter::setCompositionMode(CompositionMode)
330 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_SourceOver));
331 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_SourceOver), obj1.compositionMode());
332 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_DestinationOver));
333 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_DestinationOver), obj1.compositionMode());
334 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_Clear));
335 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_Clear), obj1.compositionMode());
336 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_Source));
337 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_Source), obj1.compositionMode());
338 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_Destination));
339 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_Destination), obj1.compositionMode());
340 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_SourceIn));
341 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_SourceIn), obj1.compositionMode());
342 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_DestinationIn));
343 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_DestinationIn), obj1.compositionMode());
344 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_SourceOut));
345 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_SourceOut), obj1.compositionMode());
346 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_DestinationOut));
347 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_DestinationOut), obj1.compositionMode());
348 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_SourceAtop));
349 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_SourceAtop), obj1.compositionMode());
350 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_DestinationAtop));
351 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_DestinationAtop), obj1.compositionMode());
352 obj1.setCompositionMode(QPainter::CompositionMode(QPainter::CompositionMode_Xor));
353 QCOMPARE(QPainter::CompositionMode(QPainter::CompositionMode_Xor), obj1.compositionMode());
354
355 // const QPen & QPainter::pen()
356 // void QPainter::setPen(const QPen &)
357 QPen var3(Qt::red);
358 obj1.setPen(var3);
359 QCOMPARE(var3, obj1.pen());
360 obj1.setPen(QPen());
361 QCOMPARE(QPen(), obj1.pen());
362
363 // const QBrush & QPainter::brush()
364 // void QPainter::setBrush(const QBrush &)
365 QBrush var4(Qt::red);
366 obj1.setBrush(var4);
367 QCOMPARE(var4, obj1.brush());
368 obj1.setBrush(QBrush());
369 QCOMPARE(QBrush(), obj1.brush());
370
371 // const QBrush & QPainter::background()
372 // void QPainter::setBackground(const QBrush &)
373 QBrush var5(Qt::yellow);
374 obj1.setBackground(var5);
375 QCOMPARE(var5, obj1.background());
376 obj1.setBackground(QBrush());
377 QCOMPARE(QBrush(), obj1.background());
378
379 // bool QPainter::matrixEnabled()
380 // void QPainter::setMatrixEnabled(bool)
381 obj1.setWorldMatrixEnabled(false);
382 QCOMPARE(false, obj1.worldMatrixEnabled());
383 obj1.setWorldMatrixEnabled(true);
384 QCOMPARE(true, obj1.worldMatrixEnabled());
385
386 // bool QPainter::viewTransformEnabled()
387 // void QPainter::setViewTransformEnabled(bool)
388 obj1.setViewTransformEnabled(false);
389 QCOMPARE(false, obj1.viewTransformEnabled());
390 obj1.setViewTransformEnabled(true);
391 QCOMPARE(true, obj1.viewTransformEnabled());
392 }
393
394
395 tst_QPainter::tst_QPainter()
396 {
397 // QtTestCase sets this to false, but this turns off alpha pixmaps on Unix.
398 QGuiApplication::setDesktopSettingsAware(true);
399 }
400
401 void tst_QPainter::cleanupTestCase()
402 {
403 QFile::remove(QLatin1String("dest.png"));
404 QFile::remove(QLatin1String("expected.png"));
405 QFile::remove(QLatin1String("foo.png"));
406 }
407
408 #ifndef QT_NO_WIDGETS
409 void tst_QPainter::drawPixmap_comp_data()
410 {
411 if (QGuiApplication::primaryScreen()->depth() < 24)
412 QSKIP("Test only works on 32 bit displays");
413
414 QTest::addColumn<uint>("dest");
415 QTest::addColumn<uint>("source");
416
417 QTest::newRow("0% on 0%, 1") << 0x00000000u<< 0x00000000u;
418 QTest::newRow("0% on 0%, 2") << 0x00007fffu << 0x00ff007fu;
419
420 QTest::newRow("50% on a=0%") << 0x00000000u << 0x7fff0000u;
421 QTest::newRow("50% on a=50%") << 0x7f000000u << 0x7fff0000u;
422 QTest::newRow("50% on deadbeef") << 0xdeafbeefu << 0x7fff0000u;
423 QTest::newRow("deadbeef on a=0%") << 0x00000000u << 0xdeadbeefu;
424 QTest::newRow("deadbeef on a=50%") << 0x7f000000u << 0xdeadbeefu;
425 QTest::newRow("50% blue on 50% red") << 0x7fff0000u << 0x7f0000ffu;
426 QTest::newRow("50% blue on 50% green") << 0x7f00ff00u << 0x7f0000ffu;
427 QTest::newRow("50% red on 50% green") << 0x7f00ff00u << 0x7fff0000u;
428 QTest::newRow("0% on 50%") << 0x7fff00ffu << 0x00ffffffu;
429 QTest::newRow("100% on deadbeef") << 0xdeafbeefu << 0xffabcdefu;
430 QTest::newRow("100% on a=0%") << 0x00000000u << 0xffabcdefu;
431 }
432
433 QRgb qt_compose_alpha(QRgb source, QRgb dest)
434 {
435 int r1 = qRed(dest), g1 = qGreen(dest), b1 = qBlue(dest), a1 = qAlpha(dest);
436 int r2 = qRed(source), g2 = qGreen(source), b2 = qBlue(source), a2 = qAlpha(source);
437
438 int alpha = qMin(a2 + ((255 - a2) * a1 + 127) / 255, 255);
439 if (alpha == 0)
440 return qRgba(0, 0, 0, 0);
441
442 return qRgba(
443 qMin((r2 * a2 + (255 - a2) * r1 * a1 / 255) / alpha, 255),
444 qMin((g2 * a2 + (255 - a2) * g1 * a1 / 255) / alpha, 255),
445 qMin((b2 * a2 + (255 - a2) * b1 * a1 / 255) / alpha, 255),
446 alpha);
447 }
448
449 /* Tests that drawing masked pixmaps works
450 */
451 void tst_QPainter::drawPixmap_comp()
452 {
453 #ifdef Q_OS_MAC
454 QSKIP("Mac has other ideas about alpha composition");
455 #endif
456 QFETCH(uint, dest);
457 QFETCH(uint, source);
458
459 QRgb expected = qt_compose_alpha(source, dest);
460
461 QColor c1(qRed(dest), qGreen(dest), qBlue(dest), qAlpha(dest));
462 QColor c2(qRed(source), qGreen(source), qBlue(source), qAlpha(source));
463
464 QPixmap destPm(10, 10), srcPm(10, 10);
465 destPm.fill(c1);
466 srcPm.fill(c2);
467
468 QPainter p(&destPm);
469 p.drawPixmap(0, 0, srcPm);
470 p.end();
471
472 QImage result = destPm.toImage().convertToFormat(QImage::Format_ARGB32);
473 bool different = false;
474 for (int y=0; y<result.height(); ++y)
475 for (int x=0; x<result.width(); ++x) {
476 bool diff;
477 if (qAlpha(expected) == 0) {
478 diff = qAlpha(result.pixel(x, y)) != 0;
479 } else {
480 // Compensate for possible roundoff / platform fudge
481 int off = 1;
482 QRgb pix = result.pixel(x, y);
483 diff = (qAbs(qRed(pix) - qRed(expected)) > off)
484 || (qAbs(qGreen(pix) - qGreen(expected)) > off)
485 || (qAbs(qBlue(pix) - qBlue(expected)) > off)
486 || (qAbs(qAlpha(pix) - qAlpha(expected)) > off);
487 }
488 if (diff && !different)
489 qDebug( "Different at %d,%d pixel [%d,%d,%d,%d] expected [%d,%d,%d,%d]", x, y,
490 qRed(result.pixel(x, y)), qGreen(result.pixel(x, y)),
491 qBlue(result.pixel(x, y)), qAlpha(result.pixel(x, y)),
492 qRed(expected), qGreen(expected), qBlue(expected), qAlpha(expected));
493 different |= diff;
494 }
495
496 QVERIFY(!different);
497 }
498 #endif
499
500 void tst_QPainter::saveAndRestore_data()
501 {
502 QTest::addColumn<QFont>("font");
503 QTest::addColumn<QPen>("pen");
504 QTest::addColumn<QBrush>("brush");
505 QTest::addColumn<QColor>("backgroundColor");
506 QTest::addColumn<int>("backgroundMode");
507 QTest::addColumn<QPoint>("brushOrigin");
508 QTest::addColumn<QRegion>("clipRegion");
509 QTest::addColumn<QRect>("window");
510 QTest::addColumn<QRect>("viewport");
511
512 QPixmap pixmap(1, 1);
513 QPainter p(&pixmap);
514 QFont font = p.font();
515 QPen pen = p.pen();
516 QBrush brush = p.brush();
517 QColor backgroundColor = p.background().color();
518 Qt::BGMode backgroundMode = p.backgroundMode();
519 QPoint brushOrigin = p.brushOrigin();
520 QRegion clipRegion = p.clipRegion();
521 QRect window = p.window();
522 QRect viewport = p.viewport();
523
524 QTest::newRow("Original") << font << pen << brush << backgroundColor << int(backgroundMode)
525 << brushOrigin << clipRegion << window << viewport;
526
527 QFont font2 = font;
528 font2.setPointSize( 24 );
529 QTest::newRow("Modified font.pointSize, brush, backgroundColor, backgroundMode")
530 << font2 << pen << QBrush(Qt::red) << QColor(Qt::blue) << int(Qt::TransparentMode)
531 << brushOrigin << clipRegion << window << viewport;
532
533 font2 = font;
534 font2.setPixelSize( 20 );
535 QTest::newRow("Modified font.pixelSize, brushOrigin, pos")
536 << font2 << pen << brush << backgroundColor << int(backgroundMode)
537 << QPoint( 50, 32 ) << clipRegion << window << viewport;
538
539 QTest::newRow("Modified clipRegion, window, viewport")
540 << font << pen << brush << backgroundColor << int(backgroundMode)
541 << brushOrigin << clipRegion.subtracted(QRect(10,10,50,30))
542 << QRect(-500, -500, 500, 500 ) << QRect( 0, 0, 50, 50 );
543 }
544
545 void tst_QPainter::saveAndRestore()
546 {
547 QFETCH( QFont, font );
548 QFETCH( QPen, pen );
549 QFETCH( QBrush, brush );
550 QFETCH( QColor, backgroundColor );
551 QFETCH( int, backgroundMode );
552 QFETCH( QPoint, brushOrigin );
553 QFETCH( QRegion, clipRegion );
554 QFETCH( QRect, window );
555 QFETCH( QRect, viewport );
556
557 QPixmap pixmap(1, 1);
558 QPainter painter(&pixmap);
559
560 QFont font_org = painter.font();
561 QPen pen_org = painter.pen();
562 QBrush brush_org = painter.brush();
563 QColor backgroundColor_org = painter.background().color();
564 Qt::BGMode backgroundMode_org = painter.backgroundMode();
565 QPoint brushOrigin_org = painter.brushOrigin();
566 QRegion clipRegion_org = painter.clipRegion();
567 QRect window_org = painter.window();
568 QRect viewport_org = painter.viewport();
569
570 painter.save();
571 painter.setFont( font );
572 painter.setPen( QPen(pen) );
573 painter.setBrush( brush );
574 painter.setBackground( backgroundColor );
575 painter.setBackgroundMode( (Qt::BGMode)backgroundMode );
576 painter.setBrushOrigin( brushOrigin );
577 painter.setClipRegion( clipRegion );
578 painter.setWindow( window );
579 painter.setViewport( viewport );
580 painter.restore();
581
582 QCOMPARE( painter.font(), font_org );
583 QCOMPARE( painter.font().pointSize(), font_org.pointSize() );
584 QCOMPARE( painter.font().pixelSize(), font_org.pixelSize() );
585 QCOMPARE( painter.pen(), pen_org );
586 QCOMPARE( painter.brush(), brush_org );
587 QCOMPARE( painter.background().color(), backgroundColor_org );
588 QCOMPARE( painter.backgroundMode(), backgroundMode_org );
589 QCOMPARE( painter.brushOrigin(), brushOrigin_org );
590 QCOMPARE( painter.clipRegion(), clipRegion_org );
591 QCOMPARE( painter.window(), window_org );
592 QCOMPARE( painter.viewport(), viewport_org );
593 }
594
595 /*
596 Helper functions
597 */
598
599 QColor tst_QPainter::baseColor( int k, int intensity )
600 {
601 int r = ( k & 1 ) * intensity;
602 int g = ( (k>>1) & 1 ) * intensity;
603 int b = ( (k>>2) & 1 ) * intensity;
604 return QColor( r, g, b );
605 }
606
607 QImage tst_QPainter::getResImage( const QString &dir, const QString &addition, const QString &extension )
608 {
609 QImage res;
610 QString resFilename = dir + QLatin1String("/res_") + addition + QLatin1Char('.') + extension;
611 if ( !res.load( resFilename ) ) {
612 QWARN(QString("Could not load result data %s %1").arg(resFilename).toLatin1());
613 return QImage();
614 }
615 return res;
616 }
617
618 QBitmap tst_QPainter::getBitmap( const QString &dir, const QString &filename, bool mask )
619 {
620 QBitmap bm;
621 QString bmFilename = dir + QLatin1Char('/') + filename + QLatin1String(".xbm");
622 if ( !bm.load( bmFilename ) ) {
623 QWARN(QString("Could not load bitmap '%1'").arg(bmFilename).toLatin1());
624 return QBitmap();
625 }
626 if ( mask ) {
627 QBitmap mask;
628 QString maskFilename = dir + QLatin1Char('/') + filename + QLatin1String("-mask.xbm");
629 if (!mask.load(maskFilename)) {
630 QWARN(QString("Could not load mask '%1'").arg(maskFilename).toLatin1());
631 return QBitmap();
632 }
633 bm.setMask( mask );
634 }
635 return bm;
636 }
637
638 static int getPaintedPixels(const QImage &image, const QColor &background)
639 {
640 uint color = background.rgba();
641
642 int pixels = 0;
643
644 for (int y = 0; y < image.height(); ++y)
645 for (int x = 0; x < image.width(); ++x)
646 if (image.pixel(x, y) != color)
647 ++pixels;
648
649 return pixels;
650 }
651
652 static QRect getPaintedSize(const QImage &image, const QColor &background)
653 {
654 // not the fastest but at least it works..
655 int xmin = image.width() + 1;
656 int xmax = -1;
657 int ymin = image.height() +1;
658 int ymax = -1;
659
660 uint color = background.rgba();
661
662 for ( int y = 0; y < image.height(); ++y ) {
663 for (int x = 0; x < image.width(); ++x) {
664 QRgb pixel = image.pixel( x, y );
665 if (pixel != color && x < xmin)
666 xmin = x;
667 if (pixel != color && x > xmax)
668 xmax = x;
669 if (pixel != color && y < ymin)
670 ymin = y;
671 if (pixel != color && y > ymax)
672 ymax = y;
673 }
674 }
675
676 return QRect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
677 }
678
679 static QRect getPaintedSize(const QPixmap &pm, const QColor &background)
680 {
681 return getPaintedSize(pm.toImage(), background);
682 }
683
684 #ifndef QT_NO_WIDGETS
685
686 #if QT_DEPRECATED_SINCE(5, 13)
687 void tst_QPainter::initFrom()
688 {
689 QWidget *widget = new QWidget();
690 QPalette pal = widget->palette();
691 pal.setColor(QPalette::WindowText, QColor(255, 0, 0));
692 pal.setBrush(QPalette::Window, QColor(0, 255, 0));
693 widget->setPalette(pal);
694 widget->show();
695
696 QFont font = widget->font();
697 font.setPointSize(26);
698 font.setItalic(true);
699 widget->setFont(font);
700
701 QPixmap pm(100, 100);
702 QPainter p(&pm);
703 p.initFrom(widget);
704
705 QCOMPARE(p.font(), font);
706 QCOMPARE(p.pen().color(), pal.color(QPalette::WindowText));
707 QCOMPARE(p.background(), pal.window());
708
709 delete widget;
710 }
711 #endif
712
713 void tst_QPainter::drawBorderPixmap()
714 {
715 QPixmap src(79,79);
716 src.fill(Qt::transparent);
717
718 QImage pm(200,200,QImage::Format_RGB32);
719 QPainter p(&pm);
720 p.setTransform(QTransform(-1,0,0,-1,173.5,153.5));
721 qDrawBorderPixmap(&p, QRect(0,0,75,105), QMargins(39,39,39,39), src, QRect(0,0,79,79), QMargins(39,39,39,39),
722 QTileRules(Qt::StretchTile,Qt::StretchTile), { });
723 }
724 #endif
725
726 void tst_QPainter::drawPixmapFragments()
727 {
728 QPixmap origPixmap(20, 20);
729 QPixmap resPixmap(20, 20);
730 QPainter::PixmapFragment fragments[4] = { {15, 15, 0, 0, 10, 10, 1, 1, 0, 1},
731 { 5, 15, 10, 0, 10, 10, 1, 1, 0, 1},
732 {15, 5, 0, 10, 10, 10, 1, 1, 0, 1},
733 { 5, 5, 10, 10, 10, 10, 1, 1, 0, 1} };
734 {
735 QPainter p(&origPixmap);
736 p.fillRect(0, 0, 10, 10, Qt::red);
737 p.fillRect(10, 0, 10, 10, Qt::green);
738 p.fillRect(0, 10, 10, 10, Qt::blue);
739 p.fillRect(10, 10, 10, 10, Qt::yellow);
740 }
741 {
742 QPainter p(&resPixmap);
743 p.drawPixmapFragments(fragments, 4, origPixmap);
744 }
745
746 QImage origImage = origPixmap.toImage().convertToFormat(QImage::Format_ARGB32);
747 QImage resImage = resPixmap.toImage().convertToFormat(QImage::Format_ARGB32);
748
749 QCOMPARE(resImage.size(), resPixmap.size());
750 QVERIFY(resImage.pixel(5, 5) == origImage.pixel(15, 15));
751 QVERIFY(resImage.pixel(5, 15) == origImage.pixel(15, 5));
752 QVERIFY(resImage.pixel(15, 5) == origImage.pixel(5, 15));
753 QVERIFY(resImage.pixel(15, 15) == origImage.pixel(5, 5));
754
755
756 QPainter::PixmapFragment fragment = QPainter::PixmapFragment::create(QPointF(20, 20), QRectF(30, 30, 2, 2));
757 QCOMPARE(fragment.x, qreal(20));
758 QCOMPARE(fragment.y, qreal(20));
759 QCOMPARE(fragment.sourceLeft, qreal(30));
760 QCOMPARE(fragment.sourceTop, qreal(30));
761 QCOMPARE(fragment.width, qreal(2));
762 QCOMPARE(fragment.height, qreal(2));
763 QCOMPARE(fragment.scaleX, qreal(1));
764 QCOMPARE(fragment.scaleY, qreal(1));
765 QCOMPARE(fragment.rotation, qreal(0));
766 QCOMPARE(fragment.opacity, qreal(1));
767 }
768
769 void tst_QPainter::drawPixmapNegativeScale()
770 {
771 // basePixmap is a 16x16 opaque white square ...
772 QPixmap basePixmap(16, 16);
773 basePixmap.fill(QColor(255, 255, 255, 255));
774 // ... with an opaque black 8x16 left strip
775 QPainter p(&basePixmap);
776 p.setCompositionMode(QPainter::CompositionMode_Source);
777 p.fillRect(QRect(0, 0, 8, 16), QColor(0, 0, 0, 255));
778 p.end();
779
780 // verify one pixel value for each strip
781 QImage baseImage = basePixmap.toImage().convertToFormat(QImage::Format_ARGB32);
782 QVERIFY(baseImage.pixel(4, 8) == qRgba(0, 0, 0, 255));
783 QVERIFY(baseImage.pixel(12, 8) == qRgba(255, 255, 255, 255));
784
785 // resultPixmap is a 16x16 square
786 QPixmap resultPixmap(16, 16);
787
788 // draw basePixmap over resultPixmap using x=-1.0 y=-1.0
789 // scaling factors (i.e. 180° rotation)
790 QPainter p2(&resultPixmap);
791 p2.setCompositionMode(QPainter::CompositionMode_Source);
792 p2.scale(qreal(-1.0), qreal(-1.0));
793 p2.translate(-resultPixmap.width(), -resultPixmap.height());
794 p2.drawPixmap(resultPixmap.rect(), basePixmap);
795 p2.end();
796
797 // check result
798 QImage resultImage = resultPixmap.toImage().convertToFormat(QImage::Format_ARGB32);
799 QVERIFY(resultImage.pixel(4, 8) == qRgba(255, 255, 255, 255)); // left strip is now white
800 QVERIFY(resultImage.pixel(12, 8) == qRgba(0, 0, 0, 255)); // and right strip is now black
801 }
802
803 void tst_QPainter::drawPixmapRounding()
804 {
805 // Just test that we don't assert
806 QBitmap bm(8, 8);
807 QImage out(64, 64, QImage::Format_RGB32);
808 QPainter p(&out);
809 qreal y = 26.499999999999996;
810 p.drawPixmap(QPointF(0, y), bm);
811 }
812
813 void tst_QPainter::drawLine_data()
814 {
815 QTest::addColumn<QLine>("line");
816
817 QTest::newRow("0-45") << QLine(0, 20, 100, 0);
818 QTest::newRow("45-90") << QLine(0, 100, 20, 0);
819 QTest::newRow("90-135") << QLine(20, 100, 0, 0);
820 QTest::newRow("135-180") << QLine(100, 20, 0, 0);
821 QTest::newRow("180-225") << QLine(100, 0, 0, 20);
822 QTest::newRow("225-270") << QLine(20, 0, 0, 100);
823 QTest::newRow("270-315") << QLine(0, 0, 20, 100);
824 QTest::newRow("315-360") << QLine(0, 0, 100, 20);
825 }
826
827 void tst_QPainter::drawLine()
828 {
829 const int offset = 5;
830 const int epsilon = 1; // allow for one pixel difference
831
832 QFETCH(QLine, line);
833
834 QPixmap pixmapUnclipped(qMin(line.x1(), line.x2())
835 + 2*offset + qAbs(line.dx()),
836 qMin(line.y1(), line.y2())
837 + 2*offset + qAbs(line.dy()));
838
839 { // unclipped
840 pixmapUnclipped.fill(Qt::white);
841 QPainter p(&pixmapUnclipped);
842 p.setRenderHint(QPainter::Qt4CompatiblePainting);
843 p.translate(offset, offset);
844 p.setPen(QPen(Qt::black));
845 p.drawLine(line);
846 p.end();
847
848 const QRect painted = getPaintedSize(pixmapUnclipped, Qt::white);
849
850 QLine l = line;
851 l.translate(offset, offset);
852 QVERIFY(qAbs(painted.width() - qAbs(l.dx())) <= epsilon);
853 QVERIFY(qAbs(painted.height() - qAbs(l.dy())) <= epsilon);
854 QVERIFY(qAbs(painted.top() - qMin(l.y1(), l.y2())) <= epsilon);
855 QVERIFY(qAbs(painted.left() - qMin(l.x1(), l.x2())) <= epsilon);
856 QVERIFY(qAbs(painted.bottom() - qMax(l.y1(), l.y2())) <= epsilon);
857 QVERIFY(qAbs(painted.right() - qMax(l.x1(), l.x2())) <= epsilon);
858 }
859
860 QPixmap pixmapClipped(qMin(line.x1(), line.x2())
861 + 2*offset + qAbs(line.dx()),
862 qMin(line.y1(), line.y2())
863 + 2*offset + qAbs(line.dy()));
864 { // clipped
865 const QRect clip = QRect(line.p1(), line.p2()).normalized();
866
867 pixmapClipped.fill(Qt::white);
868 QPainter p(&pixmapClipped);
869 p.setRenderHint(QPainter::Qt4CompatiblePainting);
870 p.translate(offset, offset);
871 p.setClipRect(clip);
872 p.setPen(QPen(Qt::black));
873 p.drawLine(line);
874 p.end();
875 }
876
877 const QImage unclipped = pixmapUnclipped.toImage();
878 const QImage clipped = pixmapClipped.toImage();
879 QCOMPARE(unclipped, clipped);
880 }
881
882 void tst_QPainter::drawLine_clipped()
883 {
884 QImage image(16, 1, QImage::Format_ARGB32_Premultiplied);
885 image.fill(0x0);
886
887 QPainter p(&image);
888 p.setPen(QPen(Qt::black, 10));
889
890 // this should fill the whole image
891 p.drawLine(-1, -1, 17, 1);
892 p.end();
893
894 for (int x = 0; x < 16; ++x)
895 QCOMPARE(image.pixel(x, 0), 0xff000000);
896 }
897
898 void tst_QPainter::drawLine_task121143()
899 {
900 QPen pen(Qt::black);
901
902 QImage image(5, 5, QImage::Format_ARGB32_Premultiplied);
903 image.fill(0xffffffff);
904 QPainter p(&image);
905 p.setPen(pen);
906 p.setRenderHint(QPainter::Qt4CompatiblePainting);
907 p.drawLine(QLine(0, 0+4, 0+4, 0));
908 p.end();
909
910 QImage expected(5, 5, QImage::Format_ARGB32_Premultiplied);
911 expected.fill(0xffffffff);
912 for (int x = 0; x < 5; ++x)
913 expected.setPixel(x, 5-x-1, pen.color().rgb());
914
915 QCOMPARE(image, expected);
916 }
917
918 void tst_QPainter::drawLine_task190634()
919 {
920 QPen pen(Qt::black, 3);
921
922 QImage image(32, 32, QImage::Format_ARGB32_Premultiplied);
923 QPainter p(&image);
924 p.fillRect(0, 0, image.width(), image.height(), Qt::white);
925
926 p.setPen(pen);
927 p.drawLine(QLineF(2, -1.6, 10, -1.6));
928 p.end();
929
930 const uint *data = reinterpret_cast<uint *>(image.bits());
931
932 for (int i = 0; i < image.width() * image.height(); ++i)
933 QCOMPARE(data[i], 0xffffffff);
934
935 p.begin(&image);
936 p.fillRect(0, 0, image.width(), image.height(), Qt::white);
937
938 p.setPen(pen);
939 p.drawLine(QLineF(-1.6, 2, -1.6, 10));
940 p.end();
941
942 data = reinterpret_cast<uint *>(image.bits());
943
944 for (int i = 0; i < image.width() * image.height(); ++i)
945 QCOMPARE(data[i], 0xffffffff);
946
947 p.begin(&image);
948 p.fillRect(0, 0, image.width(), image.height(), Qt::white);
949
950 p.setPen(pen);
951 p.drawLine( QPoint(2,-2), QPoint(3,-5) );
952 p.end();
953
954 data = reinterpret_cast<uint *>(image.bits());
955
956 for (int i = 0; i < image.width() * image.height(); ++i)
957 QCOMPARE(data[i], 0xffffffff);
958 }
959
960 void tst_QPainter::drawLine_task229459()
961 {
962 QImage image(32, 32, QImage::Format_ARGB32_Premultiplied);
963 image.fill(0x0);
964 QPen pen(Qt::black, 64);
965
966 QPainter p(&image);
967 p.setPen(pen);
968 p.drawLine(-8, -8, 10000000, 10000000);
969 p.end();
970
971 QImage expected = image;
972 expected.fill(0xff000000);
973
974 QCOMPARE(image, expected);
975 }
976
977 void tst_QPainter::drawLine_task234891()
978 {
979 QImage img(100, 1000, QImage::Format_ARGB32_Premultiplied);
980 img.fill(0x0);
981 QImage expected = img;
982
983 QPainter p(&img);
984 p.setPen(QPen(QBrush(QColor(255,0,0)), 6));
985 p.drawLine(QPointF(25000,100),QPointF(30000,105));
986
987 p.setPen(QPen(QBrush(QColor(0,255,0)), 6));
988 p.drawLine(QPointF(30000,150),QPointF(35000,155));
989
990 p.setPen(QPen(QBrush(QColor(0,0,255)), 6));
991 p.drawLine(QPointF(65000,200),QPointF(66000,205));
992
993 QCOMPARE(expected, img);
994 }
995
996 void tst_QPainter::drawLine_task216948()
997 {
998 QImage img(1, 10, QImage::Format_ARGB32_Premultiplied);
999 img.fill(0x0);
1000
1001 QPainter p(&img);
1002 QLine line(10, 0, 10, 10);
1003 p.translate(-10, 0);
1004 p.drawLine(line);
1005 p.end();
1006
1007 for (int i = 0; i < img.height(); ++i)
1008 QCOMPARE(img.pixel(0, i), QColor(Qt::black).rgba());
1009 }
1010
1011 void tst_QPainter::drawLineEndPoints()
1012 {
1013 QImage img(256, 256, QImage::Format_ARGB32_Premultiplied);
1014 img.fill(0x0);
1015
1016 QPainter p;
1017 for (int x = 0; x < img.width(); ++x) {
1018 QRgb color = qRgb(x, 0, 0);
1019 p.begin(&img);
1020 p.setPen(QPen(color));
1021 p.drawLine(x, 0, 255 - x, 255);
1022 p.end();
1023 QCOMPARE(img.pixel(x, 0), color);
1024 QCOMPARE(img.pixel(255 - x, 255), color);
1025 }
1026 for (int y = 0; y < img.height(); ++y) {
1027 QRgb color = qRgb(0, y, 0);
1028 p.begin(&img);
1029 p.setPen(QPen(color));
1030 p.drawLine(0, y, 255, 255 - y);
1031 p.end();
1032 QCOMPARE(img.pixel(0, y), color);
1033 QCOMPARE(img.pixel(255, 255 - y), color);
1034 }
1035 for (int x = 0; x < img.width(); ++x) {
1036 QRgb color = qRgb(x, 0, x);
1037 p.begin(&img);
1038 p.setPen(QPen(color));
1039 p.drawLine(x, 255, 255 - x, 0);
1040 p.end();
1041 QCOMPARE(img.pixel(x, 255), color);
1042 QCOMPARE(img.pixel(255 - x, 0), color);
1043 }
1044 for (int y = 0; y < img.height(); ++y) {
1045 QRgb color = qRgb(0, y, y);
1046 p.begin(&img);
1047 p.setPen(QPen(color));
1048 p.drawLine(255, y, 0, 255 - y);
1049 p.end();
1050 QCOMPARE(img.pixel(255, y), color);
1051 QCOMPARE(img.pixel(0, 255 - y), color);
1052 }
1053 }
1054
1055 void tst_QPainter::drawRect()
1056 {
1057 QFETCH(QRect, rect);
1058 QFETCH(bool, usePen);
1059
1060 QPixmap pixmap(rect.x() + rect.width() + 10,
1061 rect.y() + rect.height() + 10);
1062 {
1063 pixmap.fill(Qt::white);
1064 QPainter p(&pixmap);
1065 p.setPen(usePen ? QPen(Qt::black) : QPen(Qt::NoPen));
1066 p.setBrush(Qt::black);
1067 p.drawRect(rect);
1068 p.end();
1069
1070 int increment = usePen ? 1 : 0;
1071
1072 const QRect painted = getPaintedSize(pixmap, Qt::white);
1073 QCOMPARE(painted.width(), rect.width() + increment);
1074 QCOMPARE(painted.height(), rect.height() + increment);
1075 }
1076 }
1077
1078 void tst_QPainter::drawRect2()
1079 {
1080 QImage image(64, 64, QImage::Format_ARGB32_Premultiplied);
1081 {
1082 image.fill(0xffffffff);
1083
1084 QTransform transform(0.368567, 0, 0, 0, 0.368567, 0, 0.0289, 0.0289, 1);
1085
1086 QPainter p(&image);
1087 p.setRenderHint(QPainter::Qt4CompatiblePainting);
1088 p.setTransform(transform);
1089 p.setBrush(Qt::red);
1090 p.setPen(Qt::NoPen);
1091 p.drawRect(QRect(14, 14, 39, 39));
1092 p.end();
1093
1094 QRect fill = getPaintedSize(image, Qt::white);
1095 image.fill(0xffffffff);
1096
1097 p.begin(&image);
1098 p.setRenderHint(QPainter::Qt4CompatiblePainting);
1099 p.setTransform(transform);
1100 p.drawRect(QRect(14, 14, 39, 39));
1101 p.end();
1102
1103 QRect stroke = getPaintedSize(image, Qt::white);
1104 QCOMPARE(stroke.adjusted(1, 1, 0, 0), fill.adjusted(0, 0, 1, 1));
1105 }
1106 }
1107
1108 void tst_QPainter::fillRect_data()
1109 {
1110 QTest::addColumn<QImage::Format>("format");
1111
1112 QTest::newRow("argb32pm") << QImage::Format_ARGB32_Premultiplied;
1113 QTest::newRow("rgba8888pm") << QImage::Format_RGBA8888_Premultiplied;
1114 QTest::newRow("rgba64pm") << QImage::Format_RGBA64_Premultiplied;
1115 }
1116
1117 void tst_QPainter::fillRect()
1118 {
1119 QFETCH(QImage::Format, format);
1120
1121 QImage image(100, 100, format);
1122 image.fill(QColor(0, 0, 0, 0).rgba());
1123
1124 QPainter p(&image);
1125
1126 p.fillRect(0, 0, 100, 100, QColor(255, 0, 0, 127));
1127
1128 // pixmap.save("bla1.png", "PNG");
1129 QCOMPARE(getPaintedSize(image, QColor(0, 0, 0, 0)),
1130 QRect(0, 0, 100, 100));
1131 QCOMPARE(getPaintedSize(image, QColor(127, 0, 0, 127)).isValid(),
1132 QRect().isValid());
1133
1134 p.setCompositionMode(QPainter::CompositionMode_SourceIn);
1135 p.fillRect(50, 0, 50, 100, QColor(0, 0, 255, 255));
1136
1137 QCOMPARE(getPaintedSize(image, QColor(127, 0, 0, 127)),
1138 QRect(50, 0, 50, 100));
1139 QCOMPARE(getPaintedSize(image, QColor(0, 0, 127, 127)),
1140 QRect(0, 0, 50, 100));
1141 }
1142
1143 void tst_QPainter::fillRect2_data()
1144 {
1145 QTest::addColumn<QImage::Format>("format");
1146
1147 QTest::newRow("argb32") << QImage::Format_ARGB32;
1148 QTest::newRow("argb32pm") << QImage::Format_ARGB32_Premultiplied;
1149 QTest::newRow("rgba8888") << QImage::Format_RGBA8888;
1150 QTest::newRow("rgba8888pm") << QImage::Format_RGBA8888_Premultiplied;
1151 QTest::newRow("a2rgb30pm") << QImage::Format_A2RGB30_Premultiplied;
1152 QTest::newRow("a2bgr30pm") << QImage::Format_A2BGR30_Premultiplied;
1153 }
1154
1155 void tst_QPainter::fillRect2()
1156 {
1157 QFETCH(QImage::Format, format);
1158
1159 QRgb background = 0x0;
1160
1161 QImage img(1, 20, format);
1162 img.fill(background);
1163
1164 QPainter p(&img);
1165
1166 QRectF rect(0, 1, 1.2, 18);
1167 p.fillRect(rect, Qt::yellow);
1168
1169 p.end();
1170
1171 QCOMPARE(img.pixel(0, 0), background);
1172 QCOMPARE(img.pixel(0, img.height() - 1), background);
1173
1174 QCOMPARE(img.pixel(0, 1), img.pixel(0, 2));
1175 QCOMPARE(img.pixel(0, img.height() - 2), img.pixel(0, img.height() - 3));
1176 QCOMPARE(img.pixel(0, 1), QColor(Qt::yellow).rgba());
1177 }
1178
1179 void tst_QPainter::fillRect3()
1180 {
1181 QFETCH(QImage::Format, format);
1182
1183 QImage img(1, 1, format);
1184 img.fill(QColor(Qt::black).rgba());
1185
1186 QPainter p(&img);
1187 p.setCompositionMode(QPainter::CompositionMode_Source);
1188 p.fillRect(img.rect(), Qt::transparent);
1189 p.end();
1190
1191 QCOMPARE(img.pixel(0, 0), 0U);
1192 }
1193
1194 void tst_QPainter::fillRect4()
1195 {
1196 QFETCH(QImage::Format, format);
1197
1198 QImage image(100, 1, format);
1199 image.fill(0x0);
1200
1201 QImage expected = image;
1202 expected.fill(0xffffffff);
1203
1204 QPainter p(&image);
1205 p.scale(1.1, 1);
1206 p.setPen(Qt::NoPen);
1207
1208 for (int i = 0; i < 33; ++i)
1209 p.fillRect(QRectF(3 * i, 0, 3, 1), Qt::white);
1210
1211 p.end();
1212
1213 QCOMPARE(image, expected);
1214 }
1215
1216 void tst_QPainter::fillRectNonPremul_data()
1217 {
1218 QTest::addColumn<QImage::Format>("format");
1219 QTest::addColumn<uint>("color");
1220
1221 QTest::newRow("argb32 7f1f3f7f") << QImage::Format_ARGB32 << qRgba(31, 63, 127, 127);
1222 QTest::newRow("rgba8888 7f1f3f7f") << QImage::Format_RGBA8888 << qRgba(31, 63, 127, 127);
1223
1224 QTest::newRow("argb32 3f1f3f7f") << QImage::Format_ARGB32 << qRgba(31, 63, 127, 63);
1225 QTest::newRow("rgba8888 3f1f3f7f") << QImage::Format_RGBA8888 << qRgba(31, 63, 127, 63);
1226
1227 QTest::newRow("argb32 070375f4") << QImage::Format_ARGB32 << qRgba(3, 117, 244, 7);
1228 QTest::newRow("rgba8888 070375f4") << QImage::Format_RGBA8888 << qRgba(3, 117, 244, 7);
1229
1230 QTest::newRow("argb32 0301fe0c") << QImage::Format_ARGB32 << qRgba(1, 254, 12, 3);
1231 QTest::newRow("rgba8888 0301fe0c") << QImage::Format_RGBA8888 << qRgba(1, 254, 12, 3);
1232
1233 QTest::newRow("argb32 01804010") << QImage::Format_ARGB32 << qRgba(128, 64, 32, 1);
1234 QTest::newRow("rgba8888 01804010") << QImage::Format_RGBA8888 << qRgba(128, 64, 32, 1);
1235 }
1236
1237 void tst_QPainter::fillRectNonPremul()
1238 {
1239 QFETCH(QImage::Format, format);
1240 QFETCH(uint, color);
1241
1242 QImage image(1, 1, format);
1243 QRectF rect(0, 0, 1, 1);
1244
1245 // Fill with CompositionMode_SourceOver tests blend_color
1246 image.fill(Qt::transparent);
1247 QPainter painter(&image);
1248 painter.fillRect(rect, QColor::fromRgba(color));
1249 painter.end();
1250
1251 // Fill with CompositionMode_Source tests rectfill.
1252 painter.begin(&image);
1253 painter.setCompositionMode(QPainter::CompositionMode_Source);
1254 painter.fillRect(rect, QColor::fromRgba(color));
1255 painter.end();
1256
1257 QRgb p = image.pixel(0, 0);
1258 QCOMPARE(qAlpha(p), qAlpha(color));
1259 QVERIFY(qAbs(qRed(p)-qRed(color)) <= 1);
1260 QVERIFY(qAbs(qGreen(p)-qGreen(color)) <= 1);
1261 QVERIFY(qAbs(qBlue(p)-qBlue(color)) <= 1);
1262 }
1263
1264 void tst_QPainter::fillRectRGB30_data()
1265 {
1266 QTest::addColumn<uint>("color");
1267
1268 QTest::newRow("17|43|259") << (0xc0000000 | (17 << 20) | (43 << 10) | 259);
1269 QTest::newRow("2|33|444") << (0xc0000000 | (2 << 20) | (33 << 10) | 444);
1270 QTest::newRow("1000|1000|1000") << (0xc0000000 | (1000 << 20) | (1000 << 10) | 1000);
1271 }
1272
1273 void tst_QPainter::fillRectRGB30()
1274 {
1275 QFETCH(uint, color);
1276 QRectF rect(0, 0, 1, 1);
1277
1278 // Fill with CompositionMode_SourceOver tests blend_color
1279 QImage image1(1, 1, QImage::Format_A2BGR30_Premultiplied);
1280 image1.fill(Qt::transparent);
1281 QPainter painter(&image1);
1282 painter.fillRect(rect, QColor::fromRgba64(qConvertA2rgb30ToRgb64<PixelOrderBGR>(color)));
1283 painter.end();
1284
1285 uint pixel1 = ((const uint*)(image1.bits()))[0];
1286 QCOMPARE(pixel1, color);
1287
1288 // Fill with CompositionMode_Source tests rectfill.
1289 QImage image2(1, 1, QImage::Format_RGB30);
1290 painter.begin(&image2);
1291 painter.setCompositionMode(QPainter::CompositionMode_Source);
1292 painter.fillRect(rect, QColor::fromRgba64(qConvertA2rgb30ToRgb64<PixelOrderRGB>(color)));
1293 painter.end();
1294
1295 uint pixel2 = ((const uint*)(image2.bits()))[0];
1296 QCOMPARE(pixel2, color);
1297 }
1298
1299 void tst_QPainter::drawPath_data()
1300 {
1301 QTest::addColumn<QPainterPath>("path");
1302 QTest::addColumn<QRect>("expectedBounds");
1303 QTest::addColumn<int>("expectedPixels");
1304
1305 {
1306 QPainterPath p;
1307 p.addRect(2, 2, 10, 10);
1308 QTest::newRow("int-aligned rect") << p << QRect(2, 2, 10, 10) << 10 * 10;
1309 }
1310
1311 {
1312 QPainterPath p;
1313 p.addRect(2.25, 2.25, 10, 10);
1314 QTest::newRow("non-aligned rect") << p << QRect(3, 3, 10, 10) << 10 * 10;
1315 }
1316
1317 {
1318 QPainterPath p;
1319 p.addRect(2.25, 2.25, 10.5, 10.5);
1320 QTest::newRow("non-aligned rect 2") << p << QRect(3, 3, 10, 10) << 10 * 10;
1321 }
1322
1323 {
1324 QPainterPath p;
1325 p.addRect(2.5, 2.5, 10, 10);
1326 QTest::newRow("non-aligned rect 3") << p << QRect(3, 3, 10, 10) << 10 * 10;
1327 }
1328
1329 {
1330 QPainterPath p;
1331 p.addRect(2, 2, 10, 10);
1332 p.addRect(4, 4, 6, 6);
1333 QTest::newRow("rect-in-rect") << p << QRect(2, 2, 10, 10) << 10 * 10 - 6 * 6;
1334 }
1335
1336 {
1337 QPainterPath p;
1338 p.addRect(2, 2, 10, 10);
1339 p.addRect(4, 4, 6, 6);
1340 p.addRect(6, 6, 2, 2);
1341 QTest::newRow("rect-in-rect-in-rect") << p << QRect(2, 2, 10, 10) << 10 * 10 - 6 * 6 + 2 * 2;
1342 }
1343 }
1344
1345 void tst_QPainter::drawPath()
1346 {
1347 QFETCH(QPainterPath, path);
1348 QFETCH(QRect, expectedBounds);
1349 QFETCH(int, expectedPixels);
1350
1351 const int offset = 2;
1352
1353 QImage image(expectedBounds.width() + 2 * offset, expectedBounds.height() + 2 * offset,
1354 QImage::Format_ARGB32_Premultiplied);
1355 image.fill(QColor(Qt::white).rgb());
1356
1357 QPainter p(&image);
1358 p.setRenderHint(QPainter::Qt4CompatiblePainting);
1359 p.setPen(Qt::NoPen);
1360 p.setBrush(Qt::black);
1361 p.translate(offset - expectedBounds.left(), offset - expectedBounds.top());
1362 p.drawPath(path);
1363 p.end();
1364
1365 const QRect paintedBounds = getPaintedSize(image, Qt::white);
1366
1367 QCOMPARE(paintedBounds.x(), offset);
1368 QCOMPARE(paintedBounds.y(), offset);
1369 QCOMPARE(paintedBounds.width(), expectedBounds.width());
1370 QCOMPARE(paintedBounds.height(), expectedBounds.height());
1371
1372 if (expectedPixels != -1) {
1373 int paintedPixels = getPaintedPixels(image, Qt::white);
1374 QCOMPARE(paintedPixels, expectedPixels);
1375 }
1376 }
1377
1378 void tst_QPainter::drawPath2()
1379 {
1380 const int w = 50;
1381
1382 for (int h = 5; h < 200; ++h) {
1383 QPainterPath p1, p2;
1384 p1.lineTo(w, 0);
1385 p1.lineTo(w, h);
1386
1387 p2.lineTo(w, h);
1388 p2.lineTo(0, h);
1389
1390 const int offset = 2;
1391
1392 QImage image(w + 2 * offset, h + 2 * offset,
1393 QImage::Format_ARGB32_Premultiplied);
1394 image.fill(QColor(Qt::white).rgb());
1395
1396 QPainter p(&image);
1397 p.setPen(Qt::NoPen);
1398 p.setBrush(Qt::black);
1399 p.translate(offset, offset);
1400 p.drawPath(p1);
1401 p.end();
1402
1403 const int p1Pixels = getPaintedPixels(image, Qt::white);
1404
1405 image.fill(QColor(Qt::white).rgb());
1406 p.begin(&image);
1407 p.setPen(Qt::NoPen);
1408 p.setBrush(Qt::black);
1409 p.translate(offset, offset);
1410 p.drawPath(p2);
1411 p.end();
1412
1413 const int p2Pixels = getPaintedPixels(image, Qt::white);
1414
1415 QCOMPARE(p1Pixels + p2Pixels, w * h);
1416 }
1417 }
1418
1419 void tst_QPainter::drawPath3()
1420 {
1421 QImage imgA(100, 100, QImage::Format_RGB32);
1422 imgA.fill(0xffffff);
1423 QImage imgB = imgA;
1424
1425 QPainterPath path;
1426 for (int y = 0; y < imgA.height(); ++y) {
1427 for (int x = 0; x < imgA.width(); ++x) {
1428 if ((x + y) & 1) {
1429 imgA.setPixel(x, y, 0);
1430 path.addRect(x, y, 1, 1);
1431 }
1432 }
1433 }
1434
1435 QPainter p(&imgB);
1436 p.setPen(Qt::NoPen);
1437 p.setBrush(Qt::black);
1438
1439 p.drawPath(path);
1440 p.end();
1441
1442 QCOMPARE(imgA, imgB);
1443
1444 imgA.invertPixels();
1445 imgB.fill(0xffffff);
1446
1447 p.begin(&imgB);
1448 p.setPen(Qt::NoPen);
1449 p.setBrush(Qt::black);
1450
1451 QRectF rect(0, 0, imgA.width(), imgA.height());
1452 path.addRect(rect.adjusted(-10, -10, 10, 10));
1453 p.drawPath(path);
1454 p.end();
1455
1456 QCOMPARE(imgA, imgB);
1457
1458 path.setFillRule(Qt::WindingFill);
1459 imgB.fill(0xffffff);
1460
1461 p.begin(&imgB);
1462 p.setPen(Qt::NoPen);
1463 p.setBrush(Qt::black);
1464 QRect clip = rect.adjusted(10, 10, -10, -10).toRect();
1465 p.setClipRect(clip);
1466 p.drawPath(path);
1467 p.end();
1468
1469 QCOMPARE(getPaintedPixels(imgB, Qt::white), clip.width() * clip.height());
1470 }
1471
1472 void tst_QPainter::drawEllipse_data()
1473 {
1474 QTest::addColumn<QSize>("size");
1475 QTest::addColumn<bool>("usePen");
1476
1477 // The current drawEllipse algorithm (drawEllipse_midpoint_i in
1478 // qpaintengine_raster.cpp) draws ellipses that are too wide if the
1479 // ratio between width and hight is too large/small (task 114874). Those
1480 // ratios are therefore currently avoided.
1481 for (int w = 10; w < 128; w += 7) {
1482 const QByteArray wB = QByteArray::number(w);
1483 for (int h = w/2; h < qMin(2*w, 128); h += 13) {
1484 const QByteArray sB = wB + 'x' + QByteArray::number(h);
1485 QTest::newRow((sB + " with pen").constData()) << QSize(w, h) << true;
1486 QTest::newRow((sB + " no pen").constData()) << QSize(w, h) << false;
1487 }
1488 }
1489 }
1490
1491 void tst_QPainter::drawEllipse()
1492 {
1493 QFETCH(QSize, size);
1494 QFETCH(bool, usePen);
1495
1496 const int offset = 10;
1497 QRect rect(QPoint(offset, offset), size);
1498
1499 QImage image(size.width() + 2 * offset, size.height() + 2 * offset,
1500 QImage::Format_ARGB32_Premultiplied);
1501 image.fill(QColor(Qt::white).rgb());
1502
1503 QPainter p(&image);
1504 p.setPen(usePen ? QPen(Qt::black) : QPen(Qt::NoPen));
1505 p.setBrush(Qt::black);
1506 p.drawEllipse(rect);
1507 p.end();
1508
1509 QPixmap pixmap = QPixmap::fromImage(image);
1510
1511 const QRect painted = getPaintedSize(pixmap, Qt::white);
1512
1513 QCOMPARE(painted.x(), rect.x());
1514 QCOMPARE(painted.y(), rect.y() + (usePen ? 0 : 1));
1515 QCOMPARE(painted.width(), size.width() + (usePen ? 1 : 0));
1516 QCOMPARE(painted.height(), size.height() + (usePen ? 1 : -1));
1517 }
1518
1519 void tst_QPainter::drawClippedEllipse_data()
1520 {
1521 QTest::addColumn<QRect>("rect");
1522
1523 for (int w = 20; w < 128; w += 7) {
1524 const QByteArray wB = QByteArray::number(w);
1525 for (int h = w/2; h < qMin(2*w, 128); h += 13) {
1526 const QByteArray sB = wB + 'x' + QByteArray::number(h);
1527 QTest::newRow((sB + " top").constData()) << QRect(0, -h/2, w, h);
1528 QTest::newRow((sB + " topright").constData()) << QRect(w/2, -h/2, w, h);
1529 QTest::newRow((sB + " right").constData()) << QRect(w/2, 0, w, h);
1530 QTest::newRow((sB + " bottomright").constData()) << QRect(w/2, h/2, w, h);
1531 QTest::newRow((sB + " bottom").constData()) << QRect(0, h/2, w, h);
1532 QTest::newRow((sB + " bottomleft").constData()) << QRect(-w/2, h/2, w, h);
1533 QTest::newRow((sB + " left").constData()) << QRect(-w/2, 0, w, h);
1534 QTest::newRow((sB + " topleft").constData()) << QRect(-w/2, -h/2, w, h);
1535 }
1536 }
1537 }
1538
1539 void tst_QPainter::drawClippedEllipse()
1540 {
1541 QFETCH(QRect, rect);
1542 if (sizeof(qreal) != sizeof(double))
1543 QSKIP("Test only works for qreal==double");
1544 QImage image(rect.width() + 1, rect.height() + 1,
1545 QImage::Format_ARGB32_Premultiplied);
1546 QRect expected = QRect(rect.x(), rect.y(), rect.width()+1, rect.height()+1)
1547 & QRect(0, 0, image.width(), image.height());
1548
1549
1550 image.fill(QColor(Qt::white).rgb());
1551 QPainter p(&image);
1552 p.drawEllipse(rect);
1553 p.end();
1554
1555 QPixmap pixmap = QPixmap::fromImage(image);
1556 const QRect painted = getPaintedSize(pixmap, Qt::white);
1557
1558 QCOMPARE(painted.x(), expected.x());
1559 QCOMPARE(painted.y(), expected.y());
1560 QCOMPARE(painted.width(), expected.width());
1561 QCOMPARE(painted.height(), expected.height());
1562
1563 }
1564
1565 #if QT_DEPRECATED_SINCE(5, 13)
1566 void tst_QPainter::drawRoundRect()
1567 {
1568 QFETCH(QRect, rect);
1569 QFETCH(bool, usePen);
1570
1571 #ifdef Q_OS_MAC
1572 if (QTest::currentDataTag() == QByteArray("rect(6, 12, 3, 14) with pen") ||
1573 QTest::currentDataTag() == QByteArray("rect(6, 17, 3, 25) with pen") ||
1574 QTest::currentDataTag() == QByteArray("rect(10, 6, 10, 3) with pen") ||
1575 QTest::currentDataTag() == QByteArray("rect(10, 12, 10, 14) with pen") ||
1576 QTest::currentDataTag() == QByteArray("rect(13, 45, 17, 80) with pen") ||
1577 QTest::currentDataTag() == QByteArray("rect(13, 50, 17, 91) with pen") ||
1578 QTest::currentDataTag() == QByteArray("rect(17, 6, 24, 3) with pen") ||
1579 QTest::currentDataTag() == QByteArray("rect(24, 12, 38, 14) with pen"))
1580 QSKIP("The Mac paint engine is off-by-one on certain rect sizes");
1581 #endif
1582 QPixmap pixmap(rect.x() + rect.width() + 10,
1583 rect.y() + rect.height() + 10);
1584 {
1585 pixmap.fill(Qt::white);
1586 QPainter p(&pixmap);
1587 p.setRenderHint(QPainter::Qt4CompatiblePainting);
1588 p.setPen(usePen ? QPen(Qt::black) : QPen(Qt::NoPen));
1589 p.setBrush(Qt::black);
1590 p.drawRoundRect(rect);
1591 p.end();
1592
1593 int increment = usePen ? 1 : 0;
1594
1595 const QRect painted = getPaintedSize(pixmap, Qt::white);
1596 QCOMPARE(painted.width(), rect.width() + increment);
1597 QCOMPARE(painted.height(), rect.height() + increment);
1598 }
1599 }
1600 #endif
1601
1602 void tst_QPainter::drawRoundedRect()
1603 {
1604 QFETCH(QRect, rect);
1605 QFETCH(bool, usePen);
1606
1607 #ifdef Q_OS_DARWIN
1608 if (QTest::currentDataTag() == QByteArray("rect(6, 12, 3, 14) with pen") ||
1609 QTest::currentDataTag() == QByteArray("rect(6, 17, 3, 25) with pen") ||
1610 QTest::currentDataTag() == QByteArray("rect(10, 6, 10, 3) with pen") ||
1611 QTest::currentDataTag() == QByteArray("rect(10, 12, 10, 14) with pen") ||
1612 QTest::currentDataTag() == QByteArray("rect(13, 45, 17, 80) with pen") ||
1613 QTest::currentDataTag() == QByteArray("rect(13, 50, 17, 91) with pen") ||
1614 QTest::currentDataTag() == QByteArray("rect(17, 6, 24, 3) with pen") ||
1615 QTest::currentDataTag() == QByteArray("rect(24, 12, 38, 14) with pen"))
1616 QSKIP("The Mac paint engine is off-by-one on certain rect sizes");
1617 #endif
1618 QPixmap pixmap(rect.x() + rect.width() + 10,
1619 rect.y() + rect.height() + 10);
1620 {
1621 pixmap.fill(Qt::white);
1622 QPainter p(&pixmap);
1623 p.setRenderHint(QPainter::Qt4CompatiblePainting);
1624 p.setPen(usePen ? QPen(Qt::black) : QPen(Qt::NoPen));
1625 p.setBrush(Qt::black);
1626 p.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
1627 p.end();
1628
1629 int increment = usePen ? 1 : 0;
1630
1631 const QRect painted = getPaintedSize(pixmap, Qt::white);
1632 QCOMPARE(painted.width(), rect.width() + increment);
1633 QCOMPARE(painted.height(), rect.height() + increment);
1634 }
1635 }
1636
1637 void tst_QPainter::qimageFormats_data()
1638 {
1639 QTest::addColumn<QImage::Format>("format");
1640 QTest::newRow("QImage::Format_RGB32") << QImage::Format_RGB32;
1641 QTest::newRow("QImage::Format_ARGB32") << QImage::Format_ARGB32;
1642 QTest::newRow("QImage::Format_ARGB32_Premultiplied") << QImage::Format_ARGB32_Premultiplied;
1643 QTest::newRow("QImage::Format_RGB16") << QImage::Format_RGB16;
1644 QTest::newRow("Qimage::Format_ARGB8565_Premultiplied") << QImage::Format_ARGB8565_Premultiplied;
1645 QTest::newRow("Qimage::Format_RGB666") << QImage::Format_RGB666;
1646 QTest::newRow("Qimage::Format_RGB555") << QImage::Format_RGB555;
1647 QTest::newRow("Qimage::Format_ARGB8555_Premultiplied") << QImage::Format_ARGB8555_Premultiplied;
1648 QTest::newRow("Qimage::Format_RGB888") << QImage::Format_RGB888;
1649 QTest::newRow("Qimage::Format_BGR888") << QImage::Format_BGR888;
1650 QTest::newRow("Qimage::Format_A2RGB30_Premultiplied") << QImage::Format_A2RGB30_Premultiplied;
1651 QTest::newRow("Qimage::Format_RGB30") << QImage::Format_RGB30;
1652 }
1653
1654 /*
1655 Tests that QPainter can paint on various QImage formats.
1656 */
1657 void tst_QPainter::qimageFormats()
1658 {
1659 QFETCH(QImage::Format, format);
1660
1661 const QSize size(100, 100);
1662 QImage image(size, format);
1663 image.fill(0);
1664
1665 const QColor testColor(Qt::red);
1666 QPainter p(&image);
1667 QVERIFY(p.isActive());
1668 p.setBrush(QBrush(testColor));
1669 p.drawRect(QRect(QPoint(0,0), size));
1670 QCOMPARE(image.pixel(50, 50), testColor.rgb());
1671 }
1672
1673 void tst_QPainter::fillData()
1674 {
1675 QTest::addColumn<QRect>("rect");
1676 QTest::addColumn<bool>("usePen");
1677
1678 for (int w = 3; w < 50; w += 7) {
1679 const QByteArray wB = QByteArray::number(w);
1680 for (int h = 3; h < 50; h += 11) {
1681 int x = w/2 + 5;
1682 int y = h/2 + 5;
1683 const QByteArray rB = "rect(" + QByteArray::number(x) + ", " + QByteArray::number(y)
1684 + ", " + QByteArray::number(w) + ", " + QByteArray::number(h) + ')';
1685 QTest::newRow((rB + " with pen").constData()) << QRect(x, y, w, h) << true;
1686 QTest::newRow((rB + " no pen").constData()) << QRect(x, y, w, h) << false;
1687 }
1688 }
1689 }
1690
1691 /*
1692 Test that drawline works properly after setWindow has been called.
1693 */
1694 void tst_QPainter::setWindow()
1695 {
1696 QPixmap pixmap(600, 600);
1697 pixmap.fill(QColor(Qt::white));
1698
1699 QPainter painter(&pixmap);
1700 painter.setRenderHint(QPainter::Qt4CompatiblePainting);
1701 painter.setWindow(0, 0, 3, 3);
1702 painter.drawLine(1, 1, 2, 2);
1703
1704 const QRect painted = getPaintedSize(pixmap, Qt::white);
1705 QVERIFY(195 < painted.y() && painted.y() < 205); // correct value is around 200
1706 QVERIFY(195 < painted.height() && painted.height() < 205); // correct value is around 200
1707 }
1708
1709 void tst_QPainter::combinedMatrix()
1710 {
1711 QPixmap pm(64, 64);
1712
1713 QPainter p(&pm);
1714 p.setWindow(0, 0, 1, 1);
1715 p.setViewport(32, 0, 32, 32);
1716
1717 p.translate(0.5, 0.5);
1718
1719 QTransform ct = p.combinedTransform();
1720 #if QT_DEPRECATED_SINCE(5, 13)
1721 QT_WARNING_PUSH
1722 QT_WARNING_DISABLE_DEPRECATED
1723 QMatrix cm = p.combinedMatrix();
1724 QCOMPARE(cm, ct.toAffine());
1725 QT_WARNING_POP
1726 #endif
1727
1728 QPointF pt = QPointF(0, 0) * ct.toAffine();
1729
1730 QCOMPARE(pt.x(), 48.0);
1731 QCOMPARE(pt.y(), 16.0);
1732 }
1733
1734 void tst_QPainter::textOnTransparentImage()
1735 {
1736 bool foundPixel = false;
1737 QImage image(10, 10, QImage::Format_ARGB32_Premultiplied);
1738 image.fill(qRgba(0, 0, 0, 0)); // transparent
1739 {
1740 QPainter painter(&image);
1741 painter.setPen(QColor(255, 255, 255));
1742 painter.drawText(0, 10, "W");
1743 }
1744 for (int x = 0; x < image.width(); ++x)
1745 for (int y = 0; y < image.height(); ++y)
1746 if (image.pixel(x, y) != 0)
1747 foundPixel = true;
1748 QVERIFY(foundPixel);
1749 }
1750
1751 void tst_QPainter::renderHints()
1752 {
1753 QImage img(1, 1, QImage::Format_RGB32);
1754
1755 QPainter p(&img);
1756
1757 // Turn off all...
1758 p.setRenderHints(QPainter::RenderHints(0xffffffff), false);
1759 QCOMPARE(p.renderHints(), QPainter::RenderHints{});
1760
1761 // Single set/get
1762 p.setRenderHint(QPainter::Antialiasing);
1763 QVERIFY(p.renderHints() & QPainter::Antialiasing);
1764
1765 p.setRenderHint(QPainter::Antialiasing, false);
1766 QVERIFY(!(p.renderHints() & QPainter::Antialiasing));
1767
1768 // Multi set/get
1769 p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
1770 QVERIFY(p.renderHints() & (QPainter::Antialiasing | QPainter::SmoothPixmapTransform));
1771
1772 p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, false);
1773 QVERIFY(!(p.renderHints() & (QPainter::Antialiasing | QPainter::SmoothPixmapTransform)));
1774 }
1775
1776 int countPixels(const QImage &img, const QRgb &color)
1777 {
1778 int count = 0;
1779 for (int y = 0; y < img.height(); ++y) {
1780 for (int x = 0; x < img.width(); ++x) {
1781 count += ((img.pixel(x, y) & 0xffffff) == color);
1782 }
1783 }
1784 return count;
1785 }
1786
1787 template <typename T>
1788 void testClipping(QImage &img)
1789 {
1790 QPainterPath a, b;
1791 a.addRect(QRect(2, 2, 4, 4));
1792 b.addRect(QRect(4, 4, 4, 4));
1793 QPainter p(&img);
1794
1795 p.end();
1796 img.fill(0x0);
1797 p.begin(&img);
1798 p.setClipPath(a);
1799 p.setClipPath(b, Qt::IntersectClip);
1800
1801 p.setClipping(false);
1802 p.setPen(Qt::NoPen);
1803 p.setBrush(QColor(0xff0000));
1804 p.drawRect(T(0, 0, 10, 10));
1805
1806 p.setClipping(true);
1807 p.setBrush(QColor(0x00ff00));
1808 p.drawRect(T(0, 0, 10, 10));
1809
1810 QCOMPARE(countPixels(img, 0xff0000), 96);
1811 QCOMPARE(countPixels(img, 0x00ff00), 4);
1812 }
1813
1814 void tst_QPainter::disableEnableClipping()
1815 {
1816 QImage img(10, 10, QImage::Format_RGB32);
1817
1818 testClipping<QRectF>(img);
1819 testClipping<QRect>(img);
1820 }
1821
1822 void tst_QPainter::setClipRect()
1823 {
1824 QImage img(10, 10, QImage::Format_RGB32);
1825 // simple test to let valgrind check for buffer overflow
1826 {
1827 QPainter p(&img);
1828 p.setClipRect(-10, -10, 100, 100);
1829 p.fillRect(-10, -10, 100, 100, QBrush(QColor(Qt::red)));
1830 }
1831
1832 // rects with negative width/height
1833 {
1834 QPainter p(&img);
1835 p.setClipRect(QRect(10, 10, -10, 10));
1836 QVERIFY(p.clipRegion().isEmpty());
1837 p.setClipRect(QRect(10, 10, 10, -10));
1838 QVERIFY(p.clipRegion().isEmpty());
1839 p.setClipRect(QRectF(10.5, 10.5, -10.5, 10.5));
1840 QVERIFY(p.clipRegion().isEmpty());
1841 p.setClipRect(QRectF(10.5, 10.5, 10.5, -10.5));
1842 QVERIFY(p.clipRegion().isEmpty());
1843 }
1844 }
1845
1846 /*
1847 Verify that the clipping works correctly.
1848 The red outline should be covered by the blue rect on top and left,
1849 while it should be clipped on the right and bottom and thus the red outline be visible
1850
1851 See: QTBUG-83229
1852 */
1853 void tst_QPainter::clipRect()
1854 {
1855 int width = 654;
1856 int height = 480;
1857 QRect rect(0, 0, width, height);
1858
1859 QImage image(width, height, QImage::Format_ARGB32);
1860 QPainter p(&image);
1861 qreal halfWidth = width / 2.0;
1862 qreal halfHeight = height / 2.0;
1863
1864 QRectF clipRect = QRectF(halfWidth - halfWidth / 2.0, halfHeight - halfHeight / 2.0,
1865 halfWidth / 2.0, halfHeight / 2.0);
1866
1867 p.fillRect(rect, Qt::white);
1868 p.setPen(Qt::red);
1869 p.drawRect(clipRect);
1870
1871 p.setClipRect(clipRect, Qt::ReplaceClip);
1872 p.fillRect(rect, Qt::blue);
1873
1874 p.end();
1875
1876 QCOMPARE(image.pixelColor(clipRect.left() + 1, clipRect.top()), QColor(Qt::blue));
1877 QCOMPARE(image.pixelColor(clipRect.left(), clipRect.top() + 1), QColor(Qt::blue));
1878 QCOMPARE(image.pixelColor(clipRect.left() + 1, clipRect.bottom()), QColor(Qt::red));
1879 QCOMPARE(image.pixelColor(clipRect.right(), clipRect.top() + 1), QColor(Qt::red));
1880 }
1881
1882 /*
1883 This tests the two different clipping approaches in QRasterPaintEngine,
1884 one when using a QRegion and one when using a QPainterPath. They should
1885 give equal results.
1886 */
1887 void tst_QPainter::setEqualClipRegionAndPath_data()
1888 {
1889 QTest::addColumn<QSize>("deviceSize");
1890 QTest::addColumn<QRegion>("region");
1891
1892 QTest::newRow("empty") << QSize(100, 100) << QRegion();
1893 QTest::newRow("simple rect") << QSize(100, 100)
1894 << QRegion(QRect(5, 5, 10, 10));
1895
1896 QVector<QRect> rects;
1897 QRegion region;
1898
1899 rects << QRect(5, 5, 10, 10) << QRect(20, 20, 10, 10);
1900 region.setRects(rects.constData(), rects.size());
1901 QTest::newRow("two rects") << QSize(100, 100) << region;
1902
1903 rects.clear();
1904 rects << QRect(5, 5, 10, 10) << QRect(20, 5, 10, 10);
1905 region.setRects(rects.constData(), rects.size());
1906 QTest::newRow("two x-adjacent rects") << QSize(100, 100) << region;
1907
1908 rects.clear();
1909 rects << QRect(0, 0, 10, 100) << QRect(12, 0, 10, 100);
1910 region.setRects(rects.constData(), rects.size());
1911 QTest::newRow("two x-adjacent rects 2") << QSize(100, 100) << region;
1912
1913 rects.clear();
1914 rects << QRect(0, 0, 10, 100) << QRect(12, 0, 10, 100);
1915 region.setRects(rects.constData(), rects.size());
1916 QTest::newRow("two x-adjacent rects 3") << QSize(50, 50) << region;
1917
1918 rects.clear();
1919 rects << QRect(0, 0, 10, 100) << QRect(12, 0, 10, 100);
1920 region.setRects(rects.constData(), rects.size());
1921 QTest::newRow("two x-adjacent rects 4") << QSize(101, 101) << region;
1922
1923 region = QRegion(QRect(0, 0, 200, 200), QRegion::Ellipse);
1924
1925 QTest::newRow("ellipse") << QSize(190, 200) << region;
1926
1927 region ^= QRect(50, 50, 50, 50);
1928 QTest::newRow("ellipse 2") << QSize(200, 200) << region;
1929 }
1930
1931 void tst_QPainter::setEqualClipRegionAndPath()
1932 {
1933 QFETCH(QSize, deviceSize);
1934 QFETCH(QRegion, region);
1935
1936 QPainterPath path;
1937 path.addRegion(region);
1938
1939 QImage img1(deviceSize.width(), deviceSize.height(),
1940 QImage::Format_ARGB32);
1941 QImage img2(deviceSize.width(), deviceSize.height(),
1942 QImage::Format_ARGB32);
1943 img1.fill(0x12345678);
1944 img2.fill(0x12345678);
1945
1946 {
1947 QPainter p(&img1);
1948 p.setClipRegion(region);
1949 p.fillRect(0, 0, img1.width(), img1.height(), QColor(Qt::red));
1950 }
1951 {
1952 QPainter p(&img2);
1953 p.setClipPath(path);
1954 p.fillRect(0, 0, img2.width(), img2.height(), QColor(Qt::red));
1955 }
1956
1957 QCOMPARE(img1, img2);
1958
1959 // rotated
1960 img1.fill(0x12345678);
1961 img2.fill(0x12345678);
1962
1963 {
1964 QPainter p(&img1);
1965 p.rotate(25);
1966 p.setClipRegion(region);
1967 p.fillRect(0, 0, img1.width(), img1.height(), QColor(Qt::red));
1968 }
1969 {
1970 QPainter p(&img2);
1971 p.rotate(25);
1972 p.setClipPath(path);
1973 p.fillRect(0, 0, img2.width(), img2.height(), QColor(Qt::red));
1974 }
1975
1976 QCOMPARE(img1, img2);
1977
1978 img1.fill(0x12345678);
1979 img2.fill(0x12345678);
1980
1981 // simple intersectclip
1982 img1.fill(0x12345678);
1983 img2.fill(0x12345678);
1984 {
1985 QPainter p(&img1);
1986 p.setClipRegion(region);
1987 p.setClipRegion(region, Qt::IntersectClip);
1988 p.fillRect(0, 0, img1.width(), img1.height(), QColor(Qt::red));
1989 }
1990 {
1991 QPainter p(&img2);
1992 p.setClipPath(path);
1993 p.setClipPath(path, Qt::IntersectClip);
1994 p.fillRect(0, 0, img2.width(), img2.height(), QColor(Qt::red));
1995 }
1996 QCOMPARE(img1, img2);
1997
1998 img1.fill(0x12345678);
1999 img2.fill(0x12345678);
2000 {
2001 QPainter p(&img1);
2002 p.setClipPath(path);
2003 p.setClipRegion(region, Qt::IntersectClip);
2004 p.fillRect(0, 0, img1.width(), img1.height(), QColor(Qt::red));
2005 }
2006 {
2007 QPainter p(&img2);
2008 p.setClipRegion(region);
2009 p.setClipPath(path, Qt::IntersectClip);
2010 p.fillRect(0, 0, img2.width(), img2.height(), QColor(Qt::red));
2011 }
2012 QCOMPARE(img1, img2);
2013
2014 }
2015
2016 void tst_QPainter::clippedFillPath_data()
2017 {
2018 QTest::addColumn<QSize>("imageSize");
2019 QTest::addColumn<QPainterPath>("path");
2020 QTest::addColumn<QRect>("clipRect");
2021 QTest::addColumn<QBrush>("brush");
2022 QTest::addColumn<QPen>("pen");
2023
2024 QLinearGradient gradient(QPoint(0, 0), QPoint(100, 100));
2025 gradient.setColorAt(0, Qt::red);
2026 gradient.setColorAt(1, Qt::blue);
2027
2028
2029 QPen pen2(QColor(223, 223, 0, 223));
2030 pen2.setWidth(2);
2031
2032 QPainterPath path;
2033 path.addRect(QRect(15, 15, 50, 50));
2034 QTest::newRow("simple rect 0") << QSize(100, 100) << path
2035 << QRect(15, 15, 49, 49)
2036 << QBrush(Qt::NoBrush)
2037 << QPen(Qt::black);
2038 QTest::newRow("simple rect 1") << QSize(100, 100) << path
2039 << QRect(15, 15, 50, 50)
2040 << QBrush(Qt::NoBrush)
2041 << QPen(Qt::black);
2042 QTest::newRow("simple rect 2") << QSize(100, 100) << path
2043 << QRect(15, 15, 51, 51)
2044 << QBrush(Qt::NoBrush)
2045 << QPen(Qt::black);
2046 QTest::newRow("simple rect 3") << QSize(100, 100) << path
2047 << QRect(15, 15, 51, 51)
2048 << QBrush(QColor(Qt::blue))
2049 << QPen(Qt::NoPen);
2050 QTest::newRow("simple rect 4") << QSize(100, 100) << path
2051 << QRect(15, 15, 51, 51)
2052 << QBrush(gradient)
2053 << pen2;
2054
2055 path = QPainterPath();
2056 path.addEllipse(QRect(15, 15, 50, 50));
2057 QTest::newRow("ellipse 0") << QSize(100, 100) << path
2058 << QRect(15, 15, 49, 49)
2059 << QBrush(Qt::NoBrush)
2060 << QPen(Qt::black);
2061 QTest::newRow("ellipse 1") << QSize(100, 100) << path
2062 << QRect(15, 15, 50, 50)
2063 << QBrush(Qt::NoBrush)
2064 << QPen(Qt::black);
2065 QTest::newRow("ellipse 2") << QSize(100, 100) << path
2066 << QRect(15, 15, 51, 51)
2067 << QBrush(Qt::NoBrush)
2068 << QPen(Qt::black);
2069 QTest::newRow("ellipse 3") << QSize(100, 100) << path
2070 << QRect(15, 15, 51, 51)
2071 << QBrush(QColor(Qt::blue))
2072 << QPen(Qt::NoPen);
2073 QTest::newRow("ellipse 4") << QSize(100, 100) << path
2074 << QRect(15, 15, 51, 51)
2075 << QBrush(gradient)
2076 << pen2;
2077
2078 path = QPainterPath();
2079 path.addRoundedRect(QRect(15, 15, 50, 50), 20, Qt::RelativeSize);
2080 QTest::newRow("round rect 0") << QSize(100, 100) << path
2081 << QRect(15, 15, 49, 49)
2082 << QBrush(Qt::NoBrush)
2083 << QPen(Qt::black);
2084 QTest::newRow("round rect 1") << QSize(100, 100) << path
2085 << QRect(15, 15, 50, 50)
2086 << QBrush(Qt::NoBrush)
2087 << QPen(Qt::black);
2088 QTest::newRow("round rect 2") << QSize(100, 100) << path
2089 << QRect(15, 15, 51, 51)
2090 << QBrush(Qt::NoBrush)
2091 << QPen(Qt::black);
2092 QTest::newRow("round rect 3") << QSize(100, 100) << path
2093 << QRect(15, 15, 51, 51)
2094 << QBrush(QColor(Qt::blue))
2095 << QPen(Qt::NoPen);
2096 QTest::newRow("round rect 4") << QSize(100, 100) << path
2097 << QRect(15, 15, 51, 51)
2098 << QBrush(gradient)
2099 << pen2;
2100
2101 path = QPainterPath();
2102 path.moveTo(15, 50);
2103 path.cubicTo(40, 50, 40, 15, 65, 50);
2104 path.lineTo(15, 50);
2105 QTest::newRow("cubic 0") << QSize(100, 100) << path
2106 << QRect(15, 15, 49, 49)
2107 << QBrush(Qt::NoBrush)
2108 << QPen(Qt::black);
2109 QTest::newRow("cubic 1") << QSize(100, 100) << path
2110 << QRect(15, 15, 50, 50)
2111 << QBrush(Qt::NoBrush)
2112 << QPen(Qt::black);
2113 QTest::newRow("cubic 2") << QSize(100, 100) << path
2114 << QRect(15, 15, 51, 51)
2115 << QBrush(Qt::NoBrush)
2116 << QPen(Qt::black);
2117 QTest::newRow("cubic 3") << QSize(100, 100) << path
2118 << QRect(15, 15, 51, 51)
2119 << QBrush(QColor(Qt::blue))
2120 << QPen(Qt::NoPen);
2121 QTest::newRow("cubic 4") << QSize(100, 100) << path
2122 << QRect(15, 15, 51, 51)
2123 << QBrush(gradient)
2124 << pen2;
2125 }
2126
2127 void tst_QPainter::clippedFillPath()
2128 {
2129 QFETCH(QSize, imageSize);
2130 QFETCH(QPainterPath, path);
2131 QFETCH(QRect, clipRect);
2132 QPainterPath clipPath;
2133 clipPath.addRect(clipRect);
2134 QFETCH(QBrush, brush);
2135 QFETCH(QPen, pen);
2136
2137 const int width = imageSize.width();
2138 const int height = imageSize.height();
2139
2140 QImage clippedRect(width, height, QImage::Format_ARGB32_Premultiplied);
2141 clippedRect.fill(0x12345678);
2142 {
2143 QPainter painter(&clippedRect);
2144 painter.setPen(pen);
2145 painter.setBrush(brush);
2146 painter.setClipRect(clipRect);
2147 painter.drawPath(path);
2148 }
2149
2150 QImage clippedPath(width, height, QImage::Format_ARGB32_Premultiplied);
2151 clippedPath.fill(0x12345678);
2152 {
2153 QPainter painter(&clippedPath);
2154 painter.setPen(pen);
2155 painter.setBrush(brush);
2156 painter.setClipPath(clipPath);
2157 painter.drawPath(path);
2158 }
2159
2160 QCOMPARE(clippedRect, clippedPath);
2161
2162 // repeat with antialiasing
2163
2164 clippedRect.fill(0x12345678);
2165 {
2166 QPainter painter(&clippedRect);
2167 painter.setRenderHint(QPainter::Antialiasing);
2168 painter.setPen(pen);
2169 painter.setBrush(brush);
2170 painter.setClipRect(clipRect);
2171 painter.drawPath(path);
2172 }
2173
2174 clippedPath.fill(0x12345678);
2175 {
2176 QPainter painter(&clippedPath);
2177 painter.setRenderHint(QPainter::Antialiasing);
2178 painter.setPen(pen);
2179 painter.setBrush(brush);
2180 painter.setClipPath(clipPath);
2181 painter.drawPath(path);
2182 }
2183
2184 QCOMPARE(clippedRect, clippedPath);
2185 }
2186
2187 void tst_QPainter::clippedLines_data()
2188 {
2189 QTest::addColumn<QSize>("imageSize");
2190 QTest::addColumn<QLineF>("line");
2191 QTest::addColumn<QRect>("clipRect");
2192 QTest::addColumn<QPen>("pen");
2193
2194 QPen pen2(QColor(223, 223, 0, 223));
2195 pen2.setWidth(2);
2196
2197 QVector<QLineF> lines;
2198 lines << QLineF(15, 15, 65, 65)
2199 << QLineF(14, 14, 66, 66)
2200 << QLineF(16, 16, 64, 64)
2201 << QLineF(65, 65, 15, 15)
2202 << QLineF(66, 66, 14, 14)
2203 << QLineF(64, 64, 14, 14)
2204 << QLineF(15, 50, 15, 64)
2205 << QLineF(15, 50, 15, 65)
2206 << QLineF(15, 50, 15, 66)
2207 << QLineF(15, 50, 64, 50)
2208 << QLineF(15, 50, 65, 50)
2209 << QLineF(15, 50, 66, 50);
2210
2211 foreach (QLineF line, lines) {
2212 const QByteArray desc = "line (" + QByteArray::number(line.x1())
2213 + ", " + QByteArray::number(line.y1()) + ", "
2214 + QByteArray::number(line.x2()) + ", " + QByteArray::number(line.y2())
2215 + ") ";
2216 QTest::newRow((desc + '0').constData()) << QSize(100, 100) << line
2217 << QRect(15, 15, 49, 49)
2218 << QPen(Qt::black);
2219 QTest::newRow((desc + '1').constData()) << QSize(100, 100) << line
2220 << QRect(15, 15, 50, 50)
2221 << QPen(Qt::black);
2222 QTest::newRow((desc + '2').constData()) << QSize(100, 100) << line
2223 << QRect(15, 15, 51, 51)
2224 << QPen(Qt::black);
2225 QTest::newRow((desc + '3').constData()) << QSize(100, 100) << line
2226 << QRect(15, 15, 51, 51)
2227 << QPen(Qt::NoPen);
2228 QTest::newRow((desc + '4').constData()) << QSize(100, 100) << line
2229 << QRect(15, 15, 51, 51)
2230 << pen2;
2231 }
2232 }
2233
2234 void tst_QPainter::clippedLines()
2235 {
2236 QFETCH(QSize, imageSize);
2237 QFETCH(QLineF, line);
2238 QFETCH(QRect, clipRect);
2239 QPainterPath clipPath;
2240 clipPath.addRect(clipRect);
2241 QFETCH(QPen, pen);
2242
2243 const int width = imageSize.width();
2244 const int height = imageSize.height();
2245
2246 QImage clippedRect(width, height, QImage::Format_ARGB32_Premultiplied);
2247 clippedRect.fill(0x12345678);
2248 {
2249 QPainter painter(&clippedRect);
2250 painter.setPen(pen);
2251 painter.setClipRect(clipRect);
2252 painter.drawLine(line);
2253 painter.drawLine(line.toLine());
2254 }
2255
2256 QImage clippedPath(width, height, QImage::Format_ARGB32_Premultiplied);
2257 clippedPath.fill(0x12345678);
2258 {
2259 QPainter painter(&clippedPath);
2260 painter.setPen(pen);
2261 painter.setClipPath(clipPath);
2262 painter.drawLine(line);
2263 painter.drawLine(line.toLine());
2264 }
2265
2266 QCOMPARE(clippedRect, clippedPath);
2267
2268 // repeat with antialiasing
2269 clippedRect.fill(0x12345678);
2270 {
2271 QPainter painter(&clippedRect);
2272 painter.setRenderHint(QPainter::Antialiasing);
2273 painter.setPen(pen);
2274 painter.setClipRect(clipRect);
2275 painter.drawLine(line);
2276 painter.drawLine(line.toLine());
2277 }
2278
2279 clippedPath.fill(0x12345678);
2280 {
2281 QPainter painter(&clippedPath);
2282 painter.setRenderHint(QPainter::Antialiasing);
2283 painter.setPen(pen);
2284 painter.setClipPath(clipPath);
2285 painter.drawLine(line);
2286 painter.drawLine(line.toLine());
2287 }
2288
2289 QCOMPARE(clippedRect, clippedPath);
2290 }
2291
2292 void tst_QPainter::clippedPolygon_data()
2293 {
2294 clippedFillPath_data();
2295 };
2296
2297 void tst_QPainter::clippedPolygon()
2298 {
2299 QFETCH(QSize, imageSize);
2300 QFETCH(QPainterPath, path);
2301 QPolygonF polygon = path.toFillPolygon(QTransform());
2302 QFETCH(QRect, clipRect);
2303 QPainterPath clipPath;
2304 clipPath.addRect(clipRect);
2305 QFETCH(QPen, pen);
2306 QFETCH(QBrush, brush);
2307
2308 const int width = imageSize.width();
2309 const int height = imageSize.height();
2310
2311 QImage clippedRect(width, height, QImage::Format_ARGB32_Premultiplied);
2312 clippedRect.fill(0x12345678);
2313 {
2314 QPainter painter(&clippedRect);
2315 painter.setPen(pen);
2316 painter.setBrush(brush);
2317 painter.setClipRect(clipRect);
2318 painter.drawPolygon(polygon);
2319 painter.drawPolygon(polygon.toPolygon());
2320 }
2321
2322 QImage clippedPath(width, height, QImage::Format_ARGB32_Premultiplied);
2323 clippedPath.fill(0x12345678);
2324 {
2325 QPainter painter(&clippedPath);
2326 painter.setPen(pen);
2327 painter.setBrush(brush);
2328 painter.setClipRect(clipRect);
2329 painter.drawPolygon(polygon);
2330 painter.drawPolygon(polygon.toPolygon());
2331 }
2332
2333 QCOMPARE(clippedRect, clippedPath);
2334
2335 // repeat with antialiasing
2336
2337 clippedRect.fill(0x12345678);
2338 {
2339 QPainter painter(&clippedRect);
2340 painter.setRenderHint(QPainter::Antialiasing);
2341 painter.setPen(pen);
2342 painter.setBrush(brush);
2343 painter.setClipRect(clipRect);
2344 painter.drawPolygon(polygon);
2345 painter.drawPolygon(polygon.toPolygon());
2346 }
2347
2348 clippedPath.fill(0x12345678);
2349 {
2350 QPainter painter(&clippedPath);
2351 painter.setRenderHint(QPainter::Antialiasing);
2352 painter.setPen(pen);
2353 painter.setBrush(brush);
2354 painter.setClipRect(clipRect);
2355 painter.drawPolygon(polygon);
2356 painter.drawPolygon(polygon.toPolygon());
2357 }
2358
2359 QCOMPARE(clippedRect, clippedPath);
2360 }
2361
2362 // this just draws some text that should be clipped in the raster
2363 // paint engine.
2364 void tst_QPainter::clippedText()
2365 {
2366 for (char ch = 'A'; ch < 'Z'; ++ch) {
2367 //qDebug() << ch;
2368 QFont f;
2369 f.setPixelSize(24);
2370 QFontMetrics metrics(f);
2371 QRect textRect = metrics.boundingRect(QChar(ch));
2372
2373 if (textRect.width() <= 8)
2374 continue;
2375 if (textRect.height() <= 8)
2376 continue;
2377
2378 QRect imageRect = textRect.adjusted(4, 4, -4, -4);
2379
2380 QImage image(imageRect.size(), QImage::Format_ARGB32_Premultiplied);
2381
2382 image.fill(qRgba(255, 255, 255, 255));
2383 {
2384 QPainter painter(&image);
2385 painter.setFont(f);
2386 painter.setPen(Qt::black);
2387
2388 painter.drawText(0, 0, QChar(ch));
2389 }
2390
2391 image.fill(qRgba(255, 255, 255, 255));
2392 {
2393 QPainter painter(&image);
2394 painter.setFont(f);
2395 painter.setPen(Qt::black);
2396
2397 painter.drawText(-imageRect.topLeft(), QChar(ch));
2398 }
2399
2400 bool foundPixel = false;
2401 for (int x = 0; x < image.width(); ++x)
2402 for (int y = 0; y < image.height(); ++y)
2403 if (image.pixel(x, y) != 0)
2404 foundPixel = true;
2405 // can't QVERIFY(foundPixel) as sometimes all pixels are clipped
2406 // away. For example for 'O'
2407 // just call /some/ function to prevent the compiler from optimizing
2408 // foundPixel away
2409 QString::number(foundPixel);
2410
2411 //image.save(QString("debug") + ch + ".xpm");
2412 }
2413
2414 QVERIFY(true); // reached, don't trigger any valgrind errors
2415 }
2416
2417 void tst_QPainter::setOpacity_data()
2418 {
2419 QTest::addColumn<QImage::Format>("destFormat");
2420 QTest::addColumn<QImage::Format>("srcFormat");
2421
2422 QTest::newRow("ARGB32P on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2423 << QImage::Format_ARGB32_Premultiplied;
2424
2425 QTest::newRow("ARGB32 on ARGB32") << QImage::Format_ARGB32
2426 << QImage::Format_ARGB32;
2427
2428 QTest::newRow("RGB32 on RGB32") << QImage::Format_RGB32
2429 << QImage::Format_RGB32;
2430
2431 QTest::newRow("RGB16 on RGB16") << QImage::Format_RGB16
2432 << QImage::Format_RGB16;
2433
2434 QTest::newRow("ARGB8565_Premultiplied on ARGB8565_Premultiplied") << QImage::Format_ARGB8565_Premultiplied
2435 << QImage::Format_ARGB8565_Premultiplied;
2436
2437 QTest::newRow("RGB555 on RGB555") << QImage::Format_RGB555
2438 << QImage::Format_RGB555;
2439
2440 QTest::newRow("RGB666 on RGB666") << QImage::Format_RGB666
2441 << QImage::Format_RGB666;
2442
2443 QTest::newRow("ARGB8555_Premultiplied on ARGB8555_Premultiplied") << QImage::Format_ARGB8555_Premultiplied
2444 << QImage::Format_ARGB8555_Premultiplied;
2445
2446 QTest::newRow("RGB888 on RGB888") << QImage::Format_RGB888
2447 << QImage::Format_RGB888;
2448
2449 QTest::newRow("RGB32 on RGB16") << QImage::Format_RGB16
2450 << QImage::Format_RGB32;
2451
2452 QTest::newRow("RGB32 on ARGB8565_Premultiplied") << QImage::Format_ARGB8565_Premultiplied
2453 << QImage::Format_RGB32;
2454
2455 QTest::newRow("RGB32 on RGB666") << QImage::Format_RGB666
2456 << QImage::Format_RGB32;
2457
2458 QTest::newRow("RGB32 on RGB555") << QImage::Format_RGB555
2459 << QImage::Format_RGB32;
2460
2461 QTest::newRow("RGB32 on ARGB8555_Premultiplied") << QImage::Format_ARGB8555_Premultiplied
2462 << QImage::Format_RGB32;
2463
2464 QTest::newRow("RGB32 on RGB888") << QImage::Format_RGB888
2465 << QImage::Format_RGB32;
2466
2467 QTest::newRow("RGB16 on RGB32") << QImage::Format_RGB32
2468 << QImage::Format_RGB16;
2469
2470 QTest::newRow("ARGB8565_Premultiplied on RGB32") << QImage::Format_RGB32
2471 << QImage::Format_ARGB8565_Premultiplied;
2472
2473 QTest::newRow("RGB666 on RGB32") << QImage::Format_RGB32
2474 << QImage::Format_RGB666;
2475
2476 QTest::newRow("RGB555 on RGB32") << QImage::Format_RGB32
2477 << QImage::Format_RGB555;
2478
2479 QTest::newRow("ARGB8555_Premultiplied on RGB32") << QImage::Format_RGB32
2480 << QImage::Format_ARGB8555_Premultiplied;
2481
2482 QTest::newRow("RGB888 on RGB32") << QImage::Format_RGB32
2483 << QImage::Format_RGB888;
2484
2485 QTest::newRow("RGB555 on RGB888") << QImage::Format_RGB888
2486 << QImage::Format_RGB555;
2487
2488 QTest::newRow("RGB666 on RGB888") << QImage::Format_RGB888
2489 << QImage::Format_RGB666;
2490
2491 QTest::newRow("RGB444 on RGB444") << QImage::Format_RGB444
2492 << QImage::Format_RGB444;
2493
2494 QTest::newRow("RGBA8888P on RGBA8888P") << QImage::Format_RGBA8888_Premultiplied
2495 << QImage::Format_RGBA8888_Premultiplied;
2496
2497 QTest::newRow("RGBA8888 on RGBA8888") << QImage::Format_RGBA8888
2498 << QImage::Format_RGBA8888;
2499
2500 QTest::newRow("RGBx8888 on RGBx8888") << QImage::Format_RGBX8888
2501 << QImage::Format_RGBX8888;
2502
2503 QTest::newRow("RGBA8888P on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2504 << QImage::Format_RGBA8888_Premultiplied;
2505
2506 QTest::newRow("RGBx8888 on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2507 << QImage::Format_RGBX8888;
2508
2509 QTest::newRow("ARGB32P on RGBA8888P") << QImage::Format_RGBA8888_Premultiplied
2510 << QImage::Format_ARGB32_Premultiplied;
2511
2512 QTest::newRow("RGB32 on RGBx8888") << QImage::Format_RGBX8888
2513 << QImage::Format_RGB32;
2514
2515 QTest::newRow("RGB30 on RGB32") << QImage::Format_RGB32
2516 << QImage::Format_BGR30;
2517
2518 QTest::newRow("BGR30 on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2519 << QImage::Format_BGR30;
2520
2521 QTest::newRow("A2RGB30P on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2522 << QImage::Format_A2BGR30_Premultiplied;
2523
2524 QTest::newRow("A2RGB30P on A2RGB30P") << QImage::Format_A2RGB30_Premultiplied
2525 << QImage::Format_A2RGB30_Premultiplied;
2526
2527 QTest::newRow("ARGB32P on A2RGB30P") << QImage::Format_A2RGB30_Premultiplied
2528 << QImage::Format_ARGB32_Premultiplied;
2529
2530 QTest::newRow("RGB32 on A2BGR30P") << QImage::Format_A2BGR30_Premultiplied
2531 << QImage::Format_RGB32;
2532
2533 QTest::newRow("RGB30 on A2BGR30P") << QImage::Format_A2BGR30_Premultiplied
2534 << QImage::Format_RGB30;
2535
2536 QTest::newRow("A2RGB30P on A2BGR30P") << QImage::Format_A2BGR30_Premultiplied
2537 << QImage::Format_A2RGB30_Premultiplied;
2538
2539 QTest::newRow("ARGB32P on BGR30") << QImage::Format_BGR30
2540 << QImage::Format_ARGB32_Premultiplied;
2541
2542 QTest::newRow("ARGB32P on RGB30") << QImage::Format_RGB30
2543 << QImage::Format_ARGB32_Premultiplied;
2544
2545 QTest::newRow("A2RGB30P on RGB30") << QImage::Format_RGB30
2546 << QImage::Format_A2RGB30_Premultiplied;
2547
2548 QTest::newRow("RGBA64P on RGBA64P") << QImage::Format_RGBA64_Premultiplied
2549 << QImage::Format_RGBA64_Premultiplied;
2550
2551 QTest::newRow("RGBA64 on RGBA64") << QImage::Format_RGBA64
2552 << QImage::Format_RGBA64;
2553
2554 QTest::newRow("RGBx64 on RGBx64") << QImage::Format_RGBX64
2555 << QImage::Format_RGBX64;
2556
2557 QTest::newRow("RGBA64P on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2558 << QImage::Format_RGBA64_Premultiplied;
2559
2560 QTest::newRow("RGBx64 on ARGB32P") << QImage::Format_ARGB32_Premultiplied
2561 << QImage::Format_RGBX64;
2562
2563 QTest::newRow("ARGB32P on RGBA64P") << QImage::Format_RGBA64_Premultiplied
2564 << QImage::Format_ARGB32_Premultiplied;
2565
2566 }
2567
2568 void tst_QPainter::setOpacity()
2569 {
2570 QFETCH(QImage::Format, destFormat);
2571 QFETCH(QImage::Format, srcFormat);
2572
2573 const QSize imageSize(12, 12);
2574 const QRect imageRect(QPoint(0, 0), imageSize);
2575 QColor destColor = Qt::black;
2576 QColor srcColor = Qt::white;
2577
2578 QImage dest(imageSize, destFormat);
2579 QImage src(imageSize, srcFormat);
2580
2581 QPainter p;
2582 p.begin(&dest);
2583 p.fillRect(imageRect, destColor);
2584 p.end();
2585
2586 p.begin(&src);
2587 p.fillRect(imageRect, srcColor);
2588 p.end();
2589
2590 p.begin(&dest);
2591 p.setOpacity(0.5);
2592 p.drawImage(imageRect, src, imageRect);
2593 p.end();
2594
2595 QImage actual = dest.convertToFormat(QImage::Format_RGB32);
2596
2597 for (int y = 0; y < actual.height(); ++y) {
2598 QRgb *p = (QRgb *)actual.scanLine(y);
2599 for (int x = 0; x < actual.width(); ++x) {
2600 QVERIFY(qAbs(qRed(p[x]) - 127) <= 0xf);
2601 QVERIFY(qAbs(qGreen(p[x]) - 127) <= 0xf);
2602 QVERIFY(qAbs(qBlue(p[x]) - 127) <= 0xf);
2603 }
2604 }
2605 }
2606
2607 void tst_QPainter::drawhelper_blend_untransformed_data()
2608 {
2609 setOpacity_data();
2610 }
2611
2612 void tst_QPainter::drawhelper_blend_untransformed()
2613 {
2614 QFETCH(QImage::Format, destFormat);
2615 QFETCH(QImage::Format, srcFormat);
2616
2617 const int size = 128;
2618 const QSize imageSize(size, size);
2619 const QRect paintRect(1, 0, size - 2, size); // needs alignment and tailing
2620
2621 QColor destColor(127, 127, 127);
2622 QColor srcColor(Qt::white);
2623
2624 QImage dest(imageSize, destFormat);
2625 QImage src(imageSize, srcFormat);
2626
2627 QPainter p;
2628 p.begin(&src);
2629 p.fillRect(paintRect, srcColor);
2630 p.end();
2631
2632 QList<qreal> opacities = (QList<qreal>() << 0.0 << 0.1 << 0.01 << 0.4
2633 << 0.5 << 0.6 << 0.9 << 1.0);
2634 foreach (qreal opacity, opacities) {
2635 p.begin(&dest);
2636 p.fillRect(paintRect, destColor);
2637
2638 p.setOpacity(opacity);
2639 p.drawImage(paintRect, src, paintRect);
2640 p.end();
2641
2642 // sanity check: make sure all pixels are equal
2643 QImage expected(size - 2, size, destFormat);
2644 p.begin(&expected);
2645 p.fillRect(0, 0, expected.width(), expected.height(),
2646 dest.pixelColor(1, 0));
2647 p.end();
2648
2649 const QImage subDest(dest.bits() + dest.depth() / 8,
2650 dest.width() - 2, dest.height(),
2651 dest.bytesPerLine(), dest.format());
2652
2653 if (dest.format() == QImage::Format_ARGB8565_Premultiplied ||
2654 dest.format() == QImage::Format_ARGB8555_Premultiplied) {
2655 // Test skipped due to rounding errors...
2656 continue;
2657 }
2658 QCOMPARE(subDest, expected);
2659 }
2660 }
2661
2662 void tst_QPainter::drawhelper_blend_tiled_untransformed_data()
2663 {
2664 setOpacity_data();
2665 }
2666
2667 void tst_QPainter::drawhelper_blend_tiled_untransformed()
2668 {
2669 QFETCH(QImage::Format, destFormat);
2670 QFETCH(QImage::Format, srcFormat);
2671
2672 const int size = 128;
2673 const QSize imageSize(size, size);
2674 const QRect paintRect(1, 0, size - 2, size); // needs alignment and tailing
2675
2676 QColor destColor(127, 127, 127);
2677 QColor srcColor(Qt::white);
2678
2679 QImage dest(imageSize, destFormat);
2680 QImage src(imageSize / 2, srcFormat);
2681
2682 QPainter p;
2683 p.begin(&src);
2684 p.fillRect(QRect(QPoint(0, 0), imageSize/ 2), srcColor);
2685 p.end();
2686
2687 const QBrush brush(src);
2688
2689 QList<qreal> opacities = (QList<qreal>() << 0.0 << 0.1 << 0.01 << 0.4
2690 << 0.5 << 0.6 << 0.9 << 1.0);
2691 foreach (qreal opacity, opacities) {
2692 p.begin(&dest);
2693 p.fillRect(paintRect, destColor);
2694
2695 p.setOpacity(opacity);
2696 p.fillRect(paintRect, brush);
2697 p.end();
2698
2699 // sanity check: make sure all pixels are equal
2700 QImage expected(size - 2, size, destFormat);
2701 p.begin(&expected);
2702 p.fillRect(0, 0, expected.width(), expected.height(),
2703 dest.pixelColor(1, 0));
2704 p.end();
2705
2706 const QImage subDest(dest.bits() + dest.depth() / 8,
2707 dest.width() - 2, dest.height(),
2708 dest.bytesPerLine(), dest.format());
2709
2710 if (dest.format() == QImage::Format_ARGB8565_Premultiplied ||
2711 dest.format() == QImage::Format_ARGB8555_Premultiplied) {
2712 // Skipping test due to rounding errors. Test needs rewrite
2713 continue;
2714 }
2715 QCOMPARE(subDest, expected);
2716 }
2717 }
2718
2719 static QPaintEngine::PaintEngineFeatures no_porter_duff()
2720 {
2721 QPaintEngine::PaintEngineFeatures features = QPaintEngine::AllFeatures;
2722 return features & ~QPaintEngine::PorterDuff;
2723 }
2724
2725 class DummyPaintEngine : public QPaintEngine, public QPaintDevice
2726 {
2727 public:
2728 DummyPaintEngine() : QPaintEngine(no_porter_duff()) {}
2729 virtual bool begin(QPaintDevice *) { return true; }
2730 virtual bool end() { return true; }
2731
2732 virtual void updateState(const QPaintEngineState &) {}
2733 virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) {}
2734
2735 virtual Type type() const { return User; }
2736
2737 virtual QPaintEngine *paintEngine() const { return (QPaintEngine *)this; }
2738
2739 virtual int metric(PaintDeviceMetric metric) const { Q_UNUSED(metric); return 0; };
2740 };
2741
2742 static bool success;
2743
2744 void porterDuff_warningChecker(QtMsgType type, const QMessageLogContext &, const QString &msg)
2745 {
2746 if (type == QtWarningMsg && msg == QLatin1String("QPainter::setCompositionMode: PorterDuff modes not supported on device"))
2747 success = false;
2748 }
2749
2750 void tst_QPainter::porterDuff_warning()
2751 {
2752 QtMessageHandler old = qInstallMessageHandler(porterDuff_warningChecker);
2753 DummyPaintEngine dummy;
2754 QPainter p(&dummy);
2755
2756 success = true;
2757 p.setCompositionMode(QPainter::CompositionMode_Source);
2758 QVERIFY(success);
2759
2760 success = true;
2761 p.setCompositionMode(QPainter::CompositionMode_SourceOver);
2762 QVERIFY(success);
2763
2764 success = true;
2765 p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
2766 QVERIFY(!success);
2767
2768 QVERIFY(qInstallMessageHandler(old) == porterDuff_warningChecker);
2769 }
2770
2771 void tst_QPainter::drawhelper_blend_color()
2772 {
2773 QImage dest(32, 32, QImage::Format_ARGB8555_Premultiplied);
2774 dest.fill(0xff000000);
2775
2776 {
2777 QPainter p(&dest);
2778 p.fillRect(0, 0, dest.width(), dest.height(), QColor(255, 0, 0, 127));
2779 }
2780
2781 QImage expected(32, 32, QImage::Format_ARGB8555_Premultiplied);
2782 expected.fill(0xff3c007f);
2783
2784 QCOMPARE(dest.pixel(1, 1), expected.pixel(1, 1));
2785 QCOMPARE(dest, expected);
2786 }
2787
2788 #ifndef QT_NO_WIDGETS
2789 class ViewportTestWidget : public QWidget
2790 {
2791 public:
2792 ViewportTestWidget(QWidget *parent = 0) : QWidget(parent), hasPainted(false) {}
2793 QSize sizeHint() const {
2794 return QSize(100, 100);
2795 }
2796
2797 QRect viewport;
2798 bool hasPainted;
2799
2800 protected:
2801 void paintEvent(QPaintEvent *) {
2802 hasPainted = true;
2803 QPainter p(this);
2804 viewport = p.viewport();
2805 }
2806 };
2807
2808 void tst_QPainter::childWidgetViewport()
2809 {
2810 QWidget parent;
2811 parent.setAutoFillBackground(true);
2812 parent.resize(200, 200);
2813 ViewportTestWidget child(&parent);
2814 child.setAutoFillBackground(true);
2815 parent.show();
2816 parent.update();
2817 qApp->processEvents();
2818
2819 if (child.hasPainted) {
2820 QCOMPARE(child.viewport, QRect(QPoint(0, 0), child.sizeHint()));
2821 } else {
2822 qWarning("Failed to ensure that paintEvent has been run. Could not run test.");
2823 }
2824 }
2825 #endif
2826
2827 void tst_QPainter::fillRect_objectBoundingModeGradient()
2828 {
2829 QImage a(10, 10, QImage::Format_ARGB32_Premultiplied);
2830 a.fill(0x0);
2831 QImage b = a;
2832
2833 QLinearGradient g(QPoint(0, 0), QPoint(0, 1));
2834 g.setColorAt(0, Qt::red);
2835 g.setColorAt(1, Qt::blue);
2836 g.setCoordinateMode(QGradient::ObjectBoundingMode);
2837
2838 QPainter p(&a);
2839 p.fillRect(QRect(0, 0, a.width(), a.height()), g);
2840 p.end();
2841
2842 QPainterPath path;
2843 path.addRect(0, 0, a.width(), a.height());
2844
2845 p.begin(&b);
2846 p.fillPath(path, g);
2847 p.end();
2848
2849 QCOMPARE(a, b);
2850 }
2851
2852 void tst_QPainter::fillRect_stretchToDeviceMode()
2853 {
2854 QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
2855
2856 QLinearGradient g(QPoint(0, 0), QPoint(0, 1));
2857 g.setCoordinateMode(QGradient::StretchToDeviceMode);
2858
2859 QPainter p(&img);
2860 p.fillRect(img.rect(), g);
2861 p.end();
2862
2863 for (int i = 1; i < img.height(); ++i)
2864 QVERIFY(img.pixel(0, i) != img.pixel(0, i-1));
2865 }
2866
2867 void tst_QPainter::monoImages()
2868 {
2869 Qt::GlobalColor colorPairs[][2] = {
2870 { Qt::white, Qt::black },
2871 { Qt::color0, Qt::color1 },
2872 { Qt::red, Qt::blue }
2873 };
2874
2875 const int numColorPairs = sizeof(colorPairs) / sizeof(QRgb[2]);
2876
2877 QImage transparent(2, 2, QImage::Format_ARGB32_Premultiplied);
2878 transparent.fill(0x0);
2879
2880 for (int i = 1; i < QImage::NImageFormats; ++i) {
2881 for (int j = 0; j < numColorPairs; ++j) {
2882 const QImage::Format format = QImage::Format(i);
2883 if (format == QImage::Format_Indexed8)
2884 continue;
2885
2886 QImage img(2, 2, format);
2887
2888 if (img.colorCount() > 0) {
2889 img.setColor(0, QColor(colorPairs[j][0]).rgba());
2890 img.setColor(1, QColor(colorPairs[j][1]).rgba());
2891 }
2892
2893 img.fill(0x0);
2894 QPainter p(&img);
2895 p.fillRect(0, 0, 2, 2, colorPairs[j][0]);
2896 p.fillRect(0, 0, 1, 1, colorPairs[j][1]);
2897 p.fillRect(1, 1, 1, 1, colorPairs[j][1]);
2898 p.end();
2899
2900 QImage original = img;
2901
2902 p.begin(&img);
2903 p.drawImage(0, 0, transparent);
2904 p.end();
2905
2906 // drawing a transparent image on top of another image
2907 // should not change the image
2908 QCOMPARE(original, img);
2909
2910 if (img.colorCount() == 0)
2911 continue;
2912
2913 for (int k = 0; k < 2; ++k) {
2914 QPainter p(&img);
2915 p.fillRect(0, 0, 2, 2, colorPairs[j][k]);
2916 p.end();
2917
2918 QImage argb32p(2, 2, QImage::Format_ARGB32_Premultiplied);
2919 p.begin(&argb32p);
2920 p.fillRect(0, 0, 2, 2, colorPairs[j][k]);
2921 p.end();
2922
2923 QCOMPARE(argb32p, img.convertToFormat(argb32p.format()));
2924
2925 // drawing argb32p image on mono image
2926 p.begin(&img);
2927 p.drawImage(0, 0, argb32p);
2928 p.end();
2929
2930 QCOMPARE(argb32p, img.convertToFormat(argb32p.format()));
2931
2932 // drawing mono image on argb32p image
2933 p.begin(&argb32p);
2934 p.drawImage(0, 0, img);
2935 p.end();
2936
2937 QCOMPARE(argb32p, img.convertToFormat(argb32p.format()));
2938 }
2939 }
2940 }
2941 }
2942
2943 #if !defined(Q_OS_AIX) && !defined(Q_CC_MSVC) && !defined(Q_OS_SOLARIS) && !defined(__UCLIBC__)
2944 #include <fenv.h>
2945
2946 static const QString fpeExceptionString(int exception)
2947 {
2948 #ifdef FE_INEXACT
2949 if (exception & FE_INEXACT)
2950 return QLatin1String("Inexact result");
2951 #endif
2952 if (exception & FE_UNDERFLOW)
2953 return QLatin1String("Underflow");
2954 if (exception & FE_OVERFLOW)
2955 return QLatin1String("Overflow");
2956 if (exception & FE_DIVBYZERO)
2957 return QLatin1String("Divide by zero");
2958 if (exception & FE_INVALID)
2959 return QLatin1String("Invalid operation");
2960 return QLatin1String("No exception");
2961 }
2962
2963 class FpExceptionChecker
2964 {
2965 public:
2966 FpExceptionChecker(int exceptionMask)
2967 : m_exceptionMask(exceptionMask)
2968 {
2969 feclearexcept(m_exceptionMask);
2970 }
2971
2972 ~FpExceptionChecker()
2973 {
2974 const int exceptions = fetestexcept(m_exceptionMask);
2975 QVERIFY2(!exceptions, qPrintable(QLatin1String("Floating point exception: ") + fpeExceptionString(exceptions)));
2976 }
2977
2978 private:
2979 int m_exceptionMask;
2980 };
2981
2982 void fpe_rasterizeLine_task232012()
2983 {
2984 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
2985 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
2986 img.fill(0x0);
2987 QPainter p(&img);
2988
2989 p.setBrush(Qt::black);
2990 p.drawRect(QRectF(0, 0, 5, 0));
2991 p.drawRect(QRectF(0, 0, 0, 5));
2992 }
2993
2994 void fpe_pixmapTransform()
2995 {
2996 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
2997
2998 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
2999
3000 QPainter p(&img);
3001
3002 const qreal scaleFactor = 0.001;
3003 const int translateDistance = 1000000;
3004
3005 p.setPen(Qt::red);
3006 p.setBrush(QBrush(Qt::red,Qt::Dense6Pattern));
3007
3008 for (int i = 0; i < 2; ++i) {
3009 p.setRenderHint(QPainter::SmoothPixmapTransform, i);
3010
3011 p.resetTransform();
3012 p.scale(1.1, 1.1);
3013 p.translate(translateDistance, 0);
3014 p.drawRect(-translateDistance, 0, 100, 100);
3015
3016 p.resetTransform();
3017 p.scale(scaleFactor, scaleFactor);
3018 p.drawRect(QRectF(0, 0, 1 / scaleFactor, 1 / scaleFactor));
3019 }
3020 }
3021
3022 void fpe_zeroLengthLines()
3023 {
3024 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
3025
3026 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
3027
3028 QPainter p(&img);
3029
3030 p.setPen(QPen(Qt::black, 3));
3031 p.drawLine(64, 64, 64, 64);
3032 }
3033
3034 void fpe_divByZero()
3035 {
3036 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
3037
3038 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
3039
3040 QPainter p(&img);
3041
3042 p.setRenderHint(QPainter::Antialiasing);
3043
3044 p.drawRect(QRectF(10, 10, 100, 0));
3045 p.drawRect(QRectF(10, 10, 0, 100));
3046
3047 p.drawRect(QRect(10, 10, 100, 0));
3048 p.drawRect(QRect(10, 10, 0, 100));
3049
3050 p.fillRect(QRectF(10, 10, 100, 0), Qt::black);
3051 p.fillRect(QRectF(10, 10, 0, 100), Qt::black);
3052
3053 p.fillRect(QRect(10, 10, 100, 0), Qt::black);
3054 p.fillRect(QRect(10, 10, 0, 100), Qt::black);
3055 }
3056
3057 void fpe_steepSlopes()
3058 {
3059 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
3060
3061 QImage img(1024, 1024, QImage::Format_ARGB32_Premultiplied);
3062
3063 QFETCH(QTransform, transform);
3064 QFETCH(QLineF, line);
3065 QFETCH(bool, antialiased);
3066
3067 QPainter p(&img);
3068
3069 p.setPen(QPen(Qt::black, 1));
3070 p.setRenderHint(QPainter::Antialiasing, antialiased);
3071 p.setTransform(transform);
3072
3073 p.drawLine(line);
3074 }
3075
3076 void fpe_radialGradients()
3077 {
3078 FpExceptionChecker checker(FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID | FE_DIVBYZERO);
3079
3080 QImage img(21, 21, QImage::Format_ARGB32_Premultiplied);
3081 img.fill(0);
3082
3083 double m = img.width() * 0.5;
3084
3085 QPainter p(&img);
3086 p.setRenderHints(QPainter::Antialiasing);
3087 p.setPen(Qt::NoPen);
3088 p.setBrush(QRadialGradient(m, m, m));
3089 p.drawEllipse(img.rect());
3090 }
3091
3092 #define FPE_TEST(x) \
3093 void tst_QPainter::x() \
3094 { \
3095 ::x(); \
3096 }
3097 #else
3098 #define FPE_TEST(x) \
3099 void tst_QPainter::x() \
3100 { \
3101 QSKIP("Floating point exception checking (fenv.h) not available"); \
3102 }
3103 #endif
3104
3105 FPE_TEST(fpe_rasterizeLine_task232012)
3106 FPE_TEST(fpe_pixmapTransform)
3107 FPE_TEST(fpe_zeroLengthLines)
3108 FPE_TEST(fpe_divByZero)
3109 FPE_TEST(fpe_steepSlopes)
3110 FPE_TEST(fpe_radialGradients)
3111
3112 void tst_QPainter::fpe_steepSlopes_data()
3113 {
3114 QTest::addColumn<QTransform>("transform");
3115 QTest::addColumn<QLineF>("line");
3116 QTest::addColumn<bool>("antialiased");
3117
3118 {
3119 const qreal dsin = 0.000014946676875461832484392500630665523431162000633776187896728515625;
3120 const qreal dcos = 0.9999999998882984630910186751862056553363800048828125;
3121
3122 const QTransform transform = QTransform(dcos, dsin, -dsin, dcos, 64, 64);
3123 const QLineF line(2, 2, 2, 6);
3124
3125 QTest::newRow("task 207147 aa") << transform << line << true;
3126 QTest::newRow("task 207147 no aa") << transform << line << false;
3127 }
3128
3129 {
3130 QTransform transform;
3131 transform.rotate(0.0000001);
3132 const QLineF line(5, 5, 10, 5);
3133
3134 QTest::newRow("task 166702 aa") << transform << line << true;
3135 QTest::newRow("task 166702 no aa") << transform << line << false;
3136 }
3137
3138 {
3139 const QTransform transform;
3140 const QLineF line(2.5, 2.5, 2.5 + 1/256., 60000.5);
3141
3142 QTest::newRow("steep line aa") << transform << line << true;
3143 QTest::newRow("steep line no aa") << transform << line << false;
3144 }
3145
3146 {
3147 const QTransform transform;
3148 const QLineF line(2.5, 2.5, 2.5 + 1/256., 1024);
3149
3150 QTest::newRow("steep line 2 aa") << transform << line << true;
3151 QTest::newRow("steep line 2 no aa") << transform << line << false;
3152 }
3153
3154 {
3155 const QTransform transform;
3156 const QLineF line(2.5, 2.5, 2.5 + 1/64., 1024);
3157
3158 QTest::newRow("steep line 3 aa") << transform << line << true;
3159 QTest::newRow("steep line 3 no aa") << transform << line << false;
3160 }
3161 }
3162
3163 qreal randf()
3164 {
3165 return QRandomGenerator::global()->bounded(1.0);
3166 }
3167
3168 QPointF randInRect(const QRectF &rect)
3169 {
3170 const qreal x = rect.left() + rect.width() * randf();
3171 const qreal y = rect.top() + rect.height() * randf();
3172
3173 return QPointF(x, y);
3174 }
3175
3176 void tst_QPainter::rasterizer_asserts()
3177 {
3178 QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
3179
3180 QRectF middle(QPointF(0, 0), img.size());
3181 QRectF left = middle.translated(-middle.width(), 0);
3182 QRectF right = middle.translated(middle.width(), 0);
3183
3184 QPainter p(&img);
3185 img.fill(Qt::white);
3186 p.setCompositionMode(QPainter::CompositionMode_Destination);
3187 for (int i = 0; i < 100000; ++i) {
3188 QPainterPath path;
3189 path.moveTo(randInRect(middle));
3190 path.lineTo(randInRect(left));
3191 path.lineTo(randInRect(right));
3192
3193 p.fillPath(path, Qt::black);
3194 }
3195 }
3196
3197 void tst_QPainter::rasterizer_negativeCoords()
3198 {
3199 QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
3200 img.fill(0x0);
3201
3202 QImage original = img;
3203
3204 QPainter p(&img);
3205 p.rotate(90);
3206 p.fillRect(0, 0, 70, 50, Qt::black);
3207
3208 // image should not have changed
3209 QCOMPARE(img.pixel(0, 0), 0x0U);
3210 QCOMPARE(img, original);
3211 }
3212
3213 void tst_QPainter::blendOverFlow_data()
3214 {
3215 QTest::addColumn<QImage::Format>("format");
3216 QTest::addColumn<int>("width");
3217 QTest::addColumn<int>("height");
3218
3219 QImage::Format format = QImage::Format_ARGB8555_Premultiplied;
3220 QTest::newRow("555,1,1") << format << 1 << 1;
3221 QTest::newRow("555,2,2") << format << 2 << 2;
3222 QTest::newRow("555,10,10") << format << 10 << 10;
3223
3224 format = QImage::Format_ARGB8565_Premultiplied;
3225 QTest::newRow("565,1,1") << format << 1 << 1;
3226 QTest::newRow("565,2,2") << format << 2 << 2;
3227 QTest::newRow("565,10,10") << format << 10 << 10;
3228 }
3229
3230 void tst_QPainter::blendOverFlow()
3231 {
3232 QFETCH(QImage::Format, format);
3233 QFETCH(int, width);
3234 QFETCH(int, height);
3235
3236 QImage dest(width, height, format);
3237 QImage src(width, height, format);
3238
3239 {
3240 QPainter p(&dest);
3241 p.fillRect(0, 0, width, height, Qt::green);
3242 }
3243 QImage expected = dest;
3244
3245 {
3246 QPainter p(&src);
3247 p.setCompositionMode(QPainter::CompositionMode_Source);
3248 p.fillRect(0, 0, width, height, QColor(0, 255, 0, 6));
3249 }
3250
3251 {
3252 QPainter p(&dest);
3253 p.drawImage(0, 0, src);
3254 }
3255
3256 QCOMPARE(dest.pixel(0, 0), expected.pixel(0, 0));
3257 QCOMPARE(dest, expected);
3258 }
3259
3260 void tst_QPainter::largeImagePainting_data()
3261 {
3262 QTest::addColumn<int>("width");
3263 QTest::addColumn<int>("height");
3264 QTest::addColumn<bool>("antialiased");
3265
3266 QTest::newRow("tall") << 1 << 32767 << false;
3267 QTest::newRow("tall aa") << 1 << 32767 << true;
3268 QTest::newRow("wide") << 32767 << 1 << false;
3269 QTest::newRow("wide aa") << 32767 << 1 << true;
3270 }
3271
3272 void tst_QPainter::largeImagePainting()
3273 {
3274 QPainterPath path;
3275 path.addRect(0, 0, 1, 1);
3276 path.addRect(2, 0, 1, 1);
3277 path.addRect(0, 2, 1, 1);
3278
3279 QFETCH(int, width);
3280 QFETCH(int, height);
3281 QFETCH(bool, antialiased);
3282
3283 QImage img(width, height, QImage::Format_ARGB32_Premultiplied);
3284 img.fill(0x0);
3285
3286 QPainter p(&img);
3287 p.setPen(Qt::NoPen);
3288 p.setBrush(Qt::white);
3289
3290 p.setRenderHint(QPainter::Antialiasing, antialiased);
3291
3292 for (int i = 0; i < img.width(); i += 4) {
3293 p.drawPath(path);
3294 p.translate(4, 0);
3295 }
3296
3297 p.resetTransform();
3298
3299 for (int i = 4; i < img.height(); i += 4) {
3300 p.translate(0, 4);
3301 p.drawPath(path);
3302 }
3303
3304 for (int i = 0; i < img.width(); ++i) {
3305 if (i % 2)
3306 QCOMPARE(img.pixel(i, 0), 0x0U);
3307 else
3308 QCOMPARE(img.pixel(i, 0), 0xffffffffU);
3309 }
3310
3311 for (int i = 1; i < img.height(); ++i) {
3312 if (i % 2)
3313 QCOMPARE(img.pixel(0, i), 0x0U);
3314 else
3315 QCOMPARE(img.pixel(0, i), 0xffffffffU);
3316 }
3317 }
3318
3319 void tst_QPainter::imageScaling_task206785()
3320 {
3321 QImage src(32, 2, QImage::Format_ARGB32_Premultiplied);
3322 src.fill(0xffffffff);
3323
3324 QImage dst(128, 128, QImage::Format_ARGB32_Premultiplied);
3325
3326 QImage expected(128, 128, QImage::Format_ARGB32_Premultiplied);
3327 expected.fill(0xffffffff);
3328
3329 for (int i = 1; i < 5; ++i) {
3330 qreal scale = i / qreal(5);
3331
3332 dst.fill(0xff000000);
3333
3334 QPainter p(&dst);
3335 p.scale(dst.width() / qreal(src.width()), scale);
3336
3337 for (int y = 0; y * scale < dst.height(); ++y)
3338 p.drawImage(0, y, src);
3339
3340 p.end();
3341
3342 QCOMPARE(dst, expected);
3343 }
3344 }
3345
3346 #define FOR_EACH_NEIGHBOR_8 for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) if (dx != 0 || dy != 0)
3347 #define FOR_EACH_NEIGHBOR_4 for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) if ((dx == 0) != (dy == 0))
3348
3349 uint qHash(const QPoint &point)
3350 {
3351 return qHash(qMakePair(point.x(), point.y()));
3352 }
3353
3354 bool verifyOutlineFillConsistency(const QImage &img, QRgb outside, QRgb inside, QRgb outline)
3355 {
3356 if (img.pixel(img.width() / 2, img.height() / 2) != inside)
3357 return false;
3358
3359 int x = img.width() / 2;
3360 int y = img.height() / 2;
3361
3362 while (img.pixel(++x, y) == inside)
3363 ;
3364
3365 if (img.pixel(x, y) != outline)
3366 return false;
3367
3368 QQueue<QPoint> discovered;
3369 discovered.enqueue(QPoint(x, y));
3370
3371 QVector<bool> visited(img.width() * img.height());
3372 visited.fill(false);
3373
3374 while (!discovered.isEmpty()) {
3375 QPoint p = discovered.dequeue();
3376 QRgb pixel = img.pixel(p.x(), p.y());
3377
3378 bool &v = visited[p.y() * img.width() + p.x()];
3379 if (v)
3380 continue;
3381 v = true;
3382
3383 if (pixel == outline) {
3384 FOR_EACH_NEIGHBOR_8 {
3385 QPoint x(p.x() + dx, p.y() + dy);
3386 discovered.enqueue(x);
3387 }
3388 } else {
3389 FOR_EACH_NEIGHBOR_4 {
3390 if ((dx == 0) == (dy == 0))
3391 continue;
3392 QRgb neighbor = img.pixel(p.x() + dx, p.y() + dy);
3393 if ((pixel == inside && neighbor == outside) ||
3394 (pixel == outside && neighbor == inside))
3395 return false;
3396 }
3397 }
3398 }
3399
3400 return true;
3401 }
3402
3403 #undef FOR_EACH_NEIGHBOR_8
3404 #undef FOR_EACH_NEIGHBOR_4
3405
3406 void tst_QPainter::outlineFillConsistency()
3407 {
3408 QImage dst(256, 256, QImage::Format_ARGB32_Premultiplied);
3409
3410 QPolygonF poly;
3411 poly << QPointF(5, -100) << QPointF(-70, 20) << QPointF(95, 25);
3412
3413 QPen pen(Qt::red);
3414 QBrush brush(Qt::black);
3415
3416 QRgb background = 0xffffffff;
3417 for (int i = 0; i < 360; ++i) {
3418 dst.fill(background);
3419
3420 QPainter p(&dst);
3421 p.translate(dst.width() / 2, dst.height() / 2);
3422
3423 QPolygonF copy = poly;
3424 for (int j = 0; j < copy.size(); ++j)
3425 copy[j] = QTransform().rotate(i).map(copy[j]);
3426
3427 p.setPen(pen);
3428 p.setBrush(brush);
3429 p.drawPolygon(copy);
3430 p.end();
3431
3432 QVERIFY(verifyOutlineFillConsistency(dst, background, brush.color().rgba(), pen.color().rgba()));
3433 }
3434 }
3435
3436 void tst_QPainter::drawImage_task217400_data()
3437 {
3438 QTest::addColumn<QImage::Format>("format");
3439
3440 QTest::newRow("444") << QImage::Format_ARGB4444_Premultiplied;
3441 QTest::newRow("555") << QImage::Format_ARGB8555_Premultiplied;
3442 QTest::newRow("565") << QImage::Format_ARGB8565_Premultiplied;
3443 // QTest::newRow("666") << QImage::Format_ARGB6666_Premultiplied;
3444 QTest::newRow("888p") << QImage::Format_ARGB32_Premultiplied;
3445 QTest::newRow("888") << QImage::Format_ARGB32;
3446 }
3447
3448 void tst_QPainter::drawImage_task217400()
3449 {
3450 QFETCH(QImage::Format, format);
3451
3452 const QImage src = QImage(QFINDTESTDATA("task217400.png"))
3453 .convertToFormat(format);
3454 QVERIFY(!src.isNull());
3455
3456 QImage expected(src.size(), format);
3457 {
3458 QPainter p(&expected);
3459 p.fillRect(0, 0, expected.width(), expected.height(), Qt::white);
3460 p.drawImage(0, 0, src);
3461 }
3462
3463 for (int i = 1; i <= 4; ++i) {
3464 QImage dest(src.width() + i, src.height(), format);
3465 {
3466 QPainter p(&dest);
3467 p.fillRect(0, 0, dest.width(), dest.height(), Qt::white);
3468 p.drawImage(i, 0, src);
3469 }
3470
3471 const QImage result = dest.copy(i, 0, src.width(), src.height());
3472
3473 QCOMPARE(result, expected);
3474 }
3475 }
3476
3477 void tst_QPainter::drawImage_task258776()
3478 {
3479 QImage src(16, 16, QImage::Format_RGB888);
3480 QImage dest(33, 33, QImage::Format_RGB888);
3481 src.fill(0x00ff00);
3482 dest.fill(0xff0000);
3483
3484 QPainter painter(&dest);
3485 painter.drawImage(QRectF(0.499, 0.499, 32, 32), src, QRectF(0, 0, 16, 16));
3486 painter.end();
3487
3488 QImage expected(33, 33, QImage::Format_RGB32);
3489 expected.fill(0xff0000);
3490
3491 painter.begin(&expected);
3492 painter.drawImage(QRectF(0, 0, 32, 32), src);
3493 painter.end();
3494
3495 dest = dest.convertToFormat(QImage::Format_RGB32);
3496
3497 dest.save("dest.png");
3498 expected.save("expected.png");
3499 QCOMPARE(dest, expected);
3500 }
3501
3502 void tst_QPainter::drawImage_QTBUG28324()
3503 {
3504 QImage dest(512, 512, QImage::Format_ARGB32_Premultiplied);
3505 dest.fill(0x0);
3506
3507 int x = 263; int y = 89; int w = 61; int h = 39;
3508
3509 QImage source(w, h, QImage::Format_ARGB32_Premultiplied);
3510 quint32 *b = (quint32 *)source.bits();
3511 for (int j = 0; j < w * h; ++j)
3512 b[j] = 0x7f7f7f7f;
3513
3514 // nothing to test here since the bug is about
3515 // an invalid memory read, which valgrind
3516 // would complain about
3517 QPainter p(&dest);
3518 p.drawImage(x, y, source);
3519 }
3520
3521 void tst_QPainter::clipRectSaveRestore()
3522 {
3523 QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
3524 img.fill(0x0);
3525
3526 QPainter p(&img);
3527 p.setClipRect(QRect(0, 0, 10, 10));
3528 p.save();
3529 p.setClipRect(QRect(5, 5, 5, 5), Qt::IntersectClip);
3530 p.restore();
3531 p.fillRect(0, 0, 64, 64, Qt::black);
3532 p.end();
3533
3534 QCOMPARE(img.pixel(0, 0), QColor(Qt::black).rgba());
3535 }
3536
3537 void tst_QPainter::clipStateSaveRestore()
3538 {
3539 QImage img(16, 16, QImage::Format_RGB32);
3540 img.fill(Qt::blue);
3541 {
3542 QPainter p(&img);
3543 p.setClipRect(QRect(5, 5, 10, 10));
3544 p.save();
3545 p.setClipping(false);
3546 p.restore();
3547 p.fillRect(0, 0, 16, 16, Qt::red);
3548 p.end();
3549 QCOMPARE(img.pixel(0, 0), QColor(Qt::blue).rgb());
3550 }
3551
3552 img.fill(Qt::blue);
3553 {
3554 QPainter p(&img);
3555 p.setClipRect(QRect(5, 5, 10, 10));
3556 p.setClipping(false);
3557 p.save();
3558 p.setClipping(true);
3559 p.restore();
3560 p.fillRect(0, 0, 16, 16, Qt::red);
3561 p.end();
3562 QCOMPARE(img.pixel(0, 0), QColor(Qt::red).rgb());
3563 }
3564 }
3565
3566 void tst_QPainter::clippedImage()
3567 {
3568 QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
3569 img.fill(0x0);
3570
3571 QImage src(16, 16, QImage::Format_RGB32);
3572 src.fill(QColor(Qt::red).rgba());
3573
3574 QPainter p(&img);
3575 p.setClipRect(QRect(1, 1, 14, 14));
3576 p.drawImage(0, 0, src);
3577 p.end();
3578
3579 QCOMPARE(img.pixel(0, 0), 0x0U);
3580 QCOMPARE(img.pixel(1, 1), src.pixel(1, 1));
3581 }
3582
3583 void tst_QPainter::stateResetBetweenQPainters()
3584 {
3585 QImage img(16, 16, QImage::Format_ARGB32);
3586
3587 {
3588 QPainter p(&img);
3589 p.setCompositionMode(QPainter::CompositionMode_Source);
3590 p.fillRect(0, 0, 16, 16, Qt::red);
3591 }
3592
3593 {
3594 QPainter p2(&img);
3595 p2.fillRect(0, 0, 16, 16, QColor(0, 0, 255, 63));
3596 }
3597
3598 img.save("foo.png");
3599
3600 QVERIFY(img.pixel(0, 0) != qRgba(0, 0, 255, 63));
3601 QVERIFY(qRed(img.pixel(0, 0)) > 0); // We didn't erase the red channel...
3602 QVERIFY(qBlue(img.pixel(0, 0)) < 255); // We blended the blue channel
3603 }
3604
3605 void tst_QPainter::drawRect_task215378()
3606 {
3607 QImage img(11, 11, QImage::Format_ARGB32_Premultiplied);
3608 img.fill(QColor(Qt::white).rgba());
3609
3610 QPainter p(&img);
3611 p.setPen(QColor(127, 127, 127, 127));
3612 p.drawRect(0, 0, 10, 10);
3613 p.end();
3614
3615 QCOMPARE(img.pixel(0, 0), img.pixel(1, 0));
3616 QCOMPARE(img.pixel(0, 0), img.pixel(0, 1));
3617 QVERIFY(img.pixel(0, 0) != img.pixel(1, 1));
3618 }
3619
3620 void tst_QPainter::drawRect_task247505()
3621 {
3622 QImage a(10, 10, QImage::Format_ARGB32_Premultiplied);
3623 a.fill(0);
3624 QImage b = a;
3625
3626 QPainter p(&a);
3627 p.setPen(Qt::NoPen);
3628 p.setBrush(Qt::black);
3629 p.drawRect(QRectF(10, 0, -10, 10));
3630 p.end();
3631 p.begin(&b);
3632 p.setPen(Qt::NoPen);
3633 p.setBrush(Qt::black);
3634 p.drawRect(QRectF(0, 0, 10, 10));
3635 p.end();
3636
3637 QCOMPARE(a, b);
3638 }
3639
3640 void tst_QPainter::drawImage_data()
3641 {
3642 QTest::addColumn<int>("x");
3643 QTest::addColumn<int>("y");
3644 QTest::addColumn<int>("w");
3645 QTest::addColumn<int>("h");
3646 QTest::addColumn<QImage::Format>("srcFormat");
3647 QTest::addColumn<QImage::Format>("dstFormat");
3648
3649 for (int srcFormat = QImage::Format_Mono; srcFormat < QImage::NImageFormats; ++srcFormat) {
3650 for (int dstFormat = QImage::Format_Mono; dstFormat < QImage::NImageFormats; ++dstFormat) {
3651 // Indexed8 can't be painted to, and Alpha8 can't hold a color.
3652 if (dstFormat == QImage::Format_Indexed8 || dstFormat == QImage::Format_Alpha8)
3653 continue;
3654 for (int odd_x = 0; odd_x <= 1; ++odd_x) {
3655 for (int odd_width = 0; odd_width <= 1; ++odd_width) {
3656 QTest::addRow("srcFormat %d, dstFormat %d, odd x: %d, odd width: %d",
3657 srcFormat, dstFormat, odd_x, odd_width)
3658 << (10 + odd_x) << 10 << (20 + odd_width) << 20
3659 << QImage::Format(srcFormat)
3660 << QImage::Format(dstFormat);
3661 }
3662 }
3663 }
3664 }
3665 }
3666
3667 bool verifyImage(const QImage &img, int x, int y, int w, int h, uint background)
3668 {
3669 int imgWidth = img.width();
3670 int imgHeight = img.height();
3671 for (int i = 0; i < imgHeight; ++i) {
3672 for (int j = 0; j < imgWidth; ++j) {
3673 uint pixel = img.pixel(j, i);
3674 bool outside = j < x || j >= (x + w) || i < y || i >= (y + h);
3675 if (outside != (pixel == background)) {
3676 //printf("%d %d, expected %x, got %x, outside: %d\n", x, y, background, pixel, outside);
3677 return false;
3678 }
3679 }
3680 }
3681
3682 return true;
3683 }
3684
3685 void tst_QPainter::drawImage()
3686 {
3687 QFETCH(int, x);
3688 QFETCH(int, y);
3689 QFETCH(int, w);
3690 QFETCH(int, h);
3691 QFETCH(QImage::Format, srcFormat);
3692 QFETCH(QImage::Format, dstFormat);
3693
3694 QImage dst(40, 40, QImage::Format_RGB32);
3695 dst.fill(0xffffffff);
3696
3697 dst = dst.convertToFormat(dstFormat);
3698 uint background = dst.pixel(0, 0);
3699
3700 QImage src(w, h, QImage::Format_RGB32);
3701 src.fill(0xff000000);
3702 src = src.convertToFormat(srcFormat);
3703
3704 QPainter p(&dst);
3705 p.drawImage(x, y, src);
3706 p.end();
3707
3708 QVERIFY(verifyImage(dst, x, y, w, h, background));
3709 }
3710
3711 void tst_QPainter::imageCoordinateLimit()
3712 {
3713 QImage img(64, 40000, QImage::Format_MonoLSB);
3714 QPainter p(&img);
3715 p.drawText(10, 36000, QLatin1String("foo"));
3716 p.setPen(QPen(Qt::black, 2));
3717 p.drawLine(10, 0, 60, 40000);
3718
3719 p.setRenderHint(QPainter::Antialiasing);
3720 p.drawLine(10, 0, 60, 40000);
3721 }
3722
3723
3724 void tst_QPainter::imageBlending_data()
3725 {
3726 QTest::addColumn<QImage::Format>("sourceFormat");
3727 QTest::addColumn<QImage::Format>("destFormat");
3728 QTest::addColumn<int>("error");
3729
3730 int error_rgb565 = ((1<<3) + (1<<2) + (1<<3));
3731 QTest::newRow("rgb565_on_rgb565") << QImage::Format_RGB16
3732 << QImage::Format_RGB16
3733 << 0;
3734 QTest::newRow("argb8565_on_rgb565") << QImage::Format_ARGB8565_Premultiplied
3735 << QImage::Format_RGB16
3736 << error_rgb565;
3737
3738 QTest::newRow("rgb32_on_rgb565") << QImage::Format_RGB32
3739 << QImage::Format_RGB16
3740 << error_rgb565;
3741
3742 QTest::newRow("argb32pm_on_rgb565") << QImage::Format_ARGB32_Premultiplied
3743 << QImage::Format_RGB16
3744 << error_rgb565;
3745 }
3746
3747 int diffColor(quint32 ap, quint32 bp)
3748 {
3749 int a = qAlpha(ap) - qAlpha(bp);
3750 int r = qRed(ap) - qRed(bp);
3751 int b = qBlue(ap) - qBlue(bp);
3752 int g = qBlue(ap) - qBlue(bp);
3753
3754 return qAbs(a) + qAbs(r) + qAbs(g) + qAbs(b);
3755 }
3756
3757 // this test assumes premultiplied pixels...
3758
3759 void tst_QPainter::imageBlending()
3760 {
3761 QFETCH(QImage::Format, sourceFormat);
3762 QFETCH(QImage::Format, destFormat);
3763 QFETCH(int, error);
3764
3765 QImage dest;
3766 {
3767 QImage orig_dest(6, 6, QImage::Format_ARGB32_Premultiplied);
3768 orig_dest.fill(0);
3769 QPainter p(&orig_dest);
3770 p.fillRect(0, 0, 6, 3, QColor::fromRgbF(1, 0, 0));
3771 p.fillRect(3, 0, 3, 6, QColor::fromRgbF(0, 0, 1, 0.5));
3772 p.end();
3773 dest = orig_dest.convertToFormat(destFormat);
3774
3775 // An image like this: (r = red, m = magenta, b = light alpha blue, 0 = transparent)
3776 // r r r m m m
3777 // r r r m m m
3778 // r r r m m m
3779 // 0 0 0 b b b
3780 // 0 0 0 b b b
3781 // 0 0 0 b b b
3782 }
3783
3784 QImage source;
3785 {
3786 QImage orig_source(6, 6, QImage::Format_ARGB32_Premultiplied);
3787 orig_source.fill(0);
3788 QPainter p(&orig_source);
3789 p.fillRect(1, 1, 4, 4, QColor::fromRgbF(0, 1, 0, 0.5));
3790 p.fillRect(2, 2, 2, 2, QColor::fromRgbF(0, 1, 0));
3791 p.end();
3792 source = orig_source.convertToFormat(sourceFormat);
3793
3794 // An image like this: (0 = transparent, . = green at 0.5 alpha, g = opaque green.
3795 // 0 0 0 0 0 0
3796 // 0 . . . . 0
3797 // 0 . g g . 0
3798 // 0 . g g . 0
3799 // 0 . . . . 0
3800 // 0 0 0 0 0 0
3801 }
3802
3803 QPainter p(&dest);
3804 p.drawImage(0, 0, source);
3805 p.end();
3806
3807 // resulting image:
3808 // r r r m m m
3809 // r r. r. m. m. m
3810 // r r. g g m. m
3811 // 0 . g g b. b
3812 // 0 . . b. b. b
3813 // 0 0 0 b b b
3814
3815 // the g pixels, always green..
3816 QVERIFY(diffColor(dest.pixel(2, 2), 0xff00ff00) <= error); // g
3817
3818 if (source.hasAlphaChannel()) {
3819 QVERIFY(diffColor(dest.pixel(0, 0), 0xffff0000) <= error); // r
3820 QVERIFY(diffColor(dest.pixel(5, 0), 0xff7f007f) <= error); // m
3821 QVERIFY(diffColor(dest.pixel(1, 1), 0xff7f7f00) <= error); // r.
3822 QVERIFY(diffColor(dest.pixel(4, 1), 0xff3f7f3f) <= error); // m.
3823 if (dest.hasAlphaChannel()) {
3824 QVERIFY(diffColor(dest.pixel(1, 3), 0x7f007f00) <= error); // .
3825 QVERIFY(diffColor(dest.pixel(4, 3), 0x7f007f3f) <= error); // b.
3826 QVERIFY(diffColor(dest.pixel(4, 3), 0x7f007f3f) <= error); // b.
3827 QVERIFY(diffColor(dest.pixel(4, 4), 0x7f00007f) <= error); // b
3828 QVERIFY(diffColor(dest.pixel(4, 0), 0) <= 0); // 0
3829 }
3830 } else {
3831 QVERIFY(diffColor(dest.pixel(0, 0), 0xff000000) <= 0);
3832 QVERIFY(diffColor(dest.pixel(1, 1), 0xff007f00) <= error);
3833 }
3834 }
3835
3836 void tst_QPainter::imageBlending_clipped()
3837 {
3838 QImage src(20, 20, QImage::Format_RGB16);
3839 QPainter p(&src);
3840 p.fillRect(src.rect(), Qt::red);
3841 p.end();
3842
3843 QImage dst(40, 20, QImage::Format_RGB16);
3844 p.begin(&dst);
3845 p.fillRect(dst.rect(), Qt::white);
3846 p.end();
3847
3848 QImage expected = dst;
3849
3850 p.begin(&dst);
3851 p.setClipRect(QRect(23, 0, 20, 20));
3852
3853 // should be completely clipped
3854 p.drawImage(QRectF(3, 0, 20, 20), src);
3855 p.end();
3856
3857 // dst should be left unchanged
3858 QCOMPARE(dst, expected);
3859 }
3860
3861 void tst_QPainter::paintOnNullPixmap()
3862 {
3863 QPixmap pix(16, 16);
3864
3865 QPixmap textPixmap;
3866 QPainter p(&textPixmap);
3867 p.drawPixmap(10, 10, pix);
3868 p.end();
3869
3870 QPixmap textPixmap2(16,16);
3871 p.begin(&textPixmap2);
3872 p.end();
3873 }
3874
3875 void tst_QPainter::checkCompositionMode()
3876 {
3877 QImage refImage(50,50,QImage::Format_ARGB32);
3878 QPainter painter(&refImage);
3879 painter.fillRect(QRect(0,0,50,50),Qt::blue);
3880
3881 QImage testImage(50,50,QImage::Format_ARGB32);
3882 QPainter p(&testImage);
3883 p.fillRect(QRect(0,0,50,50),Qt::red);
3884 p.save();
3885 p.setCompositionMode(QPainter::CompositionMode_SourceOut);
3886 p.restore();
3887 p.fillRect(QRect(0,0,50,50),Qt::blue);
3888
3889 QCOMPARE(refImage.pixel(20,20),testImage.pixel(20,20));
3890 }
3891
3892 static QLinearGradient inverseGradient(QLinearGradient g)
3893 {
3894 QLinearGradient g2 = g;
3895
3896 QGradientStops stops = g.stops();
3897
3898 QGradientStops inverse;
3899 foreach (QGradientStop stop, stops)
3900 inverse << QGradientStop(1 - stop.first, stop.second);
3901
3902 g2.setStops(inverse);
3903 return g2;
3904 }
3905
3906 void tst_QPainter::linearGradientSymmetry_data()
3907 {
3908 QTest::addColumn<QGradientStops>("stops");
3909
3910 if (sizeof(qreal) != sizeof(float)) {
3911 QGradientStops stops;
3912 stops << qMakePair(qreal(0.0), QColor(Qt::blue));
3913 stops << qMakePair(qreal(0.2), QColor(220, 220, 220, 0));
3914 stops << qMakePair(qreal(0.6), QColor(Qt::red));
3915 stops << qMakePair(qreal(0.9), QColor(220, 220, 220, 255));
3916 stops << qMakePair(qreal(1.0), QColor(Qt::black));
3917 QTest::newRow("multiple stops") << stops;
3918 }
3919
3920 {
3921 QGradientStops stops;
3922 stops << qMakePair(qreal(0.0), QColor(Qt::blue));
3923 stops << qMakePair(qreal(1.0), QColor(Qt::black));
3924 QTest::newRow("two stops") << stops;
3925 }
3926
3927 if (sizeof(qreal) != sizeof(float)) {
3928 QGradientStops stops;
3929 stops << qMakePair(qreal(0.3), QColor(Qt::blue));
3930 stops << qMakePair(qreal(0.6), QColor(Qt::black));
3931 QTest::newRow("two stops 2") << stops;
3932 }
3933 }
3934
3935 void tst_QPainter::linearGradientSymmetry()
3936 {
3937 QFETCH(QGradientStops, stops);
3938
3939 QImage a(64, 8, QImage::Format_ARGB32_Premultiplied);
3940 QImage b(64, 8, QImage::Format_ARGB32_Premultiplied);
3941
3942 a.fill(0);
3943 b.fill(0);
3944
3945 QLinearGradient gradient(QRectF(b.rect()).topLeft(), QRectF(b.rect()).topRight());
3946 gradient.setStops(stops);
3947
3948 QPainter pa(&a);
3949 pa.fillRect(a.rect(), gradient);
3950 pa.end();
3951
3952 QPainter pb(&b);
3953 pb.fillRect(b.rect(), inverseGradient(gradient));
3954 pb.end();
3955
3956 b = b.mirrored(true);
3957 QCOMPARE(a, b);
3958 }
3959
3960 void tst_QPainter::gradientPixelFormat_data()
3961 {
3962 QTest::addColumn<QImage::Format>("format");
3963
3964 QTest::newRow("argb32") << QImage::Format_ARGB32;
3965 QTest::newRow("rgb32") << QImage::Format_RGB32;
3966 QTest::newRow("rgb888") << QImage::Format_RGB888;
3967 QTest::newRow("rgbx8888") << QImage::Format_RGBX8888;
3968 QTest::newRow("rgba8888") << QImage::Format_RGBA8888;
3969 QTest::newRow("rgba8888_pm") << QImage::Format_RGBA8888_Premultiplied;
3970 QTest::newRow("rgbx64") << QImage::Format_RGBX64;
3971 QTest::newRow("rgba64_pm") << QImage::Format_RGBA64_Premultiplied;
3972 }
3973
3974 void tst_QPainter::gradientPixelFormat()
3975 {
3976 QFETCH(QImage::Format, format);
3977
3978 QImage a(8, 64, QImage::Format_ARGB32_Premultiplied);
3979 QImage b(8, 64, format);
3980
3981
3982 QGradientStops stops;
3983 stops << qMakePair(qreal(0.0), QColor(Qt::blue));
3984 stops << qMakePair(qreal(0.3), QColor(Qt::red));
3985 stops << qMakePair(qreal(0.6), QColor(Qt::green));
3986 stops << qMakePair(qreal(1.0), QColor(Qt::black));
3987
3988 a.fill(0);
3989 b.fill(0);
3990
3991 QLinearGradient gradient(QRectF(b.rect()).topLeft(), QRectF(b.rect()).bottomLeft());
3992 gradient.setStops(stops);
3993
3994 QPainter pa(&a);
3995 pa.fillRect(a.rect(), gradient);
3996 pa.end();
3997
3998 QPainter pb(&b);
3999 pb.fillRect(b.rect(), gradient);
4000 pb.end();
4001
4002 QCOMPARE(a, b.convertToFormat(QImage::Format_ARGB32_Premultiplied));
4003 }
4004
4005 void tst_QPainter::gradientInterpolation()
4006 {
4007 QImage image(256, 8, QImage::Format_ARGB32_Premultiplied);
4008 QPainter painter;
4009
4010 QLinearGradient gradient(QRectF(image.rect()).topLeft(), QRectF(image.rect()).topRight());
4011 gradient.setColorAt(0.0, QColor(255, 0, 0, 0));
4012 gradient.setColorAt(1.0, Qt::blue);
4013
4014 image.fill(0);
4015 painter.begin(&image);
4016 painter.fillRect(image.rect(), gradient);
4017 painter.end();
4018
4019 const QRgb *line = reinterpret_cast<QRgb *>(image.scanLine(3));
4020
4021 for (int i = 0; i < 256; ++i) {
4022 QCOMPARE(qAlpha(line[i]), qBlue(line[i])); // bright blue
4023 QVERIFY(qAbs(qAlpha(line[i]) - i) < 3); // linear alpha
4024 QCOMPARE(qRed(line[i]), 0); // no red component
4025 QCOMPARE(qGreen(line[i]), 0); // no green component
4026 }
4027
4028 gradient.setInterpolationMode(QGradient::ComponentInterpolation);
4029
4030 image.fill(0);
4031 painter.begin(&image);
4032 painter.fillRect(image.rect(), gradient);
4033 painter.end();
4034
4035 for (int i = 1; i < 256; ++i) {
4036 if (i < 128) {
4037 QVERIFY(qRed(line[i]) >= qBlue(line[i])); // red is dominant
4038 } else {
4039 QVERIFY(qRed(line[i]) <= qBlue(line[i])); // blue is dominant
4040 }
4041 QVERIFY((qRed(line[i]) - 0.5) * (qAlpha(line[i - 1]) - 0.5) <= (qRed(line[i - 1]) + 0.5) * (qAlpha(line[i]) + 0.5)); // decreasing red
4042 QVERIFY((qBlue(line[i]) + 0.5) * (qAlpha(line[i - 1]) + 0.5) >= (qBlue(line[i - 1]) - 0.5) * (qAlpha(line[i]) - 0.5)); // increasing blue
4043 QVERIFY(qAbs(qAlpha(line[i]) - i) < 3); // linear alpha
4044 QCOMPARE(qGreen(line[i]), 0); // no green component
4045 }
4046 }
4047
4048 #if QT_CONFIG(raster_64bit)
4049 void tst_QPainter::linearGradientRgb30_data()
4050 {
4051 QTest::addColumn<QColor>("stop0");
4052 QTest::addColumn<QColor>("stop1");
4053
4054 QTest::newRow("white->black") << QColor(Qt::white) << QColor(Qt::black);
4055 QTest::newRow("blue->black") << QColor(Qt::blue) << QColor(Qt::black);
4056 QTest::newRow("white->red") << QColor(Qt::white) << QColor(Qt::red);
4057 }
4058
4059 void tst_QPainter::linearGradientRgb30()
4060 {
4061 QFETCH(QColor, stop0);
4062 QFETCH(QColor, stop1);
4063
4064 QLinearGradient gradient(0, 0, 1000, 1);
4065 gradient.setColorAt(0.0, stop0);
4066 gradient.setColorAt(1.0, stop1);
4067
4068 QImage image(1000, 1, QImage::Format_RGB30);
4069 QPainter painter(&image);
4070 painter.fillRect(image.rect(), gradient);
4071 painter.end();
4072
4073 for (int i = 1; i < 1000; ++i) {
4074 QColor p1 = image.pixelColor(i - 1, 0);
4075 QColor p2 = image.pixelColor(i, 0);
4076 QVERIFY(p1 != p2);
4077 QVERIFY(qGray(p1.rgb()) >= qGray(p2.rgb()));
4078 }
4079 }
4080
4081 void tst_QPainter::radialGradientRgb30_data()
4082 {
4083 linearGradientRgb30_data();
4084 }
4085
4086 void tst_QPainter::radialGradientRgb30()
4087 {
4088 QFETCH(QColor, stop0);
4089 QFETCH(QColor, stop1);
4090
4091 QRadialGradient gradient(0, 0, 1000);
4092 gradient.setColorAt(0.0, stop0);
4093 gradient.setColorAt(1.0, stop1);
4094
4095 QImage image(1000, 1, QImage::Format_A2BGR30_Premultiplied);
4096 QPainter painter(&image);
4097 painter.fillRect(image.rect(), gradient);
4098 painter.end();
4099
4100 for (int i = 1; i < 1000; ++i) {
4101 QColor p1 = image.pixelColor(i - 1, 0);
4102 QColor p2 = image.pixelColor(i, 0);
4103 QVERIFY(p1 != p2);
4104 QVERIFY(qGray(p1.rgb()) >= qGray(p2.rgb()));
4105 }
4106 }
4107 #endif
4108
4109 void tst_QPainter::drawPolygon()
4110 {
4111 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
4112
4113 QPainterPathStroker stroker;
4114 stroker.setWidth(1.5);
4115
4116 QPainterPath path;
4117 path.moveTo(2, 34);
4118 path.lineTo(34, 2);
4119
4120 QPolygonF poly = stroker.createStroke(path).toFillPolygon(QTransform());
4121
4122 img.fill(0xffffffff);
4123 QPainter p(&img);
4124 p.setRenderHint(QPainter::Antialiasing);
4125 p.setBrush(Qt::red);
4126 p.setPen(Qt::NoPen);
4127 p.drawPolygon(poly);
4128 p.translate(64, 64);
4129 p.drawPolygon(poly);
4130 p.end();
4131
4132 QImage a = img.copy();
4133
4134 img.fill(0xffffffff);
4135 p.begin(&img);
4136 p.setRenderHint(QPainter::Antialiasing);
4137 p.setBrush(Qt::red);
4138 p.setPen(Qt::NoPen);
4139 p.translate(64, 64);
4140 p.drawPolygon(poly);
4141 p.resetTransform();
4142 p.drawPolygon(poly);
4143 p.end();
4144
4145 QCOMPARE(a, img);
4146 }
4147
4148 void tst_QPainter::inactivePainter()
4149 {
4150 // This test succeeds if it doesn't segfault.
4151
4152 QPainter p;
4153 QPainterPath path;
4154 QRegion region(QRect(20, 20, 60, 40));
4155 QPolygonF polygon(QVector<QPointF>() << QPointF(0, 0) << QPointF(12, 0) << QPointF(8, 6));
4156 path.addPolygon(polygon);
4157
4158 p.save();
4159 p.restore();
4160
4161 p.background();
4162 p.setBackground(QBrush(Qt::blue));
4163
4164 p.brush();
4165 p.setBrush(Qt::red);
4166 p.setBrush(Qt::NoBrush);
4167 p.setBrush(QBrush(Qt::white, Qt::DiagCrossPattern));
4168
4169 p.backgroundMode();
4170 p.setBackgroundMode(Qt::OpaqueMode);
4171
4172 p.boundingRect(QRectF(0, 0, 100, 20), Qt::AlignCenter, QLatin1String("Hello, World!"));
4173 p.boundingRect(QRect(0, 0, 100, 20), Qt::AlignCenter, QLatin1String("Hello, World!"));
4174
4175 p.brushOrigin();
4176 p.setBrushOrigin(QPointF(12, 34));
4177 p.setBrushOrigin(QPoint(12, 34));
4178
4179 p.clipPath();
4180 p.clipRegion();
4181 p.hasClipping();
4182 p.setClipPath(path);
4183 p.setClipRect(QRectF(42, 42, 42, 42));
4184 p.setClipRect(QRect(42, 42, 42, 42));
4185 p.setClipRegion(region);
4186 p.setClipping(true);
4187
4188 #if QT_DEPRECATED_SINCE(5, 13)
4189 QT_WARNING_PUSH
4190 QT_WARNING_DISABLE_DEPRECATED
4191 p.combinedMatrix();
4192 QT_WARNING_POP
4193 #endif
4194 p.combinedTransform();
4195
4196 p.compositionMode();
4197 p.setCompositionMode(QPainter::CompositionMode_Plus);
4198
4199 p.device();
4200 #if QT_DEPRECATED_SINCE(5, 13)
4201 QT_WARNING_PUSH
4202 QT_WARNING_DISABLE_DEPRECATED
4203 p.deviceMatrix();
4204 QT_WARNING_POP
4205 #endif
4206 p.deviceTransform();
4207
4208 p.font();
4209 p.setFont(QFont(QLatin1String("Times"), 24));
4210
4211 p.fontInfo();
4212 p.fontMetrics();
4213
4214 p.layoutDirection();
4215 p.setLayoutDirection(Qt::RightToLeft);
4216
4217 p.opacity();
4218 p.setOpacity(0.75);
4219
4220 p.pen();
4221 p.setPen(QPen(Qt::red));
4222 p.setPen(Qt::green);
4223 p.setPen(Qt::NoPen);
4224
4225 p.renderHints();
4226 p.setRenderHint(QPainter::Antialiasing, true);
4227 p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, false);
4228
4229 #if QT_DEPRECATED_SINCE(5, 13)
4230 QT_WARNING_PUSH
4231 QT_WARNING_DISABLE_DEPRECATED
4232 p.resetMatrix();
4233 QT_WARNING_POP
4234 #endif
4235 p.resetTransform();
4236 p.rotate(1);
4237 p.scale(2, 2);
4238 p.shear(-1, 1);
4239 p.translate(3, 14);
4240
4241 p.viewTransformEnabled();
4242 p.setViewTransformEnabled(true);
4243
4244 p.viewport();
4245 p.setViewport(QRect(10, 10, 620, 460));
4246
4247 p.window();
4248 p.setWindow(QRect(10, 10, 620, 460));
4249
4250 #if QT_DEPRECATED_SINCE(5, 13)
4251 QT_WARNING_PUSH
4252 QT_WARNING_DISABLE_DEPRECATED
4253 p.worldMatrix();
4254 p.setWorldMatrix(QMatrix().translate(43, 21), true);
4255 QT_WARNING_POP
4256 #endif
4257 p.setWorldMatrixEnabled(true);
4258
4259 p.transform();
4260 p.setTransform(QTransform().translate(12, 34), true);
4261
4262 p.worldTransform();
4263 p.setWorldTransform(QTransform().scale(0.5, 0.5), true);
4264 }
4265
4266 bool testCompositionMode(int src, int dst, int expected, QPainter::CompositionMode op, qreal opacity = 1.0)
4267 {
4268 // The test image needs to be large enough to test SIMD code
4269 const QSize imageSize(100, 100);
4270
4271 QImage actual(imageSize, QImage::Format_ARGB32_Premultiplied);
4272 actual.fill(QColor(dst, dst, dst).rgb());
4273
4274 QPainter p(&actual);
4275 p.setCompositionMode(op);
4276 p.setOpacity(opacity);
4277 p.fillRect(QRect(QPoint(), imageSize), QColor(src, src, src));
4278 p.end();
4279
4280 if (qRed(actual.pixel(0, 0)) != expected) {
4281 qDebug("Fail: mode %d, src[%d] dst [%d] actual [%d] expected [%d]", op,
4282 src, dst, qRed(actual.pixel(0, 0)), expected);
4283 return false;
4284 } else {
4285 QImage refImage(imageSize, QImage::Format_ARGB32_Premultiplied);
4286 refImage.fill(QColor(expected, expected, expected).rgb());
4287 return actual == refImage;
4288 }
4289 }
4290
4291 void tst_QPainter::extendedBlendModes()
4292 {
4293 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Plus));
4294 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Plus));
4295 QVERIFY(testCompositionMode(127, 128, 255, QPainter::CompositionMode_Plus));
4296 QVERIFY(testCompositionMode(127, 0, 127, QPainter::CompositionMode_Plus));
4297 QVERIFY(testCompositionMode( 0, 127, 127, QPainter::CompositionMode_Plus));
4298 QVERIFY(testCompositionMode(255, 0, 255, QPainter::CompositionMode_Plus));
4299 QVERIFY(testCompositionMode( 0, 255, 255, QPainter::CompositionMode_Plus));
4300 QVERIFY(testCompositionMode(128, 128, 255, QPainter::CompositionMode_Plus));
4301
4302 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Plus, 0.3));
4303 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Plus, 0.3));
4304 QVERIFY(testCompositionMode(126, 128, 165, QPainter::CompositionMode_Plus, 0.3));
4305 QVERIFY(testCompositionMode(127, 0, 38, QPainter::CompositionMode_Plus, 0.3));
4306 QVERIFY(testCompositionMode( 0, 127, 127, QPainter::CompositionMode_Plus, 0.3));
4307 QVERIFY(testCompositionMode(255, 0, 76, QPainter::CompositionMode_Plus, 0.3));
4308 QVERIFY(testCompositionMode( 0, 255, 255, QPainter::CompositionMode_Plus, 0.3));
4309 QVERIFY(testCompositionMode(128, 128, 166, QPainter::CompositionMode_Plus, 0.3));
4310 QVERIFY(testCompositionMode(186, 200, 255, QPainter::CompositionMode_Plus, 0.3));
4311
4312 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Multiply));
4313 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Multiply));
4314 QVERIFY(testCompositionMode(127, 255, 127, QPainter::CompositionMode_Multiply));
4315 QVERIFY(testCompositionMode(255, 127, 127, QPainter::CompositionMode_Multiply));
4316 QVERIFY(testCompositionMode( 63, 255, 63, QPainter::CompositionMode_Multiply));
4317 QVERIFY(testCompositionMode(255, 63, 63, QPainter::CompositionMode_Multiply));
4318 QVERIFY(testCompositionMode(127, 127, 63, QPainter::CompositionMode_Multiply));
4319
4320 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Screen));
4321 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Screen));
4322 QVERIFY(testCompositionMode( 63, 255, 255, QPainter::CompositionMode_Screen));
4323 QVERIFY(testCompositionMode(255, 63, 255, QPainter::CompositionMode_Screen));
4324 QVERIFY(testCompositionMode( 63, 0, 63, QPainter::CompositionMode_Screen));
4325 QVERIFY(testCompositionMode( 0, 63, 63, QPainter::CompositionMode_Screen));
4326 QVERIFY(testCompositionMode(127, 127, 191, QPainter::CompositionMode_Screen));
4327
4328 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Overlay));
4329 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Overlay));
4330 QVERIFY(testCompositionMode( 63, 63, 31, QPainter::CompositionMode_Overlay));
4331 QVERIFY(testCompositionMode( 63, 255, 255, QPainter::CompositionMode_Overlay));
4332
4333 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Darken));
4334 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Darken));
4335 QVERIFY(testCompositionMode( 63, 63, 63, QPainter::CompositionMode_Darken));
4336 QVERIFY(testCompositionMode( 63, 255, 63, QPainter::CompositionMode_Darken));
4337 QVERIFY(testCompositionMode(255, 63, 63, QPainter::CompositionMode_Darken));
4338 QVERIFY(testCompositionMode( 63, 127, 63, QPainter::CompositionMode_Darken));
4339 QVERIFY(testCompositionMode(127, 63, 63, QPainter::CompositionMode_Darken));
4340
4341 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_Lighten));
4342 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Lighten));
4343 QVERIFY(testCompositionMode( 63, 63, 63, QPainter::CompositionMode_Lighten));
4344 QVERIFY(testCompositionMode( 63, 255, 255, QPainter::CompositionMode_Lighten));
4345 QVERIFY(testCompositionMode(255, 63, 255, QPainter::CompositionMode_Lighten));
4346 QVERIFY(testCompositionMode( 63, 127, 127, QPainter::CompositionMode_Lighten));
4347 QVERIFY(testCompositionMode(127, 63, 127, QPainter::CompositionMode_Lighten));
4348
4349 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_ColorDodge));
4350 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_ColorDodge));
4351 QVERIFY(testCompositionMode( 63, 127, 169, QPainter::CompositionMode_ColorDodge));
4352 QVERIFY(testCompositionMode(191, 127, 255, QPainter::CompositionMode_ColorDodge));
4353 QVERIFY(testCompositionMode(127, 191, 255, QPainter::CompositionMode_ColorDodge));
4354
4355 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_ColorBurn));
4356 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_ColorBurn));
4357 QVERIFY(testCompositionMode(127, 127, 0, QPainter::CompositionMode_ColorBurn));
4358 QVERIFY(testCompositionMode(128, 128, 2, QPainter::CompositionMode_ColorBurn));
4359 QVERIFY(testCompositionMode(191, 127, 84, QPainter::CompositionMode_ColorBurn));
4360
4361 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_HardLight));
4362 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_HardLight));
4363 QVERIFY(testCompositionMode(127, 127, 127, QPainter::CompositionMode_HardLight));
4364 QVERIFY(testCompositionMode( 63, 63, 31, QPainter::CompositionMode_HardLight));
4365 QVERIFY(testCompositionMode(127, 63, 63, QPainter::CompositionMode_HardLight));
4366
4367 QVERIFY(testCompositionMode(255, 255, 255, QPainter::CompositionMode_SoftLight));
4368 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_SoftLight));
4369 QVERIFY(testCompositionMode(127, 127, 126, QPainter::CompositionMode_SoftLight));
4370 QVERIFY(testCompositionMode( 63, 63, 39, QPainter::CompositionMode_SoftLight));
4371 QVERIFY(testCompositionMode(127, 63, 62, QPainter::CompositionMode_SoftLight));
4372
4373 QVERIFY(testCompositionMode(255, 255, 0, QPainter::CompositionMode_Difference));
4374 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Difference));
4375 QVERIFY(testCompositionMode(255, 0, 255, QPainter::CompositionMode_Difference));
4376 QVERIFY(testCompositionMode(127, 127, 0, QPainter::CompositionMode_Difference));
4377 QVERIFY(testCompositionMode(127, 128, 1, QPainter::CompositionMode_Difference));
4378 QVERIFY(testCompositionMode(127, 63, 64, QPainter::CompositionMode_Difference));
4379 QVERIFY(testCompositionMode( 0, 127, 127, QPainter::CompositionMode_Difference));
4380
4381 QVERIFY(testCompositionMode(255, 255, 0, QPainter::CompositionMode_Exclusion));
4382 QVERIFY(testCompositionMode( 0, 0, 0, QPainter::CompositionMode_Exclusion));
4383 QVERIFY(testCompositionMode(255, 0, 255, QPainter::CompositionMode_Exclusion));
4384 QVERIFY(testCompositionMode(127, 127, 127, QPainter::CompositionMode_Exclusion));
4385 QVERIFY(testCompositionMode( 63, 127, 127, QPainter::CompositionMode_Exclusion));
4386 QVERIFY(testCompositionMode( 63, 63, 95, QPainter::CompositionMode_Exclusion));
4387 QVERIFY(testCompositionMode(191, 191, 96, QPainter::CompositionMode_Exclusion));
4388 }
4389
4390 void tst_QPainter::zeroOpacity()
4391 {
4392 QImage source(1, 1, QImage::Format_ARGB32_Premultiplied);
4393 source.fill(0xffffffff);
4394
4395 QImage target(1, 1, QImage::Format_RGB32);
4396 target.fill(0xff000000);
4397
4398 QPainter p(&target);
4399 p.setOpacity(0.0);
4400 p.drawImage(0, 0, source);
4401 p.end();
4402
4403 QCOMPARE(target.pixel(0, 0), 0xff000000);
4404 }
4405
4406 void tst_QPainter::clippingBug()
4407 {
4408 QImage img(32, 32, QImage::Format_ARGB32_Premultiplied);
4409 img.fill(0);
4410
4411 QImage expected = img;
4412 QPainter p(&expected);
4413 p.fillRect(1, 1, 30, 30, Qt::red);
4414 p.end();
4415
4416 QPainterPath path;
4417 path.addRect(1, 1, 30, 30);
4418 path.addRect(1, 1, 30, 30);
4419 path.addRect(1, 1, 30, 30);
4420
4421 p.begin(&img);
4422 p.setClipPath(path);
4423 p.fillRect(0, 0, 32, 32, Qt::red);
4424 p.end();
4425
4426 QCOMPARE(img, expected);
4427 }
4428
4429 void tst_QPainter::emptyClip()
4430 {
4431 QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
4432 QPainter p(&img);
4433 p.setRenderHints(QPainter::Antialiasing);
4434 p.setClipRect(0, 32, 64, 0);
4435 p.fillRect(0, 0, 64, 64, Qt::white);
4436
4437 QPainterPath path;
4438 path.lineTo(64, 0);
4439 path.lineTo(64, 64);
4440 path.lineTo(40, 64);
4441 path.lineTo(40, 80);
4442 path.lineTo(0, 80);
4443
4444 p.fillPath(path, Qt::green);
4445 }
4446
4447 void tst_QPainter::drawImage_1x1()
4448 {
4449 QImage source(1, 1, QImage::Format_ARGB32_Premultiplied);
4450 source.fill(0xffffffff);
4451
4452 QImage img(32, 32, QImage::Format_ARGB32_Premultiplied);
4453 img.fill(0xff000000);
4454 QPainter p(&img);
4455 p.drawImage(QRectF(0.9, 0.9, 32, 32), source);
4456 p.end();
4457
4458 QImage expected = img;
4459 expected.fill(0xff000000);
4460 p.begin(&expected);
4461 p.fillRect(1, 1, 31, 31, Qt::white);
4462 p.end();
4463
4464 QCOMPARE(img, expected);
4465 }
4466
4467 void tst_QPainter::taskQT4444_dontOverflowDashOffset()
4468 {
4469 QPainter p;
4470
4471 QPen pen;
4472 pen.setWidth(2);
4473 pen.setStyle(Qt::DashDotLine);
4474
4475 QPointF point[4];
4476 point[0] = QPointF(182.50868749707968,347.78457234212630);
4477 point[1] = QPointF(182.50868749707968,107.22501998401277);
4478 point[2] = QPointF(182.50868749707968,107.22501998401277);
4479 point[3] = QPointF(520.46600762283651,107.22501998401277);
4480
4481 QImage crashImage(QSize(1000, 120), QImage::Format_ARGB32_Premultiplied);
4482 p.begin(&crashImage);
4483 p.setPen(pen);
4484 p.drawLines(point, 2);
4485 p.end();
4486
4487 QVERIFY(true); // Don't crash
4488 }
4489
4490 void tst_QPainter::painterBegin()
4491 {
4492 QImage nullImage;
4493 QImage indexed8Image(16, 16, QImage::Format_Indexed8);
4494 QImage rgb32Image(16, 16, QImage::Format_RGB32);
4495 QImage argb32Image(16, 16, QImage::Format_ARGB32_Premultiplied);
4496
4497 QPainter p;
4498
4499 // Painting on null image should fail.
4500 QVERIFY(!p.begin(&nullImage));
4501
4502 // Check that the painter is not messed up by using it on another image.
4503 QVERIFY(p.begin(&rgb32Image));
4504 QVERIFY(p.end());
4505
4506 // If painting on indexed8 image fails, the painter state should still be OK.
4507 if (p.begin(&indexed8Image))
4508 QVERIFY(p.end());
4509 QVERIFY(p.begin(&rgb32Image));
4510 QVERIFY(p.end());
4511
4512 // Try opening a painter on the two different images.
4513 QVERIFY(p.begin(&rgb32Image));
4514 QVERIFY(!p.begin(&argb32Image));
4515 QVERIFY(p.end());
4516
4517 // Try opening two painters on the same image.
4518 QVERIFY(p.begin(&rgb32Image));
4519 QPainter q;
4520 QVERIFY(!q.begin(&rgb32Image));
4521 QVERIFY(!q.end());
4522 QVERIFY(p.end());
4523
4524 // Try ending an inactive painter.
4525 QVERIFY(!p.end());
4526 }
4527
4528 void tst_QPainter::setPenColor(QPainter& p)
4529 {
4530 p.setPen(Qt::NoPen);
4531
4532 // Setting color, then style
4533 // Should work even though the pen is "NoPen with color", temporarily.
4534 QPen newPen(p.pen());
4535 newPen.setColor(Qt::red);
4536 QCOMPARE(p.pen().style(), newPen.style());
4537 QCOMPARE(p.pen().style(), Qt::NoPen);
4538 p.setPen(newPen);
4539
4540 QCOMPARE(p.pen().color().name(), QString("#ff0000"));
4541
4542 QPen newPen2(p.pen());
4543 newPen2.setStyle(Qt::SolidLine);
4544 p.setPen(newPen2);
4545
4546 QCOMPARE(p.pen().color().name(), QString("#ff0000"));
4547 }
4548
4549 void tst_QPainter::setPenColorOnImage()
4550 {
4551 QImage img(QSize(10, 10), QImage::Format_ARGB32_Premultiplied);
4552 QPainter p(&img);
4553 setPenColor(p);
4554 }
4555
4556 void tst_QPainter::setPenColorOnPixmap()
4557 {
4558 QPixmap pix(10, 10);
4559 QPainter p(&pix);
4560 setPenColor(p);
4561 }
4562
4563 #ifndef QT_NO_WIDGETS
4564 class TestProxy : public QGraphicsProxyWidget
4565 {
4566 public:
4567 TestProxy() : QGraphicsProxyWidget() {}
4568 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
4569 {
4570 QGraphicsProxyWidget::paint(painter, option, widget);
4571 deviceTransform = painter->deviceTransform();
4572 }
4573 QTransform deviceTransform;
4574 };
4575
4576 class TestWidget : public QWidget
4577 {
4578 Q_OBJECT
4579 public:
4580 TestWidget() : QWidget(), painted(false) {}
4581 void paintEvent(QPaintEvent *)
4582 {
4583 QPainter p(this);
4584 deviceTransform = p.deviceTransform();
4585 worldTransform = p.worldTransform();
4586 painted = true;
4587 }
4588 QTransform deviceTransform;
4589 QTransform worldTransform;
4590 bool painted;
4591 };
4592
4593 void tst_QPainter::QTBUG5939_attachPainterPrivate()
4594 {
4595 QWidget *w = new QWidget();
4596 QGraphicsScene *scene = new QGraphicsScene();
4597 QGraphicsView *view = new QGraphicsView(scene, w);
4598 view->move(50 ,50);
4599 TestProxy *proxy = new TestProxy();
4600 TestWidget *widget = new TestWidget();
4601 proxy->setWidget(widget);
4602 scene->addItem(proxy);
4603 proxy->setTransform(QTransform().rotate(45));
4604 w->resize(scene->sceneRect().size().toSize());
4605
4606 w->show();
4607 QTRY_VERIFY(widget->painted);
4608
4609 QVERIFY(widget->worldTransform.isIdentity());
4610 QCOMPARE(widget->deviceTransform, proxy->deviceTransform);
4611 }
4612 #endif
4613
4614 void tst_QPainter::clipBoundingRect()
4615 {
4616 QPixmap pix(500, 500);
4617
4618 QPainter p(&pix);
4619
4620 // Test a basic rectangle
4621 p.setClipRect(100, 100, 200, 100);
4622 QVERIFY(p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4623 QVERIFY(!p.clipBoundingRect().contains(QRectF(50, 50, 300, 200)));
4624 p.setClipRect(120, 120, 20, 20, Qt::IntersectClip);
4625 QVERIFY(p.clipBoundingRect().contains(QRect(120, 120, 20, 20)));
4626 QVERIFY(!p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4627
4628 // Test a basic float rectangle
4629 p.setClipRect(QRectF(100, 100, 200, 100));
4630 QVERIFY(p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4631 QVERIFY(!p.clipBoundingRect().contains(QRectF(50, 50, 300, 200)));
4632 p.setClipRect(QRectF(120, 120, 20, 20), Qt::IntersectClip);
4633 QVERIFY(p.clipBoundingRect().contains(QRect(120, 120, 20, 20)));
4634 QVERIFY(!p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4635
4636 // Test a basic path + region
4637 QPainterPath path;
4638 path.addRect(100, 100, 200, 100);
4639 p.setClipPath(path);
4640 QVERIFY(p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4641 QVERIFY(!p.clipBoundingRect().contains(QRectF(50, 50, 300, 200)));
4642 p.setClipRegion(QRegion(120, 120, 20, 20), Qt::IntersectClip);
4643 QVERIFY(p.clipBoundingRect().contains(QRect(120, 120, 20, 20)));
4644 QVERIFY(!p.clipBoundingRect().contains(QRectF(100, 100, 200, 100)));
4645
4646 p.setClipRect(0, 0, 500, 500);
4647 p.translate(250, 250);
4648 for (int i=0; i<360; ++i) {
4649 p.rotate(1);
4650 p.setClipRect(-100, -100, 200, 200, Qt::IntersectClip);
4651 }
4652 QVERIFY(p.clipBoundingRect().contains(QRectF(-100, -100, 200, 200)));
4653 QVERIFY(!p.clipBoundingRect().contains(QRectF(-250, -250, 500, 500)));
4654
4655 }
4656
4657 void tst_QPainter::transformedClip()
4658 {
4659 QImage img(8, 4, QImage::Format_ARGB32_Premultiplied);
4660 QImage img2(img.size(), img.format());
4661 QRect clip(0, 0, 2, 1);
4662 QTransform xf;
4663 xf.translate(0.2, 0);
4664 xf.scale(2.2, 1);
4665 // setClipRect(QRectF)
4666 {
4667 img.fill(Qt::green);
4668 QPainter p(&img);
4669 p.setTransform(xf);
4670 p.setClipRect(QRectF(clip));
4671 p.fillRect(img.rect(), Qt::white);
4672 }
4673 // setClipRect(QRect)
4674 {
4675 img2.fill(Qt::green);
4676 QPainter p(&img2);
4677 p.setTransform(xf);
4678 p.setClipRect(clip);
4679 p.fillRect(img2.rect(), Qt::white);
4680 QCOMPARE(img, img2);
4681 }
4682 // setClipRegion
4683 {
4684 img2.fill(Qt::green);
4685 QPainter p(&img2);
4686 p.setTransform(xf);
4687 p.setClipRegion(QRegion(clip) + QRect(0, 3, 1, 1)); // dummy extra rect to avoid single-rect codepath
4688 p.fillRect(img2.rect(), Qt::white);
4689 QCOMPARE(img.copy(0, 0, 8, 2), img2.copy(0, 0, 8, 2));
4690 }
4691 // setClipPath
4692 {
4693 img2.fill(Qt::green);
4694 QPainter p(&img2);
4695 p.setTransform(xf);
4696 QPainterPath path;
4697 path.addRect(clip);
4698 p.setClipPath(path);
4699 p.fillRect(img2.rect(), Qt::white);
4700 QCOMPARE(img, img2);
4701 }
4702 }
4703
4704 #if defined(Q_OS_MAC)
4705 // Only Mac supports sub pixel positions in raster engine currently
4706 void tst_QPainter::drawText_subPixelPositionsInRaster_qtbug5053()
4707 {
4708 QFontMetricsF fm(qApp->font());
4709
4710 QImage baseLine(fm.horizontalAdvance(QChar::fromLatin1('e')), fm.height(), QImage::Format_RGB32);
4711 baseLine.fill(Qt::white);
4712 {
4713 QPainter p(&baseLine);
4714 p.setRenderHint(QPainter::Qt4CompatiblePainting);
4715 p.drawText(0, fm.ascent(), QString::fromLatin1("e"));
4716 }
4717
4718 bool foundDifferentRasterization = false;
4719 for (int i=1; i<12; ++i) {
4720 QImage comparison(baseLine.size(), QImage::Format_RGB32);
4721 comparison.fill(Qt::white);
4722
4723 {
4724 QPainter p(&comparison);
4725 p.setRenderHint(QPainter::Qt4CompatiblePainting);
4726 p.drawText(QPointF(i / 12.0, fm.ascent()), QString::fromLatin1("e"));
4727 }
4728
4729 if (comparison != baseLine) {
4730 foundDifferentRasterization = true;
4731 break;
4732 }
4733 }
4734
4735 QVERIFY(foundDifferentRasterization);
4736 }
4737 #endif
4738
4739 void tst_QPainter::drawPointScaled()
4740 {
4741 QImage image(32, 32, QImage::Format_RGB32);
4742 image.fill(0xffffffff);
4743
4744 QPainter p(&image);
4745
4746 p.scale(0.1, 0.1);
4747
4748 QPen pen;
4749 pen.setWidth(1000);
4750 pen.setColor(Qt::red);
4751
4752 p.setPen(pen);
4753 p.drawPoint(0, 0);
4754 p.end();
4755
4756 QCOMPARE(image.pixel(16, 16), 0xffff0000);
4757 }
4758
4759 class GradientProducer : public QThread
4760 {
4761 protected:
4762 void run();
4763 };
4764
4765 void GradientProducer::run()
4766 {
4767 QImage image(1, 1, QImage::Format_RGB32);
4768 QPainter p(&image);
4769
4770 for (int i = 0; i < 1000; ++i) {
4771 QLinearGradient g;
4772 g.setColorAt(0, QColor(i % 256, 0, 0));
4773 g.setColorAt(1, Qt::white);
4774
4775 p.fillRect(image.rect(), g);
4776 }
4777 }
4778
4779 void tst_QPainter::QTBUG14614_gradientCacheRaceCondition()
4780 {
4781 const int threadCount = 16;
4782 GradientProducer producers[threadCount];
4783 for (int i = 0; i < threadCount; ++i)
4784 producers[i].start();
4785 for (int i = 0; i < threadCount; ++i)
4786 producers[i].wait();
4787 }
4788
4789 void tst_QPainter::drawTextOpacity()
4790 {
4791 QImage image(32, 32, QImage::Format_RGB32);
4792 image.fill(0xffffffff);
4793
4794 QPainter p(&image);
4795 p.setPen(QColor("#6F6F6F"));
4796 p.setOpacity(0.5);
4797 p.drawText(5, 30, QLatin1String("Qt"));
4798 p.end();
4799
4800 QImage copy = image;
4801 image.fill(0xffffffff);
4802
4803 p.begin(&image);
4804 p.setPen(QColor("#6F6F6F"));
4805 p.drawLine(-10, -10, -1, -1);
4806 p.setOpacity(0.5);
4807 p.drawText(5, 30, QLatin1String("Qt"));
4808 p.end();
4809
4810 QCOMPARE(image, copy);
4811 }
4812
4813 void tst_QPainter::QTBUG17053_zeroDashPattern()
4814 {
4815 QImage image(32, 32, QImage::Format_RGB32);
4816 image.fill(0xffffffff);
4817
4818 QImage original = image;
4819
4820 QVector<qreal> pattern;
4821 pattern << qreal(0) << qreal(0);
4822
4823 QPainter p(&image);
4824 QPen pen(Qt::black, 2.0);
4825 pen.setDashPattern(pattern);
4826
4827 p.setPen(pen);
4828 p.drawLine(0, 0, image.width(), image.height());
4829
4830 QCOMPARE(image, original);
4831 }
4832
4833 void tst_QPainter::QTBUG38781_NoBrushAndQBitmap()
4834 {
4835 QBitmap bitmap(10, 10);
4836 bitmap.fill(Qt::color0);
4837 QPainter p(&bitmap);
4838 p.setPen(Qt::color1);
4839 p.drawLine(0, 1, 9, 1); // at horizontal line at y=1
4840 p.setBrush(Qt::NoBrush);
4841 p.drawRect(0, 0, 9, 9); // a rect all around
4842
4843 QRgb white = qRgb(0xff, 0xff, 0xff);
4844 QRgb black = qRgb(0, 0, 0);
4845 QImage image = bitmap.toImage();
4846 QCOMPARE(image.pixel(0, 0), black);
4847 QCOMPARE(image.pixel(5, 5), white);
4848
4849 // Check that the rect didn't overwrite the line
4850 QCOMPARE(image.pixel(5, 1), black);
4851 }
4852
4853 class TextDrawerThread : public QThread
4854 {
4855 public:
4856 void run();
4857 QImage rendering;
4858 };
4859
4860 void TextDrawerThread::run()
4861 {
4862 rendering = QImage(100, 100, QImage::Format_ARGB32_Premultiplied);
4863 rendering.fill(0);
4864 QPainter p(&rendering);
4865 p.fillRect(10, 10, 100, 100, Qt::blue);
4866 p.setPen(Qt::green);
4867 p.drawText(20, 20, "some text");
4868 p.end();
4869 }
4870
4871 void tst_QPainter::drawTextOutsideGuiThread()
4872 {
4873 QImage referenceRendering(100, 100, QImage::Format_ARGB32_Premultiplied);
4874 referenceRendering.fill(0);
4875 QPainter p(&referenceRendering);
4876 p.fillRect(10, 10, 100, 100, Qt::blue);
4877 p.setPen(Qt::green);
4878 p.drawText(20, 20, "some text");
4879 p.end();
4880
4881 TextDrawerThread t;
4882 t.start();
4883 t.wait();
4884
4885 QCOMPARE(referenceRendering, t.rendering);
4886 }
4887
4888 void tst_QPainter::drawTextWithComplexBrush()
4889 {
4890 QImage texture(10, 10, QImage::Format_ARGB32_Premultiplied);
4891 texture.fill(Qt::red);
4892
4893 QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
4894 image.fill(Qt::white);
4895 QPainter p(&image);
4896 QFont f = p.font();
4897 f.setPixelSize(70);
4898 p.setFont(f);
4899
4900 QBrush brush(Qt::white);
4901 brush.setTextureImage(texture);
4902 p.setPen(QPen(brush, 2));
4903
4904 p.drawText(10, 10, "Hello World");
4905
4906 int paintedPixels = getPaintedPixels(image, Qt::white);
4907 QVERIFY(paintedPixels > 0);
4908 }
4909
4910 void tst_QPainter::QTBUG26013_squareCapStroke()
4911 {
4912 QImage image(4, 4, QImage::Format_RGB32);
4913
4914 QPainter p(&image);
4915 p.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::SquareCap));
4916
4917 for (int i = 0; i < 3; ++i) {
4918 qreal d = i / 3.0;
4919
4920 image.fill(0xffffffff);
4921
4922 p.drawLine(QLineF(0, d, 0, d + 2));
4923 p.drawLine(QLineF(1, d, 3, d));
4924
4925 // ensure that a horizontal line and a vertical line with square cap round up (downwards) at the same time
4926 QCOMPARE(image.pixel(0, 0), image.pixel(1, 0));
4927
4928 image.fill(0xffffffff);
4929
4930 p.drawLine(QLineF(d, 0, d + 2, 0));
4931 p.drawLine(QLineF(d, 1, d, 3));
4932
4933 // ensure that a vertical line and a horizontal line with square cap round up (to the right) at the same time
4934 QCOMPARE(image.pixel(0, 0), image.pixel(0, 1));
4935 }
4936 }
4937
4938 void tst_QPainter::QTBUG25153_drawLine()
4939 {
4940 QImage image(2, 2, QImage::Format_RGB32);
4941
4942 QVector<Qt::PenCapStyle> styles;
4943 styles << Qt::FlatCap << Qt::SquareCap << Qt::RoundCap;
4944
4945 foreach (Qt::PenCapStyle style, styles) {
4946 image.fill(0xffffffff);
4947 QPainter p(&image);
4948 p.setPen(QPen(Qt::black, 0, Qt::SolidLine, style));
4949 p.drawLine(QLineF(0, 0, 0, 0));
4950 p.end();
4951
4952 QCOMPARE(image.pixel(0, 0), 0xff000000);
4953 QCOMPARE(image.pixel(0, 1), 0xffffffff);
4954 QCOMPARE(image.pixel(1, 0), 0xffffffff);
4955 }
4956 }
4957
4958 void tst_QPainter::blendARGBonRGB_data()
4959 {
4960 QTest::addColumn<QImage::Format>("dst_format");
4961 QTest::addColumn<QImage::Format>("src_format");
4962 QTest::addColumn<QPainter::CompositionMode>("compositionMode");
4963 QTest::addColumn<QRgb>("color");
4964 QTest::addColumn<int>("expected_red");
4965
4966 QTest::newRow("ARGB over ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32
4967 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 127 ;
4968 QTest::newRow("ARGB_PM over ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32_Premultiplied
4969 << QPainter::CompositionMode_SourceOver<< qRgba(127, 0, 0, 127) << 127;
4970 QTest::newRow("ARGB source ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32
4971 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 255;
4972 QTest::newRow("ARGB_PM source ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32_Premultiplied
4973 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 255;
4974 QTest::newRow("ARGB source-in ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32
4975 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 255 ;
4976 QTest::newRow("ARGB_PM source-in ARGB32") << QImage::Format_ARGB32 << QImage::Format_ARGB32_Premultiplied
4977 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 255;
4978 QTest::newRow("ARGB over RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32
4979 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 127;
4980 QTest::newRow("ARGB_PM over RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32_Premultiplied
4981 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 127;
4982 QTest::newRow("ARGB source RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32
4983 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 255;
4984 QTest::newRow("ARGB_PM source RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32_Premultiplied
4985 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 255;
4986 QTest::newRow("ARGB source-in RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32
4987 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 255;
4988 QTest::newRow("ARGB_PM source-in RGBA8888") << QImage::Format_RGBA8888 << QImage::Format_ARGB32_Premultiplied
4989 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 255;
4990 // Only ARGB32 and RGBA8888 does inverse premultiply, on the rest over and source gives similar results:
4991 QTest::newRow("ARGB over RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32
4992 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 127;
4993 QTest::newRow("ARGB_PM over RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32_Premultiplied
4994 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 127;
4995 QTest::newRow("ARGB source RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32
4996 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 127;
4997 QTest::newRow("ARGB_PM source RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32_Premultiplied
4998 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 127;
4999 QTest::newRow("ARGB source-in RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32
5000 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 127;
5001 QTest::newRow("ARGB_PM source-in RGB32") << QImage::Format_RGB32 << QImage::Format_ARGB32_Premultiplied
5002 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 127;
5003 QTest::newRow("ARGB over RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32
5004 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 127;
5005 QTest::newRow("ARGB_PM over RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32_Premultiplied
5006 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 127;
5007 QTest::newRow("ARGB source RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32
5008 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 127;
5009 QTest::newRow("ARGB_PM source RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32_Premultiplied
5010 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 127;
5011 QTest::newRow("ARGB source-in RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32
5012 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 127;
5013 QTest::newRow("ARGB_PM source-in RGB888") << QImage::Format_RGB888 << QImage::Format_ARGB32_Premultiplied
5014 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 127;
5015 QTest::newRow("ARGB over RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32
5016 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 127;
5017 QTest::newRow("ARGB_PM over RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32_Premultiplied
5018 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 127;
5019 QTest::newRow("ARGB source RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32
5020 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 127;
5021 QTest::newRow("ARGB_PM source RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32_Premultiplied
5022 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 127;
5023 QTest::newRow("ARGB source-in RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32
5024 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 127;
5025 QTest::newRow("ARGB_PM source-in RGBx8888") << QImage::Format_RGBX8888 << QImage::Format_ARGB32_Premultiplied
5026 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 127;
5027 QTest::newRow("ARGB over RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32
5028 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 123;
5029 QTest::newRow("ARGB_PM over RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32_Premultiplied
5030 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 123;
5031 QTest::newRow("ARGB source RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32
5032 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 123;
5033 QTest::newRow("ARGB_PM source RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32_Premultiplied
5034 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 123;
5035 QTest::newRow("ARGB source-in RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32
5036 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 123;
5037 QTest::newRow("ARGB_PM source-in RGB16") << QImage::Format_RGB16 << QImage::Format_ARGB32_Premultiplied
5038 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 123;
5039 QTest::newRow("ARGB over RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32
5040 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 127) << 125;
5041 QTest::newRow("ARGB_PM over RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32_Premultiplied
5042 << QPainter::CompositionMode_SourceOver << qRgba(127, 0, 0, 127) << 125;
5043 QTest::newRow("ARGB source RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32
5044 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 127) << 125;
5045 QTest::newRow("ARGB_PM source RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32_Premultiplied
5046 << QPainter::CompositionMode_Source << qRgba(127, 0, 0, 127) << 125;
5047 QTest::newRow("ARGB source-in RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32
5048 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 127) << 125;
5049 QTest::newRow("ARGB_PM source-in RGB666") << QImage::Format_RGB666 << QImage::Format_ARGB32_Premultiplied
5050 << QPainter::CompositionMode_SourceIn << qRgba(127, 0, 0, 127) << 125;
5051 QTest::newRow("ARGB over RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32
5052 << QPainter::CompositionMode_SourceOver << qRgba(255, 0, 0, 85) << 85;
5053 QTest::newRow("ARGB_PM over RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32_Premultiplied
5054 << QPainter::CompositionMode_SourceOver << qRgba(85, 0, 0, 85) << 85;
5055 #if QT_CONFIG(raster_64bit)
5056 QTest::newRow("ARGB source RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32
5057 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 85) << 85;
5058 QTest::newRow("ARGB source RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32
5059 << QPainter::CompositionMode_Source << qRgba(255, 0, 0, 120) << 85;
5060 #endif
5061 QTest::newRow("ARGB_PM source RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32_Premultiplied
5062 << QPainter::CompositionMode_Source << qRgba(85, 0, 0, 85) << 85;
5063 #if QT_CONFIG(raster_64bit)
5064 QTest::newRow("ARGB_PM source RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32_Premultiplied
5065 << QPainter::CompositionMode_Source << qRgba(180, 0, 0, 180) << 170;
5066 #endif
5067 QTest::newRow("ARGB source-in RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32
5068 << QPainter::CompositionMode_SourceIn << qRgba(255, 0, 0, 85) << 85;
5069 QTest::newRow("ARGB_PM source-in RGB30") << QImage::Format_RGB30 << QImage::Format_ARGB32_Premultiplied
5070 << QPainter::CompositionMode_SourceIn << qRgba(85, 0, 0, 85) << 85;
5071 }
5072
5073 void tst_QPainter::blendARGBonRGB()
5074 {
5075 QFETCH(QImage::Format, dst_format);
5076 QFETCH(QImage::Format, src_format);
5077 QFETCH(QPainter::CompositionMode, compositionMode);
5078 QFETCH(QRgb, color);
5079 QFETCH(int, expected_red);
5080
5081 QImage imageRgb(16,16, dst_format);
5082 QImage imageArgb(16,16, src_format);
5083 QPainter painter;
5084
5085 imageArgb.fill(color);
5086
5087 imageRgb.fill(Qt::black);
5088 painter.begin(&imageRgb);
5089 painter.setCompositionMode(compositionMode);
5090 painter.drawImage(0, 0, imageArgb);
5091 painter.end();
5092
5093 QCOMPARE(imageRgb.pixelColor(0,0).red(), expected_red);
5094 }
5095
5096 enum CosmeticStrokerPaint
5097 {
5098 Antialiasing,
5099 Dashing
5100 };
5101
5102 static void paint_func(QPainter *p, CosmeticStrokerPaint type)
5103 {
5104 p->save();
5105 switch (type) {
5106 case Antialiasing:
5107 p->setPen(Qt::black);
5108 p->setRenderHint(QPainter::Antialiasing);
5109 p->drawLine(4, 8, 42, 42);
5110 break;
5111 case Dashing:
5112 p->setPen(QPen(Qt::black, 1, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin));
5113 p->drawLine(8, 8, 42, 8);
5114 p->drawLine(42, 8, 42, 42);
5115 p->drawLine(42, 42, 8, 42);
5116 p->drawLine(8, 42, 8, 8);
5117 break;
5118 default:
5119 Q_ASSERT(false);
5120 break;
5121 }
5122 p->restore();
5123 }
5124
5125 Q_DECLARE_METATYPE(CosmeticStrokerPaint)
5126
5127 void tst_QPainter::cosmeticStrokerClipping_data()
5128 {
5129 QTest::addColumn<CosmeticStrokerPaint>("paint");
5130
5131 QTest::newRow("antialiasing_paint") << Antialiasing;
5132 QTest::newRow("dashing_paint") << Dashing;
5133 }
5134
5135 void tst_QPainter::cosmeticStrokerClipping()
5136 {
5137 QFETCH(CosmeticStrokerPaint, paint);
5138
5139 QImage image(50, 50, QImage::Format_RGB32);
5140 image.fill(Qt::white);
5141
5142 QPainter p(&image);
5143 paint_func(&p, paint);
5144 p.end();
5145
5146 QImage old = image.copy();
5147
5148 image.paintEngine()->setSystemClip(QRect(10, 0, image.width() - 10, image.height()));
5149
5150 p.begin(&image);
5151 p.fillRect(image.rect(), Qt::white);
5152 paint_func(&p, paint);
5153
5154 // doing same paint operation again with different system clip should not change the image
5155 QCOMPARE(old, image);
5156
5157 old = image;
5158
5159 p.setClipRect(QRect(20, 20, 30, 30));
5160 p.fillRect(image.rect(), Qt::white);
5161 paint_func(&p, paint);
5162
5163 // ditto for regular clips
5164 QCOMPARE(old, image);
5165 }
5166
5167 void tst_QPainter::RasterOp_NotDestination()
5168 {
5169 QImage image(3, 3, QImage::Format_RGB32);
5170 image.fill(Qt::red);
5171
5172 {
5173 QPainter p(&image);
5174 p.setCompositionMode(QPainter::RasterOp_NotDestination);
5175 p.fillRect(image.rect(), Qt::black);
5176 }
5177
5178 uint pixel = image.pixel(1, 1);
5179 QCOMPARE(pixel, 0xff00ffff);
5180 }
5181
5182 void tst_QPainter::drawTextNoHinting()
5183 {
5184 {
5185 QImage image(250, 250, QImage::Format_RGB32);
5186 QPainter p(&image);
5187 QFont font("Arial", 8);
5188 font.setHintingPreference(QFont::PreferNoHinting);
5189 font.setStyleStrategy(QFont::PreferAntialias);
5190 p.setFont(font);
5191 p.drawText(image.rect(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz");
5192 }
5193 // Testing for a crash when DirectWrite is used on Windows
5194 QVERIFY(true);
5195 }
5196
5197 void tst_QPainter::drawPolyline_data()
5198 {
5199 QTest::addColumn< QVector<QPointF> >("points");
5200
5201 QTest::newRow("basic") << (QVector<QPointF>() << QPointF(10, 10) << QPointF(20, 10) << QPointF(20, 20));
5202 QTest::newRow("clipped") << (QVector<QPointF>() << QPoint(-10, 100) << QPoint(-1, 100) << QPoint(-1, -2) << QPoint(100, -2) << QPoint(100, 40)); // QTBUG-31579
5203 QTest::newRow("shortsegment") << (QVector<QPointF>() << QPoint(20, 100) << QPoint(20, 99) << QPoint(21, 99) << QPoint(21, 104)); // QTBUG-42398
5204 QTest::newRow("edge") << (QVector<QPointF>() << QPointF(4.5, 121.6) << QPointF(9.4, 150.9) << QPointF(14.2, 184.8) << QPointF(19.1, 130.4));
5205 }
5206
5207 void tst_QPainter::drawPolyline()
5208 {
5209 QFETCH(QVector<QPointF>, points);
5210 QImage images[2];
5211
5212 for (int r = 0; r < 2; r++) {
5213 images[r] = QImage(150, 150, QImage::Format_ARGB32);
5214 images[r].fill(Qt::white);
5215 QPainter p(images + r);
5216 QPen pen(Qt::red, 0, Qt::SolidLine, Qt::FlatCap);
5217 p.setPen(pen);
5218 QVERIFY(p.pen().isCosmetic());
5219 if (r) {
5220 for (int i = 0; i < points.count()-1; i++) {
5221 p.drawLine(points.at(i), points.at(i+1));
5222 }
5223 } else {
5224 p.drawPolyline(points);
5225 }
5226 }
5227
5228 QCOMPARE(images[0], images[1]);
5229 }
5230
5231 void tst_QPainter::QTBUG50153_drawImage_assert()
5232 {
5233 QImage::Format formats[] = {
5234 QImage::Format_RGB32, // fetchTransformedBilinearARGB32PM
5235 QImage::Format_ARGB32 // fetchTransformedBilinear
5236 };
5237
5238 for (unsigned i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) {
5239 QImage image(3027, 2999, formats[i]);
5240
5241 QImage backingStore(image.size(), QImage::Format_ARGB32);
5242 QPainter backingStorePainter(&backingStore);
5243
5244 QTransform transform;
5245 transform.scale( 0.999987, 0.999987 );
5246
5247 backingStorePainter.setTransform(transform);
5248 backingStorePainter.setRenderHint(QPainter::SmoothPixmapTransform, true);
5249 backingStorePainter.drawImage(0, 0, image);
5250
5251 // No crash, all fine
5252 }
5253 }
5254
5255 void tst_QPainter::rotateImage_data()
5256 {
5257 QTest::addColumn<QImage>("image");
5258 QTest::addColumn<bool>("smooth");
5259
5260 QImage image(128, 128, QImage::Format_RGB32);
5261 for (int y = 0; y < 128; ++y) {
5262 for (int x = 0; x < 128; ++x) {
5263 image.setPixel(x, y, qRgb(x + y, x + y, x + y));
5264 }
5265 }
5266
5267 QTest::newRow("fast") << image << false;
5268 QTest::newRow("smooth") << image << true;
5269 }
5270
5271 void tst_QPainter::rotateImage()
5272 {
5273 QFETCH(QImage, image);
5274 QFETCH(bool, smooth);
5275
5276 QImage dest(184, 184, QImage::Format_ARGB32_Premultiplied);
5277 dest.fill(Qt::transparent);
5278
5279 QPainter painter(&dest);
5280 QTransform transform;
5281 transform.translate(92, 0);
5282 transform.rotate(45);
5283 painter.setTransform(transform);
5284 painter.setRenderHint(QPainter::SmoothPixmapTransform, smooth);
5285 painter.drawImage(0, 0, image);
5286 painter.end();
5287
5288 QRgb lastRow = qRgba(0, 0, 0, 0);
5289 for (int y = 0; y < 184; ++y) {
5290 QRgb row = qRgba(0, 0, 0, 0);
5291 for (int x = 0; x < 184; ++x) {
5292 QRgb pixel = dest.pixel(x, y);
5293 if (qAlpha(pixel) < 255)
5294 continue;
5295 if (qAlpha(row) == 0) {
5296 row = pixel;
5297 } else {
5298 QCOMPARE(qRed(pixel), qGreen(pixel));
5299 QCOMPARE(qGreen(pixel), qBlue(pixel));
5300 QVERIFY(qAbs(qRed(row) - qRed(pixel)) <= 2);
5301 QVERIFY(qAbs(qGreen(row) - qGreen(pixel)) <= 2);
5302 QVERIFY(qAbs(qBlue(row) - qBlue(pixel)) <= 2);
5303 }
5304
5305 }
5306 if (qAlpha(row) && qAlpha(lastRow))
5307 QVERIFY(qGray(lastRow) <= qGray(row));
5308 lastRow = row;
5309 }
5310
5311 }
5312
5313 void tst_QPainter::QTBUG56252()
5314 {
5315 QImage sourceImage(1770, 1477, QImage::Format_RGB32);
5316 QImage rotatedImage(1478, 1771, QImage::Format_RGB32);
5317 QTransform transformCenter;
5318 transformCenter.translate(739.0, 885.5);
5319 transformCenter.rotate(270.0);
5320 transformCenter.translate(-885.0, -738.5);
5321 QPainter painter;
5322 painter.begin(&rotatedImage);
5323 painter.setTransform(transformCenter);
5324 painter.drawImage(QPoint(0, 0),sourceImage);
5325 painter.end();
5326
5327 // If no crash or illegal memory read, all is fine
5328 }
5329
5330 void tst_QPainter::blendNullRGB32()
5331 {
5332 quint32 data[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
5333
5334 QImage nullImage((const uchar*)data, 16, 1, QImage::Format_RGB32);
5335 QImage image(16, 1, QImage::Format_RGB32);
5336 image.fill(Qt::white);
5337
5338 QPainter paint(&image);
5339 paint.setCompositionMode(QPainter::CompositionMode_Source);
5340 paint.setOpacity(0.5);
5341 paint.drawImage(0, 0, nullImage);
5342 paint.end();
5343
5344 for (int i=0; i < image.width(); ++i)
5345 QVERIFY(image.pixel(i,0) != 0xffffffff);
5346 }
5347
5348 void tst_QPainter::toRGB64()
5349 {
5350 QImage dst(10, 1, QImage::Format_BGR30);
5351 QImage src(10, 1, QImage::Format_RGB16);
5352 src.fill(Qt::white);
5353
5354 QPainter paint(&dst);
5355 paint.drawImage(0, 0, src);
5356 paint.end();
5357
5358 for (int i=0; i < dst.width(); ++i) {
5359 QVERIFY(dst.pixelColor(i,0) == QColor(Qt::white));
5360 }
5361 }
5362
5363 void tst_QPainter::fillPolygon()
5364 {
5365 QImage image(50, 50, QImage::Format_RGB32);
5366 image.fill(Qt::white);
5367
5368 QPainter painter(&image);
5369 QBrush brush(Qt::black, Qt::SolidPattern);
5370 painter.setBrush(brush);
5371
5372 QPen pen(Qt::red, 0, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
5373 painter.setPen(pen);
5374
5375 const QPoint diamondpoints[5] = {
5376 QPoint(-15, 0),
5377 QPoint(0, -15),
5378 QPoint(15, 0),
5379 QPoint(0, 15),
5380 QPoint(-15, 0)
5381 };
5382 enum { Outside1, Border1, Inside, Border2, Outside2 } state;
5383
5384 for (int i = 0; i < 16 ; i++)
5385 {
5386 for (int j = 0; j < 16 ; j++)
5387 {
5388 image.fill(Qt::white);
5389 painter.resetTransform();
5390 painter.translate(25 + i/16., 25 + j/16.);
5391 painter.drawPolygon(diamondpoints, 5);
5392
5393 for (int x = 0; x < 50; x++) {
5394 state = Outside1;
5395 for (int y = 0; y < 50; y++) {
5396 QRgb c = image.pixel(x, y);
5397 switch (state) {
5398 case Outside1:
5399 if (c == QColor(Qt::red).rgb())
5400 state = Border1;
5401 else
5402 QCOMPARE(c, QColor(Qt::white).rgb());
5403 break;
5404 case Border1:
5405 if (c == QColor(Qt::black).rgb())
5406 state = Inside;
5407 else if (c == QColor(Qt::white).rgb())
5408 state = Outside2;
5409 else
5410 QCOMPARE(c, QColor(Qt::red).rgb());
5411 break;
5412 case Inside:
5413 if (c == QColor(Qt::red).rgb())
5414 state = Border2;
5415 else
5416 QCOMPARE(c, QColor(Qt::black).rgb());
5417 break;
5418 case Border2:
5419 if (c == QColor(Qt::white).rgb())
5420 state = Outside2;
5421 else
5422 QCOMPARE(c, QColor(Qt::red).rgb());
5423 break;
5424 case Outside2:
5425 QCOMPARE(c, QColor(Qt::white).rgb());
5426 }
5427 }
5428 }
5429 for (int y = 0; y < 50; y++) {
5430 state = Outside1;
5431 for (int x = 0; x < 50; x++) {
5432 QRgb c = image.pixel(x, y);
5433 switch (state) {
5434 case Outside1:
5435 if (c == QColor(Qt::red).rgb())
5436 state = Border1;
5437 else
5438 QCOMPARE(c, QColor(Qt::white).rgb());
5439 break;
5440 case Border1:
5441 if (c == QColor(Qt::black).rgb())
5442 state = Inside;
5443 else if (c == QColor(Qt::white).rgb())
5444 state = Outside2;
5445 else
5446 QCOMPARE(c, QColor(Qt::red).rgb());
5447 break;
5448 case Inside:
5449 if (c == QColor(Qt::red).rgb())
5450 state = Border2;
5451 else
5452 QCOMPARE(c, QColor(Qt::black).rgb());
5453 break;
5454 case Border2:
5455 if (c == QColor(Qt::white).rgb())
5456 state = Outside2;
5457 else
5458 QCOMPARE(c, QColor(Qt::red).rgb());
5459 break;
5460 case Outside2:
5461 QCOMPARE(c, QColor(Qt::white).rgb());
5462 }
5463 }
5464 }
5465 }
5466 }
5467 }
5468
5469 void tst_QPainter::drawImageAtPointF()
5470 {
5471 // Just test we do not crash
5472 QImage image1(10, 10, QImage::Format_RGB32);
5473 QImage image2(200, 200, QImage::Format_RGB32);
5474
5475 QPainter paint(&image2);
5476 paint.setClipRect(97, 46, 14, 14);
5477 paint.setCompositionMode(QPainter::CompositionMode_Source);
5478 paint.drawImage(QPointF(96, std::numeric_limits<int>::max()), image1);
5479 paint.drawImage(QPointF(std::numeric_limits<int>::min(), 48), image1);
5480 paint.end();
5481 }
5482
5483 void tst_QPainter::scaledDashes()
5484 {
5485 // Test that we do not hit the limit-huge-number-of-dashes path
5486 QRgb fore = qRgb(0, 0, 0xff);
5487 QRgb back = qRgb(0xff, 0xff, 0);
5488 QImage image(5, 32, QImage::Format_RGB32);
5489 image.fill(back);
5490 QPainter p(&image);
5491 QPen pen(QColor(fore), 3, Qt::DotLine);
5492 p.setPen(pen);
5493 p.scale(1, 2);
5494 p.drawLine(2, 0, 2, 16);
5495 p.end();
5496
5497 bool foreFound = false;
5498 bool backFound = false;
5499 int i = 0;
5500 while (i < 32 && (!foreFound || !backFound)) {
5501 QRgb pix = image.pixel(3, i);
5502 if (pix == fore)
5503 foreFound = true;
5504 else if (pix == back)
5505 backFound = true;
5506 i++;
5507 }
5508
5509 QVERIFY(foreFound);
5510 QVERIFY(backFound);
5511 }
5512
5513 QTEST_MAIN(tst_QPainter)
5514
5515 #include "tst_qpainter.moc"