1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
52 #if QT_DEPRECATED_SINCE(5, 0)
53 #include <QtCore/qstringlist.h>
66 template <class T> class QList;
67 template <class T> class QVector;
70 class QImageDataMisc; // internal
71 #if QT_DEPRECATED_SINCE(5, 0)
72 class QImageTextKeyLang {
74 QT_DEPRECATED QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
75 QT_DEPRECATED QImageTextKeyLang() { }
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); }
89 typedef void (*QImageCleanupFunction)(void*);
91 class Q_GUI_EXPORT QImage : public QPaintDevice
94 enum InvertMode { InvertRgb, InvertRgba };
102 Format_ARGB32_Premultiplied,
104 Format_ARGB8565_Premultiplied,
106 Format_ARGB6666_Premultiplied,
108 Format_ARGB8555_Premultiplied,
111 Format_ARGB4444_Premultiplied,
113 // reserved for future use
118 Format_Grayscale4LSB,
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);
135 #ifndef QT_NO_IMAGEFORMAT_XPM
136 explicit QImage(const char * const xpm[]);
138 explicit QImage(const QString &fileName, const char *format = 0);
140 QImage(const QImage &);
143 QImage &operator=(const QImage &);
144 #ifdef Q_COMPILER_RVALUE_REFS
145 inline QImage &operator=(QImage &&other)
146 { qSwap(d, other.d); return *this; }
148 inline void swap(QImage &other) { qSwap(d, other.d); }
154 bool operator==(const QImage &) const;
155 bool operator!=(const QImage &) const;
156 operator QVariant() const;
158 bool isDetached() const;
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)); }
164 Format format() const;
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;
175 int colorCount() const;
176 int bitPlaneCount() const;
178 QRgb color(int i) const;
179 void setColor(int i, QRgb c);
180 void setColorCount(int);
182 bool allGray() const;
183 bool isGrayscale() const;
186 const uchar *bits() const;
187 const uchar *constBits() const;
189 int byteCount() const;
191 uchar *scanLine(int);
192 const uchar *scanLine(int) const;
193 const uchar *constScanLine(int) const;
194 int bytesPerLine() const;
196 bool valid(int x, int y) const;
197 bool valid(const QPoint &pt) const;
199 int pixelIndex(int x, int y) const;
200 int pixelIndex(const QPoint &pt) const;
202 QRgb pixel(int x, int y) const;
203 QRgb pixel(const QPoint &pt) const;
205 void setPixel(int x, int y, uint index_or_rgb);
206 void setPixel(const QPoint &pt, uint index_or_rgb);
208 QVector<QRgb> colorTable() const;
209 void setColorTable(const QVector<QRgb> colors);
211 void fill(uint pixel);
212 void fill(const QColor &color);
213 void fill(Qt::GlobalColor color);
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;
223 QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
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);
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); }
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;
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); }
254 #if QT_DEPRECATED_SINCE(5, 0)
255 QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
257 qint64 cacheKey() const;
259 QPaintEngine *paintEngine() const;
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&);
269 QStringList textKeys() const;
270 QString text(const QString &key = QString()) const;
271 void setText(const QString &key, const QString &value);
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&);
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;
288 virtual int metric(PaintDeviceMetric metric) const;
291 friend class QWSOnScreenSurface;
294 friend class QRasterPlatformPixmap;
295 friend class QBlittablePlatformPixmap;
296 friend class QPixmapCacheEntry;
299 typedef QImageData * DataPtr;
300 inline DataPtr &data_ptr() { return d; }
303 Q_DECLARE_SHARED(QImage)
304 Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);
306 // Inline functions...
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); }
313 #if QT_DEPRECATED_SINCE(5, 0)
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)
322 inline QString QImage::text(const char* key, const char* lang) const
326 QString k = QString::fromAscii(key);
328 k += QLatin1Char('/') + QString::fromAscii(lang);
332 inline QList<QImageTextKeyLang> QImage::textList() const
334 QList<QImageTextKeyLang> imageTextKeys;
336 return imageTextKeys;
337 QStringList keys = textKeys();
338 for (int i = 0; i < keys.size(); ++i) {
339 int index = keys.at(i).indexOf(QLatin1Char('/'));
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;
348 return imageTextKeys;
351 inline QStringList QImage::textLanguages() const
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('/'));
360 languages += keys.at(i).mid(index+1);
366 inline QString QImage::text(const QImageTextKeyLang&kl) const
370 QString k = QString::fromAscii(kl.key.constData());
371 if (!kl.lang.isEmpty())
372 k += QLatin1Char('/') + QString::fromAscii(kl.lang.constData());
376 inline void QImage::setText(const char* key, const char* lang, const QString &s)
382 // In case detach() ran out of memory
386 QString k = QString::fromAscii(key);
388 k += QLatin1Char('/') + QString::fromAscii(lang);
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)
398 inline int QImage::numColors() const
403 inline void QImage::setNumColors(int n)
408 inline int QImage::numBytes() const
414 // QImage stream functions
416 #if !defined(QT_NO_DATASTREAM)
417 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
418 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
421 #ifndef QT_NO_DEBUG_STREAM
422 Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);