~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/image/qimage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the painting module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qimage.h"
 
30
#include "qdatastream.h"
 
31
#include "qbuffer.h"
 
32
#include "qmap.h"
 
33
#include "qmatrix.h"
 
34
#include "qimagereader.h"
 
35
#include "qimagewriter.h"
 
36
#include "qstringlist.h"
 
37
#include "qvariant.h"
 
38
#include <ctype.h>
 
39
#include <stdlib.h>
 
40
#include <limits.h>
 
41
#include <math.h>
 
42
#include <private/qdrawhelper_p.h>
 
43
#include <private/qpixmap_p.h>
 
44
 
 
45
#ifdef QT_RASTER_IMAGEENGINE
 
46
#include <private/qpaintengine_raster_p.h>
 
47
#else
 
48
#include <qpaintengine.h>
 
49
#endif
 
50
 
 
51
#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001)
 
52
#pragma message disable narrowptr
 
53
#endif
 
54
 
 
55
struct QImageData {        // internal image data
 
56
    QImageData();
 
57
    ~QImageData();
 
58
    static QImageData *create(const QSize &size, QImage::Format format, int numColors = 0);
 
59
 
 
60
    QAtomic ref;
 
61
 
 
62
    int width;
 
63
    int height;
 
64
    int depth;
 
65
    int nbytes;               // number of bytes data
 
66
    QVector<QRgb> colortable;
 
67
    uchar *data;
 
68
#ifdef QT3_SUPPORT
 
69
    uchar **jumptable;
 
70
#endif
 
71
    QImage::Format format;
 
72
    int bytes_per_line;
 
73
    int ser_no;               // serial number
 
74
 
 
75
    qreal  dpmx;                // dots per meter X (or 0)
 
76
    qreal  dpmy;                // dots per meter Y (or 0)
 
77
    QPoint  offset;           // offset in pixels
 
78
    uint own_data : 1;
 
79
    uint has_alpha_clut : 1;
 
80
 
 
81
#ifndef QT_NO_IMAGE_TEXT
 
82
    QMap<QImageTextKeyLang, QString> text_lang;
 
83
 
 
84
    QStringList languages() const
 
85
    {
 
86
        QStringList r;
 
87
        QMap<QImageTextKeyLang,QString>::const_iterator it = text_lang.begin();
 
88
        for (; it != text_lang.end(); ++it) {
 
89
            r.removeAll(it.key().lang);
 
90
            r.append(it.key().lang);
 
91
        }
 
92
        return r;
 
93
    }
 
94
    QStringList keys() const
 
95
    {
 
96
        QStringList r;
 
97
        QMap<QImageTextKeyLang,QString>::const_iterator it = text_lang.begin();
 
98
        for (; it != text_lang.end(); ++it) {
 
99
            r.removeAll(it.key().key);
 
100
            r.append(it.key().key);
 
101
        }
 
102
        return r;
 
103
    }
 
104
#endif
 
105
#ifndef QT_NO_IMAGEIO
 
106
    bool doImageIO(const QImage *image, QImageWriter* io, int quality) const;
 
107
#endif
 
108
 
 
109
    QPaintEngine *paintEngine;
 
110
};
 
111
 
 
112
extern int qt_defaultDpi();
 
113
 
 
114
QImageData::QImageData()
 
115
{
 
116
    static int serial = 0;
 
117
 
 
118
    ser_no = ++serial;
 
119
    ref = 0;
 
120
 
 
121
    width = height = depth = 0;
 
122
    nbytes = 0;
 
123
    data = 0;
 
124
    own_data = true;
 
125
#ifdef QT3_SUPPORT
 
126
    jumptable = 0;
 
127
#endif
 
128
    bytes_per_line = 0;
 
129
    format = QImage::Format_ARGB32;
 
130
 
 
131
    dpmx = qt_defaultDpi()*100./2.54;
 
132
    dpmy = qt_defaultDpi()*100./2.54;
 
133
    offset = QPoint(0,0);
 
134
 
 
135
    paintEngine = 0;
 
136
}
 
137
 
 
138
static int depthForFormat(QImage::Format format)
 
139
{
 
140
    int depth = 0;
 
141
    switch(format) {
 
142
    case QImage::Format_Invalid:
 
143
        Q_ASSERT(false);
 
144
    case QImage::Format_Mono:
 
145
    case QImage::Format_MonoLSB:
 
146
        depth = 1;
 
147
        break;
 
148
    case QImage::Format_Indexed8:
 
149
        depth = 8;
 
150
        break;
 
151
    case QImage::Format_RGB32:
 
152
    case QImage::Format_ARGB32:
 
153
    case QImage::Format_ARGB32_Premultiplied:
 
154
        depth = 32;
 
155
        break;
 
156
    }
 
157
    return depth;
 
158
}
 
159
 
 
160
QImageData * QImageData::create(const QSize &size, QImage::Format format, int numColors)
 
161
{
 
162
    int width = size.width();
 
163
    int height = size.height();
 
164
    if (width <= 0 || height <= 0 || numColors < 0 || format == QImage::Format_Invalid)
 
165
        return 0;                                // invalid parameter(s)
 
166
 
 
167
    int depth = 0;
 
168
    switch(format) {
 
169
    case QImage::Format_Invalid:
 
170
        Q_ASSERT(false);
 
171
    case QImage::Format_Mono:
 
172
    case QImage::Format_MonoLSB:
 
173
        depth = 1;
 
174
        numColors = 2;
 
175
        break;
 
176
    case QImage::Format_Indexed8:
 
177
        depth = 8;
 
178
        numColors = qMin(numColors, 256);
 
179
        numColors = qMax(0, numColors);
 
180
        break;
 
181
    case QImage::Format_RGB32:
 
182
    case QImage::Format_ARGB32:
 
183
    case QImage::Format_ARGB32_Premultiplied:
 
184
        depth = 32;
 
185
        numColors = 0;
 
186
        break;
 
187
    }
 
188
 
 
189
    QImageData *d = new QImageData;
 
190
    d->colortable.resize(numColors);
 
191
    if (depth == 1) {
 
192
        d->colortable[0] = QColor(Qt::black).rgba();
 
193
        d->colortable[1] = QColor(Qt::white).rgba();
 
194
    } else {
 
195
        for (int i = 0; i < numColors; ++i)
 
196
            d->colortable[i] = 0;
 
197
    }
 
198
 
 
199
    d->width = width;
 
200
    d->height = height;
 
201
    d->depth = depth;
 
202
    d->format = format;
 
203
    d->has_alpha_clut = false;
 
204
 
 
205
    d->bytes_per_line = ((width * d->depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 8)
 
206
 
 
207
    d->nbytes = d->bytes_per_line*height;
 
208
    d->data  = (uchar *)malloc(d->nbytes);
 
209
 
 
210
    if (!d->data) {
 
211
        delete d;
 
212
        return 0;
 
213
    }
 
214
 
 
215
    d->ref.ref();
 
216
    return d;
 
217
 
 
218
}
 
219
 
 
220
QImageData::~QImageData()
 
221
{
 
222
    delete paintEngine;
 
223
    if (data && own_data)
 
224
        free(data);
 
225
#ifdef QT3_SUPPORT
 
226
    if (jumptable)
 
227
        free(jumptable);
 
228
    jumptable = 0;
 
229
#endif
 
230
    data = 0;
 
231
}
 
232
 
 
233
/*!
 
234
    \class QImage
 
235
    \brief The QImage class provides a hardware-independent pixmap
 
236
    that allows direct access to the pixel data, and can be used as a
 
237
    paint device.
 
238
 
 
239
    \ingroup multimedia
 
240
    \ingroup shared
 
241
    \mainclass
 
242
 
 
243
    Qt provides two classes for handling image data: QImage and QPixmap.
 
244
    QImage is designed and optimized for I/O, and for direct pixel
 
245
    access/manipulation. QPixmap is designed and optimized for drawing.
 
246
    There are functions to convert between QImage and QPixmap:
 
247
    QPixmap::convertToImage() and QPixmap::convertFromImage().
 
248
 
 
249
    QImage supports a number of \link QImage::Format formats\endlink. These
 
250
    include monochrome images, 8-bit images, and 32-bit images with an optional
 
251
    alpha channel. Monochrome and 8-bit images are indexed based and use a
 
252
    color lookup table, while 32-bit images use RGB or ARGB values.
 
253
 
 
254
    An entry in the color table is an RGB triplet encoded as an \c qRgb
 
255
    value. Use the color() function to obtain an entry from the table,
 
256
    and the qRed(), qGreen(), and qBlue() functions (\c qcolor.h) to access
 
257
    the components. The qRgb() function is used to make an RGB triplet
 
258
    suitable for use with the setColor() function.
 
259
 
 
260
    Monochrome images have a color table with at most two
 
261
    colors. There are two different types of monochrome images: big endian (MSB first) or
 
262
    little endian (LSB first) bit order. To access a single bit you
 
263
    must do some bit shifts:
 
264
 
 
265
    \quotefromfile snippets/image/image.cpp
 
266
    \skipto BIT
 
267
    \skipto QImage
 
268
    \printuntil 7));
 
269
 
 
270
    If this looks complicated, you can convert the monochrome image to an 8-bit indexed
 
271
    image using convertToFormat().  8-bit images are much easier to work with than
 
272
    1-bit images because they have a single byte per pixel:
 
273
 
 
274
    \quotefromfile snippets/image/image.cpp
 
275
    \skipto 8-BIT
 
276
    \skipto QImage
 
277
    \printuntil scanLine
 
278
 
 
279
    32-bit images have no color table; instead, each pixel contains
 
280
    an ARGB value. 24 bits contain the RGB value; the most
 
281
    significant byte is reserved for the alpha buffer.
 
282
 
 
283
    \skipto 32-BIT
 
284
    \skipto QImage
 
285
    \printuntil qRgb
 
286
 
 
287
    On Qt/Embedded, scanlines are aligned to the pixel depth and may
 
288
    be padded to any degree, while on all other platforms, the
 
289
    scanlines are 32-bit aligned for all depths. The constructor
 
290
    taking a \c{uchar*} argument always expects 32-bit aligned data.
 
291
    On Qt/Embedded, an additional constructor allows the number of
 
292
    bytes-per-line to be specified.
 
293
 
 
294
    Pixel colors are retrieved with pixel() and set with setPixel().
 
295
 
 
296
    QImage supports a variety of functions that can be used to obtain
 
297
    information about the image. width(), height(), dotsPerMeterX(), and
 
298
    dotsPerMeterY() provide information about the image size and resolution.
 
299
    The depth(), numColors(), isGrayscale(), and colorTable() functions
 
300
    provide information about the color depth and available color components
 
301
    used to store the image data.
 
302
 
 
303
    It is possible to determine whether a color image can be safely
 
304
    converted to a grayscale image by using the allGray() function. The
 
305
    format(), bytesPerLine(), and numBytes() functions provide low-level
 
306
    information about the data stored in the image.
 
307
 
 
308
    QImage also supports a number of functions for creating a new
 
309
    image that is a transformed version of the original. For example,
 
310
    convertToFormat(), createAlphaMask(), createHeuristicMask(), mirrored(), scaled(), rgbSwapped()
 
311
    and transformed(). There are also functions for changing attributes of
 
312
    an image in-place, for example, setAlphaChannel(), setColor(),
 
313
    setDotsPerMeterX() and setDotsPerMeterY() and setNumColors().
 
314
 
 
315
    Images can be loaded and saved in the supported formats. Images
 
316
    are saved to a file with save(). Images are loaded from a file
 
317
    with load() (or in the constructor) or from an array of data with
 
318
    loadFromData(). The lists of supported formats are available from
 
319
    QImageReader::supportedImageFormats() and
 
320
    QImageWriter::supportedImageFormats().
 
321
 
 
322
    When loading an image, the file name can be either refer to an
 
323
    actual file on disk or to one of the application's embedded
 
324
    resources. See the \l{resources.html}{Resource System} overview
 
325
    for details on how to embed images and other resource files in
 
326
    the application's executable.
 
327
 
 
328
    Strings of text may be added to images using setText().
 
329
 
 
330
    The QImage class uses \l{shclass.html}{implicit sharing}, so you
 
331
    can pass QImage objects around by value.
 
332
 
 
333
    New image formats can be added as \link plugins-howto.html
 
334
    plugins\endlink.
 
335
 
 
336
    \sa QImageReader, QImageWriter, QPixmap QColor {shclass.html}{Shared Classes}
 
337
*/
 
338
 
 
339
/*!
 
340
    \enum QImage::Endian
 
341
 
 
342
    This enum type is used to describe the endianness of the CPU and
 
343
    graphics hardware. It is provided here for compatibility with earlier versions of Qt.
 
344
 
 
345
    \value IgnoreEndian  Endianness does not matter. Useful for some
 
346
                         operations that are independent of endianness.
 
347
    \value BigEndian     Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs.
 
348
    \value LittleEndian  Least significant bit first or little endian byte order, as on Intel x86.
 
349
*/
 
350
 
 
351
/*!
 
352
    \enum QImage::InvertMode
 
353
 
 
354
    This enum type is used to describe how pixel values should be
 
355
    inverted in the invertPixels() function.
 
356
 
 
357
    \value InvertRgb    Invert only the RGB values and leave the alpha
 
358
                        channel unchanged.
 
359
 
 
360
    \value InvertRgba   Invert all channels, including the alpha channel.
 
361
*/
 
362
 
 
363
/*!
 
364
    \enum QImage::Format
 
365
 
 
366
    \value Format_Invalid   The image is invalid.
 
367
    \value Format_Mono      The image is stored using 1-bit per pixel. Bytes are
 
368
                            packed with the most significant bit (MSB) first.
 
369
    \value Format_MonoLSB   The image is stored using 1-bit per pixel. Bytes are
 
370
                            packed with the less significant bit (LSB) first.
 
371
    \value Format_Indexed8  The image is stored using 8-bit indexes into a colormap.
 
372
    \value Format_RGB32     The image is stored using a 32-bit RGB format (0xffRRGGBB).
 
373
    \value Format_ARGB32    The image is stored using a 32-bit ARGB format (0xAARRGGBB).
 
374
    \value Format_ARGB32_Premultiplied  The image is stored using a premultiplied 32-bit
 
375
                            ARGB format (0xAARRGGBB), i.e. the red,
 
376
                            green, and blue channels are multiplied
 
377
                            by the alpha component divided by 255. (If RR, GG, or BB
 
378
                            has a higher value than the alpha channel, the results are undefined.)
 
379
 
 
380
    \sa format(), convertToFormat()
 
381
*/
 
382
 
 
383
/*****************************************************************************
 
384
  QImage member functions
 
385
 *****************************************************************************/
 
386
 
 
387
// table to flip bits
 
388
static const uchar bitflip[256] = {
 
389
    /*
 
390
        open OUT, "| fmt";
 
391
        for $i (0..255) {
 
392
            print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) |
 
393
                      (($i >> 3) & 0x04) | (($i >> 1) & 0x08) |
 
394
                      (($i << 7) & 0x80) | (($i << 5) & 0x40) |
 
395
                      (($i << 3) & 0x20) | (($i << 1) & 0x10), ", ";
 
396
        }
 
397
        close OUT;
 
398
    */
 
399
    0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
 
400
    8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
 
401
    4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
 
402
    12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
 
403
    2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
 
404
    10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
 
405
    6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
 
406
    14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
 
407
    1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
 
408
    9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
 
409
    5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
 
410
    13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
 
411
    3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
 
412
    11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
 
413
    7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
 
414
    15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
 
415
};
 
416
 
 
417
const uchar *qt_get_bitflip_array()                        // called from QPixmap code
 
418
{
 
419
    return bitflip;
 
420
}
 
421
 
 
422
#ifdef QT3_SUPPORT
 
423
static QImage::Format formatFor(int depth, QImage::Endian bitOrder)
 
424
{
 
425
    QImage::Format format;
 
426
    if (depth == 1) {
 
427
        format = bitOrder == QImage::BigEndian ? QImage::Format_Mono : QImage::Format_MonoLSB;
 
428
    } else if (depth == 8) {
 
429
        format = QImage::Format_Indexed8;
 
430
    } else if (depth == 32) {
 
431
        format = QImage::Format_RGB32;
 
432
    } else {
 
433
        qWarning("QImage: depth %d not supported", depth);
 
434
        format = QImage::Format_Invalid;
 
435
    }
 
436
    return format;
 
437
}
 
438
#endif
 
439
 
 
440
/*!
 
441
    Constructs a null image.
 
442
 
 
443
    \sa isNull()
 
444
*/
 
445
 
 
446
QImage::QImage()
 
447
    : QPaintDevice()
 
448
{
 
449
    d = 0;
 
450
}
 
451
 
 
452
/*!
 
453
    Constructs an image with \a width, \a height in format \a format.
 
454
*/
 
455
QImage::QImage(int width, int height, Format format)
 
456
    : QPaintDevice()
 
457
{
 
458
    d = QImageData::create(QSize(width, height), format, 0);
 
459
}
 
460
 
 
461
/*!
 
462
    Constructs an image with \a size in format \a format.
 
463
*/
 
464
QImage::QImage(const QSize &size, Format format)
 
465
    : QPaintDevice()
 
466
{
 
467
    d = QImageData::create(size, format, 0);
 
468
}
 
469
 
 
470
/*!
 
471
    Constructs an image \a width pixels wide, \a height pixels high with a
 
472
    format of \a format, that uses an existing memory buffer, \a
 
473
    data. The buffer must remain valid throughout the life of the
 
474
    QImage. The image does not delete the buffer at destruction.
 
475
 
 
476
    Note that \a data must be 32-bit aligned.
 
477
 
 
478
    If the image is in an indexed color format, set the color table
 
479
    for the image using setColorTable().
 
480
*/
 
481
QImage::QImage(uchar* data, int width, int height, Format format)
 
482
    : QPaintDevice()
 
483
{
 
484
    d = 0;
 
485
    if (format == Format_Invalid || width <= 0 || height <= 0 || !data)
 
486
        return;                                        // invalid parameter(s)
 
487
    d = new QImageData;
 
488
    d->ref.ref();
 
489
 
 
490
    d->own_data = false;
 
491
    d->data = data;
 
492
    d->width = width;
 
493
    d->height = height;
 
494
    d->depth = depthForFormat(format);
 
495
    d->format = format;
 
496
 
 
497
    d->bytes_per_line = ((width * d->depth + 31)/32) * 4;
 
498
    d->nbytes = d->bytes_per_line * height;
 
499
}
 
500
 
 
501
#ifndef QT_NO_IMAGEIO
 
502
/*!
 
503
    Constructs an image and tries to load the image from the file \a
 
504
    fileName.
 
505
 
 
506
    If \a format is specified, the loader attempts to read the image
 
507
    using the specified format. If \a format is not specified (which
 
508
    is the default), the loader probes the file for a header to
 
509
    guess the file format.
 
510
 
 
511
    If the loading of the image failed, this object is a \link
 
512
    isNull() null\endlink image.
 
513
 
 
514
    The QImageReader documentation lists the supported image formats and
 
515
    explains how to add extra formats.
 
516
 
 
517
    The file name can be either refer to an actual file on disk or to
 
518
    one of the application's embedded resources. See the
 
519
    \l{resources.html}{Resource System} overview for details on how
 
520
    to embed images and other resource files in the application's
 
521
    executable.
 
522
 
 
523
    \sa load(), isNull(), QImageReader
 
524
*/
 
525
 
 
526
QImage::QImage(const QString &fileName, const char *format)
 
527
    : QPaintDevice()
 
528
{
 
529
    d = 0;
 
530
    load(fileName, format);
 
531
}
 
532
 
 
533
/*!
 
534
    \overload
 
535
    Constructs an image and tries to load the image from the file \a
 
536
    fileName.
 
537
 
 
538
    If \a format is specified, the loader attempts to read the image
 
539
    using the specified format. If \a format is not specified (which
 
540
    is the default), the loader probes the files for a header to
 
541
    guess the file format.
 
542
 
 
543
    If the loading of the image failed, this object is a \link
 
544
    isNull() null\endlink image.
 
545
 
 
546
    The QImageReader documentation lists the supported image formats and
 
547
    explains how to add extra formats.
 
548
 
 
549
    The file name can be either refer to an actual file on disk or to
 
550
    one of the application's embedded resources. See the
 
551
    \l{resources.html}{Resource System} overview for details on how
 
552
    to embed images and other resource files in the application's
 
553
    executable.
 
554
 
 
555
    You can disable this constructor by defining \c
 
556
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
557
    can be useful if you want to ensure that all user-visible strings
 
558
    go through QObject::tr(), for example.
 
559
 
 
560
    \sa QString::fromAscii(), load(), isNull(), QImageReader
 
561
*/
 
562
QImage::QImage(const char *fileName, const char *format)
 
563
    : QPaintDevice()
 
564
{
 
565
    // ### Qt 5: if you remove the QImage(const QByteArray &) QT3_SUPPORT
 
566
    // constructor, remove this constructor as well. The constructor here
 
567
    // exists so that QImage("foo.png") compiles without ambiguity.
 
568
    d = 0;
 
569
    load(QString::fromAscii(fileName), format);
 
570
}
 
571
 
 
572
extern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image);
 
