Skip to content

add libjxl ICC and scRGB support #2815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

26/11/21 started 8.12.3
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