Skip to content

Commit

Permalink
Improve scRGB support (libvips#3623)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcupitt authored Oct 16, 2023
1 parent 27e8688 commit 1130557
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- threaded write in tiffsave for tiled JPEG and JPEG2000 [jcupitt]
- add vips_thread_execute() to the public API [jcupitt]
- add "keep" flag to foreign savers, deprecate "strip" [a3mar]
- improve scRGB handling [jcupitt]

18/9/23 8.14.5

Expand Down
3 changes: 1 addition & 2 deletions libvips/colour/LabQ2sRGB.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ vips_col_scRGB2sRGB_16(float R, float G, float B,
{
vips_col_make_tables_RGB_16();

return vips_col_scRGB2sRGB(65536, vips_Y2v_16,
R, G, B, r, g, b, og);
return vips_col_scRGB2sRGB(65536, vips_Y2v_16, R, G, B, r, g, b, og);
}

/* Turn scRGB into BW. Return or=1 for out of gamut - g will contain an
Expand Down
16 changes: 10 additions & 6 deletions libvips/colour/sRGB2scRGB.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,22 @@ vips_sRGB2scRGB_build(VipsObject *object)
if (vips_check_bands_atleast(class->nickname, in, 3))
return -1;

// we are changing the gamma, so any profile on the image can no longer
// work (and will cause horrible problems in any downstream colour
// handling)
if (vips_copy(in, &t[0], NULL))
return -1;
in = t[0];
vips_image_remove(in, VIPS_META_ICC_NAME);

format = in->Type == VIPS_INTERPRETATION_RGB16
? VIPS_FORMAT_USHORT
: VIPS_FORMAT_UCHAR;
if (in->BandFmt != format) {
if (vips_cast(in, &t[0], format, NULL))
if (vips_cast(in, &t[1], format, NULL))
return -1;
in = t[1];
}
else {
t[0] = in;
g_object_ref(t[0]);
}
in = t[0];

out = vips_image_new();
if (vips_image_pipelinev(out,
Expand Down
15 changes: 11 additions & 4 deletions libvips/colour/scRGB2sRGB.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ vips_scRGB2sRGB_build(VipsObject *object)
if (vips_check_bands_atleast(class->nickname, in, 3))
return -1;

// we are changing the gamma, so any profile on the image can no longer
// work (and will cause horrible problems in any downstream colour
// handling)
if (vips_copy(in, &t[0], NULL))
return -1;
in = t[0];
vips_image_remove(in, VIPS_META_ICC_NAME);

switch (scRGB2sRGB->depth) {
case 16:
interpretation = VIPS_INTERPRETATION_RGB16;
Expand All @@ -213,14 +221,13 @@ vips_scRGB2sRGB_build(VipsObject *object)
break;

default:
vips_error(class->nickname,
"%s", _("depth must be 8 or 16"));
vips_error(class->nickname, "%s", _("depth must be 8 or 16"));
return -1;
}

if (vips_cast_float(in, &t[0], NULL))
if (vips_cast_float(in, &t[1], NULL))
return -1;
in = t[0];
in = t[1];

out = vips_image_new();
if (vips_image_pipelinev(out,
Expand Down
1 change: 0 additions & 1 deletion libvips/foreign/foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@ vips__foreign_convert_saveable(VipsImage *in, VipsImage **ready,
in->Type != VIPS_INTERPRETATION_CMYK &&
in->Type != VIPS_INTERPRETATION_sRGB &&
in->Type != VIPS_INTERPRETATION_RGB16 &&
in->Type != VIPS_INTERPRETATION_scRGB &&
vips_colourspace_issupported(in) &&
(saveable == VIPS_SAVEABLE_RGB ||
saveable == VIPS_SAVEABLE_RGBA ||
Expand Down
19 changes: 15 additions & 4 deletions libvips/foreign/jxlsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
* - add animation support
*
* - libjxl is currently missing error messages (I think)
*
* - add support encoding images with > 4 bands.
*/

#define OUTPUT_BUFFER_SIZE (4096)
Expand Down Expand Up @@ -226,7 +228,7 @@ vips_foreign_save_jxl_build(VipsObject *object)
{
VipsForeignSave *save = (VipsForeignSave *) object;
VipsForeignSaveJxl *jxl = (VipsForeignSaveJxl *) object;
VipsImage **t = (VipsImage **) vips_object_local_array(object, 5);
VipsImage **t = (VipsImage **) vips_object_local_array(object, 2);

#ifdef HAVE_LIBJXL_0_7
JxlEncoderFrameSettings *frame_settings;
Expand Down Expand Up @@ -282,6 +284,17 @@ vips_foreign_save_jxl_build(VipsObject *object)
return -1;
in = t[0];

/* Mimics VIPS_SAVEABLE_RGBA.
* FIXME: add support encoding images with > 4 bands.
*/
if (in->Bands > 4) {
if (vips_extract_band(in, &t[1], 0,
"n", 4,
NULL))
return -1;
in = t[1];
}

JxlEncoderInitBasicInfo(&jxl->info);

switch (in->BandFmt) {
Expand Down Expand Up @@ -523,9 +536,7 @@ vips_foreign_save_jxl_class_init(VipsForeignSaveJxlClass *class)

foreign_class->suffs = vips__jxl_suffs;

/* This lets throuigh scRGB too, which we then save as jxl float.
*/
save_class->saveable = VIPS_SAVEABLE_RGBA;
save_class->saveable = VIPS_SAVEABLE_ANY;
save_class->format_table = bandfmt_jxl;

VIPS_ARG_INT(class, "tier", 10,
Expand Down

0 comments on commit 1130557

Please sign in to comment.