573
 
 
574
/*!
 
575
    Constructs an image from \a xpm, which must be a valid XPM image.
 
576
 
 
577
    Errors are silently ignored.
 
578
 
 
579
    Note that it's possible to squeeze the XPM variable a little bit
 
580
    by using an unusual declaration:
 
581
 
 
582
    \code
 
583
        static const char * const start_xpm[] = {
 
584
            "16 15 8 1",
 
585
            "a c #cec6bd",
 
586
        ....
 
587
    \endcode
 
588
 
 
589
    The extra \c const makes the entire definition read-only, which is
 
590
    slightly more efficient (e.g. when the code is in a shared
 
591
    library) and ROMable when the application is to be stored in ROM.
 
592
*/
 
593
 
 
594
QImage::QImage(const char * const xpm[])
 
595
    : QPaintDevice()
 
596
{
 
597
    d = 0;
 
598
    if (!qt_read_xpm_image_or_array(0, xpm, *this)) {
 
599
        // We use a qFatal rather than disabling the whole function,
 
600
        // as this constructor may be ambiguous.
 
601
        qFatal("QImage::QImage(), XPM is not supported");
 
602
    }
 
603
}
 
604
 
 
605
/*!
 
606
    \fn QImage::QImage(const QByteArray &data)
 
607
 
 
608
    Use QImage::fromData(\a data) instead.
 
609
 
 
610
    \oldcode
 
611
        QByteArray data;
 
612
        ...
 
613
        QImage image(data);
 
614
    \newcode
 
615
        QByteArray data;
 
616
        ...
 
617
        QImage image = QImage::fromData(data);
 
618
    \endcode
 
619
*/
 
620
#endif //QT_NO_IMAGEIO
 
621
 
 
622
 
 
623
/*!
 
624
    Constructs a \link shclass.html shallow copy\endlink of \a image.
 
625
*/
 
626
 
 
627
QImage::QImage(const QImage &image)
 
628
    : QPaintDevice()
 
629
{
 
630
    d = image.d;
 
631
    if (d)
 
632
        d->ref.ref();
 
633
}
 
634
 
 
635
#ifdef QT3_SUPPORT
 
636
/*!
 
637
    Constructs an image with \a w width, \a h height, \a depth bits
 
638
    per pixel, \a numColors colors and bit order \a bitOrder.
 
639
 
 
640
    Using this constructor is the same as first constructing a null
 
641
    image and then calling the create() function.
 
642
 
 
643
    \sa create()
 
644
*/
 
645
 
 
646
QImage::QImage(int w, int h, int depth, int numColors, Endian bitOrder)
 
647
    : QPaintDevice()
 
648
{
 
649
    d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), numColors);
 
650
}
 
651
 
 
652
/*!
 
653
    Constructs an image with size \a size pixels, depth \a depth bits,
 
654
    \a numColors and \a bitOrder endianness.
 
655
 
 
656
    Using this constructor is the same as first constructing a null
 
657
    image and then calling the create() function.
 
658
 
 
659
    \sa create()
 
660
*/
 
661
QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
 
662
    : QPaintDevice()
 
663
{
 
664
    d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
 
665
}
 
666
 
 
667
/*!
 
668
    Constructs an image \a w pixels wide, \a h pixels high with a
 
669
    color depth of \a depth, that uses an existing memory buffer, \a
 
670
    data. The buffer must remain valid throughout the life of the
 
671
    QImage. The image does not delete the buffer at destruction.
 
672
 
 
673
    If \a colortable is 0, a color table sufficient for \a numColors
 
674
    will be allocated (and destructed later).
 
675
 
 
676
    Note that \a data must be 32-bit aligned.
 
677
 
 
678
    The endianness is given in \a bitOrder.
 
679
*/
 
680
QImage::QImage(uchar* data, int w, int h, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
 
681
    : QPaintDevice()
 
682
{
 
683
    d = 0;
 
684
    Format f = formatFor(depth, bitOrder);
 
685
    if (f == Format_Invalid)
 
686
        return;
 
687
    if (w <= 0 || h <= 0 || numColors < 0 || !data)
 
688
        return;                                        // invalid parameter(s)
 
689
    d = new QImageData;
 
690
    d->ref.ref();
 
691
 
 
692
    d->own_data = false;
 
693
    d->data = data;
 
694
    d->width = w;
 
695
    d->height = h;
 
696
    d->depth = depth;
 
697
    d->format = f;
 
698
    if (depth == 32)
 
699
        numColors = 0;
 
700
 
 
701
    d->bytes_per_line = ((w*depth+31)/32)*4;        // bytes per scanline
 
702
    d->nbytes = d->bytes_per_line * h;
 
703
    if (colortable) {
 
704
        d->colortable.resize(numColors);
 
705
        for (int i = 0; i < numColors; ++i)
 
706
            d->colortable[i] = colortable[i];
 
707
    } else if (numColors) {
 
708
        setNumColors(numColors);
 
709
    }
 
710
}
 
711
 
 
712
#ifdef Q_WS_QWS
 
713
 
 
714
/*!
 
715
    Constructs an image that uses an existing memory buffer. The
 
716
    buffer must remain valid for the life of the QImage. The image
 
717
    does not delete the buffer at destruction. The buffer is passed as
 
718
    \a data. The image's width is \a w and its height is \a h. The
 
719
    color depth is \a depth. \a bpl specifies the number of bytes per
 
720
    line.
 
721
 
 
722
    If \a colortable is 0, a color table sufficient for \a numColors
 
723
    will be allocated (and destructed later).
 
724
 
 
725
    The endianness is specified by \a bitOrder.
 
726
 
 
727
    \warning This constructor is only available on Qt/Embedded.
 
728
*/
 
729
QImage::QImage(uchar* data, int w, int h, int depth, int bpl, const QRgb* colortable, int numColors, Endian bitOrder)
 
730
    : QPaintDevice()
 
731
{
 
732
    d = 0;
 
733
    Format f = formatFor(depth, bitOrder);
 
734
    if (f == Format_Invalid)
 
735
        return;
 
736
    if (!data || w <= 0 || h <= 0 || depth <= 0 || numColors < 0)
 
737
        return;                                        // invalid parameter(s)
 
738
 
 
739
    d = new QImageData;
 
740
    d->ref.ref();
 
741
    d->own_data = false;
 
742
    d->data = data;
 
743
    d->width = w;
 
744
    d->height = h;
 
745
    d->depth = depth;
 
746
    d->format = f;
 
747
    if (depth == 32)
 
748
        numColors = 0;
 
749
    d->bytes_per_line = bpl;
 
750
    d->nbytes = d->bytes_per_line * h;
 
751
    if (colortable) {
 
752
        d->colortable.reserve(numColors);
 
753
        for (int i = 0; i < numColors; ++i)
 
754
            d->colortable[i] = colortable[i];
 
755
    } else if (numColors) {
 
756
        setNumColors(numColors);
 
757
    }
 
758
}
 
759
#endif // Q_WS_QWS
 
760
#endif
 
761
 
 
762
/*!
 
763
    Destroys the image and cleans up.
 
764
*/
 
765
 
 
766
QImage::~QImage()
 
767
{
 
768
    if (d && !d->ref.deref())
 
769
        delete d;
 
770
}
 
771
 
 
772
/*!
 
773
    Assigns a \link shclass.html shallow copy\endlink of \a image to
 
774
    this image and returns a reference to this image.
 
775
 
 
776
    \sa copy()
 
777
*/
 
778
 
 
779
QImage &QImage::operator=(const QImage &image)
 
780
{
 
781
    QImageData *x = image.d;
 
782
    if (x)
 
783
        x->ref.ref();
 
784
    x = qAtomicSetPtr(&d, x);
 
785
    if (x && !x->ref.deref())
 
786
        delete x;
 
787
    return *this;
 
788
}
 
789
 
 
790
/*!
 
791
  \internal
 
792
*/
 
793
int QImage::devType() const
 
794
{
 
795
    return QInternal::Image;
 
796
}
 
797
 
 
798
/*!
 
799
   Returns the image as a QVariant
 
800
*/
 
801
QImage::operator QVariant() const
 
802
{
 
803
    return QVariant(QVariant::Image, this);
 
804
}
 
805
 
 
806
/*!
 
807
    Detaches from shared image d and makes sure that this image is
 
808
    the only one referring to the data.
 
809
 
 
810
    If multiple images share common data, this image makes a copy of
 
811
    the data and detaches itself from the sharing mechanism.
 
812
    Nothing is done if there is just a single reference.
 
813
 
 
814
    \sa copy()
 
815
*/
 
816
 
 
817
void QImage::detach()
 
818
{
 
819
    if (d && d->ref != 1)
 
820
        *this = copy();
 
821
}
 
822
 
 
823
 
 
824
/*!
 
825
    \fn inline QImage QImage::copy(int x, int y, int w, int h) const
 
826
    \overload
 
827
 
 
828
    Returns a sub-area of the image.
 
829
 
 
830
    The returned image is always \a w by \a h pixels in size, and is
 
831
    copied from position \a x, \a y in this image. In areas beyond
 
832
    this image pixels are filled with pixel 0.
 
833
*/
 
834
 
 
835
/*!
 
836
    \overload
 
837
 
 
838
    Returns a sub-area of the image.
 
839
 
 
840
    The returned image always has the size of the rectangle \a r. In
 
841
    areas beyond this image pixels are filled with pixel 0.
 
842
 
 
843
    If \a r is a ull rectangle the entire image is copied.
 
844
*/
 
845
QImage QImage::copy(const QRect& r) const
 
846
{
 
847
    if (!d)
 
848
        return QImage();
 
849
 
 
850
    if (r.isNull()) {
 
851
        QImage image(d->width, d->height, d->format);
 
852
 
 
853
#ifdef Q_WS_QWS
 
854
        // Qt/Embedded can create images with non-default bpl
 
855
        // make sure we don't crash.
 
856
        if (image.d->nbytes != d->nbytes) {
 
857
            int bpl = image.bytesPerLine();
 
858
            for (int i = 0; i < height(); i++)
 
859
                memcpy(image.scanLine(i), scanLine(i), bpl);
 
860
        } else
 
861
#endif
 
862
            memcpy(image.bits(), bits(), d->nbytes);
 
863
        image.d->colortable = d->colortable;
 
864
        image.d->dpmx = d->dpmx;
 
865
        image.d->dpmy = d->dpmy;
 
866
        image.d->offset = d->offset;
 
867
        image.d->has_alpha_clut = d->has_alpha_clut;
 
868
#ifndef QT_NO_IMAGE_TEXT
 
869
        image.d->text_lang = d->text_lang;
 
870
#endif
 
871
        return image;
 
872
    }
 
873
 
 
874
    int x = r.x();
 
875
    int y = r.y();
 
876
    int w = r.width();
 
877
    int h = r.height();
 
878
 
 
879
    int dx = 0;
 
880
    int dy = 0;
 
881
    if (w <= 0 || h <= 0)
 
882
        return QImage();
 
883
 
 
884
    QImage image(w, h, d->format);
 
885
 
 
886
    if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) {
 
887
        // bitBlt will not cover entire image - clear it.
 
888
        image.fill(0);
 
889
        if (x < 0) {
 
890
            dx = -x;
 
891
            x = 0;
 
892
        }
 
893
        if (y < 0) {
 
894
            dy = -y;
 
895
            y = 0;
 
896
        }
 
897
    }
 
898
 
 
899
    image.d->colortable = d->colortable;
 
900
 
 
901
    int pixels_to_copy = w - dx;
 
902
    if (pixels_to_copy > x + d->width)
 
903
        pixels_to_copy = d->width - x;
 
904
    int lines_to_copy = h - dy;
 
905
    if (lines_to_copy > y + d->height)
 
906
        lines_to_copy = d->height - y;
 
907
 
 
908
    bool byteAligned = true;
 
909
    if (d->format == Format_Mono || d->format == Format_MonoLSB)
 
910
        byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7);
 
911
 
 
912
    if (byteAligned) {
 
913
        const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line;
 
914
        uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line;
 
915
        const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3;
 
916
        for (int i = 0; i < lines_to_copy; ++i) {
 
917
            memcpy(dest, src, bytes_to_copy);
 
918
            src += d->bytes_per_line;
 
919
            dest += image.d->bytes_per_line;
 
920
        }
 
921
    } else if (d->format == Format_Mono) {
 
922
        const uchar *src = d->data + y * d->bytes_per_line;
 
923
        uchar *dest = image.d->data + dy * image.d->bytes_per_line;
 
924
        for (int i = 0; i < lines_to_copy; ++i) {
 
925
            for (int j = 0; j < pixels_to_copy; ++j) {
 
926
                if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7)))
 
927
                    dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7));
 
928
                else
 
929
                    dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7));
 
930
            }
 
931
            src += d->bytes_per_line;
 
932
            dest += image.d->bytes_per_line;
 
933
        }
 
934
    } else { // Format_MonoLSB
 
935
        Q_ASSERT(d->format == Format_MonoLSB);
 
936
        const uchar *src = d->data + y * d->bytes_per_line;
 
937
        uchar *dest = image.d->data + dy * image.d->bytes_per_line;
 
938
        for (int i = 0; i < lines_to_copy; ++i) {
 
939
            for (int j = 0; j < pixels_to_copy; ++j) {
 
940
                if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7)))
 
941
                    dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7));
 
942
                else
 
943
                    dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7));
 
944
            }
 
945
            src += d->bytes_per_line;
 
946
            dest += image.d->bytes_per_line;
 
947
        }
 
948
    }
 
949
 
 
950
    image.d->dpmx = dotsPerMeterX();
 
951
    image.d->dpmy = dotsPerMeterY();
 
952
    image.d->offset = offset();
 
953
    image.d->has_alpha_clut = d->has_alpha_clut;
 
954
#ifndef QT_NO_IMAGE_TEXT
 
955
    image.d->text_lang = d->text_lang;
 
956
#endif
 
957
    return image;
 
958
}
 
959
 
 
960
 
 
961
/*!
 
962
    \fn bool QImage::isNull() const
 
963
 
 
964
    Returns true if it is a null image; otherwise returns false.
 
965
 
 
966
    A null image has all parameters set to zero and no allocated data.
 
967
*/
 
968
bool QImage::isNull() const
 
969
{
 
970
    return !d;
 
971
}
 
972
 
 
973
/*!
 
974
    \fn int QImage::width() const
 
975
 
 
976
    Returns the width of the image.
 
977
 
 
978
    \sa height() size() rect()
 
979
*/
 
980
int QImage::width() const
 
981
{
 
982
    return d ? d->width : 0;
 
983
}
 
984
 
 
985
/*!
 
986
    \fn int QImage::height() const
 
987
 
 
988
    Returns the height of the image.
 
989
 
 
990
    \sa width() size() rect()
 
991
*/
 
992
int QImage::height() const
 
993
{
 
994
    return d ? d->height : 0;
 
995
}
 
996
 
 
997
/*!
 
998
    \fn QSize QImage::size() const
 
999
 
 
1000
    Returns the size of the image, i.e. its width and height.
 
1001
 
 
1002
    \sa width() height() rect()
 
1003
*/
 
1004
QSize QImage::size() const
 
1005
{
 
1006
    return d ? QSize(d->width, d->height) : QSize();
 
1007
}
 
1008
 
 
1009
/*!
 
1010
    \fn QRect QImage::rect() const
 
1011
 
 
1012
    Returns the enclosing rectangle (0, 0, width(), height()) of the
 
1013
    image.
 
1014
 
 
1015
    \sa width() height() size()
 
1016
*/
 
1017
QRect QImage::rect() const
 
1018
{
 
1019
    return d ? QRect(0, 0, d->width, d->height) : QRect();
 
1020
}
 
1021
 
 
1022
/*!
 
1023
    \fn int QImage::depth() const
 
1024
 
 
1025
    Returns the depth of the image.
 
1026
 
 
1027
    The image depth is the number of bits used to encode a single
 
1028
    pixel, also called bits per pixel (bpp) or bit planes of an image.
 
1029
 
 
1030
    The supported depths are 1, 8 and 32.
 
1031
 
 
1032
    \sa convertDepth()
 
1033
*/
 
1034
int QImage::depth() const
 
1035
{
 
1036
    return d ? d->depth : 0;
 
1037
}
 
1038
 
 
1039
/*!
 
1040
    \fn int QImage::numColors() const
 
1041
 
 
1042
    Returns the size of the color table for the image.
 
1043
 
 
1044
    Notice that numColors() returns 0 for 32-bpp images because these
 
1045
    images do not use color tables, but instead encode pixel values as
 
1046
    RGB triplets.
 
1047
 
 
1048
    \sa setNumColors() colorTable()
 
1049
*/
 
1050
int QImage::numColors() const
 
1051
{
 
1052
    return d ? d->colortable.size() : 0;
 
1053
}
 
1054
 
 
1055
 
 
1056
#ifdef QT3_SUPPORT
 
1057
/*!
 
1058
    \fn QImage::Endian QImage::bitOrder() const
 
1059
 
 
1060
    Returns the bit order for the image.
 
1061
 
 
1062
    If it is a 1-bpp image, this function returns either
 
1063
    QImage::BigEndian or QImage::LittleEndian.
 
1064
 
 
1065
    If it is not a 1-bpp image, this function returns
 
1066
    QImage::IgnoreEndian.
 
1067
 
 
1068
    \sa depth()
 
1069
*/
 
1070
 
 
1071
/*!
 
1072
    Returns a pointer to the scanline pointer table.
 
1073
 
 
1074
    This is the beginning of the data block for the image.
 
1075
 
 
1076
    \sa bits() scanLine()
 
1077
*/
 
1078
uchar **QImage::jumpTable()
 
1079
{
 
1080
    if (!d)
 
1081
        return 0;
 
1082
    detach();
 
1083
 
 
1084
    if (!d->jumptable) {
 
1085
        d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
 
1086
        uchar *data = d->data;
 
1087
        int height = d->height;
 
1088
        uchar **p = d->jumptable;
 
1089
        while (height--) {
 
1090
            *p++ = data;
 
1091
            data += d->bytes_per_line;
 
1092
        }
 
1093
    }
 
1094
    return d->jumptable;
 
1095
}
 
1096
 
 
1097
/*!
 
1098
    \overload
 
1099
*/
 
1100
const uchar * const *QImage::jumpTable() const
 
1101
{
 
1102
    if (!d)
 
1103
        return 0;
 
1104
    if (!d->jumptable) {
 
1105
        d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
 
1106
        uchar *data = d->data;
 
1107
        int height = d->height;
 
1108
        uchar **p = d->jumptable;
 
1109
        while (height--) {
 
1110
            *p++ = data;
 
1111
            data += d->bytes_per_line;
 
1112
        }
 
1113
    }
 
1114
    return d->jumptable;
 
1115
}
 
1116
#endif
 
1117
 
 
1118
/*!
 
1119
    Sets the color table used to translate color indexes to RGB values
 
1120
    to the specified \a colors.
 
1121
*/
 
1122
void QImage::setColorTable(const QVector<QRgb> colors)
 
1123
{
 
1124
    if (!d)
 
1125
        return;
 
1126
    detach();
 
1127
    d->colortable = colors;
 
1128
    for (int i = 0; i < d->colortable.size(); ++i)
 
1129
        d->has_alpha_clut |= (qAlpha(d->colortable.at(i)) != 255);
 
1130
}
 
1131
 
 
1132
/*!
 
1133
    Returns the color table of the image, or an empty list of
 
1134
    the image does not have a color table
 
1135
 
 
1136
    \sa numColors()
 
1137
*/
 
1138
QVector<QRgb> QImage::colorTable() const
 
1139
{
 
1140
    return d ? d->colortable : QVector<QRgb>();
 
1141
}
 
1142
 
 
1143
 
 
1144
/*!
 
1145
    Returns the number of bytes occupied by the image data.
 
1146
 
 
1147
    \sa bytesPerLine() bits()
 
1148
*/
 
1149
int QImage::numBytes() const
 
1150
{
 
1151
    return d ? d->nbytes : 0;
 
1152
}
 
1153
 
 
1154
#ifdef Q_WS_QWS
 
1155
/*!
 
1156
  \reimp
 
1157
*/
 
1158
const uchar * QImage::qwsScanLine(int i) const
 
1159
{
 
1160
    return scanLine(i);
 
1161
}
 
1162
 
 
1163
/*!
 
1164
  \reimp
 
1165
*/
 
1166
int QImage::qwsBytesPerLine() const
 
1167
{
 
1168
    return bytesPerLine();
 
1169
}
 
1170
#endif
 
1171
 
 
1172
/*!
 
1173
    Returns the number of bytes per image scanline. This is equivalent
 
1174
    to numBytes()/height().
 
1175
 
 
1176
    \sa numBytes() scanLine()
 
1177
*/
 
