Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
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 Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qbitmap.h" | - |
43 | #include <qpa/qplatformpixmap.h> | - |
44 | #include <qpa/qplatformintegration.h> | - |
45 | #include "qimage.h" | - |
46 | #include "qscreen.h" | - |
47 | #include "qvariant.h" | - |
48 | #include <qpainter.h> | - |
49 | #include <private/qguiapplication_p.h> | - |
50 | | - |
51 | QT_BEGIN_NAMESPACE | - |
52 | | - |
53 | /*! | - |
54 | \class QBitmap | - |
55 | \inmodule QtGui | - |
56 | \brief The QBitmap class provides monochrome (1-bit depth) pixmaps. | - |
57 | | - |
58 | \ingroup painting | - |
59 | \ingroup shared | - |
60 | | - |
61 | The QBitmap class is a monochrome off-screen paint device used | - |
62 | mainly for creating custom QCursor and QBrush objects, | - |
63 | constructing QRegion objects, and for setting masks for pixmaps | - |
64 | and widgets. | - |
65 | | - |
66 | QBitmap is a QPixmap subclass ensuring a depth of 1, except for | - |
67 | null objects which have a depth of 0. If a pixmap with a depth | - |
68 | greater than 1 is assigned to a bitmap, the bitmap will be | - |
69 | dithered automatically. | - |
70 | | - |
71 | Use the QColor objects Qt::color0 and Qt::color1 when drawing on a | - |
72 | QBitmap object (or a QPixmap object with depth 1). | - |
73 | | - |
74 | Painting with Qt::color0 sets the bitmap bits to 0, and painting | - |
75 | with Qt::color1 sets the bits to 1. For a bitmap, 0-bits indicate | - |
76 | background (or transparent pixels) and 1-bits indicate foreground | - |
77 | (or opaque pixels). Use the clear() function to set all the bits | - |
78 | to Qt::color0. Note that using the Qt::black and Qt::white colors | - |
79 | make no sense because the QColor::pixel() value is not necessarily | - |
80 | 0 for black and 1 for white. | - |
81 | | - |
82 | The QBitmap class provides the transformed() function returning a | - |
83 | transformed copy of the bitmap; use the QTransform argument to | - |
84 | translate, scale, shear, and rotate the bitmap. In addition, | - |
85 | QBitmap provides the static fromData() function which returns a | - |
86 | bitmap constructed from the given \c uchar data, and the static | - |
87 | fromImage() function returning a converted copy of a QImage | - |
88 | object. | - |
89 | | - |
90 | Just like the QPixmap class, QBitmap is optimized by the use of | - |
91 | implicit data sharing. For more information, see the \l {Implicit | - |
92 | Data Sharing} documentation. | - |
93 | | - |
94 | \sa QPixmap, QImage, QImageReader, QImageWriter | - |
95 | */ | - |
96 | | - |
97 | /*! \typedef QBitmap::DataPtr | - |
98 | \internal | - |
99 | */ | - |
100 | | - |
101 | /*! | - |
102 | Constructs a null bitmap. | - |
103 | | - |
104 | \sa QPixmap::isNull() | - |
105 | */ | - |
106 | QBitmap::QBitmap() | - |
107 | : QPixmap(QSize(0, 0), QPlatformPixmap::BitmapType) | - |
108 | { | - |
109 | } executed: } Execution Count:112 | 112 |
110 | | - |
111 | /*! | - |
112 | \fn QBitmap::QBitmap(int width, int height) | - |
113 | | - |
114 | Constructs a bitmap with the given \a width and \a height. The pixels | - |
115 | inside are uninitialized. | - |
116 | | - |
117 | \sa clear() | - |
118 | */ | - |
119 | | - |
120 | QBitmap::QBitmap(int w, int h) | - |
121 | : QPixmap(QSize(w, h), QPlatformPixmap::BitmapType) | - |
122 | { | - |
123 | } executed: } Execution Count:13 | 13 |
124 | | - |
125 | /*! | - |
126 | Constructs a bitmap with the given \a size. The pixels in the | - |
127 | bitmap are uninitialized. | - |
128 | | - |
129 | \sa clear() | - |
130 | */ | - |
131 | | - |
132 | QBitmap::QBitmap(const QSize &size) | - |
133 | : QPixmap(size, QPlatformPixmap::BitmapType) | - |
134 | { | - |
135 | } executed: } Execution Count:4 | 4 |
136 | | - |
137 | /*! | - |
138 | \fn QBitmap::clear() | - |
139 | | - |
140 | Clears the bitmap, setting all its bits to Qt::color0. | - |
141 | */ | - |
142 | | - |
143 | /*! | - |
144 | Constructs a bitmap that is a copy of the given \a pixmap. | - |
145 | | - |
146 | If the pixmap has a depth greater than 1, the resulting bitmap | - |
147 | will be dithered automatically. | - |
148 | | - |
149 | \sa QPixmap::depth(), fromImage(), fromData() | - |
150 | */ | - |
151 | | - |
152 | QBitmap::QBitmap(const QPixmap &pixmap) | - |
153 | { | - |
154 | QBitmap::operator=(pixmap); executed (the execution status of this line is deduced): QBitmap::operator=(pixmap); | - |
155 | } executed: } Execution Count:167 | 167 |
156 | | - |
157 | /*! | - |
158 | Constructs a bitmap from the file specified by the given \a | - |
159 | fileName. If the file does not exist, or has an unknown format, | - |
160 | the bitmap becomes a null bitmap. | - |
161 | | - |
162 | The \a fileName and \a format parameters are passed on to the | - |
163 | QPixmap::load() function. If the file format uses more than 1 bit | - |
164 | per pixel, the resulting bitmap will be dithered automatically. | - |
165 | | - |
166 | \sa QPixmap::isNull(), QImageReader::imageFormat() | - |
167 | */ | - |
168 | | - |
169 | QBitmap::QBitmap(const QString& fileName, const char *format) | - |
170 | : QPixmap(QSize(0, 0), QPlatformPixmap::BitmapType) | - |
171 | { | - |
172 | load(fileName, format, Qt::MonoOnly); executed (the execution status of this line is deduced): load(fileName, format, Qt::MonoOnly); | - |
173 | } executed: } Execution Count:1 | 1 |
174 | | - |
175 | /*! | - |
176 | \overload | - |
177 | | - |
178 | Assigns the given \a pixmap to this bitmap and returns a reference | - |
179 | to this bitmap. | - |
180 | | - |
181 | If the pixmap has a depth greater than 1, the resulting bitmap | - |
182 | will be dithered automatically. | - |
183 | | - |
184 | \sa QPixmap::depth() | - |
185 | */ | - |
186 | | - |
187 | QBitmap &QBitmap::operator=(const QPixmap &pixmap) | - |
188 | { | - |
189 | if (pixmap.isNull()) { // a null pixmap partially evaluated: pixmap.isNull() no Evaluation Count:0 | yes Evaluation Count:168 |
| 0-168 |
190 | QBitmap(0, 0).swap(*this); never executed (the execution status of this line is deduced): QBitmap(0, 0).swap(*this); | - |
191 | } else if (pixmap.depth() == 1) { // 1-bit pixmap never executed: } evaluated: pixmap.depth() == 1 yes Evaluation Count:155 | yes Evaluation Count:13 |
| 0-155 |
192 | QPixmap::operator=(pixmap); // shallow assignment executed (the execution status of this line is deduced): QPixmap::operator=(pixmap); | - |
193 | } else { // n-bit depth pixmap executed: } Execution Count:155 | 155 |
194 | QImage image; executed (the execution status of this line is deduced): QImage image; | - |
195 | image = pixmap.toImage(); // convert pixmap to image executed (the execution status of this line is deduced): image = pixmap.toImage(); | - |
196 | *this = fromImage(image); // will dither image executed (the execution status of this line is deduced): *this = fromImage(image); | - |
197 | } executed: } Execution Count:13 | 13 |
198 | return *this; executed: return *this; Execution Count:168 | 168 |
199 | } | - |
200 | | - |
201 | /*! | - |
202 | Destroys the bitmap. | - |
203 | */ | - |
204 | QBitmap::~QBitmap() | - |
205 | { | - |
206 | } | - |
207 | | - |
208 | /*! | - |
209 | \fn void QBitmap::swap(QBitmap &other) | - |
210 | \since 4.8 | - |
211 | | - |
212 | Swaps bitmap \a other with this bitmap. This operation is very | - |
213 | fast and never fails. | - |
214 | */ | - |
215 | | - |
216 | /*! | - |
217 | Returns the bitmap as a QVariant. | - |
218 | */ | - |
219 | QBitmap::operator QVariant() const | - |
220 | { | - |
221 | return QVariant(QVariant::Bitmap, this); executed: return QVariant(QVariant::Bitmap, this); Execution Count:2 | 2 |
222 | } | - |
223 | | - |
224 | /*! | - |
225 | Returns a copy of the given \a image converted to a bitmap using | - |
226 | the specified image conversion \a flags. | - |
227 | | - |
228 | \sa fromData() | - |
229 | */ | - |
230 | QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) | - |
231 | { | - |
232 | if (image.isNull()) partially evaluated: image.isNull() no Evaluation Count:0 | yes Evaluation Count:155 |
| 0-155 |
233 | return QBitmap(); never executed: return QBitmap(); | 0 |
234 | | - |
235 | QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); executed (the execution status of this line is deduced): QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); | - |
236 | | - |
237 | // make sure image.color(0) == Qt::color0 (white) | - |
238 | // and image.color(1) == Qt::color1 (black) | - |
239 | const QRgb c0 = QColor(Qt::black).rgb(); executed (the execution status of this line is deduced): const QRgb c0 = QColor(Qt::black).rgb(); | - |
240 | const QRgb c1 = QColor(Qt::white).rgb(); executed (the execution status of this line is deduced): const QRgb c1 = QColor(Qt::white).rgb(); | - |
241 | if (img.color(0) == c0 && img.color(1) == c1) { evaluated: img.color(0) == c0 yes Evaluation Count:2 | yes Evaluation Count:153 |
partially evaluated: img.color(1) == c1 yes Evaluation Count:2 | no Evaluation Count:0 |
| 0-153 |
242 | img.invertPixels(); executed (the execution status of this line is deduced): img.invertPixels(); | - |
243 | img.setColor(0, c1); executed (the execution status of this line is deduced): img.setColor(0, c1); | - |
244 | img.setColor(1, c0); executed (the execution status of this line is deduced): img.setColor(1, c0); | - |
245 | } executed: } Execution Count:2 | 2 |
246 | | - |
247 | QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType)); executed (the execution status of this line is deduced): QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType)); | - |
248 | | - |
249 | data->fromImage(img, flags | Qt::MonoOnly); executed (the execution status of this line is deduced): data->fromImage(img, flags | Qt::MonoOnly); | - |
250 | return QPixmap(data.take()); executed: return QPixmap(data.take()); Execution Count:155 | 155 |
251 | } | - |
252 | | - |
253 | /*! | - |
254 | Constructs a bitmap with the given \a size, and sets the contents to | - |
255 | the \a bits supplied. | - |
256 | | - |
257 | The bitmap data has to be byte aligned and provided in in the bit | - |
258 | order specified by \a monoFormat. The mono format must be either | - |
259 | QImage::Format_Mono or QImage::Format_MonoLSB. Use | - |
260 | QImage::Format_Mono to specify data on the XBM format. | - |
261 | | - |
262 | \sa fromImage() | - |
263 | | - |
264 | */ | - |
265 | QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat) | - |
266 | { | - |
267 | Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB); executed (the execution status of this line is deduced): qt_noop(); | - |
268 | | - |
269 | QImage image(size, monoFormat); executed (the execution status of this line is deduced): QImage image(size, monoFormat); | - |
270 | image.setColor(0, QColor(Qt::color0).rgb()); executed (the execution status of this line is deduced): image.setColor(0, QColor(Qt::color0).rgb()); | - |
271 | image.setColor(1, QColor(Qt::color1).rgb()); executed (the execution status of this line is deduced): image.setColor(1, QColor(Qt::color1).rgb()); | - |
272 | | - |
273 | // Need to memcpy each line separatly since QImage is 32bit aligned and | - |
274 | // this data is only byte aligned... | - |
275 | int bytesPerLine = (size.width() + 7) / 8; executed (the execution status of this line is deduced): int bytesPerLine = (size.width() + 7) / 8; | - |
276 | for (int y = 0; y < size.height(); ++y) evaluated: y < size.height() yes Evaluation Count:2 | yes Evaluation Count:1 |
| 1-2 |
277 | memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine); executed: memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine); Execution Count:2 | 2 |
278 | return QBitmap::fromImage(image); executed: return QBitmap::fromImage(image); Execution Count:1 | 1 |
279 | } | - |
280 | | - |
281 | /*! | - |
282 | Returns a copy of this bitmap, transformed according to the given | - |
283 | \a matrix. | - |
284 | | - |
285 | \sa QPixmap::transformed() | - |
286 | */ | - |
287 | QBitmap QBitmap::transformed(const QTransform &matrix) const | - |
288 | { | - |
289 | QBitmap bm = QPixmap::transformed(matrix); never executed (the execution status of this line is deduced): QBitmap bm = QPixmap::transformed(matrix); | - |
290 | return bm; never executed: return bm; | 0 |
291 | } | - |
292 | | - |
293 | /*! | - |
294 | \overload | - |
295 | \obsolete | - |
296 | | - |
297 | This convenience function converts the \a matrix to a QTransform | - |
298 | and calls the overloaded function. | - |
299 | */ | - |
300 | QBitmap QBitmap::transformed(const QMatrix &matrix) const | - |
301 | { | - |
302 | return transformed(QTransform(matrix)); never executed: return transformed(QTransform(matrix)); | 0 |
303 | } | - |
304 | | - |
305 | QT_END_NAMESPACE | - |
306 | | - |
| | |