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