Debian Codegraph @

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

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