1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QIMAGE_H
41#define QIMAGE_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qcolor.h>
45#include <QtGui/qrgb.h>
46#include <QtGui/qpaintdevice.h>
47#include <QtGui/qpixelformat.h>
48#include <QtGui/qtransform.h>
49#include <QtCore/qbytearray.h>
50#include <QtCore/qrect.h>
51#include <QtCore/qstring.h>
52
53#if QT_DEPRECATED_SINCE(5, 0)
54#include <QtCore/qstringlist.h>
55#endif
56
57#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
58Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(CGImage);
59#endif
60
61QT_BEGIN_NAMESPACE
62
63
64class QIODevice;
65class QStringList;
66class QMatrix;
67class QTransform;
68class QVariant;
69template <class T> class QList;
70template <class T> class QVector;
71
72struct QImageData;
73class QImageDataMisc; // internal
74#if QT_DEPRECATED_SINCE(5, 0)
75class QImageTextKeyLang {
76public:
77 QT_DEPRECATED QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
78 QT_DEPRECATED QImageTextKeyLang() { }
79
80 QByteArray key;
81 QByteArray lang;
82
83 bool operator< (const QImageTextKeyLang& other) const
84 { return key < other.key || (key==other.key && lang < other.lang); }
85 bool operator== (const QImageTextKeyLang& other) const
86 { return key==other.key && lang==other.lang; }
87 inline bool operator!= (const QImageTextKeyLang &other) const
88 { return !operator==(other); }
89private:
90 friend class QImage;
91 QImageTextKeyLang(bool /*dummy*/) {}
92};
93#endif
94
95typedef void (*QImageCleanupFunction)(void*);
96
97class Q_GUI_EXPORT QImage : public QPaintDevice
98{
99 Q_GADGET
100public:
101 enum InvertMode { InvertRgb, InvertRgba };
102 enum Format {
103 Format_Invalid,
104 Format_Mono,
105 Format_MonoLSB,
106 Format_Indexed8,
107 Format_RGB32,
108 Format_ARGB32,
109 Format_ARGB32_Premultiplied,
110 Format_RGB16,
111 Format_ARGB8565_Premultiplied,
112 Format_RGB666,
113 Format_ARGB6666_Premultiplied,
114 Format_RGB555,
115 Format_ARGB8555_Premultiplied,
116 Format_RGB888,
117 Format_RGB444,
118 Format_ARGB4444_Premultiplied,
119 Format_RGBX8888,
120 Format_RGBA8888,
121 Format_RGBA8888_Premultiplied,
122 Format_BGR30,
123 Format_A2BGR30_Premultiplied,
124 Format_RGB30,
125 Format_A2RGB30_Premultiplied,
126 Format_Alpha8,
127 Format_Grayscale8,
128 Format_RGBX64,
129 Format_RGBA64,
130 Format_RGBA64_Premultiplied,
131#if 0
132 // reserved for future use
133 Format_Grayscale16,
134#endif
135#ifndef Q_QDOC
136 NImageFormats
137#endif
138 };
139 Q_ENUM(Format)
140
141 QImage() Q_DECL_NOEXCEPT;
142 QImage(const QSize &size, Format format);
143 QImage(int width, int height, Format format);
144 QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
145 QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
146 QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
147 QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
148
149#ifndef QT_NO_IMAGEFORMAT_XPM
150 explicit QImage(const char * const xpm[]);
151#endif
152 explicit QImage(const QString &fileName, const char *format = nullptr);
153
154 QImage(const QImage &);
155#ifdef Q_COMPILER_RVALUE_REFS
156 inline QImage(QImage &&other) Q_DECL_NOEXCEPT
157 : QPaintDevice(), d(nullptr)
158 { qSwap(d, other.d); }
159#endif
160 ~QImage();
161
162 QImage &operator=(const QImage &);
163#ifdef Q_COMPILER_RVALUE_REFS
164 inline QImage &operator=(QImage &&other) Q_DECL_NOEXCEPT
165 { qSwap(d, other.d); return *this; }
166#endif
167 inline void swap(QImage &other) Q_DECL_NOEXCEPT
168 { qSwap(d, other.d); }
169
170 bool isNull() const;
171
172 int devType() const override;
173
174 bool operator==(const QImage &) const;
175 bool operator!=(const QImage &) const;
176 operator QVariant() const;
177 void detach();
178 bool isDetached() const;
179
180 QImage copy(const QRect &rect = QRect()) const;
181 inline QImage copy(int x, int y, int w, int h) const
182 { return copy(QRect(x, y, w, h)); }
183
184 Format format() const;
185
186#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QIMAGE_COMPAT_CPP)
187 Q_REQUIRED_RESULT Q_ALWAYS_INLINE QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
188 { return convertToFormat_helper(f, flags); }
189 Q_REQUIRED_RESULT Q_ALWAYS_INLINE QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
190 {
191 if (convertToFormat_inplace(f, flags))
192 return std::move(*this);
193 else
194 return convertToFormat_helper(f, flags);
195 }
196#else
197 Q_REQUIRED_RESULT QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
198#endif
199 Q_REQUIRED_RESULT QImage convertToFormat(Format f, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
200 bool reinterpretAsFormat(Format f);
201
202 int width() const;
203 int height() const;
204 QSize size() const;
205 QRect rect() const;
206
207 int depth() const;
208 int colorCount() const;
209 int bitPlaneCount() const;
210
211 QRgb color(int i) const;
212 void setColor(int i, QRgb c);
213 void setColorCount(int);
214
215 bool allGray() const;
216 bool isGrayscale() const;
217
218 uchar *bits();
219 const uchar *bits() const;
220 const uchar *constBits() const;
221
222#if QT_DEPRECATED_SINCE(5, 10)
223 QT_DEPRECATED_X("Use sizeInBytes") int byteCount() const;
224#endif
225 qsizetype sizeInBytes() const;
226
227 uchar *scanLine(int);
228 const uchar *scanLine(int) const;
229 const uchar *constScanLine(int) const;
230 int bytesPerLine() const;
231
232 bool valid(int x, int y) const;
233 bool valid(const QPoint &pt) const;
234
235 int pixelIndex(int x, int y) const;
236 int pixelIndex(const QPoint &pt) const;
237
238 QRgb pixel(int x, int y) const;
239 QRgb pixel(const QPoint &pt) const;
240
241 void setPixel(int x, int y, uint index_or_rgb);
242 void setPixel(const QPoint &pt, uint index_or_rgb);
243
244 QColor pixelColor(int x, int y) const;
245 QColor pixelColor(const QPoint &pt) const;
246
247 void setPixelColor(int x, int y, const QColor &c);
248 void setPixelColor(const QPoint &pt, const QColor &c);
249
250 QVector<QRgb> colorTable() const;
251#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
252 void setColorTable(const QVector<QRgb> &colors);
253#else
254 void setColorTable(const QVector<QRgb> colors);
255#endif
256
257 qreal devicePixelRatio() const;
258 void setDevicePixelRatio(qreal scaleFactor);
259
260 void fill(uint pixel);
261 void fill(const QColor &color);
262 void fill(Qt::GlobalColor color);
263
264
265 bool hasAlphaChannel() const;
266 void setAlphaChannel(const QImage &alphaChannel);
267 QImage alphaChannel() const;
268 QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
269#ifndef QT_NO_IMAGE_HEURISTIC_MASK
270 QImage createHeuristicMask(bool clipTight = true) const;
271#endif
272 QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
273
274 inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
275 Qt::TransformationMode mode = Qt::FastTransformation) const
276 { return scaled(QSize(w, h), aspectMode, mode); }
277 QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
278 Qt::TransformationMode mode = Qt::FastTransformation) const;
279 QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
280 QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
281 QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
282 static QMatrix trueMatrix(const QMatrix &, int w, int h);
283 QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
284 static QTransform trueMatrix(const QTransform &, int w, int h);
285#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QIMAGE_COMPAT_CPP)
286 QImage mirrored(bool horizontally = false, bool vertically = true) const &
287 { return mirrored_helper(horizontally, vertically); }
288 QImage &&mirrored(bool horizontally = false, bool vertically = true) &&
289 { mirrored_inplace(horizontally, vertically); return qMove(*this); }
290 QImage rgbSwapped() const &
291 { return rgbSwapped_helper(); }
292 QImage &&rgbSwapped() &&
293 { rgbSwapped_inplace(); return qMove(*this); }
294#else
295 QImage mirrored(bool horizontally = false, bool vertically = true) const;
296 QImage rgbSwapped() const;
297#endif
298 void invertPixels(InvertMode = InvertRgb);
299
300
301 bool load(QIODevice *device, const char* format);
302 bool load(const QString &fileName, const char *format = nullptr);
303 bool loadFromData(const uchar *buf, int len, const char *format = nullptr);
304 inline bool loadFromData(const QByteArray &data, const char *aformat = nullptr)
305 { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }
306
307 bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const;
308 bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const;
309
310 static QImage fromData(const uchar *data, int size, const char *format = nullptr);
311 inline static QImage fromData(const QByteArray &data, const char *format = nullptr)
312 { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }
313
314#if QT_DEPRECATED_SINCE(5, 0)
315 QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
316#endif
317 qint64 cacheKey() const;
318
319 QPaintEngine *paintEngine() const override;
320
321 // Auxiliary data
322 int dotsPerMeterX() const;
323 int dotsPerMeterY() const;
324 void setDotsPerMeterX(int);
325 void setDotsPerMeterY(int);
326 QPoint offset() const;
327 void setOffset(const QPoint&);
328
329 QStringList textKeys() const;
330 QString text(const QString &key = QString()) const;
331 void setText(const QString &key, const QString &value);
332
333 QPixelFormat pixelFormat() const Q_DECL_NOTHROW;
334 static QPixelFormat toPixelFormat(QImage::Format format) Q_DECL_NOTHROW;
335 static QImage::Format toImageFormat(QPixelFormat format) Q_DECL_NOTHROW;
336
337 // Platform specific conversion functions
338#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
339 CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED;
340#endif
341
342#if QT_DEPRECATED_SINCE(5, 0)
343 QT_DEPRECATED inline QString text(const char *key, const char *lang = nullptr) const;
344 QT_DEPRECATED inline QList<QImageTextKeyLang> textList() const;
345 QT_DEPRECATED inline QStringList textLanguages() const;
346 QT_DEPRECATED inline QString text(const QImageTextKeyLang&) const;
347 QT_DEPRECATED inline void setText(const char* key, const char* lang, const QString&);
348#endif
349
350#if QT_DEPRECATED_SINCE(5, 0)
351 QT_DEPRECATED inline int numColors() const;
352 QT_DEPRECATED inline void setNumColors(int);
353 QT_DEPRECATED inline int numBytes() const;
354#endif
355
356protected:
357 virtual int metric(PaintDeviceMetric metric) const override;
358 QImage mirrored_helper(bool horizontal, bool vertical) const;
359 QImage rgbSwapped_helper() const;
360 void mirrored_inplace(bool horizontal, bool vertical);
361 void rgbSwapped_inplace();
362 QImage convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const;
363 bool convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags);
364 QImage smoothScaled(int w, int h) const;
365
366private:
367 friend class QWSOnScreenSurface;
368 QImageData *d;
369
370 friend class QRasterPlatformPixmap;
371 friend class QBlittablePlatformPixmap;
372 friend class QPixmapCacheEntry;
373
374public:
375 typedef QImageData * DataPtr;
376 inline DataPtr &data_ptr() { return d; }
377};
378
379Q_DECLARE_SHARED(QImage)
380
381// Inline functions...
382
383inline bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
384inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
385inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
386inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
387inline QColor QImage::pixelColor(const QPoint &pt) const { return pixelColor(pt.x(), pt.y()); }
388inline void QImage::setPixelColor(const QPoint &pt, const QColor &c) { setPixelColor(pt.x(), pt.y(), c); }
389
390#if QT_DEPRECATED_SINCE(5, 0)
391
392QT_WARNING_PUSH
393QT_WARNING_DISABLE_DEPRECATED
394
395inline QString QImage::text(const char* key, const char* lang) const
396{
397 if (!d)
398 return QString();
399 QString k = QString::fromLatin1(key);
400 if (lang && *lang)
401 k += QLatin1Char('/') + QString::fromLatin1(lang);
402 return text(k);
403}
404
405inline QList<QImageTextKeyLang> QImage::textList() const
406{
407 QList<QImageTextKeyLang> imageTextKeys;
408 if (!d)
409 return imageTextKeys;
410 QStringList keys = textKeys();
411 for (int i = 0; i < keys.size(); ++i) {
412 int index = keys.at(i).indexOf(QLatin1Char('/'));
413 if (index > 0) {
414 QImageTextKeyLang tkl(true);
415 tkl.key = keys.at(i).left(index).toLatin1();
416 tkl.lang = keys.at(i).mid(index+1).toLatin1();
417 imageTextKeys += tkl;
418 }
419 }
420
421 return imageTextKeys;
422}
423
424inline QStringList QImage::textLanguages() const
425{
426 if (!d)
427 return QStringList();
428 QStringList keys = textKeys();
429 QStringList languages;
430 for (int i = 0; i < keys.size(); ++i) {
431 int index = keys.at(i).indexOf(QLatin1Char('/'));
432 if (index > 0)
433 languages += keys.at(i).mid(index+1);
434 }
435
436 return languages;
437}
438
439inline QString QImage::text(const QImageTextKeyLang&kl) const
440{
441 if (!d)
442 return QString();
443 QString k = QString::fromLatin1(kl.key.constData());
444 if (!kl.lang.isEmpty())
445 k += QLatin1Char('/') + QString::fromLatin1(kl.lang.constData());
446 return text(k);
447}
448
449inline void QImage::setText(const char* key, const char* lang, const QString &s)
450{
451 if (!d)
452 return;
453 detach();
454
455 // In case detach() ran out of memory
456 if (!d)
457 return;
458
459 QString k = QString::fromLatin1(key);
460 if (lang && *lang)
461 k += QLatin1Char('/') + QString::fromLatin1(lang);
462 setText(k, s);
463}
464
465QT_WARNING_POP
466
467inline int QImage::numColors() const
468{
469 return colorCount();
470}
471
472inline void QImage::setNumColors(int n)
473{
474 setColorCount(n);
475}
476
477inline int QImage::numBytes() const
478{
479 return int(sizeInBytes());
480}
481#endif
482
483// QImage stream functions
484
485#if !defined(QT_NO_DATASTREAM)
486Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
487Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
488#endif
489
490#ifndef QT_NO_DEBUG_STREAM
491Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);
492#endif
493
494
495QT_END_NAMESPACE
496
497#endif // QIMAGE_H
498