1 /*
2  *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "kis_png_export.h"
21 
22 #include <QCheckBox>
23 #include <QSlider>
24 #include <QApplication>
25 
26 #include <kpluginfactory.h>
27 
28 #include <KoColorSpace.h>
29 #include <KisImportExportManager.h>
30 #include <KisImportExportErrorCode.h>
31 #include <KoColorProfile.h>
32 #include <KoColorModelStandardIds.h>
33 #include <KoColorSpaceRegistry.h>
34 
35 #include <KisExportCheckRegistry.h>
36 
37 #include <kis_properties_configuration.h>
38 #include <kis_paint_device.h>
39 #include <KisDocument.h>
40 #include <kis_image.h>
41 #include <kis_paint_layer.h>
42 #include <kis_group_layer.h>
43 #include <kis_config.h>
44 #include <kis_meta_data_store.h>
45 #include <kis_meta_data_filter_registry_model.h>
46 #include <kis_exif_info_visitor.h>
47 #include "kis_png_converter.h"
48 #include <kis_iterator_ng.h>
49 
50 K_PLUGIN_FACTORY_WITH_JSON(KisPNGExportFactory, "krita_png_export.json", registerPlugin<KisPNGExport>();)
51 
KisPNGExport(QObject * parent,const QVariantList &)52 KisPNGExport::KisPNGExport(QObject *parent, const QVariantList &) : KisImportExportFilter(parent)
53 {
54 }
55 
~KisPNGExport()56 KisPNGExport::~KisPNGExport()
57 {
58 }
59 
convert(KisDocument * document,QIODevice * io,KisPropertiesConfigurationSP configuration)60 KisImportExportErrorCode KisPNGExport::convert(KisDocument *document, QIODevice *io,  KisPropertiesConfigurationSP configuration)
61 {
62     KisImageSP image = document->savingImage();
63 
64     KisPNGOptions options;
65 
66     options.alpha = configuration->getBool("alpha", true);
67     options.interlace = configuration->getBool("interlaced", false);
68     options.compression = configuration->getInt("compression", 3);
69     options.tryToSaveAsIndexed = configuration->getBool("indexed", false);
70     KoColor c(KoColorSpaceRegistry::instance()->rgb8());
71     c.fromQColor(Qt::white);
72     options.transparencyFillColor = configuration->getColor("transparencyFillcolor", c).toQColor();
73     options.saveSRGBProfile = configuration->getBool("saveSRGBProfile", false);
74     options.forceSRGB = configuration->getBool("forceSRGB", true);
75     options.storeAuthor = configuration->getBool("storeAuthor", false);
76     options.storeMetaData = configuration->getBool("storeMetaData", false);
77     options.saveAsHDR = configuration->getBool("saveAsHDR", false);
78 
79     vKisAnnotationSP_it beginIt = image->beginAnnotations();
80     vKisAnnotationSP_it endIt = image->endAnnotations();
81 
82     KisExifInfoVisitor eIV;
83     eIV.visit(image->rootLayer().data());
84     KisMetaData::Store *eI = 0;
85     if (eIV.metaDataCount() == 1) {
86         eI = eIV.exifInfo();
87     }
88     if (eI) {
89         KisMetaData::Store* copy = new KisMetaData::Store(*eI);
90         eI = copy;
91     }
92 
93     KisPNGConverter pngConverter(document);
94 
95     KisImportExportErrorCode res = pngConverter.buildFile(io, image->bounds(), image->xRes(), image->yRes(), image->projection(), beginIt, endIt, options, eI);
96     delete eI;
97     dbgFile << " Result =" << res;
98     return res;
99 }
100 
defaultConfiguration(const QByteArray &,const QByteArray &) const101 KisPropertiesConfigurationSP KisPNGExport::defaultConfiguration(const QByteArray &, const QByteArray &) const
102 {
103     KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
104     cfg->setProperty("alpha", true);
105     cfg->setProperty("indexed", false);
106     cfg->setProperty("compression", 3);
107     cfg->setProperty("interlaced", false);
108 
109     KoColor fill_color(KoColorSpaceRegistry::instance()->rgb8());
110     fill_color = KoColor();
111     fill_color.fromQColor(Qt::white);
112     QVariant v;
113     v.setValue(fill_color);
114 
115     cfg->setProperty("transparencyFillcolor", v);
116     cfg->setProperty("saveSRGBProfile", false);
117     cfg->setProperty("forceSRGB", true);
118     cfg->setProperty("saveAsHDR", false);
119     cfg->setProperty("storeMetaData", false);
120     cfg->setProperty("storeAuthor", false);
121 
122     return cfg;
123 }
124 
createConfigurationWidget(QWidget * parent,const QByteArray &,const QByteArray &) const125 KisConfigWidget *KisPNGExport::createConfigurationWidget(QWidget *parent, const QByteArray &, const QByteArray &) const
126 {
127     return new KisWdgOptionsPNG(parent);
128 }
129 
initializeCapabilities()130 void KisPNGExport::initializeCapabilities()
131 {
132     addCapability(KisExportCheckRegistry::instance()->get("sRGBProfileCheck")->create(KisExportCheckBase::SUPPORTED));
133     QList<QPair<KoID, KoID> > supportedColorModels;
134     supportedColorModels << QPair<KoID, KoID>()
135             << QPair<KoID, KoID>(RGBAColorModelID, Integer8BitsColorDepthID)
136             << QPair<KoID, KoID>(RGBAColorModelID, Integer16BitsColorDepthID)
137             << QPair<KoID, KoID>(GrayAColorModelID, Integer8BitsColorDepthID)
138             << QPair<KoID, KoID>(GrayAColorModelID, Integer16BitsColorDepthID);
139     addSupportedColorModels(supportedColorModels, "PNG");
140 }
141 
KisWdgOptionsPNG(QWidget * parent)142 KisWdgOptionsPNG::KisWdgOptionsPNG(QWidget *parent)
143     : KisConfigWidget(parent)
144 {
145     setupUi(this);
146 
147     connect(chkSaveAsHDR, SIGNAL(toggled(bool)), this, SLOT(slotUseHDRChanged(bool)));
148 }
149 
setConfiguration(const KisPropertiesConfigurationSP cfg)150 void KisWdgOptionsPNG::setConfiguration(const KisPropertiesConfigurationSP cfg)
151 {
152     // the export manager should have prepared some info for us!
153     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::ImageContainsTransparencyTag));
154     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::ColorModelIDTag));
155     KIS_SAFE_ASSERT_RECOVER_NOOP(cfg->hasProperty(KisImportExportFilter::sRGBTag));
156 
157     const bool isThereAlpha = cfg->getBool(KisImportExportFilter::ImageContainsTransparencyTag);
158 
159     alpha->setChecked(cfg->getBool("alpha", isThereAlpha));
160 
161     bnTransparencyFillColor->setEnabled(!alpha->isChecked());
162 
163     if (cfg->getString(KisImportExportFilter::ColorModelIDTag) == RGBAColorModelID.id()) {
164         tryToSaveAsIndexed->setVisible(true);
165         if (alpha->isChecked()) {
166             tryToSaveAsIndexed->setChecked(false);
167         }
168         else {
169             tryToSaveAsIndexed->setChecked(cfg->getBool("indexed", false));
170         }
171     }
172     else {
173         tryToSaveAsIndexed->setVisible(false);
174     }
175     interlacing->setChecked(cfg->getBool("interlaced", false));
176     compressionLevel->setValue(cfg->getInt("compression", 3));
177     compressionLevel->setRange(1, 9, 0);
178 
179     tryToSaveAsIndexed->setVisible(!isThereAlpha);
180 
181     //const bool sRGB = cfg->getBool(KisImportExportFilter::sRGBTag, false);
182 
183     //chkSRGB->setEnabled(sRGB);
184     chkSRGB->setChecked(cfg->getBool("saveSRGBProfile", true));
185 
186     //chkForceSRGB->setEnabled(!sRGB);
187     chkForceSRGB->setChecked(cfg->getBool("forceSRGB", false));
188 
189     chkSaveAsHDR->setChecked(cfg->getBool("saveAsHDR", false));
190     slotUseHDRChanged(chkSaveAsHDR->isChecked());
191 
192     chkAuthor->setChecked(cfg->getBool("storeAuthor", false));
193     chkMetaData->setChecked(cfg->getBool("storeMetaData", false));
194 
195     KoColor background(KoColorSpaceRegistry::instance()->rgb8());
196     background.fromQColor(Qt::white);
197     bnTransparencyFillColor->setDefaultColor(background);
198     bnTransparencyFillColor->setColor(cfg->getColor("transparencyFillcolor", background));
199 }
200 
configuration() const201 KisPropertiesConfigurationSP KisWdgOptionsPNG::configuration() const
202 {
203 
204     KisPropertiesConfigurationSP cfg(new KisPropertiesConfiguration());
205 
206     bool alpha = this->alpha->isChecked();
207     bool interlace = interlacing->isChecked();
208     int compression = (int)compressionLevel->value();
209     bool saveAsHDR = chkSaveAsHDR->isChecked();
210     bool tryToSaveAsIndexed = !saveAsHDR && this->tryToSaveAsIndexed->isChecked();
211     bool saveSRGB = !saveAsHDR && chkSRGB->isChecked();
212     bool forceSRGB = !saveAsHDR && chkForceSRGB->isChecked();
213     bool storeAuthor = chkAuthor->isChecked();
214     bool storeMetaData = chkMetaData->isChecked();
215 
216 
217     QVariant transparencyFillcolor;
218     transparencyFillcolor.setValue(bnTransparencyFillColor->color());
219 
220     cfg->setProperty("alpha", alpha);
221     cfg->setProperty("indexed", tryToSaveAsIndexed);
222     cfg->setProperty("compression", compression);
223     cfg->setProperty("interlaced", interlace);
224     cfg->setProperty("transparencyFillcolor", transparencyFillcolor);
225     cfg->setProperty("saveAsHDR", saveAsHDR);
226     cfg->setProperty("saveSRGBProfile", saveSRGB);
227     cfg->setProperty("forceSRGB", forceSRGB);
228     cfg->setProperty("storeAuthor", storeAuthor);
229     cfg->setProperty("storeMetaData", storeMetaData);
230 
231     return cfg;
232 }
233 
on_alpha_toggled(bool checked)234 void KisWdgOptionsPNG::on_alpha_toggled(bool checked)
235 {
236     bnTransparencyFillColor->setEnabled(!checked);
237 }
238 
slotUseHDRChanged(bool value)239 void KisWdgOptionsPNG::slotUseHDRChanged(bool value)
240 {
241     tryToSaveAsIndexed->setDisabled(value);
242     chkForceSRGB->setDisabled(value);
243     chkSRGB->setDisabled(value);
244 }
245 
246 #include "kis_png_export.moc"
247 
248