| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QIMAGE_H |
| 5 | #define QIMAGE_H |
| 6 | |
| 7 | #include <QtGui/qtguiglobal.h> |
| 8 | #include <QtGui/qcolor.h> |
| 9 | #include <QtGui/qrgb.h> |
| 10 | #include <QtGui/qpaintdevice.h> |
| 11 | #include <QtGui/qpixelformat.h> |
| 12 | #include <QtGui/qtransform.h> |
| 13 | #include <QtCore/qbytearray.h> |
| 14 | #include <QtCore/qbytearrayview.h> |
| 15 | #include <QtCore/qrect.h> |
| 16 | #include <QtCore/qstring.h> |
| 17 | #include <QtCore/qcontainerfwd.h> |
| 18 | |
| 19 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 20 | Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(CGImage); |
| 21 | #endif |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | |
| 26 | class QColorSpace; |
| 27 | class QColorTransform; |
| 28 | class QIODevice; |
| 29 | class QTransform; |
| 30 | class QVariant; |
| 31 | |
| 32 | struct QImageData; |
| 33 | |
| 34 | typedef void (*QImageCleanupFunction)(void*); |
| 35 | |
| 36 | class Q_GUI_EXPORT QImage : public QPaintDevice |
| 37 | { |
| 38 | Q_GADGET |
| 39 | public: |
| 40 | enum InvertMode { InvertRgb, InvertRgba }; |
| 41 | enum Format { |
| 42 | Format_Invalid, |
| 43 | Format_Mono, |
| 44 | Format_MonoLSB, |
| 45 | Format_Indexed8, |
| 46 | Format_RGB32, |
| 47 | Format_ARGB32, |
| 48 | Format_ARGB32_Premultiplied, |
| 49 | Format_RGB16, |
| 50 | Format_ARGB8565_Premultiplied, |
| 51 | Format_RGB666, |
| 52 | Format_ARGB6666_Premultiplied, |
| 53 | Format_RGB555, |
| 54 | Format_ARGB8555_Premultiplied, |
| 55 | Format_RGB888, |
| 56 | Format_RGB444, |
| 57 | Format_ARGB4444_Premultiplied, |
| 58 | Format_RGBX8888, |
| 59 | Format_RGBA8888, |
| 60 | Format_RGBA8888_Premultiplied, |
| 61 | Format_BGR30, |
| 62 | Format_A2BGR30_Premultiplied, |
| 63 | Format_RGB30, |
| 64 | Format_A2RGB30_Premultiplied, |
| 65 | Format_Alpha8, |
| 66 | Format_Grayscale8, |
| 67 | Format_RGBX64, |
| 68 | Format_RGBA64, |
| 69 | Format_RGBA64_Premultiplied, |
| 70 | Format_Grayscale16, |
| 71 | Format_BGR888, |
| 72 | Format_RGBX16FPx4, |
| 73 | Format_RGBA16FPx4, |
| 74 | Format_RGBA16FPx4_Premultiplied, |
| 75 | Format_RGBX32FPx4, |
| 76 | Format_RGBA32FPx4, |
| 77 | Format_RGBA32FPx4_Premultiplied, |
| 78 | Format_CMYK8888, |
| 79 | #ifndef Q_QDOC |
| 80 | NImageFormats |
| 81 | #endif |
| 82 | }; |
| 83 | Q_ENUM(Format) |
| 84 | |
| 85 | QImage() noexcept; |
| 86 | QImage(const QSize &size, Format format); |
| 87 | QImage(int width, int height, Format format); |
| 88 | QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr); |
| 89 | QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr); |
| 90 | QImage(uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr); |
| 91 | QImage(const uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr); |
| 92 | |
| 93 | #ifndef QT_NO_IMAGEFORMAT_XPM |
| 94 | explicit QImage(const char * const xpm[]); |
| 95 | #endif |
| 96 | explicit QImage(const QString &fileName, const char *format = nullptr); |
| 97 | |
| 98 | QImage(const QImage &); |
| 99 | QImage(QImage &&other) noexcept |
| 100 | : QPaintDevice(), d(std::exchange(obj&: other.d, new_val: nullptr)) |
| 101 | {} |
| 102 | ~QImage(); |
| 103 | |
| 104 | QImage &operator=(const QImage &); |
| 105 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QImage) |
| 106 | void swap(QImage &other) noexcept |
| 107 | { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
| 108 | |
| 109 | bool isNull() const; |
| 110 | |
| 111 | int devType() const override; |
| 112 | |
| 113 | bool operator==(const QImage &) const; |
| 114 | bool operator!=(const QImage &) const; |
| 115 | operator QVariant() const; |
| 116 | void detach(); |
| 117 | bool isDetached() const; |
| 118 | |
| 119 | [[nodiscard]] QImage copy(const QRect &rect = QRect()) const; |
| 120 | [[nodiscard]] QImage copy(int x, int y, int w, int h) const |
| 121 | { return copy(rect: QRect(x, y, w, h)); } |
| 122 | |
| 123 | Format format() const; |
| 124 | |
| 125 | [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const & |
| 126 | { return convertToFormat_helper(format: f, flags); } |
| 127 | [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) && |
| 128 | { |
| 129 | if (convertToFormat_inplace(format: f, flags)) |
| 130 | return std::move(*this); |
| 131 | else |
| 132 | return convertToFormat_helper(format: f, flags); |
| 133 | } |
| 134 | [[nodiscard]] QImage convertToFormat(Format f, const QList<QRgb> &colorTable, |
| 135 | Qt::ImageConversionFlags flags = Qt::AutoColor) const; |
| 136 | |
| 137 | bool reinterpretAsFormat(Format f); |
| 138 | [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const & |
| 139 | { return convertToFormat(f, flags); } |
| 140 | [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) && |
| 141 | { return convertToFormat(f, flags); } |
| 142 | void convertTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor); |
| 143 | |
| 144 | int width() const; |
| 145 | int height() const; |
| 146 | QSize size() const; |
| 147 | QRect rect() const; |
| 148 | |
| 149 | int depth() const; |
| 150 | int colorCount() const; |
| 151 | int bitPlaneCount() const; |
| 152 | |
| 153 | QRgb color(int i) const; |
| 154 | void setColor(int i, QRgb c); |
| 155 | void setColorCount(int); |
| 156 | |
| 157 | bool allGray() const; |
| 158 | bool isGrayscale() const; |
| 159 | |
| 160 | uchar *bits(); |
| 161 | const uchar *bits() const; |
| 162 | const uchar *constBits() const; |
| 163 | |
| 164 | qsizetype sizeInBytes() const; |
| 165 | |
| 166 | uchar *scanLine(int); |
| 167 | const uchar *scanLine(int) const; |
| 168 | const uchar *constScanLine(int) const; |
| 169 | qsizetype bytesPerLine() const; |
| 170 | |
| 171 | bool valid(int x, int y) const; |
| 172 | bool valid(const QPoint &pt) const; |
| 173 | |
| 174 | int pixelIndex(int x, int y) const; |
| 175 | int pixelIndex(const QPoint &pt) const; |
| 176 | |
| 177 | QRgb pixel(int x, int y) const; |
| 178 | QRgb pixel(const QPoint &pt) const; |
| 179 | |
| 180 | void setPixel(int x, int y, uint index_or_rgb); |
| 181 | void setPixel(const QPoint &pt, uint index_or_rgb); |
| 182 | |
| 183 | QColor pixelColor(int x, int y) const; |
| 184 | QColor pixelColor(const QPoint &pt) const; |
| 185 | |
| 186 | void setPixelColor(int x, int y, const QColor &c); |
| 187 | void setPixelColor(const QPoint &pt, const QColor &c); |
| 188 | |
| 189 | QList<QRgb> colorTable() const; |
| 190 | void setColorTable(const QList<QRgb> &colors); |
| 191 | |
| 192 | qreal devicePixelRatio() const; |
| 193 | void setDevicePixelRatio(qreal scaleFactor); |
| 194 | QSizeF deviceIndependentSize() const; |
| 195 | |
| 196 | void fill(uint pixel); |
| 197 | void fill(const QColor &color); |
| 198 | void fill(Qt::GlobalColor color); |
| 199 | |
| 200 | |
| 201 | bool hasAlphaChannel() const; |
| 202 | void setAlphaChannel(const QImage &alphaChannel); |
| 203 | [[nodiscard]] QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const; |
| 204 | #ifndef QT_NO_IMAGE_HEURISTIC_MASK |
| 205 | [[nodiscard]] QImage createHeuristicMask(bool clipTight = true) const; |
| 206 | #endif |
| 207 | [[nodiscard]] QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const; |
| 208 | |
| 209 | [[nodiscard]] QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio, |
| 210 | Qt::TransformationMode mode = Qt::FastTransformation) const |
| 211 | { return scaled(s: QSize(w, h), aspectMode, mode); } |
| 212 | [[nodiscard]] QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio, |
| 213 | Qt::TransformationMode mode = Qt::FastTransformation) const; |
| 214 | [[nodiscard]] QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const; |
| 215 | [[nodiscard]] QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const; |
| 216 | [[nodiscard]] QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const; |
| 217 | static QTransform trueMatrix(const QTransform &, int w, int h); |
| 218 | |
| 219 | [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) const & |
| 220 | { return mirrored_helper(horizontal: horizontally, vertical: vertically); } |
| 221 | [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) && |
| 222 | { mirrored_inplace(horizontal: horizontally, vertical: vertically); return std::move(*this); } |
| 223 | [[nodiscard]] QImage rgbSwapped() const & |
| 224 | { return rgbSwapped_helper(); } |
| 225 | [[nodiscard]] QImage rgbSwapped() && |
| 226 | { rgbSwapped_inplace(); return std::move(*this); } |
| 227 | void mirror(bool horizontally = false, bool vertically = true) |
| 228 | { mirrored_inplace(horizontal: horizontally, vertical: vertically); } |
| 229 | void rgbSwap() |
| 230 | { rgbSwapped_inplace(); } |
| 231 | void invertPixels(InvertMode = InvertRgb); |
| 232 | |
| 233 | QColorSpace colorSpace() const; |
| 234 | [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &colorSpace) const; |
| 235 | [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &colorSpace, QImage::Format format, |
| 236 | Qt::ImageConversionFlags flags = Qt::AutoColor) const &; |
| 237 | [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &colorSpace, QImage::Format format, |
| 238 | Qt::ImageConversionFlags flags = Qt::AutoColor) &&; |
| 239 | void convertToColorSpace(const QColorSpace &colorSpace); |
| 240 | void convertToColorSpace(const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor); |
| 241 | void setColorSpace(const QColorSpace &colorSpace); |
| 242 | |
| 243 | QImage colorTransformed(const QColorTransform &transform) const &; |
| 244 | QImage colorTransformed(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) const &; |
| 245 | QImage colorTransformed(const QColorTransform &transform) &&; |
| 246 | QImage colorTransformed(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) &&; |
| 247 | void applyColorTransform(const QColorTransform &transform); |
| 248 | void applyColorTransform(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor); |
| 249 | |
| 250 | bool load(QIODevice *device, const char *format); |
| 251 | bool load(const QString &fileName, const char *format = nullptr); |
| 252 | bool loadFromData(QByteArrayView data, const char *format = nullptr); |
| 253 | bool loadFromData(const uchar *buf, int len, const char *format = nullptr); // ### Qt 7: qsizetype |
| 254 | bool loadFromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop |
| 255 | { return loadFromData(data: QByteArrayView(data), format); } |
| 256 | |
| 257 | bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const; |
| 258 | bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const; |
| 259 | |
| 260 | static QImage fromData(QByteArrayView data, const char *format = nullptr); |
| 261 | static QImage fromData(const uchar *data, int size, const char *format = nullptr); // ### Qt 7: qsizetype |
| 262 | static QImage fromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop |
| 263 | { return fromData(data: QByteArrayView(data), format); } |
| 264 | |
| 265 | qint64 cacheKey() const; |
| 266 | |
| 267 | QPaintEngine *paintEngine() const override; |
| 268 | |
| 269 | // Auxiliary data |
| 270 | int dotsPerMeterX() const; |
| 271 | int dotsPerMeterY() const; |
| 272 | void setDotsPerMeterX(int); |
| 273 | void setDotsPerMeterY(int); |
| 274 | QPoint offset() const; |
| 275 | void setOffset(const QPoint&); |
| 276 | |
| 277 | QStringList textKeys() const; |
| 278 | QString text(const QString &key = QString()) const; |
| 279 | void setText(const QString &key, const QString &value); |
| 280 | |
| 281 | QPixelFormat pixelFormat() const noexcept; |
| 282 | static QPixelFormat toPixelFormat(QImage::Format format) noexcept; |
| 283 | static QImage::Format toImageFormat(QPixelFormat format) noexcept; |
| 284 | |
| 285 | // Platform specific conversion functions |
| 286 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 287 | CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED; |
| 288 | #endif |
| 289 | #if defined(Q_OS_WIN) || defined(Q_QDOC) |
| 290 | HBITMAP toHBITMAP() const; |
| 291 | HICON toHICON(const QImage &mask = {}) const; |
| 292 | static QImage fromHBITMAP(HBITMAP hbitmap); |
| 293 | static QImage fromHICON(HICON icon); |
| 294 | #endif |
| 295 | |
| 296 | protected: |
| 297 | virtual int metric(PaintDeviceMetric metric) const override; |
| 298 | QImage mirrored_helper(bool horizontal, bool vertical) const; |
| 299 | QImage rgbSwapped_helper() const; |
| 300 | void mirrored_inplace(bool horizontal, bool vertical); |
| 301 | void rgbSwapped_inplace(); |
| 302 | QImage convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const; |
| 303 | bool convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags); |
| 304 | QImage smoothScaled(int w, int h) const; |
| 305 | |
| 306 | void detachMetadata(bool invalidateCache = false); |
| 307 | |
| 308 | private: |
| 309 | QImageData *d; |
| 310 | |
| 311 | friend class QRasterPlatformPixmap; |
| 312 | friend class QBlittablePlatformPixmap; |
| 313 | friend class QPixmapCacheEntry; |
| 314 | friend struct QImageData; |
| 315 | |
| 316 | public: |
| 317 | typedef QImageData * DataPtr; |
| 318 | inline DataPtr &data_ptr() { return d; } |
| 319 | }; |
| 320 | |
| 321 | Q_DECLARE_SHARED(QImage) |
| 322 | |
| 323 | // Inline functions... |
| 324 | |
| 325 | inline bool QImage::valid(const QPoint &pt) const { return valid(x: pt.x(), y: pt.y()); } |
| 326 | inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(x: pt.x(), y: pt.y());} |
| 327 | inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(x: pt.x(), y: pt.y()); } |
| 328 | inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(x: pt.x(), y: pt.y(), index_or_rgb); } |
| 329 | inline QColor QImage::pixelColor(const QPoint &pt) const { return pixelColor(x: pt.x(), y: pt.y()); } |
| 330 | inline void QImage::setPixelColor(const QPoint &pt, const QColor &c) { setPixelColor(x: pt.x(), y: pt.y(), c); } |
| 331 | |
| 332 | // QImage stream functions |
| 333 | |
| 334 | #if !defined(QT_NO_DATASTREAM) |
| 335 | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &); |
| 336 | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &); |
| 337 | #endif |
| 338 | |
| 339 | #ifndef QT_NO_DEBUG_STREAM |
| 340 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &); |
| 341 | #endif |
| 342 | |
| 343 | |
| 344 | QT_END_NAMESPACE |
| 345 | |
| 346 | #endif // QIMAGE_H |
| 347 | |