e59aa2054900fdcd9f7c3fc925649c3d39ab95a8
[profile/ivi/qtbase.git] / src / gui / image / qimage.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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 #if QT_DEPRECATED_SINCE(5, 0)
53 #include <QtCore/qstringlist.h>
54 #endif
55
56 QT_BEGIN_HEADER
57
58 QT_BEGIN_NAMESPACE
59
60
61 class QIODevice;
62 class QStringList;
63 class QMatrix;
64 class QTransform;
65 class QVariant;
66 template <class T> class QList;
67 template <class T> class QVector;
68
69 struct QImageData;
70 class QImageDataMisc; // internal
71 #if QT_DEPRECATED_SINCE(5, 0)
72 class QImageTextKeyLang {
73 public:
74     QT_DEPRECATED QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
75     QT_DEPRECATED QImageTextKeyLang() { }
76
77     QByteArray key;
78     QByteArray lang;
79
80     bool operator< (const QImageTextKeyLang& other) const
81         { return key < other.key || (key==other.key && lang < other.lang); }
82     bool operator== (const QImageTextKeyLang& other) const
83         { return key==other.key && lang==other.lang; }
84     inline bool operator!= (const QImageTextKeyLang &other) const
85         { return !operator==(other); }
86 };
87 #endif
88
89 typedef void (*QImageCleanupFunction)(void*);
90
91 class Q_GUI_EXPORT QImage : public QPaintDevice
92 {
93 public:
94     enum InvertMode { InvertRgb, InvertRgba };
95     enum Format {
96         Format_Invalid,
97         Format_Mono,
98         Format_MonoLSB,
99         Format_Indexed8,
100         Format_RGB32,
101         Format_ARGB32,
102         Format_ARGB32_Premultiplied,
103         Format_RGB16,
104         Format_ARGB8565_Premultiplied,
105         Format_RGB666,
106         Format_ARGB6666_Premultiplied,
107         Format_RGB555,
108         Format_ARGB8555_Premultiplied,
109         Format_RGB888,
110         Format_RGB444,
111         Format_ARGB4444_Premultiplied,
112 #if 0
113         // reserved for future use
114         Format_RGB15,
115         Format_Grayscale16,
116         Format_Grayscale8,
117         Format_Grayscale4,
118         Format_Grayscale4LSB,
119         Format_Grayscale2,
120         Format_Grayscale2LSB
121 #endif
122 #ifndef qdoc
123         NImageFormats
124 #endif
125     };
126
127     QImage();
128     QImage(const QSize &size, Format format);
129     QImage(int width, int height, Format format);
130     QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = 0, void *cleanupInfo = 0);
131     QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = 0, void *cleanupInfo = 0);
132     QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = 0, void *cleanupInfo = 0);
133     QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = 0, void *cleanupInfo = 0);
134
135 #ifndef QT_NO_IMAGEFORMAT_XPM
136     explicit QImage(const char * const xpm[]);
137 #endif
138     explicit QImage(const QString &fileName, const char *format = 0);
139
140     QImage(const QImage &);
141     ~QImage();
142
143     QImage &operator=(const QImage &);
144 #ifdef Q_COMPILER_RVALUE_REFS
145     inline QImage &operator=(QImage &&other)
146     { qSwap(d, other.d); return *this; }
147 #endif
148     inline void swap(QImage &other) { qSwap(d, other.d); }
149
150     bool isNull() const;
151
152     int devType() const;
153
154     bool operator==(const QImage &) const;
155     bool operator!=(const QImage &) const;
156     operator QVariant() const;
157     void detach();
158     bool isDetached() const;
159
160     QImage copy(const QRect &rect = QRect()) const;
161     inline QImage copy(int x, int y, int w, int h) const
162         { return copy(QRect(x, y, w, h)); }
163
164     Format format() const;
165
166     QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
167     QImage convertToFormat(Format f, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
168
169     int width() const;
170     int height() const;
171     QSize size() const;
172     QRect rect() const;
173
174     int depth() const;
175     int colorCount() const;
176     int bitPlaneCount() const;
177
178     QRgb color(int i) const;
179     void setColor(int i, QRgb c);
180     void setColorCount(int);
181
182     bool allGray() const;
183     bool isGrayscale() const;
184
185     uchar *bits();
186     const uchar *bits() const;
187     const uchar *constBits() const;
188
189     int byteCount() const;
190
191     uchar *scanLine(int);
192     const uchar *scanLine(int) const;
193     const uchar *constScanLine(int) const;
194     int bytesPerLine() const;
195
196     bool valid(int x, int y) const;
197     bool valid(const QPoint &pt) const;
198
199     int pixelIndex(int x, int y) const;
200     int pixelIndex(const QPoint &pt) const;
201
202     QRgb pixel(int x, int y) const;
203     QRgb pixel(const QPoint &pt) const;
204
205     void setPixel(int x, int y, uint index_or_rgb);
206     void setPixel(const QPoint &pt, uint index_or_rgb);
207
208     QVector<QRgb> colorTable() const;
209     void setColorTable(const QVector<QRgb> colors);
210
211     void fill(uint pixel);
212     void fill(const QColor &color);
213     void fill(Qt::GlobalColor color);
214
215
216     bool hasAlphaChannel() const;
217     void setAlphaChannel(const QImage &alphaChannel);
218     QImage alphaChannel() const;
219     QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
220 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
221     QImage createHeuristicMask(bool clipTight = true) const;
222 #endif
223     QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
224
225     inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
226                         Qt::TransformationMode mode = Qt::FastTransformation) const
227         { return scaled(QSize(w, h), aspectMode, mode); }
228     QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
229                  Qt::TransformationMode mode = Qt::FastTransformation) const;
230     QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
231     QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
232     QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
233     static QMatrix trueMatrix(const QMatrix &, int w, int h);
234     QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
235     static QTransform trueMatrix(const QTransform &, int w, int h);
236     QImage mirrored(bool horizontally = false, bool vertically = true) const;
237     QImage rgbSwapped() const;
238     void invertPixels(InvertMode = InvertRgb);
239
240
241     bool load(QIODevice *device, const char* format);
242     bool load(const QString &fileName, const char* format=0);
243     bool loadFromData(const uchar *buf, int len, const char *format = 0);
244     inline bool loadFromData(const QByteArray &data, const char* aformat=0)
245         { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }
246
247     bool save(const QString &fileName, const char* format=0, int quality=-1) const;
248     bool save(QIODevice *device, const char* format=0, int quality=-1) const;
249
250     static QImage fromData(const uchar *data, int size, const char *format = 0);
251     inline static QImage fromData(const QByteArray &data, const char *format = 0)
252         { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }
253
254 #if QT_DEPRECATED_SINCE(5, 0)
255     QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
256 #endif
257     qint64 cacheKey() const;
258
259     QPaintEngine *paintEngine() const;
260
261     // Auxiliary data
262     int dotsPerMeterX() const;
263     int dotsPerMeterY() const;
264     void setDotsPerMeterX(int);
265     void setDotsPerMeterY(int);
266     QPoint offset() const;
267     void setOffset(const QPoint&);
268
269     QStringList textKeys() const;
270     QString text(const QString &key = QString()) const;
271     void setText(const QString &key, const QString &value);
272
273 #if QT_DEPRECATED_SINCE(5, 0)
274     QT_DEPRECATED inline QString text(const char* key, const char* lang=0) const;
275     QT_DEPRECATED inline QList<QImageTextKeyLang> textList() const;
276     QT_DEPRECATED inline QStringList textLanguages() const;
277     QT_DEPRECATED inline QString text(const QImageTextKeyLang&) const;
278     QT_DEPRECATED inline void setText(const char* key, const char* lang, const QString&);
279 #endif
280
281 #if QT_DEPRECATED_SINCE(5, 0)
282     QT_DEPRECATED inline int numColors() const;
283     QT_DEPRECATED inline void setNumColors(int);
284     QT_DEPRECATED inline int numBytes() const;
285 #endif
286
287 protected:
288     virtual int metric(PaintDeviceMetric metric) const;
289
290 private:
291     friend class QWSOnScreenSurface;
292     QImageData *d;
293
294     friend class QRasterPlatformPixmap;
295     friend class QBlittablePlatformPixmap;
296     friend class QPixmapCacheEntry;
297
298 public:
299     typedef QImageData * DataPtr;
300     inline DataPtr &data_ptr() { return d; }
301 };
302
303 Q_DECLARE_SHARED(QImage)
304 Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);
305
306 // Inline functions...
307
308 inline bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
309 inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
310 inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
311 inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
312
313 #if QT_DEPRECATED_SINCE(5, 0)
314
315 #if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
316 # pragma GCC diagnostic push
317 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
318 #elif defined(Q_CC_MSVC)
319 # pragma warning(disable: 4996)
320 #endif
321
322 inline QString QImage::text(const char* key, const char* lang) const
323 {
324     if (!d)
325         return QString();
326     QString k = QString::fromAscii(key);
327     if (lang && *lang)
328         k += QLatin1Char('/') + QString::fromAscii(lang);
329     return text(k);
330 }
331
332 inline QList<QImageTextKeyLang> QImage::textList() const
333 {
334     QList<QImageTextKeyLang> imageTextKeys;
335     if (!d)
336         return imageTextKeys;
337     QStringList keys = textKeys();
338     for (int i = 0; i < keys.size(); ++i) {
339         int index = keys.at(i).indexOf(QLatin1Char('/'));
340         if (index > 0) {
341             QImageTextKeyLang tkl;
342             tkl.key = keys.at(i).left(index).toAscii();
343             tkl.lang = keys.at(i).mid(index+1).toAscii();
344             imageTextKeys += tkl;
345         }
346     }
347
348     return imageTextKeys;
349 }
350
351 inline QStringList QImage::textLanguages() const
352 {
353     if (!d)
354         return QStringList();
355     QStringList keys = textKeys();
356     QStringList languages;
357     for (int i = 0; i < keys.size(); ++i) {
358         int index = keys.at(i).indexOf(QLatin1Char('/'));
359         if (index > 0)
360             languages += keys.at(i).mid(index+1);
361     }
362
363     return languages;
364 }
365
366 inline QString QImage::text(const QImageTextKeyLang&kl) const
367 {
368     if (!d)
369         return QString();
370     QString k = QString::fromAscii(kl.key.constData());
371     if (!kl.lang.isEmpty())
372         k += QLatin1Char('/') + QString::fromAscii(kl.lang.constData());
373     return text(k);
374 }
375
376 inline void QImage::setText(const char* key, const char* lang, const QString &s)
377 {
378     if (!d)
379         return;
380     detach();
381
382     // In case detach() ran out of memory
383     if (!d)
384         return;
385
386     QString k = QString::fromAscii(key);
387     if (lang && *lang)
388         k += QLatin1Char('/') + QString::fromAscii(lang);
389     setText(k, s);
390 }
391
392 #if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)
393 # pragma GCC diagnostic pop
394 #elif defined(Q_CC_MSVC)
395 # pragma warning(default: 4996)
396 #endif
397
398 inline int QImage::numColors() const
399 {
400     return colorCount();
401 }
402
403 inline void QImage::setNumColors(int n)
404 {
405     setColorCount(n);
406 }
407
408 inline int QImage::numBytes() const
409 {
410     return byteCount();
411 }
412 #endif
413
414 // QImage stream functions
415
416 #if !defined(QT_NO_DATASTREAM)
417 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
418 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
419 #endif
420
421 #ifndef QT_NO_DEBUG_STREAM
422 Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);
423 #endif
424
425
426 QT_END_NAMESPACE
427
428 QT_END_HEADER
429
430 #endif // QIMAGE_H