File indexing completed on 2024-12-22 04:15:50

0001 /*
0002  *  SPDX-FileCopyrightText: 2005 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_png_export.h"
0008 
0009 #include <QCheckBox>
0010 #include <QSlider>
0011 #include <QApplication>
0012 
0013 #include <kpluginfactory.h>
0014 
0015 #include <KoColorSpace.h>
0016 #include <KisImportExportManager.h>
0017 #include <KisImportExportErrorCode.h>
0018 #include <KoColorProfile.h>
0019 #include <KoColorModelStandardIds.h>
0020 #include <KoColorSpaceRegistry.h>
0021 
0022 #include <KisExportCheckRegistry.h>
0023 
0024 #include <kis_properties_configuration.h>
0025 #include <kis_paint_device.h>
0026 #include <KisDocument.h>
0027 #include <kis_image.h>
0028 #include <kis_paint_layer.h>
0029 #include <kis_group_layer.h>
0030 #include <kis_config.h>
0031 #include <kis_meta_data_store.h>
0032 #include <kis_meta_data_filter_registry_model.h>
0033 #include <kis_exif_info_visitor.h>
0034 #include "kis_png_converter.h"
0035 #include <kis_iterator_ng.h>
0036 
0037 K_PLUGIN_FACTORY_WITH_JSON(KisPNGExportFactory, "krita_png_export.json", registerPlugin<KisPNGExport>();)
0038 
0039 KisPNGExport::KisPNGExport(QObject *parent, const QVariantList &) : KisImportExportFilter(parent)
0040 {
0041 }
0042 
0043 KisPNGExport::~KisPNGExport()
0044 {
0045 }
0046 
0047 KisImportExportErrorCode KisPNGExport::convert(KisDocument *document, QIODevice *io,  KisPropertiesConfigurationSP configuration)
0048 {
0049     KisImageSP image = document->savingImage();
0050 
0051     KisPNGOptions options;
0052 
0053     options.alpha = configuration->getBool("alpha", true);
0054     options.interlace = configuration->getBool("interlaced", false);
0055     options.compression = configuration->getInt("compression", 3);
0056     options.tryToSaveAsIndexed = configuration->getBool("indexed", false);
0057     KoColor c(KoColorSpaceRegistry::instance()->rgb8());
0058     c.fromQColor(Qt::white);
0059     options.transparencyFillColor = configuration->getColor("transparencyFillcolor", c).toQColor();
0060     options.saveSRGBProfile = configuration->getBool("saveSRGBProfile", false);
0061     options.forceSRGB = configuration->getBool("forceSRGB", true);
0062     options.storeAuthor = configuration->getBool("storeAuthor", false);
0063     options.storeMetaData = configuration->getBool("storeMetaData", false);
0064     options.saveAsHDR = configuration->getBool("saveAsHDR", false);
0065     options.downsample = configuration->getBool("downsample", false);
0066 
0067     vKisAnnotationSP_it beginIt = image->beginAnnotations();
0068     vKisAnnotationSP_it endIt = image->endAnnotations();
0069 
0070     KisExifInfoVisitor eIV;
0071     eIV.visit(image->rootLayer().data());
0072     KisMetaData::Store *eI = 0;
0073     if (eIV.metaDataCount() == 1) {
0074         eI = eIV.exifInfo();
0075     }
0076     if (eI) {
0077         KisMetaData::Store* copy = new KisMetaData::Store(*eI);
0078         eI = copy;
0079     }
0080 
0081     KisPNGConverter pngConverter(document);
0082 
0083     KisImportExportErrorCode res = pngConverter.buildFile(io, image->bounds(), image->xRes(), image->yRes(), image->projection(), beginIt, endIt, options, eI);
0084     delete eI;
0085     dbgFile << " Result =" << res;
0086     return res;
0087 }
0088 
0089 KisPropertiesConfigurationSP KisPNGExport::defaultConfiguration(const QByteArray &, const QByteArray &) const
0090 {
0091     KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
0092     cfg->setProperty("alpha", true);
0093     cfg->setProperty("indexed", false);
0094     cfg->setProperty("compression", 3);
0095     cfg->setProperty("interlaced", false);
0096 
0097     KoColor fill_color(KoColorSpaceRegistry::instance()->rgb8());
0098     fill_color = KoColor();
0099     fill_color.fromQColor(Qt::white);
0100     QVariant v;
0101     v.setValue(fill_color);
0102 
0103     cfg->setProperty("transparencyFillcolor", v);
0104     cfg->setProperty("saveSRGBProfile", false);
0105     cfg->setProperty("forceSRGB", true);
0106     cfg->setProperty("saveAsHDR", false);
0107     cfg->setProperty("storeMetaData", false);
0108     cfg->setProperty("storeAuthor", false);
0109     cfg->setProperty("downsample", false);
0110     return cfg;
0111 }
0112 
0113 KisConfigWidget *KisPNGExport::createConfigurationWidget(QWidget *parent, const QByteArray &, const QByteArray &) const
0114 {
0115     return new KisWdgOptionsPNG(parent);
0116 }
0117 
0118 void KisPNGExport::initializeCapabilities()
0119 {
0120     addCapability(KisExportCheckRegistry::instance()->get("sRGBProfileCheck")->create(KisExportCheckBase::SUPPORTED));
0121     QList<QPair<KoID, KoID> > supportedColorModels;
0122     supportedColorModels << QPair<KoID, KoID>()
0123             << QPair<KoID, KoID>(RGBAColorModelID, Integer8BitsColorDepthID)
0124             << QPair<KoID, KoID>(RGBAColorModelID, Integer16BitsColorDepthID)
0125             << QPair<KoID, KoID>(GrayAColorModelID, Integer8BitsColorDepthID)
0126             << QPair<KoID, KoID>(GrayAColorModelID, Integer16BitsColorDepthID);
0127     addSupportedColorModels(supportedColorModels, "PNG");
0128 }
0129 
0130 KisWdgOptionsPNG::KisWdgOptionsPNG(QWidget *parent)
0131     : KisConfigWidget(parent)
0132 {
0133     setupUi(this);
0134 
0135     connect(chkSaveAsHDR, SIGNAL(toggled(bool)), this, SLOT(slotUseHDRChanged(bool)));
0136 }
0137 
0138 void KisWdgOptionsPNG::setConfiguration(const KisPropertiesConfigurationSP cfg)
0139 {
0140     // the export manager should have prepared some info for us!
0141     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::ImageContainsTransparencyTag));
0142     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::ColorModelIDTag));
0143     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::sRGBTag));
0144 
0145     const bool isThereAlpha = cfg->getBool(KisImportExportFilter::ImageContainsTransparencyTag);
0146 
0147     alpha->setChecked(cfg->getBool("alpha", isThereAlpha));
0148 
0149     bnTransparencyFillColor->setEnabled(!alpha->isChecked());
0150 
0151     if (cfg->getString(KisImportExportFilter::ColorModelIDTag) == RGBAColorModelID.id()) {
0152         tryToSaveAsIndexed->setVisible(true);
0153         if (alpha->isChecked()) {
0154             tryToSaveAsIndexed->setChecked(false);
0155         }
0156         else {
0157             tryToSaveAsIndexed->setChecked(cfg->getBool("indexed", false));
0158         }
0159     }
0160     else {
0161         tryToSaveAsIndexed->setVisible(false);
0162     }
0163     interlacing->setChecked(cfg->getBool("interlaced", false));
0164     compressionLevel->setValue(cfg->getInt("compression", 3));
0165     compressionLevel->setRange(1, 9, 0);
0166 
0167     tryToSaveAsIndexed->setVisible(!isThereAlpha);
0168 
0169     //const bool sRGB = cfg->getBool(KisImportExportFilter::sRGBTag, false);
0170 
0171     //chkSRGB->setEnabled(sRGB);
0172     chkSRGB->setChecked(cfg->getBool("saveSRGBProfile", true));
0173 
0174     //chkForceSRGB->setEnabled(!sRGB);
0175     chkForceSRGB->setChecked(cfg->getBool("forceSRGB", false));
0176 
0177     chkSaveAsHDR->setChecked(cfg->getBool("saveAsHDR", false));
0178     slotUseHDRChanged(chkSaveAsHDR->isChecked());
0179 
0180     chkAuthor->setChecked(cfg->getBool("storeAuthor", false));
0181     chkMetaData->setChecked(cfg->getBool("storeMetaData", false));
0182 
0183     KoColor background(KoColorSpaceRegistry::instance()->rgb8());
0184     background.fromQColor(Qt::white);
0185     bnTransparencyFillColor->setDefaultColor(background);
0186     bnTransparencyFillColor->setColor(cfg->getColor("transparencyFillcolor", background));
0187 
0188     chkDownsample->setChecked(cfg->getBool("downsample", false));
0189 }
0190 
0191 KisPropertiesConfigurationSP KisWdgOptionsPNG::configuration() const
0192 {
0193 
0194     KisPropertiesConfigurationSP cfg(new KisPropertiesConfiguration());
0195 
0196     bool alpha = this->alpha->isChecked();
0197     bool interlace = interlacing->isChecked();
0198     int compression = (int)compressionLevel->value();
0199     bool saveAsHDR = chkSaveAsHDR->isChecked();
0200     bool tryToSaveAsIndexed = !saveAsHDR && this->tryToSaveAsIndexed->isChecked();
0201     bool saveSRGB = !saveAsHDR && chkSRGB->isChecked();
0202     bool forceSRGB = !saveAsHDR && chkForceSRGB->isChecked();
0203     bool storeAuthor = chkAuthor->isChecked();
0204     bool storeMetaData = chkMetaData->isChecked();
0205     bool downsample = chkDownsample->isChecked();
0206 
0207 
0208     QVariant transparencyFillcolor;
0209     transparencyFillcolor.setValue(bnTransparencyFillColor->color());
0210 
0211     cfg->setProperty("alpha", alpha);
0212     cfg->setProperty("indexed", tryToSaveAsIndexed);
0213     cfg->setProperty("compression", compression);
0214     cfg->setProperty("interlaced", interlace);
0215     cfg->setProperty("transparencyFillcolor", transparencyFillcolor);
0216     cfg->setProperty("saveAsHDR", saveAsHDR);
0217     cfg->setProperty("saveSRGBProfile", saveSRGB);
0218     cfg->setProperty("forceSRGB", forceSRGB);
0219     cfg->setProperty("storeAuthor", storeAuthor);
0220     cfg->setProperty("storeMetaData", storeMetaData);
0221     cfg->setProperty("downsample", downsample);
0222     return cfg;
0223 }
0224 
0225 void KisWdgOptionsPNG::on_alpha_toggled(bool checked)
0226 {
0227     bnTransparencyFillColor->setEnabled(!checked);
0228 }
0229 
0230 void KisWdgOptionsPNG::slotUseHDRChanged(bool value)
0231 {
0232     tryToSaveAsIndexed->setDisabled(value);
0233     chkForceSRGB->setDisabled(value);
0234     chkSRGB->setDisabled(value);
0235 }
0236 
0237 #include "kis_png_export.moc"
0238