| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #include <qapplication.h> |
| 31 | #include <qpixmap.h> |
| 32 | #include <qpainter.h> |
| 33 | #include <qbitmap.h> |
| 34 | |
| 35 | static QColor baseColor( int k, int intensity ) |
| 36 | { |
| 37 | int r = ( k & 1 ) * intensity; |
| 38 | int g = ( (k>>1) & 1 ) * intensity; |
| 39 | int b = ( (k>>2) & 1 ) * intensity; |
| 40 | return QColor( r, g, b ); |
| 41 | } |
| 42 | |
| 43 | static QPixmap createDestPixmap() |
| 44 | { |
| 45 | const int colorbands = 3; |
| 46 | const int intensities = 4; |
| 47 | QPixmap pm( 32, colorbands*intensities*4 ); |
| 48 | QPainter painter; |
| 49 | painter.begin( &pm ); |
| 50 | for ( int i=0; i<colorbands; i++ ) { |
| 51 | for (int j = 0; j < intensities; j++) { |
| 52 | int intensity = 255 * (j+1) / intensities; // 25%, 50%, 75% and 100% |
| 53 | for (int k = 0; k < 8; k++) { |
| 54 | QColor col = baseColor(k, intensity); |
| 55 | painter.setPen(QPen(col, 1)); |
| 56 | painter.setBrush(col); |
| 57 | painter.drawRect(k*4, j*4 + i*intensities*4, 4, 4); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | painter.end(); |
| 62 | return pm; |
| 63 | } |
| 64 | |
| 65 | static QBitmap createDestBitmap() |
| 66 | { |
| 67 | // create a bitmap that looks like: |
| 68 | // (0 is color0 and 1 is color1) |
| 69 | // 00001111 |
| 70 | // 00001111 |
| 71 | // 00001111 |
| 72 | // 00001111 |
| 73 | // 00001111 |
| 74 | // 00001111 |
| 75 | // 00001111 |
| 76 | // 00001111 |
| 77 | QBitmap bm( 8, 8 ); |
| 78 | QPainter painter; |
| 79 | painter.begin( &bm ); |
| 80 | painter.setPen( QPen( Qt::color0, 4 ) ); |
| 81 | painter.drawLine( 2, 0, 2, 8 ); |
| 82 | painter.setPen( QPen( Qt::color1, 4 ) ); |
| 83 | painter.drawLine( 6, 0, 6, 8 ); |
| 84 | painter.end(); |
| 85 | return bm; |
| 86 | } |
| 87 | |
| 88 | static QBitmap createSrcBitmap( int size, int border ) |
| 89 | { |
| 90 | // create the source bitmap that looks like |
| 91 | // (for size=4 and border=2): |
| 92 | // |
| 93 | // |
| 94 | // 1111 |
| 95 | // 1111 |
| 96 | // 0000 |
| 97 | // 0000 |
| 98 | // |
| 99 | // |
| 100 | // If \a border is 0, the bitmap does not have a mask, otherwise the inner |
| 101 | // part is masked. |
| 102 | // \a size specifies the size of the inner (i.e. masked) part. It should be |
| 103 | // a multiple of 2. |
| 104 | int size2 = size/2; |
| 105 | int totalSize = 2 * ( size2 + border ); |
| 106 | QBitmap bm( totalSize, totalSize ); |
| 107 | QPainter painter; |
| 108 | painter.begin( &bm ); |
| 109 | painter.setPen( QPen( Qt::color0, 1 ) ); |
| 110 | painter.setBrush( Qt::color0 ); |
| 111 | painter.drawRect( border, size2+border, size, size2 ); |
| 112 | painter.setPen( QPen( Qt::color1, 1 ) ); |
| 113 | painter.setBrush( Qt::color1 ); |
| 114 | painter.drawRect( border, border, size, size2 ); |
| 115 | painter.end(); |
| 116 | if ( border > 0 ) { |
| 117 | QBitmap mask(totalSize, totalSize, true); |
| 118 | QPainter painter; |
| 119 | painter.begin(&mask); |
| 120 | painter.setPen(QPen(Qt::color1, 1)); |
| 121 | painter.setBrush(Qt::color1); |
| 122 | painter.drawRect(border, border, size, size); |
| 123 | painter.end(); |
| 124 | bm.setMask(mask); |
| 125 | } |
| 126 | return bm; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | int main( int argc, char **argv ) |
| 131 | { |
| 132 | QApplication a( argc, argv ); |
| 133 | |
| 134 | // input for tst_QPainter::drawLine_rop_bitmap() |
| 135 | { |
| 136 | QBitmap dst = createDestBitmap(); |
| 137 | dst.save("../../drawLine_rop_bitmap/dst.xbm", "XBM"); |
| 138 | } |
| 139 | |
| 140 | // input for tst_QPainter::drawPixmap_rop_bitmap() |
| 141 | { |
| 142 | QBitmap dst = createDestBitmap(); |
| 143 | QBitmap src1 = createSrcBitmap(4, 2); |
| 144 | QBitmap src2 = createSrcBitmap(4, 0); |
| 145 | dst.save("../../drawPixmap_rop_bitmap/dst.xbm", "XBM"); |
| 146 | src1.save("../../drawPixmap_rop_bitmap/src1.xbm", "XBM"); |
| 147 | src1.mask()->save("../../drawPixmap_rop_bitmap/src1-mask.xbm", "XBM"); |
| 148 | src2.save("../../drawPixmap_rop_bitmap/src2.xbm", "XBM"); |
| 149 | } |
| 150 | |
| 151 | // input for tst_QPainter::drawPixmap_rop() |
| 152 | { |
| 153 | QPixmap dst1 = createDestPixmap(); |
| 154 | QPixmap dst2 = createDestPixmap(); |
| 155 | dst2.resize(32, 32); |
| 156 | QBitmap src1 = createSrcBitmap(32, 0); |
| 157 | |
| 158 | QBitmap src_tmp = createSrcBitmap(32, 0).xForm(QWMatrix(1, 0, 0, -1, 0, 0)); |
| 159 | src_tmp.resize(32, 48); |
| 160 | QBitmap src2 = src_tmp.xForm(QWMatrix(1, 0, 0, -1, 0, 0)); |
| 161 | QBitmap mask(32, 48, true); |
| 162 | { |
| 163 | QPainter painter; |
| 164 | painter.begin(&mask); |
| 165 | painter.setPen(QPen(Qt::color1, 1)); |
| 166 | painter.setBrush(Qt::color1); |
| 167 | painter.drawRect(0, 16, 32, 32); |
| 168 | painter.end(); |
| 169 | } |
| 170 | src2.setMask(mask); |
| 171 | |
| 172 | QBitmap src3 = createSrcBitmap(32, 0).xForm(QWMatrix(1, 0, 0, -1, 0, 0)); |
| 173 | |
| 174 | dst1.save("../../drawPixmap_rop/dst1.png", "PNG"); |
| 175 | dst2.save("../../drawPixmap_rop/dst2.png", "PNG"); |
| 176 | src1.save("../../drawPixmap_rop/src1.xbm", "XBM"); |
| 177 | src2.save("../../drawPixmap_rop/src2.xbm", "XBM"); |
| 178 | src2.mask()->save("../../drawPixmap_rop/src2-mask.xbm", "XBM"); |
| 179 | src3.save("../../drawPixmap_rop/src3.xbm", "XBM"); |
| 180 | } |
| 181 | } |