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 QColorSpace;
65class QColorTransform;
66class QIODevice;
67class QMatrix;
68class QStringList;
69class QTransform;
70class QVariant;
71template <class T> class QList;
72template <class T> class QVector;
73
74struct QImageData;
75class QImageDataMisc; // internal
76#if QT_DEPRECATED_SINCE(5, 0)
77class QImageTextKeyLang {
78public:
79 QT_DEPRECATED QImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { }
80 QT_DEPRECATED QImageTextKeyLang() { }
81
82 QByteArray key;
83 QByteArray lang;
84
85 bool operator< (const QImageTextKeyLang& other) const
86 { return key < other.key || (key==other.key && lang < other.lang); }
87 bool operator== (const QImageTextKeyLang& other) const
88 { return key==other.key && lang==other.lang; }
89 inline bool operator!= (const QImageTextKeyLang &other) const
90 { return !operator==(other); }
91private:
92 friend class QImage;
93 QImageTextKeyLang(bool /*dummy*/) {}
94};
95#endif
96
97typedef void (*QImageCleanupFunction)(void*);
98
99class Q_GUI_EXPORT QImage : public QPaintDevice
100{
101 Q_GADGET
102public:
103 enum InvertMode { InvertRgb, InvertRgba };
104 enum Format {
105 Format_Invalid,
106 Format_Mono,
107 Format_MonoLSB,
108 Format_Indexed8,
109 Format_RGB32,
110 Format_ARGB32,
111 Format_ARGB32_Premultiplied,
112 Format_RGB16,
113 Format_ARGB8565_Premultiplied,
114 Format_RGB666,
115 Format_ARGB6666_Premultiplied,
116 Format_RGB555,
117 Format_ARGB8555_Premultiplied,
118 Format_RGB888,
119 Format_RGB444,
120 Format_ARGB4444_Premultiplied,
121 Format_RGBX8888,
122 Format_RGBA8888,
123 Format_RGBA8888_Premultiplied,
124 Format_BGR30,
125 Format_A2BGR30_Premultiplied,
126 Format_RGB30,
127 Format_A2RGB30_Premultiplied,
128 Format_Alpha8,
129 Format_Grayscale8,
130 Format_RGBX64,
131 Format_RGBA64,
132 Format_RGBA64_Premultiplied,
133 Format_Grayscale16,
134#ifndef Q_QDOC
135 NImageFormats
136#endif
137 };
138 Q_ENUM(Format)
139
140 QImage() Q_DECL_NOEXCEPT;
141 QImage(const QSize &size, Format format);
142 QImage(int width, int height, Format format);
143 QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
144 QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
145 QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
146 QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
147
148#ifndef QT_NO_IMAGEFORMAT_XPM
149 explicit QImage(const char * const xpm[]);
150#endif
151 explicit QImage(const QString &fileName, const char *format = nullptr);
152
153 QImage(const QImage &);
154#ifdef Q_COMPILER_RVALUE_REFS
155 inline QImage(QImage &&other) Q_DECL_NOEXCEPT
156 : QPaintDevice(), d(nullptr)
157 { qSwap(d, other.d); }
158#endif
159 ~QImage();
160
161 QImage &operator=(const QImage &);
162#ifdef Q_COMPILER_RVALUE_REFS
163 inline QImage &operator=(QImage &&other) Q_DECL_NOEXCEPT
164 { qSwap(d, other.d); return *this; }
165#endif
166 inline void swap(QImage &other) Q_DECL_NOEXCEPT
167 { qSwap(d, other.d); }
168
169 bool isNull() const;
170
171 int devType() const override;
172
173 bool operator==(const QImage &) const;
174 bool operator!=(const QImage &) const;
175 operator QVariant() const;
176 void detach();
177 bool isDetached() const;
178
179 QImage copy(const QRect &rect = QRect()) const;
180 inline QImage copy(int x, int y, int w, int h) const
181 { return copy(QRect(x, y, w, h)); }
182
183 Format format() const;
184
185#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QIMAGE_COMPAT_CPP)
186 Q_REQUIRED_RESULT Q_ALWAYS_INLINE QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
187 { return convertToFormat_helper(f, flags); }
188 Q_REQUIRED_RESULT Q_ALWAYS_INLINE QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
189 {
190 if (convertToFormat_inplace(f, flags))
191 return std::move(*this);
192 else
193 return convertToFormat_helper(f, flags);
194 }
195#else
196 Q_REQUIRED_RESULT QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
197#endif
198 Q_REQUIRED_RESULT QImage convertToFormat(Format f, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
199 bool reinterpretAsFormat(Format f);
200
201 void convertTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor);
202
203 int width() const;
204 int height() const;
205 QSize size() const;
206 QRect rect() const;
207
208 int depth() const;
209 int colorCount() const;
210 int bitPlaneCount() const;
211
212 QRgb color(int i) const;
213 void setColor(int i, QRgb c);
214 void setColorCount(int);
215
216 bool allGray() const;
217 bool isGrayscale() const;
218
219 uchar *bits();
220 const uchar *bits() const;
221 const uchar *constBits() const;
222
223#if QT_DEPRECATED_SINCE(5, 10)
224 QT_DEPRECATED_X("Use sizeInBytes") int byteCount() const;
225#endif
226 qsizetype sizeInBytes() const;
227
228 uchar *scanLine(int);
229 const uchar *scanLine(int) const;
230 const uchar *constScanLine(int) const;
231 int bytesPerLine() const;
232
233 bool valid(int x, int y) const;
234 bool valid(const QPoint &pt) const;
235
236 int pixelIndex(int x, int y) const;
237 int pixelIndex(const QPoint &pt) const;
238
239 QRgb pixel(int x, int y) const;
240 QRgb pixel(const QPoint &pt) const;
241
242 void setPixel(int x, int y, uint index_or_rgb);
243 void setPixel(const QPoint &pt, uint index_or_rgb);
244
245 QColor pixelColor(int x, int y) const;
246 QColor pixelColor(const QPoint &pt) const;
247
248 void setPixelColor(int x, int y, const QColor &c);
249 void setPixelColor(const QPoint &pt, const QColor &c);
250
251 QVector<QRgb> colorTable() const;
252#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
253 void setColorTable(const QVector<QRgb> &colors);
254#else
255 void setColorTable(const QVector<QRgb> colors);
256#endif
257
258 qreal devicePixelRatio() const;
259 void setDevicePixelRatio(qreal scaleFactor);
260
261 void fill(uint pixel);
262 void fill(const QColor &color);
263 void fill(Qt::GlobalColor color);
264
265
266 bool hasAlphaChannel() const;
267 void setAlphaChannel(const QImage &alphaChannel);
268 QImage alphaChannel() const;
269 QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
270#ifndef QT_NO_IMAGE_HEURISTIC_MASK
271 QImage createHeuristicMask(bool clipTight = true) const;
272#endif
273 QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
274
275 inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
276 Qt::TransformationMode mode = Qt::FastTransformation) const
277 { return scaled(QSize(w, h), aspectMode, mode); }
278 QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
279 Qt::TransformationMode mode = Qt::FastTransformation) const;
280 QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
281 QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
282 QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
283 static QMatrix trueMatrix(const QMatrix &, int w, int h);
284 QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
285 static QTransform trueMatrix(const QTransform &, int w, int h);
286#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QIMAGE_COMPAT_CPP)
287 QImage mirrored(bool horizontally = false, bool vertically = true) const &
288 { return mirrored_helper(horizontally, vertically); }
289 QImage &&mirrored(bool horizontally = false, bool vertically = true) &&
290 { mirrored_inplace(horizontally, vertically); return qMove(*this); }
291 QImage rgbSwapped() const &
292 { return rgbSwapped_helper(); }
293 QImage &&rgbSwapped() &&
294 { rgbSwapped_inplace(); return qMove(*this); }
295#else
296 QImage mirrored(bool horizontally = false, bool vertically = true) const;
297 QImage rgbSwapped() const;
298#endif
299 void invertPixels(InvertMode = InvertRgb);
300
301 QColorSpace colorSpace() const;
302 QImage convertedToColorSpace(const QColorSpace &) const;
303 void convertToColorSpace(const QColorSpace &);
304 void setColorSpace(const QColorSpace &);
305
306 void applyColorTransform(const QColorTransform &transform);
307
308 bool load(QIODevice *device, const char* format);
309 bool load(const QString &fileName, const char *format = nullptr);
310 bool loadFromData(const uchar *buf, int len, const char *format = nullptr);
311 inline bool loadFromData(const QByteArray &data, const char *aformat = nullptr)
312 { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }
313
314 bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const;
315 bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const;
316
317 static QImage fromData(const uchar *data, int size, const char *format = nullptr);
318 inline static QImage fromData(const QByteArray &data, const char *format = nullptr)
319 { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }
320
321#if QT_DEPRECATED_SINCE(5, 0)
322 QT_DEPRECATED inline int serialNumber() const { return cacheKey() >> 32; }
323#endif
324 qint64 cacheKey() const;
325
326 QPaintEngine *paintEngine() const override;
327
328 // Auxiliary data
329 int dotsPerMeterX() const;
330 int dotsPerMeterY() const;
331 void setDotsPerMeterX(int);
332 void setDotsPerMeterY(int);
333 QPoint offset() const;
334 void setOffset(const QPoint&);
335
336 QStringList textKeys() const;
337 QString text(const QString &key = QString()) const;
338 void setText(const QString &key, const QString &value);
339
340 QPixelFormat pixelFormat() const Q_DECL_NOTHROW;
341 static QPixelFormat toPixelFormat(QImage::Format format) Q_DECL_NOTHROW;
342 static QImage::Format toImageFormat(QPixelFormat format) Q_DECL_NOTHROW;
343
344 // Platform specific conversion functions
345#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
346 CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED;
347#endif
348
349#if QT_DEPRECATED_SINCE(5, 0)
350 QT_DEPRECATED inline QString text(const char *key, const char *lang = nullptr) const;
351 QT_DEPRECATED inline QList<QImageTextKeyLang> textList() const;
352 QT_DEPRECATED inline QStringList textLanguages() const;
353 QT_DEPRECATED inline QString text(const QImageTextKeyLang&) const;
354 QT_DEPRECATED inline void setText(const char* key, const char* lang, const QString&);
355#endif
356
357#if QT_DEPRECATED_SINCE(5, 0)
358 QT_DEPRECATED inline int numColors() const;
359 QT_DEPRECATED inline void setNumColors(int);
360 QT_DEPRECATED inline int numBytes() const;
361#endif
362
363protected:
364 virtual int metric(PaintDeviceMetric metric) const override;
365 QImage mirrored_helper(bool horizontal, bool vertical) const;
366 QImage rgbSwapped_helper() const;
367 void mirrored_inplace(bool horizontal, bool vertical);
368 void rgbSwapped_inplace();
369 QImage convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const;
370 bool convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags);
371 QImage smoothScaled(int w, int h) const;
372
373private:
374 friend class QWSOnScreenSurface;
375 QImageData *d;
376
377 friend class QRasterPlatformPixmap;
378 friend class QBlittablePlatformPixmap;
379 friend class QPixmapCacheEntry;
380
381public:
382 typedef QImageData * DataPtr;
383 inline DataPtr &data_ptr() { return d; }
384};
385
386Q_DECLARE_SHARED(QImage)
387
388// Inline functions...
389
390inline bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
391inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
392inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
393inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
394inline QColor QImage::pixelColor(const QPoint &pt) const { return pixelColor(pt.x(), pt.y()); }
395inline void QImage::setPixelColor(const QPoint &pt, const QColor &c) { setPixelColor(pt.x(), pt.y(), c); }
396
397#if QT_DEPRECATED_SINCE(5, 0)
398
399QT_WARNING_PUSH
400QT_WARNING_DISABLE_DEPRECATED
401
402inline QString QImage::text(const char* key, const char* lang) const
403{
404 if (!d)
405 return QString();
406 QString k = QString::fromLatin1(key);
407 if (lang && *lang)
408 k += QLatin1Char('/') + QString::fromLatin1(lang);
409 return text(k);
410}
411
412inline QList<QImageTextKeyLang> QImage::textList() const
413{
414 QList<QImageTextKeyLang> imageTextKeys;
415 if (!d)
416 return imageTextKeys;
417 QStringList keys = textKeys();
418 for (int i = 0; i < keys.size(); ++i) {
419 int index = keys.at(i).indexOf(QLatin1Char('/'));
420 if (index > 0) {
421 QImageTextKeyLang tkl(true);
422 tkl.key = keys.at(i).left(index).toLatin1();
423 tkl.lang = keys.at(i).mid(index+1).toLatin1();
424 imageTextKeys += tkl;
425 }
426 }
427
428 return imageTextKeys;
429}
430
431inline QStringList QImage::textLanguages() const
432{
433 if (!d)
434 return QStringList();
435 QStringList keys = textKeys();
436 QStringList languages;
437 for (int i = 0; i < keys.size(); ++i) {
438 int index = keys.at(i).indexOf(QLatin1Char('/'));
439 if (index > 0)
440 languages += keys.at(i).mid(index+1);
441 }
442
443 return languages;
444}
445
446inline QString QImage::text(const QImageTextKeyLang&kl) const
447{
448 if (!d)
449 return QString();
450 QString k = QString::fromLatin1(kl.key.constData());
451 if (!kl.lang.isEmpty())
452 k += QLatin1Char('/') + QString::fromLatin1(kl.lang.constData());
453 return text(k);
454}
455
456inline void QImage::setText(const char* key, const char* lang, const QString &s)
457{
458 if (!d)
459 return;
460 detach();
461
462 // In case detach() ran out of memory
463 if (!d)
464 return;
465
466 QString k = QString::fromLatin1(key);
467 if (lang && *lang)
468 k += QLatin1Char('/') + QString::fromLatin1(lang);
469 setText(k, s);
470}
471
472QT_WARNING_POP
473
474inline int QImage::numColors() const
475{
476 return colorCount();
477}
478
479inline void QImage::setNumColors(int n)
480{
481 setColorCount(n);
482}
483
484inline int QImage::numBytes() const
485{
486 return int(sizeInBytes());
487}
488#endif
489
490// QImage stream functions
491
492#if !defined(QT_NO_DATASTREAM)
493Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
494Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
495#endif
496
497#ifndef QT_NO_DEBUG_STREAM
498Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);
499#endif
500
501
502QT_END_NAMESPACE
503
504#endif // QIMAGE_H
505