1178
int QImage::bytesPerLine() const
 
1179
{
 
1180
    return (d && d->height) ? d->nbytes / d->height : 0;
 
1181
}
 
1182
 
 
1183
 
 
1184
/*!
 
1185
    Returns the color in the color table at index \a i. The first
 
1186
    color is at index 0.
 
1187
 
 
1188
    A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue()
 
1189
    functions to get the color value components.
 
1190
 
 
1191
    \sa setColor() numColors() QColor
 
1192
*/
 
1193
QRgb QImage::color(int i) const
 
1194
{
 
1195
    Q_ASSERT(i < numColors());
 
1196
    return d ? d->colortable.at(i) : QRgb(uint(-1));
 
1197
}
 
1198
 
 
1199
/*!
 
1200
    Sets a color in the color table at index \a i to \a c.
 
1201
 
 
1202
    A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue()
 
1203
    functions to get the color value components.
 
1204
 
 
1205
    \sa color() setNumColors() numColors()
 
1206
*/
 
1207
void QImage::setColor(int i, QRgb c)
 
1208
{
 
1209
    detach();
 
1210
    Q_ASSERT(i < numColors());
 
1211
    d->colortable[i] = c;
 
1212
    d->has_alpha_clut |= (qAlpha(c) != 255);
 
1213
}
 
1214
 
 
1215
/*!
 
1216
    Returns a pointer to the pixel data at the scanline with index \a
 
1217
    i. The first scanline is at index 0.
 
1218
 
 
1219
    The scanline data is aligned on a 32-bit boundary.
 
1220
 
 
1221
    \warning If you are accessing 32-bpp image data, cast the returned
 
1222
    pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to
 
1223
    read/write the pixel value. You cannot use the \c{uchar*} pointer
 
1224
    directly, because the pixel format depends on the byte order on
 
1225
    the underlying platform. Use qRed(), qGreen(), qBlue(), and
 
1226
    qAlpha() to access the pixels.
 
1227
 
 
1228
    \sa bytesPerLine() bits() jumpTable()
 
1229
*/
 
1230
uchar *QImage::scanLine(int i)
 
1231
{
 
1232
    detach();
 
1233
    Q_ASSERT(i >= 0 && i < height());
 
1234
    return d->data + i * d->bytes_per_line;
 
1235
}
 
1236
 
 
1237
/*!
 
1238
    \overload
 
1239
*/
 
1240
const uchar *QImage::scanLine(int i) const
 
1241
{
 
1242
    Q_ASSERT(i >= 0 && i < height());
 
1243
    return d->data + i * d->bytes_per_line;
 
1244
}
 
1245
 
 
1246
 
 
1247
/*!
 
1248
    Returns a pointer to the first pixel data. This is equivalent to
 
1249
    scanLine(0).
 
1250
 
 
1251
    \sa numBytes() scanLine() jumpTable()
 
1252
*/
 
1253
uchar *QImage::bits()
 
1254
{
 
1255
    if (!d)
 
1256
        return 0;
 
1257
    detach();
 
1258
    return d->data;
 
1259
}
 
1260
 
 
1261
/*!
 
1262
    \overload
 
1263
*/
 
1264
const uchar *QImage::bits() const
 
1265
{
 
1266
    return d ? d->data : 0;
 
1267
}
 
1268
 
 
1269
 
 
1270
 
 
1271
/*!
 
1272
  \fn void QImage::reset()
 
1273
 
 
1274
    Resets all image parameters and deallocates the image data.
 
1275
*/
 
1276
 
 
1277
/*!
 
1278
    Fills the entire image with the pixel value \a pixel.
 
1279
 
 
1280
    If the \link depth() depth\endlink of this image is 1, only the
 
1281
    lowest bit is used. If you say fill(0), fill(2), etc., the image
 
1282
    is filled with 0s. If you say fill(1), fill(3), etc., the image is
 
1283
    filled with 1s. If the depth is 8, the lowest 8 bits are used.
 
1284
 
 
1285
    Note: QImage::pixel() returns the color of the pixel at the given
 
1286
    coordinates; QColor::pixel() returns the pixel value of the
 
1287
    underlying window system (essentially an index value), so normally
 
1288
    you will want to use QImage::pixel() to use a color from an
 
1289
    existing image or QColor::rgb() to use a specific color.
 
1290
 
 
1291
    \sa invertPixels() depth() hasAlphaBuffer() create()
 
1292
*/
 
1293
 
 
1294
void QImage::fill(uint pixel)
 
1295
{
 
1296
    if (!d)
 
1297
        return;
 
1298
 
 
1299
    detach();
 
1300
    if (d->depth == 1 || d->depth == 8) {
 
1301
        if (d->depth == 1) {
 
1302
            if (pixel & 1)
 
1303
                pixel = 0xffffffff;
 
1304
            else
 
1305
                pixel = 0;
 
1306
        } else {
 
1307
            pixel &= 0xff;
 
1308
        }
 
1309
        memset(d->data, pixel, d->nbytes);
 
1310
    } else if (d->depth == 32) {
 
1311
        if (d->format == Format_RGB32)
 
1312
            pixel |= 0xff000000;
 
1313
        if (pixel == 0 || pixel == 0xffffffff) {
 
1314
            memset(d->data, (pixel & 0xff), d->nbytes);
 
1315
        } else {
 
1316
            uint *data = (uint *)d->data;
 
1317
            uint *end = (uint *)(d->data + d->nbytes);
 
1318
            while (data < end)
 
1319
                *data++ = pixel;
 
1320
        }
 
1321
    }
 
1322
}
 
1323
 
 
1324
/*!
 
1325
    Inverts all pixel values in the image.
 
1326
 
 
1327
    If the depth is 32: if \a mode is InvertRgba (the default), the alpha bits are
 
1328
    also inverted, otherwise they are left unchanged.
 
1329
 
 
1330
    If the depth is not 32, the argument \a mode has no meaning. The
 
1331
    default mode is InvertRgb, which leaves the alpha channel
 
1332
    unchanged.
 
1333
 
 
1334
    Note that inverting an 8-bit image means to replace all pixels
 
1335
    using color index \e i with a pixel using color index 255 minus \e
 
1336
    i. Similarly for a 1-bit image. The color table is not changed.
 
1337
 
 
1338
    \sa fill() depth() hasAlphaBuffer()
 
1339
*/
 
1340
 
 
1341
void QImage::invertPixels(InvertMode mode)
 
1342
{
 
1343
    if (!d)
 
1344
        return;
 
1345
 
 
1346
    detach();
 
1347
    if (depth() != 32) {
 
1348
        // number of used bytes pr line
 
1349
        int bpl = (d->width + 7) * d->depth / 8;
 
1350
        int pad = d->bytes_per_line - bpl;
 
1351
        uchar *sl = d->data;
 
1352
        for (int y=0; y<d->height; ++y) {
 
1353
            for (int x=0; x<bpl; ++x)
 
1354
                *sl++ ^= 0xff;
 
1355
            sl += pad;
 
1356
        }
 
1357
    } else {
 
1358
        quint32 *p = (quint32*)d->data;
 
1359
        quint32 *end = (quint32*)(d->data + d->nbytes);
 
1360
        uint xorbits = (mode == InvertRgba) ? 0xffffffff : 0x00ffffff;
 
1361
        while (p < end)
 
1362
            *p++ ^= xorbits;
 
1363
    }
 
1364
}
 
1365
 
 
1366
/*!
 
1367
    \fn void QImage::invertPixels(bool invertAlpha)
 
1368
 
 
1369
    Use the invertPixels() function that takes a QImage::InvertMode
 
1370
    parameter instead.
 
1371
*/
 
1372
 
 
1373
/*! \fn QImage::Endian QImage::systemByteOrder()
 
1374
 
 
1375
    Determines the host computer byte order. Returns
 
1376
    QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
 
1377
 
 
1378
    \sa systemBitOrder()
 
1379
*/
 
1380
 
 
1381
// Windows defines these
 
1382
#if defined(write)
 
1383
# undef write
 
1384
#endif
 
1385
#if defined(close)
 
1386
# undef close
 
1387
#endif
 
1388
#if defined(read)
 
1389
# undef read
 
1390
#endif
 
1391
 
 
1392
/*!
 
1393
    Resizes the color table to \a numColors colors.
 
1394
 
 
1395
    If the color table is expanded all the extra colors will be set to
 
1396
    black (RGB 0,0,0).
 
1397
 
 
1398
    \sa numColors() color() setColor() colorTable()
 
1399
*/
 
1400
 
 
1401
void QImage::setNumColors(int numColors)
 
1402
{
 
1403
    if (!d)
 
1404
        return;
 
1405
 
 
1406
    detach();
 
1407
    if (numColors == d->colortable.size())
 
1408
        return;
 
1409
    if (numColors <= 0) {                        // use no color table
 
1410
        d->colortable = QVector<QRgb>();
 
1411
        return;
 
1412
    }
 
1413
    int nc = d->colortable.size();
 
1414
    d->colortable.resize(numColors);
 
1415
    for (int i = nc; i < numColors; ++i)
 
1416
        d->colortable[i] = 0;
 
1417
}
 
1418
 
 
1419
/*!
 
1420
  returns the format of the image.
 
1421
*/
 
1422
QImage::Format QImage::format() const
 
1423
{
 
1424
    return d ? d->format : Format_Invalid;
 
1425
}
 
1426
 
 
1427
 
 
1428
#ifdef QT3_SUPPORT
 
1429
/*!
 
1430
    Returns true if alpha buffer mode is enabled; otherwise returns
 
1431
    false.
 
1432
 
 
1433
    \sa setAlphaBuffer()
 
1434
*/
 
1435
bool QImage::hasAlphaBuffer() const
 
1436
{
 
1437
    return (d && (d->format != Format_RGB32));
 
1438
}
 
1439
 
 
1440
/*!
 
1441
    Enables alpha buffer mode if \a enable is true, otherwise disables
 
1442
    it. The default setting is disabled.
 
1443
 
 
1444
    An 8-bpp image has 8-bit pixels. A pixel is an index into the
 
1445
    \link color() color table\endlink, which contains 32-bit color
 
1446
    values. In a 32-bpp image, the 32-bit pixels are the color values.
 
1447
 
 
1448
    This 32-bit value is encoded as follows: The lower 24 bits are
 
1449
    used for the red, green, and blue components. The upper 8 bits
 
1450
    contain the alpha component.
 
1451
 
 
1452
    The alpha component specifies the transparency of a pixel. 0 means
 
1453
    completely transparent and 255 means opaque. The alpha component
 
1454
    is ignored if you do not enable alpha buffer mode.
 
1455
 
 
1456
    The alpha buffer is used to set a mask when a QImage is translated
 
1457
    to a QPixmap.
 
1458
 
 
1459
    \sa hasAlphaBuffer() createAlphaMask()
 
1460
*/
 
1461
 
 
1462
void QImage::setAlphaBuffer(bool enable)
 
1463
{
 
1464
    if (!d
 
1465
        || d->format == Format_Mono
 
1466
        || d->format == Format_MonoLSB
 
1467
        || d->format == Format_Indexed8)
 
1468
        return;
 
1469
    if (enable && (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied))
 
1470
        return;
 
1471
    if (!enable && d->format == Format_RGB32)
 
1472
        return;
 
1473
    detach();
 
1474
    d->format = (enable ? Format_ARGB32 : Format_RGB32);
 
1475
}
 
1476
 
 
1477
 
 
1478
/*!
 
1479
  \fn bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
 
1480
 
 
1481
    Sets the image \a width, \a height, \a depth, its number of colors
 
1482
    (in \a numColors), and bit order. Returns true if successful, or
 
1483
    false if the parameters are incorrect or if memory cannot be
 
1484
    allocated.
 
1485
 
 
1486
    The \a width and \a height is limited to 32767. \a depth must be
 
1487
    1, 8, or 32. If \a depth is 1, \a bitOrder must be set to
 
1488
    either QImage::LittleEndian or QImage::BigEndian. For other depths
 
1489
    \a bitOrder must be QImage::IgnoreEndian.
 
1490
 
 
1491
    This function allocates a color table and a buffer for the image
 
1492
    data. The image data is not initialized.
 
1493
 
 
1494
    The image buffer is allocated as a single block that consists of a
 
1495
    table of \link scanLine() scanline\endlink pointers (jumpTable())
 
1496
    and the image data (bits()).
 
1497
 
 
1498
    \sa fill() width() height() depth() numColors() bitOrder()
 
1499
    jumpTable() scanLine() bits() bytesPerLine() numBytes()
 
1500
*/
 
1501
bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
 
1502
{
 
1503
    if (d && !d->ref.deref())
 
1504
        delete d;
 
1505
    d = QImageData::create(QSize(width, height), formatFor(depth, bitOrder), numColors);
 
1506
    return true;
 
1507
}
 
1508
 
 
1509
/*!
 
1510
    \fn bool QImage::create(const QSize& size, int depth, int numColors, Endian bitOrder)
 
1511
    \overload
 
1512
 
 
1513
    The width and height are specified in the \a size argument.
 
1514
*/
 
1515
bool QImage::create(const QSize& size, int depth, int numColors, QImage::Endian bitOrder)
 
1516
{
 
1517
    if (d && !d->ref.deref())
 
1518
        delete d;
 
1519
    d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
 
1520
    return true;
 
1521
}
 
1522
#endif // QT3_SUPPORT
 
1523
 
 
1524
/*****************************************************************************
 
1525
  Internal routines for converting image depth.
 
1526
 *****************************************************************************/
 
