From 567c44e2ec5fba3e9219a89ba2dfbbc3dca801ad Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Fri, 18 Nov 2022 12:55:55 +0100 Subject: [PATCH] jxlload: simplify rewinding (#3170) This partially reverts commit e88db2ce. --- libvips/foreign/jxlload.c | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/libvips/foreign/jxlload.c b/libvips/foreign/jxlload.c index 343c2402..72b06537 100644 --- a/libvips/foreign/jxlload.c +++ b/libvips/foreign/jxlload.c @@ -158,6 +158,14 @@ vips_foreign_load_jxl_build( VipsObject *object ) jxl->runner = JxlThreadParallelRunnerCreate( NULL, vips_concurrency_get() ); + jxl->decoder = JxlDecoderCreate( NULL ); + + if( JxlDecoderSetParallelRunner( jxl->decoder, + JxlThreadParallelRunner, jxl->runner ) ) { + vips_foreign_load_jxl_error( jxl, + "JxlDecoderSetParallelRunner" ); + return( -1 ); + } if( VIPS_OBJECT_CLASS( vips_foreign_load_jxl_parent_class )-> build( object ) ) @@ -493,24 +501,19 @@ vips_foreign_load_jxl_header( VipsForeignLoad *load ) printf( "vips_foreign_load_jxl_header:\n" ); #endif /*DEBUG*/ - /* Build the decoder we will use for the header. - */ - jxl->decoder = JxlDecoderCreate( NULL ); - if( JxlDecoderSubscribeEvents( jxl->decoder, - JXL_DEC_COLOR_ENCODING | - JXL_DEC_BASIC_INFO ) ) { - vips_foreign_load_jxl_error( jxl, "JxlDecoderSubscribeEvents" ); + if( vips_source_rewind( jxl->source ) ) return( -1 ); - } - if( JxlDecoderSetParallelRunner( jxl->decoder, - JxlThreadParallelRunner, jxl->runner ) ) { - vips_foreign_load_jxl_error( jxl, - "JxlDecoderSetParallelRunner" ); + + JxlDecoderRewind( jxl->decoder ); + if( JxlDecoderSubscribeEvents( jxl->decoder, + JXL_DEC_COLOR_ENCODING | + JXL_DEC_BASIC_INFO ) ) { + vips_foreign_load_jxl_error( jxl, + "JxlDecoderSubscribeEvents" ); return( -1 ); } - if( vips_source_rewind( jxl->source ) || - vips_foreign_load_jxl_fill_input( jxl, 0 ) ) + if( vips_foreign_load_jxl_fill_input( jxl, 0 ) ) return( -1 ); JxlDecoderSetInput( jxl->decoder, jxl->input_buffer, jxl->bytes_in_buffer ); @@ -620,26 +623,21 @@ vips_foreign_load_jxl_load( VipsForeignLoad *load ) if( vips_foreign_load_jxl_set_header( jxl, t[0] ) ) return( -1 ); - /* We have to make a new decoder ... we can't be certain the header - * decoder left the input in the correct place. - */ - VIPS_FREEF( JxlDecoderDestroy, jxl->decoder ); + /* We have to rewind ... we can't be certain the header + * decoder left the input in the correct place. + */ + if( vips_source_rewind( jxl->source ) ) + return( -1 ); - jxl->decoder = JxlDecoderCreate( NULL ); + JxlDecoderRewind( jxl->decoder ); if( JxlDecoderSubscribeEvents( jxl->decoder, JXL_DEC_FULL_IMAGE ) ) { - vips_foreign_load_jxl_error( jxl, "JxlDecoderSubscribeEvents" ); - return( -1 ); - } - if( JxlDecoderSetParallelRunner( jxl->decoder, - JxlThreadParallelRunner, jxl->runner ) ) { - vips_foreign_load_jxl_error( jxl, - "JxlDecoderSetParallelRunner" ); + vips_foreign_load_jxl_error( jxl, + "JxlDecoderSubscribeEvents" ); return( -1 ); } - if( vips_source_rewind( jxl->source ) || - vips_foreign_load_jxl_fill_input( jxl, 0 ) ) + if( vips_foreign_load_jxl_fill_input( jxl, 0 ) ) return( -1 ); JxlDecoderSetInput( jxl->decoder, jxl->input_buffer, jxl->bytes_in_buffer );