File indexing completed on 2024-05-12 16:01:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2005, 2007 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef _KIS_PNG_CONVERTER_H_
0008 #define _KIS_PNG_CONVERTER_H_
0009 
0010 #include <png.h>
0011 
0012 #include <QColor>
0013 #include <QVector>
0014 
0015 #include "kis_types.h"
0016 #include "kis_global.h"
0017 #include "kis_annotation.h"
0018 #include <kritaui_export.h>
0019 #include <KisImportExportErrorCode.h>
0020 
0021 class KoStore;
0022 class KisDocument;
0023 class KoColorSpace;
0024 
0025 
0026 namespace KisMetaData
0027 {
0028 class Filter;
0029 class Store;
0030 }
0031 
0032 struct KisPNGOptions {
0033     KisPNGOptions()
0034         : compression(0)
0035         , interlace(false)
0036         , alpha(true)
0037         , exif(true)
0038         , iptc(true)
0039         , xmp(true)
0040         , tryToSaveAsIndexed(true)
0041         , saveSRGBProfile(false)
0042         , forceSRGB(false)
0043         , storeMetaData(false)
0044         , storeAuthor(false)
0045         , saveAsHDR(false)
0046         , transparencyFillColor(Qt::white)
0047         , downsample(false)
0048     {}
0049 
0050     int compression;
0051     bool interlace;
0052     bool alpha;
0053     bool exif;
0054     bool iptc;
0055     bool xmp;
0056     bool tryToSaveAsIndexed;
0057     bool saveSRGBProfile;
0058     bool forceSRGB;
0059     bool storeMetaData;
0060     bool storeAuthor;
0061     bool saveAsHDR;
0062     QList<const KisMetaData::Filter*> filters;
0063     QColor transparencyFillColor;
0064     bool downsample; // Converts to 8 bit on export
0065 };
0066 
0067 /**
0068  * This class allows to import/export a PNG from either a file or a QIODevice.
0069  */
0070 // XXX_PROGRESS (pass KoUpdater to the png converter)
0071 class KRITAUI_EXPORT KisPNGConverter : public QObject
0072 {
0073     Q_OBJECT
0074 public:
0075     /**
0076      * Initialize the converter.
0077      * @param doc the KisDocument related to the image, can be null if you don't have a KisDocument
0078      * @param batchMode whether to use the batch mode
0079      */
0080     KisPNGConverter(KisDocument *doc, bool batchMode = false);
0081     ~KisPNGConverter() override;
0082 public:
0083     /**
0084      * Load an image from an URL. If the image is not on a local drive, the image is first downloaded to a
0085      * temporary location.
0086      * @param filename the file name of the image
0087      */
0088     KisImportExportErrorCode buildImage(const QString &filename);
0089     /**
0090      * Load an image from a QIODevice.
0091      * @param iod device to access the data
0092      */
0093     KisImportExportErrorCode buildImage(QIODevice* iod);
0094     /**
0095      * Save a layer to a PNG
0096      * @param filename the name of the destination file
0097      * @param imageRect the image rectangle to save
0098      * @param xRes resolution along x axis
0099      * @param yRes resolution along y axis
0100      * @param device the paint device to save
0101      * @param annotationsStart an iterator on the first annotation
0102      * @param annotationsEnd an iterator on the last annotation
0103      * @param options PNG formatting options
0104      * @param metaData image metadata
0105      */
0106     KisImportExportErrorCode buildFile(const QString &filename, const QRect &imageRect, const qreal xRes, const qreal yRes, KisPaintDeviceSP device, vKisAnnotationSP_it annotationsStart, vKisAnnotationSP_it annotationsEnd, KisPNGOptions options, KisMetaData::Store* metaData);
0107     KisImportExportErrorCode buildFile(QIODevice*, const QRect &imageRect, const qreal xRes, const qreal yRes, KisPaintDeviceSP device, vKisAnnotationSP_it annotationsStart, vKisAnnotationSP_it annotationsEnd, KisPNGOptions options, KisMetaData::Store* metaData);
0108     /**
0109      * Retrieve the constructed image
0110      */
0111     KisImageSP image();
0112 
0113     /**
0114      * @brief saveDeviceToStore saves the given paint device to the KoStore. If the device is not 8 bits sRGB, it will be converted to 8 bits sRGB.
0115      * @return true if the saving succeeds
0116      */
0117     static bool saveDeviceToStore(const QString &filename, const QRect &imageRect, const qreal xRes, const qreal yRes, KisPaintDeviceSP dev, KoStore *store, KisMetaData::Store* metaData = 0);
0118 
0119     static bool isColorSpaceSupported(const KoColorSpace *cs);
0120 
0121 public Q_SLOTS:
0122     virtual void cancel();
0123 private:
0124     void progress(png_structp png_ptr, png_uint_32 row_number, int pass);
0125 private:
0126     png_uint_32 m_max_row;
0127     KisImageSP m_image;
0128     KisDocument *m_doc;
0129     bool m_stop;
0130     bool m_batchMode;
0131     QString m_path;
0132 };
0133 
0134 #endif