The filter chain calls this method to perform the actual conversion. The passed mimetypes should be a pair of those you specified in your .desktop file. You have to implement this method to make the filter work.
205{
206#if LIBHEIF_HAVE_VERSION(1, 13, 0)
207 HeifLock lock;
208#endif
209
210#if LIBHEIF_HAVE_VERSION(1, 20, 2)
211 using HeifStrideType = size_t;
212 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane2));
213#elif LIBHEIF_HAVE_VERSION(1, 20, 0)
214 using HeifStrideType = size_t;
215 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane));
216#else
217 using HeifStrideType = int;
218 auto heifGetPlaneMethod = std::mem_fn(qNonConstOverload<heif_channel, HeifStrideType*>(&heif::Image::get_plane));
219#endif
220
221
223
224 try {
225 heif::Context ctx;
226 ctx.read_from_reader(reader);
227
228
229
230 heif::ImageHandle handle = ctx.get_primary_image_handle();
231
232 heif_color_profile_type profileType = heif_image_handle_get_color_profile_type(handle.get_raw_image_handle());
233
234
235 heif::Image heifimage = handle.decode_image(heif_colorspace_undefined, heif_chroma_undefined);
236 heif_colorspace heifModel = heifimage.get_colorspace();
237 heif_chroma heifChroma = heifimage.get_chroma_format();
238 int luma = handle.get_luma_bits_per_pixel();
239 bool hasAlpha = handle.has_alpha_channel();
240
241 dbgFile <<
"loading heif" << heifModel << heifChroma << luma;
242
244 bool applyOOTF = false;
245 float displayGamma = 1.2f;
246
247 struct heif_error err {
248 };
252
253 if (luma > 8) {
255 }
256
257
258 if (profileType == heif_color_profile_type_prof || profileType == heif_color_profile_type_rICC) {
259 dbgFile <<
"profile type is icc profile";
260
261
262
263
264 int rawProfileSize = (int) heif_image_handle_get_raw_color_profile_size(handle.get_raw_image_handle());
265 if (rawProfileSize > 0) {
266 QByteArray ba(rawProfileSize, 0);
267 err = heif_image_handle_get_raw_color_profile(handle.get_raw_image_handle(), ba.data());
268 if (err.code) {
269 dbgFile <<
"icc profile loading failed:" << err.message;
270 } else {
273 }
274 } else {
275 dbgFile <<
"icc profile is empty";
276 }
277 } else if (profileType == heif_color_profile_type_nclx) {
278 dbgFile <<
"profile type is nclx coding independent code points";
279
280
281
282
283 struct heif_color_profile_nclx *nclx = nullptr;
284 err = heif_image_handle_get_nclx_color_profile(handle.get_raw_image_handle(), &nclx);
285 if (err.code || !nclx) {
286 dbgFile <<
"nclx profile loading failed" << err.message;
287 } else {
290 if (nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ) {
291 dbgFile <<
"linearizing from PQ";
294 }
295 if (nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_2100_0_HLG) {
296 dbgFile <<
"linearizing from HLG";
299 }
300 if (nclx->transfer_characteristics == heif_transfer_characteristic_SMPTE_ST_428_1) {
301 dbgFile <<
"linearizing from SMPTE 428";
304 }
305
306 if (nclx->transfer_characteristics == heif_transfer_characteristic_IEC_61966_2_4 ||
307 nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_1361) {
309 }
310
312 query.whitePoint = {static_cast<double>(nclx->color_primary_white_x),
313 static_cast<double>(nclx->color_primary_white_y)};
316 return {};
317 } else {
318 return {{static_cast<double>(nclx->color_primary_red_x),
319 static_cast<double>(nclx->color_primary_red_y)},
320 {static_cast<double>(nclx->color_primary_green_x),
321 static_cast<double>(nclx->color_primary_green_y)},
322 {static_cast<double>(nclx->color_primary_blue_x),
323 static_cast<double>(nclx->color_primary_blue_y)}};
324 }
325 }();
326
328
331 }
332
333 heif_nclx_color_profile_free(nclx);
334 dbgFile <<
"nclx profile found" << profile->
name();
335 }
336 } else {
338 }
339
340
342
344 heifimage = handle.decode_image(heif_colorspace_YCbCr, heif_chroma_monochrome);
345 }
347 heifChroma = heif_chroma_monochrome;
348 } else if (heifModel == heif_colorspace_YCbCr) {
349
350 heifimage = handle.decode_image(heif_colorspace_RGB, heif_chroma_444);
352 heifModel = heifimage.get_colorspace();
353 heifChroma = heifimage.get_chroma_format();
354 }
355
356
357 if (!profile) {
361 } else {
365 }
366 }
367
369
370 int width = handle.get_width();
371
372 int height = handle.get_height();
373
374
375
376 KisImageSP image =
new KisImage(document->createUndoStore(), width, height, colorSpace,
377 "HEIF image");
378
380
381 if (luma != 8 && luma != 10 && luma != 12) {
382 dbgFile <<
"unknown bitdepth" << luma;
383 }
384
385 if (heifChroma == heif_chroma_monochrome) {
386 dbgFile <<
"monochrome heif file, bits:" << luma;
387 HeifStrideType strideG = 0;
388 HeifStrideType strideA = 0;
389 const uint8_t *imgG = heifGetPlaneMethod(heifimage, heif_channel_Y, &strideG);
390 const uint8_t *imgA =
391 heifGetPlaneMethod(heifimage, heif_channel_Alpha, &strideA);
392 const int width = heifimage.get_width(heif_channel_Y);
393 const int height = heifimage.get_height(heif_channel_Y);
396
398 hasAlpha,
399 width,
400 height,
401 it,
402 imgG,
403 imgA,
404 strideG,
405 strideA);
406 } else if (heifChroma == heif_chroma_444) {
407 dbgFile <<
"planar heif file, bits:" << luma;
408
409 HeifStrideType strideR = 0;
410 HeifStrideType strideG = 0;
411 HeifStrideType strideB = 0;
412 HeifStrideType strideA = 0;
413 const uint8_t* imgR = heifGetPlaneMethod(heifimage, heif_channel_R, &strideR);
414 const uint8_t* imgG = heifGetPlaneMethod(heifimage, heif_channel_G, &strideG);
415 const uint8_t* imgB = heifGetPlaneMethod(heifimage, heif_channel_B, &strideB);
416 const uint8_t *imgA =
417 heifGetPlaneMethod(heifimage, heif_channel_Alpha, &strideA);
419
421 linearizePolicy,
422 applyOOTF,
423 hasAlpha,
424 width,
425 height,
426 imgR,
427 strideR,
428 imgG,
429 strideG,
430 imgB,
431 strideB,
432 imgA,
433 strideA,
434 it,
435 displayGamma,
436 colorSpace);
437 } else if (heifChroma == heif_chroma_interleaved_RGB || heifChroma == heif_chroma_interleaved_RGBA) {
438 HeifStrideType stride = 0;
439 dbgFile <<
"interleaved SDR heif file, bits:" << luma;
440
441 const uint8_t *img = heifGetPlaneMethod(heifimage, heif_channel_interleaved, &stride);
442 width = heifimage.get_width(heif_channel_interleaved);
443 height = heifimage.get_height(heif_channel_interleaved);
446
448 applyOOTF,
449 hasAlpha,
450 width,
451 height,
452 img,
453 stride,
454 it,
455 displayGamma,
456 colorSpace);
457
458 } else if (heifChroma == heif_chroma_interleaved_RRGGBB_LE || heifChroma == heif_chroma_interleaved_RRGGBBAA_LE || heifChroma == heif_chroma_interleaved_RRGGBB_BE || heifChroma == heif_chroma_interleaved_RRGGBB_BE) {
459 HeifStrideType stride = 0;
460 dbgFile <<
"interleaved HDR heif file, bits:" << luma;
461
462 const uint8_t *img =
463 heifGetPlaneMethod(heifimage, heif_channel_interleaved, &stride);
466
468 linearizePolicy,
469 applyOOTF,
470 hasAlpha,
471 width,
472 height,
473 img,
474 stride,
475 it,
476 displayGamma,
477 colorSpace);
478 }
479
481 image->
cropImage(QRect(0, 0, width, height));
482
483
484
485 std::vector<heif_item_id> metadata_IDs = handle.get_list_of_metadata_block_IDs();
486
487 for (heif_item_id id : metadata_IDs) {
488
489 if (handle.get_metadata_type(id) == "Exif") {
490
491
492 std::vector<uint8_t> exif_data = handle.get_metadata(id);
493
494 if (exif_data.size()>4) {
495 size_t skip = ((quint32(exif_data[0]) << 24U)
496 | (quint32(exif_data[1])) << 16U
497 | (quint32(exif_data[2]) << 8U) | exif_data[3])
498 + 4u;
499
500 if (exif_data.size() > skip) {
503
504
505 QByteArray ba(
506 reinterpret_cast<char *>(exif_data.data() + skip),
507 static_cast<int>(exif_data.size() - skip));
508 QBuffer buf(&ba);
510 }
511 }
512 }
513
514 if (handle.get_metadata_type(id) == "mime" &&
515 handle.get_metadata_content_type(id) == "application/rdf+xml") {
516
517
518 std::vector<uint8_t> xmp_data = handle.get_metadata(id);
520
521
522 QByteArray ba(reinterpret_cast<char *>(xmp_data.data()), static_cast<int>(xmp_data.size()));
523 QBuffer buf(&ba);
525 }
526 }
527
528 document->setCurrentImage(image);
530 } catch (Error &err) {
532 }
533}
KisImportExportErrorCode setHeifError(KisDocument *document, heif::Error error)
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID GrayAColorModelID("GRAYA", ki18n("Grayscale/Alpha"))
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
ColorPrimaries
The colorPrimaries enum Enum of colorants, follows ITU H.273 for values 0 to 255, and has extra known...
@ PRIMARIES_ITU_R_BT_709_5
TransferCharacteristics
The transferCharacteristics enum Enum of transfer characteristics, follows ITU H.273 for values 0 to ...
const quint8 OPACITY_OPAQUE_U8
LinearizePolicy
The KoColorTransferFunctions class.
KisGroupLayerSP rootLayer() const
QString nextLayerName(const QString &baseName="") const
void cropImage(const QRect &newRect)
start asynchronous operation on cropping the image
KisHLineIteratorSP createHLineIteratorNG(qint32 x, qint32 y, qint32 w)
const T value(const QString &id) const
auto readPlanarLayer(const int luma, Args &&...args)
auto readInterleavedLayer(const int luma, LinearizePolicy linearizePolicy, bool applyOOTF, const int channels, const int width, const int height, const uint8_t *img, const int stride, KisHLineIteratorSP it, float displayGamma, const KoColorSpace *colorSpace)
auto readPlanarLayer(const int luma, LinearizePolicy policy, bool applyOOTF, bool hasAlpha, const int width, const int height, const uint8_t *imgR, const int strideR, const uint8_t *imgG, const int strideG, const uint8_t *imgB, const int strideB, const uint8_t *imgA, const int strideA, KisHLineIteratorSP it, float displayGamma, const KoColorSpace *colorSpace)
auto readInterleavedLayer(LinearizePolicy linearizePolicy, bool applyOOTF, const int channels, const int width, const int height, const uint8_t *img, const int stride, KisHLineIteratorSP it, float displayGamma, const KoColorSpace *colorSpace)
KisMetaData::Store * metaData()
bool addNode(KisNodeSP node, KisNodeSP parent=KisNodeSP(), KisNodeAdditionFlags flags=KisNodeAdditionFlag::None)
KisPaintDeviceSP paintDevice
The KoColorProfileQuery struct.
virtual QString colorModelID() const
const KoColorProfile * profileByName(const QString &name) const
QString colorSpaceId(const QString &colorModelId, const QString &colorDepthId) const
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()
const KoColorProfile * profileFor(const KoColorProfileQuery &query, const bool generate=true) const
profileFor tries to find the profile that matches these characteristics, if no such profile is found,...
QString defaultProfileForColorSpace(const QString &colorSpaceId) const
const KoColorProfile * createColorProfile(const QString &colorModelId, const QString &colorDepthId, const QByteArray &rawData)