1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
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 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 Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qimage.h" |
41 | |
42 | #include "qbuffer.h" |
43 | #include "qdatastream.h" |
44 | #include "qcolortransform.h" |
45 | #include "qmap.h" |
46 | #include "qmatrix.h" |
47 | #include "qtransform.h" |
48 | #include "qimagereader.h" |
49 | #include "qimagewriter.h" |
50 | #include "qstringlist.h" |
51 | #include "qvariant.h" |
52 | #include "qimagepixmapcleanuphooks_p.h" |
53 | #include <qpa/qplatformintegration.h> |
54 | #include <private/qguiapplication_p.h> |
55 | #include <ctype.h> |
56 | #include <stdlib.h> |
57 | #include <limits.h> |
58 | #include <qpa/qplatformpixmap.h> |
59 | #include <private/qcolortransform_p.h> |
60 | #include <private/qdrawhelper_p.h> |
61 | #include <private/qmemrotate_p.h> |
62 | #include <private/qimagescale_p.h> |
63 | #include <private/qsimd_p.h> |
64 | |
65 | #include <qhash.h> |
66 | |
67 | #include <private/qpaintengine_raster_p.h> |
68 | |
69 | #include <private/qimage_p.h> |
70 | #include <private/qfont_p.h> |
71 | |
72 | QT_BEGIN_NAMESPACE |
73 | |
74 | static inline bool isLocked(QImageData *data) |
75 | { |
76 | return data != 0 && data->is_locked; |
77 | } |
78 | |
79 | #if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001) |
80 | #pragma message disable narrowptr |
81 | #endif |
82 | |
83 | |
84 | #define QIMAGE_SANITYCHECK_MEMORY(image) \ |
85 | if ((image).isNull()) { \ |
86 | qWarning("QImage: out of memory, returning null image"); \ |
87 | return QImage(); \ |
88 | } |
89 | |
90 | |
91 | static QImage rotated90(const QImage &src); |
92 | static QImage rotated180(const QImage &src); |
93 | static QImage rotated270(const QImage &src); |
94 | |
95 | static int next_qimage_serial_number() |
96 | { |
97 | static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0); |
98 | return 1 + serial.fetchAndAddRelaxed(1); |
99 | } |
100 | |
101 | QImageData::QImageData() |
102 | : ref(0), width(0), height(0), depth(0), nbytes(0), devicePixelRatio(1.0), data(0), |
103 | format(QImage::Format_ARGB32), bytes_per_line(0), |
104 | ser_no(next_qimage_serial_number()), |
105 | detach_no(0), |
106 | dpmx(qt_defaultDpiX() * 100 / qreal(2.54)), |
107 | dpmy(qt_defaultDpiY() * 100 / qreal(2.54)), |
108 | offset(0, 0), own_data(true), ro_data(false), has_alpha_clut(false), |
109 | is_cached(false), is_locked(false), cleanupFunction(0), cleanupInfo(0), |
110 | paintEngine(0) |
111 | { |
112 | } |
113 | |
114 | /*! \fn QImageData * QImageData::create(const QSize &size, QImage::Format format) |
115 | |
116 | \internal |
117 | |
118 | Creates a new image data. |
119 | Returns \nullptr if invalid parameters are give or anything else failed. |
120 | */ |
121 | QImageData * QImageData::create(const QSize &size, QImage::Format format) |
122 | { |
123 | if (size.isEmpty() || format == QImage::Format_Invalid) |
124 | return nullptr; // invalid parameter(s) |
125 | |
126 | int width = size.width(); |
127 | int height = size.height(); |
128 | int depth = qt_depthForFormat(format); |
129 | auto params = calculateImageParameters(width, height, depth); |
130 | if (!params.isValid()) |
131 | return nullptr; |
132 | |
133 | QScopedPointer<QImageData> d(new QImageData); |
134 | |
135 | switch (format) { |
136 | case QImage::Format_Mono: |
137 | case QImage::Format_MonoLSB: |
138 | d->colortable.resize(2); |
139 | d->colortable[0] = QColor(Qt::black).rgba(); |
140 | d->colortable[1] = QColor(Qt::white).rgba(); |
141 | break; |
142 | default: |
143 | break; |
144 | } |
145 | |
146 | d->width = width; |
147 | d->height = height; |
148 | d->depth = depth; |
149 | d->format = format; |
150 | d->has_alpha_clut = false; |
151 | d->is_cached = false; |
152 | |
153 | d->bytes_per_line = params.bytesPerLine; |
154 | d->nbytes = params.totalSize; |
155 | d->data = (uchar *)malloc(d->nbytes); |
156 | |
157 | if (!d->data) |
158 | return nullptr; |
159 | |
160 | d->ref.ref(); |
161 | return d.take(); |
162 | } |
163 | |
164 | QImageData::~QImageData() |
165 | { |
166 | if (cleanupFunction) |
167 | cleanupFunction(cleanupInfo); |
168 | if (is_cached) |
169 | QImagePixmapCleanupHooks::executeImageHooks((((qint64) ser_no) << 32) | ((qint64) detach_no)); |
170 | delete paintEngine; |
171 | if (data && own_data) |
172 | free(data); |
173 | data = 0; |
174 | } |
175 | |
176 | #if defined(_M_ARM) |
177 | #pragma optimize("", off) |
178 | #endif |
179 | |
180 | bool QImageData::checkForAlphaPixels() const |
181 | { |
182 | bool has_alpha_pixels = false; |
183 | |
184 | switch (format) { |
185 | |
186 | case QImage::Format_Mono: |
187 | case QImage::Format_MonoLSB: |
188 | case QImage::Format_Indexed8: |
189 | has_alpha_pixels = has_alpha_clut; |
190 | break; |
191 | case QImage::Format_Alpha8: |
192 | has_alpha_pixels = true; |
193 | break; |
194 | case QImage::Format_ARGB32: |
195 | case QImage::Format_ARGB32_Premultiplied: { |
196 | const uchar *bits = data; |
197 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
198 | uint alphaAnd = 0xff000000; |
199 | for (int x=0; x<width; ++x) |
200 | alphaAnd &= reinterpret_cast<const uint*>(bits)[x]; |
201 | has_alpha_pixels = (alphaAnd != 0xff000000); |
202 | bits += bytes_per_line; |
203 | } |
204 | } break; |
205 | |
206 | case QImage::Format_RGBA8888: |
207 | case QImage::Format_RGBA8888_Premultiplied: { |
208 | const uchar *bits = data; |
209 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
210 | uchar alphaAnd = 0xff; |
211 | for (int x=0; x<width; ++x) |
212 | alphaAnd &= bits[x * 4+ 3]; |
213 | has_alpha_pixels = (alphaAnd != 0xff); |
214 | bits += bytes_per_line; |
215 | } |
216 | } break; |
217 | |
218 | case QImage::Format_A2BGR30_Premultiplied: |
219 | case QImage::Format_A2RGB30_Premultiplied: { |
220 | const uchar *bits = data; |
221 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
222 | uint alphaAnd = 0xc0000000; |
223 | for (int x=0; x<width; ++x) |
224 | alphaAnd &= reinterpret_cast<const uint*>(bits)[x]; |
225 | has_alpha_pixels = (alphaAnd != 0xc0000000); |
226 | bits += bytes_per_line; |
227 | } |
228 | } break; |
229 | |
230 | case QImage::Format_ARGB8555_Premultiplied: |
231 | case QImage::Format_ARGB8565_Premultiplied: { |
232 | const uchar *bits = data; |
233 | const uchar *end_bits = data + bytes_per_line; |
234 | |
235 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
236 | uchar alphaAnd = 0xff; |
237 | while (bits < end_bits) { |
238 | alphaAnd &= bits[0]; |
239 | bits += 3; |
240 | } |
241 | has_alpha_pixels = (alphaAnd != 0xff); |
242 | bits = end_bits; |
243 | end_bits += bytes_per_line; |
244 | } |
245 | } break; |
246 | |
247 | case QImage::Format_ARGB6666_Premultiplied: { |
248 | const uchar *bits = data; |
249 | const uchar *end_bits = data + bytes_per_line; |
250 | |
251 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
252 | uchar alphaAnd = 0xfc; |
253 | while (bits < end_bits) { |
254 | alphaAnd &= bits[0]; |
255 | bits += 3; |
256 | } |
257 | has_alpha_pixels = (alphaAnd != 0xfc); |
258 | bits = end_bits; |
259 | end_bits += bytes_per_line; |
260 | } |
261 | } break; |
262 | |
263 | case QImage::Format_ARGB4444_Premultiplied: { |
264 | const uchar *bits = data; |
265 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
266 | ushort alphaAnd = 0xf000; |
267 | for (int x=0; x<width; ++x) |
268 | alphaAnd &= reinterpret_cast<const ushort*>(bits)[x]; |
269 | has_alpha_pixels = (alphaAnd != 0xf000); |
270 | bits += bytes_per_line; |
271 | } |
272 | } break; |
273 | case QImage::Format_RGBA64: |
274 | case QImage::Format_RGBA64_Premultiplied: { |
275 | uchar *bits = data; |
276 | for (int y=0; y<height && !has_alpha_pixels; ++y) { |
277 | for (int x=0; x<width; ++x) { |
278 | has_alpha_pixels |= !(((QRgba64 *)bits)[x].isOpaque()); |
279 | } |
280 | bits += bytes_per_line; |
281 | } |
282 | } break; |
283 | |
284 | case QImage::Format_RGB32: |
285 | case QImage::Format_RGB16: |
286 | case QImage::Format_RGB444: |
287 | case QImage::Format_RGB555: |
288 | case QImage::Format_RGB666: |
289 | case QImage::Format_RGB888: |
290 | case QImage::Format_RGBX8888: |
291 | case QImage::Format_BGR30: |
292 | case QImage::Format_RGB30: |
293 | case QImage::Format_Grayscale8: |
294 | case QImage::Format_Grayscale16: |
295 | case QImage::Format_RGBX64: |
296 | break; |
297 | case QImage::Format_Invalid: |
298 | case QImage::NImageFormats: |
299 | Q_UNREACHABLE(); |
300 | break; |
301 | } |
302 | |
303 | return has_alpha_pixels; |
304 | } |
305 | #if defined(_M_ARM) |
306 | #pragma optimize("", on) |
307 | #endif |
308 | |
309 | /*! |
310 | \class QImage |
311 | |
312 | \inmodule QtGui |
313 | \ingroup painting |
314 | \ingroup shared |
315 | |
316 | \reentrant |
317 | |
318 | \brief The QImage class provides a hardware-independent image |
319 | representation that allows direct access to the pixel data, and |
320 | can be used as a paint device. |
321 | |
322 | Qt provides four classes for handling image data: QImage, QPixmap, |
323 | QBitmap and QPicture. QImage is designed and optimized for I/O, |
324 | and for direct pixel access and manipulation, while QPixmap is |
325 | designed and optimized for showing images on screen. QBitmap is |
326 | only a convenience class that inherits QPixmap, ensuring a |
327 | depth of 1. Finally, the QPicture class is a paint device that |
328 | records and replays QPainter commands. |
329 | |
330 | Because QImage is a QPaintDevice subclass, QPainter can be used to |
331 | draw directly onto images. When using QPainter on a QImage, the |
332 | painting can be performed in another thread than the current GUI |
333 | thread. |
334 | |
335 | The QImage class supports several image formats described by the |
336 | \l Format enum. These include monochrome, 8-bit, 32-bit and |
337 | alpha-blended images which are available in all versions of Qt |
338 | 4.x. |
339 | |
340 | QImage provides a collection of functions that can be used to |
341 | obtain a variety of information about the image. There are also |
342 | several functions that enables transformation of the image. |
343 | |
344 | QImage objects can be passed around by value since the QImage |
345 | class uses \l{Implicit Data Sharing}{implicit data |
346 | sharing}. QImage objects can also be streamed and compared. |
347 | |
348 | \note If you would like to load QImage objects in a static build of Qt, |
349 | refer to the \l{How to Create Qt Plugins}{Plugin HowTo}. |
350 | |
351 | \warning Painting on a QImage with the format |
352 | QImage::Format_Indexed8 is not supported. |
353 | |
354 | \tableofcontents |
355 | |
356 | \section1 Reading and Writing Image Files |
357 | |
358 | QImage provides several ways of loading an image file: The file |
359 | can be loaded when constructing the QImage object, or by using the |
360 | load() or loadFromData() functions later on. QImage also provides |
361 | the static fromData() function, constructing a QImage from the |
362 | given data. When loading an image, the file name can either refer |
363 | to an actual file on disk or to one of the application's embedded |
364 | resources. See \l{The Qt Resource System} overview for details |
365 | on how to embed images and other resource files in the |
366 | application's executable. |
367 | |
368 | Simply call the save() function to save a QImage object. |
369 | |
370 | The complete list of supported file formats are available through |
371 | the QImageReader::supportedImageFormats() and |
372 | QImageWriter::supportedImageFormats() functions. New file formats |
373 | can be added as plugins. By default, Qt supports the following |
374 | formats: |
375 | |
376 | \table |
377 | \header \li Format \li Description \li Qt's support |
378 | \row \li BMP \li Windows Bitmap \li Read/write |
379 | \row \li GIF \li Graphic Interchange Format (optional) \li Read |
380 | \row \li JPG \li Joint Photographic Experts Group \li Read/write |
381 | \row \li JPEG \li Joint Photographic Experts Group \li Read/write |
382 | \row \li PNG \li Portable Network Graphics \li Read/write |
383 | \row \li PBM \li Portable Bitmap \li Read |
384 | \row \li PGM \li Portable Graymap \li Read |
385 | \row \li PPM \li Portable Pixmap \li Read/write |
386 | \row \li XBM \li X11 Bitmap \li Read/write |
387 | \row \li XPM \li X11 Pixmap \li Read/write |
388 | \endtable |
389 | |
390 | \section1 Image Information |
391 | |
392 | QImage provides a collection of functions that can be used to |
393 | obtain a variety of information about the image: |
394 | |
395 | \table |
396 | \header |
397 | \li \li Available Functions |
398 | |
399 | \row |
400 | \li Geometry |
401 | \li |
402 | |
403 | The size(), width(), height(), dotsPerMeterX(), and |
404 | dotsPerMeterY() functions provide information about the image size |
405 | and aspect ratio. |
406 | |
407 | The rect() function returns the image's enclosing rectangle. The |
408 | valid() function tells if a given pair of coordinates is within |
409 | this rectangle. The offset() function returns the number of pixels |
410 | by which the image is intended to be offset by when positioned |
411 | relative to other images, which also can be manipulated using the |
412 | setOffset() function. |
413 | |
414 | \row |
415 | \li Colors |
416 | \li |
417 | |
418 | The color of a pixel can be retrieved by passing its coordinates |
419 | to the pixel() function. The pixel() function returns the color |
420 | as a QRgb value indepedent of the image's format. |
421 | |
422 | In case of monochrome and 8-bit images, the colorCount() and |
423 | colorTable() functions provide information about the color |
424 | components used to store the image data: The colorTable() function |
425 | returns the image's entire color table. To obtain a single entry, |
426 | use the pixelIndex() function to retrieve the pixel index for a |
427 | given pair of coordinates, then use the color() function to |
428 | retrieve the color. Note that if you create an 8-bit image |
429 | manually, you have to set a valid color table on the image as |
430 | well. |
431 | |
432 | The hasAlphaChannel() function tells if the image's format |
433 | respects the alpha channel, or not. The allGray() and |
434 | isGrayscale() functions tell whether an image's colors are all |
435 | shades of gray. |
436 | |
437 | See also the \l {QImage#Pixel Manipulation}{Pixel Manipulation} |
438 | and \l {QImage#Image Transformations}{Image Transformations} |
439 | sections. |
440 | |
441 | \row |
442 | \li Text |
443 | \li |
444 | |
445 | The text() function returns the image text associated with the |
446 | given text key. An image's text keys can be retrieved using the |
447 | textKeys() function. Use the setText() function to alter an |
448 | image's text. |
449 | |
450 | \row |
451 | \li Low-level information |
452 | \li |
453 | |
454 | The depth() function returns the depth of the image. The supported |
455 | depths are 1 (monochrome), 8, 16, 24 and 32 bits. The |
456 | bitPlaneCount() function tells how many of those bits that are |
457 | used. For more information see the |
458 | \l {QImage#Image Formats}{Image Formats} section. |
459 | |
460 | The format(), bytesPerLine(), and sizeInBytes() functions provide |
461 | low-level information about the data stored in the image. |
462 | |
463 | The cacheKey() function returns a number that uniquely |
464 | identifies the contents of this QImage object. |
465 | \endtable |
466 | |
467 | \section1 Pixel Manipulation |
468 | |
469 | The functions used to manipulate an image's pixels depend on the |
470 | image format. The reason is that monochrome and 8-bit images are |
471 | index-based and use a color lookup table, while 32-bit images |
472 | store ARGB values directly. For more information on image formats, |
473 | see the \l {Image Formats} section. |
474 | |
475 | In case of a 32-bit image, the setPixel() function can be used to |
476 | alter the color of the pixel at the given coordinates to any other |
477 | color specified as an ARGB quadruplet. To make a suitable QRgb |
478 | value, use the qRgb() (adding a default alpha component to the |
479 | given RGB values, i.e. creating an opaque color) or qRgba() |
480 | function. For example: |
481 | |
482 | \table |
483 | \header |
484 | \li {2,1}32-bit |
485 | \row |
486 | \li \inlineimage qimage-32bit_scaled.png |
487 | \li |
488 | \snippet code/src_gui_image_qimage.cpp 0 |
489 | \endtable |
490 | |
491 | In case of a 8-bit and monchrome images, the pixel value is only |
492 | an index from the image's color table. So the setPixel() function |
493 | can only be used to alter the color of the pixel at the given |
494 | coordinates to a predefined color from the image's color table, |
495 | i.e. it can only change the pixel's index value. To alter or add a |
496 | color to an image's color table, use the setColor() function. |
497 | |
498 | An entry in the color table is an ARGB quadruplet encoded as an |
499 | QRgb value. Use the qRgb() and qRgba() functions to make a |
500 | suitable QRgb value for use with the setColor() function. For |
501 | example: |
502 | |
503 | \table |
504 | \header |
505 | \li {2,1} 8-bit |
506 | \row |
507 | \li \inlineimage qimage-8bit_scaled.png |
508 | \li |
509 | \snippet code/src_gui_image_qimage.cpp 1 |
510 | \endtable |
511 | |
512 | For images with more than 8-bit per color-channel. The methods |
513 | setPixelColor() and pixelColor() can be used to set and get |
514 | with QColor values. |
515 | |
516 | QImage also provide the scanLine() function which returns a |
517 | pointer to the pixel data at the scanline with the given index, |
518 | and the bits() function which returns a pointer to the first pixel |
519 | data (this is equivalent to \c scanLine(0)). |
520 | |
521 | \section1 Image Formats |
522 | |
523 | Each pixel stored in a QImage is represented by an integer. The |
524 | size of the integer varies depending on the format. QImage |
525 | supports several image formats described by the \l Format |
526 | enum. |
527 | |
528 | Monochrome images are stored using 1-bit indexes into a color table |
529 | with at most two colors. There are two different types of |
530 | monochrome images: big endian (MSB first) or little endian (LSB |
531 | first) bit order. |
532 | |
533 | 8-bit images are stored using 8-bit indexes into a color table, |
534 | i.e. they have a single byte per pixel. The color table is a |
535 | QVector<QRgb>, and the QRgb typedef is equivalent to an unsigned |
536 | int containing an ARGB quadruplet on the format 0xAARRGGBB. |
537 | |
538 | 32-bit images have no color table; instead, each pixel contains an |
539 | QRgb value. There are three different types of 32-bit images |
540 | storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB |
541 | values respectively. In the premultiplied format the red, green, |
542 | and blue channels are multiplied by the alpha component divided by |
543 | 255. |
544 | |
545 | An image's format can be retrieved using the format() |
546 | function. Use the convertToFormat() functions to convert an image |
547 | into another format. The allGray() and isGrayscale() functions |
548 | tell whether a color image can safely be converted to a grayscale |
549 | image. |
550 | |
551 | \section1 Image Transformations |
552 | |
553 | QImage supports a number of functions for creating a new image |
554 | that is a transformed version of the original: The |
555 | createAlphaMask() function builds and returns a 1-bpp mask from |
556 | the alpha buffer in this image, and the createHeuristicMask() |
557 | function creates and returns a 1-bpp heuristic mask for this |
558 | image. The latter function works by selecting a color from one of |
559 | the corners, then chipping away pixels of that color starting at |
560 | all the edges. |
561 | |
562 | The mirrored() function returns a mirror of the image in the |
563 | desired direction, the scaled() returns a copy of the image scaled |
564 | to a rectangle of the desired measures, and the rgbSwapped() function |
565 | constructs a BGR image from a RGB image. |
566 | |
567 | The scaledToWidth() and scaledToHeight() functions return scaled |
568 | copies of the image. |
569 | |
570 | The transformed() function returns a copy of the image that is |
571 | transformed with the given transformation matrix and |
572 | transformation mode: Internally, the transformation matrix is |
573 | adjusted to compensate for unwanted translation, |
574 | i.e. transformed() returns the smallest image containing all |
575 | transformed points of the original image. The static trueMatrix() |
576 | function returns the actual matrix used for transforming the |
577 | image. |
578 | |
579 | There are also functions for changing attributes of an image |
580 | in-place: |
581 | |
582 | \table |
583 | \header \li Function \li Description |
584 | \row |
585 | \li setDotsPerMeterX() |
586 | \li Defines the aspect ratio by setting the number of pixels that fit |
587 | horizontally in a physical meter. |
588 | \row |
589 | \li setDotsPerMeterY() |
590 | \li Defines the aspect ratio by setting the number of pixels that fit |
591 | vertically in a physical meter. |
592 | \row |
593 | \li fill() |
594 | \li Fills the entire image with the given pixel value. |
595 | \row |
596 | \li invertPixels() |
597 | \li Inverts all pixel values in the image using the given InvertMode value. |
598 | \row |
599 | \li setColorTable() |
600 | \li Sets the color table used to translate color indexes. Only |
601 | monochrome and 8-bit formats. |
602 | \row |
603 | \li setColorCount() |
604 | \li Resizes the color table. Only monochrome and 8-bit formats. |
605 | |
606 | \endtable |
607 | |
608 | \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, {Image Composition Example}, |
609 | {Image Viewer Example}, {Scribble Example}, {Pixelator Example} |
610 | */ |
611 | |
612 | /*! |
613 | \fn QImage::QImage(QImage &&other) |
614 | |
615 | Move-constructs a QImage instance, making it point at the same |
616 | object that \a other was pointing to. |
617 | |
618 | \since 5.2 |
619 | */ |
620 | |
621 | /*! |
622 | \fn QImage &QImage::operator=(QImage &&other) |
623 | |
624 | Move-assigns \a other to this QImage instance. |
625 | |
626 | \since 5.2 |
627 | */ |
628 | |
629 | /*! |
630 | \typedef QImageCleanupFunction |
631 | \relates QImage |
632 | \since 5.0 |
633 | |
634 | A function with the following signature that can be used to |
635 | implement basic image memory management: |
636 | |
637 | \code |
638 | void myImageCleanupHandler(void *info); |
639 | \endcode |
640 | */ |
641 | |
642 | /*! |
643 | \enum QImage::InvertMode |
644 | |
645 | This enum type is used to describe how pixel values should be |
646 | inverted in the invertPixels() function. |
647 | |
648 | \value InvertRgb Invert only the RGB values and leave the alpha |
649 | channel unchanged. |
650 | |
651 | \value InvertRgba Invert all channels, including the alpha channel. |
652 | |
653 | \sa invertPixels() |
654 | */ |
655 | |
656 | /*! |
657 | \enum QImage::Format |
658 | |
659 | The following image formats are available in Qt. |
660 | See the notes after the table. |
661 | |
662 | \value Format_Invalid The image is invalid. |
663 | \value Format_Mono The image is stored using 1-bit per pixel. Bytes are |
664 | packed with the most significant bit (MSB) first. |
665 | \value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are |
666 | packed with the less significant bit (LSB) first. |
667 | |
668 | \value Format_Indexed8 The image is stored using 8-bit indexes |
669 | into a colormap. |
670 | |
671 | \value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB). |
672 | |
673 | \value Format_ARGB32 The image is stored using a 32-bit ARGB |
674 | format (0xAARRGGBB). |
675 | |
676 | \value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit |
677 | ARGB format (0xAARRGGBB), i.e. the red, |
678 | green, and blue channels are multiplied |
679 | by the alpha component divided by 255. (If RR, GG, or BB |
680 | has a higher value than the alpha channel, the results are |
681 | undefined.) Certain operations (such as image composition |
682 | using alpha blending) are faster using premultiplied ARGB32 |
683 | than with plain ARGB32. |
684 | |
685 | \value Format_RGB16 The image is stored using a 16-bit RGB format (5-6-5). |
686 | |
687 | \value Format_ARGB8565_Premultiplied The image is stored using a |
688 | premultiplied 24-bit ARGB format (8-5-6-5). |
689 | \value Format_RGB666 The image is stored using a 24-bit RGB format (6-6-6). |
690 | The unused most significant bits is always zero. |
691 | \value Format_ARGB6666_Premultiplied The image is stored using a |
692 | premultiplied 24-bit ARGB format (6-6-6-6). |
693 | \value Format_RGB555 The image is stored using a 16-bit RGB format (5-5-5). |
694 | The unused most significant bit is always zero. |
695 | \value Format_ARGB8555_Premultiplied The image is stored using a |
696 | premultiplied 24-bit ARGB format (8-5-5-5). |
697 | \value Format_RGB888 The image is stored using a 24-bit RGB format (8-8-8). |
698 | \value Format_RGB444 The image is stored using a 16-bit RGB format (4-4-4). |
699 | The unused bits are always zero. |
700 | \value Format_ARGB4444_Premultiplied The image is stored using a |
701 | premultiplied 16-bit ARGB format (4-4-4-4). |
702 | \value Format_RGBX8888 The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). |
703 | This is the same as the Format_RGBA8888 except alpha must always be 255. (added in Qt 5.2) |
704 | \value Format_RGBA8888 The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). |
705 | Unlike ARGB32 this is a byte-ordered format, which means the 32bit |
706 | encoding differs between big endian and little endian architectures, |
707 | being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors |
708 | is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2) |
709 | \value Format_RGBA8888_Premultiplied The image is stored using a |
710 | premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2) |
711 | \value Format_BGR30 The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4) |
712 | \value Format_A2BGR30_Premultiplied The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4) |
713 | \value Format_RGB30 The image is stored using a 32-bit RGB format (x-10-10-10). (added in Qt 5.4) |
714 | \value Format_A2RGB30_Premultiplied The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4) |
715 | \value Format_Alpha8 The image is stored using an 8-bit alpha only format. (added in Qt 5.5) |
716 | \value Format_Grayscale8 The image is stored using an 8-bit grayscale format. (added in Qt 5.5) |
717 | \value Format_Grayscale16 The image is stored using an 16-bit grayscale format. (added in Qt 5.13) |
718 | \value Format_RGBX64 The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). |
719 | This is the same as the Format_RGBX64 except alpha must always be 65535. (added in Qt 5.12) |
720 | \value Format_RGBA64 The image is stored using a 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12) |
721 | \value Format_RGBA64_Premultiplied The image is stored using a premultiplied 64-bit halfword-ordered |
722 | RGBA format (16-16-16-16). (added in Qt 5.12) |
723 | |
724 | \note Drawing into a QImage with QImage::Format_Indexed8 is not |
725 | supported. |
726 | |
727 | \note Avoid most rendering directly to most of these formats using QPainter. Rendering |
728 | is best optimized to the \c Format_RGB32 and \c Format_ARGB32_Premultiplied formats, and secondarily for rendering to the |
729 | \c Format_RGB16, \c Format_RGBX8888, \c Format_RGBA8888_Premultiplied, \c Format_RGBX64 and \c Format_RGBA64_Premultiplied formats |
730 | |
731 | \sa format(), convertToFormat() |
732 | */ |
733 | |
734 | /***************************************************************************** |
735 | QImage member functions |
736 | *****************************************************************************/ |
737 | |
738 | /*! |
739 | Constructs a null image. |
740 | |
741 | \sa isNull() |
742 | */ |
743 | |
744 | QImage::QImage() noexcept |
745 | : QPaintDevice() |
746 | { |
747 | d = 0; |
748 | } |
749 | |
750 | /*! |
751 | Constructs an image with the given \a width, \a height and \a |
752 | format. |
753 | |
754 | A \l{isNull()}{null} image will be returned if memory cannot be allocated. |
755 | |
756 | \warning This will create a QImage with uninitialized data. Call |
757 | fill() to fill the image with an appropriate pixel value before |
758 | drawing onto it with QPainter. |
759 | */ |
760 | QImage::QImage(int width, int height, Format format) |
761 | : QImage(QSize(width, height), format) |
762 | { |
763 | } |
764 | |
765 | /*! |
766 | Constructs an image with the given \a size and \a format. |
767 | |
768 | A \l{isNull()}{null} image is returned if memory cannot be allocated. |
769 | |
770 | \warning This will create a QImage with uninitialized data. Call |
771 | fill() to fill the image with an appropriate pixel value before |
772 | drawing onto it with QPainter. |
773 | */ |
774 | QImage::QImage(const QSize &size, Format format) |
775 | : QPaintDevice() |
776 | { |
777 | d = QImageData::create(size, format); |
778 | } |
779 | |
780 | |
781 | |
782 | QImageData *QImageData::create(uchar *data, int width, int height, int bpl, QImage::Format format, bool readOnly, QImageCleanupFunction cleanupFunction, void *cleanupInfo) |
783 | { |
784 | if (width <= 0 || height <= 0 || !data || format == QImage::Format_Invalid) |
785 | return nullptr; |
786 | |
787 | const int depth = qt_depthForFormat(format); |
788 | auto params = calculateImageParameters(width, height, depth); |
789 | if (!params.isValid()) |
790 | return nullptr; |
791 | |
792 | if (bpl > 0) { |
793 | // can't overflow, because has calculateImageParameters already done this multiplication |
794 | const int min_bytes_per_line = (width * depth + 7)/8; |
795 | if (bpl < min_bytes_per_line) |
796 | return nullptr; |
797 | |
798 | // recalculate the total with this value |
799 | params.bytesPerLine = bpl; |
800 | if (mul_overflow<qsizetype>(bpl, height, ¶ms.totalSize)) |
801 | return nullptr; |
802 | } |
803 | |
804 | QImageData *d = new QImageData; |
805 | d->ref.ref(); |
806 | |
807 | d->own_data = false; |
808 | d->ro_data = readOnly; |
809 | d->data = data; |
810 | d->width = width; |
811 | d->height = height; |
812 | d->depth = depth; |
813 | d->format = format; |
814 | |
815 | d->bytes_per_line = params.bytesPerLine; |
816 | d->nbytes = params.totalSize; |
817 | |
818 | d->cleanupFunction = cleanupFunction; |
819 | d->cleanupInfo = cleanupInfo; |
820 | |
821 | return d; |
822 | } |
823 | |
824 | /*! |
825 | Constructs an image with the given \a width, \a height and \a |
826 | format, that uses an existing memory buffer, \a data. The \a width |
827 | and \a height must be specified in pixels, \a data must be 32-bit aligned, |
828 | and each scanline of data in the image must also be 32-bit aligned. |
829 | |
830 | The buffer must remain valid throughout the life of the QImage and |
831 | all copies that have not been modified or otherwise detached from |
832 | the original buffer. The image does not delete the buffer at destruction. |
833 | You can provide a function pointer \a cleanupFunction along with an |
834 | extra pointer \a cleanupInfo that will be called when the last copy |
835 | is destroyed. |
836 | |
837 | If \a format is an indexed color format, the image color table is |
838 | initially empty and must be sufficiently expanded with |
839 | setColorCount() or setColorTable() before the image is used. |
840 | */ |
841 | QImage::QImage(uchar* data, int width, int height, Format format, QImageCleanupFunction cleanupFunction, void *cleanupInfo) |
842 | : QPaintDevice() |
843 | { |
844 | d = QImageData::create(data, width, height, 0, format, false, cleanupFunction, cleanupInfo); |
845 | } |
846 | |
847 | /*! |
848 | Constructs an image with the given \a width, \a height and \a |
849 | format, that uses an existing read-only memory buffer, \a |
850 | data. The \a width and \a height must be specified in pixels, \a |
851 | data must be 32-bit aligned, and each scanline of data in the |
852 | image must also be 32-bit aligned. |
853 | |
854 | The buffer must remain valid throughout the life of the QImage and |
855 | all copies that have not been modified or otherwise detached from |
856 | the original buffer. The image does not delete the buffer at destruction. |
857 | You can provide a function pointer \a cleanupFunction along with an |
858 | extra pointer \a cleanupInfo that will be called when the last copy |
859 | is destroyed. |
860 | |
861 | If \a format is an indexed color format, the image color table is |
862 | initially empty and must be sufficiently expanded with |
863 | setColorCount() or setColorTable() before the image is used. |
864 | |
865 | Unlike the similar QImage constructor that takes a non-const data buffer, |
866 | this version will never alter the contents of the buffer. For example, |
867 | calling QImage::bits() will return a deep copy of the image, rather than |
868 | the buffer passed to the constructor. This allows for the efficiency of |
869 | constructing a QImage from raw data, without the possibility of the raw |
870 | data being changed. |
871 | */ |
872 | QImage::QImage(const uchar* data, int width, int height, Format format, QImageCleanupFunction cleanupFunction, void *cleanupInfo) |
873 | : QPaintDevice() |
874 | { |
875 | d = QImageData::create(const_cast<uchar*>(data), width, height, 0, format, true, cleanupFunction, cleanupInfo); |
876 | } |
877 | |
878 | /*! |
879 | Constructs an image with the given \a width, \a height and \a |
880 | format, that uses an existing memory buffer, \a data. The \a width |
881 | and \a height must be specified in pixels. \a bytesPerLine |
882 | specifies the number of bytes per line (stride). |
883 | |
884 | The buffer must remain valid throughout the life of the QImage and |
885 | all copies that have not been modified or otherwise detached from |
886 | the original buffer. The image does not delete the buffer at destruction. |
887 | You can provide a function pointer \a cleanupFunction along with an |
888 | extra pointer \a cleanupInfo that will be called when the last copy |
889 | is destroyed. |
890 | |
891 | If \a format is an indexed color format, the image color table is |
892 | initially empty and must be sufficiently expanded with |
893 | setColorCount() or setColorTable() before the image is used. |
894 | */ |
895 | QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction, void *cleanupInfo) |
896 | :QPaintDevice() |
897 | { |
898 | d = QImageData::create(data, width, height, bytesPerLine, format, false, cleanupFunction, cleanupInfo); |
899 | } |
900 | |
901 | |
902 | /*! |
903 | Constructs an image with the given \a width, \a height and \a |
904 | format, that uses an existing memory buffer, \a data. The \a width |
905 | and \a height must be specified in pixels. \a bytesPerLine |
906 | specifies the number of bytes per line (stride). |
907 | |
908 | The buffer must remain valid throughout the life of the QImage and |
909 | all copies that have not been modified or otherwise detached from |
910 | the original buffer. The image does not delete the buffer at destruction. |
911 | You can provide a function pointer \a cleanupFunction along with an |
912 | extra pointer \a cleanupInfo that will be called when the last copy |
913 | is destroyed. |
914 | |
915 | If \a format is an indexed color format, the image color table is |
916 | initially empty and must be sufficiently expanded with |
917 | setColorCount() or setColorTable() before the image is used. |
918 | |
919 | Unlike the similar QImage constructor that takes a non-const data buffer, |
920 | this version will never alter the contents of the buffer. For example, |
921 | calling QImage::bits() will return a deep copy of the image, rather than |
922 | the buffer passed to the constructor. This allows for the efficiency of |
923 | constructing a QImage from raw data, without the possibility of the raw |
924 | data being changed. |
925 | */ |
926 | |
927 | QImage::QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction, void *cleanupInfo) |
928 | :QPaintDevice() |
929 | { |
930 | d = QImageData::create(const_cast<uchar*>(data), width, height, bytesPerLine, format, true, cleanupFunction, cleanupInfo); |
931 | } |
932 | |
933 | /*! |
934 | Constructs an image and tries to load the image from the file with |
935 | the given \a fileName. |
936 | |
937 | The loader attempts to read the image using the specified \a |
938 | format. If the \a format is not specified (which is the default), |
939 | it is auto-detected based on the file's suffix and header. For |
940 | details, see {QImageReader::setAutoDetectImageFormat()}{QImageReader}. |
941 | |
942 | If the loading of the image failed, this object is a null image. |
943 | |
944 | The file name can either refer to an actual file on disk or to one |
945 | of the application's embedded resources. See the |
946 | \l{resources.html}{Resource System} overview for details on how to |
947 | embed images and other resource files in the application's |
948 | executable. |
949 | |
950 | \sa isNull(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files} |
951 | */ |
952 | |
953 | QImage::QImage(const QString &fileName, const char *format) |
954 | : QPaintDevice() |
955 | { |
956 | d = 0; |
957 | load(fileName, format); |
958 | } |
959 | |
960 | #ifndef QT_NO_IMAGEFORMAT_XPM |
961 | extern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image); |
962 | |
963 | /*! |
964 | Constructs an image from the given \a xpm image. |
965 | |
966 | Make sure that the image is a valid XPM image. Errors are silently |
967 | ignored. |
968 | |
969 | Note that it's possible to squeeze the XPM variable a little bit |
970 | by using an unusual declaration: |
971 | |
972 | \snippet code/src_gui_image_qimage.cpp 2 |
973 | |
974 | The extra \c const makes the entire definition read-only, which is |
975 | slightly more efficient (e.g., when the code is in a shared |
976 | library) and able to be stored in ROM with the application. |
977 | */ |
978 | |
979 | QImage::QImage(const char * const xpm[]) |
980 | : QPaintDevice() |
981 | { |
982 | d = 0; |
983 | if (!xpm) |
984 | return; |
985 | if (!qt_read_xpm_image_or_array(0, xpm, *this)) |
986 | // Issue: Warning because the constructor may be ambigious |
987 | qWarning("QImage::QImage(), XPM is not supported" ); |
988 | } |
989 | #endif // QT_NO_IMAGEFORMAT_XPM |
990 | |
991 | /*! |
992 | Constructs a shallow copy of the given \a image. |
993 | |
994 | For more information about shallow copies, see the \l {Implicit |
995 | Data Sharing} documentation. |
996 | |
997 | \sa copy() |
998 | */ |
999 | |
1000 | QImage::QImage(const QImage &image) |
1001 | : QPaintDevice() |
1002 | { |
1003 | if (image.paintingActive() || isLocked(image.d)) { |
1004 | d = 0; |
1005 | image.copy().swap(*this); |
1006 | } else { |
1007 | d = image.d; |
1008 | if (d) |
1009 | d->ref.ref(); |
1010 | } |
1011 | } |
1012 | |
1013 | /*! |
1014 | Destroys the image and cleans up. |
1015 | */ |
1016 | |
1017 | QImage::~QImage() |
1018 | { |
1019 | if (d && !d->ref.deref()) |
1020 | delete d; |
1021 | } |
1022 | |
1023 | /*! |
1024 | Assigns a shallow copy of the given \a image to this image and |
1025 | returns a reference to this image. |
1026 | |
1027 | For more information about shallow copies, see the \l {Implicit |
1028 | Data Sharing} documentation. |
1029 | |
1030 | \sa copy(), QImage() |
1031 | */ |
1032 | |
1033 | QImage &QImage::operator=(const QImage &image) |
1034 | { |
1035 | if (image.paintingActive() || isLocked(image.d)) { |
1036 | operator=(image.copy()); |
1037 | } else { |
1038 | if (image.d) |
1039 | image.d->ref.ref(); |
1040 | if (d && !d->ref.deref()) |
1041 | delete d; |
1042 | d = image.d; |
1043 | } |
1044 | return *this; |
1045 | } |
1046 | |
1047 | /*! |
1048 | \fn void QImage::swap(QImage &other) |
1049 | \since 4.8 |
1050 | |
1051 | Swaps image \a other with this image. This operation is very |
1052 | fast and never fails. |
1053 | */ |
1054 | |
1055 | /*! |
1056 | \internal |
1057 | */ |
1058 | int QImage::devType() const |
1059 | { |
1060 | return QInternal::Image; |
1061 | } |
1062 | |
1063 | /*! |
1064 | Returns the image as a QVariant. |
1065 | */ |
1066 | QImage::operator QVariant() const |
1067 | { |
1068 | return QVariant(QVariant::Image, this); |
1069 | } |
1070 | |
1071 | /*! |
1072 | \internal |
1073 | |
1074 | If multiple images share common data, this image makes a copy of |
1075 | the data and detaches itself from the sharing mechanism, making |
1076 | sure that this image is the only one referring to the data. |
1077 | |
1078 | Nothing is done if there is just a single reference. |
1079 | |
1080 | \sa copy(), {QImage::isDetached()}{isDetached()}, {Implicit Data Sharing} |
1081 | */ |
1082 | void QImage::detach() |
1083 | { |
1084 | if (d) { |
1085 | if (d->is_cached && d->ref.loadRelaxed() == 1) |
1086 | QImagePixmapCleanupHooks::executeImageHooks(cacheKey()); |
1087 | |
1088 | if (d->ref.loadRelaxed() != 1 || d->ro_data) |
1089 | *this = copy(); |
1090 | |
1091 | if (d) |
1092 | ++d->detach_no; |
1093 | } |
1094 | } |
1095 | |
1096 | |
1097 | static void copyPhysicalMetadata(QImageData *dst, const QImageData *src) |
1098 | { |
1099 | dst->dpmx = src->dpmx; |
1100 | dst->dpmy = src->dpmy; |
1101 | dst->devicePixelRatio = src->devicePixelRatio; |
1102 | } |
1103 | |
1104 | static void copyMetadata(QImageData *dst, const QImageData *src) |
1105 | { |
1106 | // Doesn't copy colortable and alpha_clut, or offset. |
1107 | copyPhysicalMetadata(dst, src); |
1108 | dst->text = src->text; |
1109 | dst->colorSpace = src->colorSpace; |
1110 | } |
1111 | |
1112 | static void copyMetadata(QImage *dst, const QImage &src) |
1113 | { |
1114 | dst->setDotsPerMeterX(src.dotsPerMeterX()); |
1115 | dst->setDotsPerMeterY(src.dotsPerMeterY()); |
1116 | dst->setDevicePixelRatio(src.devicePixelRatio()); |
1117 | const auto textKeys = src.textKeys(); |
1118 | for (const auto &key: textKeys) |
1119 | dst->setText(key, src.text(key)); |
1120 | |
1121 | } |
1122 | |
1123 | /*! |
1124 | \fn QImage QImage::copy(int x, int y, int width, int height) const |
1125 | \overload |
1126 | |
1127 | The returned image is copied from the position (\a x, \a y) in |
1128 | this image, and will always have the given \a width and \a height. |
1129 | In areas beyond this image, pixels are set to 0. |
1130 | |
1131 | */ |
1132 | |
1133 | /*! |
1134 | \fn QImage QImage::copy(const QRect& rectangle) const |
1135 | |
1136 | Returns a sub-area of the image as a new image. |
1137 | |
1138 | The returned image is copied from the position (\a |
1139 | {rectangle}.x(), \a{rectangle}.y()) in this image, and will always |
1140 | have the size of the given \a rectangle. |
1141 | |
1142 | In areas beyond this image, pixels are set to 0. For 32-bit RGB |
1143 | images, this means black; for 32-bit ARGB images, this means |
1144 | transparent black; for 8-bit images, this means the color with |
1145 | index 0 in the color table which can be anything; for 1-bit |
1146 | images, this means Qt::color0. |
1147 | |
1148 | If the given \a rectangle is a null rectangle the entire image is |
1149 | copied. |
1150 | |
1151 | \sa QImage() |
1152 | */ |
1153 | QImage QImage::copy(const QRect& r) const |
1154 | { |
1155 | if (!d) |
1156 | return QImage(); |
1157 | |
1158 | if (r.isNull()) { |
1159 | QImage image(d->width, d->height, d->format); |
1160 | if (image.isNull()) |
1161 | return image; |
1162 | |
1163 | // Qt for Embedded Linux can create images with non-default bpl |
1164 | // make sure we don't crash. |
1165 | if (image.d->nbytes != d->nbytes) { |
1166 | int bpl = qMin(bytesPerLine(), image.bytesPerLine()); |
1167 | for (int i = 0; i < height(); i++) |
1168 | memcpy(image.scanLine(i), scanLine(i), bpl); |
1169 | } else |
1170 | memcpy(image.bits(), bits(), d->nbytes); |
1171 | image.d->colortable = d->colortable; |
1172 | image.d->offset = d->offset; |
1173 | image.d->has_alpha_clut = d->has_alpha_clut; |
1174 | copyMetadata(image.d, d); |
1175 | return image; |
1176 | } |
1177 | |
1178 | int x = r.x(); |
1179 | int y = r.y(); |
1180 | int w = r.width(); |
1181 | int h = r.height(); |
1182 | |
1183 | int dx = 0; |
1184 | int dy = 0; |
1185 | if (w <= 0 || h <= 0) |
1186 | return QImage(); |
1187 | |
1188 | QImage image(w, h, d->format); |
1189 | if (image.isNull()) |
1190 | return image; |
1191 | |
1192 | if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) { |
1193 | // bitBlt will not cover entire image - clear it. |
1194 | image.fill(0); |
1195 | if (x < 0) { |
1196 | dx = -x; |
1197 | x = 0; |
1198 | } |
1199 | if (y < 0) { |
1200 | dy = -y; |
1201 | y = 0; |
1202 | } |
1203 | } |
1204 | |
1205 | image.d->colortable = d->colortable; |
1206 | |
1207 | int pixels_to_copy = qMax(w - dx, 0); |
1208 | if (x > d->width) |
1209 | pixels_to_copy = 0; |
1210 | else if (pixels_to_copy > d->width - x) |
1211 | pixels_to_copy = d->width - x; |
1212 | int lines_to_copy = qMax(h - dy, 0); |
1213 | if (y > d->height) |
1214 | lines_to_copy = 0; |
1215 | else if (lines_to_copy > d->height - y) |
1216 | lines_to_copy = d->height - y; |
1217 | |
1218 | bool byteAligned = true; |
1219 | if (d->format == Format_Mono || d->format == Format_MonoLSB) |
1220 | byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7); |
1221 | |
1222 | if (byteAligned) { |
1223 | const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line; |
1224 | uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line; |
1225 | const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3; |
1226 | for (int i = 0; i < lines_to_copy; ++i) { |
1227 | memcpy(dest, src, bytes_to_copy); |
1228 | src += d->bytes_per_line; |
1229 | dest += image.d->bytes_per_line; |
1230 | } |
1231 | } else if (d->format == Format_Mono) { |
1232 | const uchar *src = d->data + y * d->bytes_per_line; |
1233 | uchar *dest = image.d->data + dy * image.d->bytes_per_line; |
1234 | for (int i = 0; i < lines_to_copy; ++i) { |
1235 | for (int j = 0; j < pixels_to_copy; ++j) { |
1236 | if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7))) |
1237 | dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7)); |
1238 | else |
1239 | dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7)); |
1240 | } |
1241 | src += d->bytes_per_line; |
1242 | dest += image.d->bytes_per_line; |
1243 | } |
1244 | } else { // Format_MonoLSB |
1245 | Q_ASSERT(d->format == Format_MonoLSB); |
1246 | const uchar *src = d->data + y * d->bytes_per_line; |
1247 | uchar *dest = image.d->data + dy * image.d->bytes_per_line; |
1248 | for (int i = 0; i < lines_to_copy; ++i) { |
1249 | for (int j = 0; j < pixels_to_copy; ++j) { |
1250 | if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7))) |
1251 | dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7)); |
1252 | else |
1253 | dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7)); |
1254 | } |
1255 | src += d->bytes_per_line; |
1256 | dest += image.d->bytes_per_line; |
1257 | } |
1258 | } |
1259 | |
1260 | copyMetadata(image.d, d); |
1261 | image.d->offset = offset(); |
1262 | image.d->has_alpha_clut = d->has_alpha_clut; |
1263 | return image; |
1264 | } |
1265 | |
1266 | |
1267 | /*! |
1268 | \fn bool QImage::isNull() const |
1269 | |
1270 | Returns \c true if it is a null image, otherwise returns \c false. |
1271 | |
1272 | A null image has all parameters set to zero and no allocated data. |
1273 | */ |
1274 | bool QImage::isNull() const |
1275 | { |
1276 | return !d; |
1277 | } |
1278 | |
1279 | /*! |
1280 | \fn int QImage::width() const |
1281 | |
1282 | Returns the width of the image. |
1283 | |
1284 | \sa {QImage#Image Information}{Image Information} |
1285 | */ |
1286 | int QImage::width() const |
1287 | { |
1288 | return d ? d->width : 0; |
1289 | } |
1290 | |
1291 | /*! |
1292 | \fn int QImage::height() const |
1293 | |
1294 | Returns the height of the image. |
1295 | |
1296 | \sa {QImage#Image Information}{Image Information} |
1297 | */ |
1298 | int QImage::height() const |
1299 | { |
1300 | return d ? d->height : 0; |
1301 | } |
1302 | |
1303 | /*! |
1304 | \fn QSize QImage::size() const |
1305 | |
1306 | Returns the size of the image, i.e. its width() and height(). |
1307 | |
1308 | \sa {QImage#Image Information}{Image Information} |
1309 | */ |
1310 | QSize QImage::size() const |
1311 | { |
1312 | return d ? QSize(d->width, d->height) : QSize(0, 0); |
1313 | } |
1314 | |
1315 | /*! |
1316 | \fn QRect QImage::rect() const |
1317 | |
1318 | Returns the enclosing rectangle (0, 0, width(), height()) of the |
1319 | image. |
1320 | |
1321 | \sa {QImage#Image Information}{Image Information} |
1322 | */ |
1323 | QRect QImage::rect() const |
1324 | { |
1325 | return d ? QRect(0, 0, d->width, d->height) : QRect(); |
1326 | } |
1327 | |
1328 | /*! |
1329 | Returns the depth of the image. |
1330 | |
1331 | The image depth is the number of bits used to store a single |
1332 | pixel, also called bits per pixel (bpp). |
1333 | |
1334 | The supported depths are 1, 8, 16, 24, 32 and 64. |
1335 | |
1336 | \sa bitPlaneCount(), convertToFormat(), {QImage#Image Formats}{Image Formats}, |
1337 | {QImage#Image Information}{Image Information} |
1338 | |
1339 | */ |
1340 | int QImage::depth() const |
1341 | { |
1342 | return d ? d->depth : 0; |
1343 | } |
1344 | |
1345 | /*! |
1346 | \obsolete |
1347 | \fn int QImage::numColors() const |
1348 | |
1349 | Returns the size of the color table for the image. |
1350 | |
1351 | \sa setColorCount() |
1352 | */ |
1353 | |
1354 | /*! |
1355 | \since 4.6 |
1356 | \fn int QImage::colorCount() const |
1357 | |
1358 | Returns the size of the color table for the image. |
1359 | |
1360 | Notice that colorCount() returns 0 for 32-bpp images because these |
1361 | images do not use color tables, but instead encode pixel values as |
1362 | ARGB quadruplets. |
1363 | |
1364 | \sa setColorCount(), {QImage#Image Information}{Image Information} |
1365 | */ |
1366 | int QImage::colorCount() const |
1367 | { |
1368 | return d ? d->colortable.size() : 0; |
1369 | } |
1370 | |
1371 | /*! |
1372 | Sets the color table used to translate color indexes to QRgb |
1373 | values, to the specified \a colors. |
1374 | |
1375 | When the image is used, the color table must be large enough to |
1376 | have entries for all the pixel/index values present in the image, |
1377 | otherwise the results are undefined. |
1378 | |
1379 | \sa colorTable(), setColor(), {QImage#Image Transformations}{Image |
1380 | Transformations} |
1381 | */ |
1382 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
1383 | void QImage::setColorTable(const QVector<QRgb> &colors) |
1384 | #else |
1385 | void QImage::setColorTable(const QVector<QRgb> colors) |
1386 | #endif |
1387 | { |
1388 | if (!d) |
1389 | return; |
1390 | detach(); |
1391 | |
1392 | // In case detach() ran out of memory |
1393 | if (!d) |
1394 | return; |
1395 | |
1396 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
1397 | d->colortable = colors; |
1398 | #else |
1399 | d->colortable = std::move(const_cast<QVector<QRgb>&>(colors)); |
1400 | #endif |
1401 | d->has_alpha_clut = false; |
1402 | for (int i = 0; i < d->colortable.size(); ++i) { |
1403 | if (qAlpha(d->colortable.at(i)) != 255) { |
1404 | d->has_alpha_clut = true; |
1405 | break; |
1406 | } |
1407 | } |
1408 | } |
1409 | |
1410 | /*! |
1411 | Returns a list of the colors contained in the image's color table, |
1412 | or an empty list if the image does not have a color table |
1413 | |
1414 | \sa setColorTable(), colorCount(), color() |
1415 | */ |
1416 | QVector<QRgb> QImage::colorTable() const |
1417 | { |
1418 | return d ? d->colortable : QVector<QRgb>(); |
1419 | } |
1420 | |
1421 | /*! |
1422 | Returns the device pixel ratio for the image. This is the |
1423 | ratio between \e{device pixels} and \e{device independent pixels}. |
1424 | |
1425 | Use this function when calculating layout geometry based on |
1426 | the image size: QSize layoutSize = image.size() / image.devicePixelRatio() |
1427 | |
1428 | The default value is 1.0. |
1429 | |
1430 | \sa setDevicePixelRatio(), QImageReader |
1431 | */ |
1432 | qreal QImage::devicePixelRatio() const |
1433 | { |
1434 | if (!d) |
1435 | return 1.0; |
1436 | return d->devicePixelRatio; |
1437 | } |
1438 | |
1439 | /*! |
1440 | Sets the device pixel ratio for the image. This is the |
1441 | ratio between image pixels and device-independent pixels. |
1442 | |
1443 | The default \a scaleFactor is 1.0. Setting it to something else has |
1444 | two effects: |
1445 | |
1446 | QPainters that are opened on the image will be scaled. For |
1447 | example, painting on a 200x200 image if with a ratio of 2.0 |
1448 | will result in effective (device-independent) painting bounds |
1449 | of 100x100. |
1450 | |
1451 | Code paths in Qt that calculate layout geometry based on the |
1452 | image size will take the ratio into account: |
1453 | QSize layoutSize = image.size() / image.devicePixelRatio() |
1454 | The net effect of this is that the image is displayed as |
1455 | high-DPI image rather than a large image |
1456 | (see \l{Drawing High Resolution Versions of Pixmaps and Images}). |
1457 | |
1458 | \sa devicePixelRatio() |
1459 | */ |
1460 | void QImage::setDevicePixelRatio(qreal scaleFactor) |
1461 | { |
1462 | if (!d) |
1463 | return; |
1464 | |
1465 | if (scaleFactor == d->devicePixelRatio) |
1466 | return; |
1467 | |
1468 | detach(); |
1469 | if (d) |
1470 | d->devicePixelRatio = scaleFactor; |
1471 | } |
1472 | |
1473 | #if QT_DEPRECATED_SINCE(5, 10) |
1474 | /*! |
1475 | \since 4.6 |
1476 | \obsolete |
1477 | Returns the number of bytes occupied by the image data. |
1478 | |
1479 | Note this method should never be called on an image larger than 2 gigabytes. |
1480 | Instead use sizeInBytes(). |
1481 | |
1482 | \sa sizeInBytes(), bytesPerLine(), bits(), {QImage#Image Information}{Image |
1483 | Information} |
1484 | */ |
1485 | int QImage::byteCount() const |
1486 | { |
1487 | Q_ASSERT(!d || d->nbytes < std::numeric_limits<int>::max()); |
1488 | return d ? int(d->nbytes) : 0; |
1489 | } |
1490 | #endif |
1491 | |
1492 | /*! |
1493 | \since 5.10 |
1494 | Returns the image data size in bytes. |
1495 | |
1496 | \sa byteCount(), bytesPerLine(), bits(), {QImage#Image Information}{Image |
1497 | Information} |
1498 | */ |
1499 | qsizetype QImage::sizeInBytes() const |
1500 | { |
1501 | return d ? d->nbytes : 0; |
1502 | } |
1503 | |
1504 | /*! |
1505 | Returns the number of bytes per image scanline. |
1506 | |
1507 | This is equivalent to sizeInBytes() / height() if height() is non-zero. |
1508 | |
1509 | \sa scanLine() |
1510 | */ |
1511 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
1512 | qsizetype QImage::bytesPerLine() const |
1513 | { |
1514 | return d ? d->bytes_per_line : 0; |
1515 | } |
1516 | #else |
1517 | int QImage::bytesPerLine() const |
1518 | { |
1519 | return d ? d->bytes_per_line : 0; |
1520 | } |
1521 | #endif |
1522 | |
1523 | |
1524 | /*! |
1525 | Returns the color in the color table at index \a i. The first |
1526 | color is at index 0. |
1527 | |
1528 | The colors in an image's color table are specified as ARGB |
1529 | quadruplets (QRgb). Use the qAlpha(), qRed(), qGreen(), and |
1530 | qBlue() functions to get the color value components. |
1531 | |
1532 | \sa setColor(), pixelIndex(), {QImage#Pixel Manipulation}{Pixel |
1533 | Manipulation} |
1534 | */ |
1535 | QRgb QImage::color(int i) const |
1536 | { |
1537 | Q_ASSERT(i < colorCount()); |
1538 | return d ? d->colortable.at(i) : QRgb(uint(-1)); |
1539 | } |
1540 | |
1541 | /*! |
1542 | \fn void QImage::setColor(int index, QRgb colorValue) |
1543 | |
1544 | Sets the color at the given \a index in the color table, to the |
1545 | given to \a colorValue. The color value is an ARGB quadruplet. |
1546 | |
1547 | If \a index is outside the current size of the color table, it is |
1548 | expanded with setColorCount(). |
1549 | |
1550 | \sa color(), colorCount(), setColorTable(), {QImage#Pixel Manipulation}{Pixel |
1551 | Manipulation} |
1552 | */ |
1553 | void QImage::setColor(int i, QRgb c) |
1554 | { |
1555 | if (!d) |
1556 | return; |
1557 | if (i < 0 || d->depth > 8 || i >= 1<<d->depth) { |
1558 | qWarning("QImage::setColor: Index out of bound %d" , i); |
1559 | return; |
1560 | } |
1561 | detach(); |
1562 | |
1563 | // In case detach() run out of memory |
1564 | if (!d) |
1565 | return; |
1566 | |
1567 | if (i >= d->colortable.size()) |
1568 | setColorCount(i+1); |
1569 | d->colortable[i] = c; |
1570 | d->has_alpha_clut |= (qAlpha(c) != 255); |
1571 | } |
1572 | |
1573 | /*! |
1574 | Returns a pointer to the pixel data at the scanline with index \a |
1575 | i. The first scanline is at index 0. |
1576 | |
1577 | The scanline data is as minimum 32-bit aligned. For 64-bit formats |
1578 | it follows the native alignment of 64-bit integers (64-bit for most |
1579 | platforms, but notably 32-bit on i386). |
1580 | |
1581 | \warning If you are accessing 32-bpp image data, cast the returned |
1582 | pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to |
1583 | read/write the pixel value. You cannot use the \c{uchar*} pointer |
1584 | directly, because the pixel format depends on the byte order on |
1585 | the underlying platform. Use qRed(), qGreen(), qBlue(), and |
1586 | qAlpha() to access the pixels. |
1587 | |
1588 | \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel |
1589 | Manipulation}, constScanLine() |
1590 | */ |
1591 | uchar *QImage::scanLine(int i) |
1592 | { |
1593 | if (!d) |
1594 | return 0; |
1595 | |
1596 | detach(); |
1597 | |
1598 | // In case detach() ran out of memory |
1599 | if (!d) |
1600 | return 0; |
1601 | |
1602 | return d->data + i * d->bytes_per_line; |
1603 | } |
1604 | |
1605 | /*! |
1606 | \overload |
1607 | */ |
1608 | const uchar *QImage::scanLine(int i) const |
1609 | { |
1610 | if (!d) |
1611 | return 0; |
1612 | |
1613 | Q_ASSERT(i >= 0 && i < height()); |
1614 | return d->data + i * d->bytes_per_line; |
1615 | } |
1616 | |
1617 | |
1618 | /*! |
1619 | Returns a pointer to the pixel data at the scanline with index \a |
1620 | i. The first scanline is at index 0. |
1621 | |
1622 | The scanline data is aligned on a 32-bit boundary. |
1623 | |
1624 | Note that QImage uses \l{Implicit Data Sharing} {implicit data |
1625 | sharing}, but this function does \e not perform a deep copy of the |
1626 | shared pixel data, because the returned data is const. |
1627 | |
1628 | \sa scanLine(), constBits() |
1629 | \since 4.7 |
1630 | */ |
1631 | const uchar *QImage::constScanLine(int i) const |
1632 | { |
1633 | if (!d) |
1634 | return 0; |
1635 | |
1636 | Q_ASSERT(i >= 0 && i < height()); |
1637 | return d->data + i * d->bytes_per_line; |
1638 | } |
1639 | |
1640 | /*! |
1641 | Returns a pointer to the first pixel data. This is equivalent to |
1642 | scanLine(0). |
1643 | |
1644 | Note that QImage uses \l{Implicit Data Sharing} {implicit data |
1645 | sharing}. This function performs a deep copy of the shared pixel |
1646 | data, thus ensuring that this QImage is the only one using the |
1647 | current return value. |
1648 | |
1649 | \sa scanLine(), sizeInBytes(), constBits() |
1650 | */ |
1651 | uchar *QImage::bits() |
1652 | { |
1653 | if (!d) |
1654 | return 0; |
1655 | detach(); |
1656 | |
1657 | // In case detach ran out of memory... |
1658 | if (!d) |
1659 | return 0; |
1660 | |
1661 | return d->data; |
1662 | } |
1663 | |
1664 | /*! |
1665 | \overload |
1666 | |
1667 | Note that QImage uses \l{Implicit Data Sharing} {implicit data |
1668 | sharing}, but this function does \e not perform a deep copy of the |
1669 | shared pixel data, because the returned data is const. |
1670 | */ |
1671 | const uchar *QImage::bits() const |
1672 | { |
1673 | return d ? d->data : 0; |
1674 | } |
1675 | |
1676 | |
1677 | /*! |
1678 | Returns a pointer to the first pixel data. |
1679 | |
1680 | Note that QImage uses \l{Implicit Data Sharing} {implicit data |
1681 | sharing}, but this function does \e not perform a deep copy of the |
1682 | shared pixel data, because the returned data is const. |
1683 | |
1684 | \sa bits(), constScanLine() |
1685 | \since 4.7 |
1686 | */ |
1687 | const uchar *QImage::constBits() const |
1688 | { |
1689 | return d ? d->data : 0; |
1690 | } |
1691 | |
1692 | /*! |
1693 | \fn void QImage::fill(uint pixelValue) |
1694 | |
1695 | Fills the entire image with the given \a pixelValue. |
1696 | |
1697 | If the depth of this image is 1, only the lowest bit is used. If |
1698 | you say fill(0), fill(2), etc., the image is filled with 0s. If |
1699 | you say fill(1), fill(3), etc., the image is filled with 1s. If |
1700 | the depth is 8, the lowest 8 bits are used and if the depth is 16 |
1701 | the lowest 16 bits are used. |
1702 | |
1703 | Note: QImage::pixel() returns the color of the pixel at the given |
1704 | coordinates while QColor::pixel() returns the pixel value of the |
1705 | underlying window system (essentially an index value), so normally |
1706 | you will want to use QImage::pixel() to use a color from an |
1707 | existing image or QColor::rgb() to use a specific color. |
1708 | |
1709 | \sa depth(), {QImage#Image Transformations}{Image Transformations} |
1710 | */ |
1711 | |
1712 | void QImage::fill(uint pixel) |
1713 | { |
1714 | if (!d) |
1715 | return; |
1716 | |
1717 | detach(); |
1718 | |
1719 | // In case detach() ran out of memory |
1720 | if (!d) |
1721 | return; |
1722 | |
1723 | if (d->depth == 1 || d->depth == 8) { |
1724 | int w = d->width; |
1725 | if (d->depth == 1) { |
1726 | if (pixel & 1) |
1727 | pixel = 0xffffffff; |
1728 | else |
1729 | pixel = 0; |
1730 | w = (w + 7) / 8; |
1731 | } else { |
1732 | pixel &= 0xff; |
1733 | } |
1734 | qt_rectfill<quint8>(d->data, pixel, 0, 0, |
1735 | w, d->height, d->bytes_per_line); |
1736 | return; |
1737 | } else if (d->depth == 16) { |
1738 | qt_rectfill<quint16>(reinterpret_cast<quint16*>(d->data), pixel, |
1739 | 0, 0, d->width, d->height, d->bytes_per_line); |
1740 | return; |
1741 | } else if (d->depth == 24) { |
1742 | qt_rectfill<quint24>(reinterpret_cast<quint24*>(d->data), pixel, |
1743 | 0, 0, d->width, d->height, d->bytes_per_line); |
1744 | return; |
1745 | } else if (d->depth == 64) { |
1746 | qt_rectfill<quint64>(reinterpret_cast<quint64*>(d->data), QRgba64::fromArgb32(pixel), |
1747 | 0, 0, d->width, d->height, d->bytes_per_line); |
1748 | return; |
1749 | } |
1750 | |
1751 | if (d->format == Format_RGB32) |
1752 | pixel |= 0xff000000; |
1753 | if (d->format == Format_RGBX8888) |
1754 | #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
1755 | pixel |= 0xff000000; |
1756 | #else |
1757 | pixel |= 0x000000ff; |
1758 | #endif |
1759 | if (d->format == Format_BGR30 || d->format == Format_RGB30) |
1760 | pixel |= 0xc0000000; |
1761 | |
1762 | qt_rectfill<uint>(reinterpret_cast<uint*>(d->data), pixel, |
1763 | 0, 0, d->width, d->height, d->bytes_per_line); |
1764 | } |
1765 | |
1766 | |
1767 | /*! |
1768 | \fn void QImage::fill(Qt::GlobalColor color) |
1769 | \overload |
1770 | \since 4.8 |
1771 | |
1772 | Fills the image with the given \a color, described as a standard global |
1773 | color. |
1774 | */ |
1775 | |
1776 | void QImage::fill(Qt::GlobalColor color) |
1777 | { |
1778 | fill(QColor(color)); |
1779 | } |
1780 | |
1781 | |
1782 | |
1783 | /*! |
1784 | \fn void QImage::fill(const QColor &color) |
1785 | |
1786 | \overload |
1787 | |
1788 | Fills the entire image with the given \a color. |
1789 | |
1790 | If the depth of the image is 1, the image will be filled with 1 if |
1791 | \a color equals Qt::color1; it will otherwise be filled with 0. |
1792 | |
1793 | If the depth of the image is 8, the image will be filled with the |
1794 | index corresponding the \a color in the color table if present; it |
1795 | will otherwise be filled with 0. |
1796 | |
1797 | \since 4.8 |
1798 | */ |
1799 | |
1800 | void QImage::fill(const QColor &color) |
1801 | { |
1802 | if (!d) |
1803 | return; |
1804 | detach(); |
1805 | |
1806 | // In case we run out of memory |
1807 | if (!d) |
1808 | return; |
1809 | |
1810 | switch (d->format) { |
1811 | case QImage::Format_RGB32: |
1812 | case QImage::Format_ARGB32: |
1813 | fill(color.rgba()); |
1814 | break; |
1815 | case QImage::Format_ARGB32_Premultiplied: |
1816 | fill(qPremultiply(color.rgba())); |
1817 | break; |
1818 | case QImage::Format_RGBX8888: |
1819 | fill(ARGB2RGBA(color.rgba() | 0xff000000)); |
1820 | break; |
1821 | case QImage::Format_RGBA8888: |
1822 | fill(ARGB2RGBA(color.rgba())); |
1823 | break; |
1824 | case QImage::Format_RGBA8888_Premultiplied: |
1825 | fill(ARGB2RGBA(qPremultiply(color.rgba()))); |
1826 | break; |
1827 | case QImage::Format_BGR30: |
1828 | case QImage::Format_A2BGR30_Premultiplied: |
1829 | fill(qConvertRgb64ToRgb30<PixelOrderBGR>(color.rgba64())); |
1830 | break; |
1831 | case QImage::Format_RGB30: |
1832 | case QImage::Format_A2RGB30_Premultiplied: |
1833 | fill(qConvertRgb64ToRgb30<PixelOrderRGB>(color.rgba64())); |
1834 | break; |
1835 | case QImage::Format_RGB16: |
1836 | fill((uint) qConvertRgb32To16(color.rgba())); |
1837 | break; |
1838 | case QImage::Format_Indexed8: { |
1839 | uint pixel = 0; |
1840 | for (int i=0; i<d->colortable.size(); ++i) { |
1841 | if (color.rgba() == d->colortable.at(i)) { |
1842 | pixel = i; |
1843 | break; |
1844 | } |
1845 | } |
1846 | fill(pixel); |
1847 | break; |
1848 | } |
1849 | case QImage::Format_Mono: |
1850 | case QImage::Format_MonoLSB: |
1851 | if (color == Qt::color1) |
1852 | fill((uint) 1); |
1853 | else |
1854 | fill((uint) 0); |
1855 | break; |
1856 | case QImage::Format_RGBX64: { |
1857 | QRgba64 c = color.rgba64(); |
1858 | c.setAlpha(65535); |
1859 | qt_rectfill<quint64>(reinterpret_cast<quint64*>(d->data), c, |
1860 | 0, 0, d->width, d->height, d->bytes_per_line); |
1861 | break; |
1862 | |
1863 | } |
1864 | case QImage::Format_RGBA64: |
1865 | case QImage::Format_RGBA64_Premultiplied: |
1866 | qt_rectfill<quint64>(reinterpret_cast<quint64*>(d->data), color.rgba64(), |
1867 | 0, 0, d->width, d->height, d->bytes_per_line); |
1868 | break; |
1869 | default: { |
1870 | QPainter p(this); |
1871 | p.setCompositionMode(QPainter::CompositionMode_Source); |
1872 | p.fillRect(rect(), color); |
1873 | }} |
1874 | } |
1875 | |
1876 | |
1877 | |
1878 | /*! |
1879 | Inverts all pixel values in the image. |
1880 | |
1881 | The given invert \a mode only have a meaning when the image's |
1882 | depth is 32. The default \a mode is InvertRgb, which leaves the |
1883 | alpha channel unchanged. If the \a mode is InvertRgba, the alpha |
1884 | bits are also inverted. |
1885 | |
1886 | Inverting an 8-bit image means to replace all pixels using color |
1887 | index \e i with a pixel using color index 255 minus \e i. The same |
1888 | is the case for a 1-bit image. Note that the color table is \e not |
1889 | changed. |
1890 | |
1891 | If the image has a premultiplied alpha channel, the image is first |
1892 | converted to an unpremultiplied image format to be inverted and |
1893 | then converted back. |
1894 | |
1895 | \sa {QImage#Image Transformations}{Image Transformations} |
1896 | */ |
1897 | |
1898 | void QImage::invertPixels(InvertMode mode) |
1899 | { |
1900 | if (!d) |
1901 | return; |
1902 | |
1903 | detach(); |
1904 | |
1905 | // In case detach() ran out of memory |
1906 | if (!d) |
1907 | return; |
1908 | |
1909 | QImage::Format originalFormat = d->format; |
1910 | // Inverting premultiplied pixels would produce invalid image data. |
1911 | if (hasAlphaChannel() && qPixelLayouts[d->format].premultiplied) { |
1912 | if (depth() > 32) { |
1913 | if (!d->convertInPlace(QImage::Format_RGBA64, 0)) |
1914 | *this = convertToFormat(QImage::Format_RGBA64); |
1915 | } else { |
1916 | if (!d->convertInPlace(QImage::Format_ARGB32, 0)) |
1917 | *this = convertToFormat(QImage::Format_ARGB32); |
1918 | } |
1919 | } |
1920 | |
1921 | if (depth() < 32) { |
1922 | // This assumes no alpha-channel as the only formats with non-premultipled alpha are 32bit. |
1923 | int bpl = (d->width * d->depth + 7) / 8; |
1924 | int pad = d->bytes_per_line - bpl; |
1925 | uchar *sl = d->data; |
1926 | for (int y=0; y<d->height; ++y) { |
1927 | for (int x=0; x<bpl; ++x) |
1928 | *sl++ ^= 0xff; |
1929 | sl += pad; |
1930 | } |
1931 | } |
1932 | else if (depth() == 64) { |
1933 | quint16 *p = (quint16*)d->data; |
1934 | quint16 *end = (quint16*)(d->data + d->nbytes); |
1935 | quint16 xorbits = 0xffff; |
1936 | while (p < end) { |
1937 | *p++ ^= xorbits; |
1938 | *p++ ^= xorbits; |
1939 | *p++ ^= xorbits; |
1940 | if (mode == InvertRgba) |
1941 | *p++ ^= xorbits; |
1942 | else |
1943 | p++; |
1944 | } |
1945 | } else { |
1946 | quint32 *p = (quint32*)d->data; |
1947 | quint32 *end = (quint32*)(d->data + d->nbytes); |
1948 | quint32 xorbits = 0xffffffff; |
1949 | switch (d->format) { |
1950 | case QImage::Format_RGBA8888: |
1951 | if (mode == InvertRgba) |
1952 | break; |
1953 | Q_FALLTHROUGH(); |
1954 | case QImage::Format_RGBX8888: |
1955 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
1956 | xorbits = 0xffffff00; |
1957 | break; |
1958 | #else |
1959 | xorbits = 0x00ffffff; |
1960 | break; |
1961 | #endif |
1962 | case QImage::Format_ARGB32: |
1963 | if (mode == InvertRgba) |
1964 | break; |
1965 | Q_FALLTHROUGH(); |
1966 | case QImage::Format_RGB32: |
1967 | xorbits = 0x00ffffff; |
1968 | break; |
1969 | case QImage::Format_BGR30: |
1970 | case QImage::Format_RGB30: |
1971 | xorbits = 0x3fffffff; |
1972 | break; |
1973 | default: |
1974 | Q_UNREACHABLE(); |
1975 | xorbits = 0; |
1976 | break; |
1977 | } |
1978 | while (p < end) |
1979 | *p++ ^= xorbits; |
1980 | } |
1981 | |
1982 | if (originalFormat != d->format) { |
1983 | if (!d->convertInPlace(originalFormat, 0)) |
1984 | *this = convertToFormat(originalFormat); |
1985 | } |
1986 | } |
1987 | |
1988 | // Windows defines these |
1989 | #if defined(write) |
1990 | # undef write |
1991 | #endif |
1992 | #if defined(close) |
1993 | # undef close |
1994 | #endif |
1995 | #if defined(read) |
1996 | # undef read |
1997 | #endif |
1998 | |
1999 | /*! |
2000 | \since 4.6 |
2001 | Resizes the color table to contain \a colorCount entries. |
2002 | |
2003 | If the color table is expanded, all the extra colors will be set to |
2004 | transparent (i.e qRgba(0, 0, 0, 0)). |
2005 | |
2006 | When the image is used, the color table must be large enough to |
2007 | have entries for all the pixel/index values present in the image, |
2008 | otherwise the results are undefined. |
2009 | |
2010 | \sa colorCount(), colorTable(), setColor(), {QImage#Image |
2011 | Transformations}{Image Transformations} |
2012 | */ |
2013 | |
2014 | void QImage::setColorCount(int colorCount) |
2015 | { |
2016 | if (!d) { |
2017 | qWarning("QImage::setColorCount: null image" ); |
2018 | return; |
2019 | } |
2020 | |
2021 | detach(); |
2022 | |
2023 | // In case detach() ran out of memory |
2024 | if (!d) |
2025 | return; |
2026 | |
2027 | if (colorCount == d->colortable.size()) |
2028 | return; |
2029 | if (colorCount <= 0) { // use no color table |
2030 | d->colortable = QVector<QRgb>(); |
2031 | return; |
2032 | } |
2033 | int nc = d->colortable.size(); |
2034 | d->colortable.resize(colorCount); |
2035 | for (int i = nc; i < colorCount; ++i) |
2036 | d->colortable[i] = 0; |
2037 | } |
2038 | |
2039 | /*! |
2040 | Returns the format of the image. |
2041 | |
2042 | \sa {QImage#Image Formats}{Image Formats} |
2043 | */ |
2044 | QImage::Format QImage::format() const |
2045 | { |
2046 | return d ? d->format : Format_Invalid; |
2047 | } |
2048 | |
2049 | /*! |
2050 | \fn QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) const & |
2051 | \fn QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) && |
2052 | |
2053 | Returns a copy of the image in the given \a format. |
2054 | |
2055 | The specified image conversion \a flags control how the image data |
2056 | is handled during the conversion process. |
2057 | |
2058 | \sa {Image Formats} |
2059 | */ |
2060 | |
2061 | static bool highColorPrecision(QImage::Format format) |
2062 | { |
2063 | // Formats with higher color precision than ARGB32_Premultiplied. |
2064 | switch (format) { |
2065 | case QImage::Format_ARGB32: |
2066 | case QImage::Format_RGBA8888: |
2067 | case QImage::Format_BGR30: |
2068 | case QImage::Format_RGB30: |
2069 | case QImage::Format_A2BGR30_Premultiplied: |
2070 | case QImage::Format_A2RGB30_Premultiplied: |
2071 | case QImage::Format_RGBX64: |
2072 | case QImage::Format_RGBA64: |
2073 | case QImage::Format_RGBA64_Premultiplied: |
2074 | case QImage::Format_Grayscale16: |
2075 | return true; |
2076 | default: |
2077 | break; |
2078 | } |
2079 | return false; |
2080 | } |
2081 | |
2082 | /*! |
2083 | \internal |
2084 | */ |
2085 | QImage QImage::convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const |
2086 | { |
2087 | if (!d || d->format == format) |
2088 | return *this; |
2089 | |
2090 | if (format == Format_Invalid || d->format == Format_Invalid) |
2091 | return QImage(); |
2092 | |
2093 | Image_Converter converter = qimage_converter_map[d->format][format]; |
2094 | if (!converter && format > QImage::Format_Indexed8 && d->format > QImage::Format_Indexed8) { |
2095 | if (highColorPrecision(format) && highColorPrecision(d->format)) { |
2096 | converter = convert_generic_to_rgb64; |
2097 | } else |
2098 | converter = convert_generic; |
2099 | } |
2100 | if (converter) { |
2101 | QImage image(d->width, d->height, format); |
2102 | |
2103 | QIMAGE_SANITYCHECK_MEMORY(image); |
2104 | |
2105 | image.d->offset = offset(); |
2106 | copyMetadata(image.d, d); |
2107 | |
2108 | converter(image.d, d, flags); |
2109 | return image; |
2110 | } |
2111 | |
2112 | // Convert indexed formats over ARGB32 or RGB32 to the final format. |
2113 | Q_ASSERT(format != QImage::Format_ARGB32 && format != QImage::Format_RGB32); |
2114 | Q_ASSERT(d->format != QImage::Format_ARGB32 && d->format != QImage::Format_RGB32); |
2115 | |
2116 | if (!hasAlphaChannel()) |
2117 | return convertToFormat(Format_RGB32, flags).convertToFormat(format, flags); |
2118 | |
2119 | return convertToFormat(Format_ARGB32, flags).convertToFormat(format, flags); |
2120 | } |
2121 | |
2122 | /*! |
2123 | \internal |
2124 | */ |
2125 | bool QImage::convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags) |
2126 | { |
2127 | return d && d->convertInPlace(format, flags); |
2128 | } |
2129 | |
2130 | static inline int pixel_distance(QRgb p1, QRgb p2) { |
2131 | int r1 = qRed(p1); |
2132 | int g1 = qGreen(p1); |
2133 | int b1 = qBlue(p1); |
2134 | int a1 = qAlpha(p1); |
2135 | |
2136 | int r2 = qRed(p2); |
2137 | int g2 = qGreen(p2); |
2138 | int b2 = qBlue(p2); |
2139 | int a2 = qAlpha(p2); |
2140 | |
2141 | return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2) + abs(a1 - a2); |
2142 | } |
2143 | |
2144 | static inline int closestMatch(QRgb pixel, const QVector<QRgb> &clut) { |
2145 | int idx = 0; |
2146 | int current_distance = INT_MAX; |
2147 | for (int i=0; i<clut.size(); ++i) { |
2148 | int dist = pixel_distance(pixel, clut.at(i)); |
2149 | if (dist < current_distance) { |
2150 | current_distance = dist; |
2151 | idx = i; |
2152 | } |
2153 | } |
2154 | return idx; |
2155 | } |
2156 | |
2157 | static QImage convertWithPalette(const QImage &src, QImage::Format format, |
2158 | const QVector<QRgb> &clut) { |
2159 | QImage dest(src.size(), format); |
2160 | dest.setColorTable(clut); |
2161 | |
2162 | QString textsKeys = src.text(); |
2163 | const auto textKeyList = textsKeys.splitRef(QLatin1Char('\n'), QString::SkipEmptyParts); |
2164 | for (const auto &textKey : textKeyList) { |
2165 | const auto textKeySplitted = textKey.split(QLatin1String(": " )); |
2166 | dest.setText(textKeySplitted[0].toString(), textKeySplitted[1].toString()); |
2167 | } |
2168 | |
2169 | int h = src.height(); |
2170 | int w = src.width(); |
2171 | |
2172 | QHash<QRgb, int> cache; |
2173 | |
2174 | if (format == QImage::Format_Indexed8) { |
2175 | for (int y=0; y<h; ++y) { |
2176 | const QRgb *src_pixels = (const QRgb *) src.scanLine(y); |
2177 | uchar *dest_pixels = (uchar *) dest.scanLine(y); |
2178 | for (int x=0; x<w; ++x) { |
2179 | int src_pixel = src_pixels[x]; |
2180 | int value = cache.value(src_pixel, -1); |
2181 | if (value == -1) { |
2182 | value = closestMatch(src_pixel, clut); |
2183 | cache.insert(src_pixel, value); |
2184 | } |
2185 | dest_pixels[x] = (uchar) value; |
2186 | } |
2187 | } |
2188 | } else { |
2189 | QVector<QRgb> table = clut; |
2190 | table.resize(2); |
2191 | for (int y=0; y<h; ++y) { |
2192 | const QRgb *src_pixels = (const QRgb *) src.scanLine(y); |
2193 | for (int x=0; x<w; ++x) { |
2194 | int src_pixel = src_pixels[x]; |
2195 | int value = cache.value(src_pixel, -1); |
2196 | if (value == -1) { |
2197 | value = closestMatch(src_pixel, table); |
2198 | cache.insert(src_pixel, value); |
2199 | } |
2200 | dest.setPixel(x, y, value); |
2201 | } |
2202 | } |
2203 | } |
2204 | |
2205 | return dest; |
2206 | } |
2207 | |
2208 | /*! |
2209 | \overload |
2210 | |
2211 | Returns a copy of the image converted to the given \a format, |
2212 | using the specified \a colorTable. |
2213 | |
2214 | Conversion from RGB formats to indexed formats is a slow operation |
2215 | and will use a straightforward nearest color approach, with no |
2216 | dithering. |
2217 | */ |
2218 | QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const |
2219 | { |
2220 | if (!d || d->format == format) |
2221 | return *this; |
2222 | |
2223 | if (format == QImage::Format_Invalid) |
2224 | return QImage(); |
2225 | if (format <= QImage::Format_Indexed8) |
2226 | return convertWithPalette(convertToFormat(QImage::Format_ARGB32, flags), format, colorTable); |
2227 | |
2228 | return convertToFormat(format, flags); |
2229 | } |
2230 | |
2231 | /*! |
2232 | \since 5.9 |
2233 | |
2234 | Changes the format of the image to \a format without changing the |
2235 | data. Only works between formats of the same depth. |
2236 | |
2237 | Returns \c true if successful. |
2238 | |
2239 | This function can be used to change images with alpha-channels to |
2240 | their corresponding opaque formats if the data is known to be opaque-only, |
2241 | or to change the format of a given image buffer before overwriting |
2242 | it with new data. |
2243 | |
2244 | \warning The function does not check if the image data is valid in the |
2245 | new format and will still return \c true if the depths are compatible. |
2246 | Operations on an image with invalid data are undefined. |
2247 | |
2248 | \warning If the image is not detached, this will cause the data to be |
2249 | copied. |
2250 | |
2251 | \sa hasAlphaChannel(), convertToFormat() |
2252 | */ |
2253 | |
2254 | bool QImage::reinterpretAsFormat(Format format) |
2255 | { |
2256 | if (!d) |
2257 | return false; |
2258 | if (d->format == format) |
2259 | return true; |
2260 | if (qt_depthForFormat(format) != qt_depthForFormat(d->format)) |
2261 | return false; |
2262 | if (!isDetached()) { // Detach only if shared, not for read-only data. |
2263 | QImageData *oldD = d; |
2264 | detach(); |
2265 | // In case detach() ran out of memory |
2266 | if (!d) { |
2267 | d = oldD; |
2268 | return false; |
2269 | } |
2270 | } |
2271 | |
2272 | d->format = format; |
2273 | return true; |
2274 | } |
2275 | |
2276 | /*! |
2277 | \since 5.13 |
2278 | |
2279 | Detach and convert the image to the given \a format in place. |
2280 | |
2281 | The specified image conversion \a flags control how the image data |
2282 | is handled during the conversion process. |
2283 | |
2284 | \sa convertToFormat() |
2285 | */ |
2286 | |
2287 | void QImage::convertTo(Format format, Qt::ImageConversionFlags flags) |
2288 | { |
2289 | if (!d || format == QImage::Format_Invalid) |
2290 | return; |
2291 | |
2292 | detach(); |
2293 | if (convertToFormat_inplace(format, flags)) |
2294 | return; |
2295 | |
2296 | *this = convertToFormat_helper(format, flags); |
2297 | } |
2298 | |
2299 | /*! |
2300 | \fn bool QImage::valid(const QPoint &pos) const |
2301 | |
2302 | Returns \c true if \a pos is a valid coordinate pair within the |
2303 | image; otherwise returns \c false. |
2304 | |
2305 | \sa rect(), QRect::contains() |
2306 | */ |
2307 | |
2308 | /*! |
2309 | \overload |
2310 | |
2311 | Returns \c true if QPoint(\a x, \a y) is a valid coordinate pair |
2312 | within the image; otherwise returns \c false. |
2313 | */ |
2314 | bool QImage::valid(int x, int y) const |
2315 | { |
2316 | return d |
2317 | && x >= 0 && x < d->width |
2318 | && y >= 0 && y < d->height; |
2319 | } |
2320 | |
2321 | /*! |
2322 | \fn int QImage::pixelIndex(const QPoint &position) const |
2323 | |
2324 | Returns the pixel index at the given \a position. |
2325 | |
2326 | If \a position is not valid, or if the image is not a paletted |
2327 | image (depth() > 8), the results are undefined. |
2328 | |
2329 | \sa valid(), depth(), {QImage#Pixel Manipulation}{Pixel Manipulation} |
2330 | */ |
2331 | |
2332 | /*! |
2333 | \overload |
2334 | |
2335 | Returns the pixel index at (\a x, \a y). |
2336 | */ |
2337 | int QImage::pixelIndex(int x, int y) const |
2338 | { |
2339 | if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) { |
2340 | qWarning("QImage::pixelIndex: coordinate (%d,%d) out of range" , x, y); |
2341 | return -12345; |
2342 | } |
2343 | const uchar * s = scanLine(y); |
2344 | switch(d->format) { |
2345 | case Format_Mono: |
2346 | return (*(s + (x >> 3)) >> (7- (x & 7))) & 1; |
2347 | case Format_MonoLSB: |
2348 | return (*(s + (x >> 3)) >> (x & 7)) & 1; |
2349 | case Format_Indexed8: |
2350 | return (int)s[x]; |
2351 | default: |
2352 | qWarning("QImage::pixelIndex: Not applicable for %d-bpp images (no palette)" , d->depth); |
2353 | } |
2354 | return 0; |
2355 | } |
2356 | |
2357 | |
2358 | /*! |
2359 | \fn QRgb QImage::pixel(const QPoint &position) const |
2360 | |
2361 | Returns the color of the pixel at the given \a position. |
2362 | |
2363 | If the \a position is not valid, the results are undefined. |
2364 | |
2365 | \warning This function is expensive when used for massive pixel |
2366 | manipulations. Use constBits() or constScanLine() when many |
2367 | pixels needs to be read. |
2368 | |
2369 | \sa setPixel(), valid(), constBits(), constScanLine(), {QImage#Pixel Manipulation}{Pixel |
2370 | Manipulation} |
2371 | */ |
2372 | |
2373 | /*! |
2374 | \overload |
2375 | |
2376 | Returns the color of the pixel at coordinates (\a x, \a y). |
2377 | */ |
2378 | QRgb QImage::pixel(int x, int y) const |
2379 | { |
2380 | if (!d || x < 0 || x >= d->width || y < 0 || y >= d->height) { |
2381 | qWarning("QImage::pixel: coordinate (%d,%d) out of range" , x, y); |
2382 | return 12345; |
2383 | } |
2384 | |
2385 | const uchar *s = d->data + y * d->bytes_per_line; |
2386 | |
2387 | int index = -1; |
2388 | switch (d->format) { |
2389 | case Format_Mono: |
2390 | index = (*(s + (x >> 3)) >> (~x & 7)) & 1; |
2391 | break; |
2392 | case Format_MonoLSB: |
2393 | index = (*(s + (x >> 3)) >> (x & 7)) & 1; |
2394 | break; |
2395 | case Format_Indexed8: |
2396 | index = s[x]; |
2397 | break; |
2398 | default: |
2399 | break; |
2400 | } |
2401 | if (index >= 0) { // Indexed format |
2402 | if (index >= d->colortable.size()) { |
2403 | qWarning("QImage::pixel: color table index %d out of range." , index); |
2404 | return 0; |
2405 | } |
2406 | return d->colortable.at(index); |
2407 | } |
2408 | |
2409 | switch (d->format) { |
2410 | case Format_RGB32: |
2411 | return 0xff000000 | reinterpret_cast<const QRgb *>(s)[x]; |
2412 | case Format_ARGB32: // Keep old behaviour. |
2413 | case Format_ARGB32_Premultiplied: |
2414 | return reinterpret_cast<const QRgb *>(s)[x]; |
2415 | case Format_RGBX8888: |
2416 | case Format_RGBA8888: // Match ARGB32 behavior. |
2417 | case Format_RGBA8888_Premultiplied: |
2418 | return RGBA2ARGB(reinterpret_cast<const quint32 *>(s)[x]); |
2419 | case Format_BGR30: |
2420 | case Format_A2BGR30_Premultiplied: |
2421 | return qConvertA2rgb30ToArgb32<PixelOrderBGR>(reinterpret_cast<const quint32 *>(s)[x]); |
2422 | case Format_RGB30: |
2423 | case Format_A2RGB30_Premultiplied: |
2424 | return qConvertA2rgb30ToArgb32<PixelOrderRGB>(reinterpret_cast<const quint32 *>(s)[x]); |
2425 | case Format_RGB16: |
2426 | return qConvertRgb16To32(reinterpret_cast<const quint16 *>(s)[x]); |
2427 | case Format_RGBX64: |
2428 | case Format_RGBA64: // Match ARGB32 behavior. |
2429 | case Format_RGBA64_Premultiplied: |
2430 | return reinterpret_cast<const QRgba64 *>(s)[x].toArgb32(); |
2431 | default: |
2432 | break; |
2433 | } |
2434 | const QPixelLayout *layout = &qPixelLayouts[d->format]; |
2435 | uint result; |
2436 | return *layout->fetchToARGB32PM(&result, s, x, 1, nullptr, nullptr); |
2437 | } |
2438 | |
2439 | /*! |
2440 | \fn void QImage::setPixel(const QPoint &position, uint index_or_rgb) |
2441 | |
2442 | Sets the pixel index or color at the given \a position to \a |
2443 | index_or_rgb. |
2444 | |
2445 | If the image's format is either monochrome or paletted, the given \a |
2446 | index_or_rgb value must be an index in the image's color table, |
2447 | otherwise the parameter must be a QRgb value. |
2448 | |
2449 | If \a position is not a valid coordinate pair in the image, or if |
2450 | \a index_or_rgb >= colorCount() in the case of monochrome and |
2451 | paletted images, the result is undefined. |
2452 | |
2453 | \warning This function is expensive due to the call of the internal |
2454 | \c{detach()} function called within; if performance is a concern, we |
2455 | recommend the use of scanLine() or bits() to access pixel data directly. |
2456 | |
2457 | \sa pixel(), {QImage#Pixel Manipulation}{Pixel Manipulation} |
2458 | */ |
2459 | |
2460 | /*! |
2461 | \overload |
2462 | |
2463 | Sets the pixel index or color at (\a x, \a y) to \a index_or_rgb. |
2464 | */ |
2465 | void QImage::setPixel(int x, int y, uint index_or_rgb) |
2466 | { |
2467 | if (!d || x < 0 || x >= width() || y < 0 || y >= height()) { |
2468 | qWarning("QImage::setPixel: coordinate (%d,%d) out of range" , x, y); |
2469 | return; |
2470 | } |
2471 | // detach is called from within scanLine |
2472 | uchar * s = scanLine(y); |
2473 | switch(d->format) { |
2474 | case Format_Mono: |
2475 | case Format_MonoLSB: |
2476 | if (index_or_rgb > 1) { |
2477 | qWarning("QImage::setPixel: Index %d out of range" , index_or_rgb); |
2478 | } else if (format() == Format_MonoLSB) { |
2479 | if (index_or_rgb==0) |
2480 | *(s + (x >> 3)) &= ~(1 << (x & 7)); |
2481 | else |
2482 | *(s + (x >> 3)) |= (1 << (x & 7)); |
2483 | } else { |
2484 | if (index_or_rgb==0) |
2485 | *(s + (x >> 3)) &= ~(1 << (7-(x & 7))); |
2486 | else |
2487 | *(s + (x >> 3)) |= (1 << (7-(x & 7))); |
2488 | } |
2489 | return; |
2490 | case Format_Indexed8: |
2491 | if (index_or_rgb >= (uint)d->colortable.size()) { |
2492 | qWarning("QImage::setPixel: Index %d out of range" , index_or_rgb); |
2493 | return; |
2494 | } |
2495 | s[x] = index_or_rgb; |
2496 | return; |
2497 | case Format_RGB32: |
2498 | //make sure alpha is 255, we depend on it in qdrawhelper for cases |
2499 | // when image is set as a texture pattern on a qbrush |
2500 | ((uint *)s)[x] = 0xff000000 | index_or_rgb; |
2501 | return; |
2502 | case Format_ARGB32: |
2503 | case Format_ARGB32_Premultiplied: |
2504 | ((uint *)s)[x] = index_or_rgb; |
2505 | return; |
2506 | case Format_RGB16: |
2507 | ((quint16 *)s)[x] = qConvertRgb32To16(qUnpremultiply(index_or_rgb)); |
2508 | return; |
2509 | case Format_RGBX8888: |
2510 | ((uint *)s)[x] = ARGB2RGBA(0xff000000 | index_or_rgb); |
2511 | return; |
2512 | case Format_RGBA8888: |
2513 | case Format_RGBA8888_Premultiplied: |
2514 | ((uint *)s)[x] = ARGB2RGBA(index_or_rgb); |
2515 | return; |
2516 | case Format_BGR30: |
2517 | ((uint *)s)[x] = qConvertRgb32ToRgb30<PixelOrderBGR>(index_or_rgb); |
2518 | return; |
2519 | case Format_A2BGR30_Premultiplied: |
2520 | ((uint *)s)[x] = qConvertArgb32ToA2rgb30<PixelOrderBGR>(index_or_rgb); |
2521 | return; |
2522 | case Format_RGB30: |
2523 | ((uint *)s)[x] = qConvertRgb32ToRgb30<PixelOrderRGB>(index_or_rgb); |
2524 | return; |
2525 | case Format_A2RGB30_Premultiplied: |
2526 | ((uint *)s)[x] = qConvertArgb32ToA2rgb30<PixelOrderRGB>(index_or_rgb); |
2527 | return; |
2528 | case Format_Invalid: |
2529 | case NImageFormats: |
2530 | Q_ASSERT(false); |
2531 | return; |
2532 | default: |
2533 | break; |
2534 | } |
2535 | |
2536 | const QPixelLayout *layout = &qPixelLayouts[d->format]; |
2537 | layout->storeFromARGB32PM(s, &index_or_rgb, x, 1, nullptr, nullptr); |
2538 | } |
2539 | |
2540 | /*! |
2541 | \fn QColor QImage::pixelColor(const QPoint &position) const |
2542 | \since 5.6 |
2543 | |
2544 | Returns the color of the pixel at the given \a position as a QColor. |
2545 | |
2546 | If the \a position is not valid, an invalid QColor is returned. |
2547 | |
2548 | \warning This function is expensive when used for massive pixel |
2549 | manipulations. Use constBits() or constScanLine() when many |
2550 | pixels needs to be read. |
2551 | |
2552 | \sa setPixel(), valid(), constBits(), constScanLine(), {QImage#Pixel Manipulation}{Pixel |
2553 | Manipulation} |
2554 | */ |
2555 | |
2556 | /*! |
2557 | \overload |
2558 | \since 5.6 |
2559 | |
2560 | Returns the color of the pixel at coordinates (\a x, \a y) as a QColor. |
2561 | */ |
2562 | QColor QImage::pixelColor(int x, int y) const |
2563 | { |
2564 | if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) { |
2565 | qWarning("QImage::pixelColor: coordinate (%d,%d) out of range" , x, y); |
2566 | return QColor(); |
2567 | } |
2568 | |
2569 | QRgba64 c; |
2570 | const uchar * s = constScanLine(y); |
2571 | switch (d->format) { |
2572 | case Format_BGR30: |
2573 | case Format_A2BGR30_Premultiplied: |
2574 | c = qConvertA2rgb30ToRgb64<PixelOrderBGR>(reinterpret_cast<const quint32 *>(s)[x]); |
2575 | break; |
2576 | case Format_RGB30: |
2577 | case Format_A2RGB30_Premultiplied: |
2578 | c = qConvertA2rgb30ToRgb64<PixelOrderRGB>(reinterpret_cast<const quint32 *>(s)[x]); |
2579 | break; |
2580 | case Format_RGBX64: |
2581 | case Format_RGBA64: |
2582 | case Format_RGBA64_Premultiplied: |
2583 | c = reinterpret_cast<const QRgba64 *>(s)[x]; |
2584 | break; |
2585 | case Format_Grayscale16: { |
2586 | quint16 v = reinterpret_cast<const quint16 *>(s)[x]; |
2587 | return QColor(qRgba64(v, v, v, 0xffff)); |
2588 | } |
2589 | default: |
2590 | c = QRgba64::fromArgb32(pixel(x, y)); |
2591 | break; |
2592 | } |
2593 | // QColor is always unpremultiplied |
2594 | if (hasAlphaChannel() && qPixelLayouts[d->format].premultiplied) |
2595 | c = c.unpremultiplied(); |
2596 | return QColor(c); |
2597 | } |
2598 | |
2599 | /*! |
2600 | \fn void QImage::setPixelColor(const QPoint &position, const QColor &color) |
2601 | \since 5.6 |
2602 | |
2603 | Sets the color at the given \a position to \a color. |
2604 | |
2605 | If \a position is not a valid coordinate pair in the image, or |
2606 | the image's format is either monochrome or paletted, the result is undefined. |
2607 | |
2608 | \warning This function is expensive due to the call of the internal |
2609 | \c{detach()} function called within; if performance is a concern, we |
2610 | recommend the use of scanLine() or bits() to access pixel data directly. |
2611 | |
2612 | \sa pixel(), bits(), scanLine(), {QImage#Pixel Manipulation}{Pixel Manipulation} |
2613 | */ |
2614 | |
2615 | /*! |
2616 | \overload |
2617 | \since 5.6 |
2618 | |
2619 | Sets the pixel color at (\a x, \a y) to \a color. |
2620 | */ |
2621 | void QImage::setPixelColor(int x, int y, const QColor &color) |
2622 | { |
2623 | if (!d || x < 0 || x >= width() || y < 0 || y >= height()) { |
2624 | qWarning("QImage::setPixelColor: coordinate (%d,%d) out of range" , x, y); |
2625 | return; |
2626 | } |
2627 | |
2628 | if (!color.isValid()) { |
2629 | qWarning("QImage::setPixelColor: color is invalid" ); |
2630 | return; |
2631 | } |
2632 | |
2633 | // QColor is always unpremultiplied |
2634 | QRgba64 c = color.rgba64(); |
2635 | if (!hasAlphaChannel()) |
2636 | c.setAlpha(65535); |
2637 | else if (qPixelLayouts[d->format].premultiplied) |
2638 | c = c.premultiplied(); |
2639 | // detach is called from within scanLine |
2640 | uchar * s = scanLine(y); |
2641 | switch (d->format) { |
2642 | case Format_Mono: |
2643 | case Format_MonoLSB: |
2644 | case Format_Indexed8: |
2645 | qWarning("QImage::setPixelColor: called on monochrome or indexed format" ); |
2646 | return; |
2647 | case Format_BGR30: |
2648 | ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderBGR>(c) | 0xc0000000; |
2649 | return; |
2650 | case Format_A2BGR30_Premultiplied: |
2651 | ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderBGR>(c); |
2652 | return; |
2653 | case Format_RGB30: |
2654 | ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderRGB>(c) | 0xc0000000; |
2655 | return; |
2656 | case Format_A2RGB30_Premultiplied: |
2657 | ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderRGB>(c); |
2658 | return; |
2659 | case Format_RGBX64: |
2660 | ((QRgba64 *)s)[x] = color.rgba64(); |
2661 | ((QRgba64 *)s)[x].setAlpha(65535); |
2662 | return; |
2663 | case Format_RGBA64: |
2664 | case Format_RGBA64_Premultiplied: |
2665 | ((QRgba64 *)s)[x] = color.rgba64(); |
2666 | return; |
2667 | default: |
2668 | setPixel(x, y, c.toArgb32()); |
2669 | return; |
2670 | } |
2671 | } |
2672 | |
2673 | /*! |
2674 | Returns \c true if all the colors in the image are shades of gray |
2675 | (i.e. their red, green and blue components are equal); otherwise |
2676 | false. |
2677 | |
2678 | Note that this function is slow for images without color table. |
2679 | |
2680 | \sa isGrayscale() |
2681 | */ |
2682 | bool QImage::allGray() const |
2683 | { |
2684 | if (!d) |
2685 | return true; |
2686 | |
2687 | switch (d->format) { |
2688 | case Format_Mono: |
2689 | case Format_MonoLSB: |
2690 | case Format_Indexed8: |
2691 | for (int i = 0; i < d->colortable.size(); ++i) { |
2692 | if (!qIsGray(d->colortable.at(i))) |
2693 | return false; |
2694 | } |
2695 | return true; |
2696 | case Format_Alpha8: |
2697 | return false; |
2698 | case Format_Grayscale8: |
2699 | case Format_Grayscale16: |
2700 | return true; |
2701 | case Format_RGB32: |
2702 | case Format_ARGB32: |
2703 | case Format_ARGB32_Premultiplied: |
2704 | #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
2705 | case Format_RGBX8888: |
2706 | case Format_RGBA8888: |
2707 | case Format_RGBA8888_Premultiplied: |
2708 | #endif |
2709 | for (int j = 0; j < d->height; ++j) { |
2710 | const QRgb *b = (const QRgb *)constScanLine(j); |
2711 | for (int i = 0; i < d->width; ++i) { |
2712 | if (!qIsGray(b[i])) |
2713 | return false; |
2714 | } |
2715 | } |
2716 | return true; |
2717 | case Format_RGB16: |
2718 | for (int j = 0; j < d->height; ++j) { |
2719 | const quint16 *b = (const quint16 *)constScanLine(j); |
2720 | for (int i = 0; i < d->width; ++i) { |
2721 | if (!qIsGray(qConvertRgb16To32(b[i]))) |
2722 | return false; |
2723 | } |
2724 | } |
2725 | return true; |
2726 | default: |
2727 | break; |
2728 | } |
2729 | |
2730 | uint buffer[BufferSize]; |
2731 | const QPixelLayout *layout = &qPixelLayouts[d->format]; |
2732 | const auto fetch = layout->fetchToARGB32PM; |
2733 | for (int j = 0; j < d->height; ++j) { |
2734 | const uchar *b = constScanLine(j); |
2735 | int x = 0; |
2736 | while (x < d->width) { |
2737 | int l = qMin(d->width - x, BufferSize); |
2738 | const uint *ptr = fetch(buffer, b, x, l, nullptr, nullptr); |
2739 | for (int i = 0; i < l; ++i) { |
2740 | if (!qIsGray(ptr[i])) |
2741 | return false; |
2742 | } |
2743 | x += l; |
2744 | } |
2745 | } |
2746 | return true; |
2747 | } |
2748 | |
2749 | /*! |
2750 | For 32-bit images, this function is equivalent to allGray(). |
2751 | |
2752 | For color indexed images, this function returns \c true if |
2753 | color(i) is QRgb(i, i, i) for all indexes of the color table; |
2754 | otherwise returns \c false. |
2755 | |
2756 | \sa allGray(), {QImage#Image Formats}{Image Formats} |
2757 | */ |
2758 | bool QImage::isGrayscale() const |
2759 | { |
2760 | if (!d) |
2761 | return false; |
2762 | |
2763 | if (d->format == QImage::Format_Alpha8) |
2764 | return false; |
2765 | |
2766 | if (d->format == QImage::Format_Grayscale8 || d->format == QImage::Format_Grayscale16) |
2767 | return true; |
2768 | |
2769 | switch (depth()) { |
2770 | case 32: |
2771 | case 24: |
2772 | case 16: |
2773 | return allGray(); |
2774 | case 8: { |
2775 | Q_ASSERT(d->format == QImage::Format_Indexed8); |
2776 | for (int i = 0; i < colorCount(); i++) |
2777 | if (d->colortable.at(i) != qRgb(i,i,i)) |
2778 | return false; |
2779 | return true; |
2780 | } |
2781 | } |
2782 | return false; |
2783 | } |
2784 | |
2785 | /*! |
2786 | \fn QImage QImage::scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, |
2787 | Qt::TransformationMode transformMode) const |
2788 | \overload |
2789 | |
2790 | Returns a copy of the image scaled to a rectangle with the given |
2791 | \a width and \a height according to the given \a aspectRatioMode |
2792 | and \a transformMode. |
2793 | |
2794 | If either the \a width or the \a height is zero or negative, this |
2795 | function returns a null image. |
2796 | */ |
2797 | |
2798 | /*! |
2799 | \fn QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, |
2800 | Qt::TransformationMode transformMode) const |
2801 | |
2802 | Returns a copy of the image scaled to a rectangle defined by the |
2803 | given \a size according to the given \a aspectRatioMode and \a |
2804 | transformMode. |
2805 | |
2806 | \image qimage-scaling.png |
2807 | |
2808 | \list |
2809 | \li If \a aspectRatioMode is Qt::IgnoreAspectRatio, the image |
2810 | is scaled to \a size. |
2811 | \li If \a aspectRatioMode is Qt::KeepAspectRatio, the image is |
2812 | scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio. |
2813 | \li If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding, |
2814 | the image is scaled to a rectangle as small as possible |
2815 | outside \a size, preserving the aspect ratio. |
2816 | \endlist |
2817 | |
2818 | If the given \a size is empty, this function returns a null image. |
2819 | |
2820 | \sa isNull(), {QImage#Image Transformations}{Image |
2821 | Transformations} |
2822 | */ |
2823 | QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const |
2824 | { |
2825 | if (!d) { |
2826 | qWarning("QImage::scaled: Image is a null image" ); |
2827 | return QImage(); |
2828 | } |
2829 | if (s.isEmpty()) |
2830 | return QImage(); |
2831 | |
2832 | QSize newSize = size(); |
2833 | newSize.scale(s, aspectMode); |
2834 | newSize.rwidth() = qMax(newSize.width(), 1); |
2835 | newSize.rheight() = qMax(newSize.height(), 1); |
2836 | if (newSize == size()) |
2837 | return *this; |
2838 | |
2839 | QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(), (qreal)newSize.height() / height()); |
2840 | QImage img = transformed(wm, mode); |
2841 | return img; |
2842 | } |
2843 | |
2844 | /*! |
2845 | \fn QImage QImage::scaledToWidth(int width, Qt::TransformationMode mode) const |
2846 | |
2847 | Returns a scaled copy of the image. The returned image is scaled |
2848 | to the given \a width using the specified transformation \a |
2849 | mode. |
2850 | |
2851 | This function automatically calculates the height of the image so |
2852 | that its aspect ratio is preserved. |
2853 | |
2854 | If the given \a width is 0 or negative, a null image is returned. |
2855 | |
2856 | \sa {QImage#Image Transformations}{Image Transformations} |
2857 | */ |
2858 | QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const |
2859 | { |
2860 | if (!d) { |
2861 | qWarning("QImage::scaleWidth: Image is a null image" ); |
2862 | return QImage(); |
2863 | } |
2864 | if (w <= 0) |
2865 | return QImage(); |
2866 | |
2867 | qreal factor = (qreal) w / width(); |
2868 | QTransform wm = QTransform::fromScale(factor, factor); |
2869 | return transformed(wm, mode); |
2870 | } |
2871 | |
2872 | /*! |
2873 | \fn QImage QImage::scaledToHeight(int height, Qt::TransformationMode mode) const |
2874 | |
2875 | Returns a scaled copy of the image. The returned image is scaled |
2876 | to the given \a height using the specified transformation \a |
2877 | mode. |
2878 | |
2879 | This function automatically calculates the width of the image so that |
2880 | the ratio of the image is preserved. |
2881 | |
2882 | If the given \a height is 0 or negative, a null image is returned. |
2883 | |
2884 | \sa {QImage#Image Transformations}{Image Transformations} |
2885 | */ |
2886 | QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const |
2887 | { |
2888 | if (!d) { |
2889 | qWarning("QImage::scaleHeight: Image is a null image" ); |
2890 | return QImage(); |
2891 | } |
2892 | if (h <= 0) |
2893 | return QImage(); |
2894 | |
2895 | qreal factor = (qreal) h / height(); |
2896 | QTransform wm = QTransform::fromScale(factor, factor); |
2897 | return transformed(wm, mode); |
2898 | } |
2899 | |
2900 | |
2901 | /*! |
2902 | \fn QMatrix QImage::trueMatrix(const QMatrix &matrix, int width, int height) |
2903 | |
2904 | Returns the actual matrix used for transforming an image with the |
2905 | given \a width, \a height and \a matrix. |
2906 | |
2907 | When transforming an image using the transformed() function, the |
2908 | transformation matrix is internally adjusted to compensate for |
2909 | unwanted translation, i.e. transformed() returns the smallest |
2910 | image containing all transformed points of the original image. |
2911 | This function returns the modified matrix, which maps points |
2912 | correctly from the original image into the new image. |
2913 | |
2914 | \sa transformed(), {QImage#Image Transformations}{Image |
2915 | Transformations} |
2916 | */ |
2917 | QMatrix QImage::trueMatrix(const QMatrix &matrix, int w, int h) |
2918 | { |
2919 | return trueMatrix(QTransform(matrix), w, h).toAffine(); |
2920 | } |
2921 | |
2922 | /*! |
2923 | Returns a copy of the image that is transformed using the given |
2924 | transformation \a matrix and transformation \a mode. |
2925 | |
2926 | The returned image will normally have the same {Image Formats}{format} as |
2927 | the original image. However, a complex transformation may result in an |
2928 | image where not all pixels are covered by the transformed pixels of the |
2929 | original image. In such cases, those background pixels will be assigned a |
2930 | transparent color value, and the transformed image will be given a format |
2931 | with an alpha channel, even if the orginal image did not have that. |
2932 | |
2933 | The transformation \a matrix is internally adjusted to compensate |
2934 | for unwanted translation; i.e. the image produced is the smallest |
2935 | image that contains all the transformed points of the original |
2936 | image. Use the trueMatrix() function to retrieve the actual matrix |
2937 | used for transforming an image. |
2938 | |
2939 | \sa trueMatrix(), {QImage#Image Transformations}{Image |
2940 | Transformations} |
2941 | */ |
2942 | QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const |
2943 | { |
2944 | return transformed(QTransform(matrix), mode); |
2945 | } |
2946 | |
2947 | /*! |
2948 | Builds and returns a 1-bpp mask from the alpha buffer in this |
2949 | image. Returns a null image if the image's format is |
2950 | QImage::Format_RGB32. |
2951 | |
2952 | The \a flags argument is a bitwise-OR of the |
2953 | Qt::ImageConversionFlags, and controls the conversion |
2954 | process. Passing 0 for flags sets all the default options. |
2955 | |
2956 | The returned image has little-endian bit order (i.e. the image's |
2957 | format is QImage::Format_MonoLSB), which you can convert to |
2958 | big-endian (QImage::Format_Mono) using the convertToFormat() |
2959 | function. |
2960 | |
2961 | \sa createHeuristicMask(), {QImage#Image Transformations}{Image |
2962 | Transformations} |
2963 | */ |
2964 | QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const |
2965 | { |
2966 | if (!d || d->format == QImage::Format_RGB32) |
2967 | return QImage(); |
2968 | |
2969 | if (d->depth == 1) { |
2970 | // A monochrome pixmap, with alpha channels on those two colors. |
2971 | // Pretty unlikely, so use less efficient solution. |
2972 | return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags); |
2973 | } |
2974 | |
2975 | QImage mask(d->width, d->height, Format_MonoLSB); |
2976 | if (!mask.isNull()) { |
2977 | dither_to_Mono(mask.d, d, flags, true); |
2978 | copyPhysicalMetadata(mask.d, d); |
2979 | } |
2980 | return mask; |
2981 | } |
2982 | |
2983 | #ifndef QT_NO_IMAGE_HEURISTIC_MASK |
2984 | /*! |
2985 | Creates and returns a 1-bpp heuristic mask for this image. |
2986 | |
2987 | The function works by selecting a color from one of the corners, |
2988 | then chipping away pixels of that color starting at all the edges. |
2989 | The four corners vote for which color is to be masked away. In |
2990 | case of a draw (this generally means that this function is not |
2991 | applicable to the image), the result is arbitrary. |
2992 | |
2993 | The returned image has little-endian bit order (i.e. the image's |
2994 | format is QImage::Format_MonoLSB), which you can convert to |
2995 | big-endian (QImage::Format_Mono) using the convertToFormat() |
2996 | function. |
2997 | |
2998 | If \a clipTight is true (the default) the mask is just large |
2999 | enough to cover the pixels; otherwise, the mask is larger than the |
3000 | data pixels. |
3001 | |
3002 | Note that this function disregards the alpha buffer. |
3003 | |
3004 | \sa createAlphaMask(), {QImage#Image Transformations}{Image |
3005 | Transformations} |
3006 | */ |
3007 | |
3008 | QImage QImage::createHeuristicMask(bool clipTight) const |
3009 | { |
3010 | if (!d) |
3011 | return QImage(); |
3012 | |
3013 | if (d->depth != 32) { |
3014 | QImage img32 = convertToFormat(Format_RGB32); |
3015 | return img32.createHeuristicMask(clipTight); |
3016 | } |
3017 | |
3018 | #define PIX(x,y) (*((const QRgb*)scanLine(y)+x) & 0x00ffffff) |
3019 | |
3020 | int w = width(); |
3021 | int h = height(); |
3022 | QImage m(w, h, Format_MonoLSB); |
3023 | QIMAGE_SANITYCHECK_MEMORY(m); |
3024 | m.setColorCount(2); |
3025 | m.setColor(0, QColor(Qt::color0).rgba()); |
3026 | m.setColor(1, QColor(Qt::color1).rgba()); |
3027 | m.fill(0xff); |
3028 | |
3029 | QRgb background = PIX(0,0); |
3030 | if (background != PIX(w-1,0) && |
3031 | background != PIX(0,h-1) && |
3032 | background != PIX(w-1,h-1)) { |
3033 | background = PIX(w-1,0); |
3034 | if (background != PIX(w-1,h-1) && |
3035 | background != PIX(0,h-1) && |
3036 | PIX(0,h-1) == PIX(w-1,h-1)) { |
3037 | background = PIX(w-1,h-1); |
3038 | } |
3039 | } |
3040 | |
3041 | int x,y; |
3042 | bool done = false; |
3043 | uchar *ypp, *ypc, *ypn; |
3044 | while(!done) { |
3045 | done = true; |
3046 | ypn = m.scanLine(0); |
3047 | ypc = 0; |
3048 | for (y = 0; y < h; y++) { |
3049 | ypp = ypc; |
3050 | ypc = ypn; |
3051 | ypn = (y == h-1) ? 0 : m.scanLine(y+1); |
3052 | const QRgb *p = (const QRgb *)scanLine(y); |
3053 | for (x = 0; x < w; x++) { |
3054 | // slowness here - it's possible to do six of these tests |
3055 | // together in one go. oh well. |
3056 | if ((x == 0 || y == 0 || x == w-1 || y == h-1 || |
3057 | !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) || |
3058 | !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) || |
3059 | !(*(ypp + (x >> 3)) & (1 << (x & 7))) || |
3060 | !(*(ypn + (x >> 3)) & (1 << (x & 7)))) && |
3061 | ( (*(ypc + (x >> 3)) & (1 << (x & 7)))) && |
3062 | ((*p & 0x00ffffff) == background)) { |
3063 | done = false; |
3064 | *(ypc + (x >> 3)) &= ~(1 << (x & 7)); |
3065 | } |
3066 | p++; |
3067 | } |
3068 | } |
3069 | } |
3070 | |
3071 | if (!clipTight) { |
3072 | ypn = m.scanLine(0); |
3073 | ypc = 0; |
3074 | for (y = 0; y < h; y++) { |
3075 | ypp = ypc; |
3076 | ypc = ypn; |
3077 | ypn = (y == h-1) ? 0 : m.scanLine(y+1); |
3078 | const QRgb *p = (const QRgb *)scanLine(y); |
3079 | for (x = 0; x < w; x++) { |
3080 | if ((*p & 0x00ffffff) != background) { |
3081 | if (x > 0) |
3082 | *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7)); |
3083 | if (x < w-1) |
3084 | *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7)); |
3085 | if (y > 0) |
3086 | *(ypp + (x >> 3)) |= (1 << (x & 7)); |
3087 | if (y < h-1) |
3088 | *(ypn + (x >> 3)) |= (1 << (x & 7)); |
3089 | } |
3090 | p++; |
3091 | } |
3092 | } |
3093 | } |
3094 | |
3095 | #undef PIX |
3096 | |
3097 | copyPhysicalMetadata(m.d, d); |
3098 | return m; |
3099 | } |
3100 | #endif //QT_NO_IMAGE_HEURISTIC_MASK |
3101 | |
3102 | /*! |
3103 | Creates and returns a mask for this image based on the given \a |
3104 | color value. If the \a mode is MaskInColor (the default value), |
3105 | all pixels matching \a color will be opaque pixels in the mask. If |
3106 | \a mode is MaskOutColor, all pixels matching the given color will |
3107 | be transparent. |
3108 | |
3109 | \sa createAlphaMask(), createHeuristicMask() |
3110 | */ |
3111 | |
3112 | QImage QImage::createMaskFromColor(QRgb color, Qt::MaskMode mode) const |
3113 | { |
3114 | if (!d) |
3115 | return QImage(); |
3116 | QImage maskImage(size(), QImage::Format_MonoLSB); |
3117 | QIMAGE_SANITYCHECK_MEMORY(maskImage); |
3118 | maskImage.fill(0); |
3119 | uchar *s = maskImage.bits(); |
3120 | |
3121 | if (depth() == 32) { |
3122 | for (int h = 0; h < d->height; h++) { |
3123 | const uint *sl = (const uint *) scanLine(h); |
3124 | for (int w = 0; w < d->width; w++) { |
3125 | if (sl[w] == color) |
3126 | *(s + (w >> 3)) |= (1 << (w & 7)); |
3127 | } |
3128 | s += maskImage.bytesPerLine(); |
3129 | } |
3130 | } else { |
3131 | for (int h = 0; h < d->height; h++) { |
3132 | for (int w = 0; w < d->width; w++) { |
3133 | if ((uint) pixel(w, h) == color) |
3134 | *(s + (w >> 3)) |= (1 << (w & 7)); |
3135 | } |
3136 | s += maskImage.bytesPerLine(); |
3137 | } |
3138 | } |
3139 | if (mode == Qt::MaskOutColor) |
3140 | maskImage.invertPixels(); |
3141 | |
3142 | copyPhysicalMetadata(maskImage.d, d); |
3143 | return maskImage; |
3144 | } |
3145 | |
3146 | /*! |
3147 | \fn QImage QImage::mirrored(bool horizontal = false, bool vertical = true) const & |
3148 | \fn QImage QImage::mirrored(bool horizontal = false, bool vertical = true) && |
3149 | |
3150 | Returns a mirror of the image, mirrored in the horizontal and/or |
3151 | the vertical direction depending on whether \a horizontal and \a |
3152 | vertical are set to true or false. |
3153 | |
3154 | Note that the original image is not changed. |
3155 | |
3156 | \sa {QImage#Image Transformations}{Image Transformations} |
3157 | */ |
3158 | |
3159 | template<class T> inline void do_mirror_data(QImageData *dst, QImageData *src, |
3160 | int dstX0, int dstY0, |
3161 | int dstXIncr, int dstYIncr, |
3162 | int w, int h) |
3163 | { |
3164 | if (dst == src) { |
3165 | // When mirroring in-place, stop in the middle for one of the directions, since we |
3166 | // are swapping the bytes instead of merely copying. |
3167 | const int srcXEnd = (dstX0 && !dstY0) ? w / 2 : w; |
3168 | const int srcYEnd = dstY0 ? h / 2 : h; |
3169 | for (int srcY = 0, dstY = dstY0; srcY < srcYEnd; ++srcY, dstY += dstYIncr) { |
3170 | T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line); |
3171 | T *dstPtr = (T *) (dst->data + dstY * dst->bytes_per_line); |
3172 | for (int srcX = 0, dstX = dstX0; srcX < srcXEnd; ++srcX, dstX += dstXIncr) |
3173 | std::swap(srcPtr[srcX], dstPtr[dstX]); |
3174 | } |
3175 | // If mirroring both ways, the middle line needs to be mirrored horizontally only. |
3176 | if (dstX0 && dstY0 && (h & 1)) { |
3177 | int srcY = h / 2; |
3178 | int srcXEnd2 = w / 2; |
3179 | T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line); |
3180 | for (int srcX = 0, dstX = dstX0; srcX < srcXEnd2; ++srcX, dstX += dstXIncr) |
3181 | std::swap(srcPtr[srcX], srcPtr[dstX]); |
3182 | } |
3183 | } else { |
3184 | for (int srcY = 0, dstY = dstY0; srcY < h; ++srcY, dstY += dstYIncr) { |
3185 | T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line); |
3186 | T *dstPtr = (T *) (dst->data + dstY * dst->bytes_per_line); |
3187 | for (int srcX = 0, dstX = dstX0; srcX < w; ++srcX, dstX += dstXIncr) |
3188 | dstPtr[dstX] = srcPtr[srcX]; |
3189 | } |
3190 | } |
3191 | } |
3192 | |
3193 | inline void do_flip(QImageData *dst, QImageData *src, int w, int h, int depth) |
3194 | { |
3195 | const int data_bytes_per_line = w * (depth / 8); |
3196 | if (dst == src) { |
3197 | uint *srcPtr = reinterpret_cast<uint *>(src->data); |
3198 | uint *dstPtr = reinterpret_cast<uint *>(dst->data + (h - 1) * dst->bytes_per_line); |
3199 | h = h / 2; |
3200 | const int uint_per_line = (data_bytes_per_line + 3) >> 2; // bytes per line must be a multiple of 4 |
3201 | for (int y = 0; y < h; ++y) { |
3202 | // This is auto-vectorized, no need for SSE2 or NEON versions: |
3203 | for (int x = 0; x < uint_per_line; x++) { |
3204 | const uint d = dstPtr[x]; |
3205 | const uint s = srcPtr[x]; |
3206 | dstPtr[x] = s; |
3207 | srcPtr[x] = d; |
3208 | } |
3209 | srcPtr += src->bytes_per_line >> 2; |
3210 | dstPtr -= dst->bytes_per_line >> 2; |
3211 | } |
3212 | |
3213 | } else { |
3214 | const uchar *srcPtr = src->data; |
3215 | uchar *dstPtr = dst->data + (h - 1) * dst->bytes_per_line; |
3216 | for (int y = 0; y < h; ++y) { |
3217 | memcpy(dstPtr, srcPtr, data_bytes_per_line); |
3218 | srcPtr += src->bytes_per_line; |
3219 | dstPtr -= dst->bytes_per_line; |
3220 | } |
3221 | } |
3222 | } |
3223 | |
3224 | inline void do_mirror(QImageData *dst, QImageData *src, bool horizontal, bool vertical) |
3225 | { |
3226 | Q_ASSERT(src->width == dst->width && src->height == dst->height && src->depth == dst->depth); |
3227 | int w = src->width; |
3228 | int h = src->height; |
3229 | int depth = src->depth; |
3230 | |
3231 | if (src->depth == 1) { |
3232 | w = (w + 7) / 8; // byte aligned width |
3233 | depth = 8; |
3234 | } |
3235 | |
3236 | if (vertical && !horizontal) { |
3237 | // This one is simple and common, so do it a little more optimized |
3238 | do_flip(dst, src, w, h, depth); |
3239 | return; |
3240 | } |
3241 | |
3242 | int dstX0 = 0, dstXIncr = 1; |
3243 | int dstY0 = 0, dstYIncr = 1; |
3244 | if (horizontal) { |
3245 | // 0 -> w-1, 1 -> w-2, 2 -> w-3, ... |
3246 | dstX0 = w - 1; |
3247 | dstXIncr = -1; |
3248 | } |
3249 | if (vertical) { |
3250 | // 0 -> h-1, 1 -> h-2, 2 -> h-3, ... |
3251 | dstY0 = h - 1; |
3252 | dstYIncr = -1; |
3253 | } |
3254 | |
3255 | switch (depth) { |
3256 | case 64: |
3257 | do_mirror_data<quint64>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h); |
3258 | break; |
3259 | case 32: |
3260 | do_mirror_data<quint32>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h); |
3261 | break; |
3262 | case 24: |
3263 | do_mirror_data<quint24>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h); |
3264 | break; |
3265 | case 16: |
3266 | do_mirror_data<quint16>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h); |
3267 | break; |
3268 | case 8: |
3269 | do_mirror_data<quint8>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h); |
3270 | break; |
3271 | default: |
3272 | Q_ASSERT(false); |
3273 | break; |
3274 | } |
3275 | |
3276 | // The bytes are now all in the correct place. In addition, the bits in the individual |
3277 | // bytes have to be flipped too when horizontally mirroring a 1 bit-per-pixel image. |
3278 | if (horizontal && dst->depth == 1) { |
3279 | Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB); |
3280 | const int shift = 8 - (dst->width % 8); |
3281 | const uchar *bitflip = qt_get_bitflip_array(); |
3282 | for (int y = 0; y < h; ++y) { |
3283 | uchar *begin = dst->data + y * dst->bytes_per_line; |
3284 | uchar *end = begin + dst->bytes_per_line; |
3285 | for (uchar *p = begin; p < end; ++p) { |
3286 | *p = bitflip[*p]; |
3287 | // When the data is non-byte aligned, an extra bit shift (of the number of |
3288 | // unused bits at the end) is needed for the entire scanline. |
3289 | if (shift != 8 && p != begin) { |
3290 | if (dst->format == QImage::Format_Mono) { |
3291 | for (int i = 0; i < shift; ++i) { |
3292 | p[-1] <<= 1; |
3293 | p[-1] |= (*p & (128 >> i)) >> (7 - i); |
3294 | } |
3295 | } else { |
3296 | for (int i = 0; i < shift; ++i) { |
3297 | p[-1] >>= 1; |
3298 | p[-1] |= (*p & (1 << i)) << (7 - i); |
3299 | } |
3300 | } |
3301 | } |
3302 | } |
3303 | if (shift != 8) { |
3304 | if (dst->format == QImage::Format_Mono) |
3305 | end[-1] <<= shift; |
3306 | else |
3307 | end[-1] >>= shift; |
3308 | } |
3309 | } |
3310 | } |
3311 | } |
3312 | |
3313 | /*! |
3314 | \internal |
3315 | */ |
3316 | QImage QImage::mirrored_helper(bool horizontal, bool vertical) const |
3317 | { |
3318 | if (!d) |
3319 | return QImage(); |
3320 | |
3321 | if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical)) |
3322 | return *this; |
3323 | |
3324 | // Create result image, copy colormap |
3325 | QImage result(d->width, d->height, d->format); |
3326 | QIMAGE_SANITYCHECK_MEMORY(result); |
3327 | |
3328 | // check if we ran out of of memory.. |
3329 | if (!result.d) |
3330 | return QImage(); |
3331 | |
3332 | result.d->colortable = d->colortable; |
3333 | result.d->has_alpha_clut = d->has_alpha_clut; |
3334 | copyMetadata(result.d, d); |
3335 | |
3336 | do_mirror(result.d, d, horizontal, vertical); |
3337 | |
3338 | return result; |
3339 | } |
3340 | |
3341 | /*! |
3342 | \internal |
3343 | */ |
3344 | void QImage::mirrored_inplace(bool horizontal, bool vertical) |
3345 | { |
3346 | if (!d || (d->width <= 1 && d->height <= 1) || (!horizontal && !vertical)) |
3347 | return; |
3348 | |
3349 | detach(); |
3350 | if (!d) |
3351 | return; |
3352 | if (!d->own_data) |
3353 | *this = copy(); |
3354 | |
3355 | do_mirror(d, d, horizontal, vertical); |
3356 | } |
3357 | |
3358 | /*! |
3359 | \fn QImage QImage::rgbSwapped() const & |
3360 | \fn QImage QImage::rgbSwapped() && |
3361 | |
3362 | Returns a QImage in which the values of the red and blue |
3363 | components of all pixels have been swapped, effectively converting |
3364 | an RGB image to an BGR image. |
3365 | |
3366 | The original QImage is not changed. |
3367 | |
3368 | \sa {QImage#Image Transformations}{Image Transformations} |
3369 | */ |
3370 | |
3371 | inline void rgbSwapped_generic(int width, int height, const QImage *src, QImage *dst, const QPixelLayout* layout) |
3372 | { |
3373 | const RbSwapFunc func = layout->rbSwap; |
3374 | if (!func) { |
3375 | qWarning("Trying to rb-swap an image format where it doesn't make sense" ); |
3376 | if (src != dst) |
3377 | *dst = *src; |
3378 | return; |
3379 | } |
3380 | |
3381 | for (int i = 0; i < height; ++i) { |
3382 | uchar *q = dst->scanLine(i); |
3383 | const uchar *p = src->constScanLine(i); |
3384 | func(q, p, width); |
3385 | } |
3386 | } |
3387 | |
3388 | /*! |
3389 | \internal |
3390 | */ |
3391 | QImage QImage::rgbSwapped_helper() const |
3392 | { |
3393 | if (isNull()) |
3394 | return *this; |
3395 | |
3396 | QImage res; |
3397 | |
3398 | switch (d->format) { |
3399 | case Format_Invalid: |
3400 | case NImageFormats: |
3401 | Q_ASSERT(false); |
3402 | break; |
3403 | case Format_Alpha8: |
3404 | case Format_Grayscale8: |
3405 | case Format_Grayscale16: |
3406 | return *this; |
3407 | case Format_Mono: |
3408 | case Format_MonoLSB: |
3409 | case Format_Indexed8: |
3410 | res = copy(); |
3411 | for (int i = 0; i < res.d->colortable.size(); i++) { |
3412 | QRgb c = res.d->colortable.at(i); |
3413 | res.d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00)); |
3414 | } |
3415 | break; |
3416 | case Format_RGBX8888: |
3417 | case Format_RGBA8888: |
3418 | case Format_RGBA8888_Premultiplied: |
3419 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
3420 | res = QImage(d->width, d->height, d->format); |
3421 | QIMAGE_SANITYCHECK_MEMORY(res); |
3422 | for (int i = 0; i < d->height; i++) { |
3423 | uint *q = (uint*)res.scanLine(i); |
3424 | const uint *p = (const uint*)constScanLine(i); |
3425 | const uint *end = p + d->width; |
3426 | while (p < end) { |
3427 | uint c = *p; |
3428 | *q = ((c << 16) & 0xff000000) | ((c >> 16) & 0xff00) | (c & 0x00ff00ff); |
3429 | p++; |
3430 | q++; |
3431 | } |
3432 | } |
3433 | break; |
3434 | #else |
3435 | // On little-endian rgba8888 is abgr32 and can use same rgb-swap as argb32 |
3436 | Q_FALLTHROUGH(); |
3437 | #endif |
3438 | case Format_RGB32: |
3439 | case Format_ARGB32: |
3440 | case Format_ARGB32_Premultiplied: |
3441 | res = QImage(d->width, d->height, d->format); |
3442 | QIMAGE_SANITYCHECK_MEMORY(res); |
3443 | for (int i = 0; i < d->height; i++) { |
3444 | uint *q = (uint*)res.scanLine(i); |
3445 | const uint *p = (const uint*)constScanLine(i); |
3446 | const uint *end = p + d->width; |
3447 | while (p < end) { |
3448 | uint c = *p; |
3449 | *q = ((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00); |
3450 | p++; |
3451 | q++; |
3452 | } |
3453 | } |
3454 | break; |
3455 | case Format_RGB16: |
3456 | res = QImage(d->width, d->height, d->format); |
3457 | QIMAGE_SANITYCHECK_MEMORY(res); |
3458 | for (int i = 0; i < d->height; i++) { |
3459 | ushort *q = (ushort*)res.scanLine(i); |
3460 | const ushort *p = (const ushort*)constScanLine(i); |
3461 | const ushort *end = p + d->width; |
3462 | while (p < end) { |
3463 | ushort c = *p; |
3464 | *q = ((c << 11) & 0xf800) | ((c >> 11) & 0x1f) | (c & 0x07e0); |
3465 | p++; |
3466 | q++; |
3467 | } |
3468 | } |
3469 | break; |
3470 | case Format_RGBX64: |
3471 | case Format_RGBA64: |
3472 | case Format_RGBA64_Premultiplied: |
3473 | res = QImage(d->width, d->height, d->format); |
3474 | QIMAGE_SANITYCHECK_MEMORY(res); |
3475 | for (int i = 0; i < d->height; i++) { |
3476 | QRgba64 *q = reinterpret_cast<QRgba64 *>(res.scanLine(i)); |
3477 | const QRgba64 *p = reinterpret_cast<const QRgba64 *>(constScanLine(i)); |
3478 | const QRgba64 *end = p + d->width; |
3479 | while (p < end) { |
3480 | QRgba64 c = *p; |
3481 | *q = QRgba64::fromRgba64(c.blue(), c.green(), c.red(), c.alpha()); |
3482 | p++; |
3483 | q++; |
3484 | } |
3485 | } |
3486 | break; |
3487 | default: |
3488 | res = QImage(d->width, d->height, d->format); |
3489 | rgbSwapped_generic(d->width, d->height, this, &res, &qPixelLayouts[d->format]); |
3490 | break; |
3491 | } |
3492 | copyMetadata(res.d, d); |
3493 | return res; |
3494 | } |
3495 | |
3496 | /*! |
3497 | \internal |
3498 | */ |
3499 | void QImage::rgbSwapped_inplace() |
3500 | { |
3501 | if (isNull()) |
3502 | return; |
3503 | |
3504 | detach(); |
3505 | if (!d) |
3506 | return; |
3507 | if (!d->own_data) |
3508 | *this = copy(); |
3509 | |
3510 | switch (d->format) { |
3511 | case Format_Invalid: |
3512 | case NImageFormats: |
3513 | Q_ASSERT(false); |
3514 | break; |
3515 | case Format_Alpha8: |
3516 | case Format_Grayscale8: |
3517 | case Format_Grayscale16: |
3518 | return; |
3519 | case Format_Mono: |
3520 | case Format_MonoLSB: |
3521 | case Format_Indexed8: |
3522 | for (int i = 0; i < d->colortable.size(); i++) { |
3523 | QRgb c = d->colortable.at(i); |
3524 | d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00)); |
3525 | } |
3526 | break; |
3527 | case Format_RGBX8888: |
3528 | case Format_RGBA8888: |
3529 | case Format_RGBA8888_Premultiplied: |
3530 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
3531 | for (int i = 0; i < d->height; i++) { |
3532 | uint *p = (uint*)scanLine(i); |
3533 | uint *end = p + d->width; |
3534 | while (p < end) { |
3535 | uint c = *p; |
3536 | *p = ((c << 16) & 0xff000000) | ((c >> 16) & 0xff00) | (c & 0x00ff00ff); |
3537 | p++; |
3538 | } |
3539 | } |
3540 | break; |
3541 | #else |
3542 | // On little-endian rgba8888 is abgr32 and can use same rgb-swap as argb32 |
3543 | Q_FALLTHROUGH(); |
3544 | #endif |
3545 | case Format_RGB32: |
3546 | case Format_ARGB32: |
3547 | case Format_ARGB32_Premultiplied: |
3548 | for (int i = 0; i < d->height; i++) { |
3549 | uint *p = (uint*)scanLine(i); |
3550 | uint *end = p + d->width; |
3551 | while (p < end) { |
3552 | uint c = *p; |
3553 | *p = ((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00); |
3554 | p++; |
3555 | } |
3556 | } |
3557 | break; |
3558 | case Format_RGB16: |
3559 | for (int i = 0; i < d->height; i++) { |
3560 | ushort *p = (ushort*)scanLine(i); |
3561 | ushort *end = p + d->width; |
3562 | while (p < end) { |
3563 | ushort c = *p; |
3564 | *p = ((c << 11) & 0xf800) | ((c >> 11) & 0x1f) | (c & 0x07e0); |
3565 | p++; |
3566 | } |
3567 | } |
3568 | break; |
3569 | case Format_BGR30: |
3570 | case Format_A2BGR30_Premultiplied: |
3571 | case Format_RGB30: |
3572 | case Format_A2RGB30_Premultiplied: |
3573 | for (int i = 0; i < d->height; i++) { |
3574 | uint *p = (uint*)scanLine(i); |
3575 | uint *end = p + d->width; |
3576 | while (p < end) { |
3577 | *p = qRgbSwapRgb30(*p); |
3578 | p++; |
3579 | } |
3580 | } |
3581 | break; |
3582 | case Format_RGBX64: |
3583 | case Format_RGBA64: |
3584 | case Format_RGBA64_Premultiplied: |
3585 | for (int i = 0; i < d->height; i++) { |
3586 | QRgba64 *p = reinterpret_cast<QRgba64 *>(scanLine(i)); |
3587 | QRgba64 *end = p + d->width; |
3588 | while (p < end) { |
3589 | QRgba64 c = *p; |
3590 | *p = QRgba64::fromRgba64(c.blue(), c.green(), c.red(), c.alpha()); |
3591 | p++; |
3592 | } |
3593 | } |
3594 | break; |
3595 | default: |
3596 | rgbSwapped_generic(d->width, d->height, this, this, &qPixelLayouts[d->format]); |
3597 | break; |
3598 | } |
3599 | } |
3600 | |
3601 | /*! |
3602 | Loads an image from the file with the given \a fileName. Returns \c true if |
3603 | the image was successfully loaded; otherwise invalidates the image |
3604 | and returns \c false. |
3605 | |
3606 | The loader attempts to read the image using the specified \a format, e.g., |
3607 | PNG or JPG. If \a format is not specified (which is the default), it is |
3608 | auto-detected based on the file's suffix and header. For details, see |
3609 | QImageReader::setAutoDetectImageFormat(). |
3610 | |
3611 | The file name can either refer to an actual file on disk or to one |
3612 | of the application's embedded resources. See the |
3613 | \l{resources.html}{Resource System} overview for details on how to |
3614 | embed images and other resource files in the application's |
3615 | executable. |
3616 | |
3617 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files} |
3618 | */ |
3619 | |
3620 | bool QImage::load(const QString &fileName, const char* format) |
3621 | { |
3622 | *this = QImageReader(fileName, format).read(); |
3623 | return !isNull(); |
3624 | } |
3625 | |
3626 | /*! |
3627 | \overload |
3628 | |
3629 | This function reads a QImage from the given \a device. This can, |
3630 | for example, be used to load an image directly into a QByteArray. |
3631 | */ |
3632 | |
3633 | bool QImage::load(QIODevice* device, const char* format) |
3634 | { |
3635 | *this = QImageReader(device, format).read(); |
3636 | return !isNull(); |
3637 | } |
3638 | |
3639 | /*! |
3640 | \fn bool QImage::loadFromData(const uchar *data, int len, const char *format) |
3641 | |
3642 | Loads an image from the first \a len bytes of the given binary \a |
3643 | data. Returns \c true if the image was successfully loaded; otherwise |
3644 | invalidates the image and returns \c false. |
3645 | |
3646 | The loader attempts to read the image using the specified \a format, e.g., |
3647 | PNG or JPG. If \a format is not specified (which is the default), the |
3648 | loader probes the file for a header to guess the file format. |
3649 | |
3650 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files} |
3651 | */ |
3652 | |
3653 | bool QImage::loadFromData(const uchar *data, int len, const char *format) |
3654 | { |
3655 | *this = fromData(data, len, format); |
3656 | return !isNull(); |
3657 | } |
3658 | |
3659 | /*! |
3660 | \fn bool QImage::loadFromData(const QByteArray &data, const char *format) |
3661 | |
3662 | \overload |
3663 | |
3664 | Loads an image from the given QByteArray \a data. |
3665 | */ |
3666 | |
3667 | /*! |
3668 | \fn QImage QImage::fromData(const uchar *data, int size, const char *format) |
3669 | |
3670 | Constructs a QImage from the first \a size bytes of the given |
3671 | binary \a data. The loader attempts to read the image using the |
3672 | specified \a format. If \a format is not specified (which is the default), |
3673 | the loader probes the data for a header to guess the file format. |
3674 | |
3675 | If \a format is specified, it must be one of the values returned by |
3676 | QImageReader::supportedImageFormats(). |
3677 | |
3678 | If the loading of the image fails, the image returned will be a null image. |
3679 | |
3680 | \sa load(), save(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files} |
3681 | */ |
3682 | |
3683 | QImage QImage::fromData(const uchar *data, int size, const char *format) |
3684 | { |
3685 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size); |
3686 | QBuffer b; |
3687 | b.setData(a); |
3688 | b.open(QIODevice::ReadOnly); |
3689 | return QImageReader(&b, format).read(); |
3690 | } |
3691 | |
3692 | /*! |
3693 | \fn QImage QImage::fromData(const QByteArray &data, const char *format) |
3694 | |
3695 | \overload |
3696 | |
3697 | Loads an image from the given QByteArray \a data. |
3698 | */ |
3699 | |
3700 | /*! |
3701 | Saves the image to the file with the given \a fileName, using the |
3702 | given image file \a format and \a quality factor. If \a format is |
3703 | 0, QImage will attempt to guess the format by looking at \a fileName's |
3704 | suffix. |
3705 | |
3706 | The \a quality factor must be in the range 0 to 100 or -1. Specify |
3707 | 0 to obtain small compressed files, 100 for large uncompressed |
3708 | files, and -1 (the default) to use the default settings. |
3709 | |
3710 | Returns \c true if the image was successfully saved; otherwise |
3711 | returns \c false. |
3712 | |
3713 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing |
3714 | Image Files} |
3715 | */ |
3716 | bool QImage::save(const QString &fileName, const char *format, int quality) const |
3717 | { |
3718 | if (isNull()) |
3719 | return false; |
3720 | QImageWriter writer(fileName, format); |
3721 | return d->doImageIO(this, &writer, quality); |
3722 | } |
3723 | |
3724 | /*! |
3725 | \overload |
3726 | |
3727 | This function writes a QImage to the given \a device. |
3728 | |
3729 | This can, for example, be used to save an image directly into a |
3730 | QByteArray: |
3731 | |
3732 | \snippet image/image.cpp 0 |
3733 | */ |
3734 | |
3735 | bool QImage::save(QIODevice* device, const char* format, int quality) const |
3736 | { |
3737 | if (isNull()) |
3738 | return false; // nothing to save |
3739 | QImageWriter writer(device, format); |
3740 | return d->doImageIO(this, &writer, quality); |
3741 | } |
3742 | |
3743 | /* \internal |
3744 | */ |
3745 | |
3746 | bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const |
3747 | { |
3748 | if (quality > 100 || quality < -1) |
3749 | qWarning("QPixmap::save: Quality out of range [-1, 100]" ); |
3750 | if (quality >= 0) |
3751 | writer->setQuality(qMin(quality,100)); |
3752 | return writer->write(*image); |
3753 | } |
3754 | |
3755 | /***************************************************************************** |
3756 | QImage stream functions |
3757 | *****************************************************************************/ |
3758 | #if !defined(QT_NO_DATASTREAM) |
3759 | /*! |
3760 | \fn QDataStream &operator<<(QDataStream &stream, const QImage &image) |
3761 | \relates QImage |
3762 | |
3763 | Writes the given \a image to the given \a stream as a PNG image, |
3764 | or as a BMP image if the stream's version is 1. Note that writing |
3765 | the stream to a file will not produce a valid image file. |
3766 | |
3767 | \sa QImage::save(), {Serializing Qt Data Types} |
3768 | */ |
3769 | |
3770 | QDataStream &operator<<(QDataStream &s, const QImage &image) |
3771 | { |
3772 | if (s.version() >= 5) { |
3773 | if (image.isNull()) { |
3774 | s << (qint32) 0; // null image marker |
3775 | return s; |
3776 | } else { |
3777 | s << (qint32) 1; |
3778 | // continue ... |
3779 | } |
3780 | } |
3781 | QImageWriter writer(s.device(), s.version() == 1 ? "bmp" : "png" ); |
3782 | writer.write(image); |
3783 | return s; |
3784 | } |
3785 | |
3786 | /*! |
3787 | \fn QDataStream &operator>>(QDataStream &stream, QImage &image) |
3788 | \relates QImage |
3789 | |
3790 | Reads an image from the given \a stream and stores it in the given |
3791 | \a image. |
3792 | |
3793 | \sa QImage::load(), {Serializing Qt Data Types} |
3794 | */ |
3795 | |
3796 | QDataStream &operator>>(QDataStream &s, QImage &image) |
3797 | { |
3798 | if (s.version() >= 5) { |
3799 | qint32 nullMarker; |
3800 | s >> nullMarker; |
3801 | if (!nullMarker) { |
3802 | image = QImage(); // null image |
3803 | return s; |
3804 | } |
3805 | } |
3806 | image = QImageReader(s.device(), s.version() == 1 ? "bmp" : "png" ).read(); |
3807 | if (image.isNull() && s.version() >= 5) |
3808 | s.setStatus(QDataStream::ReadPastEnd); |
3809 | return s; |
3810 | } |
3811 | #endif // QT_NO_DATASTREAM |
3812 | |
3813 | |
3814 | |
3815 | /*! |
3816 | \fn bool QImage::operator==(const QImage & image) const |
3817 | |
3818 | Returns \c true if this image and the given \a image have the same |
3819 | contents; otherwise returns \c false. |
3820 | |
3821 | The comparison can be slow, unless there is some obvious |
3822 | difference (e.g. different size or format), in which case the |
3823 | function will return quickly. |
3824 | |
3825 | \sa operator=() |
3826 | */ |
3827 | |
3828 | bool QImage::operator==(const QImage & i) const |
3829 | { |
3830 | // same object, or shared? |
3831 | if (i.d == d) |
3832 | return true; |
3833 | if (!i.d || !d) |
3834 | return false; |
3835 | |
3836 | // obviously different stuff? |
3837 | if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format) |
3838 | return false; |
3839 | |
3840 | if (d->format != Format_RGB32) { |
3841 | if (d->format >= Format_ARGB32) { // all bits defined |
3842 | const int n = d->width * d->depth / 8; |
3843 | if (n == d->bytes_per_line && n == i.d->bytes_per_line) { |
3844 | if (memcmp(bits(), i.bits(), d->nbytes)) |
3845 | return false; |
3846 | } else { |
3847 | for (int y = 0; y < d->height; ++y) { |
3848 | if (memcmp(scanLine(y), i.scanLine(y), n)) |
3849 | return false; |
3850 | } |
3851 | } |
3852 | } else { |
3853 | const int w = width(); |
3854 | const int h = height(); |
3855 | const QVector<QRgb> &colortable = d->colortable; |
3856 | const QVector<QRgb> &icolortable = i.d->colortable; |
3857 | for (int y=0; y<h; ++y) { |
3858 | for (int x=0; x<w; ++x) { |
3859 | if (colortable[pixelIndex(x, y)] != icolortable[i.pixelIndex(x, y)]) |
3860 | return false; |
3861 | } |
3862 | } |
3863 | } |
3864 | } else { |
3865 | //alpha channel undefined, so we must mask it out |
3866 | for(int l = 0; l < d->height; l++) { |
3867 | int w = d->width; |
3868 | const uint *p1 = reinterpret_cast<const uint*>(scanLine(l)); |
3869 | const uint *p2 = reinterpret_cast<const uint*>(i.scanLine(l)); |
3870 | while (w--) { |
3871 | if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff)) |
3872 | return false; |
3873 | } |
3874 | } |
3875 | } |
3876 | return true; |
3877 | } |
3878 | |
3879 | |
3880 | /*! |
3881 | \fn bool QImage::operator!=(const QImage & image) const |
3882 | |
3883 | Returns \c true if this image and the given \a image have different |
3884 | contents; otherwise returns \c false. |
3885 | |
3886 | The comparison can be slow, unless there is some obvious |
3887 | difference, such as different widths, in which case the function |
3888 | will return quickly. |
3889 | |
3890 | \sa operator=() |
3891 | */ |
3892 | |
3893 | bool QImage::operator!=(const QImage & i) const |
3894 | { |
3895 | return !(*this == i); |
3896 | } |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | /*! |
3902 | Returns the number of pixels that fit horizontally in a physical |
3903 | meter. Together with dotsPerMeterY(), this number defines the |
3904 | intended scale and aspect ratio of the image. |
3905 | |
3906 | \sa setDotsPerMeterX(), {QImage#Image Information}{Image |
3907 | Information} |
3908 | */ |
3909 | int QImage::dotsPerMeterX() const |
3910 | { |
3911 | return d ? qRound(d->dpmx) : 0; |
3912 | } |
3913 | |
3914 | /*! |
3915 | Returns the number of pixels that fit vertically in a physical |
3916 | meter. Together with dotsPerMeterX(), this number defines the |
3917 | intended scale and aspect ratio of the image. |
3918 | |
3919 | \sa setDotsPerMeterY(), {QImage#Image Information}{Image |
3920 | Information} |
3921 | */ |
3922 | int QImage::dotsPerMeterY() const |
3923 | { |
3924 | return d ? qRound(d->dpmy) : 0; |
3925 | } |
3926 | |
3927 | /*! |
3928 | Sets the number of pixels that fit horizontally in a physical |
3929 | meter, to \a x. |
3930 | |
3931 | Together with dotsPerMeterY(), this number defines the intended |
3932 | scale and aspect ratio of the image, and determines the scale |
3933 | at which QPainter will draw graphics on the image. It does not |
3934 | change the scale or aspect ratio of the image when it is rendered |
3935 | on other paint devices. |
3936 | |
3937 | \sa dotsPerMeterX(), {QImage#Image Information}{Image Information} |
3938 | */ |
3939 | void QImage::setDotsPerMeterX(int x) |
3940 | { |
3941 | if (!d || !x) |
3942 | return; |
3943 | detach(); |
3944 | |
3945 | if (d) |
3946 | d->dpmx = x; |
3947 | } |
3948 | |
3949 | /*! |
3950 | Sets the number of pixels that fit vertically in a physical meter, |
3951 | to \a y. |
3952 | |
3953 | Together with dotsPerMeterX(), this number defines the intended |
3954 | scale and aspect ratio of the image, and determines the scale |
3955 | at which QPainter will draw graphics on the image. It does not |
3956 | change the scale or aspect ratio of the image when it is rendered |
3957 | on other paint devices. |
3958 | |
3959 | \sa dotsPerMeterY(), {QImage#Image Information}{Image Information} |
3960 | */ |
3961 | void QImage::setDotsPerMeterY(int y) |
3962 | { |
3963 | if (!d || !y) |
3964 | return; |
3965 | detach(); |
3966 | |
3967 | if (d) |
3968 | d->dpmy = y; |
3969 | } |
3970 | |
3971 | /*! |
3972 | \fn QPoint QImage::offset() const |
3973 | |
3974 | Returns the number of pixels by which the image is intended to be |
3975 | offset by when positioning relative to other images. |
3976 | |
3977 | \sa setOffset(), {QImage#Image Information}{Image Information} |
3978 | */ |
3979 | QPoint QImage::offset() const |
3980 | { |
3981 | return d ? d->offset : QPoint(); |
3982 | } |
3983 | |
3984 | |
3985 | /*! |
3986 | \fn void QImage::setOffset(const QPoint& offset) |
3987 | |
3988 | Sets the number of pixels by which the image is intended to be |
3989 | offset by when positioning relative to other images, to \a offset. |
3990 | |
3991 | \sa offset(), {QImage#Image Information}{Image Information} |
3992 | */ |
3993 | void QImage::setOffset(const QPoint& p) |
3994 | { |
3995 | if (!d) |
3996 | return; |
3997 | detach(); |
3998 | |
3999 | if (d) |
4000 | d->offset = p; |
4001 | } |
4002 | |
4003 | /*! |
4004 | Returns the text keys for this image. |
4005 | |
4006 | You can use these keys with text() to list the image text for a |
4007 | certain key. |
4008 | |
4009 | \sa text() |
4010 | */ |
4011 | QStringList QImage::textKeys() const |
4012 | { |
4013 | return d ? QStringList(d->text.keys()) : QStringList(); |
4014 | } |
4015 | |
4016 | /*! |
4017 | Returns the image text associated with the given \a key. If the |
4018 | specified \a key is an empty string, the whole image text is |
4019 | returned, with each key-text pair separated by a newline. |
4020 | |
4021 | \sa setText(), textKeys() |
4022 | */ |
4023 | QString QImage::text(const QString &key) const |
4024 | { |
4025 | if (!d) |
4026 | return QString(); |
4027 | |
4028 | if (!key.isEmpty()) |
4029 | return d->text.value(key); |
4030 | |
4031 | QString tmp; |
4032 | for (auto it = d->text.begin(), end = d->text.end(); it != end; ++it) |
4033 | tmp += it.key() + QLatin1String(": " ) + it.value().simplified() + QLatin1String("\n\n" ); |
4034 | if (!tmp.isEmpty()) |
4035 | tmp.chop(2); // remove final \n\n |
4036 | return tmp; |
4037 | } |
4038 | |
4039 | /*! |
4040 | \fn void QImage::setText(const QString &key, const QString &text) |
4041 | |
4042 | Sets the image text to the given \a text and associate it with the |
4043 | given \a key. |
4044 | |
4045 | If you just want to store a single text block (i.e., a "comment" |
4046 | or just a description), you can either pass an empty key, or use a |
4047 | generic key like "Description". |
4048 | |
4049 | The image text is embedded into the image data when you |
4050 | call save() or QImageWriter::write(). |
4051 | |
4052 | Not all image formats support embedded text. You can find out |
4053 | if a specific image or format supports embedding text |
4054 | by using QImageWriter::supportsOption(). We give an example: |
4055 | |
4056 | \snippet image/supportedformat.cpp 0 |
4057 | |
4058 | You can use QImageWriter::supportedImageFormats() to find out |
4059 | which image formats are available to you. |
4060 | |
4061 | \sa text(), textKeys() |
4062 | */ |
4063 | void QImage::setText(const QString &key, const QString &value) |
4064 | { |
4065 | if (!d) |
4066 | return; |
4067 | detach(); |
4068 | |
4069 | if (d) |
4070 | d->text.insert(key, value); |
4071 | } |
4072 | |
4073 | /*! |
4074 | \fn QString QImage::text(const char* key, const char* language) const |
4075 | \obsolete |
4076 | |
4077 | Returns the text recorded for the given \a key in the given \a |
4078 | language, or in a default language if \a language is 0. |
4079 | |
4080 | Use text() instead. |
4081 | |
4082 | The language the text is recorded in is no longer relevant since |
4083 | the text is always set using QString and UTF-8 representation. |
4084 | */ |
4085 | |
4086 | /*! |
4087 | \fn QString QImage::text(const QImageTextKeyLang& keywordAndLanguage) const |
4088 | \overload |
4089 | \obsolete |
4090 | |
4091 | Returns the text recorded for the given \a keywordAndLanguage. |
4092 | |
4093 | Use text() instead. |
4094 | |
4095 | The language the text is recorded in is no longer relevant since |
4096 | the text is always set using QString and UTF-8 representation. |
4097 | */ |
4098 | |
4099 | /*! |
4100 | \fn void QImage::setText(const char* key, const char* language, const QString& text) |
4101 | \obsolete |
4102 | |
4103 | Sets the image text to the given \a text and associate it with the |
4104 | given \a key. The text is recorded in the specified \a language, |
4105 | or in a default language if \a language is 0. |
4106 | |
4107 | Use setText() instead. |
4108 | |
4109 | The language the text is recorded in is no longer relevant since |
4110 | the text is always set using QString and UTF-8 representation. |
4111 | |
4112 | \omit |
4113 | Records string \a for the keyword \a key. The \a key should be |
4114 | a portable keyword recognizable by other software - some suggested |
4115 | values can be found in |
4116 | \l{http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.Anc-text} |
4117 | {the PNG specification}. \a s can be any text. \a lang should |
4118 | specify the language code (see |
4119 | \l{http://www.rfc-editor.org/rfc/rfc1766.txt}{RFC 1766}) or 0. |
4120 | \endomit |
4121 | */ |
4122 | |
4123 | /* |
4124 | Sets the image bits to the \a pixmap contents and returns a |
4125 | reference to the image. |
4126 | |
4127 | If the image shares data with other images, it will first |
4128 | dereference the shared data. |
4129 | |
4130 | Makes a call to QPixmap::convertToImage(). |
4131 | */ |
4132 | |
4133 | /*! |
4134 | \internal |
4135 | |
4136 | Used by QPainter to retrieve a paint engine for the image. |
4137 | */ |
4138 | |
4139 | QPaintEngine *QImage::paintEngine() const |
4140 | { |
4141 | if (!d) |
4142 | return 0; |
4143 | |
4144 | if (!d->paintEngine) { |
4145 | QPaintDevice *paintDevice = const_cast<QImage *>(this); |
4146 | QPaintEngine *paintEngine = 0; |
4147 | QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration(); |
4148 | if (platformIntegration) |
4149 | paintEngine = platformIntegration->createImagePaintEngine(paintDevice); |
4150 | d->paintEngine = paintEngine ? paintEngine : new QRasterPaintEngine(paintDevice); |
4151 | } |
4152 | |
4153 | return d->paintEngine; |
4154 | } |
4155 | |
4156 | |
4157 | /*! |
4158 | \internal |
4159 | |
4160 | Returns the size for the specified \a metric on the device. |
4161 | */ |
4162 | int QImage::metric(PaintDeviceMetric metric) const |
4163 | { |
4164 | if (!d) |
4165 | return 0; |
4166 | |
4167 | switch (metric) { |
4168 | case PdmWidth: |
4169 | return d->width; |
4170 | |
4171 | case PdmHeight: |
4172 | return d->height; |
4173 | |
4174 | case PdmWidthMM: |
4175 | return qRound(d->width * 1000 / d->dpmx); |
4176 | |
4177 | case |
---|