| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2023 Mirco Miranda <mircomir@outlook.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef SCANLINECONVERTER_P_H |
| 8 | #define SCANLINECONVERTER_P_H |
| 9 | |
| 10 | #include <QColorSpace> |
| 11 | #include <QImage> |
| 12 | |
| 13 | /*! |
| 14 | * \brief The scanlineFormatConversion class |
| 15 | * A class to convert an image scan line. It introduces some overhead on small images |
| 16 | * but performs better on large images. :) |
| 17 | */ |
| 18 | class ScanLineConverter |
| 19 | { |
| 20 | public: |
| 21 | ScanLineConverter(const QImage::Format &targetFormat); |
| 22 | ScanLineConverter(const ScanLineConverter &other); |
| 23 | ScanLineConverter &operator=(const ScanLineConverter &other); |
| 24 | |
| 25 | /*! |
| 26 | * \brief targetFormat |
| 27 | * \return The target format set in the constructor. |
| 28 | */ |
| 29 | QImage::Format targetFormat() const; |
| 30 | |
| 31 | /*! |
| 32 | * \brief setTargetColorSpace |
| 33 | * Set the colorspace conversion. |
| 34 | * |
| 35 | * In addition to format conversion, it is also possible to convert the color |
| 36 | * space if the source image has a different one set. |
| 37 | * The conversion is done on the source format if and only if the image |
| 38 | * has a color depth greater than 24 bit and the color profile set is different |
| 39 | * from that of the image itself. |
| 40 | * \param colorSpace |
| 41 | */ |
| 42 | void setTargetColorSpace(const QColorSpace &colorSpace); |
| 43 | QColorSpace targetColorSpace() const; |
| 44 | |
| 45 | /*! |
| 46 | * \brief setDefaultSourceColorSpace |
| 47 | * Set the source color space to use when the image does not have a color space. |
| 48 | * \param colorSpace |
| 49 | */ |
| 50 | void setDefaultSourceColorSpace(const QColorSpace &colorSpace); |
| 51 | QColorSpace defaultSourceColorSpace() const; |
| 52 | |
| 53 | /*! |
| 54 | * \brief convertedScanLine |
| 55 | * Convert the scanline \a y. |
| 56 | * \note If the image format (and color space) is the same of converted format, it returns the image scan line. |
| 57 | * \return The scan line converted. |
| 58 | */ |
| 59 | const uchar *convertedScanLine(const QImage &image, qint32 y); |
| 60 | |
| 61 | /*! |
| 62 | * \brief bytesPerLine |
| 63 | * \return The size of the last converted scanline. |
| 64 | */ |
| 65 | qsizetype bytesPerLine() const; |
| 66 | |
| 67 | /*! |
| 68 | * \brief isColorSpaceConversionNeeded |
| 69 | * Calculates if a color space conversion is needed. |
| 70 | * \note Only 24 bit or grater images. |
| 71 | * \param image The source image. |
| 72 | * \param targetColorSpace The target color space. |
| 73 | * \param defaultColorSpace The default color space to use it image does not contains one. |
| 74 | * \return True if the conversion should be done otherwise false. |
| 75 | */ |
| 76 | static bool isColorSpaceConversionNeeded(const QImage &image, const QColorSpace &targetColorSpace, const QColorSpace &defaultColorSpace = QColorSpace()); |
| 77 | |
| 78 | /*! |
| 79 | * \brief isColorSpaceConversionNeeded |
| 80 | */ |
| 81 | inline bool isColorSpaceConversionNeeded(const QImage &image) const |
| 82 | { |
| 83 | return isColorSpaceConversionNeeded(image, targetColorSpace: _colorSpace, defaultColorSpace: _defaultColorSpace); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | // data |
| 88 | QImage::Format _targetFormat; |
| 89 | QColorSpace _colorSpace; |
| 90 | QColorSpace _defaultColorSpace; |
| 91 | |
| 92 | // internal buffers |
| 93 | QImage _tmpBuffer; |
| 94 | QImage _convBuffer; |
| 95 | }; |
| 96 | |
| 97 | #endif // SCANLINECONVERTER_P_H |
| 98 | |