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++
JPEG XL: Export dialog rework and add option to save layers as multipage
Rasyuqa A. H.c44ca4990730
diff --git a/plugins/impex/jxl/JPEGXLExport.cpp b/plugins/impex/jxl/JPEGXLExport.cppindex d006b3bd2c..6f87381922 100644--- a/plugins/impex/jxl/JPEGXLExport.cpp+++ b/plugins/impex/jxl/JPEGXLExport.cpp@@ -76,6 +76,11 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODeviceKisImageSP image = document->savingImage();const QRect bounds = image->bounds();+ const bool cfgFlattenLayer = cfg->getBool("flattenLayers", true);+ const bool cfgHaveAnimation = cfg->getBool("haveAnimation", false);+ const bool cfgMultiLayer = cfg->getBool("multiLayer", false);+ const bool cfgMultiPage = cfg->getBool("multiPage", false);+auto enc = JxlEncoderMake(nullptr);auto runner = JxlResizableParallelRunnerMake(nullptr);if (JXL_ENC_SUCCESS != JxlEncoderSetParallelRunner(enc.get(), JxlResizableParallelRunner, runner.get())) {@@ -233,7 +238,7 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODeviceinfo->uses_original_profile = JXL_FALSE;dbgFile << "JXL use internal XYB profile";}- if (image->animationInterface()->hasAnimation() && cfg->getBool("haveAnimation", true)) {+ if (image->animationInterface()->hasAnimation() && cfgHaveAnimation) {info->have_animation = JXL_TRUE;info->animation.have_timecodes = JXL_FALSE;info->animation.num_loops = 0;@@ -241,6 +246,12 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODeviceinfo->animation.tps_numerator =static_cast<uint32_t>(image->animationInterface()->framerate());info->animation.tps_denominator = 1;+ } else if (cfgMultiPage) {+ info->have_animation = JXL_TRUE;+ info->animation.have_timecodes = JXL_FALSE;+ info->animation.num_loops = 0;+ info->animation.tps_numerator = 1;+ info->animation.tps_denominator = 1;}return info;}();@@ -593,7 +604,7 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice// See: https://github.com/libjxl/libjxl/issues/2463const int setPatches = [&]() -> int {#if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0)- if ((cfg->getInt("effort", 7) > 4) && !cfg->getBool("flattenLayers", true)) {+ if ((cfg->getInt("effort", 7) > 4) && cfgFlattenLayer) {warnFile << "Using workaround for layer exports, disabling patches option on effort > 4";return 0;}@@ -649,7 +660,7 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice{const bool isAnimated = [&]() {- if (image->animationInterface()->hasAnimation() && cfg->getBool("haveAnimation", true)) {+ if (image->animationInterface()->hasAnimation() && cfgHaveAnimation) {KisLayerUtils::flattenImage(image, nullptr);image->waitForDone();@@ -763,12 +774,11 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice}} else {auto frameHeader = std::make_unique<JxlFrameHeader>();- const bool flattenLayers = cfg->getBool("flattenLayers", true);// (On layered export) Convert group layer to paint layer to preserve// out-of-bound pixels so that it won't get clipped to canvas sizequint32 lastValidLayer = 0;- if (!flattenLayers) {+ if (cfgMultiLayer || cfgMultiPage) {for (quint32 pos = 0; pos < image->root()->childCount(); pos++) {KisNodeSP node = image->root()->at(pos);KisLayer *layer = qobject_cast<KisLayer *>(node.data());@@ -788,13 +798,13 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevicefor (quint32 pos = 0; pos < image->root()->childCount(); pos++) {KisNodeSP node = image->root()->at(pos);// Skip invalid and invisible layers- if (!flattenLayers && (!node || !node->visible() || node->isFakeNode())) {+ if ((cfgMultiLayer || cfgMultiPage) && (!node || !node->visible() || node->isFakeNode())) {dbgFile << "Skipping hidden layer" << node->name();continue;}const bool isFirstLayer = (node == image->root()->firstChild());- if (!flattenLayers) {+ if (cfgMultiLayer || cfgMultiPage) {dbgFile << "Visiting on layer" << node->name();if (!node->inherits("KisPaintLayer")) {@@ -812,14 +822,14 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice}const QRect layerBounds = [&]() {- if (node->exactBounds().isEmpty() || flattenLayers) {+ if (node->exactBounds().isEmpty() || cfgFlattenLayer) {return image->bounds();}return node->exactBounds();}();KisPaintDeviceSP dev;- if (flattenLayers) {+ if (cfgFlattenLayer) {dev = image->projection();} else {dev = node->projection();@@ -880,11 +890,15 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice}}();- if (!flattenLayers) {+ if (cfgMultiLayer || cfgMultiPage) {JxlEncoderInitFrameHeader(frameHeader.get());// Set frame duration to 0 to indicate a multi-layered image- frameHeader->duration = 0;+ if (cfgMultiLayer) {+ frameHeader->duration = 0;+ } else if (cfgMultiPage) {+ frameHeader->duration = 0xFFFFFFFF;+ }// Enable crop info if layer dimension is different than main// This also enables out-of-bound pixels to be preserved@@ -907,7 +921,7 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice// EXPERIMENTAL! Additive blending mode on JPEG-XL produces// slightly different result than Krita.const QString frameName = node->name();- if (!isFirstLayer) {+ if (!isFirstLayer && cfgMultiLayer) {if (node->compositeOpId() == QString("add")) {frameHeader->layer_info.blend_info.blendmode = JXL_BLEND_MULADD;} else {@@ -980,21 +994,21 @@ KisImportExportErrorCode JPEGXLExport::convert(KisDocument *document, QIODevice}const int progress = (pos * 100) / image->root()->childCount();setProgress(progress);- if ((pos == lastValidLayer && !flattenLayers) || flattenLayers) {+ if ((pos == lastValidLayer && (cfgMultiLayer || cfgMultiPage)) || cfgFlattenLayer) {JxlEncoderCloseInput(enc.get());}if (JxlEncoderFlushInput(enc.get()) != JXL_ENC_SUCCESS) {errFile << "JxlEncoderFlushInput failed";return ImportExportCodes::InternalError;}- if (flattenLayers) {+ if (cfgFlattenLayer) {break;}}}#else// Quit loop if flatten is active- if (flattenLayers) {+ if (cfgFlattenLayer) {break;}}@@ -1091,8 +1105,10 @@ KisPropertiesConfigurationSP JPEGXLExport::defaultConfiguration(const QByteArray// JXL_ENC_FRAME_SETTING_ALREADY_DOWNSAMPLED// JXL_ENC_FRAME_SETTING_COLOR_TRANSFORM- cfg->setProperty("haveAnimation", true);+ cfg->setProperty("haveAnimation", false);cfg->setProperty("flattenLayers", true);+ cfg->setProperty("multiLayer", false);+ cfg->setProperty("multiPage", false);cfg->setProperty("lossless", true);cfg->setProperty("effort", 7);cfg->setProperty("decodingSpeed", 0);diff --git a/plugins/impex/jxl/kis_wdg_options_jpegxl.cpp b/plugins/impex/jxl/kis_wdg_options_jpegxl.cppindex dd3455b596..d83879da9d 100644--- a/plugins/impex/jxl/kis_wdg_options_jpegxl.cpp+++ b/plugins/impex/jxl/kis_wdg_options_jpegxl.cpp@@ -19,6 +19,13 @@ KisWdgOptionsJPEGXL::KisWdgOptionsJPEGXL(QWidget *parent){setupUi(this);+ {+ flattenLayers->setToolTip(i18nc("JPEG-XL encoder options", "Flatten the image and save as single layer image (default)"));+ haveAnimation->setToolTip(i18nc("JPEG-XL encoder options", "Flatten the image and render animation frames as animated image"));+ multiLayer->setToolTip(i18nc("JPEG-XL encoder options", "Rasterize layers (including group and filter layers) and save as multi-layered image"));+ multiPage->setToolTip(i18nc("JPEG-XL encoder options", "Rasterize layers (including group and filter layers) and save as multi-page image"));+ }+{resampling->addItem(i18nc("JPEG-XL encoder options", "Default (only for low quality)"), -1);resampling->addItem(i18nc("JPEG-XL encoder options", "No downsampling"), 1);@@ -161,8 +168,10 @@ void KisWdgOptionsJPEGXL::setConfiguration(const KisPropertiesConfigurationSP cf{using SpaceList = QList<std::tuple<QString, QString, QString>>;- haveAnimation->setChecked(cfg->getBool("haveAnimation", true));+ haveAnimation->setChecked(cfg->getBool("haveAnimation", false));flattenLayers->setChecked(cfg->getBool("flattenLayers", true));+ multiLayer->setChecked(cfg->getBool("multiLayer", false));+ multiPage->setChecked(cfg->getBool("multiPage", false));lossyEncoding->setChecked(!cfg->getBool("lossless", true));effort->setValue(cfg->getInt("effort", 7));decodingSpeed->setValue(cfg->getInt("decodingSpeed", 0));@@ -280,6 +289,8 @@ KisPropertiesConfigurationSP KisWdgOptionsJPEGXL::configuration() constcfg->setProperty("haveAnimation", haveAnimation->isChecked());cfg->setProperty("flattenLayers", flattenLayers->isChecked());+ cfg->setProperty("multiLayer", multiLayer->isChecked());+ cfg->setProperty("multiPage", multiPage->isChecked());cfg->setProperty("lossless", !lossyEncoding->isChecked());cfg->setProperty("effort", effort->value());cfg->setProperty("decodingSpeed", decodingSpeed->value());diff --git a/plugins/impex/jxl/kis_wdg_options_jpegxl.ui b/plugins/impex/jxl/kis_wdg_options_jpegxl.uiindex e53e91929b..665cff3a33 100644--- a/plugins/impex/jxl/kis_wdg_options_jpegxl.ui+++ b/plugins/impex/jxl/kis_wdg_options_jpegxl.ui@@ -27,24 +27,54 @@<string><html><head/><body><p><span style=" font-size:18pt; text-decoration: underline;">Advanced</span></p><p>The option &quot;Default (encoder chooses)&quot; is common. It means that the encoder automatically selects optimal settings based on your quality/speed tradeoff selection. This option is recommended for most users.</p><p><span style=" font-size:14pt; text-decoration: underline;">Encoder Parameters:</span></p><p>general settings that apply to the overall JPEG XL encoding process, regardless of which compression method is used.</p><p><span style=" font-size:14pt; text-decoration: underline;">VarDCT parameters:</span></p><p>Variable Discrete Cosine Transform (Optimized for photographic content)</p><p>This one is in the same family of compression techniques as used by the original JPEG, and thus best for ‘Natural’ images, such as photographs and images with a lot of gradients and textures.</p><p><span style=" font-size:14pt; text-decoration: underline;">Modular Parameters:</span></p><p>Settings for Modular mode - optimized for graphical content, illustrations, and lossless compression.</p><p><br/></p><p><br/></p></body></html></string></property><property name="currentIndex">- <number>2</number>+ <number>0</number></property><widget class="QWidget" name="general"><attribute name="title"><string>General</string></attribute><layout class="QFormLayout" name="formLayout_10">- <item row="0" column="0">- <widget class="QCheckBox" name="haveAnimation">- <property name="toolTip">- <string>If this is not enabled, only the first frame will be saved.</string>- </property>- <property name="text">- <string>Save as animated JPEG-XL</string>+ <item row="0" column="0" colspan="2">+ <widget class="QGroupBox" name="groupBox">+ <property name="title">+ <string>Save as:</string></property>+ <layout class="QVBoxLayout" name="verticalLayout_6">+ <item>+ <widget class="QRadioButton" name="flattenLayers">+ <property name="text">+ <string>Flattened image</string>+ </property>+ <property name="checked">+ <bool>true</bool>+ </property>+ </widget>+ </item>+ <item>+ <widget class="QRadioButton" name="haveAnimation">+ <property name="text">+ <string>Animated image</string>+ </property>+ </widget>+ </item>+ <item>+ <widget class="QRadioButton" name="multiLayer">+ <property name="text">+ <string>Multi-layered image</string>+ </property>+ </widget>+ </item>+ <item>+ <widget class="QRadioButton" name="multiPage">+ <property name="text">+ <string>Multi-paged image</string>+ </property>+ </widget>+ </item>+ </layout></widget></item>- <item row="2" column="0" colspan="2">+ <item row="1" column="0" colspan="2"><widget class="QGroupBox" name="groupBox_2"><property name="title"><string>Encoding options</string>@@ -243,19 +273,6 @@ Default is 0.</string></property></spacer></item>- <item row="1" column="0">- <widget class="QCheckBox" name="flattenLayers">- <property name="toolTip">- <string><html><head/><body><p>If this is not enabled, the layers will be rasterized and saved. However, animated exports are always flattened regardless of this setting.</p><p>It is advised to leave this option checked for web delivery and general compatibility with other applications.</p></body></html></string>- </property>- <property name="text">- <string>Flatten the image</string>- </property>- <property name="checked">- <bool>true</bool>- </property>- </widget>- </item></layout></widget><widget class="QWidget" name="tab">@@ -402,7 +419,7 @@ Default is 0.</string><string><html><head/><body><p><br/></p></body></html></string></property><property name="currentIndex">- <number>2</number>+ <number>0</number></property><widget class="QWidget" name="encoderParameters"><attribute name="title">