1527
 
 
1528
typedef void (*Image_Converter)(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
 
1529
 
 
1530
static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
1531
{
 
1532
    Q_ASSERT(src->format == QImage::Format_ARGB32);
 
1533
    Q_ASSERT(dest->format == QImage::Format_ARGB32_Premultiplied);
 
1534
    Q_ASSERT(src->width == dest->width);
 
1535
    Q_ASSERT(src->height == dest->height);
 
1536
    Q_ASSERT(src->nbytes == dest->nbytes);
 
1537
    Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
 
1538
 
 
1539
    const QRgb *src_data = (QRgb *) src->data;
 
1540
    const QRgb *end = src_data + (src->nbytes>>2);
 
1541
    QRgb *dest_data = (QRgb *) dest->data;
 
1542
    while (src_data < end) {
 
1543
        *dest_data = PREMUL(*src_data);
 
1544
        ++src_data;
 
1545
        ++dest_data;
 
1546
    }
 
1547
}
 
1548
 
 
1549
static void convert_ARGB_PM_to_ARGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
1550
{
 
1551
    Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
 
1552
    Q_ASSERT(dest->format == QImage::Format_ARGB32);
 
1553
    Q_ASSERT(src->width == dest->width);
 
1554
    Q_ASSERT(src->height == dest->height);
 
1555
    Q_ASSERT(src->nbytes == dest->nbytes);
 
1556
    Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
 
1557
 
 
1558
    const QRgb *src_data = (QRgb *) src->data;
 
1559
    const QRgb *end = src_data + (src->nbytes>>2);
 
1560
    QRgb *dest_data = (QRgb *) dest->data;
 
1561
    while (src_data < end) {
 
1562
        *dest_data = INV_PREMUL(*src_data);
 
1563
        ++src_data;
 
1564
        ++dest_data;
 
1565
    }
 
1566
}
 
1567
 
 
1568
static void convert_ARGB_PM_to_RGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
1569
{
 
1570
    Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
 
1571
    Q_ASSERT(dest->format == QImage::Format_RGB32);
 
1572
    Q_ASSERT(src->width == dest->width);
 
1573
    Q_ASSERT(src->height == dest->height);
 
1574
    Q_ASSERT(src->nbytes == dest->nbytes);
 
1575
    Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
 
1576
 
 
1577
    const QRgb *src_data = (QRgb *) src->data;
 
1578
    const QRgb *end = src_data + (src->nbytes>>2);
 
1579
    QRgb *dest_data = (QRgb *) dest->data;
 
1580
    while (src_data < end) {
 
1581
        *dest_data = 0xff000000 | INV_PREMUL(*src_data);
 
1582
        ++src_data;
 
1583
        ++dest_data;
 
1584
    }
 
1585
}
 
1586
 
 
1587
static void swap_bit_order(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
1588
{
 
1589
    Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
 
1590
    Q_ASSERT(dest->format == QImage::Format_Mono || dest->format == QImage::Format_MonoLSB);
 
1591
    Q_ASSERT(src->width == dest->width);
 
1592
    Q_ASSERT(src->height == dest->height);
 
1593
    Q_ASSERT(src->nbytes == dest->nbytes);
 
1594
    Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
 
1595
 
 
1596
    dest->colortable = src->colortable;
 
1597
 
 
1598
    const uchar *src_data = src->data;
 
1599
    const uchar *end = src->data + src->nbytes;
 
1600
    uchar *dest_data = dest->data;
 
1601
    while (src_data < end) {
 
1602
        *dest_data = bitflip[*src_data];
 
1603
        ++src_data;
 
1604
        ++dest_data;
 
1605
    }
 
1606
}
 
1607
 
 
1608
static void mask_alpha_converter(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
1609
{
 
1610
    Q_ASSERT(src->width == dest->width);
 
1611
    Q_ASSERT(src->height == dest->height);
 
1612
    Q_ASSERT(src->nbytes == dest->nbytes);
 
1613
 
 
1614
    const uint *src_data = (const uint *)src->data;
 
1615
    const uint *end = (const uint *)(src->data + src->nbytes);
 
1616
    uint *dest_data = (uint *)dest->data;
 
1617
    while (src_data < end) {
 
1618
        *dest_data = *src_data | 0xff000000;
 
1619
        ++src_data;
 
1620
        ++dest_data;
 
1621
    }
 
1622
}
 
1623
 
 
1624
static QVector<QRgb> fix_color_table(const QVector<QRgb> &ctbl, QImage::Format format)
 
1625
{
 
1626
    QVector<QRgb> colorTable = ctbl;
 
1627
    if (format == QImage::Format_RGB32) {
 
1628
        // check if the color table has alpha
 
1629
        for (int i = 0; i < colorTable.size(); ++i)
 
1630
            if (qAlpha(colorTable.at(i) != 0xff))
 
1631
                colorTable[i] = colorTable.at(i) | 0xff000000;
 
1632
    } else if (format == QImage::Format_ARGB32_Premultiplied) {
 
1633
        // check if the color table has alpha
 
1634
        for (int i = 0; i < colorTable.size(); ++i)
 
1635
            colorTable[i] = PREMUL(colorTable.at(i));
 
1636
    }
 
1637
    return colorTable;
 
1638
}
 
1639
 
 
1640
#ifndef QT_NO_IMAGE_DITHER_TO_1
 
1641
//
 
1642
// dither_to_1:  Uses selected dithering algorithm.
 
1643
//
 
1644
 
 
1645
static void dither_to_Mono(QImageData *dst, const QImageData *src,
 
1646
                           Qt::ImageConversionFlags flags, bool fromalpha)
 
1647
{
 
1648
    Q_ASSERT(src->width == dst->width);
 
1649
    Q_ASSERT(src->height == dst->height);
 
1650
    Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB);
 
1651
 
 
1652
    dst->colortable.clear();
 
1653
    dst->colortable.append(0xffffffff);
 
1654
    dst->colortable.append(0xff000000);
 
1655
 
 
1656
    enum { Threshold, Ordered, Diffuse } dithermode;
 
1657
 
 
1658
    if (fromalpha) {
 
1659
        if ((flags & Qt::AlphaDither_Mask) == Qt::DiffuseAlphaDither)
 
1660
            dithermode = Diffuse;
 
1661
        else if ((flags & Qt::AlphaDither_Mask) == Qt::OrderedAlphaDither)
 
1662
            dithermode = Ordered;
 
1663
        else
 
1664
            dithermode = Threshold;
 
1665
    } else {
 
1666
        if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither)
 
1667
            dithermode = Threshold;
 
1668
        else if ((flags & Qt::Dither_Mask) == Qt::OrderedDither)
 
1669
            dithermode = Ordered;
 
1670
        else
 
1671
            dithermode = Diffuse;
 
1672
    }
 
1673
 
 
1674
    int          w = src->width;
 
1675
    int          h = src->height;
 
1676
    int          d = src->depth;
 
1677
    uchar gray[256];                                // gray map for 8 bit images
 
1678
    bool  use_gray = (d == 8);
 
1679
    if (use_gray) {                                // make gray map
 
1680
        if (fromalpha) {
 
1681
            // Alpha 0x00 -> 0 pixels (white)
 
1682
            // Alpha 0xFF -> 1 pixels (black)
 
1683
            for (int i = 0; i < src->colortable.size(); i++)
 
1684
                gray[i] = (255 - (src->colortable.at(i) >> 24));
 
1685
        } else {
 
1686
            // Pixel 0x00 -> 1 pixels (black)
 
1687
            // Pixel 0xFF -> 0 pixels (white)
 
1688
            for (int i = 0; i < src->colortable.size(); i++)
 
1689
                gray[i] = qGray(src->colortable.at(i));
 
1690
        }
 
1691
    }
 
1692
 
 
1693
    uchar *dst_data = dst->data;
 
1694
    int dst_bpl = dst->bytes_per_line;
 
1695
    const uchar *src_data = src->data;
 
1696
    int src_bpl = src->bytes_per_line;
 
1697
 
 
1698
    switch (dithermode) {
 
1699
    case Diffuse: {
 
1700
        int *line1 = new int[w];
 
1701
        int *line2 = new int[w];
 
1702
        int bmwidth = (w+7)/8;
 
1703
 
 
1704
        int *b1, *b2;
 
1705
        int wbytes = w * (d/8);
 
1706
        register const uchar *p = src->data;
 
1707
        const uchar *end = p + wbytes;
 
1708
        b2 = line2;
 
1709
        if (use_gray) {                        // 8 bit image
 
1710
            while (p < end)
 
1711
                *b2++ = gray[*p++];
 
1712
        } else {                                // 32 bit image
 
1713
            if (fromalpha) {
 
1714
                while (p < end) {
 
1715
                    *b2++ = 255 - (*(uint*)p >> 24);
 
1716
                    p += 4;
 
1717
                }
 
1718
            } else {
 
1719
                while (p < end) {
 
1720
                    *b2++ = qGray(*(uint*)p);
 
1721
                    p += 4;
 
1722
                }
 
1723
            }
 
1724
        }
 
1725
        for (int y=0; y<h; y++) {                        // for each scan line...
 
1726
            int *tmp = line1; line1 = line2; line2 = tmp;
 
1727
            bool not_last_line = y < h - 1;
 
1728
            if (not_last_line) {                // calc. grayvals for next line
 
1729
                p = src->data + (y+1)*src->bytes_per_line;
 
1730
                end = p + wbytes;
 
1731
                b2 = line2;
 
1732
                if (use_gray) {                // 8 bit image
 
1733
                    while (p < end)
 
1734
                        *b2++ = gray[*p++];
 
1735
                } else {                        // 24 bit image
 
1736
                    if (fromalpha) {
 
1737
                        while (p < end) {
 
1738
                            *b2++ = 255 - (*(uint*)p >> 24);
 
1739
                            p += 4;
 
1740
                        }
 
1741
                    } else {
 
1742
                        while (p < end) {
 
1743
                            *b2++ = qGray(*(uint*)p);
 
1744
                            p += 4;
 
1745
                        }
 
1746
                    }
 
1747
                }
 
1748
            }
 
1749
 
 
1750
            int err;
 
1751
            uchar *p = dst->data + y*dst->bytes_per_line;
 
1752
            memset(p, 0, bmwidth);
 
1753
            b1 = line1;
 
1754
            b2 = line2;
 
1755
            int bit = 7;
 
1756
            for (int x=1; x<=w; x++) {
 
1757
                if (*b1 < 128) {                // black pixel
 
1758
                    err = *b1++;
 
1759
                    *p |= 1 << bit;
 
1760
                } else {                        // white pixel
 
1761
                    err = *b1++ - 255;
 
1762
                }
 
1763
                if (bit == 0) {
 
1764
                    p++;
 
1765
                    bit = 7;
 
1766
                } else {
 
1767
                    bit--;
 
1768
                }
 
1769
                if (x < w)
 
1770
                    *b1 += (err*7)>>4;                // spread error to right pixel
 
1771
                if (not_last_line) {
 
1772
                    b2[0] += (err*5)>>4;        // pixel below
 
1773
                    if (x > 1)
 
1774
                        b2[-1] += (err*3)>>4;        // pixel below left
 
1775
                    if (x < w)
 
1776
                        b2[1] += err>>4;        // pixel below right
 
1777
                }
 
1778
                b2++;
 
1779
            }
 
1780
        }
 
1781
        delete [] line1;
 
1782
        delete [] line2;
 
1783
    } break;
 
1784
    case Ordered: {
 
1785
 
 
1786
        memset(dst->data, 0, dst->nbytes);
 
1787
        if (d == 32) {
 
1788
            for (int i=0; i<h; i++) {
 
1789
                const uint *p = (const uint *)src_data;
 
1790
                const uint *end = p + w;
 
1791
                uchar *m = dst_data;
 
1792
                int bit = 7;
 
1793
                int j = 0;
 
1794
                if (fromalpha) {
 
1795
                    while (p < end) {
 
1796
                        if ((*p++ >> 24) >= qt_bayer_matrix[j++&15][i&15])
 
1797
                            *m |= 1 << bit;
 
1798
                        if (bit == 0) {
 
1799
                            m++;
 
1800
                            bit = 7;
 
1801
                        } else {
 
1802
                            bit--;
 
1803
                        }
 
1804
                    }
 
1805
                } else {
 
1806
                    while (p < end) {
 
1807
                        if ((uint)qGray(*p++) < qt_bayer_matrix[j++&15][i&15])
 
1808
                            *m |= 1 << bit;
 
1809
                        if (bit == 0) {
 
1810
                            m++;
 
1811
                            bit = 7;
 
1812
                        } else {
 
1813
                            bit--;
 
1814
                        }
 
1815
                    }
 
1816
                }
 
1817
                dst_data += dst_bpl;
 
1818
                src_data += src_bpl;
 
1819
            }
 
1820
        } else
 
1821
            /* (d == 8) */ {
 
1822
            for (int i=0; i<h; i++) {
 
1823
                const uchar *p = src_data;
 
1824
                const uchar *end = p + w;
 
1825
                uchar *m = dst_data;
 
1826
                int bit = 7;
 
1827
                int j = 0;
 
1828
                while (p < end) {
 
1829
                    if ((uint)gray[*p++] < qt_bayer_matrix[j++&15][i&15])
 
1830
                        *m |= 1 << bit;
 
1831
                    if (bit == 0) {
 
1832
                        m++;
 
1833
                        bit = 7;
 
1834
                    } else {
 
1835
                        bit--;
 
1836
                    }
 
1837
                }
 
1838
                dst_data += dst_bpl;
 
1839
                src_data += src_bpl;
 
1840
            }
 
1841
        }
 
1842
    } break;
 
1843
    default: { // Threshold:
 
1844
        memset(dst->data, 0, dst->nbytes);
 
1845
        if (d == 32) {
 
1846
            for (int i=0; i<h; i++) {
 
1847
                const uint *p = (const uint *)src_data;
 
1848
                const uint *end = p + w;
 
1849
                uchar *m = dst_data;
 
1850
                int bit = 7;
 
1851
                if (fromalpha) {
 
1852
                    while (p < end) {
 
1853
                        if ((*p++ >> 24) >= 128)
 
1854
                            *m |= 1 << bit;        // Set mask "on"
 
1855
                        if (bit == 0) {
 
1856
                            m++;
 
1857
                            bit = 7;
 
1858
                        } else {
 
1859
                            bit--;
 
1860
                        }
 
1861
                    }
 
1862
                } else {
 
1863
                    while (p < end) {
 
1864
                        if (qGray(*p++) < 128)
 
1865
                            *m |= 1 << bit;        // Set pixel "black"
 
1866
                        if (bit == 0) {
 
1867
                            m++;
 
1868
                            bit = 7;
 
1869
                        } else {
 
1870
                            bit--;
 
1871
                        }
 
1872
                    }
 
1873
                }
 
1874
                dst_data += dst_bpl;
 
1875
                src_data += src_bpl;
 
1876
            }
 
1877
        } else
 
1878
            if (d == 8) {
 
1879
                for (int i=0; i<h; i++) {
 
1880
                    const uchar *p = src_data;
 
1881
                    const uchar *end = p + w;
 
1882
                    uchar *m = dst_data;
 
1883
                    int bit = 7;
 
1884
                    while (p < end) {
 
1885
                        if (gray[*p++] < 128)
 
1886
                            *m |= 1 << bit;                // Set mask "on"/ pixel "black"
 
1887
                        if (bit == 0) {
 
1888
                            m++;
 
1889
                            bit = 7;
 
1890
                        } else {
 
1891
                            bit--;
 
1892
                        }
 
1893
                    }
 
1894
                    dst_data += dst_bpl;
 
1895
                    src_data += src_bpl;
 
1896
                }
 
1897
            }
 
1898
        }
 
1899
    }
 
1900
 
 
1901
    if (dst->format == QImage::Format_MonoLSB) {
 
1902
        // need to swap bit order
 
1903
        uchar *sl = dst->data;
 
1904
        int bpl = (dst->width + 7) * dst->depth / 8;
 
1905
        int pad = dst->bytes_per_line - bpl;
 
1906
        for (int y=0; y<dst->height; ++y) {
 
1907
            for (int x=0; x<bpl; ++x) {
 
1908
                *sl = bitflip[*sl];
 
1909
                ++sl;
 
1910
            }
 
1911
            sl += pad;
 
1912
        }
 
1913
    }
 
1914
}
 
1915
#endif
 
1916
 
 
1917
static void convert_X_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
 
1918
{
 
1919
    dither_to_Mono(dst, src, flags, false);
 
1920
}
 
1921
 
 
1922
static void convert_ARGB_PM_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
 
1923
{
 
1924
    QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
 
1925
    convert_ARGB_PM_to_ARGB(tmp, src, flags);
 
1926
    dither_to_Mono(dst, tmp, flags, false);
 
1927
    delete tmp;
 
1928
}
 
1929
 
 
1930
//
 
1931
// convert_32_to_8:  Converts a 32 bits depth (true color) to an 8 bit
 
1932
// image with a colormap. If the 32 bit image has more than 256 colors,
 
1933
// we convert the red,green and blue bytes into a single byte encoded
 
1934
// as 6 shades of each of red, green and blue.
 
1935
//
 
1936
// if dithering is needed, only 1 color at most is available for alpha.
 
1937
//
 
1938
struct QRgbMap {
 
1939
    inline QRgbMap() : rgb(0xffffffff) { }
 
1940
    inline bool used() const { return rgb!=0xffffffff; }
 
1941
    uchar  pix;
 
1942
    QRgb  rgb;
 
1943
};
 
1944
 
 
1945
static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
 
1946
{
 
1947
    Q_ASSERT(src->format == QImage::Format_RGB32 || src->format == QImage::Format_ARGB32);
 
1948
    Q_ASSERT(dst->format == QImage::Format_Indexed8);
 
1949
    Q_ASSERT(src->width == dst->width);
 
1950
    Q_ASSERT(src->height == dst->height);
 
1951
 
 
1952
    bool    do_quant = (flags & Qt::DitherMode_Mask) == Qt::PreferDither
 
1953
                       || src->format == QImage::Format_ARGB32;
 
1954
    uint alpha_mask = src->format == QImage::Format_RGB32 ? 0xff000000 : 0;
 
1955
 
 
1956
    const int tablesize = 997; // prime
 
1957
    QRgbMap table[tablesize];
 
1958
    int   pix=0;
 
1959
 
 
1960
    if (!dst->colortable.isEmpty()) {
 
1961
        QVector<QRgb> ctbl = dst->colortable;
 
1962
        dst->colortable.resize(256);
 
1963
        // Preload palette into table.
 
1964
        // Almost same code as pixel insertion below
 
1965
        for (int i = 0; i < dst->colortable.size(); ++i) {
 
1966
            // Find in table...
 
1967
            QRgb p = ctbl.at(i) | alpha_mask;
 
1968
            int hash = p % tablesize;
 
1969
            for (;;) {
 
1970
                if (table[hash].used()) {
 
1971
                    if (table[hash].rgb == p) {
 
1972
                        // Found previous insertion - use it
 
1973
                        break;
 
1974
                    } else {
 
1975
                        // Keep searching...
 
1976
                        if (++hash == tablesize) hash = 0;
 
1977
                    }
 
1978
                } else {
 
1979
                    // Cannot be in table
 
1980
                    Q_ASSERT (pix != 256);        // too many colors
 
1981
                    // Insert into table at this unused position
 
1982
                    dst->colortable[pix] = p;
 
1983
                    table[hash].pix = pix++;
 
1984
                    table[hash].rgb = p;
 
1985
                    break;
 
1986
                }
 
1987
            }
 
1988
        }
 
1989
    }
 
1990
 
 
1991
    if ((flags & Qt::DitherMode_Mask) != Qt::PreferDither) {
 
1992
        dst->colortable.resize(256);
 
1993
        const uchar *src_data = src->data;
 
1994
        uchar *dest_data = dst->data;
 
1995
        for (int y = 0; y < src->height; y++) {        // check if <= 256 colors
 
1996
            const QRgb *s = (const QRgb *)src_data;
 
1997
            uchar *b = dest_data;
 
1998
            for (int x = 0; x < src->width; ++x) {
 
1999
                QRgb p = s[x] | alpha_mask;
 
2000
                int hash = p % tablesize;
 
2001
                for (;;) {
 
2002
                    if (table[hash].used()) {
 
2003
                        if (table[hash].rgb == (p)) {
 
2004
                            // Found previous insertion - use it
 
2005
                            break;
 
2006
                        } else {
 
2007
                            // Keep searching...
 
2008
                            if (++hash == tablesize) hash = 0;
 
2009
                        }
 
2010
                    } else {
 
2011
                        // Cannot be in table
 
2012
                        if (pix == 256) {        // too many colors
 
2013
                            do_quant = true;
 
2014
                            // Break right out
 
2015
                            x = src->width;
 
2016
                            y = src->height;
 
2017
                        } else {
 
2018
                            // Insert into table at this unused position
 
2019
                            dst->colortable[pix] = p;
 
2020
                            table[hash].pix = pix++;
 
2021
                            table[hash].rgb = p;
 
2022
                        }
 
2023
                        break;
 
2024
                    }
 
2025
                }
 
2026
                *b++ = table[hash].pix;                // May occur once incorrectly
 
2027
            }
 
2028
            src_data += src->bytes_per_line;
 
2029
            dest_data += dst->bytes_per_line;
 
2030
        }
 
2031
    }
 
2032
    int numColors = do_quant ? 256 : pix;
 
2033
 
 
2034
    dst->colortable.resize(numColors);
 
2035
 
 
2036
    if (do_quant) {                                // quantization needed
 
2037
 
 
2038
#define MAX_R 5
 
2039
#define MAX_G 5
 
2040
#define MAX_B 5
 
2041
#define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b))
 
2042
 
 
2043
        for (int rc=0; rc<=MAX_R; rc++)                // build 6x6x6 color cube
 
2044
            for (int gc=0; gc<=MAX_G; gc++)
 
2045
                for (int bc=0; bc<=MAX_B; bc++)
 
2046
                    dst->colortable[INDEXOF(rc,gc,bc)] = 0xff000000 | qRgb(rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B);
 
2047
 
 
2048
        const uchar *src_data = src->data;
 
2049
        uchar *dest_data = dst->data;
 
2050
        if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither) {
 
2051
            for (int y = 0; y < src->height; y++) {
 
2052
                const QRgb *p = (const QRgb *)src_data;
 
2053
                const QRgb *end = p + src->width;
 
2054
                uchar *b = dest_data;
 
2055
 
 
2056
                while (p < end) {
 
2057
#define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255))
 
2058
                    *b++ =
 
2059
                        INDEXOF(
 
2060
                            DITHER(qRed(*p), MAX_R),
 
2061
                            DITHER(qGreen(*p), MAX_G),
 
2062
                            DITHER(qBlue(*p), MAX_B)
 
2063
                            );
 
2064
#undef DITHER
 
2065
                    p++;
 
2066
                }
 
2067
                src_data += src->bytes_per_line;
 
2068
                dest_data += dst->bytes_per_line;
 
2069
            }
 
2070
        } else if ((flags & Qt::Dither_Mask) == Qt::DiffuseDither) {
 
2071
            int* line1[3];
 
2072
            int* line2[3];
 
2073
            int* pv[3];
 
2074
            line1[0] = new int[src->width];
 
2075
            line2[0] = new int[src->width];
 
2076
            line1[1] = new int[src->width];
 
2077
            line2[1] = new int[src->width];
 
2078
            line1[2] = new int[src->width];
 
2079
            line2[2] = new int[src->width];
 
2080
            pv[0] = new int[src->width];
 
2081
            pv[1] = new int[src->width];
 
2082
            pv[2] = new int[src->width];
 
2083
 
 
2084
            int endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian);
 
2085
            for (int y = 0; y < src->height; y++) {
 
2086
                const uchar* q = src_data;
 
2087
                const uchar* q2 = y < src->height - 1 ? q + src->bytes_per_line : src->data;
 
2088
                uchar *b = dest_data;
 
2089
                for (int chan = 0; chan < 3; chan++) {
 
2090
                    int *l1 = (y&1) ? line2[chan] : line1[chan];
 
2091
                    int *l2 = (y&1) ? line1[chan] : line2[chan];
 
2092
                    if (y == 0) {
 
2093
                        for (int i = 0; i < src->width; i++)
 
2094
                            l1[i] = q[i*4+chan+endian];
 
2095
                    }
 
2096
                    if (y+1 < src->height) {
 
2097
                        for (int i = 0; i < src->width; i++)
 
2098
                            l2[i] = q2[i*4+chan+endian];
 
2099
                    }
 
2100
                    // Bi-directional error diffusion
 
2101
                    if (y&1) {
 
2102
                        for (int x = 0; x < src->width; x++) {
 
2103
                            int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
 
2104
                            int err = l1[x] - pix * 255 / 5;
 
2105
                            pv[chan][x] = pix;
 
2106
 
 
2107
                            // Spread the error around...
 
2108
                            if (x + 1< src->width) {
 
2109
                                l1[x+1] += (err*7)>>4;
 
2110
                                l2[x+1] += err>>4;
 
2111
                            }
 
2112
                            l2[x]+=(err*5)>>4;
 
2113
                            if (x>1)
 
2114
                                l2[x-1]+=(err*3)>>4;
 
2115
                        }
 
2116
                    } else {
 
2117
                        for (int x = src->width; x-- > 0;) {
 
2118
                            int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
 
2119
                            int err = l1[x] - pix * 255 / 5;
 
2120
                            pv[chan][x] = pix;
 
2121
 
 
2122
                            // Spread the error around...
 
2123
                            if (x > 0) {
 
2124
                                l1[x-1] += (err*7)>>4;
 
2125
                                l2[x-1] += err>>4;
 
2126
                            }
 
2127
                            l2[x]+=(err*5)>>4;
 
2128
                            if (x + 1 < src->width)
 
2129
                                l2[x+1]+=(err*3)>>4;
 
2130
                        }
 
2131
                    }
 
2132
                }
 
2133
                if (endian) {
 
2134
                    for (int x = 0; x < src->width; x++) {
 
2135
                        *b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]);
 
2136
                    }
 
2137
                } else {
 
2138
                    for (int x = 0; x < src->width; x++) {
 
2139
                        *b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]);
 
2140
                    }
 
2141
                }
 
2142
                src_data += src->bytes_per_line;
 
2143
                dest_data += dst->bytes_per_line;
 
2144
            }
 
2145
            delete [] line1[0];
 
2146
            delete [] line2[0];
 
2147
            delete [] line1[1];
 
2148
            delete [] line2[1];
 
2149
            delete [] line1[2];
 
2150
            delete [] line2[2];
 
2151
            delete [] pv[0];
 
2152
            delete [] pv[1];
 
2153
            delete [] pv[2];
 
2154
        } else { // OrderedDither
 
2155
            for (int y = 0; y < src->height; y++) {
 
2156
                const QRgb *p = (const QRgb *)src_data;
 
2157
                const QRgb *end = p + src->width;
 
2158
                uchar *b = dest_data;
 
2159
 
 
2160
                int x = 0;
 
2161
                while (p < end) {
 
2162
                    uint d = qt_bayer_matrix[y & 15][x & 15] << 8;
 
2163
 
 
2164
#define DITHER(p, d, m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) >> 16))
 
2165
                    *b++ =
 
2166
                        INDEXOF(
 
2167
                            DITHER(qRed(*p), d, MAX_R),
 
2168
                            DITHER(qGreen(*p), d, MAX_G),
 
2169
                            DITHER(qBlue(*p), d, MAX_B)
 
2170
                            );
 
2171
#undef DITHER
 
2172
 
 
2173
                    p++;
 
2174
                    x++;
 
2175
                }
 
2176
                src_data += src->bytes_per_line;
 
2177
                dest_data += dst->bytes_per_line;
 
2178
            }
 
2179
        }
 
2180
 
 
2181
#ifndef QT_NO_IMAGE_DITHER_TO_1
 
2182
        if (src->format != QImage::Format_RGB32) {
 
2183
            const int trans = 216;
 
2184
            Q_ASSERT(dst->colortable.size() > trans);
 
2185
            dst->colortable[trans] = 0;
 
2186
            QImageData *mask = QImageData::create(QSize(src->width, src->height), QImage::Format_Mono);
 
2187
            dither_to_Mono(mask, src, flags, true);
 
2188
            uchar *dst_data = dst->data;
 
2189
            const uchar *mask_data = mask->data;
 
2190
            for (int y = 0; y < src->height; y++) {
 
2191
                for (int x = 0; x < src->width ; x++) {
 
2192
                    if (!(mask_data[x>>3] & (0x80 >> (x & 7))))
 
2193
                        dst_data[x] = trans;
 
2194
                }
 
2195
                mask_data += mask->bytes_per_line;
 
2196
                dst_data += dst->bytes_per_line;
 
2197
            }
 
2198
            dst->has_alpha_clut = true;
 
2199
        }
 
2200
#endif
 
2201
 
 
2202
#undef MAX_R
 
2203
#undef MAX_G
 
2204
#undef MAX_B
 
2205
#undef INDEXOF
 
2206
 
 
2207
    }
 
2208
}
 
2209
 
 
2210
static void convert_ARGB_PM_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
 
2211
{
 
2212
    QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
 
2213
    convert_ARGB_PM_to_ARGB(tmp, src, flags);
 
2214
    convert_RGB_to_Indexed8(dst, tmp, flags);
 
2215
    delete tmp;
 
2216
}
 
2217
 
 
2218
static void convert_ARGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
 
2219
{
 
2220
    convert_RGB_to_Indexed8(dst, src, flags);
 
2221
}
 
2222
 
 
2223
static void convert_Indexed8_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
2224
{
 
2225
    Q_ASSERT(src->format == QImage::Format_Indexed8);
 
2226
    Q_ASSERT(dest->format == QImage::Format_RGB32
 
2227
             || dest->format == QImage::Format_ARGB32
 
2228
             || dest->format == QImage::Format_ARGB32_Premultiplied);
 
2229
    Q_ASSERT(src->width == dest->width);
 
2230
    Q_ASSERT(src->height == dest->height);
 
2231
 
 
2232
    QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
 
2233
 
 
2234
    int w = src->width;
 
2235
    const uchar *src_data = src->data;
 
2236
    uchar *dest_data = dest->data;
 
2237
    for (int y = 0; y < src->height; y++) {
 
2238
        uint *p = (uint *)dest_data;
 
2239
        const uchar *b = src_data;
 
2240
        uint *end = p + w;
 
2241
 
 
2242
        while (p < end)
 
2243
            *p++ = colorTable.at(*b++);
 
2244
 
 
2245
        src_data += src->bytes_per_line;
 
2246
        dest_data += dest->bytes_per_line;
 
2247
    }
 
2248
}
 
2249
 
 
2250
static void convert_Mono_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
2251
{
 
2252
    Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
 
2253
    Q_ASSERT(dest->format == QImage::Format_RGB32
 
2254
             || dest->format == QImage::Format_ARGB32
 
2255
             || dest->format == QImage::Format_ARGB32_Premultiplied);
 
2256
    Q_ASSERT(src->width == dest->width);
 
2257
    Q_ASSERT(src->height == dest->height);
 
2258
 
 
2259
    QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
 
2260
 
 
2261
    // Default to black / white colors
 
2262
    if (colorTable.size() < 2) {
 
2263
        if (colorTable.size() == 0)
 
2264
            colorTable << 0xff000000;
 
2265
        colorTable << 0xffffffff;
 
2266
    }
 
2267
 
 
2268
    const uchar *src_data = src->data;
 
2269
    uchar *dest_data = dest->data;
 
2270
    if (src->format == QImage::Format_Mono) {
 
2271
        for (int y = 0; y < dest->height; y++) {
 
2272
            register uint *p = (uint *)dest_data;
 
2273
            for (int x = 0; x < dest->width; x++)
 
2274
                *p++ = colorTable.at((src_data[x>>3] >> (7 - (x & 7))) & 1);
 
2275
 
 
2276
            src_data += src->bytes_per_line;
 
2277
            dest_data += dest->bytes_per_line;
 
2278
        }
 
2279
    } else {
 
2280
        for (int y = 0; y < dest->height; y++) {
 
2281
            register uint *p = (uint *)dest_data;
 
2282
            for (int x = 0; x < dest->width; x++)
 
2283
                *p++ = colorTable.at((src_data[x>>3] >> (x & 7)) & 1);
 
2284
 
 
2285
            src_data += src->bytes_per_line;
 
2286
            dest_data += dest->bytes_per_line;
 
2287
        }
 
2288
    }
 
2289
}
 
2290
 
 
2291
 
 
2292
static void convert_Mono_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
 
2293
{
 
2294
    Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
 
2295
    Q_ASSERT(dest->format == QImage::Format_Indexed8);
 
2296
    Q_ASSERT(src->width == dest->width);
 
2297
    Q_ASSERT(src->height == dest->height);
 
2298
 
 
2299
    QVector<QRgb> ctbl = src->colortable;
 
2300
    if (ctbl.size() > 2) {
 
2301
        ctbl.resize(2);
 
2302
    } else if (ctbl.size() < 2) {
 
2303
        if (ctbl.size() == 0)
 
2304
            ctbl << 0xff000000;
 
2305
        ctbl << 0xffffffff;
 
2306
    }
 
2307
    dest->colortable = ctbl;
 
2308
 
 
2309
 
 
2310
    const uchar *src_data = src->data;
 
2311
    uchar *dest_data = dest->data;
 
2312
    if (src->format == QImage::Format_Mono) {
 
2313
        for (int y = 0; y < dest->height; y++) {
 
2314
            register uchar *p = dest_data;
 
2315
            for (int x = 0; x < dest->width; x++)
 
2316
                *p++ = (src_data[x>>3] >> (7 - (x & 7))) & 1;
 
2317
            src_data += src->bytes_per_line;
 
2318
            dest_data += dest->bytes_per_line;
 
2319
        }
 
2320
    } else {
 
2321
        for (int y = 0; y < dest->height; y++) {
 
2322
            register uchar *p = dest_data;
 
2323
            for (int x = 0; x < dest->width; x++)
 
2324
                *p++ = (src_data[x>>3] >> (x & 7)) & 1;
 
2325
            src_data += src->bytes_per_line;
 
2326
            dest_data += dest->bytes_per_line;
 
2327
        }
 
2328
    }
 
2329
}
 
2330
 
 
2331
// first index source, second dest
 
2332
static const Image_Converter converter_map[QImage::Format_ARGB32_Premultiplied][QImage::Format_ARGB32_Premultiplied] =
 
2333
{
 
2334
    { 0,
 
2335
      swap_bit_order,
 
2336
      convert_Mono_to_Indexed8,
 
2337
      convert_Mono_to_X32,
 
2338
      convert_Mono_to_X32,
 
2339
      convert_Mono_to_X32
 
2340
    }, // Format_Mono
 
2341
 
 
2342
    { swap_bit_order,
 
2343
      0,
 
2344
      convert_Mono_to_Indexed8,
 
2345
      convert_Mono_to_X32,
 
2346
      convert_Mono_to_X32,
 
2347
      convert_Mono_to_X32
 
2348
    }, // Format_MonoLSB
 
2349
 
 
2350
    {
 
2351
        convert_X_to_Mono,
 
2352
        convert_X_to_Mono,
 
2353
        0,
 
2354
        convert_Indexed8_to_X32,
 
2355
        convert_Indexed8_to_X32,
 
2356
        convert_Indexed8_to_X32
 
2357
    }, // Format_Indexed8
 
2358
 
 
2359
    {
 
2360
        convert_X_to_Mono,
 
2361
        convert_X_to_Mono,
 
2362
        convert_RGB_to_Indexed8,
 
2363
        0,
 
2364
        mask_alpha_converter,
 
2365
        mask_alpha_converter
 
2366
    }, // Format_RGB32
 
2367
 
 
2368
    {
 
2369
        convert_X_to_Mono,
 
2370
        convert_X_to_Mono,
 
2371
        convert_ARGB_to_Indexed8,
 
2372
        mask_alpha_converter,
 
2373
        0,
 
2374
        convert_ARGB_to_ARGB_PM
 
2375
    }, // Format_ARGB32
 
2376
 
 
2377
    {
 
2378
        convert_ARGB_PM_to_Mono,
 
2379
        convert_ARGB_PM_to_Mono,
 
2380
        convert_ARGB_PM_to_Indexed8,
 
2381
        convert_ARGB_PM_to_RGB,
 
2382
        convert_ARGB_PM_to_ARGB,
 
2383
        0
 
2384
    }  // Format_ARGB32_Premultiplied
 
2385
};
 
2386
 
 
2387
/*!
 
2388
    Returns a copy of the image in the given \a format.
 
2389
 
 
2390
    The image conversion flags specified by \a flags control how the image
 
2391
    data is handled during the conversion process.
 
2392
*/
 
2393
QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) const
 
2394
{
 
2395
    if (!d || d->format == format)
 
2396
        return *this;
 
2397
 
 
2398
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
 
2399
    const Image_Converter *converterPtr = &converter_map[d->format - 1][format - 1];
 
2400
    Image_Converter converter = *converterPtr;
 
2401
#else
 
2402
    Image_Converter converter = converter_map[d->format - 1][format - 1];
 
2403
#endif
 
2404
    if (!converter)
 
2405
        return QImage();
 
2406
 
 
2407
    QImage image(d->width, d->height, format);
 
2408
    converter(image.d, d, flags);
 
2409
    return image;
 
2410
}
 
2411
 
 
2412
/*!
 
2413
    Returns a copy of the image converted to the given \a format, using
 
2414
    a color table specified by \a colorTable.
 
2415
 
 
2416
    The image conversion flags specified by \a flags control how the image
 
2417
    data is handled during the conversion process.
 
2418
*/
 
2419
QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const
 
2420
{
 
2421
    if (d->format == format)
 
2422
        return *this;
 
2423
 
 
2424
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
 
2425
    const Image_Converter *converterPtr = &converter_map[d->format - 1][format - 1];
 
2426
    Image_Converter converter = *converterPtr;
 
2427
#else
 
2428
    Image_Converter converter = converter_map[d->format - 1][format - 1];
 
2429
#endif
 
2430
 
 
2431
    if (!converter)
 
2432
        return QImage();
 
2433
 
 
2434
    QImage image(d->width, d->height, format);
 
2435
    if (image.d->depth <= 8)
 
2436
        image.d->colortable = colorTable;
 
2437
    converter(image.d, d, flags);
 
2438
    return image;
 
2439
}
 
2440
 
 
2441
#ifdef QT3_SUPPORT
 
2442
/*!
 
2443
    Converts the depth (bpp) of the image to \a depth and returns the
 
2444
    converted image. The original image is not changed.
 
2445
 
 
2446
    The \a depth argument must be 1, 8 or 32.
 
2447
 
 
2448
    Returns \c *this if \a depth is equal to the image depth, or a
 
2449
    \link isNull() null\endlink image if this image cannot be
 
2450
    converted.
 
2451
 
 
2452
    If the image needs to be modified to fit in a lower-resolution
 
2453
    result (e.g. converting from 32-bit to 8-bit), use the \a
 
2454
    flags to specify how you'd prefer this to happen.
 
2455
 
 
2456
    \sa Qt::ImageConversionFlags depth() isNull()
 
2457
*/
 
2458
 
 
2459
QImage QImage::convertDepth(int depth, Qt::ImageConversionFlags flags) const
 
2460
{
 
2461
    if (!d || d->depth == depth)
 
2462
        return *this;
 
2463
 
 
2464
    Format format = formatFor (depth, QImage::LittleEndian);
 
2465
    return convertToFormat(format, flags);
 
2466
}
 
2467
#endif
 
2468
 
 
2469
/*!
 
2470
    Returns true if (\a x, \a y) is a valid coordinate in the image;
 
2471
    otherwise returns false.
 
2472
 
 
2473
    \sa width() height() pixelIndex()
 
2474
*/
 
2475
 
 
2476
bool QImage::valid(int x, int y) const
 
2477
{
 
2478
    return d
 
2479
        && x >= 0 && x < d->width
 
2480
        && y >= 0 && y < d->height;
 
2481
}
 
2482
 
 
2483
/*!
 
2484
    Returns the pixel index at the given coordinates.
 
2485
 
 
2486
    If (\a x, \a y) is not \link valid() valid\endlink, or if the
 
2487
    image is not a paletted image (depth() \> 8), the results are
 
2488
    undefined.
 
2489
 
 
2490
    \sa valid() depth()
 
2491
*/
 
2492
 
 
2493
int QImage::pixelIndex(int x, int y) const
 
2494
{
 
2495
    if (!d || x < 0 || x >= d->width) {
 
2496
        qWarning("QImage::pixel: x=%d out of range", x);
 
2497
        return -12345;
 
2498
    }
 
2499
    const uchar * s = scanLine(y);
 
2500
    switch(d->format) {
 
2501
    case Format_Mono:
 
2502
        return (*(s + (x >> 3)) >> (7- (x & 7))) & 1;
 
2503
    case Format_MonoLSB:
 
2504
        return (*(s + (x >> 3)) >> (x & 7)) & 1;
 
2505
    case Format_Indexed8:
 
2506
        return (int)s[x];
 
2507
    default:
 
2508
        qWarning("QImage::pixelIndex: Not applicable for %d-bpp images (no palette)", d->depth);
 
2509
    }
 
2510
    return 0;
 
2511
}
 
2512
 
 
2513
 
 
2514
/*!
 
2515
    Returns the color of the pixel at the coordinates (\a x, \a y).
 
2516
 
 
2517
    If (\a x, \a y) is not \link valid() on the image\endlink, the
 
2518
    results are undefined.
 
2519
 
 
2520
    \sa setPixel() qRed() qGreen() qBlue() valid()
 
2521
*/
 
2522
 
 
2523
QRgb QImage::pixel(int x, int y) const
 
2524
{
 
2525
    if (!d || x < 0 || x >= d->width) {
 
2526
        qWarning("QImage::pixel: x=%d out of range", x);
 
2527
        return 12345;
 
2528
    }
 
2529
    const uchar * s = scanLine(y);
 
2530
    switch(d->format) {
 
2531
    case Format_Mono:
 
2532
        return d->colortable.at((*(s + (x >> 3)) >> (7- (x & 7))) & 1);
 
2533
    case Format_MonoLSB:
 
2534
        return d->colortable.at((*(s + (x >> 3)) >> (x & 7)) & 1);
 
2535
    case Format_Indexed8:
 
2536
        return d->colortable.at((int)s[x]);
 
2537
    default:
 
2538
        return ((QRgb*)s)[x];
 
2539
    }
 
2540
}
 
2541
 
 
2542
 
 
2543
/*!
 
2544
    Sets the pixel index or color at the coordinates (\a x, \a y) to
 
2545
    \a index_or_rgb.
 
2546
 
 
2547
    If (\a x, \a y) is not \link valid() valid\endlink, the result is
 
2548
    undefined.
 
2549
 
 
2550
    If the image is a paletted image (depth() \<= 8) and \a
 
2551
    index_or_rgb \>= numColors(), the result is undefined.
 
2552
 
 
2553
    \sa pixelIndex() pixel() qRgb() qRgba() valid()
 
2554
*/
 
2555
 
 
2556
void QImage::setPixel(int x, int y, uint index_or_rgb)
 
2557
{
 
2558
    if (!d || x < 0 || x >= width()) {
 
2559
        qWarning("QImage::setPixel: x=%d out of range", x);
 
2560
        return;
 
2561
    }
 
2562
    detach();
 
2563
    if (d->depth == 1) {
 
2564
        uchar * s = scanLine(y);
 
2565
        if (index_or_rgb > 1) {
 
2566
            qWarning("QImage::setPixel: index=%d out of range", index_or_rgb);
 
2567
        } else if (format() == Format_MonoLSB) {
 
2568
            if (index_or_rgb==0)
 
2569
                *(s + (x >> 3)) &= ~(1 << (x & 7));
 
2570
            else
 
2571
                *(s + (x >> 3)) |= (1 << (x & 7));
 
2572
        } else {
 
2573
            if (index_or_rgb==0)
 
2574
                *(s + (x >> 3)) &= ~(1 << (7-(x & 7)));
 
2575
            else
 
2576
                *(s + (x >> 3)) |= (1 << (7-(x & 7)));
 
2577
        }
 
2578
    } else if (depth() == 8) {
 
2579
        if (index_or_rgb > (uint)d->colortable.size()) {
 
2580
            qWarning("QImage::setPixel: index=%d out of range", index_or_rgb);
 
2581
            return;
 
2582
        }
 
2583
        uchar * s = scanLine(y);
 
2584
        s[x] = index_or_rgb;
 
2585
    } else if (depth() == 32) {
 
2586
        QRgb * s = (QRgb*)scanLine(y);
 
2587
        s[x] = index_or_rgb;
 
2588
    }
 
2589
}
 
2590
 
 
2591
#ifdef QT3_SUPPORT
 
2592
/*!
 
2593
    Converts the bit order of the image to \a bitOrder and returns the
 
2594
    converted image. The original image is not changed.
 
2595
 
 
2596
    Returns \c *this if the \a bitOrder is equal to the image bit
 
2597
    order, or a \link isNull() null\endlink image if this image cannot
 
2598
    be converted.
 
2599
 
 
2600
    \sa bitOrder() systemBitOrder() isNull()
 
2601
*/
 
2602
 
 
2603
QImage QImage::convertBitOrder(Endian bitOrder) const
 
2604
{
 
2605
    if (!d || isNull() || d->depth != 1 || !(bitOrder == BigEndian || bitOrder == LittleEndian))
 
2606
        return QImage();
 
2607
 
 
2608
    if ((d->format == Format_Mono && bitOrder == BigEndian)
 
2609
        || (d->format == Format_MonoLSB && bitOrder == LittleEndian))
 
2610
        return *this;
 
2611
 
 
2612
    QImage image(d->width, d->height, d->format == Format_Mono ? Format_MonoLSB : Format_Mono);
 
2613
 
 
2614
    const uchar *data = d->data;
 
2615
    const uchar *end = data + d->nbytes;
 
2616
    uchar *ndata = image.d->data;
 
2617
    while (data < end)
 
2618
        *ndata++ = bitflip[*data++];
 
2619
 
 
2620
    image.setDotsPerMeterX(dotsPerMeterX());
 
2621
    image.setDotsPerMeterY(dotsPerMeterY());
 
2622
 
 
2623
    image.d->colortable = d->colortable;
 
2624
    return image;
 
2625
}
 
2626
#endif
 
2627
/*!
 
2628
    Returns true if all the colors in the image are shades of gray
 
2629
    (i.e. their red, green and blue components are equal); otherwise
 
2630
    returns false.
 
2631
 
 
2632
    This function is slow for large 32-bit images.
 
2633
 
 
2634
    \sa isGrayscale()
 
2635
*/
 
2636
bool QImage::allGray() const
 
2637
{
 
2638
    if (!d)
 
2639
        return true;
 
2640
 
 
2641
    if (depth()==32) {
 
2642
        int p = width()*height();
 
2643
        QRgb* b = (QRgb*)bits();
 
2644
        while (p--)
 
2645
            if (!qIsGray(*b++))
 
2646
                return false;
 
2647
    } else {
 
2648
        if (d->colortable.isEmpty())
 
2649
            return true;
 
2650
        for (int i = 0; i < numColors(); i++)
 
2651
            if (!qIsGray(d->colortable.at(i)))
 
2652
                return false;
 
2653
    }
 
2654
    return true;
 
2655
}
 
2656
 
 
2657
/*!
 
2658
    For 32-bit images, this function is equivalent to allGray().
 
2659
 
 
2660
    For 8-bpp images, this function returns true if color(i) is
 
2661
    QRgb(i,i,i) for all indexes of the color table; otherwise returns
 
2662
    false.
 
2663
 
 
2664
    \sa allGray() depth()
 
2665
*/
 
2666
bool QImage::isGrayscale() const
 
2667
{
 
2668
    if (!d)
 
2669
        return false;
 
2670
 
 
2671
    switch (depth()) {
 
2672
    case 32:
 
2673
        return allGray();
 
2674
    case 8: {
 
2675
        for (int i = 0; i < numColors(); i++)
 
2676
            if (d->colortable.at(i) != qRgb(i,i,i))
 
2677
                return false;
 
2678
        return true;
 
2679
        }
 
2680
    }
 
2681
    return false;
 
2682
}
 
2683
 
 
2684
 
 
2685
/*!
 
2686
    \fn QImage QImage::smoothScale(int width, int height, Qt::AspectRatioMode mode) const
 
2687
 
 
2688
    Use scale(\a width, \a height, \a mode, Qt::SmoothTransformation) instead.
 
2689
*/
 
2690
 
 
2691
/*!
 
2692
    \fn QImage QImage::smoothScale(const QSize &size, Qt::AspectRatioMode mode) const
 
2693
    \overload
 
2694
 
 
2695
    Use scale(\a size, \a mode, Qt::SmoothTransformation) instead.
 
2696
*/
 
2697
 
 
2698
/*!
 
2699
    \fn QImage QImage::scaled(int w, int h, Qt::AspectRatioMode aspectRatioMode,
 
2700
                             Qt::TransformationMode transformMode) const
 
2701
 
 
2702
    Returns a copy of the image scaled to a rectangle of width \a w
 
2703
    and height \a h according to \a aspectRatioMode and \a transformMode.
 
2704
 
 
2705
    \list
 
2706
    \i If \a aspectRatioMode is \c Qt::IgnoreAspectRatio, the image
 
2707
       is scaled to (\a w, \a h).
 
2708
    \i If \a aspectRatioMode is \c Qt::KeepAspectRatio, the image is
 
2709
       scaled to a rectangle as large as possible inside (\a w, \a
 
2710
       h), preserving the aspect ratio.
 
2711
    \i If \a aspectRatioMode is \c Qt::KeepAspectRatioByExpanding,
 
2712
       the image is scaled to a rectangle as small as possible
 
2713
       outside (\a w, \a h), preserving the aspect ratio.
 
2714
    \endlist
 
2715
 
 
2716
    If either the width \a w or the height \a h is zero or negative,
 
2717
    this function returns a \l{isNull()}{null} image.
 
2718
 
 
2719
    \sa scaledToWidth(), scaledToHeight(), transformed()
 
2720
*/
 
2721
 
 
2722
/*!
 
2723
    \fn QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectMode, Qt::TransformationMode transformMode) const
 
2724
 
 
2725
    \overload
 
2726
 
 
2727
    Scales the image to the given \a size, using the aspect ratio and
 
2728
    transformation modes specified by \a aspectMode and \a transformMode.
 
2729
*/
 
2730
#ifndef QT_NO_IMAGE_TRANSFORMATION
 
2731
QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
 
2732
{
 
2733
    if (!d) {
 
2734
        qWarning("QImage::scaled: Image is a null image");
 
2735
        return QImage();
 
2736
    }
 
2737
    if (s.isEmpty())
 
2738
        return QImage();
 
2739
 
 
2740
    QSize newSize = size();
 
2741
    newSize.scale(s, aspectMode);
 
2742
    if (newSize == size())
 
2743
        return copy();
 
2744
 
 
2745
    QImage img;
 
2746
    QMatrix wm;
 
2747
    wm.scale((double)newSize.width() / width(), (double)newSize.height() / height());
 
2748
    img = transformed(wm, mode);
 
2749
    return img;
 
2750
}
 
2751
#endif
 
2752
 
 
2753
/*!
 
2754
    Returns a scaled copy of the image with a width of \a w pixels using
 
2755
    the specified transformation \a mode. This function automatically
 
2756
    calculates the height of the image so that its aspect ratio is preserved.
 
2757
 
 
2758
    If \a w is 0 or negative a \link isNull() null\endlink image is
 
2759
    returned.
 
2760
 
 
2761
    \sa scaled(), scaledToHeight(), transformed()
 
2762
*/
 
2763
#ifndef QT_NO_IMAGE_TRANSFORMATION
 
2764
QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const
 
2765
{
 
2766
    if (!d) {
 
2767
        qWarning("QImage::scaleWidth: Image is a null image");
 
2768
        return QImage();
 
2769
    }
 
2770
    if (w <= 0)
 
2771
        return QImage();
 
2772
 
 
2773
    QMatrix wm;
 
2774
    double factor = (double) w / width();
 
2775
    wm.scale(factor, factor);
 
2776
    return transformed(wm, mode);
 
2777
}
 
2778
#endif
 
2779
 
 
2780
/*!
 
2781
    Returns a scaled copy of the image with a height of \a h pixels
 
2782
    using a transformation specified by \a mode.
 
2783
    This function automatically calculates the width of the image so that
 
2784
    the ratio of the image is preserved.
 
2785
 
 
2786
    If \a h is 0 or negative a \link isNull() null\endlink image is
 
2787
    returned.
 
2788
 
 
2789
    \sa scaled(), scaledToWidth(), transformed()
 
2790
*/
 
2791
#ifndef QT_NO_IMAGE_TRANSFORMATION
 
2792
QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const
 
2793
{
 
2794
    if (!d) {
 
2795
        qWarning("QImage::scaleHeight: Image is a null image");
 
2796
        return QImage();
 
2797
    }
 
2798
    if (h <= 0)
 
2799
        return QImage();
 
2800
 
 
2801
    QMatrix wm;
 
2802
    double factor = (double) h / height();
 
2803
    wm.scale(factor, factor);
 
2804
    return transformed(wm, mode);
 
2805
}
 
2806
#endif
 
2807
 
 
2808
 
 
2809
/*!
 
2810
    Returns the actual matrix used for transforming a image with \a w
 
2811
    width and \a h height and matrix \a matrix.
 
2812
 
 
2813
    When transforming a image with transformed(), the transformation matrix
 
2814
    is internally adjusted to compensate for unwanted translation,
 
2815
    i.e. transformed() returns the smallest image containing all
 
2816
    transformed points of the original image.
 
2817
 
 
2818
    This function returns the modified matrix, which maps points
 
2819
    correctly from the original image into the new image.
 
2820
 
 
2821
    \sa transformed(), QMatrix
 
2822
*/
 
2823
#ifndef QT_NO_PIXMAP_TRANSFORMATION
 
2824
QMatrix QImage::trueMatrix(const QMatrix &matrix, int w, int h)
 
2825
{
 
2826
    const qreal dt = qreal(0.);
 
2827
    qreal x1,y1, x2,y2, x3,y3, x4,y4;                // get corners
 
2828
    qreal xx = qreal(w);
 
2829
    qreal yy = qreal(h);
 
2830
 
 
2831
    QMatrix mat(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0.);
 
2832
 
 
2833
    mat.map(dt, dt, &x1, &y1);
 
2834
    mat.map(xx, dt, &x2, &y2);
 
2835
    mat.map(xx, yy, &x3, &y3);
 
2836
    mat.map(dt, yy, &x4, &y4);
 
2837
 
 
2838
    qreal ymin = y1;                                // lowest y value
 
2839
    if (y2 < ymin) ymin = y2;
 
2840
    if (y3 < ymin) ymin = y3;
 
2841
    if (y4 < ymin) ymin = y4;
 
2842
    qreal xmin = x1;                                // lowest x value
 
2843
    if (x2 < xmin) xmin = x2;
 
2844
    if (x3 < xmin) xmin = x3;
 
2845
    if (x4 < xmin) xmin = x4;
 
2846
 
 
2847
    qreal ymax = y1;                                // lowest y value
 
2848
    if (y2 > ymax) ymax = y2;
 
2849
    if (y3 > ymax) ymax = y3;
 
2850
    if (y4 > ymax) ymax = y4;
 
2851
    qreal xmax = x1;                                // lowest x value
 
2852
    if (x2 > xmax) xmax = x2;
 
2853
    if (x3 > xmax) xmax = x3;
 
2854
    if (x4 > xmax) xmax = x4;
 
2855
 
 
2856
    if (xmax-xmin > 1.0)
 
2857
        xmin -= xmin/(xmax-xmin);
 
2858
    if (ymax-ymin > 1.0)
 
2859
        ymin -= ymin/(ymax-ymin);
 
2860
 
 
2861
    mat.setMatrix(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), -xmin, -ymin);
 
2862
    return mat;
 
2863
}
 
2864
#endif // QT_NO_WMATRIX
 
2865
 
 
2866
/*!
 
2867
    Returns a copy of the image that is transformed with the transformation
 
2868
    matrix specified by \a matrix and using the transformation mode specified
 
2869
    by \a mode.
 
2870
 
 
2871
    The transformation \a matrix is internally adjusted to compensate
 
2872
    for unwanted translation; i.e. the image produced is the smallest image
 
2873
    that contains all the transformed points of the original image.
 
2874
 
 
2875
    \sa scaled(), QPixmap::transformed(), QPixmap::trueMatrix(), QMatrix
 
2876
*/
 
2877
#ifndef QT_NO_IMAGE_TRANSFORMATION
 
2878
QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
 
2879
{
 
2880
    if (!d)
 
2881
        return QImage();
 
2882
 
 
2883
    // source image data
 
2884
    int ws = width();
 
2885
    int hs = height();
 
2886
 
 
2887
    // target image data
 
2888
    int wd;
 
2889
    int hd;
 
2890
 
 
2891
    // compute size of target image
 
2892
    QMatrix mat = trueMatrix(matrix, ws, hs);
 
2893
    bool complex_xform = false;
 
2894
    if (mat.m12() == 0.0F && mat.m21() == 0.0F) {
 
2895
        if (mat.m11() == 1.0F && mat.m22() == 1.0F) // identity matrix
 
2896
            return *this;
 
2897
        hd = int(qAbs(mat.m22()) * hs + 0.9999);
 
2898
        wd = int(qAbs(mat.m11()) * ws + 0.9999);
 
2899
        hd = qAbs(hd);
 
2900
        wd = qAbs(wd);
 
2901
    } else {                                        // rotation or shearing
 
2902
        QPolygonF a(QRectF(0, 0, ws, hs));
 
2903
        a = mat.map(a);
 
2904
        QRectF r = a.boundingRect().normalized();
 
2905
        wd = int(r.width() + 0.9999);
 
2906
        hd = int(r.height() + 0.9999);
 
2907
        complex_xform = true;
 
2908
    }
 
2909
 
 
2910
    if (wd == 0 || hd == 0)
 
2911
        return QImage();
 
2912
 
 
2913
    int bpp = depth();
 
2914
 
 
2915
    int sbpl = bytesPerLine();
 
2916
    const uchar *sptr = bits();
 
2917
 
 
2918
    QImage dImage(wd, hd, d->format);
 
2919
    dImage.d->colortable = d->colortable;
 
2920
    dImage.d->has_alpha_clut = d->has_alpha_clut;
 
2921
    if (dImage.d->format == Format_RGB32 && complex_xform)
 
2922
        dImage.d->format = Format_ARGB32_Premultiplied;
 
2923
    dImage.d->dpmx = dotsPerMeterX();
 
2924
    dImage.d->dpmy = dotsPerMeterY();
 
2925
 
 
2926
    switch (bpp) {
 
2927
        // initizialize the data
 
2928
        case 1:
 
2929
            memset(dImage.bits(), 0, dImage.numBytes());
 
2930
            break;
 
2931
        case 8:
 
2932
            if (dImage.d->colortable.size() < 256) {
 
2933
                // colors are left in the color table, so pick that one as transparent
 
2934
                dImage.d->colortable.append(0x0);
 
2935
                memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.numBytes());
 
2936
            } else {
 
2937
                memset(dImage.bits(), 0, dImage.numBytes());
 
2938
            }
 
2939
            break;
 
2940
        case 32:
 
2941
            memset(dImage.bits(), 0x00, dImage.numBytes());
 
2942
            break;
 
2943
    }
 
2944
 
 
2945
    if (d->format == QImage::Format_RGB32 || d->format == QImage::Format_ARGB32_Premultiplied) {
 
2946
        QPainter p(&dImage);
 
2947
        if (mode == Qt::SmoothTransformation) {
 
2948
            p.setRenderHint(QPainter::Antialiasing);
 
2949
            p.setRenderHint(QPainter::SmoothPixmapTransform);
 
2950
        }
 
2951
        p.setMatrix(mat);
 
2952
        p.drawImage(QPoint(0, 0), *this);
 
2953
    } else {
 
2954
        bool invertible;
 
2955
        mat = mat.inverted(&invertible);                // invert matrix
 
2956
        if (!invertible)        // error, return null image
 
2957
            return QImage();
 
2958
 
 
2959
        // create target image (some of the code is from QImage::copy())
 
2960
        int type = format() == Format_Mono ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST;
 
2961
        int dbpl = dImage.bytesPerLine();
 
2962
        qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
 
2963
    }
 
2964
    return dImage;
 
2965
}
 
2966
#endif
 
2967
 
 
2968
/*!
 
2969
    Builds and returns a 1-bpp mask from the alpha buffer in this
 
2970
    image. Returns a \link isNull() null\endlink image if the image is
 
2971
    of format RGB32.
 
2972
 
 
2973
    See QPixmap::convertFromImage() for a description of the \a
 
2974
    flags argument.
 
2975
 
 
2976
    The returned image has little-endian bit order, which you can
 
2977
    convert to big-endian using convertToFormat().
 
2978
 
 
2979
    \sa createHeuristicMask()
 
2980
*/
 
2981
#ifndef QT_NO_IMAGE_DITHER_TO_1
 
2982
QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const
 
2983
{
 
2984
    if (!d || d->format == QImage::Format_RGB32)
 
2985
        return QImage();
 
2986
 
 
2987
    if (d->depth == 1) {
 
2988
        // A monochrome pixmap, with alpha channels on those two colors.
 
2989
        // Pretty unlikely, so use less efficient solution.
 
2990
        return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
 
2991
    }
 
2992
 
 
2993
    QImage mask(d->width, d->height, Format_MonoLSB);
 
2994
    dither_to_Mono(mask.d, d, flags, true);
 
2995
    return mask;
 
2996
}
 
2997
#endif
 
2998
 
 
2999
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
 
3000
/*!
 
3001
    Creates and returns a 1-bpp heuristic mask for this image. It
 
3002
    works by selecting a color from one of the corners, then chipping
 
3003
    away pixels of that color starting at all the edges.
 
3004
 
 
3005
    The four corners vote for which color is to be masked away. In
 
3006
    case of a draw (this generally means that this function is not
 
3007
    applicable to the image), the result is arbitrary.
 
3008
 
 
3009
    The returned image has little-endian bit order, which you can
 
3010
    convert to big-endian using convertToFormat().
 
3011
 
 
3012
    If \a clipTight is true (the default) the mask is just large enough to cover the
 
3013
    pixels; otherwise, the mask is larger than the data pixels.
 
3014
 
 
3015
    This function disregards the alpha buffer.
 
3016
 
 
3017
    \sa createAlphaMask()
 
3018
*/
 
3019
 
 
3020
QImage QImage::createHeuristicMask(bool clipTight) const
 
3021
{
 
3022
    if (!d)
 
3023
        return QImage();
 
3024
 
 
3025
    if (d->depth != 32) {
 
3026
        QImage img32 = convertToFormat(Format_RGB32);
 
3027
        return img32.createHeuristicMask(clipTight);
 
3028
    }
 
3029
 
 
3030
#define PIX(x,y)  (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
 
3031
 
 
3032
    int w = width();
 
3033
    int h = height();
 
3034
    QImage m(w, h, Format_MonoLSB);
 
3035
    m.setNumColors(2);
 
3036
    m.setColor(0, 0xffffff);
 
3037
    m.setColor(1, 0);
 
3038
    m.fill(0xff);
 
3039
 
 
3040
    QRgb background = PIX(0,0);
 
3041
    if (background != PIX(w-1,0) &&
 
3042
         background != PIX(0,h-1) &&
 
3043
         background != PIX(w-1,h-1)) {
 
3044
        background = PIX(w-1,0);
 
3045
        if (background != PIX(w-1,h-1) &&
 
3046
             background != PIX(0,h-1) &&
 
3047
             PIX(0,h-1) == PIX(w-1,h-1)) {
 
3048
            background = PIX(w-1,h-1);
 
3049
        }
 
3050
    }
 
3051
 
 
3052
    int x,y;
 
3053
    bool done = false;
 
3054
    uchar *ypp, *ypc, *ypn;
 
3055
    while(!done) {
 
3056
        done = true;
 
3057
        ypn = m.scanLine(0);
 
3058
        ypc = 0;
 
3059
        for (y = 0; y < h; y++) {
 
3060
            ypp = ypc;
 
3061
            ypc = ypn;
 
3062
            ypn = (y == h-1) ? 0 : m.scanLine(y+1);
 
3063
            QRgb *p = (QRgb *)scanLine(y);
 
3064
            for (x = 0; x < w; x++) {
 
3065
                // slowness here - it's possible to do six of these tests
 
3066
                // together in one go. oh well.
 
3067
                if ((x == 0 || y == 0 || x == w-1 || y == h-1 ||
 
3068
                       !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) ||
 
3069
                       !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) ||
 
3070
                       !(*(ypp + (x     >> 3)) & (1 << (x     & 7))) ||
 
3071
                       !(*(ypn + (x     >> 3)) & (1 << (x     & 7)))) &&
 
3072
                     (       (*(ypc + (x     >> 3)) & (1 << (x     & 7)))) &&
 
3073
                     ((*p & 0x00ffffff) == background)) {
 
3074
                    done = false;
 
3075
                    *(ypc + (x >> 3)) &= ~(1 << (x & 7));
 
3076
                }
 
3077
                p++;
 
3078
            }
 
3079
        }
 
3080
    }
 
3081
 
 
3082
    if (!clipTight) {
 
3083
        ypn = m.scanLine(0);
 
3084
        ypc = 0;
 
3085
        for (y = 0; y < h; y++) {
 
3086
            ypp = ypc;
 
3087
            ypc = ypn;
 
3088
            ypn = (y == h-1) ? 0 : m.scanLine(y+1);
 
3089
            QRgb *p = (QRgb *)scanLine(y);
 
3090
            for (x = 0; x < w; x++) {
 
3091
                if ((*p & 0x00ffffff) != background) {
 
3092
                    if (x > 0)
 
3093
                        *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7));
 
3094
                    if (x < w-1)
 
3095
                        *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7));
 
3096
                    if (y > 0)
 
3097
                        *(ypp + (x >> 3)) |= (1 << (x & 7));
 
3098
                    if (y < h-1)
 
3099
                        *(ypn + (x >> 3)) |= (1 << (x & 7));
 
3100
                }
 
3101
                p++;
 
3102
            }
 
3103
        }
 
3104
    }
 
3105
 
 
3106
#undef PIX
 
3107
 
 
3108
    return m;
 
3109
}
 
3110
#endif //QT_NO_IMAGE_HEURISTIC_MASK
 
3111
 
 
3112
#ifndef QT_NO_IMAGE_MIRROR
 
3113
/*
 
3114
  This code is contributed by Philipp Lang,
 
3115
  GeneriCom Software Germany (www.generi.com)
 
3116
  under the terms of the QPL, Version 1.0
 
3117
*/
 
3118
 
 
3119
/*!
 
3120
  \fn QImage QImage::mirror(bool horizontal, bool vertical) const
 
3121
 
 
3122
    \overload
 
3123
 
 
3124
    Returns a mirror of the image, mirrored in the horizontal and/or
 
3125
    the vertical direction depending on whether \a horizontal and \a
 
3126
    vertical are set to true or false. The original image is not
 
3127
    changed.
 
3128
 
 
3129
    \sa scaled()
 
3130
*/
 
3131
 
 
3132
/*!
 
3133
    \overload
 
3134
 
 
3135
    Returns a mirror of the image, mirrored in the horizontal and/or
 
3136
    the vertical direction depending on whether \a horizontal and \a
 
3137
    vertical are set to true or false. The original image is not
 
3138
    changed.
 
3139
 
 
3140
    \sa scaled()
 
3141
*/
 
3142
QImage QImage::mirrored(bool horizontal, bool vertical) const
 
3143
{
 
3144
    if (!d)
 
3145
        return QImage();
 
3146
 
 
3147
    if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
 
3148
        return QImage();
 
3149
 
 
3150
    int w = d->width;
 
3151
    int h = d->height;
 
3152
    // Create result image, copy colormap
 
3153
    QImage result(d->width, d->height, d->format);
 
3154
    result.d->colortable = d->colortable;
 
3155
 
 
3156
    if (depth() == 1)
 
3157
        w = (w+7)/8;
 
3158
    int dxi = horizontal ? -1 : 1;
 
3159
    int dxs = horizontal ? w-1 : 0;
 
3160
    int dyi = vertical ? -1 : 1;
 
3161
    int dy = vertical ? h-1: 0;
 
3162
 
 
3163
    // 1 bit, 8 bit
 
3164
    if (d->depth == 1 || d->depth == 8) {
 
3165
        for (int sy = 0; sy < h; sy++, dy += dyi) {
 
3166
            quint8* ssl = (quint8*)(d->data + sy*d->bytes_per_line);
 
3167
            quint8* dsl = (quint8*)(result.d->data + dy*result.d->bytes_per_line);
 
3168
            int dx = dxs;
 
3169
            for (int sx = 0; sx < w; sx++, dx += dxi)
 
3170
                dsl[dx] = ssl[sx];
 
3171
        }
 
3172
    }
 
3173
    // 32 bit
 
3174
    else if (depth() == 32) {
 
3175
        for (int sy = 0; sy < h; sy++, dy += dyi) {
 
3176
            quint32* ssl = (quint32*)(d->data + sy*d->bytes_per_line);
 
3177
            quint32* dsl = (quint32*)(result.d->data + dy*result.d->bytes_per_line);
 
3178
            int dx = dxs;
 
3179
            for (int sx = 0; sx < w; sx++, dx += dxi)
 
3180
                dsl[dx] = ssl[sx];
 
3181
        }
 
3182
    }
 
3183
 
 
3184
    // special handling of 1 bit images for horizontal mirroring
 
3185
    if (horizontal && depth() == 1) {
 
3186
        int shift = width() % 8;
 
3187
        for (int y = h-1; y >= 0; y--) {
 
3188
            quint8* a0 = (quint8*)(result.d->data + y*d->bytes_per_line);
 
3189
            // Swap bytes
 
3190
            quint8* a = a0+dxs;
 
3191
            while (a >= a0) {
 
3192
                *a = bitflip[*a];
 
3193
                a--;
 
3194
            }
 
3195
            // Shift bits if unaligned
 
3196
            if (shift != 0) {
 
3197
                a = a0+dxs;
 
3198
                quint8 c = 0;
 
3199
                if (format() == Format_MonoLSB) {
 
3200
                    while (a >= a0) {
 
3201
                        quint8 nc = *a << shift;
 
3202
                        *a = (*a >> (8-shift)) | c;
 
3203
                        --a;
 
3204
                        c = nc;
 
3205
                    }
 
3206
                } else {
 
3207
                    while (a >= a0) {
 
3208
                        quint8 nc = *a >> shift;
 
3209
                        *a = (*a << (8-shift)) | c;
 
3210
                        --a;
 
3211
                        c = nc;
 
3212
                    }
 
3213
                }
 
3214
            }
 
3215
        }
 
3216
    }
 
3217
 
 
3218
    return result;
 
3219
}
 
