1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qimage.h"
|
---|
43 | #include "qdatastream.h"
|
---|
44 | #include "qbuffer.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 <ctype.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <limits.h>
|
---|
55 | #include <math.h>
|
---|
56 | #include <private/qdrawhelper_p.h>
|
---|
57 | #include <private/qmemrotate_p.h>
|
---|
58 | #include <private/qpixmapdata_p.h>
|
---|
59 | #include <private/qimagescale_p.h>
|
---|
60 |
|
---|
61 | #include <qhash.h>
|
---|
62 |
|
---|
63 | #ifdef QT_RASTER_IMAGEENGINE
|
---|
64 | #include <private/qpaintengine_raster_p.h>
|
---|
65 | #else
|
---|
66 | #include <qpaintengine.h>
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #include <private/qimage_p.h>
|
---|
70 |
|
---|
71 | QT_BEGIN_NAMESPACE
|
---|
72 |
|
---|
73 | static inline bool checkPixelSize(const QImage::Format format)
|
---|
74 | {
|
---|
75 | switch (format) {
|
---|
76 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
77 | return (sizeof(qargb8565) == 3);
|
---|
78 | case QImage::Format_RGB666:
|
---|
79 | return (sizeof(qrgb666) == 3);
|
---|
80 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
81 | return (sizeof(qargb6666) == 3);
|
---|
82 | case QImage::Format_RGB555:
|
---|
83 | return (sizeof(qrgb555) == 2);
|
---|
84 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
85 | return (sizeof(qargb8555) == 3);
|
---|
86 | case QImage::Format_RGB888:
|
---|
87 | return (sizeof(qrgb888) == 3);
|
---|
88 | case QImage::Format_RGB444:
|
---|
89 | return (sizeof(qrgb444) == 2);
|
---|
90 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
91 | return (sizeof(qargb4444) == 2);
|
---|
92 | default:
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | #if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001)
|
---|
98 | #pragma message disable narrowptr
|
---|
99 | #endif
|
---|
100 |
|
---|
101 |
|
---|
102 | #define QIMAGE_SANITYCHECK_MEMORY(image) \
|
---|
103 | if ((image).isNull()) { \
|
---|
104 | qWarning("QImage: out of memory, returning null image"); \
|
---|
105 | return QImage(); \
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | // ### Qt 5: remove
|
---|
110 | typedef void (*_qt_image_cleanup_hook)(int);
|
---|
111 | Q_GUI_EXPORT _qt_image_cleanup_hook qt_image_cleanup_hook = 0;
|
---|
112 |
|
---|
113 | // ### Qt 5: rename
|
---|
114 | typedef void (*_qt_image_cleanup_hook_64)(qint64);
|
---|
115 | Q_GUI_EXPORT _qt_image_cleanup_hook_64 qt_image_cleanup_hook_64 = 0;
|
---|
116 |
|
---|
117 | static QImage rotated90(const QImage &src);
|
---|
118 | static QImage rotated180(const QImage &src);
|
---|
119 | static QImage rotated270(const QImage &src);
|
---|
120 |
|
---|
121 | // ### Qt 5: remove
|
---|
122 | Q_GUI_EXPORT qint64 qt_image_id(const QImage &image)
|
---|
123 | {
|
---|
124 | return image.cacheKey();
|
---|
125 | }
|
---|
126 |
|
---|
127 | const QVector<QRgb> *qt_image_colortable(const QImage &image)
|
---|
128 | {
|
---|
129 | return &image.d->colortable;
|
---|
130 | }
|
---|
131 |
|
---|
132 | extern int qt_defaultDpiX();
|
---|
133 | extern int qt_defaultDpiY();
|
---|
134 |
|
---|
135 | QBasicAtomicInt qimage_serial_number = Q_BASIC_ATOMIC_INITIALIZER(1);
|
---|
136 |
|
---|
137 | QImageData::QImageData()
|
---|
138 | : ref(0), width(0), height(0), depth(0), nbytes(0), data(0),
|
---|
139 | #ifdef QT3_SUPPORT
|
---|
140 | jumptable(0),
|
---|
141 | #endif
|
---|
142 | format(QImage::Format_ARGB32), bytes_per_line(0),
|
---|
143 | ser_no(qimage_serial_number.fetchAndAddRelaxed(1)),
|
---|
144 | detach_no(0),
|
---|
145 | dpmx(qt_defaultDpiX() * 100 / qreal(2.54)),
|
---|
146 | dpmy(qt_defaultDpiY() * 100 / qreal(2.54)),
|
---|
147 | offset(0, 0), own_data(true), ro_data(false), has_alpha_clut(false),
|
---|
148 | is_cached(false), paintEngine(0)
|
---|
149 | {
|
---|
150 | }
|
---|
151 |
|
---|
152 | static int depthForFormat(QImage::Format format)
|
---|
153 | {
|
---|
154 | int depth = 0;
|
---|
155 | switch(format) {
|
---|
156 | case QImage::Format_Invalid:
|
---|
157 | case QImage::NImageFormats:
|
---|
158 | Q_ASSERT(false);
|
---|
159 | case QImage::Format_Mono:
|
---|
160 | case QImage::Format_MonoLSB:
|
---|
161 | depth = 1;
|
---|
162 | break;
|
---|
163 | case QImage::Format_Indexed8:
|
---|
164 | depth = 8;
|
---|
165 | break;
|
---|
166 | case QImage::Format_RGB32:
|
---|
167 | case QImage::Format_ARGB32:
|
---|
168 | case QImage::Format_ARGB32_Premultiplied:
|
---|
169 | depth = 32;
|
---|
170 | break;
|
---|
171 | case QImage::Format_RGB555:
|
---|
172 | case QImage::Format_RGB16:
|
---|
173 | case QImage::Format_RGB444:
|
---|
174 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
175 | depth = 16;
|
---|
176 | break;
|
---|
177 | case QImage::Format_RGB666:
|
---|
178 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
179 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
180 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
181 | case QImage::Format_RGB888:
|
---|
182 | depth = 24;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | return depth;
|
---|
186 | }
|
---|
187 |
|
---|
188 | QImageData * QImageData::create(const QSize &size, QImage::Format format, int numColors)
|
---|
189 | {
|
---|
190 | if (!size.isValid() || numColors < 0 || format == QImage::Format_Invalid)
|
---|
191 | return 0; // invalid parameter(s)
|
---|
192 |
|
---|
193 | if (!checkPixelSize(format)) {
|
---|
194 | qWarning("QImageData::create(): Invalid pixel size for format %i",
|
---|
195 | format);
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 |
|
---|
199 | uint width = size.width();
|
---|
200 | uint height = size.height();
|
---|
201 | uint depth = depthForFormat(format);
|
---|
202 |
|
---|
203 | switch (format) {
|
---|
204 | case QImage::Format_Mono:
|
---|
205 | case QImage::Format_MonoLSB:
|
---|
206 | numColors = 2;
|
---|
207 | break;
|
---|
208 | case QImage::Format_Indexed8:
|
---|
209 | numColors = qBound(0, numColors, 256);
|
---|
210 | break;
|
---|
211 | default:
|
---|
212 | numColors = 0;
|
---|
213 | break;
|
---|
214 | }
|
---|
215 |
|
---|
216 | const int bytes_per_line = ((width * depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 8)
|
---|
217 |
|
---|
218 | // sanity check for potential overflows
|
---|
219 | if (INT_MAX/depth < width
|
---|
220 | || bytes_per_line <= 0
|
---|
221 | || height <= 0
|
---|
222 | || INT_MAX/uint(bytes_per_line) < height
|
---|
223 | || INT_MAX/sizeof(uchar *) < uint(height))
|
---|
224 | return 0;
|
---|
225 |
|
---|
226 | QImageData *d = new QImageData;
|
---|
227 | d->colortable.resize(numColors);
|
---|
228 | if (depth == 1) {
|
---|
229 | d->colortable[0] = QColor(Qt::black).rgba();
|
---|
230 | d->colortable[1] = QColor(Qt::white).rgba();
|
---|
231 | } else {
|
---|
232 | for (int i = 0; i < numColors; ++i)
|
---|
233 | d->colortable[i] = 0;
|
---|
234 | }
|
---|
235 |
|
---|
236 | d->width = width;
|
---|
237 | d->height = height;
|
---|
238 | d->depth = depth;
|
---|
239 | d->format = format;
|
---|
240 | d->has_alpha_clut = false;
|
---|
241 | d->is_cached = false;
|
---|
242 |
|
---|
243 | d->bytes_per_line = bytes_per_line;
|
---|
244 |
|
---|
245 | d->nbytes = d->bytes_per_line*height;
|
---|
246 | d->data = (uchar *)malloc(d->nbytes);
|
---|
247 |
|
---|
248 | if (!d->data) {
|
---|
249 | delete d;
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | d->ref.ref();
|
---|
254 | return d;
|
---|
255 |
|
---|
256 | }
|
---|
257 |
|
---|
258 | QImageData::~QImageData()
|
---|
259 | {
|
---|
260 | if (is_cached && qt_image_cleanup_hook_64)
|
---|
261 | qt_image_cleanup_hook_64((((qint64) ser_no) << 32) | ((qint64) detach_no));
|
---|
262 | delete paintEngine;
|
---|
263 | if (data && own_data)
|
---|
264 | free(data);
|
---|
265 | #ifdef QT3_SUPPORT
|
---|
266 | if (jumptable)
|
---|
267 | free(jumptable);
|
---|
268 | jumptable = 0;
|
---|
269 | #endif
|
---|
270 | data = 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | bool QImageData::checkForAlphaPixels() const
|
---|
275 | {
|
---|
276 | bool has_alpha_pixels = false;
|
---|
277 |
|
---|
278 | switch (format) {
|
---|
279 |
|
---|
280 | case QImage::Format_Indexed8:
|
---|
281 | has_alpha_pixels = has_alpha_clut;
|
---|
282 | break;
|
---|
283 |
|
---|
284 | case QImage::Format_ARGB32:
|
---|
285 | case QImage::Format_ARGB32_Premultiplied: {
|
---|
286 | uchar *bits = data;
|
---|
287 | for (int y=0; y<height && !has_alpha_pixels; ++y) {
|
---|
288 | for (int x=0; x<width; ++x)
|
---|
289 | has_alpha_pixels |= (((uint *)bits)[x] & 0xff000000) != 0xff000000;
|
---|
290 | bits += bytes_per_line;
|
---|
291 | }
|
---|
292 | } break;
|
---|
293 |
|
---|
294 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
295 | case QImage::Format_ARGB8565_Premultiplied: {
|
---|
296 | uchar *bits = data;
|
---|
297 | uchar *end_bits = data + bytes_per_line;
|
---|
298 |
|
---|
299 | for (int y=0; y<height && !has_alpha_pixels; ++y) {
|
---|
300 | while (bits < end_bits) {
|
---|
301 | has_alpha_pixels |= bits[0] != 0;
|
---|
302 | bits += 3;
|
---|
303 | }
|
---|
304 | bits = end_bits;
|
---|
305 | end_bits += bytes_per_line;
|
---|
306 | }
|
---|
307 | } break;
|
---|
308 |
|
---|
309 | case QImage::Format_ARGB6666_Premultiplied: {
|
---|
310 | uchar *bits = data;
|
---|
311 | uchar *end_bits = data + bytes_per_line;
|
---|
312 |
|
---|
313 | for (int y=0; y<height && !has_alpha_pixels; ++y) {
|
---|
314 | while (bits < end_bits) {
|
---|
315 | has_alpha_pixels |= (bits[0] & 0xfc) != 0;
|
---|
316 | bits += 3;
|
---|
317 | }
|
---|
318 | bits = end_bits;
|
---|
319 | end_bits += bytes_per_line;
|
---|
320 | }
|
---|
321 | } break;
|
---|
322 |
|
---|
323 | case QImage::Format_ARGB4444_Premultiplied: {
|
---|
324 | uchar *bits = data;
|
---|
325 | uchar *end_bits = data + bytes_per_line;
|
---|
326 |
|
---|
327 | for (int y=0; y<height && !has_alpha_pixels; ++y) {
|
---|
328 | while (bits < end_bits) {
|
---|
329 | has_alpha_pixels |= (bits[0] & 0xf0) != 0;
|
---|
330 | bits += 2;
|
---|
331 | }
|
---|
332 | bits = end_bits;
|
---|
333 | end_bits += bytes_per_line;
|
---|
334 | }
|
---|
335 | } break;
|
---|
336 |
|
---|
337 | default:
|
---|
338 | break;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return has_alpha_pixels;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*!
|
---|
345 | \class QImage
|
---|
346 |
|
---|
347 | \ingroup multimedia
|
---|
348 | \ingroup shared
|
---|
349 | \mainclass
|
---|
350 | \reentrant
|
---|
351 |
|
---|
352 | \brief The QImage class provides a hardware-independent image
|
---|
353 | representation that allows direct access to the pixel data, and
|
---|
354 | can be used as a paint device.
|
---|
355 |
|
---|
356 | Qt provides four classes for handling image data: QImage, QPixmap,
|
---|
357 | QBitmap and QPicture. QImage is designed and optimized for I/O,
|
---|
358 | and for direct pixel access and manipulation, while QPixmap is
|
---|
359 | designed and optimized for showing images on screen. QBitmap is
|
---|
360 | only a convenience class that inherits QPixmap, ensuring a
|
---|
361 | depth of 1. Finally, the QPicture class is a paint device that
|
---|
362 | records and replays QPainter commands.
|
---|
363 |
|
---|
364 | Because QImage is a QPaintDevice subclass, QPainter can be used to
|
---|
365 | draw directly onto images. When using QPainter on a QImage, the
|
---|
366 | painting can be performed in another thread than the current GUI
|
---|
367 | thread.
|
---|
368 |
|
---|
369 | The QImage class supports several image formats described by the
|
---|
370 | \l Format enum. These include monochrome, 8-bit, 32-bit and
|
---|
371 | alpha-blended images which are available in all versions of Qt
|
---|
372 | 4.x.
|
---|
373 |
|
---|
374 | QImage provides a collection of functions that can be used to
|
---|
375 | obtain a variety of information about the image. There are also
|
---|
376 | several functions that enables transformation of the image.
|
---|
377 |
|
---|
378 | QImage objects can be passed around by value since the QImage
|
---|
379 | class uses \l{Implicit Data Sharing}{implicit data
|
---|
380 | sharing}. QImage objects can also be streamed and compared.
|
---|
381 |
|
---|
382 | \note If you would like to load QImage objects in a static build of Qt,
|
---|
383 | refer to the \l{How To Create Qt Plugins#Static Plugins}{Plugin HowTo}.
|
---|
384 |
|
---|
385 | \tableofcontents
|
---|
386 |
|
---|
387 | \section1 Reading and Writing Image Files
|
---|
388 |
|
---|
389 | QImage provides several ways of loading an image file: The file
|
---|
390 | can be loaded when constructing the QImage object, or by using the
|
---|
391 | load() or loadFromData() functions later on. QImage also provides
|
---|
392 | the static fromData() function, constructing a QImage from the
|
---|
393 | given data. When loading an image, the file name can either refer
|
---|
394 | to an actual file on disk or to one of the application's embedded
|
---|
395 | resources. See \l{The Qt Resource System} overview for details
|
---|
396 | on how to embed images and other resource files in the
|
---|
397 | application's executable.
|
---|
398 |
|
---|
399 | Simply call the save() function to save a QImage object.
|
---|
400 |
|
---|
401 | The complete list of supported file formats are available through
|
---|
402 | the QImageReader::supportedImageFormats() and
|
---|
403 | QImageWriter::supportedImageFormats() functions. New file formats
|
---|
404 | can be added as plugins. By default, Qt supports the following
|
---|
405 | formats:
|
---|
406 |
|
---|
407 | \table
|
---|
408 | \header \o Format \o Description \o Qt's support
|
---|
409 | \row \o BMP \o Windows Bitmap \o Read/write
|
---|
410 | \row \o GIF \o Graphic Interchange Format (optional) \o Read
|
---|
411 | \row \o JPG \o Joint Photographic Experts Group \o Read/write
|
---|
412 | \row \o JPEG \o Joint Photographic Experts Group \o Read/write
|
---|
413 | \row \o PNG \o Portable Network Graphics \o Read/write
|
---|
414 | \row \o PBM \o Portable Bitmap \o Read
|
---|
415 | \row \o PGM \o Portable Graymap \o Read
|
---|
416 | \row \o PPM \o Portable Pixmap \o Read/write
|
---|
417 | \row \o TIFF \o Tagged Image File Format \o Read/write
|
---|
418 | \row \o XBM \o X11 Bitmap \o Read/write
|
---|
419 | \row \o XPM \o X11 Pixmap \o Read/write
|
---|
420 | \endtable
|
---|
421 |
|
---|
422 | \section1 Image Information
|
---|
423 |
|
---|
424 | QImage provides a collection of functions that can be used to
|
---|
425 | obtain a variety of information about the image:
|
---|
426 |
|
---|
427 | \table
|
---|
428 | \header
|
---|
429 | \o \o Available Functions
|
---|
430 |
|
---|
431 | \row
|
---|
432 | \o Geometry
|
---|
433 | \o
|
---|
434 |
|
---|
435 | The size(), width(), height(), dotsPerMeterX(), and
|
---|
436 | dotsPerMeterY() functions provide information about the image size
|
---|
437 | and aspect ratio.
|
---|
438 |
|
---|
439 | The rect() function returns the image's enclosing rectangle. The
|
---|
440 | valid() function tells if a given pair of coordinates is within
|
---|
441 | this rectangle. The offset() function returns the number of pixels
|
---|
442 | by which the image is intended to be offset by when positioned
|
---|
443 | relative to other images, which also can be manipulated using the
|
---|
444 | setOffset() function.
|
---|
445 |
|
---|
446 | \row
|
---|
447 | \o Colors
|
---|
448 | \o
|
---|
449 |
|
---|
450 | The color of a pixel can be retrieved by passing its coordinates
|
---|
451 | to the pixel() function. The pixel() function returns the color
|
---|
452 | as a QRgb value indepedent of the image's format.
|
---|
453 |
|
---|
454 | In case of monochrome and 8-bit images, the numColors() and
|
---|
455 | colorTable() functions provide information about the color
|
---|
456 | components used to store the image data: The colorTable() function
|
---|
457 | returns the image's entire color table. To obtain a single entry,
|
---|
458 | use the pixelIndex() function to retrieve the pixel index for a
|
---|
459 | given pair of coordinates, then use the color() function to
|
---|
460 | retrieve the color. Note that if you create an 8-bit image
|
---|
461 | manually, you have to set a valid color table on the image as
|
---|
462 | well.
|
---|
463 |
|
---|
464 | The hasAlphaChannel() function tells if the image's format
|
---|
465 | respects the alpha channel, or not. The allGray() and
|
---|
466 | isGrayscale() functions tell whether an image's colors are all
|
---|
467 | shades of gray.
|
---|
468 |
|
---|
469 | See also the \l {QImage#Pixel Manipulation}{Pixel Manipulation}
|
---|
470 | and \l {QImage#Image Transformations}{Image Transformations}
|
---|
471 | sections.
|
---|
472 |
|
---|
473 | \row
|
---|
474 | \o Text
|
---|
475 | \o
|
---|
476 |
|
---|
477 | The text() function returns the image text associated with the
|
---|
478 | given text key. An image's text keys can be retrieved using the
|
---|
479 | textKeys() function. Use the setText() function to alter an
|
---|
480 | image's text.
|
---|
481 |
|
---|
482 | \row
|
---|
483 | \o Low-level information
|
---|
484 | \o
|
---|
485 | The depth() function returns the depth of the image. The supported
|
---|
486 | depths are 1 (monochrome), 8 and 32 (for more information see the
|
---|
487 | \l {QImage#Image Formats}{Image Formats} section).
|
---|
488 |
|
---|
489 | The format(), bytesPerLine(), and numBytes() functions provide
|
---|
490 | low-level information about the data stored in the image.
|
---|
491 |
|
---|
492 | The cacheKey() function returns a number that uniquely
|
---|
493 | identifies the contents of this QImage object.
|
---|
494 | \endtable
|
---|
495 |
|
---|
496 | \section1 Pixel Manipulation
|
---|
497 |
|
---|
498 | The functions used to manipulate an image's pixels depend on the
|
---|
499 | image format. The reason is that monochrome and 8-bit images are
|
---|
500 | index-based and use a color lookup table, while 32-bit images
|
---|
501 | store ARGB values directly. For more information on image formats,
|
---|
502 | see the \l {Image Formats} section.
|
---|
503 |
|
---|
504 | In case of a 32-bit image, the setPixel() function can be used to
|
---|
505 | alter the color of the pixel at the given coordinates to any other
|
---|
506 | color specified as an ARGB quadruplet. To make a suitable QRgb
|
---|
507 | value, use the qRgb() (adding a default alpha component to the
|
---|
508 | given RGB values, i.e. creating an opaque color) or qRgba()
|
---|
509 | function. For example:
|
---|
510 |
|
---|
511 | \table
|
---|
512 | \row
|
---|
513 | \o \inlineimage qimage-32bit_scaled.png
|
---|
514 | \o
|
---|
515 | \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 0
|
---|
516 | \header
|
---|
517 | \o {2,1}32-bit
|
---|
518 | \endtable
|
---|
519 |
|
---|
520 | In case of a 8-bit and monchrome images, the pixel value is only
|
---|
521 | an index from the image's color table. So the setPixel() function
|
---|
522 | can only be used to alter the color of the pixel at the given
|
---|
523 | coordinates to a predefined color from the image's color table,
|
---|
524 | i.e. it can only change the pixel's index value. To alter or add a
|
---|
525 | color to an image's color table, use the setColor() function.
|
---|
526 |
|
---|
527 | An entry in the color table is an ARGB quadruplet encoded as an
|
---|
528 | QRgb value. Use the qRgb() and qRgba() functions to make a
|
---|
529 | suitable QRgb value for use with the setColor() function. For
|
---|
530 | example:
|
---|
531 |
|
---|
532 | \table
|
---|
533 | \row
|
---|
534 | \o \inlineimage qimage-8bit_scaled.png
|
---|
535 | \o
|
---|
536 | \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 1
|
---|
537 | \header
|
---|
538 | \o {2,1} 8-bit
|
---|
539 | \endtable
|
---|
540 |
|
---|
541 | QImage also provide the scanLine() function which returns a
|
---|
542 | pointer to the pixel data at the scanline with the given index,
|
---|
543 | and the bits() function which returns a pointer to the first pixel
|
---|
544 | data (this is equivalent to \c scanLine(0)).
|
---|
545 |
|
---|
546 | \section1 Image Formats
|
---|
547 |
|
---|
548 | Each pixel stored in a QImage is represented by an integer. The
|
---|
549 | size of the integer varies depending on the format. QImage
|
---|
550 | supports several image formats described by the \l Format
|
---|
551 | enum. The monochrome (1-bit), 8-bit and 32-bit images are
|
---|
552 | available in all versions of Qt. In addition Qt for Embedded Linux
|
---|
553 | also supports 2-bit, 4-bit, and 16-bit images. For more information
|
---|
554 | about the Qt Extended specific formats, see the documentation of the \l
|
---|
555 | Format enum.
|
---|
556 |
|
---|
557 | Monochrome images are stored using 1-bit indexes into a color table
|
---|
558 | with at most two colors. There are two different types of
|
---|
559 | monochrome images: big endian (MSB first) or little endian (LSB
|
---|
560 | first) bit order.
|
---|
561 |
|
---|
562 | 8-bit images are stored using 8-bit indexes into a color table,
|
---|
563 | i.e. they have a single byte per pixel. The color table is a
|
---|
564 | QVector<QRgb>, and the QRgb typedef is equivalent to an unsigned
|
---|
565 | int containing an ARGB quadruplet on the format 0xAARRGGBB.
|
---|
566 |
|
---|
567 | 32-bit images have no color table; instead, each pixel contains an
|
---|
568 | QRgb value. There are three different types of 32-bit images
|
---|
569 | storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB
|
---|
570 | values respectively. In the premultiplied format the red, green,
|
---|
571 | and blue channels are multiplied by the alpha component divided by
|
---|
572 | 255.
|
---|
573 |
|
---|
574 | An image's format can be retrieved using the format()
|
---|
575 | function. Use the convertToFormat() functions to convert an image
|
---|
576 | into another format. The allGray() and isGrayscale() functions
|
---|
577 | tell whether a color image can safely be converted to a grayscale
|
---|
578 | image.
|
---|
579 |
|
---|
580 | \section1 Image Transformations
|
---|
581 |
|
---|
582 | QImage supports a number of functions for creating a new image
|
---|
583 | that is a transformed version of the original: The
|
---|
584 | createAlphaMask() function builds and returns a 1-bpp mask from
|
---|
585 | the alpha buffer in this image, and the createHeuristicMask()
|
---|
586 | function creates and returns a 1-bpp heuristic mask for this
|
---|
587 | image. The latter function works by selecting a color from one of
|
---|
588 | the corners, then chipping away pixels of that color starting at
|
---|
589 | all the edges.
|
---|
590 |
|
---|
591 | The mirrored() function returns a mirror of the image in the
|
---|
592 | desired direction, the scaled() returns a copy of the image scaled
|
---|
593 | to a rectangle of the desired measures, the rgbSwapped() function
|
---|
594 | constructs a BGR image from a RGB image, and the alphaChannel()
|
---|
595 | function constructs an image from this image's alpha channel.
|
---|
596 |
|
---|
597 | The scaledToWidth() and scaledToHeight() functions return scaled
|
---|
598 | copies of the image.
|
---|
599 |
|
---|
600 | The transformed() function returns a copy of the image that is
|
---|
601 | transformed with the given transformation matrix and
|
---|
602 | transformation mode: Internally, the transformation matrix is
|
---|
603 | adjusted to compensate for unwanted translation,
|
---|
604 | i.e. transformed() returns the smallest image containing all
|
---|
605 | transformed points of the original image. The static trueMatrix()
|
---|
606 | function returns the actual matrix used for transforming the
|
---|
607 | image.
|
---|
608 |
|
---|
609 | There are also functions for changing attributes of an image
|
---|
610 | in-place:
|
---|
611 |
|
---|
612 | \table
|
---|
613 | \header \o Function \o Description
|
---|
614 | \row
|
---|
615 | \o setAlphaChannel()
|
---|
616 | \o Sets the alpha channel of the image.
|
---|
617 | \row
|
---|
618 | \o setDotsPerMeterX()
|
---|
619 | \o Defines the aspect ratio by setting the number of pixels that fit
|
---|
620 | horizontally in a physical meter.
|
---|
621 | \row
|
---|
622 | \o setDotsPerMeterY()
|
---|
623 | \o Defines the aspect ratio by setting the number of pixels that fit
|
---|
624 | vertically in a physical meter.
|
---|
625 | \row
|
---|
626 | \o fill()
|
---|
627 | \o Fills the entire image with the given pixel value.
|
---|
628 | \row
|
---|
629 | \o invertPixels()
|
---|
630 | \o Inverts all pixel values in the image using the given InvertMode value.
|
---|
631 | \row
|
---|
632 | \o setColorTable()
|
---|
633 | \o Sets the color table used to translate color indexes. Only
|
---|
634 | monochrome and 8-bit formats.
|
---|
635 | \row
|
---|
636 | \o setNumColors()
|
---|
637 | \o Resizes the color table. Only monochrome and 8-bit formats.
|
---|
638 |
|
---|
639 | \endtable
|
---|
640 |
|
---|
641 | \section1 Legal Information
|
---|
642 |
|
---|
643 | For smooth scaling, the transformed() functions use code based on
|
---|
644 | smooth scaling algorithm by Daniel M. Duley.
|
---|
645 |
|
---|
646 | \legalese
|
---|
647 | Copyright (C) 2004, 2005 Daniel M. Duley
|
---|
648 |
|
---|
649 | Redistribution and use in source and binary forms, with or without
|
---|
650 | modification, are permitted provided that the following conditions
|
---|
651 | are met:
|
---|
652 |
|
---|
653 | 1. Redistributions of source code must retain the above copyright
|
---|
654 | notice, this list of conditions and the following disclaimer.
|
---|
655 | 2. Redistributions in binary form must reproduce the above copyright
|
---|
656 | notice, this list of conditions and the following disclaimer in the
|
---|
657 | documentation and/or other materials provided with the distribution.
|
---|
658 |
|
---|
659 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
660 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
661 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
662 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
663 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
664 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
665 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
666 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
667 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
668 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
669 | \endlegalese
|
---|
670 |
|
---|
671 | \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, {Image Composition Example},
|
---|
672 | {Image Viewer Example}, {Scribble Example}, {Pixelator Example}
|
---|
673 | */
|
---|
674 |
|
---|
675 | /*!
|
---|
676 | \enum QImage::Endian
|
---|
677 | \compat
|
---|
678 |
|
---|
679 | This enum type is used to describe the endianness of the CPU and
|
---|
680 | graphics hardware. It is provided here for compatibility with earlier versions of Qt.
|
---|
681 |
|
---|
682 | Use the \l Format enum instead. The \l Format enum specify the
|
---|
683 | endianess for monchrome formats, but for other formats the
|
---|
684 | endianess is not relevant.
|
---|
685 |
|
---|
686 | \value IgnoreEndian Endianness does not matter. Useful for some
|
---|
687 | operations that are independent of endianness.
|
---|
688 | \value BigEndian Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs.
|
---|
689 | \value LittleEndian Least significant bit first or little endian byte order, as on Intel x86.
|
---|
690 | */
|
---|
691 |
|
---|
692 | /*!
|
---|
693 | \enum QImage::InvertMode
|
---|
694 |
|
---|
695 | This enum type is used to describe how pixel values should be
|
---|
696 | inverted in the invertPixels() function.
|
---|
697 |
|
---|
698 | \value InvertRgb Invert only the RGB values and leave the alpha
|
---|
699 | channel unchanged.
|
---|
700 |
|
---|
701 | \value InvertRgba Invert all channels, including the alpha channel.
|
---|
702 |
|
---|
703 | \sa invertPixels()
|
---|
704 | */
|
---|
705 |
|
---|
706 | /*!
|
---|
707 | \enum QImage::Format
|
---|
708 |
|
---|
709 | The following image formats are available in all versions of Qt:
|
---|
710 |
|
---|
711 | \value Format_Invalid The image is invalid.
|
---|
712 | \value Format_Mono The image is stored using 1-bit per pixel. Bytes are
|
---|
713 | packed with the most significant bit (MSB) first.
|
---|
714 | \value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are
|
---|
715 | packed with the less significant bit (LSB) first.
|
---|
716 | \value Format_Indexed8 The image is stored using 8-bit indexes into a colormap.
|
---|
717 | \value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB).
|
---|
718 | \value Format_ARGB32 The image is stored using a 32-bit ARGB format (0xAARRGGBB).
|
---|
719 | \value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit
|
---|
720 | ARGB format (0xAARRGGBB), i.e. the red,
|
---|
721 | green, and blue channels are multiplied
|
---|
722 | by the alpha component divided by 255. (If RR, GG, or BB
|
---|
723 | has a higher value than the alpha channel, the results are
|
---|
724 | undefined.) Certain operations (such as image composition
|
---|
725 | using alpha blending) are faster using premultiplied ARGB32
|
---|
726 | than with plain ARGB32.
|
---|
727 | \value Format_RGB16 The image is stored using a 16-bit RGB format (5-6-5).
|
---|
728 | \value Format_ARGB8565_Premultiplied The image is stored using a
|
---|
729 | premultiplied 24-bit ARGB format (8-5-6-5).
|
---|
730 | \value Format_RGB666 The image is stored using a 24-bit RGB format (6-6-6).
|
---|
731 | The unused most significant bits is always zero.
|
---|
732 | \value Format_ARGB6666_Premultiplied The image is stored using a
|
---|
733 | premultiplied 24-bit ARGB format (6-6-6-6).
|
---|
734 | \value Format_RGB555 The image is stored using a 16-bit RGB format (5-5-5).
|
---|
735 | The unused most significant bit is always zero.
|
---|
736 | \value Format_ARGB8555_Premultiplied The image is stored using a
|
---|
737 | premultiplied 24-bit ARGB format (8-5-5-5).
|
---|
738 | \value Format_RGB888 The image is stored using a 24-bit RGB format (8-8-8).
|
---|
739 | \value Format_RGB444 The image is stored using a 16-bit RGB format (4-4-4).
|
---|
740 | The unused bits are always zero.
|
---|
741 | \value Format_ARGB4444_Premultiplied The image is stored using a
|
---|
742 | premultiplied 16-bit ARGB format (4-4-4-4).
|
---|
743 |
|
---|
744 | \sa format(), convertToFormat()
|
---|
745 | */
|
---|
746 |
|
---|
747 | /*****************************************************************************
|
---|
748 | QImage member functions
|
---|
749 | *****************************************************************************/
|
---|
750 |
|
---|
751 | // table to flip bits
|
---|
752 | static const uchar bitflip[256] = {
|
---|
753 | /*
|
---|
754 | open OUT, "| fmt";
|
---|
755 | for $i (0..255) {
|
---|
756 | print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) |
|
---|
757 | (($i >> 3) & 0x04) | (($i >> 1) & 0x08) |
|
---|
758 | (($i << 7) & 0x80) | (($i << 5) & 0x40) |
|
---|
759 | (($i << 3) & 0x20) | (($i << 1) & 0x10), ", ";
|
---|
760 | }
|
---|
761 | close OUT;
|
---|
762 | */
|
---|
763 | 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
|
---|
764 | 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
|
---|
765 | 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
|
---|
766 | 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
|
---|
767 | 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
|
---|
768 | 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
|
---|
769 | 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
|
---|
770 | 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
|
---|
771 | 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
|
---|
772 | 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
|
---|
773 | 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
|
---|
774 | 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
|
---|
775 | 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
|
---|
776 | 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
|
---|
777 | 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
|
---|
778 | 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
|
---|
779 | };
|
---|
780 |
|
---|
781 | const uchar *qt_get_bitflip_array() // called from QPixmap code
|
---|
782 | {
|
---|
783 | return bitflip;
|
---|
784 | }
|
---|
785 |
|
---|
786 | #if defined(QT3_SUPPORT)
|
---|
787 | static QImage::Format formatFor(int depth, QImage::Endian bitOrder)
|
---|
788 | {
|
---|
789 | QImage::Format format;
|
---|
790 | if (depth == 1) {
|
---|
791 | format = bitOrder == QImage::BigEndian ? QImage::Format_Mono : QImage::Format_MonoLSB;
|
---|
792 | } else if (depth == 8) {
|
---|
793 | format = QImage::Format_Indexed8;
|
---|
794 | } else if (depth == 32) {
|
---|
795 | format = QImage::Format_RGB32;
|
---|
796 | } else if (depth == 24) {
|
---|
797 | format = QImage::Format_RGB888;
|
---|
798 | } else if (depth == 16) {
|
---|
799 | format = QImage::Format_RGB16;
|
---|
800 | } else {
|
---|
801 | qWarning("QImage: Depth %d not supported", depth);
|
---|
802 | format = QImage::Format_Invalid;
|
---|
803 | }
|
---|
804 | return format;
|
---|
805 | }
|
---|
806 | #endif
|
---|
807 |
|
---|
808 | /*!
|
---|
809 | Constructs a null image.
|
---|
810 |
|
---|
811 | \sa isNull()
|
---|
812 | */
|
---|
813 |
|
---|
814 | QImage::QImage()
|
---|
815 | : QPaintDevice()
|
---|
816 | {
|
---|
817 | d = 0;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /*!
|
---|
821 | Constructs an image with the given \a width, \a height and \a
|
---|
822 | format.
|
---|
823 |
|
---|
824 | \warning This will create a QImage with uninitialized data. Call
|
---|
825 | fill() to fill the image with an appropriate pixel value before
|
---|
826 | drawing onto it with QPainter.
|
---|
827 | */
|
---|
828 | QImage::QImage(int width, int height, Format format)
|
---|
829 | : QPaintDevice()
|
---|
830 | {
|
---|
831 | d = QImageData::create(QSize(width, height), format, 0);
|
---|
832 | }
|
---|
833 |
|
---|
834 | /*!
|
---|
835 | Constructs an image with the given \a size and \a format.
|
---|
836 |
|
---|
837 | \warning This will create a QImage with uninitialized data. Call
|
---|
838 | fill() to fill the image with an appropriate pixel value before
|
---|
839 | drawing onto it with QPainter.
|
---|
840 | */
|
---|
841 | QImage::QImage(const QSize &size, Format format)
|
---|
842 | : QPaintDevice()
|
---|
843 | {
|
---|
844 | d = QImageData::create(size, format, 0);
|
---|
845 | }
|
---|
846 |
|
---|
847 |
|
---|
848 |
|
---|
849 | QImageData *QImageData::create(uchar *data, int width, int height, int bpl, QImage::Format format, bool readOnly)
|
---|
850 | {
|
---|
851 | QImageData *d = 0;
|
---|
852 |
|
---|
853 | if (format == QImage::Format_Invalid)
|
---|
854 | return d;
|
---|
855 |
|
---|
856 | if (!checkPixelSize(format)) {
|
---|
857 | qWarning("QImageData::create(): Invalid pixel size for format %i",
|
---|
858 | format);
|
---|
859 | return 0;
|
---|
860 | }
|
---|
861 |
|
---|
862 | const int depth = depthForFormat(format);
|
---|
863 | const int calc_bytes_per_line = ((width * depth + 31)/32) * 4;
|
---|
864 | const int min_bytes_per_line = (width * depth + 7)/8;
|
---|
865 |
|
---|
866 | if (bpl <= 0)
|
---|
867 | bpl = calc_bytes_per_line;
|
---|
868 |
|
---|
869 | if (width <= 0 || height <= 0 || !data
|
---|
870 | || INT_MAX/sizeof(uchar *) < uint(height)
|
---|
871 | || INT_MAX/uint(depth) < uint(width)
|
---|
872 | || bpl <= 0
|
---|
873 | || height <= 0
|
---|
874 | || bpl < min_bytes_per_line
|
---|
875 | || INT_MAX/uint(bpl) < uint(height))
|
---|
876 | return d; // invalid parameter(s)
|
---|
877 |
|
---|
878 | d = new QImageData;
|
---|
879 | d->ref.ref();
|
---|
880 |
|
---|
881 | d->own_data = false;
|
---|
882 | d->ro_data = readOnly;
|
---|
883 | d->data = data;
|
---|
884 | d->width = width;
|
---|
885 | d->height = height;
|
---|
886 | d->depth = depth;
|
---|
887 | d->format = format;
|
---|
888 |
|
---|
889 | d->bytes_per_line = bpl;
|
---|
890 | d->nbytes = d->bytes_per_line * height;
|
---|
891 |
|
---|
892 | return d;
|
---|
893 | }
|
---|
894 |
|
---|
895 | /*!
|
---|
896 | Constructs an image with the given \a width, \a height and \a
|
---|
897 | format, that uses an existing memory buffer, \a data. The \a width
|
---|
898 | and \a height must be specified in pixels, \a data must be 32-bit aligned,
|
---|
899 | and each scanline of data in the image must also be 32-bit aligned.
|
---|
900 |
|
---|
901 | The buffer must remain valid throughout the life of the
|
---|
902 | QImage. The image does not delete the buffer at destruction.
|
---|
903 |
|
---|
904 | If \a format is an indexed color format, the image color table is
|
---|
905 | initially empty and must be sufficiently expanded with
|
---|
906 | setNumColors() or setColorTable() before the image is used.
|
---|
907 | */
|
---|
908 | QImage::QImage(uchar* data, int width, int height, Format format)
|
---|
909 | : QPaintDevice()
|
---|
910 | {
|
---|
911 | d = QImageData::create(data, width, height, 0, format, false);
|
---|
912 | }
|
---|
913 |
|
---|
914 | /*!
|
---|
915 | Constructs an image with the given \a width, \a height and \a
|
---|
916 | format, that uses an existing read-only memory buffer, \a
|
---|
917 | data. The \a width and \a height must be specified in pixels, \a
|
---|
918 | data must be 32-bit aligned, and each scanline of data in the
|
---|
919 | image must also be 32-bit aligned.
|
---|
920 |
|
---|
921 | The buffer must remain valid throughout the life of the QImage and
|
---|
922 | all copies that have not been modified or otherwise detached from
|
---|
923 | the original buffer. The image does not delete the buffer at
|
---|
924 | destruction.
|
---|
925 |
|
---|
926 | If \a format is an indexed color format, the image color table is
|
---|
927 | initially empty and must be sufficiently expanded with
|
---|
928 | setNumColors() or setColorTable() before the image is used.
|
---|
929 |
|
---|
930 | Unlike the similar QImage constructor that takes a non-const data buffer,
|
---|
931 | this version will never alter the contents of the buffer. For example,
|
---|
932 | calling QImage::bits() will return a deep copy of the image, rather than
|
---|
933 | the buffer passed to the constructor. This allows for the efficiency of
|
---|
934 | constructing a QImage from raw data, without the possibility of the raw
|
---|
935 | data being changed.
|
---|
936 | */
|
---|
937 | QImage::QImage(const uchar* data, int width, int height, Format format)
|
---|
938 | : QPaintDevice()
|
---|
939 | {
|
---|
940 | d = QImageData::create(const_cast<uchar*>(data), width, height, 0, format, true);
|
---|
941 | }
|
---|
942 |
|
---|
943 | /*!
|
---|
944 | Constructs an image with the given \a width, \a height and \a
|
---|
945 | format, that uses an existing memory buffer, \a data. The \a width
|
---|
946 | and \a height must be specified in pixels. \a bytesPerLine
|
---|
947 | specifies the number of bytes per line (stride).
|
---|
948 |
|
---|
949 | The buffer must remain valid throughout the life of the
|
---|
950 | QImage. The image does not delete the buffer at destruction.
|
---|
951 |
|
---|
952 | If \a format is an indexed color format, the image color table is
|
---|
953 | initially empty and must be sufficiently expanded with
|
---|
954 | setNumColors() or setColorTable() before the image is used.
|
---|
955 | */
|
---|
956 | QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format)
|
---|
957 | :QPaintDevice()
|
---|
958 | {
|
---|
959 | d = QImageData::create(data, width, height, bytesPerLine, format, false);
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | /*!
|
---|
964 | Constructs an image with the given \a width, \a height and \a
|
---|
965 | format, that uses an existing memory buffer, \a data. The \a width
|
---|
966 | and \a height must be specified in pixels. \a bytesPerLine
|
---|
967 | specifies the number of bytes per line (stride).
|
---|
968 |
|
---|
969 | The buffer must remain valid throughout the life of the
|
---|
970 | QImage. The image does not delete the buffer at destruction.
|
---|
971 |
|
---|
972 | If \a format is an indexed color format, the image color table is
|
---|
973 | initially empty and must be sufficiently expanded with
|
---|
974 | setNumColors() or setColorTable() before the image is used.
|
---|
975 |
|
---|
976 | Unlike the similar QImage constructor that takes a non-const data buffer,
|
---|
977 | this version will never alter the contents of the buffer. For example,
|
---|
978 | calling QImage::bits() will return a deep copy of the image, rather than
|
---|
979 | the buffer passed to the constructor. This allows for the efficiency of
|
---|
980 | constructing a QImage from raw data, without the possibility of the raw
|
---|
981 | data being changed.
|
---|
982 | */
|
---|
983 |
|
---|
984 | QImage::QImage(const uchar *data, int width, int height, int bytesPerLine, Format format)
|
---|
985 | :QPaintDevice()
|
---|
986 | {
|
---|
987 | d = QImageData::create(const_cast<uchar*>(data), width, height, bytesPerLine, format, true);
|
---|
988 | }
|
---|
989 |
|
---|
990 | /*!
|
---|
991 | Constructs an image and tries to load the image from the file with
|
---|
992 | the given \a fileName.
|
---|
993 |
|
---|
994 | The loader attempts to read the image using the specified \a
|
---|
995 | format. If the \a format is not specified (which is the default),
|
---|
996 | the loader probes the file for a header to guess the file format.
|
---|
997 |
|
---|
998 | If the loading of the image failed, this object is a null image.
|
---|
999 |
|
---|
1000 | The file name can either refer to an actual file on disk or to one
|
---|
1001 | of the application's embedded resources. See the
|
---|
1002 | \l{resources.html}{Resource System} overview for details on how to
|
---|
1003 | embed images and other resource files in the application's
|
---|
1004 | executable.
|
---|
1005 |
|
---|
1006 | \sa isNull(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
|
---|
1007 | */
|
---|
1008 |
|
---|
1009 | QImage::QImage(const QString &fileName, const char *format)
|
---|
1010 | : QPaintDevice()
|
---|
1011 | {
|
---|
1012 | d = 0;
|
---|
1013 | load(fileName, format);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /*!
|
---|
1017 | Constructs an image and tries to load the image from the file with
|
---|
1018 | the given \a fileName.
|
---|
1019 |
|
---|
1020 | The loader attempts to read the image using the specified \a
|
---|
1021 | format. If the \a format is not specified (which is the default),
|
---|
1022 | the loader probes the file for a header to guess the file format.
|
---|
1023 |
|
---|
1024 | If the loading of the image failed, this object is a null image.
|
---|
1025 |
|
---|
1026 | The file name can either refer to an actual file on disk or to one
|
---|
1027 | of the application's embedded resources. See the
|
---|
1028 | \l{resources.html}{Resource System} overview for details on how to
|
---|
1029 | embed images and other resource files in the application's
|
---|
1030 | executable.
|
---|
1031 |
|
---|
1032 | You can disable this constructor by defining \c
|
---|
1033 | QT_NO_CAST_FROM_ASCII when you compile your applications. This can
|
---|
1034 | be useful, for example, if you want to ensure that all
|
---|
1035 | user-visible strings go through QObject::tr().
|
---|
1036 |
|
---|
1037 | \sa QString::fromAscii(), isNull(), {QImage#Reading and Writing
|
---|
1038 | Image Files}{Reading and Writing Image Files}
|
---|
1039 | */
|
---|
1040 | #ifndef QT_NO_CAST_FROM_ASCII
|
---|
1041 | QImage::QImage(const char *fileName, const char *format)
|
---|
1042 | : QPaintDevice()
|
---|
1043 | {
|
---|
1044 | // ### Qt 5: if you remove the QImage(const QByteArray &) QT3_SUPPORT
|
---|
1045 | // constructor, remove this constructor as well. The constructor here
|
---|
1046 | // exists so that QImage("foo.png") compiles without ambiguity.
|
---|
1047 | d = 0;
|
---|
1048 | load(QString::fromAscii(fileName), format);
|
---|
1049 | }
|
---|
1050 | #endif
|
---|
1051 |
|
---|
1052 | #ifndef QT_NO_IMAGEFORMAT_XPM
|
---|
1053 | extern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image);
|
---|
1054 |
|
---|
1055 | /*!
|
---|
1056 | Constructs an image from the given \a xpm image.
|
---|
1057 |
|
---|
1058 | Make sure that the image is a valid XPM image. Errors are silently
|
---|
1059 | ignored.
|
---|
1060 |
|
---|
1061 | Note that it's possible to squeeze the XPM variable a little bit
|
---|
1062 | by using an unusual declaration:
|
---|
1063 |
|
---|
1064 | \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 2
|
---|
1065 |
|
---|
1066 | The extra \c const makes the entire definition read-only, which is
|
---|
1067 | slightly more efficient (e.g., when the code is in a shared
|
---|
1068 | library) and able to be stored in ROM with the application.
|
---|
1069 | */
|
---|
1070 |
|
---|
1071 | QImage::QImage(const char * const xpm[])
|
---|
1072 | : QPaintDevice()
|
---|
1073 | {
|
---|
1074 | d = 0;
|
---|
1075 | if (!xpm)
|
---|
1076 | return;
|
---|
1077 | if (!qt_read_xpm_image_or_array(0, xpm, *this))
|
---|
1078 | // Issue: Warning because the constructor may be ambigious
|
---|
1079 | qWarning("QImage::QImage(), XPM is not supported");
|
---|
1080 | }
|
---|
1081 | #endif // QT_NO_IMAGEFORMAT_XPM
|
---|
1082 |
|
---|
1083 | /*!
|
---|
1084 | \fn QImage::QImage(const QByteArray &data)
|
---|
1085 |
|
---|
1086 | Use the static fromData() function instead.
|
---|
1087 |
|
---|
1088 | \oldcode
|
---|
1089 | QByteArray data;
|
---|
1090 | ...
|
---|
1091 | QImage image(data);
|
---|
1092 | \newcode
|
---|
1093 | QByteArray data;
|
---|
1094 | ...
|
---|
1095 | QImage image = QImage::fromData(data);
|
---|
1096 | \endcode
|
---|
1097 | */
|
---|
1098 |
|
---|
1099 |
|
---|
1100 | /*!
|
---|
1101 | Constructs a shallow copy of the given \a image.
|
---|
1102 |
|
---|
1103 | For more information about shallow copies, see the \l {Implicit
|
---|
1104 | Data Sharing} documentation.
|
---|
1105 |
|
---|
1106 | \sa copy()
|
---|
1107 | */
|
---|
1108 |
|
---|
1109 | QImage::QImage(const QImage &image)
|
---|
1110 | : QPaintDevice()
|
---|
1111 | {
|
---|
1112 | d = image.d;
|
---|
1113 | if (d)
|
---|
1114 | d->ref.ref();
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | #ifdef QT3_SUPPORT
|
---|
1118 | /*!
|
---|
1119 | \fn QImage::QImage(int width, int height, int depth, int numColors, Endian bitOrder)
|
---|
1120 |
|
---|
1121 | Constructs an image with the given \a width, \a height, \a depth,
|
---|
1122 | \a numColors colors and \a bitOrder.
|
---|
1123 |
|
---|
1124 | Use the constructor that accepts a width, a height and a format
|
---|
1125 | (i.e. specifying the depth and bit order), in combination with the
|
---|
1126 | setNumColors() function, instead.
|
---|
1127 |
|
---|
1128 | \oldcode
|
---|
1129 | QImage image(width, height, depth, numColors);
|
---|
1130 | \newcode
|
---|
1131 | QImage image(width, height, format);
|
---|
1132 |
|
---|
1133 | // For 8 bit images the default number of colors is 256. If
|
---|
1134 | // another number of colors is required it can be specified
|
---|
1135 | // using the setNumColors() function.
|
---|
1136 | image.setNumColors(numColors);
|
---|
1137 | \endcode
|
---|
1138 | */
|
---|
1139 |
|
---|
1140 | QImage::QImage(int w, int h, int depth, int numColors, Endian bitOrder)
|
---|
1141 | : QPaintDevice()
|
---|
1142 | {
|
---|
1143 | d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), numColors);
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /*!
|
---|
1147 | Constructs an image with the given \a size, \a depth, \a numColors
|
---|
1148 | and \a bitOrder.
|
---|
1149 |
|
---|
1150 | Use the constructor that accepts a size and a format
|
---|
1151 | (i.e. specifying the depth and bit order), in combination with the
|
---|
1152 | setNumColors() function, instead.
|
---|
1153 |
|
---|
1154 | \oldcode
|
---|
1155 | QSize mySize(width, height);
|
---|
1156 | QImage image(mySize, depth, numColors);
|
---|
1157 | \newcode
|
---|
1158 | QSize mySize(width, height);
|
---|
1159 | QImage image(mySize, format);
|
---|
1160 |
|
---|
1161 | // For 8 bit images the default number of colors is 256. If
|
---|
1162 | // another number of colors is required it can be specified
|
---|
1163 | // using the setNumColors() function.
|
---|
1164 | image.setNumColors(numColors);
|
---|
1165 | \endcode
|
---|
1166 | */
|
---|
1167 | QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
|
---|
1168 | : QPaintDevice()
|
---|
1169 | {
|
---|
1170 | d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /*!
|
---|
1174 | \fn QImage::QImage(uchar* data, int width, int height, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
|
---|
1175 |
|
---|
1176 | Constructs an image with the given \a width, \a height, depth, \a
|
---|
1177 | colortable, \a numColors and \a bitOrder, that uses an existing
|
---|
1178 | memory buffer, \a data.
|
---|
1179 |
|
---|
1180 | Use the constructor that accepts a uchar pointer, a width, a
|
---|
1181 | height and a format (i.e. specifying the depth and bit order), in
|
---|
1182 | combination with the setColorTable() function, instead.
|
---|
1183 |
|
---|
1184 | \oldcode
|
---|
1185 | uchar *myData;
|
---|
1186 | QRgb *myColorTable;
|
---|
1187 |
|
---|
1188 | QImage image(myData, width, height, depth,
|
---|
1189 | myColorTable, numColors, IgnoreEndian);
|
---|
1190 | \newcode
|
---|
1191 | uchar *myData;
|
---|
1192 | QVector<QRgb> myColorTable;
|
---|
1193 |
|
---|
1194 | QImage image(myData, width, height, format);
|
---|
1195 | image.setColorTable(myColorTable);
|
---|
1196 | \endcode
|
---|
1197 | */
|
---|
1198 | QImage::QImage(uchar* data, int w, int h, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
|
---|
1199 | : QPaintDevice()
|
---|
1200 | {
|
---|
1201 | d = 0;
|
---|
1202 | Format f = formatFor(depth, bitOrder);
|
---|
1203 | if (f == Format_Invalid)
|
---|
1204 | return;
|
---|
1205 |
|
---|
1206 | const int bytes_per_line = ((w*depth+31)/32)*4; // bytes per scanline
|
---|
1207 | if (w <= 0 || h <= 0 || numColors < 0 || !data
|
---|
1208 | || INT_MAX/sizeof(uchar *) < uint(h)
|
---|
1209 | || INT_MAX/uint(depth) < uint(w)
|
---|
1210 | || bytes_per_line <= 0
|
---|
1211 | || INT_MAX/uint(bytes_per_line) < uint(h))
|
---|
1212 | return; // invalid parameter(s)
|
---|
1213 | d = new QImageData;
|
---|
1214 | d->ref.ref();
|
---|
1215 |
|
---|
1216 | d->own_data = false;
|
---|
1217 | d->data = data;
|
---|
1218 | d->width = w;
|
---|
1219 | d->height = h;
|
---|
1220 | d->depth = depth;
|
---|
1221 | d->format = f;
|
---|
1222 | if (depth == 32)
|
---|
1223 | numColors = 0;
|
---|
1224 |
|
---|
1225 | d->bytes_per_line = bytes_per_line;
|
---|
1226 | d->nbytes = d->bytes_per_line * h;
|
---|
1227 | if (colortable) {
|
---|
1228 | d->colortable.resize(numColors);
|
---|
1229 | for (int i = 0; i < numColors; ++i)
|
---|
1230 | d->colortable[i] = colortable[i];
|
---|
1231 | } else if (numColors) {
|
---|
1232 | setNumColors(numColors);
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | #ifdef Q_WS_QWS
|
---|
1237 |
|
---|
1238 | /*!
|
---|
1239 | \fn QImage::QImage(uchar* data, int width, int height, int depth, int bytesPerLine, const QRgb* colortable, int numColors, Endian bitOrder)
|
---|
1240 |
|
---|
1241 | Constructs an image with the given \a width, \a height, \a depth,
|
---|
1242 | \a bytesPerLine, \a colortable, \a numColors and \a bitOrder, that
|
---|
1243 | uses an existing memory buffer, \a data. The image does not delete
|
---|
1244 | the buffer at destruction.
|
---|
1245 |
|
---|
1246 | \warning This constructor is only available in Qt for Embedded Linux.
|
---|
1247 |
|
---|
1248 | The data has to be 32-bit aligned, and each scanline of data in the image
|
---|
1249 | must also be 32-bit aligned, so it's no longer possible to specify a custom
|
---|
1250 | \a bytesPerLine value.
|
---|
1251 | */
|
---|
1252 | QImage::QImage(uchar* data, int w, int h, int depth, int bpl, const QRgb* colortable, int numColors, Endian bitOrder)
|
---|
1253 | : QPaintDevice()
|
---|
1254 | {
|
---|
1255 | d = 0;
|
---|
1256 | Format f = formatFor(depth, bitOrder);
|
---|
1257 | if (f == Format_Invalid)
|
---|
1258 | return;
|
---|
1259 | if (!data || w <= 0 || h <= 0 || depth <= 0 || numColors < 0
|
---|
1260 | || INT_MAX/sizeof(uchar *) < uint(h)
|
---|
1261 | || INT_MAX/uint(depth) < uint(w)
|
---|
1262 | || bpl <= 0
|
---|
1263 | || INT_MAX/uint(bpl) < uint(h))
|
---|
1264 | return; // invalid parameter(s)
|
---|
1265 |
|
---|
1266 | d = new QImageData;
|
---|
1267 | d->ref.ref();
|
---|
1268 | d->own_data = false;
|
---|
1269 | d->data = data;
|
---|
1270 | d->width = w;
|
---|
1271 | d->height = h;
|
---|
1272 | d->depth = depth;
|
---|
1273 | d->format = f;
|
---|
1274 | if (depth == 32)
|
---|
1275 | numColors = 0;
|
---|
1276 | d->bytes_per_line = bpl;
|
---|
1277 | d->nbytes = d->bytes_per_line * h;
|
---|
1278 | if (colortable) {
|
---|
1279 | d->colortable.resize(numColors);
|
---|
1280 | for (int i = 0; i < numColors; ++i)
|
---|
1281 | d->colortable[i] = colortable[i];
|
---|
1282 | } else if (numColors) {
|
---|
1283 | setNumColors(numColors);
|
---|
1284 | }
|
---|
1285 | }
|
---|
1286 | #endif // Q_WS_QWS
|
---|
1287 | #endif // QT3_SUPPORT
|
---|
1288 |
|
---|
1289 | /*!
|
---|
1290 | Destroys the image and cleans up.
|
---|
1291 | */
|
---|
1292 |
|
---|
1293 | QImage::~QImage()
|
---|
1294 | {
|
---|
1295 | if (d && !d->ref.deref())
|
---|
1296 | delete d;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /*!
|
---|
1300 | Assigns a shallow copy of the given \a image to this image and
|
---|
1301 | returns a reference to this image.
|
---|
1302 |
|
---|
1303 | For more information about shallow copies, see the \l {Implicit
|
---|
1304 | Data Sharing} documentation.
|
---|
1305 |
|
---|
1306 | \sa copy(), QImage()
|
---|
1307 | */
|
---|
1308 |
|
---|
1309 | QImage &QImage::operator=(const QImage &image)
|
---|
1310 | {
|
---|
1311 | if (image.d)
|
---|
1312 | image.d->ref.ref();
|
---|
1313 | if (d && !d->ref.deref())
|
---|
1314 | delete d;
|
---|
1315 | d = image.d;
|
---|
1316 | return *this;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | /*!
|
---|
1320 | \internal
|
---|
1321 | */
|
---|
1322 | int QImage::devType() const
|
---|
1323 | {
|
---|
1324 | return QInternal::Image;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | /*!
|
---|
1328 | Returns the image as a QVariant.
|
---|
1329 | */
|
---|
1330 | QImage::operator QVariant() const
|
---|
1331 | {
|
---|
1332 | return QVariant(QVariant::Image, this);
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /*!
|
---|
1336 | \internal
|
---|
1337 |
|
---|
1338 | If multiple images share common data, this image makes a copy of
|
---|
1339 | the data and detaches itself from the sharing mechanism, making
|
---|
1340 | sure that this image is the only one referring to the data.
|
---|
1341 |
|
---|
1342 | Nothing is done if there is just a single reference.
|
---|
1343 |
|
---|
1344 | \sa copy(), isDetached(), {Implicit Data Sharing}
|
---|
1345 | */
|
---|
1346 | void QImage::detach()
|
---|
1347 | {
|
---|
1348 | if (d) {
|
---|
1349 | if (d->is_cached && qt_image_cleanup_hook_64 && d->ref == 1)
|
---|
1350 | qt_image_cleanup_hook_64(cacheKey());
|
---|
1351 |
|
---|
1352 | if (d->ref != 1 || d->ro_data)
|
---|
1353 | *this = copy();
|
---|
1354 |
|
---|
1355 | if (d)
|
---|
1356 | ++d->detach_no;
|
---|
1357 | }
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 |
|
---|
1361 | /*!
|
---|
1362 | \fn QImage QImage::copy(int x, int y, int width, int height) const
|
---|
1363 | \overload
|
---|
1364 |
|
---|
1365 | The returned image is copied from the position (\a x, \a y) in
|
---|
1366 | this image, and will always have the given \a width and \a height.
|
---|
1367 | In areas beyond this image, pixels are set to 0.
|
---|
1368 |
|
---|
1369 | */
|
---|
1370 |
|
---|
1371 | /*!
|
---|
1372 | \fn QImage QImage::copy(const QRect& rectangle) const
|
---|
1373 |
|
---|
1374 | Returns a sub-area of the image as a new image.
|
---|
1375 |
|
---|
1376 | The returned image is copied from the position (\a
|
---|
1377 | {rectangle}.x(), \a{rectangle}.y()) in this image, and will always
|
---|
1378 | have the size of the given \a rectangle.
|
---|
1379 |
|
---|
1380 | In areas beyond this image, pixels are set to 0. For 32-bit RGB
|
---|
1381 | images, this means black; for 32-bit ARGB images, this means
|
---|
1382 | transparent black; for 8-bit images, this means the color with
|
---|
1383 | index 0 in the color table which can be anything; for 1-bit
|
---|
1384 | images, this means Qt::color0.
|
---|
1385 |
|
---|
1386 | If the given \a rectangle is a null rectangle the entire image is
|
---|
1387 | copied.
|
---|
1388 |
|
---|
1389 | \sa QImage()
|
---|
1390 | */
|
---|
1391 | QImage QImage::copy(const QRect& r) const
|
---|
1392 | {
|
---|
1393 | if (!d)
|
---|
1394 | return QImage();
|
---|
1395 |
|
---|
1396 | if (r.isNull()) {
|
---|
1397 | QImage image(d->width, d->height, d->format);
|
---|
1398 | if (image.isNull())
|
---|
1399 | return image;
|
---|
1400 |
|
---|
1401 | // Qt for Embedded Linux can create images with non-default bpl
|
---|
1402 | // make sure we don't crash.
|
---|
1403 | if (image.d->nbytes != d->nbytes) {
|
---|
1404 | int bpl = image.bytesPerLine();
|
---|
1405 | for (int i = 0; i < height(); i++)
|
---|
1406 | memcpy(image.scanLine(i), scanLine(i), bpl);
|
---|
1407 | } else
|
---|
1408 | memcpy(image.bits(), bits(), d->nbytes);
|
---|
1409 | image.d->colortable = d->colortable;
|
---|
1410 | image.d->dpmx = d->dpmx;
|
---|
1411 | image.d->dpmy = d->dpmy;
|
---|
1412 | image.d->offset = d->offset;
|
---|
1413 | image.d->has_alpha_clut = d->has_alpha_clut;
|
---|
1414 | #ifndef QT_NO_IMAGE_TEXT
|
---|
1415 | image.d->text = d->text;
|
---|
1416 | #endif
|
---|
1417 | return image;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | int x = r.x();
|
---|
1421 | int y = r.y();
|
---|
1422 | int w = r.width();
|
---|
1423 | int h = r.height();
|
---|
1424 |
|
---|
1425 | int dx = 0;
|
---|
1426 | int dy = 0;
|
---|
1427 | if (w <= 0 || h <= 0)
|
---|
1428 | return QImage();
|
---|
1429 |
|
---|
1430 | QImage image(w, h, d->format);
|
---|
1431 | if (image.isNull())
|
---|
1432 | return image;
|
---|
1433 |
|
---|
1434 | if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) {
|
---|
1435 | // bitBlt will not cover entire image - clear it.
|
---|
1436 | image.fill(0);
|
---|
1437 | if (x < 0) {
|
---|
1438 | dx = -x;
|
---|
1439 | x = 0;
|
---|
1440 | }
|
---|
1441 | if (y < 0) {
|
---|
1442 | dy = -y;
|
---|
1443 | y = 0;
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | image.d->colortable = d->colortable;
|
---|
1448 |
|
---|
1449 | int pixels_to_copy = qMax(w - dx, 0);
|
---|
1450 | if (x > d->width)
|
---|
1451 | pixels_to_copy = 0;
|
---|
1452 | else if (pixels_to_copy > d->width - x)
|
---|
1453 | pixels_to_copy = d->width - x;
|
---|
1454 | int lines_to_copy = qMax(h - dy, 0);
|
---|
1455 | if (y > d->height)
|
---|
1456 | lines_to_copy = 0;
|
---|
1457 | else if (lines_to_copy > d->height - y)
|
---|
1458 | lines_to_copy = d->height - y;
|
---|
1459 |
|
---|
1460 | bool byteAligned = true;
|
---|
1461 | if (d->format == Format_Mono || d->format == Format_MonoLSB)
|
---|
1462 | byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7);
|
---|
1463 |
|
---|
1464 | if (byteAligned) {
|
---|
1465 | const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line;
|
---|
1466 | uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line;
|
---|
1467 | const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3;
|
---|
1468 | for (int i = 0; i < lines_to_copy; ++i) {
|
---|
1469 | memcpy(dest, src, bytes_to_copy);
|
---|
1470 | src += d->bytes_per_line;
|
---|
1471 | dest += image.d->bytes_per_line;
|
---|
1472 | }
|
---|
1473 | } else if (d->format == Format_Mono) {
|
---|
1474 | const uchar *src = d->data + y * d->bytes_per_line;
|
---|
1475 | uchar *dest = image.d->data + dy * image.d->bytes_per_line;
|
---|
1476 | for (int i = 0; i < lines_to_copy; ++i) {
|
---|
1477 | for (int j = 0; j < pixels_to_copy; ++j) {
|
---|
1478 | if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7)))
|
---|
1479 | dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7));
|
---|
1480 | else
|
---|
1481 | dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7));
|
---|
1482 | }
|
---|
1483 | src += d->bytes_per_line;
|
---|
1484 | dest += image.d->bytes_per_line;
|
---|
1485 | }
|
---|
1486 | } else { // Format_MonoLSB
|
---|
1487 | Q_ASSERT(d->format == Format_MonoLSB);
|
---|
1488 | const uchar *src = d->data + y * d->bytes_per_line;
|
---|
1489 | uchar *dest = image.d->data + dy * image.d->bytes_per_line;
|
---|
1490 | for (int i = 0; i < lines_to_copy; ++i) {
|
---|
1491 | for (int j = 0; j < pixels_to_copy; ++j) {
|
---|
1492 | if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7)))
|
---|
1493 | dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7));
|
---|
1494 | else
|
---|
1495 | dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7));
|
---|
1496 | }
|
---|
1497 | src += d->bytes_per_line;
|
---|
1498 | dest += image.d->bytes_per_line;
|
---|
1499 | }
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | image.d->dpmx = dotsPerMeterX();
|
---|
1503 | image.d->dpmy = dotsPerMeterY();
|
---|
1504 | image.d->offset = offset();
|
---|
1505 | image.d->has_alpha_clut = d->has_alpha_clut;
|
---|
1506 | #ifndef QT_NO_IMAGE_TEXT
|
---|
1507 | image.d->text = d->text;
|
---|
1508 | #endif
|
---|
1509 | return image;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 |
|
---|
1513 | /*!
|
---|
1514 | \fn bool QImage::isNull() const
|
---|
1515 |
|
---|
1516 | Returns true if it is a null image, otherwise returns false.
|
---|
1517 |
|
---|
1518 | A null image has all parameters set to zero and no allocated data.
|
---|
1519 | */
|
---|
1520 | bool QImage::isNull() const
|
---|
1521 | {
|
---|
1522 | return !d;
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | /*!
|
---|
1526 | \fn int QImage::width() const
|
---|
1527 |
|
---|
1528 | Returns the width of the image.
|
---|
1529 |
|
---|
1530 | \sa {QImage#Image Information}{Image Information}
|
---|
1531 | */
|
---|
1532 | int QImage::width() const
|
---|
1533 | {
|
---|
1534 | return d ? d->width : 0;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | /*!
|
---|
1538 | \fn int QImage::height() const
|
---|
1539 |
|
---|
1540 | Returns the height of the image.
|
---|
1541 |
|
---|
1542 | \sa {QImage#Image Information}{Image Information}
|
---|
1543 | */
|
---|
1544 | int QImage::height() const
|
---|
1545 | {
|
---|
1546 | return d ? d->height : 0;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /*!
|
---|
1550 | \fn QSize QImage::size() const
|
---|
1551 |
|
---|
1552 | Returns the size of the image, i.e. its width() and height().
|
---|
1553 |
|
---|
1554 | \sa {QImage#Image Information}{Image Information}
|
---|
1555 | */
|
---|
1556 | QSize QImage::size() const
|
---|
1557 | {
|
---|
1558 | return d ? QSize(d->width, d->height) : QSize(0, 0);
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | /*!
|
---|
1562 | \fn QRect QImage::rect() const
|
---|
1563 |
|
---|
1564 | Returns the enclosing rectangle (0, 0, width(), height()) of the
|
---|
1565 | image.
|
---|
1566 |
|
---|
1567 | \sa {QImage#Image Information}{Image Information}
|
---|
1568 | */
|
---|
1569 | QRect QImage::rect() const
|
---|
1570 | {
|
---|
1571 | return d ? QRect(0, 0, d->width, d->height) : QRect();
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | /*!
|
---|
1575 | Returns the depth of the image.
|
---|
1576 |
|
---|
1577 | The image depth is the number of bits used to encode a single
|
---|
1578 | pixel, also called bits per pixel (bpp).
|
---|
1579 |
|
---|
1580 | The supported depths are 1, 8, 16, 24 and 32.
|
---|
1581 |
|
---|
1582 | \sa convertToFormat(), {QImage#Image Formats}{Image Formats},
|
---|
1583 | {QImage#Image Information}{Image Information}
|
---|
1584 |
|
---|
1585 | */
|
---|
1586 | int QImage::depth() const
|
---|
1587 | {
|
---|
1588 | return d ? d->depth : 0;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | /*!
|
---|
1592 | \fn int QImage::numColors() const
|
---|
1593 |
|
---|
1594 | Returns the size of the color table for the image.
|
---|
1595 |
|
---|
1596 | Notice that numColors() returns 0 for 32-bpp images because these
|
---|
1597 | images do not use color tables, but instead encode pixel values as
|
---|
1598 | ARGB quadruplets.
|
---|
1599 |
|
---|
1600 | \sa setNumColors(), {QImage#Image Information}{Image Information}
|
---|
1601 | */
|
---|
1602 | int QImage::numColors() const
|
---|
1603 | {
|
---|
1604 | return d ? d->colortable.size() : 0;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 |
|
---|
1608 | #ifdef QT3_SUPPORT
|
---|
1609 | /*!
|
---|
1610 | \fn QImage::Endian QImage::bitOrder() const
|
---|
1611 |
|
---|
1612 | Returns the bit order for the image. If it is a 1-bpp image, this
|
---|
1613 | function returns either QImage::BigEndian or
|
---|
1614 | QImage::LittleEndian. Otherwise, this function returns
|
---|
1615 | QImage::IgnoreEndian.
|
---|
1616 |
|
---|
1617 | Use the format() function instead for the monochrome formats. For
|
---|
1618 | non-monochrome formats the bit order is irrelevant.
|
---|
1619 | */
|
---|
1620 |
|
---|
1621 | /*!
|
---|
1622 | Returns a pointer to the scanline pointer table. This is the
|
---|
1623 | beginning of the data block for the image.
|
---|
1624 |
|
---|
1625 | Use the bits() or scanLine() function instead.
|
---|
1626 | */
|
---|
1627 | uchar **QImage::jumpTable()
|
---|
1628 | {
|
---|
1629 | if (!d)
|
---|
1630 | return 0;
|
---|
1631 | detach();
|
---|
1632 |
|
---|
1633 | // in case detach() ran out of memory..
|
---|
1634 | if (!d)
|
---|
1635 | return 0;
|
---|
1636 |
|
---|
1637 | if (!d->jumptable) {
|
---|
1638 | d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
|
---|
1639 | uchar *data = d->data;
|
---|
1640 | int height = d->height;
|
---|
1641 | uchar **p = d->jumptable;
|
---|
1642 | while (height--) {
|
---|
1643 | *p++ = data;
|
---|
1644 | data += d->bytes_per_line;
|
---|
1645 | }
|
---|
1646 | }
|
---|
1647 | return d->jumptable;
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | /*!
|
---|
1651 | \overload
|
---|
1652 | */
|
---|
1653 | const uchar * const *QImage::jumpTable() const
|
---|
1654 | {
|
---|
1655 | if (!d)
|
---|
1656 | return 0;
|
---|
1657 | if (!d->jumptable) {
|
---|
1658 | d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
|
---|
1659 | uchar *data = d->data;
|
---|
1660 | int height = d->height;
|
---|
1661 | uchar **p = d->jumptable;
|
---|
1662 | while (height--) {
|
---|
1663 | *p++ = data;
|
---|
1664 | data += d->bytes_per_line;
|
---|
1665 | }
|
---|
1666 | }
|
---|
1667 | return d->jumptable;
|
---|
1668 | }
|
---|
1669 | #endif
|
---|
1670 |
|
---|
1671 | /*!
|
---|
1672 | Sets the color table used to translate color indexes to QRgb
|
---|
1673 | values, to the specified \a colors.
|
---|
1674 |
|
---|
1675 | When the image is used, the color table must be large enough to
|
---|
1676 | have entries for all the pixel/index values present in the image,
|
---|
1677 | otherwise the results are undefined.
|
---|
1678 |
|
---|
1679 | \sa colorTable(), setColor(), {QImage#Image Transformations}{Image
|
---|
1680 | Transformations}
|
---|
1681 | */
|
---|
1682 | void QImage::setColorTable(const QVector<QRgb> colors)
|
---|
1683 | {
|
---|
1684 | if (!d)
|
---|
1685 | return;
|
---|
1686 | detach();
|
---|
1687 |
|
---|
1688 | // In case detach() ran out of memory
|
---|
1689 | if (!d)
|
---|
1690 | return;
|
---|
1691 |
|
---|
1692 | d->colortable = colors;
|
---|
1693 | d->has_alpha_clut = false;
|
---|
1694 | for (int i = 0; i < d->colortable.size(); ++i)
|
---|
1695 | d->has_alpha_clut |= (qAlpha(d->colortable.at(i)) != 255);
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | /*!
|
---|
1699 | Returns a list of the colors contained in the image's color table,
|
---|
1700 | or an empty list if the image does not have a color table
|
---|
1701 |
|
---|
1702 | \sa setColorTable(), numColors(), color()
|
---|
1703 | */
|
---|
1704 | QVector<QRgb> QImage::colorTable() const
|
---|
1705 | {
|
---|
1706 | return d ? d->colortable : QVector<QRgb>();
|
---|
1707 | }
|
---|
1708 |
|
---|
1709 |
|
---|
1710 | /*!
|
---|
1711 | Returns the number of bytes occupied by the image data.
|
---|
1712 |
|
---|
1713 | \sa bytesPerLine(), bits(), {QImage#Image Information}{Image
|
---|
1714 | Information}
|
---|
1715 | */
|
---|
1716 | int QImage::numBytes() const
|
---|
1717 | {
|
---|
1718 | return d ? d->nbytes : 0;
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | /*!
|
---|
1722 | Returns the number of bytes per image scanline.
|
---|
1723 |
|
---|
1724 | This is equivalent to numBytes()/ height().
|
---|
1725 |
|
---|
1726 | \sa scanLine()
|
---|
1727 | */
|
---|
1728 | int QImage::bytesPerLine() const
|
---|
1729 | {
|
---|
1730 | return (d && d->height) ? d->nbytes / d->height : 0;
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 |
|
---|
1734 | /*!
|
---|
1735 | Returns the color in the color table at index \a i. The first
|
---|
1736 | color is at index 0.
|
---|
1737 |
|
---|
1738 | The colors in an image's color table are specified as ARGB
|
---|
1739 | quadruplets (QRgb). Use the qAlpha(), qRed(), qGreen(), and
|
---|
1740 | qBlue() functions to get the color value components.
|
---|
1741 |
|
---|
1742 | \sa setColor(), pixelIndex(), {QImage#Pixel Manipulation}{Pixel
|
---|
1743 | Manipulation}
|
---|
1744 | */
|
---|
1745 | QRgb QImage::color(int i) const
|
---|
1746 | {
|
---|
1747 | Q_ASSERT(i < numColors());
|
---|
1748 | return d ? d->colortable.at(i) : QRgb(uint(-1));
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | /*!
|
---|
1752 | \fn void QImage::setColor(int index, QRgb colorValue)
|
---|
1753 |
|
---|
1754 | Sets the color at the given \a index in the color table, to the
|
---|
1755 | given to \a colorValue. The color value is an ARGB quadruplet.
|
---|
1756 |
|
---|
1757 | If \a index is outside the current size of the color table, it is
|
---|
1758 | expanded with setNumColors().
|
---|
1759 |
|
---|
1760 | \sa color(), numColors(), setColorTable(), {QImage#Pixel Manipulation}{Pixel
|
---|
1761 | Manipulation}
|
---|
1762 | */
|
---|
1763 | void QImage::setColor(int i, QRgb c)
|
---|
1764 | {
|
---|
1765 | if (!d)
|
---|
1766 | return;
|
---|
1767 | if (i < 0 || d->depth > 8 || i >= 1<<d->depth) {
|
---|
1768 | qWarning("QImage::setColor: Index out of bound %d", i);
|
---|
1769 | return;
|
---|
1770 | }
|
---|
1771 | detach();
|
---|
1772 |
|
---|
1773 | // In case detach() run out of memory
|
---|
1774 | if (!d)
|
---|
1775 | return;
|
---|
1776 |
|
---|
1777 | if (i >= d->colortable.size())
|
---|
1778 | setNumColors(i+1);
|
---|
1779 | d->colortable[i] = c;
|
---|
1780 | d->has_alpha_clut |= (qAlpha(c) != 255);
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 | /*!
|
---|
1784 | Returns a pointer to the pixel data at the scanline with index \a
|
---|
1785 | i. The first scanline is at index 0.
|
---|
1786 |
|
---|
1787 | The scanline data is aligned on a 32-bit boundary.
|
---|
1788 |
|
---|
1789 | \warning If you are accessing 32-bpp image data, cast the returned
|
---|
1790 | pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to
|
---|
1791 | read/write the pixel value. You cannot use the \c{uchar*} pointer
|
---|
1792 | directly, because the pixel format depends on the byte order on
|
---|
1793 | the underlying platform. Use qRed(), qGreen(), qBlue(), and
|
---|
1794 | qAlpha() to access the pixels.
|
---|
1795 |
|
---|
1796 | \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
|
---|
1797 | Manipulation}
|
---|
1798 | */
|
---|
1799 | uchar *QImage::scanLine(int i)
|
---|
1800 | {
|
---|
1801 | if (!d)
|
---|
1802 | return 0;
|
---|
1803 |
|
---|
1804 | detach();
|
---|
1805 |
|
---|
1806 | // In case detach() ran out of memory
|
---|
1807 | if (!d)
|
---|
1808 | return 0;
|
---|
1809 |
|
---|
1810 | return d->data + i * d->bytes_per_line;
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | /*!
|
---|
1814 | \overload
|
---|
1815 | */
|
---|
1816 | const uchar *QImage::scanLine(int i) const
|
---|
1817 | {
|
---|
1818 | if (!d)
|
---|
1819 | return 0;
|
---|
1820 |
|
---|
1821 | Q_ASSERT(i >= 0 && i < height());
|
---|
1822 | return d->data + i * d->bytes_per_line;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 |
|
---|
1826 | /*!
|
---|
1827 | Returns a pointer to the first pixel data. This is equivalent to
|
---|
1828 | scanLine(0).
|
---|
1829 |
|
---|
1830 | Note that QImage uses \l{Implicit Data Sharing} {implicit data
|
---|
1831 | sharing}. This function performs a deep copy of the shared pixel
|
---|
1832 | data, thus ensuring that this QImage is the only one using the
|
---|
1833 | current return value.
|
---|
1834 |
|
---|
1835 | \sa scanLine(), numBytes()
|
---|
1836 | */
|
---|
1837 | uchar *QImage::bits()
|
---|
1838 | {
|
---|
1839 | if (!d)
|
---|
1840 | return 0;
|
---|
1841 | detach();
|
---|
1842 |
|
---|
1843 | // In case detach ran out of memory...
|
---|
1844 | if (!d)
|
---|
1845 | return 0;
|
---|
1846 |
|
---|
1847 | return d->data;
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | /*!
|
---|
1851 | \overload
|
---|
1852 |
|
---|
1853 | Note that QImage uses \l{Implicit Data Sharing} {implicit data
|
---|
1854 | sharing}, but this function does \e not perform a deep copy of the
|
---|
1855 | shared pixel data, because the returned data is const.
|
---|
1856 | */
|
---|
1857 | const uchar *QImage::bits() const
|
---|
1858 | {
|
---|
1859 | return d ? d->data : 0;
|
---|
1860 | }
|
---|
1861 |
|
---|
1862 |
|
---|
1863 |
|
---|
1864 | /*!
|
---|
1865 | \fn void QImage::reset()
|
---|
1866 |
|
---|
1867 | Resets all image parameters and deallocates the image data.
|
---|
1868 |
|
---|
1869 | Assign a null image instead.
|
---|
1870 |
|
---|
1871 | \oldcode
|
---|
1872 | QImage image;
|
---|
1873 | image.reset();
|
---|
1874 | \newcode
|
---|
1875 | QImage image;
|
---|
1876 | image = QImage();
|
---|
1877 | \endcode
|
---|
1878 | */
|
---|
1879 |
|
---|
1880 | /*!
|
---|
1881 | \fn void QImage::fill(uint pixelValue)
|
---|
1882 |
|
---|
1883 | Fills the entire image with the given \a pixelValue.
|
---|
1884 |
|
---|
1885 | If the depth of this image is 1, only the lowest bit is used. If
|
---|
1886 | you say fill(0), fill(2), etc., the image is filled with 0s. If
|
---|
1887 | you say fill(1), fill(3), etc., the image is filled with 1s. If
|
---|
1888 | the depth is 8, the lowest 8 bits are used and if the depth is 16
|
---|
1889 | the lowest 16 bits are used.
|
---|
1890 |
|
---|
1891 | Note: QImage::pixel() returns the color of the pixel at the given
|
---|
1892 | coordinates while QColor::pixel() returns the pixel value of the
|
---|
1893 | underlying window system (essentially an index value), so normally
|
---|
1894 | you will want to use QImage::pixel() to use a color from an
|
---|
1895 | existing image or QColor::rgb() to use a specific color.
|
---|
1896 |
|
---|
1897 | \sa depth(), {QImage#Image Transformations}{Image Transformations}
|
---|
1898 | */
|
---|
1899 |
|
---|
1900 | void QImage::fill(uint pixel)
|
---|
1901 | {
|
---|
1902 | if (!d)
|
---|
1903 | return;
|
---|
1904 |
|
---|
1905 | detach();
|
---|
1906 |
|
---|
1907 | // In case detach() ran out of memory
|
---|
1908 | if (!d)
|
---|
1909 | return;
|
---|
1910 |
|
---|
1911 | if (d->depth == 1 || d->depth == 8) {
|
---|
1912 | int w = d->width;
|
---|
1913 | if (d->depth == 1) {
|
---|
1914 | if (pixel & 1)
|
---|
1915 | pixel = 0xffffffff;
|
---|
1916 | else
|
---|
1917 | pixel = 0;
|
---|
1918 | w = (w + 7) / 8;
|
---|
1919 | } else {
|
---|
1920 | pixel &= 0xff;
|
---|
1921 | }
|
---|
1922 | qt_rectfill<quint8>(d->data, pixel, 0, 0,
|
---|
1923 | w, d->height, d->bytes_per_line);
|
---|
1924 | return;
|
---|
1925 | } else if (d->depth == 16) {
|
---|
1926 | qt_rectfill<quint16>(reinterpret_cast<quint16*>(d->data), pixel,
|
---|
1927 | 0, 0, d->width, d->height, d->bytes_per_line);
|
---|
1928 | return;
|
---|
1929 | } else if (d->depth == 24) {
|
---|
1930 | qt_rectfill<quint24>(reinterpret_cast<quint24*>(d->data), pixel,
|
---|
1931 | 0, 0, d->width, d->height, d->bytes_per_line);
|
---|
1932 | return;
|
---|
1933 | }
|
---|
1934 |
|
---|
1935 | if (d->format == Format_RGB32)
|
---|
1936 | pixel |= 0xff000000;
|
---|
1937 |
|
---|
1938 | qt_rectfill<uint>(reinterpret_cast<uint*>(d->data), pixel,
|
---|
1939 | 0, 0, d->width, d->height, d->bytes_per_line);
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | /*!
|
---|
1943 | Inverts all pixel values in the image.
|
---|
1944 |
|
---|
1945 | The given invert \a mode only have a meaning when the image's
|
---|
1946 | depth is 32. The default \a mode is InvertRgb, which leaves the
|
---|
1947 | alpha channel unchanged. If the \a mode is InvertRgba, the alpha
|
---|
1948 | bits are also inverted.
|
---|
1949 |
|
---|
1950 | Inverting an 8-bit image means to replace all pixels using color
|
---|
1951 | index \e i with a pixel using color index 255 minus \e i. The same
|
---|
1952 | is the case for a 1-bit image. Note that the color table is \e not
|
---|
1953 | changed.
|
---|
1954 |
|
---|
1955 | \sa {QImage#Image Transformations}{Image Transformations}
|
---|
1956 | */
|
---|
1957 |
|
---|
1958 | void QImage::invertPixels(InvertMode mode)
|
---|
1959 | {
|
---|
1960 | if (!d)
|
---|
1961 | return;
|
---|
1962 |
|
---|
1963 | detach();
|
---|
1964 |
|
---|
1965 | // In case detach() ran out of memory
|
---|
1966 | if (!d)
|
---|
1967 | return;
|
---|
1968 |
|
---|
1969 | if (depth() != 32) {
|
---|
1970 | // number of used bytes pr line
|
---|
1971 | int bpl = (d->width * d->depth + 7) / 8;
|
---|
1972 | int pad = d->bytes_per_line - bpl;
|
---|
1973 | uchar *sl = d->data;
|
---|
1974 | for (int y=0; y<d->height; ++y) {
|
---|
1975 | for (int x=0; x<bpl; ++x)
|
---|
1976 | *sl++ ^= 0xff;
|
---|
1977 | sl += pad;
|
---|
1978 | }
|
---|
1979 | } else {
|
---|
1980 | quint32 *p = (quint32*)d->data;
|
---|
1981 | quint32 *end = (quint32*)(d->data + d->nbytes);
|
---|
1982 | uint xorbits = (mode == InvertRgba) ? 0xffffffff : 0x00ffffff;
|
---|
1983 | while (p < end)
|
---|
1984 | *p++ ^= xorbits;
|
---|
1985 | }
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 | /*!
|
---|
1989 | \fn void QImage::invertPixels(bool invertAlpha)
|
---|
1990 |
|
---|
1991 | Use the invertPixels() function that takes a QImage::InvertMode
|
---|
1992 | parameter instead.
|
---|
1993 | */
|
---|
1994 |
|
---|
1995 | /*! \fn QImage::Endian QImage::systemByteOrder()
|
---|
1996 |
|
---|
1997 | Determines the host computer byte order. Returns
|
---|
1998 | QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
|
---|
1999 |
|
---|
2000 | This function is no longer relevant for QImage. Use QSysInfo
|
---|
2001 | instead.
|
---|
2002 | */
|
---|
2003 |
|
---|
2004 | // Windows defines these
|
---|
2005 | #if defined(write)
|
---|
2006 | # undef write
|
---|
2007 | #endif
|
---|
2008 | #if defined(close)
|
---|
2009 | # undef close
|
---|
2010 | #endif
|
---|
2011 | #if defined(read)
|
---|
2012 | # undef read
|
---|
2013 | #endif
|
---|
2014 |
|
---|
2015 | /*!
|
---|
2016 | Resizes the color table to contain \a numColors entries.
|
---|
2017 |
|
---|
2018 | If the color table is expanded, all the extra colors will be set to
|
---|
2019 | transparent (i.e qRgba(0, 0, 0, 0)).
|
---|
2020 |
|
---|
2021 | When the image is used, the color table must be large enough to
|
---|
2022 | have entries for all the pixel/index values present in the image,
|
---|
2023 | otherwise the results are undefined.
|
---|
2024 |
|
---|
2025 | \sa numColors(), colorTable(), setColor(), {QImage#Image
|
---|
2026 | Transformations}{Image Transformations}
|
---|
2027 | */
|
---|
2028 |
|
---|
2029 | void QImage::setNumColors(int numColors)
|
---|
2030 | {
|
---|
2031 | if (!d) {
|
---|
2032 | qWarning("QImage::setNumColors: null image");
|
---|
2033 | return;
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | detach();
|
---|
2037 |
|
---|
2038 | // In case detach() ran out of memory
|
---|
2039 | if (!d)
|
---|
2040 | return;
|
---|
2041 |
|
---|
2042 | if (numColors == d->colortable.size())
|
---|
2043 | return;
|
---|
2044 | if (numColors <= 0) { // use no color table
|
---|
2045 | d->colortable = QVector<QRgb>();
|
---|
2046 | return;
|
---|
2047 | }
|
---|
2048 | int nc = d->colortable.size();
|
---|
2049 | d->colortable.resize(numColors);
|
---|
2050 | for (int i = nc; i < numColors; ++i)
|
---|
2051 | d->colortable[i] = 0;
|
---|
2052 |
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | /*!
|
---|
2056 | Returns the format of the image.
|
---|
2057 |
|
---|
2058 | \sa {QImage#Image Formats}{Image Formats}
|
---|
2059 | */
|
---|
2060 | QImage::Format QImage::format() const
|
---|
2061 | {
|
---|
2062 | return d ? d->format : Format_Invalid;
|
---|
2063 | }
|
---|
2064 |
|
---|
2065 |
|
---|
2066 | #ifdef QT3_SUPPORT
|
---|
2067 | /*!
|
---|
2068 | Returns true if alpha buffer mode is enabled; otherwise returns
|
---|
2069 | false.
|
---|
2070 |
|
---|
2071 | Use the hasAlphaChannel() function instead.
|
---|
2072 |
|
---|
2073 | */
|
---|
2074 | bool QImage::hasAlphaBuffer() const
|
---|
2075 | {
|
---|
2076 | if (!d)
|
---|
2077 | return false;
|
---|
2078 |
|
---|
2079 | switch (d->format) {
|
---|
2080 | case Format_ARGB32:
|
---|
2081 | case Format_ARGB32_Premultiplied:
|
---|
2082 | case Format_ARGB8565_Premultiplied:
|
---|
2083 | case Format_ARGB8555_Premultiplied:
|
---|
2084 | case Format_ARGB6666_Premultiplied:
|
---|
2085 | case Format_ARGB4444_Premultiplied:
|
---|
2086 | return true;
|
---|
2087 | default:
|
---|
2088 | return false;
|
---|
2089 | }
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | /*!
|
---|
2093 | Enables alpha buffer mode if \a enable is true, otherwise disables
|
---|
2094 | it. The alpha buffer is used to set a mask when a QImage is
|
---|
2095 | translated to a QPixmap.
|
---|
2096 |
|
---|
2097 | If a monochrome or indexed 8-bit image has alpha channels in their
|
---|
2098 | color tables they will automatically detect that they have an
|
---|
2099 | alpha channel, so this function is not required. To force alpha
|
---|
2100 | channels on 32-bit images, use the convertToFormat() function.
|
---|
2101 | */
|
---|
2102 |
|
---|
2103 | void QImage::setAlphaBuffer(bool enable)
|
---|
2104 | {
|
---|
2105 | if (!d
|
---|
2106 | || d->format == Format_Mono
|
---|
2107 | || d->format == Format_MonoLSB
|
---|
2108 | || d->format == Format_Indexed8)
|
---|
2109 | return;
|
---|
2110 | if (enable && (d->format == Format_ARGB32 ||
|
---|
2111 | d->format == Format_ARGB32_Premultiplied ||
|
---|
2112 | d->format == Format_ARGB8565_Premultiplied ||
|
---|
2113 | d->format == Format_ARGB6666_Premultiplied ||
|
---|
2114 | d->format == Format_ARGB8555_Premultiplied ||
|
---|
2115 | d->format == Format_ARGB4444_Premultiplied))
|
---|
2116 | {
|
---|
2117 | return;
|
---|
2118 | }
|
---|
2119 | if (!enable && (d->format == Format_RGB32 ||
|
---|
2120 | d->format == Format_RGB555 ||
|
---|
2121 | d->format == Format_RGB666 ||
|
---|
2122 | d->format == Format_RGB888 ||
|
---|
2123 | d->format == Format_RGB444))
|
---|
2124 | {
|
---|
2125 | return;
|
---|
2126 | }
|
---|
2127 | detach();
|
---|
2128 | d->format = (enable ? Format_ARGB32 : Format_RGB32);
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 |
|
---|
2132 | /*!
|
---|
2133 | \fn bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
|
---|
2134 |
|
---|
2135 | Sets the image \a width, \a height, \a depth, its number of colors
|
---|
2136 | (in \a numColors), and bit order. Returns true if successful, or
|
---|
2137 | false if the parameters are incorrect or if memory cannot be
|
---|
2138 | allocated.
|
---|
2139 |
|
---|
2140 | The \a width and \a height is limited to 32767. \a depth must be
|
---|
2141 | 1, 8, or 32. If \a depth is 1, \a bitOrder must be set to
|
---|
2142 | either QImage::LittleEndian or QImage::BigEndian. For other depths
|
---|
2143 | \a bitOrder must be QImage::IgnoreEndian.
|
---|
2144 |
|
---|
2145 | This function allocates a color table and a buffer for the image
|
---|
2146 | data. The image data is not initialized. The image buffer is
|
---|
2147 | allocated as a single block that consists of a table of scanLine()
|
---|
2148 | pointers (jumpTable()) and the image data (bits()).
|
---|
2149 |
|
---|
2150 | Use a QImage constructor instead.
|
---|
2151 | */
|
---|
2152 | bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
|
---|
2153 | {
|
---|
2154 | if (d && !d->ref.deref())
|
---|
2155 | delete d;
|
---|
2156 | d = QImageData::create(QSize(width, height), formatFor(depth, bitOrder), numColors);
|
---|
2157 | return true;
|
---|
2158 | }
|
---|
2159 |
|
---|
2160 | /*!
|
---|
2161 | \fn bool QImage::create(const QSize& size, int depth, int numColors, Endian bitOrder)
|
---|
2162 | \overload
|
---|
2163 |
|
---|
2164 | The width and height are specified in the \a size argument.
|
---|
2165 |
|
---|
2166 | Use a QImage constructor instead.
|
---|
2167 | */
|
---|
2168 | bool QImage::create(const QSize& size, int depth, int numColors, QImage::Endian bitOrder)
|
---|
2169 | {
|
---|
2170 | if (d && !d->ref.deref())
|
---|
2171 | delete d;
|
---|
2172 | d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
|
---|
2173 | return true;
|
---|
2174 | }
|
---|
2175 | #endif // QT3_SUPPORT
|
---|
2176 |
|
---|
2177 | /*****************************************************************************
|
---|
2178 | Internal routines for converting image depth.
|
---|
2179 | *****************************************************************************/
|
---|
2180 |
|
---|
2181 | typedef void (*Image_Converter)(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
|
---|
2182 |
|
---|
2183 | static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2184 | {
|
---|
2185 | Q_ASSERT(src->format == QImage::Format_ARGB32);
|
---|
2186 | Q_ASSERT(dest->format == QImage::Format_ARGB32_Premultiplied);
|
---|
2187 | Q_ASSERT(src->width == dest->width);
|
---|
2188 | Q_ASSERT(src->height == dest->height);
|
---|
2189 |
|
---|
2190 | const int src_pad = (src->bytes_per_line >> 2) - src->width;
|
---|
2191 | const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
|
---|
2192 | const QRgb *src_data = (QRgb *) src->data;
|
---|
2193 | QRgb *dest_data = (QRgb *) dest->data;
|
---|
2194 |
|
---|
2195 | for (int i = 0; i < src->height; ++i) {
|
---|
2196 | const QRgb *end = src_data + src->width;
|
---|
2197 | while (src_data < end) {
|
---|
2198 | *dest_data = PREMUL(*src_data);
|
---|
2199 | ++src_data;
|
---|
2200 | ++dest_data;
|
---|
2201 | }
|
---|
2202 | src_data += src_pad;
|
---|
2203 | dest_data += dest_pad;
|
---|
2204 | }
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | static void convert_ARGB_PM_to_ARGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2208 | {
|
---|
2209 | Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
|
---|
2210 | Q_ASSERT(dest->format == QImage::Format_ARGB32);
|
---|
2211 | Q_ASSERT(src->width == dest->width);
|
---|
2212 | Q_ASSERT(src->height == dest->height);
|
---|
2213 |
|
---|
2214 | const int src_pad = (src->bytes_per_line >> 2) - src->width;
|
---|
2215 | const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
|
---|
2216 | const QRgb *src_data = (QRgb *) src->data;
|
---|
2217 | QRgb *dest_data = (QRgb *) dest->data;
|
---|
2218 |
|
---|
2219 | for (int i = 0; i < src->height; ++i) {
|
---|
2220 | const QRgb *end = src_data + src->width;
|
---|
2221 | while (src_data < end) {
|
---|
2222 | *dest_data = INV_PREMUL(*src_data);
|
---|
2223 | ++src_data;
|
---|
2224 | ++dest_data;
|
---|
2225 | }
|
---|
2226 | src_data += src_pad;
|
---|
2227 | dest_data += dest_pad;
|
---|
2228 | }
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | static void convert_ARGB_PM_to_RGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2232 | {
|
---|
2233 | Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
|
---|
2234 | Q_ASSERT(dest->format == QImage::Format_RGB32);
|
---|
2235 | Q_ASSERT(src->width == dest->width);
|
---|
2236 | Q_ASSERT(src->height == dest->height);
|
---|
2237 |
|
---|
2238 | const int src_pad = (src->bytes_per_line >> 2) - src->width;
|
---|
2239 | const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
|
---|
2240 | const QRgb *src_data = (QRgb *) src->data;
|
---|
2241 | QRgb *dest_data = (QRgb *) dest->data;
|
---|
2242 |
|
---|
2243 | for (int i = 0; i < src->height; ++i) {
|
---|
2244 | const QRgb *end = src_data + src->width;
|
---|
2245 | while (src_data < end) {
|
---|
2246 | *dest_data = 0xff000000 | INV_PREMUL(*src_data);
|
---|
2247 | ++src_data;
|
---|
2248 | ++dest_data;
|
---|
2249 | }
|
---|
2250 | src_data += src_pad;
|
---|
2251 | dest_data += dest_pad;
|
---|
2252 | }
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 | static void swap_bit_order(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2256 | {
|
---|
2257 | Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
|
---|
2258 | Q_ASSERT(dest->format == QImage::Format_Mono || dest->format == QImage::Format_MonoLSB);
|
---|
2259 | Q_ASSERT(src->width == dest->width);
|
---|
2260 | Q_ASSERT(src->height == dest->height);
|
---|
2261 | Q_ASSERT(src->nbytes == dest->nbytes);
|
---|
2262 | Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
|
---|
2263 |
|
---|
2264 | dest->colortable = src->colortable;
|
---|
2265 |
|
---|
2266 | const uchar *src_data = src->data;
|
---|
2267 | const uchar *end = src->data + src->nbytes;
|
---|
2268 | uchar *dest_data = dest->data;
|
---|
2269 | while (src_data < end) {
|
---|
2270 | *dest_data = bitflip[*src_data];
|
---|
2271 | ++src_data;
|
---|
2272 | ++dest_data;
|
---|
2273 | }
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | static void mask_alpha_converter(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2277 | {
|
---|
2278 | Q_ASSERT(src->width == dest->width);
|
---|
2279 | Q_ASSERT(src->height == dest->height);
|
---|
2280 |
|
---|
2281 | const int src_pad = (src->bytes_per_line >> 2) - src->width;
|
---|
2282 | const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
|
---|
2283 | const uint *src_data = (const uint *)src->data;
|
---|
2284 | uint *dest_data = (uint *)dest->data;
|
---|
2285 |
|
---|
2286 | for (int i = 0; i < src->height; ++i) {
|
---|
2287 | const uint *end = src_data + src->width;
|
---|
2288 | while (src_data < end) {
|
---|
2289 | *dest_data = *src_data | 0xff000000;
|
---|
2290 | ++src_data;
|
---|
2291 | ++dest_data;
|
---|
2292 | }
|
---|
2293 | src_data += src_pad;
|
---|
2294 | dest_data += dest_pad;
|
---|
2295 | }
|
---|
2296 | }
|
---|
2297 |
|
---|
2298 | static QVector<QRgb> fix_color_table(const QVector<QRgb> &ctbl, QImage::Format format)
|
---|
2299 | {
|
---|
2300 | QVector<QRgb> colorTable = ctbl;
|
---|
2301 | if (format == QImage::Format_RGB32) {
|
---|
2302 | // check if the color table has alpha
|
---|
2303 | for (int i = 0; i < colorTable.size(); ++i)
|
---|
2304 | if (qAlpha(colorTable.at(i) != 0xff))
|
---|
2305 | colorTable[i] = colorTable.at(i) | 0xff000000;
|
---|
2306 | } else if (format == QImage::Format_ARGB32_Premultiplied) {
|
---|
2307 | // check if the color table has alpha
|
---|
2308 | for (int i = 0; i < colorTable.size(); ++i)
|
---|
2309 | colorTable[i] = PREMUL(colorTable.at(i));
|
---|
2310 | }
|
---|
2311 | return colorTable;
|
---|
2312 | }
|
---|
2313 |
|
---|
2314 | //
|
---|
2315 | // dither_to_1: Uses selected dithering algorithm.
|
---|
2316 | //
|
---|
2317 |
|
---|
2318 | static void dither_to_Mono(QImageData *dst, const QImageData *src,
|
---|
2319 | Qt::ImageConversionFlags flags, bool fromalpha)
|
---|
2320 | {
|
---|
2321 | Q_ASSERT(src->width == dst->width);
|
---|
2322 | Q_ASSERT(src->height == dst->height);
|
---|
2323 | Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB);
|
---|
2324 |
|
---|
2325 | dst->colortable.clear();
|
---|
2326 | dst->colortable.append(0xffffffff);
|
---|
2327 | dst->colortable.append(0xff000000);
|
---|
2328 |
|
---|
2329 | enum { Threshold, Ordered, Diffuse } dithermode;
|
---|
2330 |
|
---|
2331 | if (fromalpha) {
|
---|
2332 | if ((flags & Qt::AlphaDither_Mask) == Qt::DiffuseAlphaDither)
|
---|
2333 | dithermode = Diffuse;
|
---|
2334 | else if ((flags & Qt::AlphaDither_Mask) == Qt::OrderedAlphaDither)
|
---|
2335 | dithermode = Ordered;
|
---|
2336 | else
|
---|
2337 | dithermode = Threshold;
|
---|
2338 | } else {
|
---|
2339 | if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither)
|
---|
2340 | dithermode = Threshold;
|
---|
2341 | else if ((flags & Qt::Dither_Mask) == Qt::OrderedDither)
|
---|
2342 | dithermode = Ordered;
|
---|
2343 | else
|
---|
2344 | dithermode = Diffuse;
|
---|
2345 | }
|
---|
2346 |
|
---|
2347 | int w = src->width;
|
---|
2348 | int h = src->height;
|
---|
2349 | int d = src->depth;
|
---|
2350 | uchar gray[256]; // gray map for 8 bit images
|
---|
2351 | bool use_gray = (d == 8);
|
---|
2352 | if (use_gray) { // make gray map
|
---|
2353 | if (fromalpha) {
|
---|
2354 | // Alpha 0x00 -> 0 pixels (white)
|
---|
2355 | // Alpha 0xFF -> 1 pixels (black)
|
---|
2356 | for (int i = 0; i < src->colortable.size(); i++)
|
---|
2357 | gray[i] = (255 - (src->colortable.at(i) >> 24));
|
---|
2358 | } else {
|
---|
2359 | // Pixel 0x00 -> 1 pixels (black)
|
---|
2360 | // Pixel 0xFF -> 0 pixels (white)
|
---|
2361 | for (int i = 0; i < src->colortable.size(); i++)
|
---|
2362 | gray[i] = qGray(src->colortable.at(i));
|
---|
2363 | }
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | uchar *dst_data = dst->data;
|
---|
2367 | int dst_bpl = dst->bytes_per_line;
|
---|
2368 | const uchar *src_data = src->data;
|
---|
2369 | int src_bpl = src->bytes_per_line;
|
---|
2370 |
|
---|
2371 | switch (dithermode) {
|
---|
2372 | case Diffuse: {
|
---|
2373 | int *line1 = new int[w];
|
---|
2374 | int *line2 = new int[w];
|
---|
2375 | int bmwidth = (w+7)/8;
|
---|
2376 |
|
---|
2377 | int *b1, *b2;
|
---|
2378 | int wbytes = w * (d/8);
|
---|
2379 | register const uchar *p = src->data;
|
---|
2380 | const uchar *end = p + wbytes;
|
---|
2381 | b2 = line2;
|
---|
2382 | if (use_gray) { // 8 bit image
|
---|
2383 | while (p < end)
|
---|
2384 | *b2++ = gray[*p++];
|
---|
2385 | } else { // 32 bit image
|
---|
2386 | if (fromalpha) {
|
---|
2387 | while (p < end) {
|
---|
2388 | *b2++ = 255 - (*(uint*)p >> 24);
|
---|
2389 | p += 4;
|
---|
2390 | }
|
---|
2391 | } else {
|
---|
2392 | while (p < end) {
|
---|
2393 | *b2++ = qGray(*(uint*)p);
|
---|
2394 | p += 4;
|
---|
2395 | }
|
---|
2396 | }
|
---|
2397 | }
|
---|
2398 | for (int y=0; y<h; y++) { // for each scan line...
|
---|
2399 | int *tmp = line1; line1 = line2; line2 = tmp;
|
---|
2400 | bool not_last_line = y < h - 1;
|
---|
2401 | if (not_last_line) { // calc. grayvals for next line
|
---|
2402 | p = src->data + (y+1)*src->bytes_per_line;
|
---|
2403 | end = p + wbytes;
|
---|
2404 | b2 = line2;
|
---|
2405 | if (use_gray) { // 8 bit image
|
---|
2406 | while (p < end)
|
---|
2407 | *b2++ = gray[*p++];
|
---|
2408 | } else { // 24 bit image
|
---|
2409 | if (fromalpha) {
|
---|
2410 | while (p < end) {
|
---|
2411 | *b2++ = 255 - (*(uint*)p >> 24);
|
---|
2412 | p += 4;
|
---|
2413 | }
|
---|
2414 | } else {
|
---|
2415 | while (p < end) {
|
---|
2416 | *b2++ = qGray(*(uint*)p);
|
---|
2417 | p += 4;
|
---|
2418 | }
|
---|
2419 | }
|
---|
2420 | }
|
---|
2421 | }
|
---|
2422 |
|
---|
2423 | int err;
|
---|
2424 | uchar *p = dst->data + y*dst->bytes_per_line;
|
---|
2425 | memset(p, 0, bmwidth);
|
---|
2426 | b1 = line1;
|
---|
2427 | b2 = line2;
|
---|
2428 | int bit = 7;
|
---|
2429 | for (int x=1; x<=w; x++) {
|
---|
2430 | if (*b1 < 128) { // black pixel
|
---|
2431 | err = *b1++;
|
---|
2432 | *p |= 1 << bit;
|
---|
2433 | } else { // white pixel
|
---|
2434 | err = *b1++ - 255;
|
---|
2435 | }
|
---|
2436 | if (bit == 0) {
|
---|
2437 | p++;
|
---|
2438 | bit = 7;
|
---|
2439 | } else {
|
---|
2440 | bit--;
|
---|
2441 | }
|
---|
2442 | if (x < w)
|
---|
2443 | *b1 += (err*7)>>4; // spread error to right pixel
|
---|
2444 | if (not_last_line) {
|
---|
2445 | b2[0] += (err*5)>>4; // pixel below
|
---|
2446 | if (x > 1)
|
---|
2447 | b2[-1] += (err*3)>>4; // pixel below left
|
---|
2448 | if (x < w)
|
---|
2449 | b2[1] += err>>4; // pixel below right
|
---|
2450 | }
|
---|
2451 | b2++;
|
---|
2452 | }
|
---|
2453 | }
|
---|
2454 | delete [] line1;
|
---|
2455 | delete [] line2;
|
---|
2456 | } break;
|
---|
2457 | case Ordered: {
|
---|
2458 |
|
---|
2459 | memset(dst->data, 0, dst->nbytes);
|
---|
2460 | if (d == 32) {
|
---|
2461 | for (int i=0; i<h; i++) {
|
---|
2462 | const uint *p = (const uint *)src_data;
|
---|
2463 | const uint *end = p + w;
|
---|
2464 | uchar *m = dst_data;
|
---|
2465 | int bit = 7;
|
---|
2466 | int j = 0;
|
---|
2467 | if (fromalpha) {
|
---|
2468 | while (p < end) {
|
---|
2469 | if ((*p++ >> 24) >= qt_bayer_matrix[j++&15][i&15])
|
---|
2470 | *m |= 1 << bit;
|
---|
2471 | if (bit == 0) {
|
---|
2472 | m++;
|
---|
2473 | bit = 7;
|
---|
2474 | } else {
|
---|
2475 | bit--;
|
---|
2476 | }
|
---|
2477 | }
|
---|
2478 | } else {
|
---|
2479 | while (p < end) {
|
---|
2480 | if ((uint)qGray(*p++) < qt_bayer_matrix[j++&15][i&15])
|
---|
2481 | *m |= 1 << bit;
|
---|
2482 | if (bit == 0) {
|
---|
2483 | m++;
|
---|
2484 | bit = 7;
|
---|
2485 | } else {
|
---|
2486 | bit--;
|
---|
2487 | }
|
---|
2488 | }
|
---|
2489 | }
|
---|
2490 | dst_data += dst_bpl;
|
---|
2491 | src_data += src_bpl;
|
---|
2492 | }
|
---|
2493 | } else
|
---|
2494 | /* (d == 8) */ {
|
---|
2495 | for (int i=0; i<h; i++) {
|
---|
2496 | const uchar *p = src_data;
|
---|
2497 | const uchar *end = p + w;
|
---|
2498 | uchar *m = dst_data;
|
---|
2499 | int bit = 7;
|
---|
2500 | int j = 0;
|
---|
2501 | while (p < end) {
|
---|
2502 | if ((uint)gray[*p++] < qt_bayer_matrix[j++&15][i&15])
|
---|
2503 | *m |= 1 << bit;
|
---|
2504 | if (bit == 0) {
|
---|
2505 | m++;
|
---|
2506 | bit = 7;
|
---|
2507 | } else {
|
---|
2508 | bit--;
|
---|
2509 | }
|
---|
2510 | }
|
---|
2511 | dst_data += dst_bpl;
|
---|
2512 | src_data += src_bpl;
|
---|
2513 | }
|
---|
2514 | }
|
---|
2515 | } break;
|
---|
2516 | default: { // Threshold:
|
---|
2517 | memset(dst->data, 0, dst->nbytes);
|
---|
2518 | if (d == 32) {
|
---|
2519 | for (int i=0; i<h; i++) {
|
---|
2520 | const uint *p = (const uint *)src_data;
|
---|
2521 | const uint *end = p + w;
|
---|
2522 | uchar *m = dst_data;
|
---|
2523 | int bit = 7;
|
---|
2524 | if (fromalpha) {
|
---|
2525 | while (p < end) {
|
---|
2526 | if ((*p++ >> 24) >= 128)
|
---|
2527 | *m |= 1 << bit; // Set mask "on"
|
---|
2528 | if (bit == 0) {
|
---|
2529 | m++;
|
---|
2530 | bit = 7;
|
---|
2531 | } else {
|
---|
2532 | bit--;
|
---|
2533 | }
|
---|
2534 | }
|
---|
2535 | } else {
|
---|
2536 | while (p < end) {
|
---|
2537 | if (qGray(*p++) < 128)
|
---|
2538 | *m |= 1 << bit; // Set pixel "black"
|
---|
2539 | if (bit == 0) {
|
---|
2540 | m++;
|
---|
2541 | bit = 7;
|
---|
2542 | } else {
|
---|
2543 | bit--;
|
---|
2544 | }
|
---|
2545 | }
|
---|
2546 | }
|
---|
2547 | dst_data += dst_bpl;
|
---|
2548 | src_data += src_bpl;
|
---|
2549 | }
|
---|
2550 | } else
|
---|
2551 | if (d == 8) {
|
---|
2552 | for (int i=0; i<h; i++) {
|
---|
2553 | const uchar *p = src_data;
|
---|
2554 | const uchar *end = p + w;
|
---|
2555 | uchar *m = dst_data;
|
---|
2556 | int bit = 7;
|
---|
2557 | while (p < end) {
|
---|
2558 | if (gray[*p++] < 128)
|
---|
2559 | *m |= 1 << bit; // Set mask "on"/ pixel "black"
|
---|
2560 | if (bit == 0) {
|
---|
2561 | m++;
|
---|
2562 | bit = 7;
|
---|
2563 | } else {
|
---|
2564 | bit--;
|
---|
2565 | }
|
---|
2566 | }
|
---|
2567 | dst_data += dst_bpl;
|
---|
2568 | src_data += src_bpl;
|
---|
2569 | }
|
---|
2570 | }
|
---|
2571 | }
|
---|
2572 | }
|
---|
2573 |
|
---|
2574 | if (dst->format == QImage::Format_MonoLSB) {
|
---|
2575 | // need to swap bit order
|
---|
2576 | uchar *sl = dst->data;
|
---|
2577 | int bpl = (dst->width + 7) * dst->depth / 8;
|
---|
2578 | int pad = dst->bytes_per_line - bpl;
|
---|
2579 | for (int y=0; y<dst->height; ++y) {
|
---|
2580 | for (int x=0; x<bpl; ++x) {
|
---|
2581 | *sl = bitflip[*sl];
|
---|
2582 | ++sl;
|
---|
2583 | }
|
---|
2584 | sl += pad;
|
---|
2585 | }
|
---|
2586 | }
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | static void convert_X_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
|
---|
2590 | {
|
---|
2591 | dither_to_Mono(dst, src, flags, false);
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | static void convert_ARGB_PM_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
|
---|
2595 | {
|
---|
2596 | QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
|
---|
2597 | convert_ARGB_PM_to_ARGB(tmp, src, flags);
|
---|
2598 | dither_to_Mono(dst, tmp, flags, false);
|
---|
2599 | delete tmp;
|
---|
2600 | }
|
---|
2601 |
|
---|
2602 | //
|
---|
2603 | // convert_32_to_8: Converts a 32 bits depth (true color) to an 8 bit
|
---|
2604 | // image with a colormap. If the 32 bit image has more than 256 colors,
|
---|
2605 | // we convert the red,green and blue bytes into a single byte encoded
|
---|
2606 | // as 6 shades of each of red, green and blue.
|
---|
2607 | //
|
---|
2608 | // if dithering is needed, only 1 color at most is available for alpha.
|
---|
2609 | //
|
---|
2610 | struct QRgbMap {
|
---|
2611 | inline QRgbMap() : used(0) { }
|
---|
2612 | uchar pix;
|
---|
2613 | uchar used;
|
---|
2614 | QRgb rgb;
|
---|
2615 | };
|
---|
2616 |
|
---|
2617 | static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
|
---|
2618 | {
|
---|
2619 | Q_ASSERT(src->format == QImage::Format_RGB32 || src->format == QImage::Format_ARGB32);
|
---|
2620 | Q_ASSERT(dst->format == QImage::Format_Indexed8);
|
---|
2621 | Q_ASSERT(src->width == dst->width);
|
---|
2622 | Q_ASSERT(src->height == dst->height);
|
---|
2623 |
|
---|
2624 | bool do_quant = (flags & Qt::DitherMode_Mask) == Qt::PreferDither
|
---|
2625 | || src->format == QImage::Format_ARGB32;
|
---|
2626 | uint alpha_mask = src->format == QImage::Format_RGB32 ? 0xff000000 : 0;
|
---|
2627 |
|
---|
2628 | const int tablesize = 997; // prime
|
---|
2629 | QRgbMap table[tablesize];
|
---|
2630 | int pix=0;
|
---|
2631 |
|
---|
2632 | if (!dst->colortable.isEmpty()) {
|
---|
2633 | QVector<QRgb> ctbl = dst->colortable;
|
---|
2634 | dst->colortable.resize(256);
|
---|
2635 | // Preload palette into table.
|
---|
2636 | // Almost same code as pixel insertion below
|
---|
2637 | for (int i = 0; i < dst->colortable.size(); ++i) {
|
---|
2638 | // Find in table...
|
---|
2639 | QRgb p = ctbl.at(i) | alpha_mask;
|
---|
2640 | int hash = p % tablesize;
|
---|
2641 | for (;;) {
|
---|
2642 | if (table[hash].used) {
|
---|
2643 | if (table[hash].rgb == p) {
|
---|
2644 | // Found previous insertion - use it
|
---|
2645 | break;
|
---|
2646 | } else {
|
---|
2647 | // Keep searching...
|
---|
2648 | if (++hash == tablesize) hash = 0;
|
---|
2649 | }
|
---|
2650 | } else {
|
---|
2651 | // Cannot be in table
|
---|
2652 | Q_ASSERT (pix != 256); // too many colors
|
---|
2653 | // Insert into table at this unused position
|
---|
2654 | dst->colortable[pix] = p;
|
---|
2655 | table[hash].pix = pix++;
|
---|
2656 | table[hash].rgb = p;
|
---|
2657 | table[hash].used = 1;
|
---|
2658 | break;
|
---|
2659 | }
|
---|
2660 | }
|
---|
2661 | }
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | if ((flags & Qt::DitherMode_Mask) != Qt::PreferDither) {
|
---|
2665 | dst->colortable.resize(256);
|
---|
2666 | const uchar *src_data = src->data;
|
---|
2667 | uchar *dest_data = dst->data;
|
---|
2668 | for (int y = 0; y < src->height; y++) { // check if <= 256 colors
|
---|
2669 | const QRgb *s = (const QRgb *)src_data;
|
---|
2670 | uchar *b = dest_data;
|
---|
2671 | for (int x = 0; x < src->width; ++x) {
|
---|
2672 | QRgb p = s[x] | alpha_mask;
|
---|
2673 | int hash = p % tablesize;
|
---|
2674 | for (;;) {
|
---|
2675 | if (table[hash].used) {
|
---|
2676 | if (table[hash].rgb == (p)) {
|
---|
2677 | // Found previous insertion - use it
|
---|
2678 | break;
|
---|
2679 | } else {
|
---|
2680 | // Keep searching...
|
---|
2681 | if (++hash == tablesize) hash = 0;
|
---|
2682 | }
|
---|
2683 | } else {
|
---|
2684 | // Cannot be in table
|
---|
2685 | if (pix == 256) { // too many colors
|
---|
2686 | do_quant = true;
|
---|
2687 | // Break right out
|
---|
2688 | x = src->width;
|
---|
2689 | y = src->height;
|
---|
2690 | } else {
|
---|
2691 | // Insert into table at this unused position
|
---|
2692 | dst->colortable[pix] = p;
|
---|
2693 | table[hash].pix = pix++;
|
---|
2694 | table[hash].rgb = p;
|
---|
2695 | table[hash].used = 1;
|
---|
2696 | }
|
---|
2697 | break;
|
---|
2698 | }
|
---|
2699 | }
|
---|
2700 | *b++ = table[hash].pix; // May occur once incorrectly
|
---|
2701 | }
|
---|
2702 | src_data += src->bytes_per_line;
|
---|
2703 | dest_data += dst->bytes_per_line;
|
---|
2704 | }
|
---|
2705 | }
|
---|
2706 | int numColors = do_quant ? 256 : pix;
|
---|
2707 |
|
---|
2708 | dst->colortable.resize(numColors);
|
---|
2709 |
|
---|
2710 | if (do_quant) { // quantization needed
|
---|
2711 |
|
---|
2712 | #define MAX_R 5
|
---|
2713 | #define MAX_G 5
|
---|
2714 | #define MAX_B 5
|
---|
2715 | #define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b))
|
---|
2716 |
|
---|
2717 | for (int rc=0; rc<=MAX_R; rc++) // build 6x6x6 color cube
|
---|
2718 | for (int gc=0; gc<=MAX_G; gc++)
|
---|
2719 | for (int bc=0; bc<=MAX_B; bc++)
|
---|
2720 | dst->colortable[INDEXOF(rc,gc,bc)] = 0xff000000 | qRgb(rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B);
|
---|
2721 |
|
---|
2722 | const uchar *src_data = src->data;
|
---|
2723 | uchar *dest_data = dst->data;
|
---|
2724 | if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither) {
|
---|
2725 | for (int y = 0; y < src->height; y++) {
|
---|
2726 | const QRgb *p = (const QRgb *)src_data;
|
---|
2727 | const QRgb *end = p + src->width;
|
---|
2728 | uchar *b = dest_data;
|
---|
2729 |
|
---|
2730 | while (p < end) {
|
---|
2731 | #define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255))
|
---|
2732 | *b++ =
|
---|
2733 | INDEXOF(
|
---|
2734 | DITHER(qRed(*p), MAX_R),
|
---|
2735 | DITHER(qGreen(*p), MAX_G),
|
---|
2736 | DITHER(qBlue(*p), MAX_B)
|
---|
2737 | );
|
---|
2738 | #undef DITHER
|
---|
2739 | p++;
|
---|
2740 | }
|
---|
2741 | src_data += src->bytes_per_line;
|
---|
2742 | dest_data += dst->bytes_per_line;
|
---|
2743 | }
|
---|
2744 | } else if ((flags & Qt::Dither_Mask) == Qt::DiffuseDither) {
|
---|
2745 | int* line1[3];
|
---|
2746 | int* line2[3];
|
---|
2747 | int* pv[3];
|
---|
2748 | line1[0] = new int[src->width];
|
---|
2749 | line2[0] = new int[src->width];
|
---|
2750 | line1[1] = new int[src->width];
|
---|
2751 | line2[1] = new int[src->width];
|
---|
2752 | line1[2] = new int[src->width];
|
---|
2753 | line2[2] = new int[src->width];
|
---|
2754 | pv[0] = new int[src->width];
|
---|
2755 | pv[1] = new int[src->width];
|
---|
2756 | pv[2] = new int[src->width];
|
---|
2757 |
|
---|
2758 | int endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian);
|
---|
2759 | for (int y = 0; y < src->height; y++) {
|
---|
2760 | const uchar* q = src_data;
|
---|
2761 | const uchar* q2 = y < src->height - 1 ? q + src->bytes_per_line : src->data;
|
---|
2762 | uchar *b = dest_data;
|
---|
2763 | for (int chan = 0; chan < 3; chan++) {
|
---|
2764 | int *l1 = (y&1) ? line2[chan] : line1[chan];
|
---|
2765 | int *l2 = (y&1) ? line1[chan] : line2[chan];
|
---|
2766 | if (y == 0) {
|
---|
2767 | for (int i = 0; i < src->width; i++)
|
---|
2768 | l1[i] = q[i*4+chan+endian];
|
---|
2769 | }
|
---|
2770 | if (y+1 < src->height) {
|
---|
2771 | for (int i = 0; i < src->width; i++)
|
---|
2772 | l2[i] = q2[i*4+chan+endian];
|
---|
2773 | }
|
---|
2774 | // Bi-directional error diffusion
|
---|
2775 | if (y&1) {
|
---|
2776 | for (int x = 0; x < src->width; x++) {
|
---|
2777 | int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
|
---|
2778 | int err = l1[x] - pix * 255 / 5;
|
---|
2779 | pv[chan][x] = pix;
|
---|
2780 |
|
---|
2781 | // Spread the error around...
|
---|
2782 | if (x + 1< src->width) {
|
---|
2783 | l1[x+1] += (err*7)>>4;
|
---|
2784 | l2[x+1] += err>>4;
|
---|
2785 | }
|
---|
2786 | l2[x]+=(err*5)>>4;
|
---|
2787 | if (x>1)
|
---|
2788 | l2[x-1]+=(err*3)>>4;
|
---|
2789 | }
|
---|
2790 | } else {
|
---|
2791 | for (int x = src->width; x-- > 0;) {
|
---|
2792 | int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
|
---|
2793 | int err = l1[x] - pix * 255 / 5;
|
---|
2794 | pv[chan][x] = pix;
|
---|
2795 |
|
---|
2796 | // Spread the error around...
|
---|
2797 | if (x > 0) {
|
---|
2798 | l1[x-1] += (err*7)>>4;
|
---|
2799 | l2[x-1] += err>>4;
|
---|
2800 | }
|
---|
2801 | l2[x]+=(err*5)>>4;
|
---|
2802 | if (x + 1 < src->width)
|
---|
2803 | l2[x+1]+=(err*3)>>4;
|
---|
2804 | }
|
---|
2805 | }
|
---|
2806 | }
|
---|
2807 | if (endian) {
|
---|
2808 | for (int x = 0; x < src->width; x++) {
|
---|
2809 | *b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]);
|
---|
2810 | }
|
---|
2811 | } else {
|
---|
2812 | for (int x = 0; x < src->width; x++) {
|
---|
2813 | *b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]);
|
---|
2814 | }
|
---|
2815 | }
|
---|
2816 | src_data += src->bytes_per_line;
|
---|
2817 | dest_data += dst->bytes_per_line;
|
---|
2818 | }
|
---|
2819 | delete [] line1[0];
|
---|
2820 | delete [] line2[0];
|
---|
2821 | delete [] line1[1];
|
---|
2822 | delete [] line2[1];
|
---|
2823 | delete [] line1[2];
|
---|
2824 | delete [] line2[2];
|
---|
2825 | delete [] pv[0];
|
---|
2826 | delete [] pv[1];
|
---|
2827 | delete [] pv[2];
|
---|
2828 | } else { // OrderedDither
|
---|
2829 | for (int y = 0; y < src->height; y++) {
|
---|
2830 | const QRgb *p = (const QRgb *)src_data;
|
---|
2831 | const QRgb *end = p + src->width;
|
---|
2832 | uchar *b = dest_data;
|
---|
2833 |
|
---|
2834 | int x = 0;
|
---|
2835 | while (p < end) {
|
---|
2836 | uint d = qt_bayer_matrix[y & 15][x & 15] << 8;
|
---|
2837 |
|
---|
2838 | #define DITHER(p, d, m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) >> 16))
|
---|
2839 | *b++ =
|
---|
2840 | INDEXOF(
|
---|
2841 | DITHER(qRed(*p), d, MAX_R),
|
---|
2842 | DITHER(qGreen(*p), d, MAX_G),
|
---|
2843 | DITHER(qBlue(*p), d, MAX_B)
|
---|
2844 | );
|
---|
2845 | #undef DITHER
|
---|
2846 |
|
---|
2847 | p++;
|
---|
2848 | x++;
|
---|
2849 | }
|
---|
2850 | src_data += src->bytes_per_line;
|
---|
2851 | dest_data += dst->bytes_per_line;
|
---|
2852 | }
|
---|
2853 | }
|
---|
2854 |
|
---|
2855 | if (src->format != QImage::Format_RGB32
|
---|
2856 | && src->format != QImage::Format_RGB16) {
|
---|
2857 | const int trans = 216;
|
---|
2858 | Q_ASSERT(dst->colortable.size() > trans);
|
---|
2859 | dst->colortable[trans] = 0;
|
---|
2860 | QImageData *mask = QImageData::create(QSize(src->width, src->height), QImage::Format_Mono);
|
---|
2861 | dither_to_Mono(mask, src, flags, true);
|
---|
2862 | uchar *dst_data = dst->data;
|
---|
2863 | const uchar *mask_data = mask->data;
|
---|
2864 | for (int y = 0; y < src->height; y++) {
|
---|
2865 | for (int x = 0; x < src->width ; x++) {
|
---|
2866 | if (!(mask_data[x>>3] & (0x80 >> (x & 7))))
|
---|
2867 | dst_data[x] = trans;
|
---|
2868 | }
|
---|
2869 | mask_data += mask->bytes_per_line;
|
---|
2870 | dst_data += dst->bytes_per_line;
|
---|
2871 | }
|
---|
2872 | dst->has_alpha_clut = true;
|
---|
2873 | delete mask;
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | #undef MAX_R
|
---|
2877 | #undef MAX_G
|
---|
2878 | #undef MAX_B
|
---|
2879 | #undef INDEXOF
|
---|
2880 |
|
---|
2881 | }
|
---|
2882 | }
|
---|
2883 |
|
---|
2884 | static void convert_ARGB_PM_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
|
---|
2885 | {
|
---|
2886 | QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
|
---|
2887 | convert_ARGB_PM_to_ARGB(tmp, src, flags);
|
---|
2888 | convert_RGB_to_Indexed8(dst, tmp, flags);
|
---|
2889 | delete tmp;
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | static void convert_ARGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
|
---|
2893 | {
|
---|
2894 | convert_RGB_to_Indexed8(dst, src, flags);
|
---|
2895 | }
|
---|
2896 |
|
---|
2897 | static void convert_Indexed8_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2898 | {
|
---|
2899 | Q_ASSERT(src->format == QImage::Format_Indexed8);
|
---|
2900 | Q_ASSERT(dest->format == QImage::Format_RGB32
|
---|
2901 | || dest->format == QImage::Format_ARGB32
|
---|
2902 | || dest->format == QImage::Format_ARGB32_Premultiplied);
|
---|
2903 | Q_ASSERT(src->width == dest->width);
|
---|
2904 | Q_ASSERT(src->height == dest->height);
|
---|
2905 |
|
---|
2906 | QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
|
---|
2907 |
|
---|
2908 | int w = src->width;
|
---|
2909 | const uchar *src_data = src->data;
|
---|
2910 | uchar *dest_data = dest->data;
|
---|
2911 | for (int y = 0; y < src->height; y++) {
|
---|
2912 | uint *p = (uint *)dest_data;
|
---|
2913 | const uchar *b = src_data;
|
---|
2914 | uint *end = p + w;
|
---|
2915 |
|
---|
2916 | while (p < end)
|
---|
2917 | *p++ = colorTable.at(*b++);
|
---|
2918 |
|
---|
2919 | src_data += src->bytes_per_line;
|
---|
2920 | dest_data += dest->bytes_per_line;
|
---|
2921 | }
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | static void convert_Mono_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2925 | {
|
---|
2926 | Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
|
---|
2927 | Q_ASSERT(dest->format == QImage::Format_RGB32
|
---|
2928 | || dest->format == QImage::Format_ARGB32
|
---|
2929 | || dest->format == QImage::Format_ARGB32_Premultiplied);
|
---|
2930 | Q_ASSERT(src->width == dest->width);
|
---|
2931 | Q_ASSERT(src->height == dest->height);
|
---|
2932 |
|
---|
2933 | QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
|
---|
2934 |
|
---|
2935 | // Default to black / white colors
|
---|
2936 | if (colorTable.size() < 2) {
|
---|
2937 | if (colorTable.size() == 0)
|
---|
2938 | colorTable << 0xff000000;
|
---|
2939 | colorTable << 0xffffffff;
|
---|
2940 | }
|
---|
2941 |
|
---|
2942 | const uchar *src_data = src->data;
|
---|
2943 | uchar *dest_data = dest->data;
|
---|
2944 | if (src->format == QImage::Format_Mono) {
|
---|
2945 | for (int y = 0; y < dest->height; y++) {
|
---|
2946 | register uint *p = (uint *)dest_data;
|
---|
2947 | for (int x = 0; x < dest->width; x++)
|
---|
2948 | *p++ = colorTable.at((src_data[x>>3] >> (7 - (x & 7))) & 1);
|
---|
2949 |
|
---|
2950 | src_data += src->bytes_per_line;
|
---|
2951 | dest_data += dest->bytes_per_line;
|
---|
2952 | }
|
---|
2953 | } else {
|
---|
2954 | for (int y = 0; y < dest->height; y++) {
|
---|
2955 | register uint *p = (uint *)dest_data;
|
---|
2956 | for (int x = 0; x < dest->width; x++)
|
---|
2957 | *p++ = colorTable.at((src_data[x>>3] >> (x & 7)) & 1);
|
---|
2958 |
|
---|
2959 | src_data += src->bytes_per_line;
|
---|
2960 | dest_data += dest->bytes_per_line;
|
---|
2961 | }
|
---|
2962 | }
|
---|
2963 | }
|
---|
2964 |
|
---|
2965 |
|
---|
2966 | static void convert_Mono_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
|
---|
2967 | {
|
---|
2968 | Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
|
---|
2969 | Q_ASSERT(dest->format == QImage::Format_Indexed8);
|
---|
2970 | Q_ASSERT(src->width == dest->width);
|
---|
2971 | Q_ASSERT(src->height == dest->height);
|
---|
2972 |
|
---|
2973 | QVector<QRgb> ctbl = src->colortable;
|
---|
2974 | if (ctbl.size() > 2) {
|
---|
2975 | ctbl.resize(2);
|
---|
2976 | } else if (ctbl.size() < 2) {
|
---|
2977 | if (ctbl.size() == 0)
|
---|
2978 | ctbl << 0xff000000;
|
---|
2979 | ctbl << 0xffffffff;
|
---|
2980 | }
|
---|
2981 | dest->colortable = ctbl;
|
---|
2982 | dest->has_alpha_clut = src->has_alpha_clut;
|
---|
2983 |
|
---|
2984 |
|
---|
2985 | const uchar *src_data = src->data;
|
---|
2986 | uchar *dest_data = dest->data;
|
---|
2987 | if (src->format == QImage::Format_Mono) {
|
---|
2988 | for (int y = 0; y < dest->height; y++) {
|
---|
2989 | register uchar *p = dest_data;
|
---|
2990 | for (int x = 0; x < dest->width; x++)
|
---|
2991 | *p++ = (src_data[x>>3] >> (7 - (x & 7))) & 1;
|
---|
2992 | src_data += src->bytes_per_line;
|
---|
2993 | dest_data += dest->bytes_per_line;
|
---|
2994 | }
|
---|
2995 | } else {
|
---|
2996 | for (int y = 0; y < dest->height; y++) {
|
---|
2997 | register uchar *p = dest_data;
|
---|
2998 | for (int x = 0; x < dest->width; x++)
|
---|
2999 | *p++ = (src_data[x>>3] >> (x & 7)) & 1;
|
---|
3000 | src_data += src->bytes_per_line;
|
---|
3001 | dest_data += dest->bytes_per_line;
|
---|
3002 | }
|
---|
3003 | }
|
---|
3004 | }
|
---|
3005 |
|
---|
3006 | #define CONVERT_DECL(DST, SRC) \
|
---|
3007 | static void convert_##SRC##_to_##DST(QImageData *dest, \
|
---|
3008 | const QImageData *src, \
|
---|
3009 | Qt::ImageConversionFlags) \
|
---|
3010 | { \
|
---|
3011 | qt_rectconvert<DST, SRC>(reinterpret_cast<DST*>(dest->data), \
|
---|
3012 | reinterpret_cast<const SRC*>(src->data), \
|
---|
3013 | 0, 0, src->width, src->height, \
|
---|
3014 | dest->bytes_per_line, src->bytes_per_line); \
|
---|
3015 | }
|
---|
3016 |
|
---|
3017 | CONVERT_DECL(quint32, quint16)
|
---|
3018 | CONVERT_DECL(quint16, quint32)
|
---|
3019 | CONVERT_DECL(quint32, qargb8565)
|
---|
3020 | CONVERT_DECL(qargb8565, quint32)
|
---|
3021 | CONVERT_DECL(quint32, qrgb555)
|
---|
3022 | CONVERT_DECL(qrgb666, quint32)
|
---|
3023 | CONVERT_DECL(quint32, qrgb666)
|
---|
3024 | CONVERT_DECL(qargb6666, quint32)
|
---|
3025 | CONVERT_DECL(quint32, qargb6666)
|
---|
3026 | CONVERT_DECL(qrgb555, quint32)
|
---|
3027 | #if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
|
---|
3028 | CONVERT_DECL(quint16, qrgb555)
|
---|
3029 | CONVERT_DECL(qrgb555, quint16)
|
---|
3030 | #endif
|
---|
3031 | CONVERT_DECL(quint32, qrgb888)
|
---|
3032 | CONVERT_DECL(qrgb888, quint32)
|
---|
3033 | CONVERT_DECL(quint32, qargb8555)
|
---|
3034 | CONVERT_DECL(qargb8555, quint32)
|
---|
3035 | CONVERT_DECL(quint32, qrgb444)
|
---|
3036 | CONVERT_DECL(qrgb444, quint32)
|
---|
3037 | CONVERT_DECL(quint32, qargb4444)
|
---|
3038 | CONVERT_DECL(qargb4444, quint32)
|
---|
3039 | #undef CONVERT_DECL
|
---|
3040 | #define CONVERT_PTR(DST, SRC) convert_##SRC##_to_##DST
|
---|
3041 |
|
---|
3042 | /*
|
---|
3043 | Format_Invalid,
|
---|
3044 | Format_Mono,
|
---|
3045 | Format_MonoLSB,
|
---|
3046 | Format_Indexed8,
|
---|
3047 | Format_RGB32,
|
---|
3048 | Format_ARGB32,
|
---|
3049 | Format_ARGB32_Premultiplied,
|
---|
3050 | Format_RGB16,
|
---|
3051 | Format_ARGB8565_Premultiplied,
|
---|
3052 | Format_RGB666,
|
---|
3053 | Format_ARGB6666_Premultiplied,
|
---|
3054 | Format_RGB555,
|
---|
3055 | Format_ARGB8555_Premultiplied,
|
---|
3056 | Format_RGB888
|
---|
3057 | Format_RGB444
|
---|
3058 | Format_ARGB4444_Premultiplied
|
---|
3059 | */
|
---|
3060 |
|
---|
3061 |
|
---|
3062 | // first index source, second dest
|
---|
3063 | static const Image_Converter converter_map[QImage::NImageFormats][QImage::NImageFormats] =
|
---|
3064 | {
|
---|
3065 | {
|
---|
3066 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
---|
3067 | },
|
---|
3068 | {
|
---|
3069 | 0,
|
---|
3070 | 0,
|
---|
3071 | swap_bit_order,
|
---|
3072 | convert_Mono_to_Indexed8,
|
---|
3073 | convert_Mono_to_X32,
|
---|
3074 | convert_Mono_to_X32,
|
---|
3075 | convert_Mono_to_X32,
|
---|
3076 | 0,
|
---|
3077 | 0,
|
---|
3078 | 0,
|
---|
3079 | 0,
|
---|
3080 | 0,
|
---|
3081 | 0,
|
---|
3082 | 0,
|
---|
3083 | 0,
|
---|
3084 | 0
|
---|
3085 | }, // Format_Mono
|
---|
3086 |
|
---|
3087 | {
|
---|
3088 | 0,
|
---|
3089 | swap_bit_order,
|
---|
3090 | 0,
|
---|
3091 | convert_Mono_to_Indexed8,
|
---|
3092 | convert_Mono_to_X32,
|
---|
3093 | convert_Mono_to_X32,
|
---|
3094 | convert_Mono_to_X32,
|
---|
3095 | 0,
|
---|
3096 | 0,
|
---|
3097 | 0,
|
---|
3098 | 0,
|
---|
3099 | 0,
|
---|
3100 | 0,
|
---|
3101 | 0,
|
---|
3102 | 0,
|
---|
3103 | 0
|
---|
3104 | }, // Format_MonoLSB
|
---|
3105 |
|
---|
3106 | {
|
---|
3107 | 0,
|
---|
3108 | convert_X_to_Mono,
|
---|
3109 | convert_X_to_Mono,
|
---|
3110 | 0,
|
---|
3111 | convert_Indexed8_to_X32,
|
---|
3112 | convert_Indexed8_to_X32,
|
---|
3113 | convert_Indexed8_to_X32,
|
---|
3114 | 0,
|
---|
3115 | 0,
|
---|
3116 | 0,
|
---|
3117 | 0,
|
---|
3118 | 0,
|
---|
3119 | 0,
|
---|
3120 | 0,
|
---|
3121 | 0,
|
---|
3122 | 0
|
---|
3123 | }, // Format_Indexed8
|
---|
3124 |
|
---|
3125 | {
|
---|
3126 | 0,
|
---|
3127 | convert_X_to_Mono,
|
---|
3128 | convert_X_to_Mono,
|
---|
3129 | convert_RGB_to_Indexed8,
|
---|
3130 | 0,
|
---|
3131 | mask_alpha_converter,
|
---|
3132 | mask_alpha_converter,
|
---|
3133 | CONVERT_PTR(quint16, quint32),
|
---|
3134 | CONVERT_PTR(qargb8565, quint32),
|
---|
3135 | CONVERT_PTR(qrgb666, quint32),
|
---|
3136 | CONVERT_PTR(qargb6666, quint32),
|
---|
3137 | CONVERT_PTR(qrgb555, quint32),
|
---|
3138 | CONVERT_PTR(qargb8555, quint32),
|
---|
3139 | CONVERT_PTR(qrgb888, quint32),
|
---|
3140 | CONVERT_PTR(qrgb444, quint32),
|
---|
3141 | CONVERT_PTR(qargb4444, quint32)
|
---|
3142 | }, // Format_RGB32
|
---|
3143 |
|
---|
3144 | {
|
---|
3145 | 0,
|
---|
3146 | convert_X_to_Mono,
|
---|
3147 | convert_X_to_Mono,
|
---|
3148 | convert_ARGB_to_Indexed8,
|
---|
3149 | mask_alpha_converter,
|
---|
3150 | 0,
|
---|
3151 | convert_ARGB_to_ARGB_PM,
|
---|
3152 | CONVERT_PTR(quint16, quint32),
|
---|
3153 | CONVERT_PTR(qargb8565, quint32),
|
---|
3154 | CONVERT_PTR(qrgb666, quint32),
|
---|
3155 | CONVERT_PTR(qargb6666, quint32),
|
---|
3156 | CONVERT_PTR(qrgb555, quint32),
|
---|
3157 | CONVERT_PTR(qargb8555, quint32),
|
---|
3158 | CONVERT_PTR(qrgb888, quint32),
|
---|
3159 | CONVERT_PTR(qrgb444, quint32),
|
---|
3160 | CONVERT_PTR(qargb4444, quint32)
|
---|
3161 | }, // Format_ARGB32
|
---|
3162 |
|
---|
3163 | {
|
---|
3164 | 0,
|
---|
3165 | convert_ARGB_PM_to_Mono,
|
---|
3166 | convert_ARGB_PM_to_Mono,
|
---|
3167 | convert_ARGB_PM_to_Indexed8,
|
---|
3168 | convert_ARGB_PM_to_RGB,
|
---|
3169 | convert_ARGB_PM_to_ARGB,
|
---|
3170 | 0,
|
---|
3171 | 0,
|
---|
3172 | 0,
|
---|
3173 | 0,
|
---|
3174 | 0,
|
---|
3175 | 0,
|
---|
3176 | 0,
|
---|
3177 | 0,
|
---|
3178 | 0,
|
---|
3179 | 0
|
---|
3180 | }, // Format_ARGB32_Premultiplied
|
---|
3181 |
|
---|
3182 | {
|
---|
3183 | 0,
|
---|
3184 | 0,
|
---|
3185 | 0,
|
---|
3186 | 0,
|
---|
3187 | CONVERT_PTR(quint32, quint16),
|
---|
3188 | CONVERT_PTR(quint32, quint16),
|
---|
3189 | CONVERT_PTR(quint32, quint16),
|
---|
3190 | 0,
|
---|
3191 | 0,
|
---|
3192 | 0,
|
---|
3193 | 0,
|
---|
3194 | #if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
|
---|
3195 | CONVERT_PTR(qrgb555, quint16),
|
---|
3196 | #else
|
---|
3197 | 0,
|
---|
3198 | #endif
|
---|
3199 | 0,
|
---|
3200 | 0,
|
---|
3201 | 0,
|
---|
3202 | 0
|
---|
3203 | }, // Format_RGB16
|
---|
3204 |
|
---|
3205 | {
|
---|
3206 | 0,
|
---|
3207 | 0,
|
---|
3208 | 0,
|
---|
3209 | 0,
|
---|
3210 | CONVERT_PTR(quint32, qargb8565),
|
---|
3211 | CONVERT_PTR(quint32, qargb8565),
|
---|
3212 | CONVERT_PTR(quint32, qargb8565),
|
---|
3213 | 0,
|
---|
3214 | 0,
|
---|
3215 | 0,
|
---|
3216 | 0,
|
---|
3217 | 0,
|
---|
3218 | 0,
|
---|
3219 | 0,
|
---|
3220 | 0,
|
---|
3221 | 0
|
---|
3222 | }, // Format_ARGB8565_Premultiplied
|
---|
3223 |
|
---|
3224 | {
|
---|
3225 | 0,
|
---|
3226 | 0,
|
---|
3227 | 0,
|
---|
3228 | 0,
|
---|
3229 | CONVERT_PTR(quint32, qrgb666),
|
---|
3230 | CONVERT_PTR(quint32, qrgb666),
|
---|
3231 | CONVERT_PTR(quint32, qrgb666),
|
---|
3232 | 0,
|
---|
3233 | 0,
|
---|
3234 | 0,
|
---|
3235 | 0,
|
---|
3236 | 0,
|
---|
3237 | 0,
|
---|
3238 | 0,
|
---|
3239 | 0,
|
---|
3240 | 0
|
---|
3241 | }, // Format_RGB666
|
---|
3242 |
|
---|
3243 | {
|
---|
3244 | 0,
|
---|
3245 | 0,
|
---|
3246 | 0,
|
---|
3247 | 0,
|
---|
3248 | CONVERT_PTR(quint32, qargb6666),
|
---|
3249 | CONVERT_PTR(quint32, qargb6666),
|
---|
3250 | CONVERT_PTR(quint32, qargb6666),
|
---|
3251 | 0,
|
---|
3252 | 0,
|
---|
3253 | 0,
|
---|
3254 | 0,
|
---|
3255 | 0,
|
---|
3256 | 0,
|
---|
3257 | 0,
|
---|
3258 | 0,
|
---|
3259 | 0
|
---|
3260 | }, // Format_ARGB6666_Premultiplied
|
---|
3261 |
|
---|
3262 | {
|
---|
3263 | 0,
|
---|
3264 | 0,
|
---|
3265 | 0,
|
---|
3266 | 0,
|
---|
3267 | CONVERT_PTR(quint32, qrgb555),
|
---|
3268 | CONVERT_PTR(quint32, qrgb555),
|
---|
3269 | CONVERT_PTR(quint32, qrgb555),
|
---|
3270 | #if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
|
---|
3271 | CONVERT_PTR(quint16, qrgb555),
|
---|
3272 | #else
|
---|
3273 | 0,
|
---|
3274 | #endif
|
---|
3275 | 0,
|
---|
3276 | 0,
|
---|
3277 | 0,
|
---|
3278 | 0,
|
---|
3279 | 0,
|
---|
3280 | 0,
|
---|
3281 | 0,
|
---|
3282 | 0
|
---|
3283 | }, // Format_RGB555
|
---|
3284 |
|
---|
3285 | {
|
---|
3286 | 0,
|
---|
3287 | 0,
|
---|
3288 | 0,
|
---|
3289 | 0,
|
---|
3290 | CONVERT_PTR(quint32, qargb8555),
|
---|
3291 | CONVERT_PTR(quint32, qargb8555),
|
---|
3292 | CONVERT_PTR(quint32, qargb8555),
|
---|
3293 | 0,
|
---|
3294 | 0,
|
---|
3295 | 0,
|
---|
3296 | 0,
|
---|
3297 | 0,
|
---|
3298 | 0,
|
---|
3299 | 0,
|
---|
3300 | 0,
|
---|
3301 | 0
|
---|
3302 | }, // Format_ARGB8555_Premultiplied
|
---|
3303 |
|
---|
3304 | {
|
---|
3305 | 0,
|
---|
3306 | 0,
|
---|
3307 | 0,
|
---|
3308 | 0,
|
---|
3309 | CONVERT_PTR(quint32, qrgb888),
|
---|
3310 | CONVERT_PTR(quint32, qrgb888),
|
---|
3311 | CONVERT_PTR(quint32, qrgb888),
|
---|
3312 | 0,
|
---|
3313 | 0,
|
---|
3314 | 0,
|
---|
3315 | 0,
|
---|
3316 | 0,
|
---|
3317 | 0,
|
---|
3318 | 0,
|
---|
3319 | 0,
|
---|
3320 | 0
|
---|
3321 | }, // Format_RGB888
|
---|
3322 |
|
---|
3323 | {
|
---|
3324 | 0,
|
---|
3325 | 0,
|
---|
3326 | 0,
|
---|
3327 | 0,
|
---|
3328 | CONVERT_PTR(quint32, qrgb444),
|
---|
3329 | CONVERT_PTR(quint32, qrgb444),
|
---|
3330 | CONVERT_PTR(quint32, qrgb444),
|
---|
3331 | 0,
|
---|
3332 | 0,
|
---|
3333 | 0,
|
---|
3334 | 0,
|
---|
3335 | 0,
|
---|
3336 | 0,
|
---|
3337 | 0,
|
---|
3338 | 0,
|
---|
3339 | 0
|
---|
3340 | }, // Format_RGB444
|
---|
3341 |
|
---|
3342 | {
|
---|
3343 | 0,
|
---|
3344 | 0,
|
---|
3345 | 0,
|
---|
3346 | 0,
|
---|
3347 | CONVERT_PTR(quint32, qargb4444),
|
---|
3348 | CONVERT_PTR(quint32, qargb4444),
|
---|
3349 | CONVERT_PTR(quint32, qargb4444),
|
---|
3350 | 0,
|
---|
3351 | 0,
|
---|
3352 | 0,
|
---|
3353 | 0,
|
---|
3354 | 0,
|
---|
3355 | 0,
|
---|
3356 | 0,
|
---|
3357 | 0,
|
---|
3358 | 0
|
---|
3359 | } // Format_ARGB4444_Premultiplied
|
---|
3360 | };
|
---|
3361 |
|
---|
3362 | /*!
|
---|
3363 | Returns a copy of the image in the given \a format.
|
---|
3364 |
|
---|
3365 | The specified image conversion \a flags control how the image data
|
---|
3366 | is handled during the conversion process.
|
---|
3367 |
|
---|
3368 | \sa {QImage#Image Format}{Image Format}
|
---|
3369 | */
|
---|
3370 | QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) const
|
---|
3371 | {
|
---|
3372 | if (!d || d->format == format)
|
---|
3373 | return *this;
|
---|
3374 |
|
---|
3375 | if (format == Format_Invalid || d->format == Format_Invalid)
|
---|
3376 | return QImage();
|
---|
3377 |
|
---|
3378 | const Image_Converter *converterPtr = &converter_map[d->format][format];
|
---|
3379 | Image_Converter converter = *converterPtr;
|
---|
3380 | if (converter) {
|
---|
3381 | QImage image(d->width, d->height, format);
|
---|
3382 |
|
---|
3383 | QIMAGE_SANITYCHECK_MEMORY(image);
|
---|
3384 |
|
---|
3385 | image.setDotsPerMeterY(dotsPerMeterY());
|
---|
3386 | image.setDotsPerMeterX(dotsPerMeterX());
|
---|
3387 |
|
---|
3388 | #if !defined(QT_NO_IMAGE_TEXT)
|
---|
3389 | image.d->text = d->text;
|
---|
3390 | #endif // !QT_NO_IMAGE_TEXT
|
---|
3391 |
|
---|
3392 | converter(image.d, d, flags);
|
---|
3393 | return image;
|
---|
3394 | }
|
---|
3395 |
|
---|
3396 | Q_ASSERT(format != QImage::Format_ARGB32);
|
---|
3397 | Q_ASSERT(d->format != QImage::Format_ARGB32);
|
---|
3398 |
|
---|
3399 | QImage image = convertToFormat(Format_ARGB32, flags);
|
---|
3400 | return image.convertToFormat(format, flags);
|
---|
3401 | }
|
---|
3402 |
|
---|
3403 |
|
---|
3404 |
|
---|
3405 | static inline int pixel_distance(QRgb p1, QRgb p2) {
|
---|
3406 | int r1 = qRed(p1);
|
---|
3407 | int g1 = qGreen(p1);
|
---|
3408 | int b1 = qBlue(p1);
|
---|
3409 | int a1 = qAlpha(p1);
|
---|
3410 |
|
---|
3411 | int r2 = qRed(p2);
|
---|
3412 | int g2 = qGreen(p2);
|
---|
3413 | int b2 = qBlue(p2);
|
---|
3414 | int a2 = qAlpha(p2);
|
---|
3415 |
|
---|
3416 | return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2) + abs(a1 - a2);
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | static inline int closestMatch(QRgb pixel, const QVector<QRgb> &clut) {
|
---|
3420 | int idx = 0;
|
---|
3421 | int current_distance = INT_MAX;
|
---|
3422 | for (int i=0; i<clut.size(); ++i) {
|
---|
3423 | int dist = pixel_distance(pixel, clut.at(i));
|
---|
3424 | if (dist < current_distance) {
|
---|
3425 | current_distance = dist;
|
---|
3426 | idx = i;
|
---|
3427 | }
|
---|
3428 | }
|
---|
3429 | return idx;
|
---|
3430 | }
|
---|
3431 |
|
---|
3432 | static QImage convertWithPalette(const QImage &src, QImage::Format format,
|
---|
3433 | const QVector<QRgb> &clut) {
|
---|
3434 | QImage dest(src.size(), format);
|
---|
3435 | dest.setColorTable(clut);
|
---|
3436 |
|
---|
3437 | #if !defined(QT_NO_IMAGE_TEXT)
|
---|
3438 | QString textsKeys = src.text();
|
---|
3439 | QStringList textKeyList = textsKeys.split(QLatin1Char('\n'), QString::SkipEmptyParts);
|
---|
3440 | foreach (const QString &textKey, textKeyList) {
|
---|
3441 | QStringList textKeySplitted = textKey.split(QLatin1String(": "));
|
---|
3442 | dest.setText(textKeySplitted[0], textKeySplitted[1]);
|
---|
3443 | }
|
---|
3444 | #endif // !QT_NO_IMAGE_TEXT
|
---|
3445 |
|
---|
3446 | int h = src.height();
|
---|
3447 | int w = src.width();
|
---|
3448 |
|
---|
3449 | QHash<QRgb, int> cache;
|
---|
3450 |
|
---|
3451 | if (format == QImage::Format_Indexed8) {
|
---|
3452 | for (int y=0; y<h; ++y) {
|
---|
3453 | QRgb *src_pixels = (QRgb *) src.scanLine(y);
|
---|
3454 | uchar *dest_pixels = (uchar *) dest.scanLine(y);
|
---|
3455 | for (int x=0; x<w; ++x) {
|
---|
3456 | int src_pixel = src_pixels[x];
|
---|
3457 | int value = cache.value(src_pixel, -1);
|
---|
3458 | if (value == -1) {
|
---|
3459 | value = closestMatch(src_pixel, clut);
|
---|
3460 | cache.insert(src_pixel, value);
|
---|
3461 | }
|
---|
3462 | dest_pixels[x] = (uchar) value;
|
---|
3463 | }
|
---|
3464 | }
|
---|
3465 | } else {
|
---|
3466 | QVector<QRgb> table = clut;
|
---|
3467 | table.resize(2);
|
---|
3468 | for (int y=0; y<h; ++y) {
|
---|
3469 | QRgb *src_pixels = (QRgb *) src.scanLine(y);
|
---|
3470 | for (int x=0; x<w; ++x) {
|
---|
3471 | int src_pixel = src_pixels[x];
|
---|
3472 | int value = cache.value(src_pixel, -1);
|
---|
3473 | if (value == -1) {
|
---|
3474 | value = closestMatch(src_pixel, table);
|
---|
3475 | cache.insert(src_pixel, value);
|
---|
3476 | }
|
---|
3477 | dest.setPixel(x, y, value);
|
---|
3478 | }
|
---|
3479 | }
|
---|
3480 | }
|
---|
3481 |
|
---|
3482 | return dest;
|
---|
3483 | }
|
---|
3484 |
|
---|
3485 | /*!
|
---|
3486 | \overload
|
---|
3487 |
|
---|
3488 | Returns a copy of the image converted to the given \a format,
|
---|
3489 | using the specified \a colorTable.
|
---|
3490 |
|
---|
3491 | Conversion from 32 bit to 8 bit indexed is a slow operation and
|
---|
3492 | will use a straightforward nearest color approach, with no
|
---|
3493 | dithering.
|
---|
3494 | */
|
---|
3495 | QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const
|
---|
3496 | {
|
---|
3497 | if (d->format == format)
|
---|
3498 | return *this;
|
---|
3499 |
|
---|
3500 | if (format <= QImage::Format_Indexed8 && depth() == 32) {
|
---|
3501 | return convertWithPalette(*this, format, colorTable);
|
---|
3502 | }
|
---|
3503 |
|
---|
3504 | const Image_Converter *converterPtr = &converter_map[d->format][format];
|
---|
3505 | Image_Converter converter = *converterPtr;
|
---|
3506 | if (!converter)
|
---|
3507 | return QImage();
|
---|
3508 |
|
---|
3509 | QImage image(d->width, d->height, format);
|
---|
3510 | QIMAGE_SANITYCHECK_MEMORY(image);
|
---|
3511 |
|
---|
3512 | #if !defined(QT_NO_IMAGE_TEXT)
|
---|
3513 | image.d->text = d->text;
|
---|
3514 | #endif // !QT_NO_IMAGE_TEXT
|
---|
3515 |
|
---|
3516 | converter(image.d, d, flags);
|
---|
3517 | return image;
|
---|
3518 | }
|
---|
3519 |
|
---|
3520 | #ifdef QT3_SUPPORT
|
---|
3521 | /*!
|
---|
3522 | Converts the depth (bpp) of the image to the given \a depth and
|
---|
3523 | returns the converted image. The original image is not changed.
|
---|
3524 | Returns this image if \a depth is equal to the image depth, or a
|
---|
3525 | null image if this image cannot be converted. The \a depth
|
---|
3526 | argument must be 1, 8 or 32. If the image needs to be modified to
|
---|
3527 | fit in a lower-resolution result (e.g. converting from 32-bit to
|
---|
3528 | 8-bit), use the \a flags to specify how you'd prefer this to
|
---|
3529 | happen.
|
---|
3530 |
|
---|
3531 | Use the convertToFormat() function instead.
|
---|
3532 | */
|
---|
3533 |
|
---|
3534 | QImage QImage::convertDepth(int depth, Qt::ImageConversionFlags flags) const
|
---|
3535 | {
|
---|
3536 | if (!d || d->depth == depth)
|
---|
3537 | return *this;
|
---|
3538 |
|
---|
3539 | Format format = formatFor (depth, QImage::LittleEndian);
|
---|
3540 | return convertToFormat(format, flags);
|
---|
3541 | }
|
---|
3542 | #endif
|
---|
3543 |
|
---|
3544 | /*!
|
---|
3545 | \fn bool QImage::valid(const QPoint &pos) const
|
---|
3546 |
|
---|
3547 | Returns true if \a pos is a valid coordinate pair within the
|
---|
3548 | image; otherwise returns false.
|
---|
3549 |
|
---|
3550 | \sa rect(), QRect::contains()
|
---|
3551 | */
|
---|
3552 |
|
---|
3553 | /*!
|
---|
3554 | \overload
|
---|
3555 |
|
---|
3556 | Returns true if QPoint(\a x, \a y) is a valid coordinate pair
|
---|
3557 | within the image; otherwise returns false.
|
---|
3558 | */
|
---|
3559 | bool QImage::valid(int x, int y) const
|
---|
3560 | {
|
---|
3561 | return d
|
---|
3562 | && x >= 0 && x < d->width
|
---|
3563 | && y >= 0 && y < d->height;
|
---|
3564 | }
|
---|
3565 |
|
---|
3566 | /*!
|
---|
3567 | \fn int QImage::pixelIndex(const QPoint &position) const
|
---|
3568 |
|
---|
3569 | Returns the pixel index at the given \a position.
|
---|
3570 |
|
---|
3571 | If \a position is not valid, or if the image is not a paletted
|
---|
3572 | image (depth() > 8), the results are undefined.
|
---|
3573 |
|
---|
3574 | \sa valid(), depth(), {QImage#Pixel Manipulation}{Pixel Manipulation}
|
---|
3575 | */
|
---|
3576 |
|
---|
3577 | /*!
|
---|
3578 | \overload
|
---|
3579 |
|
---|
3580 | Returns the pixel index at (\a x, \a y).
|
---|
3581 | */
|
---|
3582 | int QImage::pixelIndex(int x, int y) const
|
---|
3583 | {
|
---|
3584 | if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
|
---|
3585 | qWarning("QImage::pixelIndex: coordinate (%d,%d) out of range", x, y);
|
---|
3586 | return -12345;
|
---|
3587 | }
|
---|
3588 | const uchar * s = scanLine(y);
|
---|
3589 | switch(d->format) {
|
---|
3590 | case Format_Mono:
|
---|
3591 | return (*(s + (x >> 3)) >> (7- (x & 7))) & 1;
|
---|
3592 | case Format_MonoLSB:
|
---|
3593 | return (*(s + (x >> 3)) >> (x & 7)) & 1;
|
---|
3594 | case Format_Indexed8:
|
---|
3595 | return (int)s[x];
|
---|
3596 | default:
|
---|
3597 | qWarning("QImage::pixelIndex: Not applicable for %d-bpp images (no palette)", d->depth);
|
---|
3598 | }
|
---|
3599 | return 0;
|
---|
3600 | }
|
---|
3601 |
|
---|
3602 |
|
---|
3603 | /*!
|
---|
3604 | \fn QRgb QImage::pixel(const QPoint &position) const
|
---|
3605 |
|
---|
3606 | Returns the color of the pixel at the given \a position.
|
---|
3607 |
|
---|
3608 | If the \a position is not valid, the results are undefined.
|
---|
3609 |
|
---|
3610 | \sa setPixel(), valid(), {QImage#Pixel Manipulation}{Pixel
|
---|
3611 | Manipulation}
|
---|
3612 | */
|
---|
3613 |
|
---|
3614 | /*!
|
---|
3615 | \overload
|
---|
3616 |
|
---|
3617 | Returns the color of the pixel at coordinates (\a x, \a y).
|
---|
3618 | */
|
---|
3619 | QRgb QImage::pixel(int x, int y) const
|
---|
3620 | {
|
---|
3621 | if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
|
---|
3622 | qWarning("QImage::pixel: coordinate (%d,%d) out of range", x, y);
|
---|
3623 | return 12345;
|
---|
3624 | }
|
---|
3625 | const uchar * s = scanLine(y);
|
---|
3626 | switch(d->format) {
|
---|
3627 | case Format_Mono:
|
---|
3628 | return d->colortable.at((*(s + (x >> 3)) >> (7- (x & 7))) & 1);
|
---|
3629 | case Format_MonoLSB:
|
---|
3630 | return d->colortable.at((*(s + (x >> 3)) >> (x & 7)) & 1);
|
---|
3631 | case Format_Indexed8:
|
---|
3632 | return d->colortable.at((int)s[x]);
|
---|
3633 | case Format_ARGB8565_Premultiplied:
|
---|
3634 | return qt_colorConvert<quint32, qargb8565>(reinterpret_cast<const qargb8565*>(s)[x], 0);
|
---|
3635 | case Format_RGB666:
|
---|
3636 | return qt_colorConvert<quint32, qrgb666>(reinterpret_cast<const qrgb666*>(s)[x], 0);
|
---|
3637 | case Format_ARGB6666_Premultiplied:
|
---|
3638 | return qt_colorConvert<quint32, qargb6666>(reinterpret_cast<const qargb6666*>(s)[x], 0);
|
---|
3639 | case Format_RGB555:
|
---|
3640 | return qt_colorConvert<quint32, qrgb555>(reinterpret_cast<const qrgb555*>(s)[x], 0);
|
---|
3641 | case Format_ARGB8555_Premultiplied:
|
---|
3642 | return qt_colorConvert<quint32, qargb8555>(reinterpret_cast<const qargb8555*>(s)[x], 0);
|
---|
3643 | case Format_RGB888:
|
---|
3644 | return qt_colorConvert<quint32, qrgb888>(reinterpret_cast<const qrgb888*>(s)[x], 0);
|
---|
3645 | case Format_RGB444:
|
---|
3646 | return qt_colorConvert<quint32, qrgb444>(reinterpret_cast<const qrgb444*>(s)[x], 0);
|
---|
3647 | case Format_ARGB4444_Premultiplied:
|
---|
3648 | return qt_colorConvert<quint32, qargb4444>(reinterpret_cast<const qargb4444*>(s)[x], 0);
|
---|
3649 | case Format_RGB16:
|
---|
3650 | return qt_colorConvert<quint32, quint16>(reinterpret_cast<const quint16*>(s)[x], 0);
|
---|
3651 | default:
|
---|
3652 | return ((QRgb*)s)[x];
|
---|
3653 | }
|
---|
3654 | }
|
---|
3655 |
|
---|
3656 |
|
---|
3657 | /*!
|
---|
3658 | \fn void QImage::setPixel(const QPoint &position, uint index_or_rgb)
|
---|
3659 |
|
---|
3660 | Sets the pixel index or color at the given \a position to \a
|
---|
3661 | index_or_rgb.
|
---|
3662 |
|
---|
3663 | If the image's format is either monochrome or 8-bit, the given \a
|
---|
3664 | index_or_rgb value must be an index in the image's color table,
|
---|
3665 | otherwise the parameter must be a QRgb value.
|
---|
3666 |
|
---|
3667 | If \a position is not a valid coordinate pair in the image, or if
|
---|
3668 | \a index_or_rgb >= numColors() in the case of monochrome and
|
---|
3669 | 8-bit images, the result is undefined.
|
---|
3670 |
|
---|
3671 | \warning This function is expensive due to the call of the internal
|
---|
3672 | \c{detach()} function called within; if performance is a concern, we
|
---|
3673 | recommend the use of \l{QImage::}{scanLine()} to access pixel data
|
---|
3674 | directly.
|
---|
3675 |
|
---|
3676 | \sa pixel(), {QImage#Pixel Manipulation}{Pixel Manipulation}
|
---|
3677 | */
|
---|
3678 |
|
---|
3679 | /*!
|
---|
3680 | \overload
|
---|
3681 |
|
---|
3682 | Sets the pixel index or color at (\a x, \a y) to \a index_or_rgb.
|
---|
3683 | */
|
---|
3684 | void QImage::setPixel(int x, int y, uint index_or_rgb)
|
---|
3685 | {
|
---|
3686 | if (!d || x < 0 || x >= width() || y < 0 || y >= height()) {
|
---|
3687 | qWarning("QImage::setPixel: coordinate (%d,%d) out of range", x, y);
|
---|
3688 | return;
|
---|
3689 | }
|
---|
3690 | // detach is called from within scanLine
|
---|
3691 | uchar * s = scanLine(y);
|
---|
3692 | const quint32p p = quint32p::fromRawData(index_or_rgb);
|
---|
3693 | switch(d->format) {
|
---|
3694 | case Format_Mono:
|
---|
3695 | case Format_MonoLSB:
|
---|
3696 | if (index_or_rgb > 1) {
|
---|
3697 | qWarning("QImage::setPixel: Index %d out of range", index_or_rgb);
|
---|
3698 | } else if (format() == Format_MonoLSB) {
|
---|
3699 | if (index_or_rgb==0)
|
---|
3700 | *(s + (x >> 3)) &= ~(1 << (x & 7));
|
---|
3701 | else
|
---|
3702 | *(s + (x >> 3)) |= (1 << (x & 7));
|
---|
3703 | } else {
|
---|
3704 | if (index_or_rgb==0)
|
---|
3705 | *(s + (x >> 3)) &= ~(1 << (7-(x & 7)));
|
---|
3706 | else
|
---|
3707 | *(s + (x >> 3)) |= (1 << (7-(x & 7)));
|
---|
3708 | }
|
---|
3709 | break;
|
---|
3710 | case Format_Indexed8:
|
---|
3711 | if (index_or_rgb > (uint)d->colortable.size()) {
|
---|
3712 | qWarning("QImage::setPixel: Index %d out of range", index_or_rgb);
|
---|
3713 | return;
|
---|
3714 | }
|
---|
3715 | s[x] = index_or_rgb;
|
---|
3716 | break;
|
---|
3717 | case Format_RGB32:
|
---|
3718 | //make sure alpha is 255, we depend on it in qdrawhelper for cases
|
---|
3719 | // when image is set as a texture pattern on a qbrush
|
---|
3720 | ((uint *)s)[x] = uint(255 << 24) | index_or_rgb;
|
---|
3721 | break;
|
---|
3722 | case Format_ARGB32:
|
---|
3723 | case Format_ARGB32_Premultiplied:
|
---|
3724 | ((uint *)s)[x] = index_or_rgb;
|
---|
3725 | break;
|
---|
3726 | case Format_RGB16:
|
---|
3727 | ((quint16 *)s)[x] = qt_colorConvert<quint16, quint32p>(p, 0);
|
---|
3728 | break;
|
---|
3729 | case Format_ARGB8565_Premultiplied:
|
---|
3730 | ((qargb8565*)s)[x] = qt_colorConvert<qargb8565, quint32p>(p, 0);
|
---|
3731 | break;
|
---|
3732 | case Format_RGB666:
|
---|
3733 | ((qrgb666*)s)[x] = qt_colorConvert<qrgb666, quint32p>(p, 0);
|
---|
3734 | break;
|
---|
3735 | case Format_ARGB6666_Premultiplied:
|
---|
3736 | ((qargb6666*)s)[x] = qt_colorConvert<qargb6666, quint32p>(p, 0);
|
---|
3737 | break;
|
---|
3738 | case Format_RGB555:
|
---|
3739 | ((qrgb555*)s)[x] = qt_colorConvert<qrgb555, quint32p>(p, 0);
|
---|
3740 | break;
|
---|
3741 | case Format_ARGB8555_Premultiplied:
|
---|
3742 | ((qargb8555*)s)[x] = qt_colorConvert<qargb8555, quint32p>(p, 0);
|
---|
3743 | break;
|
---|
3744 | case Format_RGB888:
|
---|
3745 | ((qrgb888*)s)[x] = qt_colorConvert<qrgb888, quint32p>(p, 0);
|
---|
3746 | break;
|
---|
3747 | case Format_RGB444:
|
---|
3748 | ((qrgb444*)s)[x] = qt_colorConvert<qrgb444, quint32p>(p, 0);
|
---|
3749 | break;
|
---|
3750 | case Format_ARGB4444_Premultiplied:
|
---|
3751 | ((qargb4444*)s)[x] = qt_colorConvert<qargb4444, quint32p>(p, 0);
|
---|
3752 | break;
|
---|
3753 | case Format_Invalid:
|
---|
3754 | case NImageFormats:
|
---|
3755 | Q_ASSERT(false);
|
---|
3756 | }
|
---|
3757 | }
|
---|
3758 |
|
---|
3759 | #ifdef QT3_SUPPORT
|
---|
3760 | /*!
|
---|
3761 | Converts the bit order of the image to the given \a bitOrder and
|
---|
3762 | returns the converted image. The original image is not changed.
|
---|
3763 | Returns this image if the given \a bitOrder is equal to the image
|
---|
3764 | current bit order, or a null image if this image cannot be
|
---|
3765 | converted.
|
---|
3766 |
|
---|
3767 | Use convertToFormat() instead.
|
---|
3768 | */
|
---|
3769 |
|
---|
3770 | QImage QImage::convertBitOrder(Endian bitOrder) const
|
---|
3771 | {
|
---|
3772 | if (!d || isNull() || d->depth != 1 || !(bitOrder == BigEndian || bitOrder == LittleEndian))
|
---|
3773 | return QImage();
|
---|
3774 |
|
---|
3775 | if ((d->format == Format_Mono && bitOrder == BigEndian)
|
---|
3776 | || (d->format == Format_MonoLSB && bitOrder == LittleEndian))
|
---|
3777 | return *this;
|
---|
3778 |
|
---|
3779 | QImage image(d->width, d->height, d->format == Format_Mono ? Format_MonoLSB : Format_Mono);
|
---|
3780 |
|
---|
3781 | const uchar *data = d->data;
|
---|
3782 | const uchar *end = data + d->nbytes;
|
---|
3783 | uchar *ndata = image.d->data;
|
---|
3784 | while (data < end)
|
---|
3785 | *ndata++ = bitflip[*data++];
|
---|
3786 |
|
---|
3787 | image.setDotsPerMeterX(dotsPerMeterX());
|
---|
3788 | image.setDotsPerMeterY(dotsPerMeterY());
|
---|
3789 |
|
---|
3790 | image.d->colortable = d->colortable;
|
---|
3791 | return image;
|
---|
3792 | }
|
---|
3793 | #endif
|
---|
3794 | /*!
|
---|
3795 | Returns true if all the colors in the image are shades of gray
|
---|
3796 | (i.e. their red, green and blue components are equal); otherwise
|
---|
3797 | false.
|
---|
3798 |
|
---|
3799 | Note that this function is slow for images without color table.
|
---|
3800 |
|
---|
3801 | \sa isGrayscale()
|
---|
3802 | */
|
---|
3803 | bool QImage::allGray() const
|
---|
3804 | {
|
---|
3805 | if (!d)
|
---|
3806 | return true;
|
---|
3807 |
|
---|
3808 | if (d->depth == 32) {
|
---|
3809 | int p = width()*height();
|
---|
3810 | const QRgb* b = (const QRgb*)bits();
|
---|
3811 | while (p--)
|
---|
3812 | if (!qIsGray(*b++))
|
---|
3813 | return false;
|
---|
3814 | } else if (d->depth == 16) {
|
---|
3815 | int p = width()*height();
|
---|
3816 | const ushort* b = (const ushort *)bits();
|
---|
3817 | while (p--)
|
---|
3818 | if (!qIsGray(qt_colorConvert<quint32, quint16>(*b++, 0)))
|
---|
3819 | return false;
|
---|
3820 | } else if (d->format == QImage::Format_RGB888) {
|
---|
3821 | int p = width()*height();
|
---|
3822 | const qrgb888* b = (const qrgb888 *)bits();
|
---|
3823 | while (p--)
|
---|
3824 | if (!qIsGray(qt_colorConvert<quint32, qrgb888>(*b++, 0)))
|
---|
3825 | return false;
|
---|
3826 | } else {
|
---|
3827 | if (d->colortable.isEmpty())
|
---|
3828 | return true;
|
---|
3829 | for (int i = 0; i < numColors(); i++)
|
---|
3830 | if (!qIsGray(d->colortable.at(i)))
|
---|
3831 | return false;
|
---|
3832 | }
|
---|
3833 | return true;
|
---|
3834 | }
|
---|
3835 |
|
---|
3836 | /*!
|
---|
3837 | For 32-bit images, this function is equivalent to allGray().
|
---|
3838 |
|
---|
3839 | For 8-bpp images, this function returns true if color(i) is
|
---|
3840 | QRgb(i, i, i) for all indexes of the color table; otherwise
|
---|
3841 | returns false.
|
---|
3842 |
|
---|
3843 | \sa allGray(), {QImage#Image Formats}{Image Formats}
|
---|
3844 | */
|
---|
3845 | bool QImage::isGrayscale() const
|
---|
3846 | {
|
---|
3847 | if (!d)
|
---|
3848 | return false;
|
---|
3849 |
|
---|
3850 | switch (depth()) {
|
---|
3851 | case 32:
|
---|
3852 | case 24:
|
---|
3853 | case 16:
|
---|
3854 | return allGray();
|
---|
3855 | case 8: {
|
---|
3856 | for (int i = 0; i < numColors(); i++)
|
---|
3857 | if (d->colortable.at(i) != qRgb(i,i,i))
|
---|
3858 | return false;
|
---|
3859 | return true;
|
---|
3860 | }
|
---|
3861 | }
|
---|
3862 | return false;
|
---|
3863 | }
|
---|
3864 |
|
---|
3865 |
|
---|
3866 | /*!
|
---|
3867 | \fn QImage QImage::smoothScale(int width, int height, Qt::AspectRatioMode mode) const
|
---|
3868 |
|
---|
3869 | Use scaled() instead.
|
---|
3870 |
|
---|
3871 | \oldcode
|
---|
3872 | QImage image;
|
---|
3873 | image.smoothScale(width, height, mode);
|
---|
3874 | \newcode
|
---|
3875 | QImage image;
|
---|
3876 | image.scaled(width, height, mode, Qt::SmoothTransformation);
|
---|
3877 | \endcode
|
---|
3878 | */
|
---|
3879 |
|
---|
3880 | /*!
|
---|
3881 | \fn QImage QImage::smoothScale(const QSize &size, Qt::AspectRatioMode mode) const
|
---|
3882 | \overload
|
---|
3883 |
|
---|
3884 | Use scaled() instead.
|
---|
3885 |
|
---|
3886 | \oldcode
|
---|
3887 | QImage image;
|
---|
3888 | image.smoothScale(size, mode);
|
---|
3889 | \newcode
|
---|
3890 | QImage image;
|
---|
3891 | image.scaled(size, mode, Qt::SmoothTransformation);
|
---|
3892 | \endcode
|
---|
3893 | */
|
---|
3894 |
|
---|
3895 | /*!
|
---|
3896 | \fn QImage QImage::scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode,
|
---|
3897 | Qt::TransformationMode transformMode) const
|
---|
3898 | \overload
|
---|
3899 |
|
---|
3900 | Returns a copy of the image scaled to a rectangle with the given
|
---|
3901 | \a width and \a height according to the given \a aspectRatioMode
|
---|
3902 | and \a transformMode.
|
---|
3903 |
|
---|
3904 | If either the \a width or the \a height is zero or negative, this
|
---|
3905 | function returns a null image.
|
---|
3906 | */
|
---|
3907 |
|
---|
3908 | /*!
|
---|
3909 | \fn QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode,
|
---|
3910 | Qt::TransformationMode transformMode) const
|
---|
3911 |
|
---|
3912 | Returns a copy of the image scaled to a rectangle defined by the
|
---|
3913 | given \a size according to the given \a aspectRatioMode and \a
|
---|
3914 | transformMode.
|
---|
3915 |
|
---|
3916 | \image qimage-scaling.png
|
---|
3917 |
|
---|
3918 | \list
|
---|
3919 | \i If \a aspectRatioMode is Qt::IgnoreAspectRatio, the image
|
---|
3920 | is scaled to \a size.
|
---|
3921 | \i If \a aspectRatioMode is Qt::KeepAspectRatio, the image is
|
---|
3922 | scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
|
---|
3923 | \i If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
|
---|
3924 | the image is scaled to a rectangle as small as possible
|
---|
3925 | outside \a size, preserving the aspect ratio.
|
---|
3926 | \endlist
|
---|
3927 |
|
---|
3928 | If the given \a size is empty, this function returns a null image.
|
---|
3929 |
|
---|
3930 | \sa isNull(), {QImage#Image Transformations}{Image
|
---|
3931 | Transformations}
|
---|
3932 | */
|
---|
3933 | QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
|
---|
3934 | {
|
---|
3935 | if (!d) {
|
---|
3936 | qWarning("QImage::scaled: Image is a null image");
|
---|
3937 | return QImage();
|
---|
3938 | }
|
---|
3939 | if (s.isEmpty())
|
---|
3940 | return QImage();
|
---|
3941 |
|
---|
3942 | QSize newSize = size();
|
---|
3943 | newSize.scale(s, aspectMode);
|
---|
3944 | if (newSize == size())
|
---|
3945 | return copy();
|
---|
3946 |
|
---|
3947 | QImage img;
|
---|
3948 | QTransform wm;
|
---|
3949 | wm.scale((qreal)newSize.width() / width(), (qreal)newSize.height() / height());
|
---|
3950 | img = transformed(wm, mode);
|
---|
3951 | return img;
|
---|
3952 | }
|
---|
3953 |
|
---|
3954 | /*!
|
---|
3955 | \fn QImage QImage::scaledToWidth(int width, Qt::TransformationMode mode) const
|
---|
3956 |
|
---|
3957 | Returns a scaled copy of the image. The returned image is scaled
|
---|
3958 | to the given \a width using the specified transformation \a
|
---|
3959 | mode.
|
---|
3960 |
|
---|
3961 | This function automatically calculates the height of the image so
|
---|
3962 | that its aspect ratio is preserved.
|
---|
3963 |
|
---|
3964 | If the given \a width is 0 or negative, a null image is returned.
|
---|
3965 |
|
---|
3966 | \sa {QImage#Image Transformations}{Image Transformations}
|
---|
3967 | */
|
---|
3968 | QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const
|
---|
3969 | {
|
---|
3970 | if (!d) {
|
---|
3971 | qWarning("QImage::scaleWidth: Image is a null image");
|
---|
3972 | return QImage();
|
---|
3973 | }
|
---|
3974 | if (w <= 0)
|
---|
3975 | return QImage();
|
---|
3976 |
|
---|
3977 | QTransform wm;
|
---|
3978 | qreal factor = (qreal) w / width();
|
---|
3979 | wm.scale(factor, factor);
|
---|
3980 | return transformed(wm, mode);
|
---|
3981 | }
|
---|
3982 |
|
---|
3983 | /*!
|
---|
3984 | \fn QImage QImage::scaledToHeight(int height, Qt::TransformationMode mode) const
|
---|
3985 |
|
---|
3986 | Returns a scaled copy of the image. The returned image is scaled
|
---|
3987 | to the given \a height using the specified transformation \a
|
---|
3988 | mode.
|
---|
3989 |
|
---|
3990 | This function automatically calculates the width of the image so that
|
---|
3991 | the ratio of the image is preserved.
|
---|
3992 |
|
---|
3993 | If the given \a height is 0 or negative, a null image is returned.
|
---|
3994 |
|
---|
3995 | \sa {QImage#Image Transformations}{Image Transformations}
|
---|
3996 | */
|
---|
3997 | QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const
|
---|
3998 | {
|
---|
3999 | if (!d) {
|
---|
4000 | qWarning("QImage::scaleHeight: Image is a null image");
|
---|
4001 | return QImage();
|
---|
4002 | }
|
---|
4003 | if (h <= 0)
|
---|
4004 | return QImage();
|
---|
4005 |
|
---|
4006 | QTransform wm;
|
---|
4007 | qreal factor = (qreal) h / height();
|
---|
4008 | wm.scale(factor, factor);
|
---|
4009 | return transformed(wm, mode);
|
---|
4010 | }
|
---|
4011 |
|
---|
4012 |
|
---|
4013 | /*!
|
---|
4014 | \fn QMatrix QImage::trueMatrix(const QMatrix &matrix, int width, int height)
|
---|
4015 |
|
---|
4016 | Returns the actual matrix used for transforming an image with the
|
---|
4017 | given \a width, \a height and \a matrix.
|
---|
4018 |
|
---|
4019 | When transforming an image using the transformed() function, the
|
---|
4020 | transformation matrix is internally adjusted to compensate for
|
---|
4021 | unwanted translation, i.e. transformed() returns the smallest
|
---|
4022 | image containing all transformed points of the original image.
|
---|
4023 | This function returns the modified matrix, which maps points
|
---|
4024 | correctly from the original image into the new image.
|
---|
4025 |
|
---|
4026 | \sa transformed(), {QImage#Image Transformations}{Image
|
---|
4027 | Transformations}
|
---|
4028 | */
|
---|
4029 | QMatrix QImage::trueMatrix(const QMatrix &matrix, int w, int h)
|
---|
4030 | {
|
---|
4031 | return trueMatrix(QTransform(matrix), w, h).toAffine();
|
---|
4032 | }
|
---|
4033 |
|
---|
4034 | /*!
|
---|
4035 | Returns a copy of the image that is transformed using the given
|
---|
4036 | transformation \a matrix and transformation \a mode.
|
---|
4037 |
|
---|
4038 | The transformation \a matrix is internally adjusted to compensate
|
---|
4039 | for unwanted translation; i.e. the image produced is the smallest
|
---|
4040 | image that contains all the transformed points of the original
|
---|
4041 | image. Use the trueMatrix() function to retrieve the actual matrix
|
---|
4042 | used for transforming an image.
|
---|
4043 |
|
---|
4044 | \sa trueMatrix(), {QImage#Image Transformations}{Image
|
---|
4045 | Transformations}
|
---|
4046 | */
|
---|
4047 | QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
|
---|
4048 | {
|
---|
4049 | return transformed(QTransform(matrix), mode);
|
---|
4050 | }
|
---|
4051 |
|
---|
4052 | /*!
|
---|
4053 | Builds and returns a 1-bpp mask from the alpha buffer in this
|
---|
4054 | image. Returns a null image if the image's format is
|
---|
4055 | QImage::Format_RGB32.
|
---|
4056 |
|
---|
4057 | The \a flags argument is a bitwise-OR of the
|
---|
4058 | Qt::ImageConversionFlags, and controls the conversion
|
---|
4059 | process. Passing 0 for flags sets all the default options.
|
---|
4060 |
|
---|
4061 | The returned image has little-endian bit order (i.e. the image's
|
---|
4062 | format is QImage::Format_MonoLSB), which you can convert to
|
---|
4063 | big-endian (QImage::Format_Mono) using the convertToFormat()
|
---|
4064 | function.
|
---|
4065 |
|
---|
4066 | \sa createHeuristicMask(), {QImage#Image Transformations}{Image
|
---|
4067 | Transformations}
|
---|
4068 | */
|
---|
4069 | QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const
|
---|
4070 | {
|
---|
4071 | if (!d || d->format == QImage::Format_RGB32)
|
---|
4072 | return QImage();
|
---|
4073 |
|
---|
4074 | if (d->depth == 1) {
|
---|
4075 | // A monochrome pixmap, with alpha channels on those two colors.
|
---|
4076 | // Pretty unlikely, so use less efficient solution.
|
---|
4077 | return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
|
---|
4078 | }
|
---|
4079 |
|
---|
4080 | QImage mask(d->width, d->height, Format_MonoLSB);
|
---|
4081 | dither_to_Mono(mask.d, d, flags, true);
|
---|
4082 | return mask;
|
---|
4083 | }
|
---|
4084 |
|
---|
4085 | #ifndef QT_NO_IMAGE_HEURISTIC_MASK
|
---|
4086 | /*!
|
---|
4087 | Creates and returns a 1-bpp heuristic mask for this image.
|
---|
4088 |
|
---|
4089 | The function works by selecting a color from one of the corners,
|
---|
4090 | then chipping away pixels of that color starting at all the edges.
|
---|
4091 | The four corners vote for which color is to be masked away. In
|
---|
4092 | case of a draw (this generally means that this function is not
|
---|
4093 | applicable to the image), the result is arbitrary.
|
---|
4094 |
|
---|
4095 | The returned image has little-endian bit order (i.e. the image's
|
---|
4096 | format is QImage::Format_MonoLSB), which you can convert to
|
---|
4097 | big-endian (QImage::Format_Mono) using the convertToFormat()
|
---|
4098 | function.
|
---|
4099 |
|
---|
4100 | If \a clipTight is true (the default) the mask is just large
|
---|
4101 | enough to cover the pixels; otherwise, the mask is larger than the
|
---|
4102 | data pixels.
|
---|
4103 |
|
---|
4104 | Note that this function disregards the alpha buffer.
|
---|
4105 |
|
---|
4106 | \sa createAlphaMask(), {QImage#Image Transformations}{Image
|
---|
4107 | Transformations}
|
---|
4108 | */
|
---|
4109 |
|
---|
4110 | QImage QImage::createHeuristicMask(bool clipTight) const
|
---|
4111 | {
|
---|
4112 | if (!d)
|
---|
4113 | return QImage();
|
---|
4114 |
|
---|
4115 | if (d->depth != 32) {
|
---|
4116 | QImage img32 = convertToFormat(Format_RGB32);
|
---|
4117 | return img32.createHeuristicMask(clipTight);
|
---|
4118 | }
|
---|
4119 |
|
---|
4120 | #define PIX(x,y) (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
|
---|
4121 |
|
---|
4122 | int w = width();
|
---|
4123 | int h = height();
|
---|
4124 | QImage m(w, h, Format_MonoLSB);
|
---|
4125 | m.setNumColors(2);
|
---|
4126 | m.setColor(0, QColor(Qt::color0).rgba());
|
---|
4127 | m.setColor(1, QColor(Qt::color1).rgba());
|
---|
4128 | m.fill(0xff);
|
---|
4129 |
|
---|
4130 | QRgb background = PIX(0,0);
|
---|
4131 | if (background != PIX(w-1,0) &&
|
---|
4132 | background != PIX(0,h-1) &&
|
---|
4133 | background != PIX(w-1,h-1)) {
|
---|
4134 | background = PIX(w-1,0);
|
---|
4135 | if (background != PIX(w-1,h-1) &&
|
---|
4136 | background != PIX(0,h-1) &&
|
---|
4137 | PIX(0,h-1) == PIX(w-1,h-1)) {
|
---|
4138 | background = PIX(w-1,h-1);
|
---|
4139 | }
|
---|
4140 | }
|
---|
4141 |
|
---|
4142 | int x,y;
|
---|
4143 | bool done = false;
|
---|
4144 | uchar *ypp, *ypc, *ypn;
|
---|
4145 | while(!done) {
|
---|
4146 | done = true;
|
---|
4147 | ypn = m.scanLine(0);
|
---|
4148 | ypc = 0;
|
---|
4149 | for (y = 0; y < h; y++) {
|
---|
4150 | ypp = ypc;
|
---|
4151 | ypc = ypn;
|
---|
4152 | ypn = (y == h-1) ? 0 : m.scanLine(y+1);
|
---|
4153 | QRgb *p = (QRgb *)scanLine(y);
|
---|
4154 | for (x = 0; x < w; x++) {
|
---|
4155 | // slowness here - it's possible to do six of these tests
|
---|
4156 | // together in one go. oh well.
|
---|
4157 | if ((x == 0 || y == 0 || x == w-1 || y == h-1 ||
|
---|
4158 | !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) ||
|
---|
4159 | !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) ||
|
---|
4160 | !(*(ypp + (x >> 3)) & (1 << (x & 7))) ||
|
---|
4161 | !(*(ypn + (x >> 3)) & (1 << (x & 7)))) &&
|
---|
4162 | ( (*(ypc + (x >> 3)) & (1 << (x & 7)))) &&
|
---|
4163 | ((*p & 0x00ffffff) == background)) {
|
---|
4164 | done = false;
|
---|
4165 | *(ypc + (x >> 3)) &= ~(1 << (x & 7));
|
---|
4166 | }
|
---|
4167 | p++;
|
---|
4168 | }
|
---|
4169 | }
|
---|
4170 | }
|
---|
4171 |
|
---|
4172 | if (!clipTight) {
|
---|
4173 | ypn = m.scanLine(0);
|
---|
4174 | ypc = 0;
|
---|
4175 | for (y = 0; y < h; y++) {
|
---|
4176 | ypp = ypc;
|
---|
4177 | ypc = ypn;
|
---|
4178 | ypn = (y == h-1) ? 0 : m.scanLine(y+1);
|
---|
4179 | QRgb *p = (QRgb *)scanLine(y);
|
---|
4180 | for (x = 0; x < w; x++) {
|
---|
4181 | if ((*p & 0x00ffffff) != background) {
|
---|
4182 | if (x > 0)
|
---|
4183 | *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7));
|
---|
4184 | if (x < w-1)
|
---|
4185 | *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7));
|
---|
4186 | if (y > 0)
|
---|
4187 | *(ypp + (x >> 3)) |= (1 << (x & 7));
|
---|
4188 | if (y < h-1)
|
---|
4189 | *(ypn + (x >> 3)) |= (1 << (x & 7));
|
---|
4190 | }
|
---|
4191 | p++;
|
---|
4192 | }
|
---|
4193 | }
|
---|
4194 | }
|
---|
4195 |
|
---|
4196 | #undef PIX
|
---|
4197 |
|
---|
4198 | return m;
|
---|
4199 | }
|
---|
4200 | #endif //QT_NO_IMAGE_HEURISTIC_MASK
|
---|
4201 |
|
---|
4202 | /*!
|
---|
4203 | Creates and returns a mask for this image based on the given \a
|
---|
4204 | color value. If the \a mode is MaskInColor (the default value),
|
---|
4205 | all pixels matching \a color will be opaque pixels in the mask. If
|
---|
4206 | \a mode is MaskOutColor, all pixels matching the given color will
|
---|
4207 | be transparent.
|
---|
4208 |
|
---|
4209 | \sa createAlphaMask(), createHeuristicMask()
|
---|
4210 | */
|
---|
4211 |
|
---|
4212 | QImage QImage::createMaskFromColor(QRgb color, Qt::MaskMode mode) const
|
---|
4213 | {
|
---|
4214 | if (!d)
|
---|
4215 | return QImage();
|
---|
4216 | QImage maskImage(size(), QImage::Format_MonoLSB);
|
---|
4217 | maskImage.fill(0);
|
---|
4218 | uchar *s = maskImage.bits();
|
---|
4219 |
|
---|
4220 | if (depth() == 32) {
|
---|
4221 | for (int h = 0; h < d->height; h++) {
|
---|
4222 | const uint *sl = (uint *) scanLine(h);
|
---|
4223 | for (int w = 0; w < d->width; w++) {
|
---|
4224 | if (sl[w] == color)
|
---|
4225 | *(s + (w >> 3)) |= (1 << (w & 7));
|
---|
4226 | }
|
---|
4227 | s += maskImage.bytesPerLine();
|
---|
4228 | }
|
---|
4229 | } else {
|
---|
4230 | for (int h = 0; h < d->height; h++) {
|
---|
4231 | for (int w = 0; w < d->width; w++) {
|
---|
4232 | if ((uint) pixel(w, h) == color)
|
---|
4233 | *(s + (w >> 3)) |= (1 << (w & 7));
|
---|
4234 | }
|
---|
4235 | s += maskImage.bytesPerLine();
|
---|
4236 | }
|
---|
4237 | }
|
---|
4238 | if (mode == Qt::MaskOutColor)
|
---|
4239 | maskImage.invertPixels();
|
---|
4240 | return maskImage;
|
---|
4241 | }
|
---|
4242 |
|
---|
4243 |
|
---|
4244 | /*
|
---|
4245 | This code is contributed by Philipp Lang,
|
---|
4246 | GeneriCom Software Germany (www.generi.com)
|
---|
4247 | under the terms of the QPL, Version 1.0
|
---|
4248 | */
|
---|
4249 |
|
---|
4250 | /*!
|
---|
4251 | \fn QImage QImage::mirror(bool horizontal, bool vertical) const
|
---|
4252 |
|
---|
4253 | Use mirrored() instead.
|
---|
4254 | */
|
---|
4255 |
|
---|
4256 | /*!
|
---|
4257 | Returns a mirror of the image, mirrored in the horizontal and/or
|
---|
4258 | the vertical direction depending on whether \a horizontal and \a
|
---|
4259 | vertical are set to true or false.
|
---|
4260 |
|
---|
4261 | Note that the original image is not changed.
|
---|
4262 |
|
---|
4263 | \sa {QImage#Image Transformations}{Image Transformations}
|
---|
4264 | */
|
---|
4265 | QImage QImage::mirrored(bool horizontal, bool vertical) const
|
---|
4266 | {
|
---|
4267 | if (!d)
|
---|
4268 | return QImage();
|
---|
4269 |
|
---|
4270 | if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
|
---|
4271 | return *this;
|
---|
4272 |
|
---|
4273 | int w = d->width;
|
---|
4274 | int h = d->height;
|
---|
4275 | // Create result image, copy colormap
|
---|
4276 | QImage result(d->width, d->height, d->format);
|
---|
4277 | result.d->colortable = d->colortable;
|
---|
4278 | result.d->has_alpha_clut = d->has_alpha_clut;
|
---|
4279 |
|
---|
4280 | if (depth() == 1)
|
---|
4281 | w = (w+7)/8;
|
---|
4282 | int dxi = horizontal ? -1 : 1;
|
---|
4283 | int dxs = horizontal ? w-1 : 0;
|
---|
4284 | int dyi = vertical ? -1 : 1;
|
---|
4285 | int dy = vertical ? h-1: 0;
|
---|
4286 |
|
---|
4287 | // 1 bit, 8 bit
|
---|
4288 | if (d->depth == 1 || d->depth == 8) {
|
---|
4289 | for (int sy = 0; sy < h; sy++, dy += dyi) {
|
---|
4290 | quint8* ssl = (quint8*)(d->data + sy*d->bytes_per_line);
|
---|
4291 | quint8* dsl = (quint8*)(result.d->data + dy*result.d->bytes_per_line);
|
---|
4292 | int dx = dxs;
|
---|
4293 | for (int sx = 0; sx < w; sx++, dx += dxi)
|
---|
4294 | dsl[dx] = ssl[sx];
|
---|
4295 | }
|
---|
4296 | }
|
---|
4297 | // 16 bit
|
---|
4298 | else if (d->depth == 16) {
|
---|
4299 | for (int sy = 0; sy < h; sy++, dy += dyi) {
|
---|
4300 | quint16* ssl = (quint16*)(d->data + sy*d->bytes_per_line);
|
---|
4301 | quint16* dsl = (quint16*)(result.d->data + dy*result.d->bytes_per_line);
|
---|
4302 | int dx = dxs;
|
---|
4303 | for (int sx = 0; sx < w; sx++, dx += dxi)
|
---|
4304 | dsl[dx] = ssl[sx];
|
---|
4305 | }
|
---|
4306 | }
|
---|
4307 | // 24 bit
|
---|
4308 | else if (d->depth == 24) {
|
---|
4309 | for (int sy = 0; sy < h; sy++, dy += dyi) {
|
---|
4310 | quint24* ssl = (quint24*)(d->data + sy*d->bytes_per_line);
|
---|
4311 | quint24* dsl = (quint24*)(result.d->data + dy*result.d->bytes_per_line);
|
---|
4312 | int dx = dxs;
|
---|
4313 | for (int sx = 0; sx < w; sx++, dx += dxi)
|
---|
4314 | dsl[dx] = ssl[sx];
|
---|
4315 | }
|
---|
4316 | }
|
---|
4317 | // 32 bit
|
---|
4318 | else if (d->depth == 32) {
|
---|
4319 | for (int sy = 0; sy < h; sy++, dy += dyi) {
|
---|
4320 | quint32* ssl = (quint32*)(d->data + sy*d->bytes_per_line);
|
---|
4321 | quint32* dsl = (quint32*)(result.d->data + dy*result.d->bytes_per_line);
|
---|
4322 | int dx = dxs;
|
---|
4323 | for (int sx = 0; sx < w; sx++, dx += dxi)
|
---|
4324 | dsl[dx] = ssl[sx];
|
---|
4325 | }
|
---|
4326 | }
|
---|
4327 |
|
---|
4328 | // special handling of 1 bit images for horizontal mirroring
|
---|
4329 | if (horizontal && d->depth == 1) {
|
---|
4330 | int shift = width() % 8;
|
---|
4331 | for (int y = h-1; y >= 0; y--) {
|
---|
4332 | quint8* a0 = (quint8*)(result.d->data + y*d->bytes_per_line);
|
---|
4333 | // Swap bytes
|
---|
4334 | quint8* a = a0+dxs;
|
---|
4335 | while (a >= a0) {
|
---|
4336 | *a = bitflip[*a];
|
---|
4337 | a--;
|
---|
4338 | }
|
---|
4339 | // Shift bits if unaligned
|
---|
4340 | if (shift != 0) {
|
---|
4341 | a = a0+dxs;
|
---|
4342 | quint8 c = 0;
|
---|
4343 | if (format() == Format_MonoLSB) {
|
---|
4344 | while (a >= a0) {
|
---|
4345 | quint8 nc = *a << shift;
|
---|
4346 | *a = (*a >> (8-shift)) | c;
|
---|
4347 | --a;
|
---|
4348 | c = nc;
|
---|
4349 | }
|
---|
4350 | } else {
|
---|
4351 | while (a >= a0) {
|
---|
4352 | quint8 nc = *a >> shift;
|
---|
4353 | *a = (*a << (8-shift)) | c;
|
---|
4354 | --a;
|
---|
4355 | c = nc;
|
---|
4356 | }
|
---|
4357 | }
|
---|
4358 | }
|
---|
4359 | }
|
---|
4360 | }
|
---|
4361 |
|
---|
4362 | return result;
|
---|
4363 | }
|
---|
4364 |
|
---|
4365 | /*!
|
---|
4366 | \fn QImage QImage::swapRGB() const
|
---|
4367 |
|
---|
4368 | Use rgbSwapped() instead.
|
---|
4369 |
|
---|
4370 | \omit
|
---|
4371 | Returns a QImage in which the values of the red and blue
|
---|
4372 | components of all pixels have been swapped, effectively converting
|
---|
4373 | an RGB image to an BGR image. The original QImage is not changed.
|
---|
4374 | \endomit
|
---|
4375 | */
|
---|
4376 |
|
---|
4377 | /*!
|
---|
4378 | Returns a QImage in which the values of the red and blue
|
---|
4379 | components of all pixels have been swapped, effectively converting
|
---|
4380 | an RGB image to an BGR image.
|
---|
4381 |
|
---|
4382 | The original QImage is not changed.
|
---|
4383 |
|
---|
4384 | \sa {QImage#Image Transformations}{Image Transformations}
|
---|
4385 | */
|
---|
4386 | QImage QImage::rgbSwapped() const
|
---|
4387 | {
|
---|
4388 | if (isNull())
|
---|
4389 | return *this;
|
---|
4390 | QImage res;
|
---|
4391 | switch (d->format) {
|
---|
4392 | case Format_Invalid:
|
---|
4393 | case NImageFormats:
|
---|
4394 | Q_ASSERT(false);
|
---|
4395 | break;
|
---|
4396 | case Format_Mono:
|
---|
4397 | case Format_MonoLSB:
|
---|
4398 | case Format_Indexed8:
|
---|
4399 | res = copy();
|
---|
4400 | for (int i = 0; i < res.d->colortable.size(); i++) {
|
---|
4401 | QRgb c = res.d->colortable.at(i);
|
---|
4402 | res.d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
|
---|
4403 | }
|
---|
4404 | break;
|
---|
4405 | case Format_RGB32:
|
---|
4406 | case Format_ARGB32:
|
---|
4407 | case Format_ARGB32_Premultiplied:
|
---|
4408 | res = QImage(d->width, d->height, d->format);
|
---|
4409 | for (int i = 0; i < d->height; i++) {
|
---|
4410 | uint *q = (uint*)res.scanLine(i);
|
---|
4411 | uint *p = (uint*)scanLine(i);
|
---|
4412 | uint *end = p + d->width;
|
---|
4413 | while (p < end) {
|
---|
4414 | *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | (*p & 0xff00ff00);
|
---|
4415 | p++;
|
---|
4416 | q++;
|
---|
4417 | }
|
---|
4418 | }
|
---|
4419 | break;
|
---|
4420 | case Format_RGB16:
|
---|
4421 | res = QImage(d->width, d->height, d->format);
|
---|
4422 | for (int i = 0; i < d->height; i++) {
|
---|
4423 | ushort *q = (ushort*)res.scanLine(i);
|
---|
4424 | const ushort *p = (const ushort*)scanLine(i);
|
---|
4425 | const ushort *end = p + d->width;
|
---|
4426 | while (p < end) {
|
---|
4427 | *q = ((*p << 11) & 0xf800) | ((*p >> 11) & 0x1f) | (*p & 0x07e0);
|
---|
4428 | p++;
|
---|
4429 | q++;
|
---|
4430 | }
|
---|
4431 | }
|
---|
4432 | break;
|
---|
4433 | case Format_ARGB8565_Premultiplied:
|
---|
4434 | res = QImage(d->width, d->height, d->format);
|
---|
4435 | for (int i = 0; i < d->height; i++) {
|
---|
4436 | quint8 *p = (quint8*)scanLine(i);
|
---|
4437 | const quint8 *end = p + d->width * sizeof(qargb8565);
|
---|
4438 | while (p < end) {
|
---|
4439 | quint16 *q = reinterpret_cast<quint16*>(p + 1);
|
---|
4440 | *q = ((*q << 11) & 0xf800) | ((*q >> 11) & 0x1f) | (*q & 0x07e0);
|
---|
4441 | p += sizeof(qargb8565);
|
---|
4442 | }
|
---|
4443 | }
|
---|
4444 | break;
|
---|
4445 | case Format_RGB666:
|
---|
4446 | res = QImage(d->width, d->height, d->format);
|
---|
4447 | for (int i = 0; i < d->height; i++) {
|
---|
4448 | qrgb666 *q = reinterpret_cast<qrgb666*>(res.scanLine(i));
|
---|
4449 | const qrgb666 *p = reinterpret_cast<const qrgb666*>(scanLine(i));
|
---|
4450 | const qrgb666 *end = p + d->width;
|
---|
4451 | while (p < end) {
|
---|
4452 | const QRgb rgb = quint32(*p++);
|
---|
4453 | *q++ = qRgb(qBlue(rgb), qGreen(rgb), qRed(rgb));
|
---|
4454 | }
|
---|
4455 | }
|
---|
4456 | break;
|
---|
4457 | case Format_ARGB6666_Premultiplied:
|
---|
4458 | res = QImage(d->width, d->height, d->format);
|
---|
4459 | for (int i = 0; i < d->height; i++) {
|
---|
4460 | qargb6666 *q = reinterpret_cast<qargb6666*>(res.scanLine(i));
|
---|
4461 | const qargb6666 *p = reinterpret_cast<const qargb6666*>(scanLine(i));
|
---|
4462 | const qargb6666 *end = p + d->width;
|
---|
4463 | while (p < end) {
|
---|
4464 | const QRgb rgb = quint32(*p++);
|
---|
4465 | *q++ = qRgba(qBlue(rgb), qGreen(rgb), qRed(rgb), qAlpha(rgb));
|
---|
4466 | }
|
---|
4467 | }
|
---|
4468 | break;
|
---|
4469 | case Format_RGB555:
|
---|
4470 | res = QImage(d->width, d->height, d->format);
|
---|
4471 | for (int i = 0; i < d->height; i++) {
|
---|
4472 | ushort *q = (ushort*)res.scanLine(i);
|
---|
4473 | const ushort *p = (const ushort*)scanLine(i);
|
---|
4474 | const ushort *end = p + d->width;
|
---|
4475 | while (p < end) {
|
---|
4476 | *q = ((*p << 10) & 0x7800) | ((*p >> 10) & 0x1f) | (*p & 0x83e0);
|
---|
4477 | p++;
|
---|
4478 | q++;
|
---|
4479 | }
|
---|
4480 | }
|
---|
4481 | break;
|
---|
4482 | case Format_ARGB8555_Premultiplied:
|
---|
4483 | res = QImage(d->width, d->height, d->format);
|
---|
4484 | for (int i = 0; i < d->height; i++) {
|
---|
4485 | quint8 *p = (quint8*)scanLine(i);
|
---|
4486 | const quint8 *end = p + d->width * sizeof(qargb8555);
|
---|
4487 | while (p < end) {
|
---|
4488 | quint16 *q = reinterpret_cast<quint16*>(p + 1);
|
---|
4489 | *q = ((*q << 10) & 0x7800) | ((*q >> 10) & 0x1f) | (*q & 0x83e0);
|
---|
4490 | p += sizeof(qargb8555);
|
---|
4491 | }
|
---|
4492 | }
|
---|
4493 | break;
|
---|
4494 | case Format_RGB888:
|
---|
4495 | res = QImage(d->width, d->height, d->format);
|
---|
4496 | for (int i = 0; i < d->height; i++) {
|
---|
4497 | quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i));
|
---|
4498 | const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i));
|
---|
4499 | const quint8 *end = p + d->width * sizeof(qrgb888);
|
---|
4500 | while (p < end) {
|
---|
4501 | q[0] = p[2];
|
---|
4502 | q[1] = p[1];
|
---|
4503 | q[2] = p[0];
|
---|
4504 | q += sizeof(qrgb888);
|
---|
4505 | p += sizeof(qrgb888);
|
---|
4506 | }
|
---|
4507 | }
|
---|
4508 | break;
|
---|
4509 | case Format_RGB444:
|
---|
4510 | res = QImage(d->width, d->height, d->format);
|
---|
4511 | for (int i = 0; i < d->height; i++) {
|
---|
4512 | quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i));
|
---|
4513 | const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i));
|
---|
4514 | const quint8 *end = p + d->width * sizeof(qrgb444);
|
---|
4515 | while (p < end) {
|
---|
4516 | q[0] = (p[0] & 0xf0) | ((p[1] & 0x0f) << 8);
|
---|
4517 | q[1] = ((p[0] & 0x0f) >> 8) | (p[1] & 0xf0);
|
---|
4518 | q += sizeof(qrgb444);
|
---|
4519 | p += sizeof(qrgb444);
|
---|
4520 | }
|
---|
4521 | }
|
---|
4522 | break;
|
---|
4523 | case Format_ARGB4444_Premultiplied:
|
---|
4524 | res = QImage(d->width, d->height, d->format);
|
---|
4525 | for (int i = 0; i < d->height; i++) {
|
---|
4526 | quint8 *q = reinterpret_cast<quint8*>(res.scanLine(i));
|
---|
4527 | const quint8 *p = reinterpret_cast<const quint8*>(scanLine(i));
|
---|
4528 | const quint8 *end = p + d->width * sizeof(qargb4444);
|
---|
4529 | while (p < end) {
|
---|
4530 | q[0] = (p[0] & 0xf0) | ((p[1] & 0x0f) << 8);
|
---|
4531 | q[1] = ((p[0] & 0x0f) >> 8) | (p[1] & 0xf0);
|
---|
4532 | q += sizeof(qargb4444);
|
---|
4533 | p += sizeof(qargb4444);
|
---|
4534 | }
|
---|
4535 | }
|
---|
4536 | break;
|
---|
4537 | }
|
---|
4538 | return res;
|
---|
4539 | }
|
---|
4540 |
|
---|
4541 | /*!
|
---|
4542 | Loads an image from the file with the given \a fileName. Returns true if
|
---|
4543 | the image was successfully loaded; otherwise returns false.
|
---|
4544 |
|
---|
4545 | The loader attempts to read the image using the specified \a format, e.g.,
|
---|
4546 | PNG or JPG. If \a format is not specified (which is the default), the
|
---|
4547 | loader probes the file for a header to guess the file format.
|
---|
4548 |
|
---|
4549 | The file name can either refer to an actual file on disk or to one
|
---|
4550 | of the application's embedded resources. See the
|
---|
4551 | \l{resources.html}{Resource System} overview for details on how to
|
---|
4552 | embed images and other resource files in the application's
|
---|
4553 | executable.
|
---|
4554 |
|
---|
4555 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
|
---|
4556 | */
|
---|
4557 |
|
---|
4558 | bool QImage::load(const QString &fileName, const char* format)
|
---|
4559 | {
|
---|
4560 | if (fileName.isEmpty())
|
---|
4561 | return false;
|
---|
4562 |
|
---|
4563 | QImage image = QImageReader(fileName, format).read();
|
---|
4564 | if (!image.isNull()) {
|
---|
4565 | operator=(image);
|
---|
4566 | return true;
|
---|
4567 | }
|
---|
4568 | return false;
|
---|
4569 | }
|
---|
4570 |
|
---|
4571 | /*!
|
---|
4572 | \overload
|
---|
4573 |
|
---|
4574 | This function reads a QImage from the given \a device. This can,
|
---|
4575 | for example, be used to load an image directly into a QByteArray.
|
---|
4576 | */
|
---|
4577 |
|
---|
4578 | bool QImage::load(QIODevice* device, const char* format)
|
---|
4579 | {
|
---|
4580 | QImage image = QImageReader(device, format).read();
|
---|
4581 | if(!image.isNull()) {
|
---|
4582 | operator=(image);
|
---|
4583 | return true;
|
---|
4584 | }
|
---|
4585 | return false;
|
---|
4586 | }
|
---|
4587 |
|
---|
4588 | /*!
|
---|
4589 | \fn bool QImage::loadFromData(const uchar *data, int len, const char *format)
|
---|
4590 |
|
---|
4591 | Loads an image from the first \a len bytes of the given binary \a
|
---|
4592 | data. Returns true if the image was successfully loaded; otherwise
|
---|
4593 | returns false.
|
---|
4594 |
|
---|
4595 | The loader attempts to read the image using the specified \a format, e.g.,
|
---|
4596 | PNG or JPG. If \a format is not specified (which is the default), the
|
---|
4597 | loader probes the file for a header to guess the file format.
|
---|
4598 |
|
---|
4599 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
|
---|
4600 | */
|
---|
4601 |
|
---|
4602 | bool QImage::loadFromData(const uchar *data, int len, const char *format)
|
---|
4603 | {
|
---|
4604 | QImage image = fromData(data, len, format);
|
---|
4605 | if (!image.isNull()) {
|
---|
4606 | operator=(image);
|
---|
4607 | return true;
|
---|
4608 | }
|
---|
4609 | return false;
|
---|
4610 | }
|
---|
4611 |
|
---|
4612 | /*!
|
---|
4613 | \fn bool QImage::loadFromData(const QByteArray &data, const char *format)
|
---|
4614 |
|
---|
4615 | \overload
|
---|
4616 |
|
---|
4617 | Loads an image from the given QByteArray \a data.
|
---|
4618 | */
|
---|
4619 |
|
---|
4620 | /*!
|
---|
4621 | \fn QImage QImage::fromData(const uchar *data, int size, const char *format)
|
---|
4622 |
|
---|
4623 | Constructs a QImage from the first \a size bytes of the given
|
---|
4624 | binary \a data. The loader attempts to read the image using the
|
---|
4625 | specified \a format. If \a format is not specified (which is the default),
|
---|
4626 | the loader probes the file for a header to guess the file format.
|
---|
4627 |
|
---|
4628 | If the loading of the image failed, this object is a null image.
|
---|
4629 |
|
---|
4630 | \sa load(), save(), {QImage#Reading and Writing Image
|
---|
4631 | Files}{Reading and Writing Image Files}
|
---|
4632 | */
|
---|
4633 | QImage QImage::fromData(const uchar *data, int size, const char *format)
|
---|
4634 | {
|
---|
4635 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
|
---|
4636 | QBuffer b;
|
---|
4637 | b.setData(a);
|
---|
4638 | b.open(QIODevice::ReadOnly);
|
---|
4639 | return QImageReader(&b, format).read();
|
---|
4640 | }
|
---|
4641 |
|
---|
4642 | /*!
|
---|
4643 | \fn QImage QImage::fromData(const QByteArray &data, const char *format)
|
---|
4644 |
|
---|
4645 | \overload
|
---|
4646 |
|
---|
4647 | Loads an image from the given QByteArray \a data.
|
---|
4648 | */
|
---|
4649 |
|
---|
4650 | /*!
|
---|
4651 | Saves the image to the file with the given \a fileName, using the
|
---|
4652 | given image file \a format and \a quality factor. If \a format is
|
---|
4653 | 0, QImage will attempt to guess the format by looking at \a fileName's
|
---|
4654 | suffix.
|
---|
4655 |
|
---|
4656 | The \a quality factor must be in the range 0 to 100 or -1. Specify
|
---|
4657 | 0 to obtain small compressed files, 100 for large uncompressed
|
---|
4658 | files, and -1 (the default) to use the default settings.
|
---|
4659 |
|
---|
4660 | Returns true if the image was successfully saved; otherwise
|
---|
4661 | returns false.
|
---|
4662 |
|
---|
4663 | \sa {QImage#Reading and Writing Image Files}{Reading and Writing
|
---|
4664 | Image Files}
|
---|
4665 | */
|
---|
4666 | bool QImage::save(const QString &fileName, const char *format, int quality) const
|
---|
4667 | {
|
---|
4668 | if (isNull())
|
---|
4669 | return false;
|
---|
4670 | QImageWriter writer(fileName, format);
|
---|
4671 | return d->doImageIO(this, &writer, quality);
|
---|
4672 | }
|
---|
4673 |
|
---|
4674 | /*!
|
---|
4675 | \overload
|
---|
4676 |
|
---|
4677 | This function writes a QImage to the given \a device.
|
---|
4678 |
|
---|
4679 | This can, for example, be used to save an image directly into a
|
---|
4680 | QByteArray:
|
---|
4681 |
|
---|
4682 | \snippet doc/src/snippets/image/image.cpp 0
|
---|
4683 | */
|
---|
4684 |
|
---|
4685 | bool QImage::save(QIODevice* device, const char* format, int quality) const
|
---|
4686 | {
|
---|
4687 | if (isNull())
|
---|
4688 | return false; // nothing to save
|
---|
4689 | QImageWriter writer(device, format);
|
---|
4690 | return d->doImageIO(this, &writer, quality);
|
---|
4691 | }
|
---|
4692 |
|
---|
4693 | /* \internal
|
---|
4694 | */
|
---|
4695 |
|
---|
4696 | bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const
|
---|
4697 | {
|
---|
4698 | if (quality > 100 || quality < -1)
|
---|
4699 | qWarning("QPixmap::save: Quality out of range [-1, 100]");
|
---|
4700 | if (quality >= 0)
|
---|
4701 | writer->setQuality(qMin(quality,100));
|
---|
4702 | return writer->write(*image);
|
---|
4703 | }
|
---|
4704 |
|
---|
4705 | /*****************************************************************************
|
---|
4706 | QImage stream functions
|
---|
4707 | *****************************************************************************/
|
---|
4708 | #if !defined(QT_NO_DATASTREAM)
|
---|
4709 | /*!
|
---|
4710 | \fn QDataStream &operator<<(QDataStream &stream, const QImage &image)
|
---|
4711 | \relates QImage
|
---|
4712 |
|
---|
4713 | Writes the given \a image to the given \a stream as a PNG image,
|
---|
4714 | or as a BMP image if the stream's version is 1. Note that writing
|
---|
4715 | the stream to a file will not produce a valid image file.
|
---|
4716 |
|
---|
4717 | \sa QImage::save(), {Format of the QDataStream Operators}
|
---|
4718 | */
|
---|
4719 |
|
---|
4720 | QDataStream &operator<<(QDataStream &s, const QImage &image)
|
---|
4721 | {
|
---|
4722 | if (s.version() >= 5) {
|
---|
4723 | if (image.isNull()) {
|
---|
4724 | s << (qint32) 0; // null image marker
|
---|
4725 | return s;
|
---|
4726 | } else {
|
---|
4727 | s << (qint32) 1;
|
---|
4728 | // continue ...
|
---|
4729 | }
|
---|
4730 | }
|
---|
4731 | QImageWriter writer(s.device(), s.version() == 1 ? "bmp" : "png");
|
---|
4732 | writer.write(image);
|
---|
4733 | return s;
|
---|
4734 | }
|
---|
4735 |
|
---|
4736 | /*!
|
---|
4737 | \fn QDataStream &operator>>(QDataStream &stream, QImage &image)
|
---|
4738 | \relates QImage
|
---|
4739 |
|
---|
4740 | Reads an image from the given \a stream and stores it in the given
|
---|
4741 | \a image.
|
---|
4742 |
|
---|
4743 | \sa QImage::load(), {Format of the QDataStream Operators}
|
---|
4744 | */
|
---|
4745 |
|
---|
4746 | QDataStream &operator>>(QDataStream &s, QImage &image)
|
---|
4747 | {
|
---|
4748 | if (s.version() >= 5) {
|
---|
4749 | qint32 nullMarker;
|
---|
4750 | s >> nullMarker;
|
---|
4751 | if (!nullMarker) {
|
---|
4752 | image = QImage(); // null image
|
---|
4753 | return s;
|
---|
4754 | }
|
---|
4755 | }
|
---|
4756 | image = QImageReader(s.device(), 0).read();
|
---|
4757 | return s;
|
---|
4758 | }
|
---|
4759 | #endif
|
---|
4760 |
|
---|
4761 |
|
---|
4762 | #ifdef QT3_SUPPORT
|
---|
4763 | /*!
|
---|
4764 | \fn QImage QImage::convertDepthWithPalette(int depth, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
|
---|
4765 |
|
---|
4766 | Returns an image with the given \a depth, using the \a
|
---|
4767 | palette_count colors pointed to by \a palette. If \a depth is 1 or
|
---|
4768 | 8, the returned image will have its color table ordered in the
|
---|
4769 | same way as \a palette.
|
---|
4770 |
|
---|
4771 | If the image needs to be modified to fit in a lower-resolution
|
---|
4772 | result (e.g. converting from 32-bit to 8-bit), use the \a flags to
|
---|
4773 | specify how you'd prefer this to happen.
|
---|
4774 |
|
---|
4775 | Note: currently no closest-color search is made. If colors are
|
---|
4776 | found that are not in the palette, the palette may not be used at
|
---|
4777 | all. This result should not be considered valid because it may
|
---|
4778 | change in future implementations.
|
---|
4779 |
|
---|
4780 | Currently inefficient for non-32-bit images.
|
---|
4781 |
|
---|
4782 | Use the convertToFormat() function in combination with the
|
---|
4783 | setColorTable() function instead.
|
---|
4784 | */
|
---|
4785 | QImage QImage::convertDepthWithPalette(int d, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
|
---|
4786 | {
|
---|
4787 | Format f = formatFor(d, QImage::LittleEndian);
|
---|
4788 | QVector<QRgb> colortable;
|
---|
4789 | for (int i = 0; i < palette_count; ++i)
|
---|
4790 | colortable.append(palette[i]);
|
---|
4791 | return convertToFormat(f, colortable, flags);
|
---|
4792 | }
|
---|
4793 |
|
---|
4794 | /*!
|
---|
4795 | \relates QImage
|
---|
4796 |
|
---|
4797 | Copies a block of pixels from \a src to \a dst. The pixels
|
---|
4798 | copied from source (src) are converted according to
|
---|
4799 | \a flags if it is incompatible with the destination
|
---|
4800 | (\a dst).
|
---|
4801 |
|
---|
4802 | \a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy is the
|
---|
4803 | top-left position in \a dst and \a sw, \a sh is the size of the
|
---|
4804 | copied block. The copying is clipped if areas outside \a src or \a
|
---|
4805 | dst are specified. If \a sw is -1, it is adjusted to
|
---|
4806 | src->width(). Similarly, if \a sh is -1, it is adjusted to
|
---|
4807 | src->height().
|
---|
4808 |
|
---|
4809 | Currently inefficient for non 32-bit images.
|
---|
4810 |
|
---|
4811 | Use copy() or QPainter::drawImage() instead.
|
---|
4812 | */
|
---|
4813 | void bitBlt(QImage *dst, int dx, int dy, const QImage *src, int sx, int sy, int sw, int sh,
|
---|
4814 | Qt::ImageConversionFlags flags)
|
---|
4815 | {
|
---|
4816 | if (dst->isNull() || src->isNull())
|
---|
4817 | return;
|
---|
4818 | QPainter p(dst);
|
---|
4819 | p.drawImage(QPoint(dx, dy), *src, QRect(sx, sy, sw, sh), flags);
|
---|
4820 | }
|
---|
4821 | #endif
|
---|
4822 |
|
---|
4823 | /*!
|
---|
4824 | \fn bool QImage::operator==(const QImage & image) const
|
---|
4825 |
|
---|
4826 | Returns true if this image and the given \a image have the same
|
---|
4827 | contents; otherwise returns false.
|
---|
4828 |
|
---|
4829 | The comparison can be slow, unless there is some obvious
|
---|
4830 | difference (e.g. different size or format), in which case the
|
---|
4831 | function will return quickly.
|
---|
4832 |
|
---|
4833 | \sa operator=()
|
---|
4834 | */
|
---|
4835 |
|
---|
4836 | bool QImage::operator==(const QImage & i) const
|
---|
4837 | {
|
---|
4838 | // same object, or shared?
|
---|
4839 | if (i.d == d)
|
---|
4840 | return true;
|
---|
4841 | if (!i.d || !d)
|
---|
4842 | return false;
|
---|
4843 |
|
---|
4844 | // obviously different stuff?
|
---|
4845 | if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format)
|
---|
4846 | return false;
|
---|
4847 |
|
---|
4848 | if (d->format != Format_RGB32) {
|
---|
4849 | if (d->colortable != i.d->colortable)
|
---|
4850 | return false;
|
---|
4851 | if (d->format >= Format_ARGB32) { // all bits defined
|
---|
4852 | const int n = d->width * d->depth / 8;
|
---|
4853 | if (n == d->bytes_per_line && n == i.d->bytes_per_line) {
|
---|
4854 | if (memcmp(bits(), i.bits(), d->nbytes))
|
---|
4855 | return false;
|
---|
4856 | } else {
|
---|
4857 | for (int y = 0; y < d->height; ++y) {
|
---|
4858 | if (memcmp(scanLine(y), i.scanLine(y), n))
|
---|
4859 | return false;
|
---|
4860 | }
|
---|
4861 | }
|
---|
4862 | } else {
|
---|
4863 | int w = width();
|
---|
4864 | int h = height();
|
---|
4865 | for (int y=0; y<h; ++y) {
|
---|
4866 | for (int x=0; x<w; ++x) {
|
---|
4867 | if (pixelIndex(x, y) != i.pixelIndex(x, y))
|
---|
4868 | return false;
|
---|
4869 | }
|
---|
4870 | }
|
---|
4871 | }
|
---|
4872 | } else {
|
---|
4873 | //alpha channel undefined, so we must mask it out
|
---|
4874 | for(int l = 0; l < d->height; l++) {
|
---|
4875 | int w = d->width;
|
---|
4876 | const uint *p1 = reinterpret_cast<const uint*>(scanLine(l));
|
---|
4877 | const uint *p2 = reinterpret_cast<const uint*>(i.scanLine(l));
|
---|
4878 | while (w--) {
|
---|
4879 | if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
|
---|
4880 | return false;
|
---|
4881 | }
|
---|
4882 | }
|
---|
4883 | }
|
---|
4884 | return true;
|
---|
4885 | }
|
---|
4886 |
|
---|
4887 |
|
---|
4888 | /*!
|
---|
4889 | \fn bool QImage::operator!=(const QImage & image) const
|
---|
4890 |
|
---|
4891 | Returns true if this image and the given \a image have different
|
---|
4892 | contents; otherwise returns false.
|
---|
4893 |
|
---|
4894 | The comparison can be slow, unless there is some obvious
|
---|
4895 | difference, such as different widths, in which case the function
|
---|
4896 | will return quickly.
|
---|
4897 |
|
---|
4898 | \sa operator=()
|
---|
4899 | */
|
---|
4900 |
|
---|
4901 | bool QImage::operator!=(const QImage & i) const
|
---|
4902 | {
|
---|
4903 | return !(*this == i);
|
---|
4904 | }
|
---|
4905 |
|
---|
4906 |
|
---|
4907 |
|
---|
4908 |
|
---|
4909 | /*!
|
---|
4910 | Returns the number of pixels that fit horizontally in a physical
|
---|
4911 | meter. Together with dotsPerMeterY(), this number defines the
|
---|
4912 | intended scale and aspect ratio of the image.
|
---|
4913 |
|
---|
4914 | \sa setDotsPerMeterX(), {QImage#Image Information}{Image
|
---|
4915 | Information}
|
---|
4916 | */
|
---|
4917 | int QImage::dotsPerMeterX() const
|
---|
4918 | {
|
---|
4919 | return d ? qRound(d->dpmx) : 0;
|
---|
4920 | }
|
---|
4921 |
|
---|
4922 | /*!
|
---|
4923 | Returns the number of pixels that fit vertically in a physical
|
---|
4924 | meter. Together with dotsPerMeterX(), this number defines the
|
---|
4925 | intended scale and aspect ratio of the image.
|
---|
4926 |
|
---|
4927 | \sa setDotsPerMeterY(), {QImage#Image Information}{Image
|
---|
4928 | Information}
|
---|
4929 | */
|
---|
4930 | int QImage::dotsPerMeterY() const
|
---|
4931 | {
|
---|
4932 | return d ? qRound(d->dpmy) : 0;
|
---|
4933 | }
|
---|
4934 |
|
---|
4935 | /*!
|
---|
4936 | Sets the number of pixels that fit horizontally in a physical
|
---|
4937 | meter, to \a x.
|
---|
4938 |
|
---|
4939 | Together with dotsPerMeterY(), this number defines the intended
|
---|
4940 | scale and aspect ratio of the image, and determines the scale
|
---|
4941 | at which QPainter will draw graphics on the image. It does not
|
---|
4942 | change the scale or aspect ratio of the image when it is rendered
|
---|
4943 | on other paint devices.
|
---|
4944 |
|
---|
4945 | \sa dotsPerMeterX(), {QImage#Image Information}{Image Information}
|
---|
4946 | */
|
---|
4947 | void QImage::setDotsPerMeterX(int x)
|
---|
4948 | {
|
---|
4949 | if (!d || !x)
|
---|
4950 | return;
|
---|
4951 | detach();
|
---|
4952 |
|
---|
4953 | if (d)
|
---|
4954 | d->dpmx = x;
|
---|
4955 | }
|
---|
4956 |
|
---|
4957 | /*!
|
---|
4958 | Sets the number of pixels that fit vertically in a physical meter,
|
---|
4959 | to \a y.
|
---|
4960 |
|
---|
4961 | Together with dotsPerMeterX(), this number defines the intended
|
---|
4962 | scale and aspect ratio of the image, and determines the scale
|
---|
4963 | at which QPainter will draw graphics on the image. It does not
|
---|
4964 | change the scale or aspect ratio of the image when it is rendered
|
---|
4965 | on other paint devices.
|
---|
4966 |
|
---|
4967 | \sa dotsPerMeterY(), {QImage#Image Information}{Image Information}
|
---|
4968 | */
|
---|
4969 | void QImage::setDotsPerMeterY(int y)
|
---|
4970 | {
|
---|
4971 | if (!d || !y)
|
---|
4972 | return;
|
---|
4973 | detach();
|
---|
4974 |
|
---|
4975 | if (d)
|
---|
4976 | d->dpmy = y;
|
---|
4977 | }
|
---|
4978 |
|
---|
4979 | /*!
|
---|
4980 | \fn QPoint QImage::offset() const
|
---|
4981 |
|
---|
4982 | Returns the number of pixels by which the image is intended to be
|
---|
4983 | offset by when positioning relative to other images.
|
---|
4984 |
|
---|
4985 | \sa setOffset(), {QImage#Image Information}{Image Information}
|
---|
4986 | */
|
---|
4987 | QPoint QImage::offset() const
|
---|
4988 | {
|
---|
4989 | return d ? d->offset : QPoint();
|
---|
4990 | }
|
---|
4991 |
|
---|
4992 |
|
---|
4993 | /*!
|
---|
4994 | \fn void QImage::setOffset(const QPoint& offset)
|
---|
4995 |
|
---|
4996 | Sets the the number of pixels by which the image is intended to be
|
---|
4997 | offset by when positioning relative to other images, to \a offset.
|
---|
4998 |
|
---|
4999 | \sa offset(), {QImage#Image Information}{Image Information}
|
---|
5000 | */
|
---|
5001 | void QImage::setOffset(const QPoint& p)
|
---|
5002 | {
|
---|
5003 | if (!d)
|
---|
5004 | return;
|
---|
5005 | detach();
|
---|
5006 |
|
---|
5007 | if (d)
|
---|
5008 | d->offset = p;
|
---|
5009 | }
|
---|
5010 | #ifndef QT_NO_IMAGE_TEXT
|
---|
5011 |
|
---|
5012 | /*!
|
---|
5013 | Returns the text keys for this image.
|
---|
5014 |
|
---|
5015 | You can use these keys with text() to list the image text for a
|
---|
5016 | certain key.
|
---|
5017 |
|
---|
5018 | \sa text()
|
---|
5019 | */
|
---|
5020 | QStringList QImage::textKeys() const
|
---|
5021 | {
|
---|
5022 | return d ? QStringList(d->text.keys()) : QStringList();
|
---|
5023 | }
|
---|
5024 |
|
---|
5025 | /*!
|
---|
5026 | Returns the image text associated with the given \a key. If the
|
---|
5027 | specified \a key is an empty string, the whole image text is
|
---|
5028 | returned, with each key-text pair separated by a newline.
|
---|
5029 |
|
---|
5030 | \sa setText(), textKeys()
|
---|
5031 | */
|
---|
5032 | QString QImage::text(const QString &key) const
|
---|
5033 | {
|
---|
5034 | if (!d)
|
---|
5035 | return QString();
|
---|
5036 |
|
---|
5037 | if (!key.isEmpty())
|
---|
5038 | return d->text.value(key);
|
---|
5039 |
|
---|
5040 | QString tmp;
|
---|
5041 | foreach (const QString &key, d->text.keys()) {
|
---|
5042 | if (!tmp.isEmpty())
|
---|
5043 | tmp += QLatin1String("\n\n");
|
---|
5044 | tmp += key + QLatin1String(": ") + d->text.value(key).simplified();
|
---|
5045 | }
|
---|
5046 | return tmp;
|
---|
5047 | }
|
---|
5048 |
|
---|
5049 | /*!
|
---|
5050 | \fn void QImage::setText(const QString &key, const QString &text)
|
---|
5051 |
|
---|
5052 | Sets the image text to the given \a text and associate it with the
|
---|
5053 | given \a key.
|
---|
5054 |
|
---|
5055 | If you just want to store a single text block (i.e., a "comment"
|
---|
5056 | or just a description), you can either pass an empty key, or use a
|
---|
5057 | generic key like "Description".
|
---|
5058 |
|
---|
5059 | The image text is embedded into the image data when you
|
---|
5060 | call save() or QImageWriter::write().
|
---|
5061 |
|
---|
5062 | Not all image formats support embedded text. You can find out
|
---|
5063 | if a specific image or format supports embedding text
|
---|
5064 | by using QImageWriter::supportsOption(). We give an example:
|
---|
5065 |
|
---|
5066 | \snippet doc/src/snippets/image/supportedformat.cpp 0
|
---|
5067 |
|
---|
5068 | You can use QImageWriter::supportedImageFormats() to find out
|
---|
5069 | which image formats are available to you.
|
---|
5070 |
|
---|
5071 | \sa text(), textKeys()
|
---|
5072 | */
|
---|
5073 | void QImage::setText(const QString &key, const QString &value)
|
---|
5074 | {
|
---|
5075 | if (!d)
|
---|
5076 | return;
|
---|
5077 | detach();
|
---|
5078 |
|
---|
5079 | if (d)
|
---|
5080 | d->text.insert(key, value);
|
---|
5081 | }
|
---|
5082 |
|
---|
5083 | /*!
|
---|
5084 | \fn QString QImage::text(const char* key, const char* language) const
|
---|
5085 | \obsolete
|
---|
5086 |
|
---|
5087 | Returns the text recorded for the given \a key in the given \a
|
---|
5088 | language, or in a default language if \a language is 0.
|
---|
5089 |
|
---|
5090 | Use text() instead.
|
---|
5091 |
|
---|
5092 | The language the text is recorded in is no longer relevant since
|
---|
5093 | the text is always set using QString and UTF-8 representation.
|
---|
5094 | */
|
---|
5095 | QString QImage::text(const char* key, const char* lang) const
|
---|
5096 | {
|
---|
5097 | if (!d)
|
---|
5098 | return QString();
|
---|
5099 | QString k = QString::fromAscii(key);
|
---|
5100 | if (lang && *lang)
|
---|
5101 | k += QLatin1Char('/') + QString::fromAscii(lang);
|
---|
5102 | return d->text.value(k);
|
---|
5103 | }
|
---|
5104 |
|
---|
5105 | /*!
|
---|
5106 | \fn QString QImage::text(const QImageTextKeyLang& keywordAndLanguage) const
|
---|
5107 | \overload
|
---|
5108 | \obsolete
|
---|
5109 |
|
---|
5110 | Returns the text recorded for the given \a keywordAndLanguage.
|
---|
5111 |
|
---|
5112 | Use text() instead.
|
---|
5113 |
|
---|
5114 | The language the text is recorded in is no longer relevant since
|
---|
5115 | the text is always set using QString and UTF-8 representation.
|
---|
5116 | */
|
---|
5117 | QString QImage::text(const QImageTextKeyLang& kl) const
|
---|
5118 | {
|
---|
5119 | if (!d)
|
---|
5120 | return QString();
|
---|
5121 | QString k = QString::fromAscii(kl.key);
|
---|
5122 | if (!kl.lang.isEmpty())
|
---|
5123 | k += QLatin1Char('/') + QString::fromAscii(kl.lang);
|
---|
5124 | return d->text.value(k);
|
---|
5125 | }
|
---|
5126 |
|
---|
5127 | /*!
|
---|
5128 | \obsolete
|
---|
5129 |
|
---|
5130 | Returns the language identifiers for which some texts are
|
---|
5131 | recorded. Note that if you want to iterate over the list, you
|
---|
5132 | should iterate over a copy.
|
---|
5133 |
|
---|
5134 | The language the text is recorded in is no longer relevant since
|
---|
5135 | the text is always set using QString and UTF-8 representation.
|
---|
5136 | */
|
---|
5137 | QStringList QImage::textLanguages() const
|
---|
5138 | {
|
---|
5139 | if (!d)
|
---|
5140 | return QStringList();
|
---|
5141 | QStringList keys = textKeys();
|
---|
5142 | QStringList languages;
|
---|
5143 | for (int i = 0; i < keys.size(); ++i) {
|
---|
5144 | int index = keys.at(i).indexOf(QLatin1Char('/'));
|
---|
5145 | if (index > 0)
|
---|
5146 | languages += keys.at(i).mid(index+1);
|
---|
5147 | }
|
---|
5148 |
|
---|
5149 | return languages;
|
---|
5150 | }
|
---|
5151 |
|
---|
5152 | /*!
|
---|
5153 | \obsolete
|
---|
5154 |
|
---|
5155 | Returns a list of QImageTextKeyLang objects that enumerate all the
|
---|
5156 | texts key/language pairs set for this image.
|
---|
5157 |
|
---|
5158 | Use textKeys() instead.
|
---|
5159 |
|
---|
5160 | The language the text is recorded in is no longer relevant since
|
---|
5161 | the text is always set using QString and UTF-8 representation.
|
---|
5162 | */
|
---|
5163 | QList<QImageTextKeyLang> QImage::textList() const
|
---|
5164 | {
|
---|
5165 | QList<QImageTextKeyLang> imageTextKeys;
|
---|
5166 | if (!d)
|
---|
5167 | return imageTextKeys;
|
---|
5168 | QStringList keys = textKeys();
|
---|
5169 | for (int i = 0; i < keys.size(); ++i) {
|
---|
5170 | int index = keys.at(i).indexOf(QLatin1Char('/'));
|
---|
5171 | if (index > 0) {
|
---|
5172 | QImageTextKeyLang tkl;
|
---|
5173 | tkl.key = keys.at(i).left(index).toAscii();
|
---|
5174 | tkl.lang = keys.at(i).mid(index+1).toAscii();
|
---|
5175 | imageTextKeys += tkl;
|
---|
5176 | }
|
---|
5177 | }
|
---|
5178 |
|
---|
5179 | return imageTextKeys;
|
---|
5180 | }
|
---|
5181 |
|
---|
5182 | /*!
|
---|
5183 | \fn void QImage::setText(const char* key, const char* language, const QString& text)
|
---|
5184 | \obsolete
|
---|
5185 |
|
---|
5186 | Sets the image text to the given \a text and associate it with the
|
---|
5187 | given \a key. The text is recorded in the specified \a language,
|
---|
5188 | or in a default language if \a language is 0.
|
---|
5189 |
|
---|
5190 | Use setText() instead.
|
---|
5191 |
|
---|
5192 | The language the text is recorded in is no longer relevant since
|
---|
5193 | the text is always set using QString and UTF-8 representation.
|
---|
5194 |
|
---|
5195 | \omit
|
---|
5196 | Records string \a for the keyword \a key. The \a key should be
|
---|
5197 | a portable keyword recognizable by other software - some suggested
|
---|
5198 | values can be found in
|
---|
5199 | \l{http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.Anc-text}
|
---|
5200 | {the PNG specification}. \a s can be any text. \a lang should
|
---|
5201 | specify the language code (see
|
---|
5202 | \l{http://www.rfc-editor.org/rfc/rfc1766.txt}{RFC 1766}) or 0.
|
---|
5203 | \endomit
|
---|
5204 | */
|
---|
5205 | void QImage::setText(const char* key, const char* lang, const QString& s)
|
---|
5206 | {
|
---|
5207 | if (!d)
|
---|
5208 | return;
|
---|
5209 | detach();
|
---|
5210 |
|
---|
5211 | // In case detach() ran out of memory
|
---|
5212 | if (!d)
|
---|
5213 | return;
|
---|
5214 |
|
---|
5215 | QString k = QString::fromAscii(key);
|
---|
5216 | if (lang && *lang)
|
---|
5217 | k += QLatin1Char('/') + QString::fromAscii(lang);
|
---|
5218 | d->text.insert(k, s);
|
---|
5219 | }
|
---|
5220 |
|
---|
5221 | #endif // QT_NO_IMAGE_TEXT
|
---|
5222 |
|
---|
5223 | /*
|
---|
5224 | Sets the image bits to the \a pixmap contents and returns a
|
---|
5225 | reference to the image.
|
---|
5226 |
|
---|
5227 | If the image shares data with other images, it will first
|
---|
5228 | dereference the shared data.
|
---|
5229 |
|
---|
5230 | Makes a call to QPixmap::convertToImage().
|
---|
5231 | */
|
---|
5232 |
|
---|
5233 | /*! \fn QImage::Endian QImage::systemBitOrder()
|
---|
5234 |
|
---|
5235 | Determines the bit order of the display hardware. Returns
|
---|
5236 | QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
|
---|
5237 |
|
---|
5238 | This function is no longer relevant for QImage. Use QSysInfo
|
---|
5239 | instead.
|
---|
5240 | */
|
---|
5241 |
|
---|
5242 |
|
---|
5243 | /*!
|
---|
5244 | \internal
|
---|
5245 |
|
---|
5246 | Used by QPainter to retrieve a paint engine for the image.
|
---|
5247 | */
|
---|
5248 |
|
---|
5249 | QPaintEngine *QImage::paintEngine() const
|
---|
5250 | {
|
---|
5251 | if (!d)
|
---|
5252 | return 0;
|
---|
5253 |
|
---|
5254 | #ifdef QT_RASTER_IMAGEENGINE
|
---|
5255 | if (!d->paintEngine) {
|
---|
5256 | d->paintEngine = new QRasterPaintEngine(const_cast<QImage *>(this));
|
---|
5257 | }
|
---|
5258 | #endif
|
---|
5259 | return d->paintEngine;
|
---|
5260 | }
|
---|
5261 |
|
---|
5262 |
|
---|
5263 | /*!
|
---|
5264 | \reimp
|
---|
5265 |
|
---|
5266 | Returns the size for the specified \a metric on the device.
|
---|
5267 | */
|
---|
5268 | int QImage::metric(PaintDeviceMetric metric) const
|
---|
5269 | {
|
---|
5270 | if (!d)
|
---|
5271 | return 0;
|
---|
5272 |
|
---|
5273 | switch (metric) {
|
---|
5274 | case PdmWidth:
|
---|
5275 | return d->width;
|
---|
5276 | break;
|
---|
5277 |
|
---|
5278 | case PdmHeight:
|
---|
5279 | return d->height;
|
---|
5280 | break;
|
---|
5281 |
|
---|
5282 | case PdmWidthMM:
|
---|
5283 | return qRound(d->width * 1000 / d->dpmx);
|
---|
5284 | break;
|
---|
5285 |
|
---|
5286 | case PdmHeightMM:
|
---|
5287 | return qRound(d->height * 1000 / d->dpmy);
|
---|
5288 | break;
|
---|
5289 |
|
---|
5290 | case PdmNumColors:
|
---|
5291 | return d->colortable.size();
|
---|
5292 | break;
|
---|
5293 |
|
---|
5294 | case PdmDepth:
|
---|
5295 | return d->depth;
|
---|
5296 | break;
|
---|
5297 |
|
---|
5298 | case PdmDpiX:
|
---|
5299 | return qRound(d->dpmx * 0.0254);
|
---|
5300 | break;
|
---|
5301 |
|
---|
5302 | case PdmDpiY:
|
---|
5303 | return qRound(d->dpmy * 0.0254);
|
---|
5304 | break;
|
---|
5305 |
|
---|
5306 | case PdmPhysicalDpiX:
|
---|
5307 | return qRound(d->dpmx * 0.0254);
|
---|
5308 | break;
|
---|
5309 |
|
---|
5310 | case PdmPhysicalDpiY:
|
---|
5311 | return qRound(d->dpmy * 0.0254);
|
---|
5312 | break;
|
---|
5313 |
|
---|
5314 | default:
|
---|
5315 | qWarning("QImage::metric(): Unhandled metric type %d", metric);
|
---|
5316 | break;
|
---|
5317 | }
|
---|
5318 | return 0;
|
---|
5319 | }
|
---|
5320 |
|
---|
5321 |
|
---|
5322 |
|
---|
5323 | /*****************************************************************************
|
---|
5324 | QPixmap (and QImage) helper functions
|
---|
5325 | *****************************************************************************/
|
---|
5326 | /*
|
---|
5327 | This internal function contains the common (i.e. platform independent) code
|
---|
5328 | to do a transformation of pixel data. It is used by QPixmap::transform() and by
|
---|
5329 | QImage::transform().
|
---|
5330 |
|
---|
5331 | \a trueMat is the true transformation matrix (see QPixmap::trueMatrix()) and
|
---|
5332 | \a xoffset is an offset to the matrix.
|
---|
5333 |
|
---|
5334 | \a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a
|
---|
5335 | depth specifies the colordepth of the data.
|
---|
5336 |
|
---|
5337 | \a dptr is a pointer to the destination data, \a dbpl specifies the bits per
|
---|
5338 | line for the destination data, \a p_inc is the offset that we advance for
|
---|
5339 | every scanline and \a dHeight is the height of the destination image.
|
---|
5340 |
|
---|
5341 | \a sprt is the pointer to the source data, \a sbpl specifies the bits per
|
---|
5342 | line of the source data, \a sWidth and \a sHeight are the width and height of
|
---|
5343 | the source data.
|
---|
5344 | */
|
---|
5345 |
|
---|
5346 | #undef IWX_MSB
|
---|
5347 | #define IWX_MSB(b) if (trigx < maxws && trigy < maxhs) { \
|
---|
5348 | if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
|
---|
5349 | (1 << (7-((trigx>>12)&7)))) \
|
---|
5350 | *dptr |= b; \
|
---|
5351 | } \
|
---|
5352 | trigx += m11; \
|
---|
5353 | trigy += m12;
|
---|
5354 | // END OF MACRO
|
---|
5355 | #undef IWX_LSB
|
---|
5356 | #define IWX_LSB(b) if (trigx < maxws && trigy < maxhs) { \
|
---|
5357 | if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
|
---|
5358 | (1 << ((trigx>>12)&7))) \
|
---|
5359 | *dptr |= b; \
|
---|
5360 | } \
|
---|
5361 | trigx += m11; \
|
---|
5362 | trigy += m12;
|
---|
5363 | // END OF MACRO
|
---|
5364 | #undef IWX_PIX
|
---|
5365 | #define IWX_PIX(b) if (trigx < maxws && trigy < maxhs) { \
|
---|
5366 | if ((*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
|
---|
5367 | (1 << (7-((trigx>>12)&7)))) == 0) \
|
---|
5368 | *dptr &= ~b; \
|
---|
5369 | } \
|
---|
5370 | trigx += m11; \
|
---|
5371 | trigy += m12;
|
---|
5372 | // END OF MACRO
|
---|
5373 | bool qt_xForm_helper(const QTransform &trueMat, int xoffset, int type, int depth,
|
---|
5374 | uchar *dptr, int dbpl, int p_inc, int dHeight,
|
---|
5375 | const uchar *sptr, int sbpl, int sWidth, int sHeight)
|
---|
5376 | {
|
---|
5377 | int m11 = int(trueMat.m11()*4096.0);
|
---|
5378 | int m12 = int(trueMat.m12()*4096.0);
|
---|
5379 | int m21 = int(trueMat.m21()*4096.0);
|
---|
5380 | int m22 = int(trueMat.m22()*4096.0);
|
---|
5381 | int dx = qRound(trueMat.dx()*4096.0);
|
---|
5382 | int dy = qRound(trueMat.dy()*4096.0);
|
---|
5383 |
|
---|
5384 | int m21ydx = dx + (xoffset<<16) + (m11 + m21) / 2;
|
---|
5385 | int m22ydy = dy + (m12 + m22) / 2;
|
---|
5386 | uint trigx;
|
---|
5387 | uint trigy;
|
---|
5388 | uint maxws = sWidth<<12;
|
---|
5389 | uint maxhs = sHeight<<12;
|
---|
5390 |
|
---|
5391 | for (int y=0; y<dHeight; y++) { // for each target scanline
|
---|
5392 | trigx = m21ydx;
|
---|
5393 | trigy = m22ydy;
|
---|
5394 | uchar *maxp = dptr + dbpl;
|
---|
5395 | if (depth != 1) {
|
---|
5396 | switch (depth) {
|
---|
5397 | case 8: // 8 bpp transform
|
---|
5398 | while (dptr < maxp) {
|
---|
5399 | if (trigx < maxws && trigy < maxhs)
|
---|
5400 | *dptr = *(sptr+sbpl*(trigy>>12)+(trigx>>12));
|
---|
5401 | trigx += m11;
|
---|
5402 | trigy += m12;
|
---|
5403 | dptr++;
|
---|
5404 | }
|
---|
5405 | break;
|
---|
5406 |
|
---|
5407 | case 16: // 16 bpp transform
|
---|
5408 | while (dptr < maxp) {
|
---|
5409 | if (trigx < maxws && trigy < maxhs)
|
---|
5410 | *((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>12) +
|
---|
5411 | ((trigx>>12)<<1)));
|
---|
5412 | trigx += m11;
|
---|
5413 | trigy += m12;
|
---|
5414 | dptr++;
|
---|
5415 | dptr++;
|
---|
5416 | }
|
---|
5417 | break;
|
---|
5418 |
|
---|
5419 | case 24: // 24 bpp transform
|
---|
5420 | while (dptr < maxp) {
|
---|
5421 | if (trigx < maxws && trigy < maxhs) {
|
---|
5422 | const uchar *p2 = sptr+sbpl*(trigy>>12) + ((trigx>>12)*3);
|
---|
5423 | dptr[0] = p2[0];
|
---|
5424 | dptr[1] = p2[1];
|
---|
5425 | dptr[2] = p2[2];
|
---|
5426 | }
|
---|
5427 | trigx += m11;
|
---|
5428 | trigy += m12;
|
---|
5429 | dptr += 3;
|
---|
5430 | }
|
---|
5431 | break;
|
---|
5432 |
|
---|
5433 | case 32: // 32 bpp transform
|
---|
5434 | while (dptr < maxp) {
|
---|
5435 | if (trigx < maxws && trigy < maxhs)
|
---|
5436 | *((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>12) +
|
---|
5437 | ((trigx>>12)<<2)));
|
---|
5438 | trigx += m11;
|
---|
5439 | trigy += m12;
|
---|
5440 | dptr += 4;
|
---|
5441 | }
|
---|
5442 | break;
|
---|
5443 |
|
---|
5444 | default: {
|
---|
5445 | return false;
|
---|
5446 | }
|
---|
5447 | }
|
---|
5448 | } else {
|
---|
5449 | switch (type) {
|
---|
5450 | case QT_XFORM_TYPE_MSBFIRST:
|
---|
5451 | while (dptr < maxp) {
|
---|
5452 | IWX_MSB(128);
|
---|
5453 | IWX_MSB(64);
|
---|
5454 | IWX_MSB(32);
|
---|
5455 | IWX_MSB(16);
|
---|
5456 | IWX_MSB(8);
|
---|
5457 | IWX_MSB(4);
|
---|
5458 | IWX_MSB(2);
|
---|
5459 | IWX_MSB(1);
|
---|
5460 | dptr++;
|
---|
5461 | }
|
---|
5462 | break;
|
---|
5463 | case QT_XFORM_TYPE_LSBFIRST:
|
---|
5464 | while (dptr < maxp) {
|
---|
5465 | IWX_LSB(1);
|
---|
5466 | IWX_LSB(2);
|
---|
5467 | IWX_LSB(4);
|
---|
5468 | IWX_LSB(8);
|
---|
5469 | IWX_LSB(16);
|
---|
5470 | IWX_LSB(32);
|
---|
5471 | IWX_LSB(64);
|
---|
5472 | IWX_LSB(128);
|
---|
5473 | dptr++;
|
---|
5474 | }
|
---|
5475 | break;
|
---|
5476 | # if defined(Q_WS_WIN)
|
---|
5477 | case QT_XFORM_TYPE_WINDOWSPIXMAP:
|
---|
5478 | while (dptr < maxp) {
|
---|
5479 | IWX_PIX(128);
|
---|
5480 | IWX_PIX(64);
|
---|
5481 | IWX_PIX(32);
|
---|
5482 | IWX_PIX(16);
|
---|
5483 | IWX_PIX(8);
|
---|
5484 | IWX_PIX(4);
|
---|
5485 | IWX_PIX(2);
|
---|
5486 | IWX_PIX(1);
|
---|
5487 | dptr++;
|
---|
5488 | }
|
---|
5489 | break;
|
---|
5490 | # endif
|
---|
5491 | }
|
---|
5492 | }
|
---|
5493 | m21ydx += m21;
|
---|
5494 | m22ydy += m22;
|
---|
5495 | dptr += p_inc;
|
---|
5496 | }
|
---|
5497 | return true;
|
---|
5498 | }
|
---|
5499 | #undef IWX_MSB
|
---|
5500 | #undef IWX_LSB
|
---|
5501 | #undef IWX_PIX
|
---|
5502 |
|
---|
5503 | /*!
|
---|
5504 | \fn QImage QImage::xForm(const QMatrix &matrix) const
|
---|
5505 |
|
---|
5506 | Use transformed() instead.
|
---|
5507 |
|
---|
5508 | \oldcode
|
---|
5509 | QImage image;
|
---|
5510 | ...
|
---|
5511 | image.xForm(matrix);
|
---|
5512 | \newcode
|
---|
5513 | QImage image;
|
---|
5514 | ...
|
---|
5515 | image.transformed(matrix);
|
---|
5516 | \endcode
|
---|
5517 | */
|
---|
5518 |
|
---|
5519 | /*! \obsolete
|
---|
5520 | Returns a number that identifies the contents of this
|
---|
5521 | QImage object. Distinct QImage objects can only have the same
|
---|
5522 | serial number if they refer to the same contents (but they don't
|
---|
5523 | have to).
|
---|
5524 |
|
---|
5525 | Use cacheKey() instead.
|
---|
5526 |
|
---|
5527 | \warning The serial number doesn't necessarily change when the
|
---|
5528 | image is altered. This means that it may be dangerous to use
|
---|
5529 | it as a cache key.
|
---|
5530 |
|
---|
5531 | \sa operator==()
|
---|
5532 | */
|
---|
5533 |
|
---|
5534 | int QImage::serialNumber() const
|
---|
5535 | {
|
---|
5536 | if (!d)
|
---|
5537 | return 0;
|
---|
5538 | else
|
---|
5539 | return d->ser_no;
|
---|
5540 | }
|
---|
5541 |
|
---|
5542 | /*!
|
---|
5543 | Returns a number that identifies the contents of this QImage
|
---|
5544 | object. Distinct QImage objects can only have the same key if they
|
---|
5545 | refer to the same contents.
|
---|
5546 |
|
---|
5547 | The key will change when the image is altered.
|
---|
5548 | */
|
---|
5549 | qint64 QImage::cacheKey() const
|
---|
5550 | {
|
---|
5551 | if (!d)
|
---|
5552 | return 0;
|
---|
5553 | else
|
---|
5554 | return (((qint64) d->ser_no) << 32) | ((qint64) d->detach_no);
|
---|
5555 | }
|
---|
5556 |
|
---|
5557 | /*!
|
---|
5558 | \internal
|
---|
5559 |
|
---|
5560 | Returns true if the image is detached; otherwise returns false.
|
---|
5561 |
|
---|
5562 | \sa detach(), {Implicit Data Sharing}
|
---|
5563 | */
|
---|
5564 |
|
---|
5565 | bool QImage::isDetached() const
|
---|
5566 | {
|
---|
5567 | return d && d->ref == 1;
|
---|
5568 | }
|
---|
5569 |
|
---|
5570 |
|
---|
5571 | /*!
|
---|
5572 | \obsolete
|
---|
5573 | Sets the alpha channel of this image to the given \a alphaChannel.
|
---|
5574 |
|
---|
5575 | If \a alphaChannel is an 8 bit grayscale image, the intensity values are
|
---|
5576 | written into this buffer directly. Otherwise, \a alphaChannel is converted
|
---|
5577 | to 32 bit and the intensity of the RGB pixel values is used.
|
---|
5578 |
|
---|
5579 | Note that the image will be converted to the Format_ARGB32_Premultiplied
|
---|
5580 | format if the function succeeds.
|
---|
5581 |
|
---|
5582 | Use one of the composition mods in QPainter::CompositionMode instead.
|
---|
5583 |
|
---|
5584 | \sa alphaChannel(), {QImage#Image Transformations}{Image
|
---|
5585 | Transformations}, {QImage#Image Formats}{Image Formats}
|
---|
5586 | */
|
---|
5587 |
|
---|
5588 | void QImage::setAlphaChannel(const QImage &alphaChannel)
|
---|
5589 | {
|
---|
5590 | if (!d)
|
---|
5591 | return;
|
---|
5592 |
|
---|
5593 | int w = d->width;
|
---|
5594 | int h = d->height;
|
---|
5595 |
|
---|
5596 | if (w != alphaChannel.d->width || h != alphaChannel.d->height) {
|
---|
5597 | qWarning("QImage::setAlphaChannel: "
|
---|
5598 | "Alpha channel must have same dimensions as the target image");
|
---|
5599 | return;
|
---|
5600 | }
|
---|
5601 |
|
---|
5602 | if (d->paintEngine && d->paintEngine->isActive()) {
|
---|
5603 | qWarning("QImage::setAlphaChannel: "
|
---|
5604 | "Unable to set alpha channel while image is being painted on");
|
---|
5605 | return;
|
---|
5606 | }
|
---|
5607 |
|
---|
5608 | detach();
|
---|
5609 |
|
---|
5610 | *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
---|
5611 |
|
---|
5612 | // Slight optimization since alphachannels are returned as 8-bit grays.
|
---|
5613 | if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
|
---|
5614 | const uchar *src_data = alphaChannel.d->data;
|
---|
5615 | const uchar *dest_data = d->data;
|
---|
5616 | for (int y=0; y<h; ++y) {
|
---|
5617 | const uchar *src = src_data;
|
---|
5618 | QRgb *dest = (QRgb *)dest_data;
|
---|
5619 | for (int x=0; x<w; ++x) {
|
---|
5620 | int alpha = *src;
|
---|
5621 | int destAlpha = qt_div_255(alpha * qAlpha(*dest));
|
---|
5622 | *dest = ((destAlpha << 24)
|
---|
5623 | | (qt_div_255(qRed(*dest) * alpha) << 16)
|
---|
5624 | | (qt_div_255(qGreen(*dest) * alpha) << 8)
|
---|
5625 | | (qt_div_255(qBlue(*dest) * alpha)));
|
---|
5626 | ++dest;
|
---|
5627 | ++src;
|
---|
5628 | }
|
---|
5629 | src_data += alphaChannel.d->bytes_per_line;
|
---|
5630 | dest_data += d->bytes_per_line;
|
---|
5631 | }
|
---|
5632 |
|
---|
5633 | } else {
|
---|
5634 | const QImage sourceImage = alphaChannel.convertToFormat(QImage::Format_RGB32);
|
---|
5635 | const uchar *src_data = sourceImage.d->data;
|
---|
5636 | const uchar *dest_data = d->data;
|
---|
5637 | for (int y=0; y<h; ++y) {
|
---|
5638 | const QRgb *src = (const QRgb *) src_data;
|
---|
5639 | QRgb *dest = (QRgb *) dest_data;
|
---|
5640 | for (int x=0; x<w; ++x) {
|
---|
5641 | int alpha = qGray(*src);
|
---|
5642 | int destAlpha = qt_div_255(alpha * qAlpha(*dest));
|
---|
5643 | *dest = ((destAlpha << 24)
|
---|
5644 | | (qt_div_255(qRed(*dest) * alpha) << 16)
|
---|
5645 | | (qt_div_255(qGreen(*dest) * alpha) << 8)
|
---|
5646 | | (qt_div_255(qBlue(*dest) * alpha)));
|
---|
5647 | ++dest;
|
---|
5648 | ++src;
|
---|
5649 | }
|
---|
5650 | src_data += sourceImage.d->bytes_per_line;
|
---|
5651 | dest_data += d->bytes_per_line;
|
---|
5652 | }
|
---|
5653 | }
|
---|
5654 | }
|
---|
5655 |
|
---|
5656 |
|
---|
5657 | /*!
|
---|
5658 | Returns the alpha channel of the image as a new grayscale QImage in which
|
---|
5659 | each pixel's red, green, and blue values are given the alpha value of the
|
---|
5660 | original image. The color depth of the returned image is 8-bit.
|
---|
5661 |
|
---|
5662 | You can see an example of use of this function in QPixmap's
|
---|
5663 | \l{QPixmap::}{alphaChannel()}, which works in the same way as
|
---|
5664 | this function on QPixmaps.
|
---|
5665 |
|
---|
5666 | \sa setAlphaChannel(), hasAlphaChannel(),
|
---|
5667 | {QPixmap#Pixmap Information}{Pixmap},
|
---|
5668 | {QImage#Image Transformations}{Image Transformations}
|
---|
5669 | */
|
---|
5670 |
|
---|
5671 | QImage QImage::alphaChannel() const
|
---|
5672 | {
|
---|
5673 | if (!d)
|
---|
5674 | return QImage();
|
---|
5675 |
|
---|
5676 | int w = d->width;
|
---|
5677 | int h = d->height;
|
---|
5678 |
|
---|
5679 | QImage image(w, h, Format_Indexed8);
|
---|
5680 | image.setNumColors(256);
|
---|
5681 |
|
---|
5682 | // set up gray scale table.
|
---|
5683 | for (int i=0; i<256; ++i)
|
---|
5684 | image.setColor(i, qRgb(i, i, i));
|
---|
5685 |
|
---|
5686 | if (!hasAlphaChannel()) {
|
---|
5687 | image.fill(255);
|
---|
5688 | return image;
|
---|
5689 | }
|
---|
5690 |
|
---|
5691 | if (d->format == Format_Indexed8) {
|
---|
5692 | const uchar *src_data = d->data;
|
---|
5693 | uchar *dest_data = image.d->data;
|
---|
5694 | for (int y=0; y<h; ++y) {
|
---|
5695 | const uchar *src = src_data;
|
---|
5696 | uchar *dest = dest_data;
|
---|
5697 | for (int x=0; x<w; ++x) {
|
---|
5698 | *dest = qAlpha(d->colortable.at(*src));
|
---|
5699 | ++dest;
|
---|
5700 | ++src;
|
---|
5701 | }
|
---|
5702 | src_data += d->bytes_per_line;
|
---|
5703 | dest_data += image.d->bytes_per_line;
|
---|
5704 | }
|
---|
5705 | } else {
|
---|
5706 | QImage alpha32 = *this;
|
---|
5707 | if (d->format != Format_ARGB32 && d->format != Format_ARGB32_Premultiplied)
|
---|
5708 | alpha32 = convertToFormat(Format_ARGB32);
|
---|
5709 |
|
---|
5710 | const uchar *src_data = alpha32.d->data;
|
---|
5711 | uchar *dest_data = image.d->data;
|
---|
5712 | for (int y=0; y<h; ++y) {
|
---|
5713 | const QRgb *src = (const QRgb *) src_data;
|
---|
5714 | uchar *dest = dest_data;
|
---|
5715 | for (int x=0; x<w; ++x) {
|
---|
5716 | *dest = qAlpha(*src);
|
---|
5717 | ++dest;
|
---|
5718 | ++src;
|
---|
5719 | }
|
---|
5720 | src_data += alpha32.d->bytes_per_line;
|
---|
5721 | dest_data += image.d->bytes_per_line;
|
---|
5722 | }
|
---|
5723 | }
|
---|
5724 |
|
---|
5725 | return image;
|
---|
5726 | }
|
---|
5727 |
|
---|
5728 | /*!
|
---|
5729 | Returns true if the image has a format that respects the alpha
|
---|
5730 | channel, otherwise returns false.
|
---|
5731 |
|
---|
5732 | \sa alphaChannel(), {QImage#Image Information}{Image Information}
|
---|
5733 | */
|
---|
5734 | bool QImage::hasAlphaChannel() const
|
---|
5735 | {
|
---|
5736 | return d && (d->format == Format_ARGB32_Premultiplied
|
---|
5737 | || d->format == Format_ARGB32
|
---|
5738 | || d->format == Format_ARGB8565_Premultiplied
|
---|
5739 | || d->format == Format_ARGB8555_Premultiplied
|
---|
5740 | || d->format == Format_ARGB6666_Premultiplied
|
---|
5741 | || d->format == Format_ARGB4444_Premultiplied
|
---|
5742 | || (d->has_alpha_clut && (d->format == Format_Indexed8
|
---|
5743 | || d->format == Format_Mono
|
---|
5744 | || d->format == Format_MonoLSB)));
|
---|
5745 | }
|
---|
5746 |
|
---|
5747 |
|
---|
5748 | #ifdef QT3_SUPPORT
|
---|
5749 | #if defined(Q_WS_X11)
|
---|
5750 | QT_BEGIN_INCLUDE_NAMESPACE
|
---|
5751 | #include <private/qt_x11_p.h>
|
---|
5752 | QT_END_INCLUDE_NAMESPACE
|
---|
5753 | #endif
|
---|
5754 |
|
---|
5755 | QImage::Endian QImage::systemBitOrder()
|
---|
5756 | {
|
---|
5757 | #if defined(Q_WS_X11)
|
---|
5758 | return BitmapBitOrder(X11->display) == MSBFirst ? BigEndian : LittleEndian;
|
---|
5759 | #else
|
---|
5760 | return BigEndian;
|
---|
5761 | #endif
|
---|
5762 | }
|
---|
5763 | #endif
|
---|
5764 |
|
---|
5765 | /*!
|
---|
5766 | \fn QImage QImage::copy(const QRect &rect, Qt::ImageConversionFlags flags) const
|
---|
5767 | \compat
|
---|
5768 |
|
---|
5769 | Use copy() instead.
|
---|
5770 | */
|
---|
5771 |
|
---|
5772 | /*!
|
---|
5773 | \fn QImage QImage::copy(int x, int y, int w, int h, Qt::ImageConversionFlags flags) const
|
---|
5774 | \compat
|
---|
5775 |
|
---|
5776 | Use copy() instead.
|
---|
5777 | */
|
---|
5778 |
|
---|
5779 | /*!
|
---|
5780 | \fn QImage QImage::scaleWidth(int w) const
|
---|
5781 | \compat
|
---|
5782 |
|
---|
5783 | Use scaledToWidth() instead.
|
---|
5784 | */
|
---|
5785 |
|
---|
5786 | /*!
|
---|
5787 | \fn QImage QImage::scaleHeight(int h) const
|
---|
5788 | \compat
|
---|
5789 |
|
---|
5790 | Use scaledToHeight() instead.
|
---|
5791 | */
|
---|
5792 |
|
---|
5793 | static QImage smoothScaled(const QImage &source, int w, int h) {
|
---|
5794 | QImage src = source;
|
---|
5795 | if (src.format() == QImage::Format_ARGB32)
|
---|
5796 | src = src.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
---|
5797 | else if (src.depth() < 32) {
|
---|
5798 | if (src.hasAlphaChannel())
|
---|
5799 | src = src.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
---|
5800 | else
|
---|
5801 | src = src.convertToFormat(QImage::Format_RGB32);
|
---|
5802 | }
|
---|
5803 |
|
---|
5804 | return qSmoothScaleImage(src, w, h);
|
---|
5805 | }
|
---|
5806 |
|
---|
5807 |
|
---|
5808 | static QImage rotated90(const QImage &image) {
|
---|
5809 | QImage out(image.height(), image.width(), image.format());
|
---|
5810 | if (image.numColors() > 0)
|
---|
5811 | out.setColorTable(image.colorTable());
|
---|
5812 | int w = image.width();
|
---|
5813 | int h = image.height();
|
---|
5814 | switch (image.format()) {
|
---|
5815 | case QImage::Format_RGB32:
|
---|
5816 | case QImage::Format_ARGB32:
|
---|
5817 | case QImage::Format_ARGB32_Premultiplied:
|
---|
5818 | qt_memrotate270(reinterpret_cast<const quint32*>(image.bits()),
|
---|
5819 | w, h, image.bytesPerLine(),
|
---|
5820 | reinterpret_cast<quint32*>(out.bits()),
|
---|
5821 | out.bytesPerLine());
|
---|
5822 | break;
|
---|
5823 | case QImage::Format_RGB666:
|
---|
5824 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
5825 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
5826 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
5827 | case QImage::Format_RGB888:
|
---|
5828 | qt_memrotate270(reinterpret_cast<const quint24*>(image.bits()),
|
---|
5829 | w, h, image.bytesPerLine(),
|
---|
5830 | reinterpret_cast<quint24*>(out.bits()),
|
---|
5831 | out.bytesPerLine());
|
---|
5832 | break;
|
---|
5833 | case QImage::Format_RGB555:
|
---|
5834 | case QImage::Format_RGB16:
|
---|
5835 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
5836 | qt_memrotate270(reinterpret_cast<const quint16*>(image.bits()),
|
---|
5837 | w, h, image.bytesPerLine(),
|
---|
5838 | reinterpret_cast<quint16*>(out.bits()),
|
---|
5839 | out.bytesPerLine());
|
---|
5840 | break;
|
---|
5841 | case QImage::Format_Indexed8:
|
---|
5842 | qt_memrotate270(reinterpret_cast<const quint8*>(image.bits()),
|
---|
5843 | w, h, image.bytesPerLine(),
|
---|
5844 | reinterpret_cast<quint8*>(out.bits()),
|
---|
5845 | out.bytesPerLine());
|
---|
5846 | break;
|
---|
5847 | default:
|
---|
5848 | for (int y=0; y<h; ++y) {
|
---|
5849 | if (image.numColors())
|
---|
5850 | for (int x=0; x<w; ++x)
|
---|
5851 | out.setPixel(h-y-1, x, image.pixelIndex(x, y));
|
---|
5852 | else
|
---|
5853 | for (int x=0; x<w; ++x)
|
---|
5854 | out.setPixel(h-y-1, x, image.pixel(x, y));
|
---|
5855 | }
|
---|
5856 | break;
|
---|
5857 | }
|
---|
5858 | return out;
|
---|
5859 | }
|
---|
5860 |
|
---|
5861 |
|
---|
5862 | static QImage rotated180(const QImage &image) {
|
---|
5863 | return image.mirrored(true, true);
|
---|
5864 | }
|
---|
5865 |
|
---|
5866 |
|
---|
5867 | static QImage rotated270(const QImage &image) {
|
---|
5868 | QImage out(image.height(), image.width(), image.format());
|
---|
5869 | if (image.numColors() > 0)
|
---|
5870 | out.setColorTable(image.colorTable());
|
---|
5871 | int w = image.width();
|
---|
5872 | int h = image.height();
|
---|
5873 | switch (image.format()) {
|
---|
5874 | case QImage::Format_RGB32:
|
---|
5875 | case QImage::Format_ARGB32:
|
---|
5876 | case QImage::Format_ARGB32_Premultiplied:
|
---|
5877 | qt_memrotate90(reinterpret_cast<const quint32*>(image.bits()),
|
---|
5878 | w, h, image.bytesPerLine(),
|
---|
5879 | reinterpret_cast<quint32*>(out.bits()),
|
---|
5880 | out.bytesPerLine());
|
---|
5881 | break;
|
---|
5882 | case QImage::Format_RGB666:
|
---|
5883 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
5884 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
5885 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
5886 | case QImage::Format_RGB888:
|
---|
5887 | qt_memrotate90(reinterpret_cast<const quint24*>(image.bits()),
|
---|
5888 | w, h, image.bytesPerLine(),
|
---|
5889 | reinterpret_cast<quint24*>(out.bits()),
|
---|
5890 | out.bytesPerLine());
|
---|
5891 | break;
|
---|
5892 | case QImage::Format_RGB555:
|
---|
5893 | case QImage::Format_RGB16:
|
---|
5894 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
5895 | qt_memrotate90(reinterpret_cast<const quint16*>(image.bits()),
|
---|
5896 | w, h, image.bytesPerLine(),
|
---|
5897 | reinterpret_cast<quint16*>(out.bits()),
|
---|
5898 | out.bytesPerLine());
|
---|
5899 | break;
|
---|
5900 | case QImage::Format_Indexed8:
|
---|
5901 | qt_memrotate90(reinterpret_cast<const quint8*>(image.bits()),
|
---|
5902 | w, h, image.bytesPerLine(),
|
---|
5903 | reinterpret_cast<quint8*>(out.bits()),
|
---|
5904 | out.bytesPerLine());
|
---|
5905 | break;
|
---|
5906 | default:
|
---|
5907 | for (int y=0; y<h; ++y) {
|
---|
5908 | if (image.numColors())
|
---|
5909 | for (int x=0; x<w; ++x)
|
---|
5910 | out.setPixel(y, w-x-1, image.pixelIndex(x, y));
|
---|
5911 | else
|
---|
5912 | for (int x=0; x<w; ++x)
|
---|
5913 | out.setPixel(y, w-x-1, image.pixel(x, y));
|
---|
5914 | }
|
---|
5915 | break;
|
---|
5916 | }
|
---|
5917 | return out;
|
---|
5918 | }
|
---|
5919 |
|
---|
5920 | /*!
|
---|
5921 | Returns a copy of the image that is transformed using the given
|
---|
5922 | transformation \a matrix and transformation \a mode.
|
---|
5923 |
|
---|
5924 | The transformation \a matrix is internally adjusted to compensate
|
---|
5925 | for unwanted translation; i.e. the image produced is the smallest
|
---|
5926 | image that contains all the transformed points of the original
|
---|
5927 | image. Use the trueMatrix() function to retrieve the actual matrix
|
---|
5928 | used for transforming an image.
|
---|
5929 |
|
---|
5930 | Unlike the other overload, this function can be used to perform perspective
|
---|
5931 | transformations on images.
|
---|
5932 |
|
---|
5933 | \sa trueMatrix(), {QImage#Image Transformations}{Image
|
---|
5934 | Transformations}
|
---|
5935 | */
|
---|
5936 |
|
---|
5937 | QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode ) const
|
---|
5938 | {
|
---|
5939 | if (!d)
|
---|
5940 | return QImage();
|
---|
5941 |
|
---|
5942 | // source image data
|
---|
5943 | int ws = width();
|
---|
5944 | int hs = height();
|
---|
5945 |
|
---|
5946 | // target image data
|
---|
5947 | int wd;
|
---|
5948 | int hd;
|
---|
5949 |
|
---|
5950 | // compute size of target image
|
---|
5951 | QTransform mat = trueMatrix(matrix, ws, hs);
|
---|
5952 | bool complex_xform = false;
|
---|
5953 | bool scale_xform = false;
|
---|
5954 | if (mat.type() <= QTransform::TxScale) {
|
---|
5955 | if (mat.type() == QTransform::TxNone) // identity matrix
|
---|
5956 | return *this;
|
---|
5957 | else if (mat.m11() == -1. && mat.m22() == -1.)
|
---|
5958 | return rotated180(*this);
|
---|
5959 |
|
---|
5960 | if (mode == Qt::FastTransformation) {
|
---|
5961 | hd = qRound(qAbs(mat.m22()) * hs);
|
---|
5962 | wd = qRound(qAbs(mat.m11()) * ws);
|
---|
5963 | } else {
|
---|
5964 | hd = int(qAbs(mat.m22()) * hs + 0.9999);
|
---|
5965 | wd = int(qAbs(mat.m11()) * ws + 0.9999);
|
---|
5966 | }
|
---|
5967 | scale_xform = true;
|
---|
5968 | } else {
|
---|
5969 | if (mat.type() <= QTransform::TxRotate && mat.m11() == 0 && mat.m22() == 0) {
|
---|
5970 | if (mat.m12() == 1. && mat.m21() == -1.)
|
---|
5971 | return rotated90(*this);
|
---|
5972 | else if (mat.m12() == -1. && mat.m21() == 1.)
|
---|
5973 | return rotated270(*this);
|
---|
5974 | }
|
---|
5975 |
|
---|
5976 | QPolygonF a(QRectF(0, 0, ws, hs));
|
---|
5977 | a = mat.map(a);
|
---|
5978 | QRect r = a.boundingRect().toAlignedRect();
|
---|
5979 | wd = r.width();
|
---|
5980 | hd = r.height();
|
---|
5981 | complex_xform = true;
|
---|
5982 | }
|
---|
5983 |
|
---|
5984 | if (wd == 0 || hd == 0)
|
---|
5985 | return QImage();
|
---|
5986 |
|
---|
5987 | // Make use of the optimized algorithm when we're scaling
|
---|
5988 | if (scale_xform && mode == Qt::SmoothTransformation) {
|
---|
5989 | if (mat.m11() < 0.0F && mat.m22() < 0.0F) { // horizontal/vertical flip
|
---|
5990 | return smoothScaled(mirrored(true, true), wd, hd);
|
---|
5991 | } else if (mat.m11() < 0.0F) { // horizontal flip
|
---|
5992 | return smoothScaled(mirrored(true, false), wd, hd);
|
---|
5993 | } else if (mat.m22() < 0.0F) { // vertical flip
|
---|
5994 | return smoothScaled(mirrored(false, true), wd, hd);
|
---|
5995 | } else { // no flipping
|
---|
5996 | return smoothScaled(*this, wd, hd);
|
---|
5997 | }
|
---|
5998 | }
|
---|
5999 |
|
---|
6000 | int bpp = depth();
|
---|
6001 |
|
---|
6002 | int sbpl = bytesPerLine();
|
---|
6003 | const uchar *sptr = bits();
|
---|
6004 |
|
---|
6005 | QImage::Format target_format = d->format;
|
---|
6006 |
|
---|
6007 | if (complex_xform || mode == Qt::SmoothTransformation) {
|
---|
6008 | if (d->format < QImage::Format_RGB32 || !hasAlphaChannel()) {
|
---|
6009 | switch(d->format) {
|
---|
6010 | case QImage::Format_RGB16:
|
---|
6011 | target_format = Format_ARGB8565_Premultiplied;
|
---|
6012 | break;
|
---|
6013 | case QImage::Format_RGB555:
|
---|
6014 | target_format = Format_ARGB8555_Premultiplied;
|
---|
6015 | break;
|
---|
6016 | case QImage::Format_RGB666:
|
---|
6017 | target_format = Format_ARGB6666_Premultiplied;
|
---|
6018 | break;
|
---|
6019 | case QImage::Format_RGB444:
|
---|
6020 | target_format = Format_ARGB4444_Premultiplied;
|
---|
6021 | break;
|
---|
6022 | default:
|
---|
6023 | target_format = Format_ARGB32_Premultiplied;
|
---|
6024 | break;
|
---|
6025 | }
|
---|
6026 | }
|
---|
6027 | }
|
---|
6028 |
|
---|
6029 | QImage dImage(wd, hd, target_format);
|
---|
6030 | QIMAGE_SANITYCHECK_MEMORY(dImage);
|
---|
6031 |
|
---|
6032 | if (target_format == QImage::Format_MonoLSB
|
---|
6033 | || target_format == QImage::Format_Mono
|
---|
6034 | || target_format == QImage::Format_Indexed8) {
|
---|
6035 | dImage.d->colortable = d->colortable;
|
---|
6036 | dImage.d->has_alpha_clut = d->has_alpha_clut | complex_xform;
|
---|
6037 | }
|
---|
6038 |
|
---|
6039 | dImage.d->dpmx = dotsPerMeterX();
|
---|
6040 | dImage.d->dpmy = dotsPerMeterY();
|
---|
6041 |
|
---|
6042 | switch (bpp) {
|
---|
6043 | // initizialize the data
|
---|
6044 | case 8:
|
---|
6045 | if (dImage.d->colortable.size() < 256) {
|
---|
6046 | // colors are left in the color table, so pick that one as transparent
|
---|
6047 | dImage.d->colortable.append(0x0);
|
---|
6048 | memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.numBytes());
|
---|
6049 | } else {
|
---|
6050 | memset(dImage.bits(), 0, dImage.numBytes());
|
---|
6051 | }
|
---|
6052 | break;
|
---|
6053 | case 1:
|
---|
6054 | case 16:
|
---|
6055 | case 24:
|
---|
6056 | case 32:
|
---|
6057 | memset(dImage.bits(), 0x00, dImage.numBytes());
|
---|
6058 | break;
|
---|
6059 | }
|
---|
6060 |
|
---|
6061 | if (target_format >= QImage::Format_RGB32) {
|
---|
6062 | QPainter p(&dImage);
|
---|
6063 | if (mode == Qt::SmoothTransformation) {
|
---|
6064 | p.setRenderHint(QPainter::Antialiasing);
|
---|
6065 | p.setRenderHint(QPainter::SmoothPixmapTransform);
|
---|
6066 | }
|
---|
6067 | p.setTransform(mat);
|
---|
6068 | p.drawImage(QPoint(0, 0), *this);
|
---|
6069 | } else {
|
---|
6070 | bool invertible;
|
---|
6071 | mat = mat.inverted(&invertible); // invert matrix
|
---|
6072 | if (!invertible) // error, return null image
|
---|
6073 | return QImage();
|
---|
6074 |
|
---|
6075 | // create target image (some of the code is from QImage::copy())
|
---|
6076 | int type = format() == Format_Mono ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST;
|
---|
6077 | int dbpl = dImage.bytesPerLine();
|
---|
6078 | qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
|
---|
6079 | }
|
---|
6080 | return dImage;
|
---|
6081 | }
|
---|
6082 |
|
---|
6083 | /*!
|
---|
6084 | \fn QTransform QImage::trueMatrix(const QTransform &matrix, int width, int height)
|
---|
6085 |
|
---|
6086 | Returns the actual matrix used for transforming an image with the
|
---|
6087 | given \a width, \a height and \a matrix.
|
---|
6088 |
|
---|
6089 | When transforming an image using the transformed() function, the
|
---|
6090 | transformation matrix is internally adjusted to compensate for
|
---|
6091 | unwanted translation, i.e. transformed() returns the smallest
|
---|
6092 | image containing all transformed points of the original image.
|
---|
6093 | This function returns the modified matrix, which maps points
|
---|
6094 | correctly from the original image into the new image.
|
---|
6095 |
|
---|
6096 | Unlike the other overload, this function creates transformation
|
---|
6097 | matrices that can be used to perform perspective
|
---|
6098 | transformations on images.
|
---|
6099 |
|
---|
6100 | \sa transformed(), {QImage#Image Transformations}{Image
|
---|
6101 | Transformations}
|
---|
6102 | */
|
---|
6103 |
|
---|
6104 | QTransform QImage::trueMatrix(const QTransform &matrix, int w, int h)
|
---|
6105 | {
|
---|
6106 | const QRectF rect(0, 0, w, h);
|
---|
6107 | const QRect mapped = matrix.mapRect(rect).toAlignedRect();
|
---|
6108 | const QPoint delta = mapped.topLeft();
|
---|
6109 | return matrix * QTransform().translate(-delta.x(), -delta.y());
|
---|
6110 | }
|
---|
6111 |
|
---|
6112 |
|
---|
6113 | /*!
|
---|
6114 | \typedef QImage::DataPtr
|
---|
6115 | \internal
|
---|
6116 | */
|
---|
6117 |
|
---|
6118 | /*!
|
---|
6119 | \fn DataPtr & QImage::data_ptr()
|
---|
6120 | \internal
|
---|
6121 | */
|
---|
6122 |
|
---|
6123 | QT_END_NAMESPACE
|
---|