1
/****************************************************************************
2
**
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtGui module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QIMAGE_H
43
#define QIMAGE_H
44
45
#include <QtGui/qtransform.h>
46
#include <QtGui/qpaintdevice.h>
47
#include <QtGui/qrgb.h>
48
#include <QtCore/qbytearray.h>
49
#include <QtCore/qrect.h>
50
#include <QtCore/qstring.h>
51
52
QT_BEGIN_HEADER
53
54
QT_BEGIN_NAMESPACE
55
56
QT_MODULE(Gui)
57
58
class QIODevice;
59
class QStringList;
60
class QMatrix;
61
class QTransform;
62
class QVariant;
63
template <class T> class QList;
64
template <class T> class QVector;
65
66
struct QImageData;
67
class QImageDataMisc; // internal
68
#ifndef QT_NO_IMAGE_TEXT
69
class Q_GUI_EXPORT QImageTextKeyLang {
70
public:
71
    QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
72
    QImageTextKeyLang() { }
73
74
    QByteArray key;
75
    QByteArray lang;
76
77
    bool operator< (const QImageTextKeyLang& other) const
78
        { return key < other.key || (key==other.key && lang < other.lang); }
79
    bool operator== (const QImageTextKeyLang& other) const
80
        { return key==other.key && lang==other.lang; }
81
    inline bool operator!= (const QImageTextKeyLang &other) const
82
        { return !operator==(other); }
83
};
84
#endif //QT_NO_IMAGE_TEXT
85
86
87
class Q_GUI_EXPORT QImage : public QPaintDevice
88
{
89
public:
90
    enum InvertMode { InvertRgb, InvertRgba };
91
    enum Format {
92
        Format_Invalid,
93
        Format_Mono,
94
        Format_MonoLSB,
95
        Format_Indexed8,
96
        Format_RGB32,
97
        Format_ARGB32,
98
        Format_ARGB32_Premultiplied,
99
        Format_RGB16,
100
        Format_ARGB8565_Premultiplied,
101
        Format_RGB666,
102
        Format_ARGB6666_Premultiplied,
103
        Format_RGB555,
104
        Format_ARGB8555_Premultiplied,
105
        Format_RGB888,
106
        Format_RGB444,
107
        Format_ARGB4444_Premultiplied,
108
#if 0
109
        // reserved for future use
110
        Format_RGB15,
111
        Format_Grayscale16,
112
        Format_Grayscale8,
113
        Format_Grayscale4,
114
        Format_Grayscale4LSB,
115
        Format_Grayscale2,
116
        Format_Grayscale2LSB
117
#endif
118
#ifndef qdoc
119
        NImageFormats
120
#endif
121
    };
122
123
    QImage();
124
    QImage(const QSize &size, Format format);
125
    QImage(int width, int height, Format format);
126
    QImage(uchar *data, int width, int height, Format format);
127
    QImage(const uchar *data, int width, int height, Format format);
128
    QImage(uchar *data, int width, int height, int bytesPerLine, Format format);
129
    QImage(const uchar *data, int width, int height, int bytesPerLine, Format format);
130
131
#ifndef QT_NO_IMAGEFORMAT_XPM
132
    explicit QImage(const char * const xpm[]);
133
#endif
134
    explicit QImage(const QString &fileName, const char *format = 0);
135
#ifndef QT_NO_CAST_FROM_ASCII
136
    explicit QImage(const char *fileName, const char *format = 0);
137
#endif
138
139
    QImage(const QImage &);
140
    ~QImage();
141
142
    QImage &operator=(const QImage &);
143
    bool isNull() const;
144
145
    int devType() const;
146
147
    bool operator==(const QImage &) const;
148
    bool operator!=(const QImage &) const;
149
    operator QVariant() const;
150
    void detach();
151
    bool isDetached() const;
152
153
    QImage copy(const QRect &rect = QRect()) const;
154
    inline QImage copy(int x, int y, int w, int h) const
155
        { return copy(QRect(x, y, w, h)); }
156
157
    Format format() const;
158
159
    QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
160
    QImage convertToFormat(Format f, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
161
162
    int width() const;
163
    int height() const;
164
    QSize size() const;
165
    QRect rect() const;
166
167
    int depth() const;
168
#ifdef QT_DEPRECATED
169
    QT_DEPRECATED int numColors() const;
170
#endif
171
    int colorCount() const;
172
173
    QRgb color(int i) const;
174
    void setColor(int i, QRgb c);
175
#ifdef QT_DEPRECATED
176
    QT_DEPRECATED void setNumColors(int);
177
#endif
178
    void setColorCount(int);
179
180
    bool allGray() const;
181
    bool isGrayscale() const;
182
183
    uchar *bits();
184
    const uchar *bits() const;
185
#ifdef QT_DEPRECATED
186
    QT_DEPRECATED int numBytes() const;
187
#endif
188
    int byteCount() const;
189
190
    uchar *scanLine(int);
191
    const uchar *scanLine(int) const;
192
    int bytesPerLine() const;
193
194
    bool valid(int x, int y) const;
195
    bool valid(const QPoint &pt) const;
196
197
    int pixelIndex(int x, int y) const;
198
    int pixelIndex(const QPoint &pt) const;
199
200
    QRgb pixel(int x, int y) const;
201
    QRgb pixel(const QPoint &pt) const;
202
203
    void setPixel(int x, int y, uint index_or_rgb);
204
    void setPixel(const QPoint &pt, uint index_or_rgb);
205
206
    QVector<QRgb> colorTable() const;
207
    void setColorTable(const QVector<QRgb> colors);
208
209
    void fill(uint pixel);
210
211
    bool hasAlphaChannel() const;
212
    void setAlphaChannel(const QImage &alphaChannel);
213
    QImage alphaChannel() const;
214
    QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
215
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
216
    QImage createHeuristicMask(bool clipTight = true) const;
217
#endif
218
    QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
219
220
    inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
221
                        Qt::TransformationMode mode = Qt::FastTransformation) const
222
        { return scaled(QSize(w, h), aspectMode, mode); }
223
    QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
224
                 Qt::TransformationMode mode = Qt::FastTransformation) const;
225
    QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
226
    QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
227
    QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
228
    static QMatrix trueMatrix(const QMatrix &, int w, int h);
229
    QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
230
    static QTransform trueMatrix(const QTransform &, int w, int h);
231
    QImage mirrored(bool horizontally = false, bool vertically = true) const;
232
    QImage rgbSwapped() const;
233
    void invertPixels(InvertMode = InvertRgb);
234
235
236
    bool load(QIODevice *device, const char* format);
237
    bool load(const QString &fileName, const char* format=0);
238
    bool loadFromData(const uchar *buf, int len, const char *format = 0);
239
    inline bool loadFromData(const QByteArray &data, const char* aformat=0)
240
        { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }
241
242
    bool save(const QString &fileName, const char* format=0, int quality=-1) const;
243
    bool save(QIODevice *device, const char* format=0, int quality=-1) const;
244
245
    static QImage fromData(const uchar *data, int size, const char *format = 0);
246
    inline static QImage fromData(const QByteArray &data, const char *format = 0)
247
        { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }
248
249
    int serialNumber() const;
250
    qint64 cacheKey() const;
251
252
    QPaintEngine *paintEngine() const;
253
254
    // Auxiliary data
255
    int dotsPerMeterX() const;
256
    int dotsPerMeterY() const;
257
    void setDotsPerMeterX(int);
258
    void setDotsPerMeterY(int);
259
    QPoint offset() const;
260
    void setOffset(const QPoint&);
261
#ifndef QT_NO_IMAGE_TEXT
262
    QStringList textKeys() const;
263
    QString text(const QString &key = QString()) const;
264
    void setText(const QString &key, const QString &value);
265
266
    // The following functions are obsolete as of 4.1
267
    QString text(const char* key, const char* lang=0) const;
268
    QList<QImageTextKeyLang> textList() const;
269
    QStringList textLanguages() const;
270
    QString text(const QImageTextKeyLang&) const;
271
    void setText(const char* key, const char* lang, const QString&);
272
#endif
273
274
#ifdef QT3_SUPPORT
275
    enum Endian { BigEndian, LittleEndian, IgnoreEndian };
276
    QT3_SUPPORT_CONSTRUCTOR QImage(int width, int height, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
277
    QT3_SUPPORT_CONSTRUCTOR QImage(const QSize&, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
278
    QT3_SUPPORT_CONSTRUCTOR QImage(uchar *data, int w, int h, int depth, const QRgb *colortable, int numColors, Endian bitOrder);
279
#ifdef Q_WS_QWS
280
    QT3_SUPPORT_CONSTRUCTOR QImage(uchar *data, int w, int h, int depth, int pbl, const QRgb *colortable, int numColors, Endian bitOrder);
281
#endif
282
    inline QT3_SUPPORT Endian bitOrder() const {
283
        Format f = format();
284
        return f == Format_Mono ? BigEndian : (f == Format_MonoLSB ? LittleEndian : IgnoreEndian);
285
    }
286
    QT3_SUPPORT QImage convertDepth(int, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
287
    QT3_SUPPORT QImage convertDepthWithPalette(int, QRgb* p, int pc, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
288
    QT3_SUPPORT QImage convertBitOrder(Endian) const;
289
    QT3_SUPPORT bool hasAlphaBuffer() const;
290
    QT3_SUPPORT void setAlphaBuffer(bool);
291
    QT3_SUPPORT uchar **jumpTable();
292
    QT3_SUPPORT const uchar * const *jumpTable() const;
293
    inline QT3_SUPPORT void reset() { *this = QImage(); }
294
    static inline QT3_SUPPORT Endian systemByteOrder()
295
        { return QSysInfo::ByteOrder == QSysInfo::BigEndian ? BigEndian : LittleEndian; }
296
    inline QT3_SUPPORT QImage swapRGB() const { return rgbSwapped(); }
297
    inline QT3_SUPPORT QImage mirror(bool horizontally = false, bool vertically = true) const
298
        { return mirrored(horizontally, vertically); }
299
    QT3_SUPPORT bool create(const QSize&, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
300
    QT3_SUPPORT bool create(int width, int height, int depth, int numColors=0, Endian bitOrder=IgnoreEndian);
301
    inline QT3_SUPPORT QImage xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
302
    inline QT3_SUPPORT QImage smoothScale(int w, int h, Qt::AspectRatioMode mode = Qt::IgnoreAspectRatio) const
303
        { return scaled(QSize(w, h), mode, Qt::SmoothTransformation); }
304
    inline QImage QT3_SUPPORT smoothScale(const QSize &s, Qt::AspectRatioMode mode = Qt::IgnoreAspectRatio) const
305
        { return scaled(s, mode, Qt::SmoothTransformation); }
306
    inline QT3_SUPPORT QImage scaleWidth(int w) const { return scaledToWidth(w); }
307
    inline QT3_SUPPORT QImage scaleHeight(int h) const { return scaledToHeight(h); }
308
    inline QT3_SUPPORT void invertPixels(bool invertAlpha) { invertAlpha ? invertPixels(InvertRgba) : invertPixels(InvertRgb); }
309
    inline QT3_SUPPORT QImage copy(int x, int y, int w, int h, Qt::ImageConversionFlags) const
310
        { return copy(QRect(x, y, w, h)); }
311
    inline QT3_SUPPORT QImage copy(const QRect &rect, Qt::ImageConversionFlags) const
312
        { return copy(rect); }
313
    static QT3_SUPPORT Endian systemBitOrder();
314
    inline QT3_SUPPORT_CONSTRUCTOR QImage(const QByteArray &data)
315
        { d = 0; *this = QImage::fromData(data); }
316
#endif
317
318
protected:
319
    virtual int metric(PaintDeviceMetric metric) const;
320
321
private:
322
    friend class QWSOnScreenSurface;
323
    QImageData *d;
324
325
    friend class QRasterPixmapData;
326
    friend class QPixmapCacheEntry;
327
    friend Q_GUI_EXPORT qint64 qt_image_id(const QImage &image);
328
    friend const QVector<QRgb> *qt_image_colortable(const QImage &image);
329
330
public:
331
    typedef QImageData * DataPtr;
332
    inline DataPtr &data_ptr() { return d; }
333
};
334
335
Q_DECLARE_SHARED(QImage)
336
Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);
337
338
// Inline functions...
339
340
Q_GUI_EXPORT_INLINE bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
341
Q_GUI_EXPORT_INLINE int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
342
Q_GUI_EXPORT_INLINE QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
343
Q_GUI_EXPORT_INLINE void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
344
345
// QImage stream functions
346
347
#if !defined(QT_NO_DATASTREAM)
348
Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
349
Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
350
#endif
351
352
#ifdef QT3_SUPPORT
353
Q_GUI_EXPORT QT3_SUPPORT void bitBlt(QImage* dst, int dx, int dy, const QImage* src,
354
                                     int sx=0, int sy=0, int sw=-1, int sh=-1, Qt::ImageConversionFlags flags = Qt::AutoColor);
355
#endif
356
357
QT_END_NAMESPACE
358
359
QT_END_HEADER
360
361
#endif // QIMAGE_H