3220
#endif //QT_NO_IMAGE_MIRROR
 
3221
 
 
3222
/*!
 
3223
  \fn QImage QImage::swapRGB() const
 
3224
 
 
3225
    Returns a QImage in which the values of the red and blue
 
3226
    components of all pixels have been swapped, effectively converting
 
3227
    an RGB image to a BGR image. The original QImage is not changed.
 
3228
*/
 
3229
 
 
3230
/*!
 
3231
    Returns a QImage in which the values of the red and blue
 
3232
    components of all pixels have been swapped, effectively converting
 
3233
    an RGB image to a BGR image. The original QImage is not changed.
 
3234
*/
 
3235
QImage QImage::rgbSwapped() const
 
3236
{
 
3237
    QImage res = copy();
 
3238
    if (!isNull()) {
 
3239
        if (depth() == 32) {
 
3240
            for (int i=0; i < height(); i++) {
 
3241
                uint *p = (uint*)scanLine(i);
 
3242
                uint *q = (uint*)res.scanLine(i);
 
3243
                uint *end = p + width();
 
3244
                while (p < end) {
 
3245
                    *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) |
 
3246
                         (*p & 0xff00ff00);
 
3247
                    p++;
 
3248
                    q++;
 
3249
                }
 
3250
            }
 
3251
        } else
 
3252
        {
 
3253
            QVector<QRgb> newcolors;
 
3254
            for (int i = 0; i < res.d->colortable.size(); i++) {
 
3255
                QRgb c = res.d->colortable.at(i);
 
3256
                newcolors.append(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
 
3257
            }
 
3258
        }
 
3259
    }
 
3260
    return res;
 
3261
}
 
3262
 
 
3263
#ifndef QT_NO_IMAGEIO
 
3264
/*!
 
3265
    Loads an image from the file \a fileName. Returns true if the
 
3266
    image was successfully loaded; otherwise returns false.
 
3267
 
 
3268
    If \a format is specified, the loader attempts to read the image
 
3269
    using the specified format. If \a format is not specified (which
 
3270
    is the default), the loader probes the file for a header to
 
3271
    guess the file format.
 
3272
 
 
3273
    The QImageReader documentation lists the supported image formats and
 
3274
    explains how to add extra formats.
 
3275
 
 
3276
    The file name can either refer to an actual file on disk or to
 
3277
    one of the application's embedded resources. See the
 
3278
    \l{resources.html}{Resource System} overview for details on how
 
3279
    to embed images and other resource files in the application's
 
3280
    executable.
 
3281
 
 
3282
    \sa loadFromData(), save(), QImageReader::imageFormat(), QPixmap::load(),
 
3283
    QImageReader
 
3284
*/
 
3285
 
 
3286
bool QImage::load(const QString &fileName, const char* format)
 
3287
{
 
3288
    QImage image = QImageReader(fileName, format).read();
 
3289
    if (!image.isNull()) {
 
3290
        operator=(image);
 
3291
        return true;
 
3292
    }
 
3293
    return false;
 
3294
}
 
3295
 
 
3296
/*!
 
3297
    Loads an image from the first \a len bytes of binary data in \a
 
3298
    data. Returns true if the image was successfully loaded; otherwise
 
3299
    returns false.
 
3300
 
 
3301
    If \a format is specified, the loader attempts to read the image
 
3302
    using the specified format. If \a format is not specified (which
 
3303
    is the default), the loader probes the file for a header to
 
3304
    guess the file format.
 
3305
 
 
3306
    The QImageReader documentation lists the supported image formats and
 
3307
    explains how to add extra formats.
 
3308
 
 
3309
    \sa load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(),
 
3310
    QImageReader
 
3311
*/
 
3312
 
 
3313
bool QImage::loadFromData(const uchar *data, int len, const char *format)
 
3314
{
 
3315
    QImage image = fromData(data, len, format);
 
3316
    if (!image.isNull()) {
 
3317
        operator=(image);
 
3318
        return true;
 
3319
    }
 
3320
    return false;
 
3321
}
 
3322
 
 
3323
/*!
 
3324
  \fn bool QImage::loadFromData(const QByteArray &data, const char *format)
 
3325
 
 
3326
    \overload
 
3327
 
 
3328
    Loads an image from the QByteArray \a data.
 
3329
*/
 
3330
 
 
3331
/*!
 
3332
    \fn QImage QImage::fromData(const uchar *data, int size, const char *format)
 
3333
 
 
3334
    Constructs a QImage from the first \a size bytes of binary data
 
3335
    in \a data. If the loading of the image failed, this object is a \link
 
3336
    isNull() null\endlink image.
 
3337
 
 
3338
    If \a format is specified, the loader attempts to read the image
 
3339
    using the specified format. If \a format is not specified (which
 
3340
    is the default), the loader probes the file for a header to
 
3341
    guess the file format.
 
3342
 
 
3343
    The QImageReader documentation lists the supported image formats and
 
3344
    explains how to add extra formats.
 
3345
 
 
3346
    \sa load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(),
 
3347
    QImageReader
 
3348
*/
 
3349
QImage QImage::fromData(const uchar *data, int size, const char *format)
 
3350
{
 
3351
    QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
 
3352
    QBuffer b;
 
3353
    b.setData(a);
 
3354
    b.open(QIODevice::ReadOnly);
 
3355
    return QImageReader(&b, format).read();
 
3356
}
 
3357
 
 
3358
/*!
 
3359
    \fn QImage QImage::fromData(const QByteArray &data, const char *format)
 
3360
 
 
3361
    \overload
 
3362
 
 
3363
    Loads an image from the QByteArray \a data.
 
3364
*/
 
3365
 
 
3366
/*!
 
3367
    Saves the image to the file \a fileName, using the image file
 
3368
    format \a format and a quality factor of \a quality. \a quality
 
3369
    must be in the range 0 to 100 or -1. Specify 0 to obtain small
 
3370
    compressed files, 100 for large uncompressed files, and -1 (the
 
3371
    default) to use the default settings.
 
3372
 
 
3373
    Returns true if the image was successfully saved; otherwise
 
3374
    returns false.
 
3375
 
 
3376
    \sa load(), loadFromData(), QImageReader::imageFormat(), QPixmap::save(),
 
3377
    QImageReader
 
3378
*/
 
3379
bool QImage::save(const QString &fileName, const char *format, int quality) const
 
3380
{
 
3381
    if (isNull())
 
3382
        return false;
 
3383
    QImageWriter writer(fileName, format);
 
3384
    return d->doImageIO(this, &writer, quality);
 
3385
}
 
3386
 
 
3387
/*!
 
3388
    \overload
 
3389
 
 
3390
    This function writes a QImage to the QIODevice, \a device. This
 
3391
    can be used, for example, to save an image directly into a
 
3392
    QByteArray:
 
3393
 
 
3394
    \quotefromfile snippets/image/image.cpp
 
3395
    \skipto SAVE
 
3396
    \skipto QImage
 
3397
    \printuntil save
 
3398
*/
 
3399
 
 
3400
bool QImage::save(QIODevice* device, const char* format, int quality) const
 
3401
{
 
3402
    if (isNull())
 
3403
        return false;                                // nothing to save
 
3404
    QImageWriter writer(device, format);
 
3405
    return d->doImageIO(this, &writer, quality);
 
3406
}
 
3407
 
 
3408
/* \internal
 
3409
*/
 
3410
 
 
3411
bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const
 
3412
{
 
3413
    if (quality > 100  || quality < -1)
 
3414
        qWarning("QPixmap::save: quality out of range [-1,100]");
 
3415
    if (quality >= 0)
 
3416
        writer->setQuality(qMin(quality,100));
 
3417
    return writer->write(*image);
 
3418
}
 
3419
#endif //QT_NO_IMAGEIO
 
3420
 
 
3421
/*****************************************************************************
 
3422
  QImage stream functions
 
3423
 *****************************************************************************/
 
3424
#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO)
 
3425
/*!
 
3426
    \relates QImage
 
3427
 
 
3428
    Writes the image \a image to the stream \a s as a PNG image, or as a
 
3429
    BMP image if the stream's version is 1.
 
3430
 
 
3431
    Note that writing the stream to a file will not produce a valid image file.
 
3432
 
 
3433
    \sa QImage::save()
 
3434
    \link datastreamformat.html Format of the QDataStream operators \endlink
 
3435
*/
 
3436
 
 
3437
QDataStream &operator<<(QDataStream &s, const QImage &image)
 
3438
{
 
3439
    if (s.version() >= 5) {
 
3440
        if (image.isNull()) {
 
3441
            s << (qint32) 0; // null image marker
 
3442
            return s;
 
3443
        } else {
 
3444
            s << (qint32) 1;
 
3445
            // continue ...
 
3446
        }
 
3447
    }
 
3448
    QImageWriter writer(s.device(), s.version() == 1 ? "bmp" : "png");
 
3449
    writer.write(image);
 
3450
    return s;
 
3451
}
 
3452
 
 
3453
/*!
 
3454
    \relates QImage
 
3455
 
 
3456
    Reads an image from the stream \a s and stores it in \a image.
 
3457
 
 
3458
    \sa QImage::load()
 
3459
    \link datastreamformat.html Format of the QDataStream operators \endlink
 
3460
*/
 
3461
 
 
3462
QDataStream &operator>>(QDataStream &s, QImage &image)
 
3463
{
 
3464
    if (s.version() >= 5) {
 
3465
        qint32 nullMarker;
 
3466
        s >> nullMarker;
 
3467
        if (!nullMarker) {
 
3468
            image = QImage(); // null image
 
3469
            return s;
 
3470
        }
 
3471
    }
 
3472
    image = QImageReader(s.device(), 0).read();
 
3473
    return s;
 
3474
}
 
3475
#endif
 
3476
 
 
3477
 
 
3478
#ifdef QT3_SUPPORT
 
3479
/*!
 
3480
    Returns an image with depth \a d, using the \a palette_count
 
3481
    colors pointed to by \a palette. If \a d is 1 or 8, the returned
 
3482
    image will have its color table ordered the same as \a palette.
 
3483
 
 
3484
    If the image needs to be modified to fit in a lower-resolution
 
3485
    result (e.g. converting from 32-bit to 8-bit), use the \a
 
3486
    flags to specify how you'd prefer this to happen.
 
3487
 
 
3488
    Note: currently no closest-color search is made. If colors are
 
3489
    found that are not in the palette, the palette may not be used at
 
3490
    all. This result should not be considered valid because it may
 
3491
    change in future implementations.
 
3492
 
 
3493
    Currently inefficient for non-32-bit images.
 
3494
 
 
3495
    \sa Qt::ImageConversionFlags
 
3496
*/
 
3497
QImage QImage::convertDepthWithPalette(int d, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
 
3498
{
 
3499
    Format f = formatFor(d, QImage::LittleEndian);
 
3500
    QVector<QRgb> colortable;
 
3501
    for (int i = 0; i < palette_count; ++i)
 
3502
        colortable.append(palette[i]);
 
3503
    return convertToFormat(f, colortable, flags);
 
3504
}
 
3505
 
 
3506
/*!
 
3507
    \relates QImage
 
3508
 
 
3509
    Copies a block of pixels from \a src to \a dst. The pixels
 
3510
    copied from source (src) are converted according to
 
3511
    \a flags if it is incompatible with the destination
 
3512
    (\a dst).
 
3513
 
 
3514
    \a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy
 
3515
    is the top-left position in \a dst and \a sw, \a sh is the
 
3516
    size of the copied block.
 
3517
 
 
3518
    The copying is clipped if areas outside \a src or \a dst are
 
3519
    specified.
 
3520
 
 
3521
    If \a sw is -1, it is adjusted to src->width(). Similarly, if \a
 
3522
    sh is -1, it is adjusted to src->height().
 
3523
 
 
3524
    Currently inefficient for non 32-bit images.
 
3525
*/
 
3526
void bitBlt(QImage *dst, int dx, int dy, const QImage *src, int sx, int sy, int sw, int sh,
 
3527
            Qt::ImageConversionFlags flags)
 
3528
{
 
3529
    if (dst->isNull() || src->isNull())
 
3530
        return;
 
3531
    QPainter p(dst);
 
3532
    p.drawImage(QPoint(dx, dy), *src, QRect(sx, sy, sw, sh), flags);
 
3533
}
 
3534
#endif
 
3535
 
 
3536
/*!
 
3537
    Returns true if this image and image \a i have the same contents;
 
3538
    otherwise returns false. The comparison can be slow, unless there
 
3539
    is some obvious difference, such as different widths, in which
 
3540
    case the function will return quickly.
 
3541
 
 
3542
    \sa operator=()
 
3543
*/
 
3544
 
 
3545
bool QImage::operator==(const QImage & i) const
 
3546
{
 
3547
    // same object, or shared?
 
3548
    if (i.d == d)
 
3549
        return true;
 
3550
    if (!i.d || !d)
 
3551
        return false;
 
3552
 
 
3553
    // obviously different stuff?
 
3554
    if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format)
 
3555
        return false;
 
3556
    if (d->format != Format_RGB32) {
 
3557
        if (d->colortable != i.d->colortable)
 
3558
            return false;
 
3559
        if (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied) {
 
3560
            if (memcmp(bits(), i.bits(), d->nbytes))
 
3561
                return false;
 
3562
        } else {
 
3563
            int w = width();
 
3564
            int h = height();
 
3565
            for (int y=0; y<h; ++y) {
 
3566
                for (int x=0; x<w; ++x) {
 
3567
                    if (pixelIndex(x, y) != i.pixelIndex(x, y))
 
3568
                        return false;
 
3569
                }
 
3570
            }
 
3571
        }
 
3572
    } else {
 
3573
        //alpha channel undefined, so we must mask it out
 
3574
        for(int l = 0; l < d->height; l++) {
 
3575
            int w = d->width;
 
3576
            const uint *p1 = reinterpret_cast<const uint*>(scanLine(l));
 
3577
            const uint *p2 = reinterpret_cast<const uint*>(i.scanLine(l));
 
3578
            while (w--) {
 
3579
                if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
 
3580
                    return false;
 
3581
            }
 
3582
        }
 
3583
    }
 
3584
    return true;
 
3585
}
 
3586
 
 
3587
 
 
3588
/*!
 
3589
    Returns true if this image and image \a i have different contents;
 
3590
    otherwise returns false. The comparison can be slow, unless there
 
3591
    is some obvious difference, such as different widths, in which
 
3592
    case the function will return quickly.
 
3593
 
 
3594
    \sa operator=()
 
3595
*/
 
3596
 
 
3597
bool QImage::operator!=(const QImage & i) const
 
3598
{
 
3599
    return !(*this == i);
 
3600
}
 
3601
 
 
3602
 
 
3603
 
 
3604
 
 
3605
/*!
 
3606
    Returns the number of pixels that fit horizontally in a physical
 
3607
    meter. This and dotsPerMeterY() define the intended scale and
 
3608
    aspect ratio of the image.
 
3609
 
 
3610
    \sa setDotsPerMeterX()
 
3611
*/
 
3612
int QImage::dotsPerMeterX() const
 
3613
{
 
3614
    return d ? qRound(d->dpmx) : 0;
 
3615
}
 
3616
 
 
3617
/*!
 
3618
    Returns the number of pixels that fit vertically in a physical
 
3619
    meter. This and dotsPerMeterX() define the intended scale and
 
3620
    aspect ratio of the image.
 
3621
 
 
3622
    \sa setDotsPerMeterY()
 
3623
*/
 
3624
int QImage::dotsPerMeterY() const
 
3625
{
 
3626
    return d ? qRound(d->dpmy) : 0;
 
3627
}
 
3628
 
 
3629
/*!
 
3630
    Sets the value returned by dotsPerMeterX() to \a x.
 
3631
*/
 
3632
void QImage::setDotsPerMeterX(int x)
 
3633
{
 
3634
    if (!d || !x)
 
3635
        return;
 
3636
    detach();
 
3637
    d->dpmx = x;
 
3638
}
 
3639
 
 
3640
/*!
 
3641
    Sets the value returned by dotsPerMeterY() to \a y.
 
3642
*/
 
3643
void QImage::setDotsPerMeterY(int y)
 
3644
{
 
3645
    if (!d || !y)
 
3646
        return;
 
3647
    detach();
 
3648
    d->dpmy = y;
 
3649
}
 
3650
 
 
3651
/*!
 
3652
    \fn QPoint QImage::offset() const
 
3653
 
 
3654
    Returns the number of pixels by which the image is intended to be
 
3655
    offset by when positioning relative to other images.
 
3656
*/
 
3657
QPoint QImage::offset() const
 
3658
{
 
3659
    return d ? d->offset : QPoint();
 
3660
}
 
3661
 
 
3662
 
 
3663
/*!
 
3664
    Sets the value returned by offset() to \a p.
 
3665
*/
 
3666
void QImage::setOffset(const QPoint& p)
 
3667
{
 
3668
    if (!d)
 
3669
        return;
 
3670
    detach();
 
3671
    d->offset = p;
 
3672
}
 
3673
#ifndef QT_NO_IMAGE_TEXT
 
3674
 
 
3675
/*!
 
3676
    Returns the string recorded for the keyword \a key in language \a
 
3677
    lang, or in a default language if \a lang is 0.
 
3678
*/
 
3679
QString QImage::text(const char* key, const char* lang) const
 
3680
{
 
3681
    return d ? d->text_lang.value(QImageTextKeyLang(key,lang)) : QString();
 
3682
}
 
3683
 
 
3684
/*!
 
3685
    \overload
 
3686
 
 
3687
    Returns the string recorded for the keyword and language \a kl.
 
3688
*/
 
3689
QString QImage::text(const QImageTextKeyLang& kl) const
 
3690
{
 
3691
    return d ? d->text_lang.value(kl) : QString();
 
3692
}
 
3693
 
 
3694
/*!
 
3695
    Returns the language identifiers for which some texts are
 
3696
    recorded.
 
3697
 
 
3698
    Note that if you want to iterate over the list, you should iterate
 
3699
    over a copy, e.g.
 
3700
 
 
3701
    \code
 
3702
        QStringList list = myImage.textLanguages();
 
3703
        QStringList::Iterator it = list.begin();
 
3704
        while(it != list.end()) {
 
3705
            myProcessing(*it);
 
3706
            ++it;
 
3707
        }
 
3708
    \endcode
 
3709
 
 
3710
    \sa textList() text() setText() textKeys()
 
3711
*/
 
3712
QStringList QImage::textLanguages() const
 
3713
{
 
3714
    return d ? d->languages() : QStringList();
 
3715
}
 
3716
 
 
3717
/*!
 
3718
    Returns the keywords for which some texts are recorded.
 
3719
 
 
3720
    Note that if you want to iterate over the list, you should iterate
 
3721
    over a copy, e.g.
 
3722
 
 
3723
    \code
 
3724
        QStringList list = myImage.textKeys();
 
3725
        QStringList::Iterator it = list.begin();
 
3726
        while(it != list.end()) {
 
3727
            myProcessing(*it);
 
3728
            ++it;
 
3729
        }
 
3730
    \endcode
 
3731
 
 
3732
    \sa textList() text() setText() textLanguages()
 
3733
*/
 
3734
QStringList QImage::textKeys() const
 
3735
{
 
3736
    return d ? d->keys() : QStringList();
 
3737
}
 
3738
 
 
3739
/*!
 
3740
    Returns a list of QImageTextKeyLang objects that enumerate all the
 
3741
    texts key/language pairs set by setText() for this image.
 
3742
*/
 
3743
QList<QImageTextKeyLang> QImage::textList() const
 
3744
{
 
3745
    return d ? d->text_lang.keys() : QList<QImageTextKeyLang>();
 
3746
}
 
3747
 
 
3748
/*!
 
3749
    Records string \a s for the keyword \a key. The \a key should be
 
3750
    a portable keyword recognizable by other software - some suggested
 
3751
    values can be found in
 
3752
    \link http://www.libpng.org/pub/png/spec/PNG-Chunks.html#C.Anc-text
 
3753
    the PNG specification\endlink. \a s can be any text. \a lang should
 
3754
    specify the language code (see
 
3755
    \link ftp://ftp.isi.edu/in-notes/1766 RFC 1766\endlink) or 0.
 
3756
*/
 
3757
void QImage::setText(const char* key, const char* lang, const QString& s)
 
3758
{
 
3759
    if (!d)
 
3760
        return;
 
3761
    detach();
 
3762
    QImageTextKeyLang x(key, lang);
 
3763
    d->text_lang.insert(x, s);
 
3764
}
 
3765
 
 
3766
#endif // QT_NO_IMAGE_TEXT
 
3767
 
 
3768
/*
 
3769
    Sets the image bits to the \a pixmap contents and returns a
 
3770
    reference to the image.
 
3771
 
 
3772
    If the image shares data with other images, it will first
 
3773
    dereference the shared data.
 
3774
 
 
3775
    Makes a call to QPixmap::convertToImage().
 
3776
*/
 
3777
 
 
3778
/*! \fn QImage::Endian QImage::systemBitOrder()
 
3779
 
 
3780
    Determines the bit order of the display hardware. Returns
 
3781
    QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
 
3782
 
 
3783
    \sa systemByteOrder()
 
3784
*/
 
3785
 
 
3786
 
 
3787
/*!
 
3788
    \internal
 
3789
 
 
3790
    Used by QPainter to retreive a paint engine for the image.
 
3791
*/
 
3792
 
 
3793
QPaintEngine *QImage::paintEngine() const
 
3794
{
 
3795
    if (!d)
 
3796
        return 0;
 
3797
 
 
3798
#ifdef QT_RASTER_IMAGEENGINE
 
3799
    if (!d->paintEngine) {
 
3800
        d->paintEngine = new QRasterPaintEngine();
 
3801
    }
 
3802
#endif
 
3803
    return d->paintEngine;
 
3804
}
 
3805
 
 
3806
 
 
3807
/*!
 
3808
    Returns the size for the specified \a metric on the device.
 
3809
*/
 
3810
int QImage::metric(PaintDeviceMetric metric) const
 
3811
{
 
3812
    if (!d)
 
3813
        return 0;
 
3814
 
 
3815
    switch (metric) {
 
3816
    case PdmWidth:
 
3817
        return d->width;
 
3818
        break;
 
3819
 
 
3820
    case PdmHeight:
 
3821
        return d->height;
 
3822
        break;
 
3823
 
 
3824
    case PdmWidthMM:
 
3825
        return qRound(d->width * 1000 / d->dpmx);
 
3826
        break;
 
3827
 
 
3828
    case PdmHeightMM:
 
3829
        return qRound(d->height * 1000 / d->dpmy);
 
3830
        break;
 
3831
 
 
3832
    case PdmNumColors:
 
3833
        return d->colortable.size();
 
3834
        break;
 
3835
 
 
3836
    case PdmDepth:
 
3837
        return d->depth;
 
3838
        break;
 
3839
 
 
3840
    case PdmDpiX:
 
3841
        return (int)(d->dpmx * 0.0254);
 
3842
        break;
 
3843
 
 
3844
    case PdmDpiY:
 
3845
        return (int)(d->dpmy * 0.0254);
 
3846
        break;
 
3847
 
 
3848
    case PdmPhysicalDpiX:
 
3849
        return (int)(d->dpmx * 0.0254);
 
3850
        break;
 
3851
 
 
3852
    case PdmPhysicalDpiY:
 
3853
        return (int)(d->dpmy * 0.0254);
 
3854
        break;
 
3855
 
 
3856
    default:
 
3857
        qWarning("QImage::metric(), Unhandled metric type: %d\n", metric);
 
3858
        break;
 
3859
    }
 
3860
    return 0;
 
3861
}
 
3862
 
 
3863
 
 
3864
 
 
3865
/*****************************************************************************
 
3866
  QPixmap (and QImage) helper functions
 
3867
 *****************************************************************************/
 
3868
/*
 
3869
  This internal function contains the common (i.e. platform independent) code
 
3870
  to do a transformation of pixel data. It is used by QPixmap::transform() and by
 
3871
  QImage::transform().
 
3872
 
 
3873
  \a trueMat is the true transformation matrix (see QPixmap::trueMatrix()) and
 
3874
  \a xoffset is an offset to the matrix.
 
3875
 
 
3876
  \a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a
 
3877
  depth specifies the colordepth of the data.
 
3878
 
 
3879
  \a dptr is a pointer to the destination data, \a dbpl specifies the bits per
 
3880
  line for the destination data, \a p_inc is the offset that we advance for
 
3881
  every scanline and \a dHeight is the height of the destination image.
 
3882
 
 
3883
  \a sprt is the pointer to the source data, \a sbpl specifies the bits per
 
3884
  line of the source data, \a sWidth and \a sHeight are the width and height of
 
3885
  the source data.
 
3886
*/
 
3887
 
 
3888
#ifndef QT_NO_PIXMAP_TRANSFORMATION
 
3889
#undef IWX_MSB
 
3890
#define IWX_MSB(b)        if (trigx < maxws && trigy < maxhs) {                              \
 
3891
                            if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) &                      \
 
3892
                                 (1 << (7-((trigx>>12)&7))))                              \
 
3893
                                *dptr |= b;                                              \
 
3894
                        }                                                              \
 
3895
                        trigx += m11;                                                      \
 
3896
                        trigy += m12;
 
3897
        // END OF MACRO
 
3898
#undef IWX_LSB
 
3899
#define IWX_LSB(b)        if (trigx < maxws && trigy < maxhs) {                              \
 
3900
                            if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) &                      \
 
3901
                                 (1 << ((trigx>>12)&7)))                              \
 
3902
                                *dptr |= b;                                              \
 
3903
                        }                                                              \
 
3904
                        trigx += m11;                                                      \
 
3905
                        trigy += m12;
 
3906
        // END OF MACRO
 
3907
#undef IWX_PIX
 
3908
#define IWX_PIX(b)        if (trigx < maxws && trigy < maxhs) {                              \
 
3909
                            if ((*(sptr+sbpl*(trigy>>12)+(trigx>>15)) &              \
 
3910
                                 (1 << (7-((trigx>>12)&7)))) == 0)                      \
 
3911
                                *dptr &= ~b;                                              \
 
3912
                        }                                                              \
 
3913
                        trigx += m11;                                                      \
 
3914
                        trigy += m12;
 
3915
        // END OF MACRO
 
3916
bool qt_xForm_helper(const QMatrix &trueMat, int xoffset, int type, int depth,
 
3917
                     uchar *dptr, int dbpl, int p_inc, int dHeight,
 
3918
                     const uchar *sptr, int sbpl, int sWidth, int sHeight)
 
3919
{
 
3920
    int m11 = qRound(trueMat.m11()*4096.0);
 
3921
    int m12 = qRound(trueMat.m12()*4096.0);
 
3922
    int m21 = qRound(trueMat.m21()*4096.0);
 
3923
    int m22 = qRound(trueMat.m22()*4096.0);
 
3924
    int dx  = qRound(trueMat.dx()*4096.0);
 
3925
    int dy  = qRound(trueMat.dy()*4096.0);
 
3926
 
 
3927
    int m21ydx = dx + (xoffset<<16);
 
3928
    int m22ydy = dy;
 
3929
    uint trigx;
 
3930
    uint trigy;
 
3931
    uint maxws = sWidth<<12;
 
3932
    uint maxhs = sHeight<<12;
 
3933
 
 
3934
    for (int y=0; y<dHeight; y++) {                // for each target scanline
 
3935
        trigx = m21ydx;
 
3936
        trigy = m22ydy;
 
3937
        uchar *maxp = dptr + dbpl;
 
3938
        if (depth != 1) {
 
3939
            switch (depth) {
 
3940
                case 8:                                // 8 bpp transform
 
3941
                while (dptr < maxp) {
 
3942
                    if (trigx < maxws && trigy < maxhs)
 
3943
                        *dptr = *(sptr+sbpl*(trigy>>12)+(trigx>>12));
 
3944
                    trigx += m11;
 
3945
                    trigy += m12;
 
3946
                    dptr++;
 
3947
                }
 
3948
                break;
 
3949
 
 
3950
                case 16:                        // 16 bpp transform
 
3951
                while (dptr < maxp) {
 
3952
                    if (trigx < maxws && trigy < maxhs)
 
3953
                        *((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>12) +
 
3954
                                                     ((trigx>>12)<<1)));
 
3955
                    trigx += m11;
 
3956
                    trigy += m12;
 
3957
                    dptr++;
 
3958
                    dptr++;
 
3959
                }
 
3960
                break;
 
3961
 
 
3962
                case 24:                        // 24 bpp transform
 
3963
                while (dptr < maxp) {
 
3964
                    if (trigx < maxws && trigy < maxhs) {
 
3965
                        const uchar *p2 = sptr+sbpl*(trigy>>12) + ((trigx>>12)*3);
 
3966
                        dptr[0] = p2[0];
 
3967
                        dptr[1] = p2[1];
 
3968
                        dptr[2] = p2[2];
 
3969
                    }
 
3970
                    trigx += m11;
 
3971
                    trigy += m12;
 
3972
                    dptr += 3;
 
3973
                }
 
3974
                break;
 
3975
 
 
3976
                case 32:                        // 32 bpp transform
 
3977
                while (dptr < maxp) {
 
3978
                    if (trigx < maxws && trigy < maxhs)
 
3979
                        *((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>12) +
 
3980
                                                   ((trigx>>12)<<2)));
 
3981
                    trigx += m11;
 
3982
                    trigy += m12;
 
3983
                    dptr += 4;
 
3984
                }
 
3985
                break;
 
3986
 
 
3987
                default: {
 
3988
                return false;
 
3989
                }
 
3990
            }
 
3991
        } else  {
 
3992
            switch (type) {
 
3993
                case QT_XFORM_TYPE_MSBFIRST:
 
3994
                    while (dptr < maxp) {
 
3995
                        IWX_MSB(128);
 
3996
                        IWX_MSB(64);
 
3997
                        IWX_MSB(32);
 
3998
                        IWX_MSB(16);
 
3999
                        IWX_MSB(8);
 
4000
                        IWX_MSB(4);
 
4001
                        IWX_MSB(2);
 
4002
                        IWX_MSB(1);
 
4003
                        dptr++;
 
4004
                    }
 
4005
                    break;
 
4006
                case QT_XFORM_TYPE_LSBFIRST:
 
4007
                    while (dptr < maxp) {
 
4008
                        IWX_LSB(1);
 
4009
                        IWX_LSB(2);
 
4010
                        IWX_LSB(4);
 
4011
                        IWX_LSB(8);
 
4012
                        IWX_LSB(16);
 
4013
                        IWX_LSB(32);
 
4014
                        IWX_LSB(64);
 
4015
                        IWX_LSB(128);
 
4016
                        dptr++;
 
4017
                    }
 
4018
                    break;
 
4019
#  if defined(Q_WS_WIN)
 
4020
                case QT_XFORM_TYPE_WINDOWSPIXMAP:
 
4021
                    while (dptr < maxp) {
 
4022
                        IWX_PIX(128);
 
4023
                        IWX_PIX(64);
 
4024
                        IWX_PIX(32);
 
4025
                        IWX_PIX(16);
 
4026
                        IWX_PIX(8);
 
4027
                        IWX_PIX(4);
 
4028
                        IWX_PIX(2);
 
4029
                        IWX_PIX(1);
 
4030
                        dptr++;
 
4031
                    }
 
4032
                    break;
 
4033
#  endif
 
4034
            }
 
4035
        }
 
4036
        m21ydx += m21;
 
4037
        m22ydy += m22;
 
4038
        dptr += p_inc;
 
4039
    }
 
4040
    return true;
 
4041
}
 
4042
#undef IWX_MSB
 
4043
#undef IWX_LSB
 
4044
#undef IWX_PIX
 
4045
#endif // QT_NO_PIXMAP_TRANSFORMATION
 
4046
 
 
4047
/*!
 
4048
    \fn QImage QImage::xForm(const QMatrix &matrix) const
 
4049
 
 
4050
    Use transform(\a matrix) instead.
 
4051
*/
 
4052
 
 
4053
/*!
 
4054
    Returns a number that uniquely identifies the contents of this
 
4055
    QImage object. This means that multiple QImage objects can have
 
4056
    the same serial number as long as they refer to the same contents.
 
4057
 
 
4058
    A null image always have a serial number of 0.
 
4059
 
 
4060
    An example of where this is useful is for caching QImages.
 
4061
*/
 
4062
 
 
4063
int QImage::serialNumber() const
 
4064
{
 
4065
    if (!d)
 
4066
        return 0;
 
4067
    else
 
4068
        return d->ser_no;
 
4069
}
 
4070
 
 
4071
/*!
 
4072
    Returns true if the image is detached; otherwise returns false.
 
4073
 
 
4074
    \sa detach()
 
4075
*/
 
4076
 
 
4077
bool QImage::isDetached() const
 
4078
{
 
4079
    return d && d->ref == 1;
 
4080
}
 
4081
 
 
4082
 
 
4083
/*!
 
4084
    Sets the alpha channel of this image to \a alphaChannel.
 
4085
 
 
4086
    If \a alphaChannel is an 8 bit grayscale image, the intensity
 
4087
    values are written into this buffer directly. Otherwise, \a
 
4088
    alphaChannel is converted to 32 bit and the intensity of the RGB
 
4089
    pixel values is used.
 
4090
 
 
4091
    The image will be converted to the format
 
4092
    Format_ARGB32_Premultiplied if the function succeeds.
 
4093
*/
 
4094
 
 
4095
void QImage::setAlphaChannel(const QImage &alphaChannel)
 
4096
{
 
4097
    if (!d)
 
4098
        return;
 
4099
 
 
4100
    int w = d->width;
 
4101
    int h = d->height;
 
4102
 
 
4103
    if (w != alphaChannel.d->width || h != alphaChannel.d->height) {
 
4104
        qWarning("QImage::setAlphaChannel(), "
 
4105
                 "alpha channel must have same dimensions as the target image.");
 
4106
        return;
 
4107
    }
 
4108
 
 
4109
    detach();
 
4110
 
 
4111
    *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
 
4112
 
 
4113
    // Slight optimization since alphachannels are returned as 8-bit grays.
 
4114
    if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
 
4115
        const uchar *src_data = alphaChannel.d->data;
 
4116
        const uchar *dest_data = d->data;
 
4117
        for (int y=0; y<h; ++y) {
 
4118
            const uchar *src = src_data;
 
4119
            QRgb *dest = (QRgb *)dest_data;
 
4120
            for (int x=0; x<w; ++x) {
 
4121
                int alpha = *src;
 
4122
                int destAlpha = qt_div_255(alpha * qAlpha(*dest));
 
4123
                *dest = ((destAlpha << 24)
 
4124
                         | (qt_div_255(qRed(*dest) * alpha) << 16)
 
4125
                         | (qt_div_255(qGreen(*dest) * alpha) << 8)
 
4126
                         | (qt_div_255(qBlue(*dest) * alpha)));
 
4127
                ++dest;
 
4128
                ++src;
 
4129
            }
 
4130
            src_data += alphaChannel.d->bytes_per_line;
 
4131
            dest_data += d->bytes_per_line;
 
4132
        }
 
4133
 
 
4134
    } else {
 
4135
        const QImage sourceImage = alphaChannel.convertToFormat(QImage::Format_RGB32);
 
4136
        const uchar *src_data = sourceImage.d->data;
 
4137
        const uchar *dest_data = d->data;
 
4138
        for (int y=0; y<h; ++y) {
 
4139
            const QRgb *src = (const QRgb *) src_data;
 
4140
            QRgb *dest = (QRgb *) dest_data;
 
4141
            for (int x=0; x<w; ++x) {
 
4142
                int alpha = qGray(*src);
 
4143
                int destAlpha = qt_div_255(alpha * qAlpha(*dest));
 
4144
                *dest = ((destAlpha << 24)
 
4145
                         | (qt_div_255(qRed(*dest) * alpha) << 16)
 
4146
                         | (qt_div_255(qGreen(*dest) * alpha) << 8)
 
4147
                         | (qt_div_255(qBlue(*dest) * alpha)));
 
4148
                ++dest;
 
4149
                ++src;
 
4150
            }
 
4151
            src_data += sourceImage.d->bytes_per_line;
 
4152
            dest_data += d->bytes_per_line;
 
4153
        }
 
4154
    }
 
4155
}
 
4156
 
 
4157
 
 
4158
/*!
 
4159
    Extracts the alpha channel from this image as an 8 bit gray scale
 
4160
    image and returns it.
 
4161
*/
 
4162
 
 
4163
QImage QImage::alphaChannel() const
 
4164
{
 
4165
    if (!d)
 
4166
        return QImage();
 
4167
 
 
4168
    int w = d->width;
 
4169
    int h = d->height;
 
4170
 
 
4171
    QImage image(w, h, Format_Indexed8);
 
4172
    image.setNumColors(256);
 
4173
 
 
4174
    // set up gray scale table.
 
4175
    for (int i=0; i<256; ++i)
 
4176
        image.setColor(i, qRgb(i, i, i));
 
4177
 
 
4178
    if (d->format == Format_Indexed8 && hasAlphaChannel()) {
 
4179
        const uchar *src_data = d->data;
 
4180
        uchar *dest_data = image.d->data;
 
4181
        for (int y=0; y<h; ++y) {
 
4182
            const QRgb *src = (const QRgb *) src_data;
 
4183
            uchar *dest = dest_data;
 
4184
            for (int x=0; x<w; ++x) {
 
4185
                *dest = qAlpha(d->colortable.at(*src));
 
4186
                ++dest;
 
4187
                ++src;
 
4188
            }
 
4189
            src_data += d->bytes_per_line;
 
4190
            dest_data += image.d->bytes_per_line;
 
4191
        }
 
4192
    } else if (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied) {
 
4193
        const uchar *src_data = d->data;
 
4194
        uchar *dest_data = image.d->data;
 
4195
        for (int y=0; y<h; ++y) {
 
4196
            const QRgb *src = (const QRgb *) src_data;
 
4197
            uchar *dest = dest_data;
 
4198
            for (int x=0; x<w; ++x) {
 
4199
                *dest = qAlpha(*src);
 
4200
                ++dest;
 
4201
                ++src;
 
4202
            }
 
4203
            src_data += d->bytes_per_line;
 
4204
            dest_data += image.d->bytes_per_line;
 
4205
        }
 
4206
    } else {
 
4207
        image.fill(255);
 
4208
    }
 
4209
 
 
4210
    return image;
 
4211
}
 
4212
 
 
4213
/*!
 
4214
    Returns true if the image has an alpha channel.
 
4215
*/
 
4216
bool QImage::hasAlphaChannel() const
 
4217
{
 
4218
    return d && (d->format == Format_ARGB32_Premultiplied
 
4219
                 || d->format == Format_ARGB32
 
4220
                 || (d->format == Format_Indexed8 && d->has_alpha_clut));
 
4221
}
 
4222
 
 
4223
 
 
4224
#ifdef QT3_SUPPORT
 
4225
#if defined(Q_WS_X11)
 
4226
#include <private/qt_x11_p.h>
 
4227
#endif
 
4228
 
 
4229
inline QImage::Endian QImage::systemBitOrder()
 
4230
{
 
4231
#if defined(Q_WS_X11)
 
4232
    return BitmapBitOrder(X11->display) == MSBFirst ? BigEndian : LittleEndian;
 
4233
#else
 
4234
    return BigEndian;
 
4235
#endif
 
4236
}
 
4237
#endif
 
4238
 
 
4239
/*!
 
4240
    \fn QImage QImage::copy(const QRect &rect, Qt::ImageConversionFlags flags) const
 
4241
    \compat
 
4242
 
 
4243
    Use copy() and convertToFormat() instead.
 
4244
*/
 
4245
 
 
4246
/*!
 
4247
    \fn QImage QImage::copy(int x, int y, int w, int h, Qt::ImageConversionFlags flags) const
 
4248
    \compat
 
4249
 
 
4250
    Use copy() and convertToFormat() instead.
 
4251
*/
 
4252
 
 
4253
/*!
 
4254
    \fn QImage QImage::scaleWidth(int w) const
 
4255
    \compat
 
4256
 
 
4257
    Use scaledToWidth() instead.
 
4258
*/
 
4259
 
 
4260
/*!
 
4261
    \fn QImage QImage::scaleHeight(int h) const
 
4262
    \compat
 
4263
 
 
4264
    Use scaledToHeight() instead.
 
4265
*/