Skip to content

Commit f47d573

Browse files
committed
Attach export ICC profile to JP2 images
1 parent ad9f6d2 commit f47d573

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/imageio/format/j2k.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of darktable,
3-
Copyright (C) 2012-2023 darktable developers.
3+
Copyright (C) 2012-2025 darktable developers.
44
55
darktable is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -418,6 +418,40 @@ int write_image(dt_imageio_module_data_t *j2k_tmp, const char *filename, const v
418418
}
419419
}
420420

421+
/* Determine the actual (export vs colorout) color profile used */
422+
const dt_colorspaces_color_profile_t *cp = dt_colorspaces_get_output_profile(imgid, over_type, over_filename);
423+
/* ICC writing doesn't work w/ OpenJPEG 2.5.3 and earlier (prefer runtime detection) */
424+
const char *opj_ver = opj_version();
425+
if(opj_ver[0] > '2' || (opj_ver[0] == '2' && (opj_ver[2] > '5' || (opj_ver[2] == '5' && opj_ver[4] > '3'))))
426+
{
427+
uint32_t icc_profile_len;
428+
cmsSaveProfileToMem(cp->profile, NULL, &icc_profile_len);
429+
if(icc_profile_len > 0)
430+
{
431+
uint8_t *icc_profile_buf = malloc(sizeof(uint8_t) * icc_profile_len);
432+
if(icc_profile_buf == NULL)
433+
{
434+
dt_print(DT_DEBUG_ALWAYS, "failed to allocate ICC profile");
435+
opj_image_destroy(image);
436+
rc = 0;
437+
goto exit;
438+
}
439+
cmsSaveProfileToMem(cp->profile, icc_profile_buf, &icc_profile_len);
440+
image->icc_profile_buf = icc_profile_buf; /* freed later by opj_image_destroy() */
441+
image->icc_profile_len = icc_profile_len;
442+
}
443+
}
444+
else
445+
{
446+
if(cp->type != DT_COLORSPACE_SRGB)
447+
{
448+
dt_control_log("%s", _("unable to attach output profile to JP2"));
449+
dt_print(
450+
DT_DEBUG_ALWAYS,
451+
"Warning: exporting with anything but sRGB profile might lead to wrong results when opening the image");
452+
}
453+
}
454+
421455
/*Encoding image*/
422456

423457
/* Decide if MCT should be used */

0 commit comments

Comments
 (0)