Commit 1f203162 authored by Victor Wåhlström's avatar Victor Wåhlström

Added optional success state bool to KoColor::fromXML() to improve handling of bad XML.

PNG/JPEG config property "transparencyFillcolor" now stores KoColor instead of a comma separated string. This also fixes a potential crash due to bad data in config.

KisPropertiesConfiguration::getColor() will now return the provided default color if there are problems parsing the property XML.
parent 2bb8af16
......@@ -264,11 +264,14 @@ KoColor KisPropertiesConfiguration::getColor(const QString& name, const KoColor&
QDomDocument doc;
doc.setContent(v.toString());
QDomElement e = doc.documentElement().firstChild().toElement();
return KoColor::fromXML(e, Integer16BitsColorDepthID.id());
bool ok;
KoColor c = KoColor::fromXML(e, Integer16BitsColorDepthID.id(), &ok);
if (ok) {
return c;
}
}
} else {
return color;
}
return color;
}
void KisPropertiesConfiguration::dump() const
......
......@@ -283,8 +283,15 @@ qreal KoColor::opacityF() const
return d->colorSpace->opacityF(d->data);
}
KoColor KoColor::fromXML(const QDomElement& elt, const QString & bitDepthId)
KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId)
{
bool ok;
return fromXML(elt, bitDepthId, &ok);
}
KoColor KoColor::fromXML(const QDomElement& elt, const QString& bitDepthId, bool* ok)
{
*ok = true;
QString modelId;
if (elt.tagName() == "CMYK") {
modelId = CMYKAColorModelID.id();
......@@ -317,9 +324,11 @@ KoColor KoColor::fromXML(const QDomElement& elt, const QString & bitDepthId)
}
if (cs) {
KoColor c(cs);
// TODO: Provide a way for colorFromXML() to notify the caller if parsing failed. Currently it returns default values on failure.
cs->colorFromXML(c.data(), elt);
return c;
} else {
*ok = false;
return KoColor();
}
}
......
......@@ -87,7 +87,6 @@ public:
/// assign new profile without converting pixel data
void setProfile(const KoColorProfile *profile);
/// Replace the existing color data, and colorspace with the specified data.
/// The data is copied.
void setColor(const quint8 * data, const KoColorSpace * colorSpace = 0);
......@@ -157,6 +156,21 @@ public:
*/
static KoColor fromXML(const QDomElement& elt, const QString & bitDepthId);
/**
* Unserialize a color following Create's swatch color specification available
* at http://create.freedesktop.org/wiki/index.php/Swatches_-_colour_file_format
*
* @param elt the element to unserialize (<CMYK />, <sRGB />, <RGB />)
* @param bitDepthId the bit depth is unspecified by the spec, this allow to select
* a preferred bit depth for creating the KoColor object (if that
* bit depth isn't available, this function will randomly select
* an other bit depth)
* @param ok If a an error occurs, *ok is set to false; otherwise it's set to true
* @return the unserialize color, or an empty color object if the function failed
* to unserialize the color
*/
static KoColor fromXML(const QDomElement& elt, const QString & bitDepthId, bool* ok);
/**
* @brief toQString create a user-visible string of the channel names and the channel values
* @param color the color to create the string from
......
......@@ -85,8 +85,9 @@ KisImportExportFilter::ConversionStatus KisJPEGExport::convert(KisDocument *docu
options.exif = configuration->getBool("exif", true);
options.iptc = configuration->getBool("iptc", true);
options.xmp = configuration->getBool("xmp", true);
QStringList rgb = configuration->getString("transparencyFillcolor", "255,255,255").split(',');
options.transparencyFillColor = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt());
KoColor c(KoColorSpaceRegistry::instance()->rgb8());
c.fromQColor(Qt::white);
options.transparencyFillColor = configuration->getColor("transparencyFillcolor", c).toQColor();
KisMetaData::FilterRegistryModel m;
m.setEnabledFilters(configuration->getString("filters").split(","));
options.filters = m.enabledFilters();
......@@ -133,7 +134,14 @@ KisPropertiesConfigurationSP KisJPEGExport::defaultConfiguration(const QByteArra
cfg->setProperty("exif", true);
cfg->setProperty("iptc", true);
cfg->setProperty("xmp", true);
cfg->setProperty("transparencyFillcolor", QString("255,255,255"));
KoColor fill_color(KoColorSpaceRegistry::instance()->rgb8());
fill_color = KoColor();
fill_color.fromQColor(Qt::white);
QVariant v;
v.setValue(fill_color);
cfg->setProperty("transparencyFillcolor", v);
cfg->setProperty("filters", "");
return cfg;
......@@ -185,12 +193,10 @@ void KisWdgOptionsJPEG::setConfiguration(const KisPropertiesConfigurationSP cfg)
chkForceSRGB->setVisible(cfg->getBool("is_sRGB"));
chkForceSRGB->setChecked(cfg->getBool("forceSRGB", false));
chkSaveProfile->setChecked(cfg->getBool("saveProfile", true));
QStringList rgb = cfg->getString("transparencyFillcolor", "255,255,255").split(',');
KoColor background(KoColorSpaceRegistry::instance()->rgb8());
background.fromQColor(Qt::white);
bnTransparencyFillColor->setDefaultColor(background);
background.fromQColor(QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt()));
bnTransparencyFillColor->setColor(background);
bnTransparencyFillColor->setColor(cfg->getColor("transparencyFillcolor", background));
m_filterRegistryModel.setEnabledFilters(cfg->getString("filters").split(','));
......@@ -199,6 +205,10 @@ void KisWdgOptionsJPEG::setConfiguration(const KisPropertiesConfigurationSP cfg)
KisPropertiesConfigurationSP KisWdgOptionsJPEG::configuration() const
{
KisPropertiesConfigurationSP cfg = new KisPropertiesConfiguration();
QVariant transparencyFillcolor;
transparencyFillcolor.setValue(bnTransparencyFillColor->color());
cfg->setProperty("progressive", progressive->isChecked());
cfg->setProperty("quality", (int)qualityLevel->value());
cfg->setProperty("forceSRGB", chkForceSRGB->isChecked());
......@@ -210,8 +220,8 @@ KisPropertiesConfigurationSP KisWdgOptionsJPEG::configuration() const
cfg->setProperty("exif", exif->isChecked());
cfg->setProperty("iptc", iptc->isChecked());
cfg->setProperty("xmp", xmp->isChecked());
QColor c = bnTransparencyFillColor->color().toQColor();
cfg->setProperty("transparencyFillcolor", QString("%1,%2,%3").arg(c.red()).arg(c.green()).arg(c.blue()));
cfg->setProperty("transparencyFillcolor", transparencyFillcolor);
QString enabledFilters;
Q_FOREACH (const KisMetaData::Filter* filter, m_filterRegistryModel.enabledFilters()) {
enabledFilters = enabledFilters + filter->id() + ',';
......
......@@ -79,8 +79,9 @@ KisImportExportFilter::ConversionStatus KisPNGExport::convert(KisDocument *docum
options.interlace = configuration->getBool("interlaced", false);
options.compression = configuration->getInt("compression", 3);
options.tryToSaveAsIndexed = configuration->getBool("indexed", false);
QStringList rgb = configuration->getString("transparencyFillcolor", "255,255,255").split(',');
options.transparencyFillColor = QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt());
KoColor c(KoColorSpaceRegistry::instance()->rgb8());
c.fromQColor(Qt::white);
options.transparencyFillColor = configuration->getColor("transparencyFillcolor", c).toQColor();
options.saveSRGBProfile = configuration->getBool("saveSRGBProfile", false);
options.forceSRGB = configuration->getBool("forceSRGB", true);
......@@ -119,7 +120,14 @@ KisPropertiesConfigurationSP KisPNGExport::defaultConfiguration(const QByteArray
cfg->setProperty("indexed", false);
cfg->setProperty("compression", 3);
cfg->setProperty("interlaced", false);
cfg->setProperty("transparencyFillcolor", QString("255,255,255"));
KoColor fill_color(KoColorSpaceRegistry::instance()->rgb8());
fill_color = KoColor();
fill_color.fromQColor(Qt::white);
QVariant v;
v.setValue(fill_color);
cfg->setProperty("transparencyFillcolor", v);
cfg->setProperty("saveSRGBProfile", false);
cfg->setProperty("forceSRGB", true);
......@@ -171,7 +179,7 @@ void KisWdgOptionsPNG::setConfiguration(const KisPropertiesConfigurationSP cfg)
}
interlacing->setChecked(cfg->getBool("interlaced", false));
compressionLevel->setValue(cfg->getInt("compression", 3));
compressionLevel->setRange(1, 9 , 0);
compressionLevel->setRange(1, 9, 0);
tryToSaveAsIndexed->setVisible(!isThereAlpha);
......@@ -183,13 +191,10 @@ void KisWdgOptionsPNG::setConfiguration(const KisPropertiesConfigurationSP cfg)
chkForceSRGB->setEnabled(!sRGB);
chkForceSRGB->setChecked(cfg->getBool("forceSRGB", false));
QStringList rgb = cfg->getString("transparencyFillcolor", "255,255,255").split(',');
KoColor c(KoColorSpaceRegistry::instance()->rgb8());
c.fromQColor(Qt::white);
bnTransparencyFillColor->setDefaultColor(c);
c.fromQColor(QColor(rgb[0].toInt(), rgb[1].toInt(), rgb[2].toInt()));
bnTransparencyFillColor->setColor(c);
KoColor background(KoColorSpaceRegistry::instance()->rgb8());
background.fromQColor(Qt::white);
bnTransparencyFillColor->setDefaultColor(background);
bnTransparencyFillColor->setColor(cfg->getColor("transparencyFillcolor", background));
}
KisPropertiesConfigurationSP KisWdgOptionsPNG::configuration() const
......@@ -201,15 +206,17 @@ KisPropertiesConfigurationSP KisWdgOptionsPNG::configuration() const
bool interlace = interlacing->isChecked();
int compression = (int)compressionLevel->value();
bool tryToSaveAsIndexed = this->tryToSaveAsIndexed->isChecked();
QColor c = bnTransparencyFillColor->color().toQColor();
bool saveSRGB = chkSRGB->isChecked();
bool forceSRGB = chkForceSRGB->isChecked();
QVariant transparencyFillcolor;
transparencyFillcolor.setValue(bnTransparencyFillColor->color());
cfg->setProperty("alpha", alpha);
cfg->setProperty("indexed", tryToSaveAsIndexed);
cfg->setProperty("compression", compression);
cfg->setProperty("interlaced", interlace);
cfg->setProperty("transparencyFillcolor", QString("%1,%2,%3").arg(c.red()).arg(c.green()).arg(c.blue()));
cfg->setProperty("transparencyFillcolor", transparencyFillcolor);
cfg->setProperty("saveSRGBProfile", saveSRGB);
cfg->setProperty("forceSRGB", forceSRGB);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment