Skip to content

Commit

Permalink
add libjxl ICC and scRGB support (libvips#2815)
Browse files Browse the repository at this point in the history
* add ICC and scRGB support

see libvips#2568

* final polish
  • Loading branch information
jcupitt authored May 28, 2022
1 parent ed79cfb commit 6260a37
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- add vips_tiffsave_target()
- add vips_target_end(), deprecate vips_target_finish()
- add "mixed" to webpsave [dloebl]
- add support for ICC profiles and linear encoding to JXL load and save [f1ac]
- add "reoptimise" to gifsave [dloebl]
- add "bitdepth" to magicksave [dloebl]

Expand Down
4 changes: 1 addition & 3 deletions libvips/foreign/jxlload.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/*
#define DEBUG_VERBOSE
#define DEBUG
*/
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
Expand Down Expand Up @@ -434,7 +434,6 @@ vips_foreign_load_jxl_set_header( VipsForeignLoadJxl *jxl, VipsImage *out )
break;

case VIPS_FORMAT_USHORT:
case VIPS_FORMAT_UINT:
interpretation = VIPS_INTERPRETATION_GREY16;
break;

Expand All @@ -451,7 +450,6 @@ vips_foreign_load_jxl_set_header( VipsForeignLoadJxl *jxl, VipsImage *out )
break;

case VIPS_FORMAT_USHORT:
case VIPS_FORMAT_UINT:
interpretation = VIPS_INTERPRETATION_RGB16;
break;

Expand Down
71 changes: 46 additions & 25 deletions libvips/foreign/jxlsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*
* 18/3/20
* - from heifload.c
* 21/5/22
* - add ICC profile support
*/

/*
Expand Down Expand Up @@ -31,7 +33,9 @@
*/

/*
#define DEBUG
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
Expand All @@ -53,8 +57,6 @@
#include "pforeign.h"

/* TODO:
*
* - libjxl currently seems to be missing API to attach a profile
*
* - libjxl encode only works in one shot mode, so there's no way to write in
* chunks
Expand All @@ -64,8 +66,6 @@
* - add animation support
*
* - libjxl is currently missing error messages (I think)
*
* - fix scRGB gamma
*/

#define OUTPUT_BUFFER_SIZE (4096)
Expand Down Expand Up @@ -330,8 +330,18 @@ vips_foreign_save_jxl_build( VipsObject *object )
jxl->info.intensity_target = stonits;
}

/* FIXME libjxl doesn't seem to have this API yet.
*
/* We will be setting the ICC profile, or calling
* JxlEncoderSetColorEncoding().
*/
jxl->info.uses_original_profile = TRUE;

if( JxlEncoderSetBasicInfo( jxl->encoder, &jxl->info ) ) {
vips_foreign_save_jxl_error( jxl, "JxlEncoderSetBasicInfo" );
return( -1 );
}

/* Set ICC profile, sRGB, or scRGB.
*/
if( vips_image_get_typeof( save->ready, VIPS_META_ICC_NAME ) ) {
const void *data;
size_t length;
Expand All @@ -340,28 +350,40 @@ vips_foreign_save_jxl_build( VipsObject *object )
VIPS_META_ICC_NAME, &data, &length ) )
return( -1 );

jxl->info.uses_original_profile = JXL_TRUE;
... attach profile
#ifdef DEBUG
printf( "attaching %zd bytes of ICC\n", length );
#endif /*DEBUG*/
if( JxlEncoderSetICCProfile( jxl->encoder,
(guint8 *) data, length ) ) {
vips_foreign_save_jxl_error( jxl,
"JxlEncoderSetColorEncoding" );
return( -1 );
}
}
else
jxl->info.uses_original_profile = JXL_FALSE;
*/
else {
if( save->ready->Type == VIPS_INTERPRETATION_scRGB ) {
#ifdef DEBUG
printf( "setting sRGB colourspace\n" );
#endif /*DEBUG*/

/* Remove this when libjxl gets API to attach an ICC profile.
*/
jxl->info.uses_original_profile = JXL_FALSE;
JxlColorEncodingSetToLinearSRGB( &jxl->color_encoding,
jxl->format.num_channels < 3 );
}
else {
#ifdef DEBUG
printf( "setting scRGB colourspace\n" );
#endif /*DEBUG*/

if( JxlEncoderSetBasicInfo( jxl->encoder, &jxl->info ) ) {
vips_foreign_save_jxl_error( jxl, "JxlEncoderSetBasicInfo" );
return( -1 );
}
JxlColorEncodingSetToSRGB( &jxl->color_encoding,
jxl->format.num_channels < 3 );
}

JxlColorEncodingSetToSRGB( &jxl->color_encoding,
jxl->format.num_channels < 3 );
if( JxlEncoderSetColorEncoding( jxl->encoder, &jxl->color_encoding ) ) {
vips_foreign_save_jxl_error( jxl,
"JxlEncoderSetColorEncoding" );
return( -1 );
if( JxlEncoderSetColorEncoding( jxl->encoder,
&jxl->color_encoding ) ) {
vips_foreign_save_jxl_error( jxl,
"JxlEncoderSetColorEncoding" );
return( -1 );
}
}

/* Render the entire image in memory. libjxl seems to be missing
Expand Down Expand Up @@ -430,7 +452,6 @@ vips_foreign_save_jxl_build( VipsObject *object )
*/
#define UC VIPS_FORMAT_UCHAR
#define US VIPS_FORMAT_USHORT
#define UI VIPS_FORMAT_UINT
#define F VIPS_FORMAT_FLOAT

/* Type promotion for save ... unsigned ints + float + double.
Expand Down

0 comments on commit 6260a37

Please sign in to comment.