@@ -62,6 +62,18 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
62
62
basic_info .xsize , basic_info .ysize ,
63
63
basic_info .bits_per_sample , basic_info .uses_original_profile ));
64
64
65
+ /* Allocate the cf_image_t structure and set dimensions */
66
+ img = malloc (sizeof (cf_image_t ));
67
+ if (!img ) {
68
+ DEBUG_printf (("DEBUG: _cf_image_create_from_jxl_decoder: Memory allocation for image structure failed.\n" ));
69
+ return NULL ;
70
+ }
71
+ img -> xsize = (int ) basic_info .xsize ;
72
+ img -> ysize = (int ) basic_info .ysize ;
73
+ img -> xppi = (basic_info .uses_original_profile ) ? 300 : 72 ;
74
+ img -> yppi = (basic_info .uses_original_profile ) ? 300 : 72 ;
75
+ img -> colorspace = CF_IMAGE_RGB ;
76
+
65
77
/* Determine the size needed for the output buffer.
66
78
We request output as 16-bit unsigned integers to preserve high color depth. */
67
79
status = JxlDecoderImageOutBufferSize (decoder , & pixel_format , & buffer_size );
@@ -84,6 +96,7 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
84
96
if (status != JXL_DEC_SUCCESS ) {
85
97
DEBUG_printf ("DEBUG: _cf_image_create_from_jxl_decoder: Failed to set output buffer.\n" );
86
98
free (output_buffer );
99
+ free (img );
87
100
return NULL ;
88
101
}
89
102
@@ -92,27 +105,28 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
92
105
if (status == JXL_DEC_ERROR ) {
93
106
DEBUG_printf ("DEBUG: _cf_image_create_from_jxl_decoder: JPEG‑XL decoding error.\n" );
94
107
free (output_buffer );
108
+ free (img );
95
109
return NULL ;
96
110
}
97
111
}
98
112
99
- /* Allocate and populate the cf_image_t structure */
100
- img = malloc (sizeof (cf_image_t ));
101
- if (!img ) {
102
- DEBUG_printf ("DEBUG: _cf_image_create_from_jxl_decoder: Memory allocation for image structure failed.\n" );
103
- free (output_buffer );
104
- return NULL ;
105
- }
113
+ DEBUG_printf (("DEBUG: _cf_image_create_from_jxl_decoder: Decoding complete.\n" ));
106
114
107
- img -> xsize = (int )basic_info .xsize ;
108
- img -> ysize = (int )basic_info .ysize ;
109
- img -> xppi = (basic_info .uses_original_profile ) ? 300 : 72 ; // Adjust if necessary
110
- img -> yppi = (basic_info .uses_original_profile ) ? 300 : 72 ; // Adjust if necessary
115
+ /* Initialize tile cache for the image */
116
+ cfImageSetMaxTiles (img , 0 );
111
117
112
- /* For JPEG‑XL, we assume the decoded format is RGB. */
113
- img -> colorspace = CF_IMAGE_RGB ;
114
- img -> data = output_buffer ;
115
-
118
+ /* Copy decoded data row by row into the image tile cache */
119
+ {
120
+ int row ;
121
+ int bytes_per_pixel = pixel_format .num_channels * 1 ; /* 1 byte per channel */
122
+ int row_stride = img -> xsize * bytes_per_pixel ;
123
+ for (row = 0 ; row < img -> ysize ; row ++ ) {
124
+ unsigned char * row_data = ((unsigned char * ) output_buffer ) + row * row_stride ;
125
+ _cfImagePutRow (img , 0 , row , img -> xsize , row_data );
126
+ }
127
+ }
128
+
129
+ free (output_buffer );
116
130
return img ;
117
131
}
118
132
@@ -182,17 +196,17 @@ _cfImageReadJPEGXL(cf_image_t *img, FILE *fp,
182
196
}
183
197
184
198
/* Create an image from the decoder output */
185
- cf_image_t * new_img = _cf_image_create_from_jxl_decoder (decoder );
186
- if (!new_img ) {
187
- DEBUG_printf ("DEBUG: JPEG‑XL: Failed to create image from decoder data.\n" );
188
- JxlDecoderDestroy (decoder );
189
- free (data );
190
- return -1 ;
199
+ {
200
+ cf_image_t * new_img = _cf_image_create_from_jxl_decoder (decoder , fp , primary , secondary , saturation , hue , lut );
201
+ if (!new_img ) {
202
+ DEBUG_printf (("DEBUG: _cfImageReadJPEGXL: Failed to create image from decoder data.\n" ));
203
+ JxlDecoderDestroy (decoder );
204
+ free (data );
205
+ return -1 ;
206
+ }
207
+ memcpy (img , new_img , sizeof (cf_image_t ));
208
+ free (new_img );
191
209
}
192
-
193
- /* Copy the new image into the provided structure */
194
- memcpy (img , new_img , sizeof (cf_image_t ));
195
- free (new_img );
196
210
197
211
JxlDecoderDestroy (decoder );
198
212
free (data );
0 commit comments