Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

WebP: Add option to force sRGB convert and ICC profile embedding

Rasyuqa A. H.7cff86886f9c
diff --git a/plugins/impex/webp/kis_wdg_options_webp.cpp b/plugins/impex/webp/kis_wdg_options_webp.cpp
index 891947e81b..16ae0ccc1a 100644
--- a/plugins/impex/webp/kis_wdg_options_webp.cpp
+++ b/plugins/impex/webp/kis_wdg_options_webp.cpp
@@ -76,6 +76,8 @@ void KisWdgOptionsWebP::setConfiguration(const KisPropertiesConfigurationSP cfg)
KisSpinBoxI18nHelper::setText(quality, i18nc("{n} is the number value, % is the percent sign", "{n}%"));
tradeoff->setValue(cfg->getInt("method", 4));
dithering->setChecked(cfg->getBool("dithering", true));
+ chkForceSRGB->setChecked(cfg->getBool("force_srgb", false));
+ chkSaveProfile->setChecked(cfg->getBool("save_profile", true));
targetSize->setValue(cfg->getInt("target_size", 0));
targetPSNR->setValue(cfg->getDouble("target_PSNR", 0.0));
@@ -178,6 +180,8 @@ KisPropertiesConfigurationSP KisWdgOptionsWebP::configuration() const
cfg->setProperty("quality", quality->value());
cfg->setProperty("method", tradeoff->value());
cfg->setProperty("dithering", dithering->isChecked());
+ cfg->setProperty("force_srgb", chkForceSRGB->isChecked());
+ cfg->setProperty("save_profile", chkSaveProfile->isChecked());
cfg->setProperty("target_size", targetSize->value());
cfg->setProperty("target_PSNR", targetPSNR->value());
diff --git a/plugins/impex/webp/kis_wdg_options_webp.ui b/plugins/impex/webp/kis_wdg_options_webp.ui
index c89d1116c8..36acaeb91b 100644
--- a/plugins/impex/webp/kis_wdg_options_webp.ui
+++ b/plugins/impex/webp/kis_wdg_options_webp.ui
@@ -140,6 +140,33 @@ If Lossless is disabled: 0 = smallest size, 100 = largest size.</string>
</layout>
</widget>
</item>
+ <item row="2" column="0">
+ <widget class="QCheckBox" name="chkForceSRGB">
+ <property name="text">
+ <string>Force convert to sRGB</string>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="0">
+ <widget class="QCheckBox" name="chkSaveProfile">
+ <property name="text">
+ <string>Save ICC Profile</string>
+ </property>
+ </widget>
+ </item>
+ <item row="4" column="0">
+ <spacer name="verticalSpacer">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
</layout>
</widget>
<widget class="QWidget" name="advanced">
diff --git a/plugins/impex/webp/kis_webp_export.cpp b/plugins/impex/webp/kis_webp_export.cpp
index 8df6db45af..24edd825dc 100644
--- a/plugins/impex/webp/kis_webp_export.cpp
+++ b/plugins/impex/webp/kis_webp_export.cpp
@@ -74,6 +74,8 @@ KisPropertiesConfigurationSP KisWebPExport::defaultConfiguration(const QByteArra
cfg->setProperty("quality", preset.quality);
cfg->setProperty("method", preset.method);
cfg->setProperty("dithering", true);
+ cfg->setProperty("force_srgb", false);
+ cfg->setProperty("save_profile", true);
cfg->setProperty("target_size", preset.target_size);
cfg->setProperty("target_PSNR", preset.target_PSNR);
@@ -150,6 +152,26 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
const KoColorSpace *cs =
document->savingImage()->projection()->colorSpace();
+ const bool needSrgbConversion = [&]() {
+ if (!cfg->getBool("force_srgb", false)) {
+ return false;
+ }
+
+ if (cs->colorModelId() != RGBAColorModelID) {
+ return true;
+ }
+
+ const bool hasPrimaries = cs->profile()->hasColorants();
+ const TransferCharacteristics gamma = cs->profile()->getTransferCharacteristics();
+ if (hasPrimaries) {
+ const ColorPrimaries primaries = cs->profile()->getColorPrimaries();
+ if (gamma == TRC_IEC_61966_2_1 && primaries == PRIMARIES_ITU_R_BT_709_5) {
+ return false;
+ }
+ }
+ return true;
+ }();
+
// Then comes the animation chunk.
WebPData imageChunk = {nullptr, 0};
@@ -296,8 +318,8 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
const KisPaintDeviceSP src = dev;
const KoID depthId = src->colorSpace()->colorDepthId();
const KoColorSpace *destCs = KoColorSpaceRegistry::instance()->rgb8();
- if (cs->colorModelId() == RGBAColorModelID) {
- // Preserve color profile if model is RGB
+ if (cs->colorModelId() == RGBAColorModelID && !needSrgbConversion) {
+ // Preserve color profile if model is RGB and force convert sRGB is off
destCs = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(),
Integer8BitsColorDepthID.id(),
src->colorSpace()->profile());
@@ -345,6 +367,10 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
? dst->colorSpace()->profile()
: nullptr;
+ if (needSrgbConversion) {
+ imageProfile = KoColorSpaceRegistry::instance()->p709SRGBProfile();
+ }
+
const QImage imageOut = dst->convertToQImage(imageProfile, 0, 0, bounds.width(), bounds.height())
.convertToFormat(QImage::Format_RGBA8888);
@@ -418,8 +444,8 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
const KisPaintDeviceSP src = document->savingImage()->projection();
const KoID depthId = src->colorSpace()->colorDepthId();
const KoColorSpace *destCs = KoColorSpaceRegistry::instance()->rgb8();
- if (cs->colorModelId() == RGBAColorModelID) {
- // Preserve color profile if model is RGB
+ if (cs->colorModelId() == RGBAColorModelID && !needSrgbConversion) {
+ // Preserve color profile if model is RGB and force convert sRGB is off
destCs = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(),
Integer8BitsColorDepthID.id(),
src->colorSpace()->profile());
@@ -467,6 +493,10 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
? dst->colorSpace()->profile()
: nullptr;
+ if (needSrgbConversion) {
+ imageProfile = KoColorSpaceRegistry::instance()->p709SRGBProfile();
+ }
+
const QImage imageOut = dst->convertToQImage(imageProfile, 0, 0, bounds.width(), bounds.height())
.convertToFormat(QImage::Format_RGBA8888);
@@ -513,8 +543,10 @@ KisImportExportErrorCode KisWebPExport::convert(KisDocument *document, QIODevice
}
// According to the standard, the ICC profile must be written first.
- {
- const QByteArray profile = image->profile()->rawData();
+ if (cfg->getBool("save_profile", true)) {
+ const QByteArray profile = needSrgbConversion
+ ? KoColorSpaceRegistry::instance()->p709SRGBProfile()->rawData()
+ : image->profile()->rawData();
WebPData iccChunk = {reinterpret_cast<const uint8_t *>(profile.data()),
static_cast<size_t>(profile.size())};