134#if LIBHEIF_HAVE_VERSION(1, 13, 0)
138#if LIBHEIF_HAVE_VERSION(1, 20, 2)
139 using HeifStrideType = size_t;
140 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane2));
141#elif LIBHEIF_HAVE_VERSION(1, 20, 0)
142 using HeifStrideType = size_t;
143 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane));
145 using HeifStrideType = int;
146 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane));
175 bool convertToRec2020 =
false;
178 QString conversionOption =
179 (configuration->getString(
"floatingPointConversionOption",
181 if (conversionOption ==
"Rec2100PQ") {
182 convertToRec2020 =
true;
184 }
else if (conversionOption ==
"Rec2100HLG") {
185 convertToRec2020 =
true;
187 }
else if (conversionOption ==
"ApplyPQ") {
189 }
else if (conversionOption ==
"ApplyHLG") {
191 }
else if (conversionOption ==
"ApplySMPTE428") {
209 int quality = configuration->getInt(
"quality", 50);
210 bool lossless = configuration->getBool(
"lossless",
false);
212 float hlgGamma = configuration->getFloat(
"HLGgamma", 1.2f);
213 bool removeHGLOOTF =
false;
222 heif::Encoder
encoder(heif_compression_HEVC);
226 encoder = heif::Encoder(heif_compression_AV1);
230 encoder.set_lossy_quality(quality);
233 encoder.set_lossy_quality(100);
235 encoder.set_lossless(lossless);
237 encoder.set_parameter(
"chroma", configuration->getString(
"chroma",
"444").toStdString());
242 int width = image->
width();
243 int height = image->
height();
247 heif_chroma chroma = hasAlpha? heif_chroma_interleaved_RRGGBBAA_LE: heif_chroma_interleaved_RRGGBB_LE;
248 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
249 chroma = hasAlpha? heif_chroma_interleaved_RRGGBBAA_BE: heif_chroma_interleaved_RRGGBB_BE;
256 dbgFile <<
"saving as 8bit rgba";
257 img.create(width,height, heif_colorspace_RGB, heif_chroma_444);
258 img.add_plane(heif_channel_R, width,height, 8);
259 img.add_plane(heif_channel_G, width,height, 8);
260 img.add_plane(heif_channel_B, width,height, 8);
262 HeifStrideType strideR = 0;
263 HeifStrideType strideG = 0;
264 HeifStrideType strideB = 0;
265 HeifStrideType strideA = 0;
267 uint8_t *ptrR = heifGetPlaneMethod(img, heif_channel_R, &strideR);
268 uint8_t *ptrG = heifGetPlaneMethod(img, heif_channel_G, &strideG);
269 uint8_t *ptrB = heifGetPlaneMethod(img, heif_channel_B, &strideB);
271 uint8_t *ptrA = [&]() -> uint8_t * {
273 img.add_plane(heif_channel_Alpha, width, height, 8);
274 return heifGetPlaneMethod(img, heif_channel_Alpha, &strideA);
297 dbgFile <<
"Saving as 12bit rgba";
298 img.create(width, height, heif_colorspace_RGB, chroma);
299 img.add_plane(heif_channel_interleaved, width, height, 12);
301 HeifStrideType stride = 0;
303 uint8_t *ptr = heifGetPlaneMethod(img, heif_channel_interleaved, &stride);
336 dbgFile <<
"Saving as 8 bit monochrome.";
337 img.create(width, height, heif_colorspace_monochrome, heif_chroma_monochrome);
339 img.add_plane(heif_channel_Y, width, height, 8);
341 HeifStrideType strideG = 0;
342 HeifStrideType strideA = 0;
344 uint8_t *ptrG = heifGetPlaneMethod(img, heif_channel_Y, &strideG);
345 uint8_t *ptrA = [&]() -> uint8_t * {
347 img.add_plane(heif_channel_Alpha, width, height, 8);
348 return heifGetPlaneMethod(img, heif_channel_Alpha, &strideA);
369 dbgFile <<
"Saving as 12 bit monochrome";
370 img.create(width, height, heif_colorspace_monochrome, heif_chroma_monochrome);
372 img.add_plane(heif_channel_Y, width, height, 12);
374 HeifStrideType strideG = 0;
375 HeifStrideType strideA = 0;
377 uint8_t *ptrG = heifGetPlaneMethod(img, heif_channel_Y, &strideG);
378 uint8_t *ptrA = [&]() -> uint8_t * {
380 img.add_plane(heif_channel_Alpha, width, height, 12);
381 return heifGetPlaneMethod(img, heif_channel_Alpha, &strideA);
407 std::vector<uint8_t> rawProfile(rawProfileBA.begin(), rawProfileBA.end());
408 img.set_raw_color_profile(heif_color_profile_type_prof, rawProfile);
410 heif::ColorProfile_nclx nclxDescription;
411 nclxDescription.set_full_range_flag(
true);
412 nclxDescription.set_matrix_coefficients(heif_matrix_coefficients_RGB_GBR);
413 if (convertToRec2020) {
414#if LIBHEIF_HAVE_VERSION(1, 14, 1)
415 nclxDescription.set_color_primaries(heif_color_primaries_ITU_R_BT_2020_2_and_2100_0);
417 nclxDescription.set_color_primaties(heif_color_primaries_ITU_R_BT_2020_2_and_2100_0);
426 errFile <<
"Attempt to export a file with unsupported primaries" << primaries;
429#if LIBHEIF_HAVE_VERSION(1, 14, 1)
430 nclxDescription.set_color_primaries(heif_color_primaries(primaries));
432 nclxDescription.set_color_primaties(heif_color_primaries(primaries));
437 nclxDescription.set_transfer_characteristics(heif_transfer_characteristic_ITU_R_BT_2100_0_PQ);
439 nclxDescription.set_transfer_characteristics(heif_transfer_characteristic_ITU_R_BT_2100_0_HLG);
441 nclxDescription.set_transfer_characteristics(heif_transfer_characteristic_SMPTE_ST_428_1);
444 img.set_nclx_color_profile(nclxDescription);
450 heif::Context::EncodingOptions options;
455 options.macOS_compatibility_workaround_no_nclx_profile =
false;
458 heif::ImageHandle handle = ctx.encode_image(img,
encoder, options);
466 QScopedPointer<KisMetaData::Store> metaDataStore;
474 if (!metaDataStore->empty()) {
479 QByteArray data = buffer.data();
482 if (data.size() > 4) {
483 ctx.add_exif_metadata(handle, data.constData(), data.size());
490 QByteArray data = buffer.data();
493 if (data.size() > 0) {
494 ctx.add_XMP_metadata(handle, data.constData(), data.size());
505 }
catch (Error &err) {
534 chromaOptions <<
"420" <<
"422" <<
"444";
535 cmbChroma->addItems(chromaOptions);
536 cmbChroma->setItemData(0, i18nc(
"@tooltip",
"The brightness of the image will be at full resolution, while the colorfulness will be halved in both dimensions."), Qt::ToolTipRole);
537 cmbChroma->setItemData(1, i18nc(
"@tooltip",
"The brightness of the image will be at full resolution, while the colorfulness will be halved horizontally."), Qt::ToolTipRole);
538 cmbChroma->setItemData(2, i18nc(
"@tooltip",
"Both brightness and colorfulness of the image will be at full resolution."), Qt::ToolTipRole);
539 chkLossless->setChecked(cfg->getBool(
"lossless",
true));
540 sliderQuality->setValue(qreal(cfg->getInt(
"quality", 50)));
541 cmbChroma->setCurrentIndex(chromaOptions.indexOf(cfg->getString(
"chroma",
"444")));
552 QStringList conversionOptionsList = { i18nc(
"Color space name",
"Rec 2100 PQ"), i18nc(
"Color space name",
"Rec 2100 HLG")};
553 QStringList toolTipList = {i18nc(
"@tooltip",
"The image will be converted to Rec 2020 linear first, and then encoded with a perceptual quantizer curve"
554 " (also known as SMPTE 2048 curve). Recommended for HDR images where the absolute brightness is important."),
555 i18nc(
"@tooltip",
"The image will be converted to Rec 2020 linear first, and then encoded with a Hybrid Log Gamma curve."
556 " Recommended for HDR images where the display may not understand HDR.")};
557 QStringList conversionOptionName = {
"Rec2100PQ",
"Rec2100HLG"};
561 conversionOptionsList << i18nc(
"Color space option plus transfer function name",
"Keep colorants, encode PQ");
562 toolTipList << i18nc(
"@tooltip",
"The image will be linearized first, and then encoded with a perceptual quantizer curve"
563 " (also known as the SMPTE 2048 curve). Recommended for images where the absolute brightness is important.");
564 conversionOptionName <<
"ApplyPQ";
566 conversionOptionsList << i18nc(
"Color space option plus transfer function name",
"Keep colorants, encode HLG");
567 toolTipList << i18nc(
"@tooltip",
"The image will be linearized first, and then encoded with a Hybrid Log Gamma curve."
568 " Recommended for images intended for screens which cannot understand PQ");
569 conversionOptionName <<
"ApplyHLG";
571 conversionOptionsList << i18nc(
"Color space option plus transfer function name",
"Keep colorants, encode SMPTE ST 428");
572 toolTipList << i18nc(
"@tooltip",
"The image will be linearized first, and then encoded with SMPTE ST 428."
573 " Krita always opens images like these as linear floating point, this option is there to reverse that");
574 conversionOptionName <<
"ApplySMPTE428";
577 conversionOptionsList << i18nc(
"Color space option",
"No changes, clip");
578 toolTipList << i18nc(
"@tooltip",
"The image will be converted plainly to 12bit integer, and values that are out of bounds are clipped, the icc profile will be embedded.");
579 conversionOptionName <<
"KeepSame";
581 cmbConversionPolicy->addItems(conversionOptionsList);
582 for (
int i=0; i< toolTipList.size(); i++) {
583 cmbConversionPolicy->setItemData(i, toolTipList.at(i), Qt::ToolTipRole);
584 cmbConversionPolicy->setItemData(i, conversionOptionName.at(i), Qt::UserRole+1);
587 cfg->getString(
"floatingPointConversionOption",
"KeepSame");
588 if (conversionOptionName.contains(optionName)) {
589 cmbConversionPolicy->setCurrentIndex(
590 conversionOptionName.indexOf(optionName));
593 lossySettings->setEnabled(!chkLossless->isChecked());