Skip to content

Commit d98ddc1

Browse files
Update image-jpeg-xl.c
1 parent f7528f4 commit d98ddc1

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

cupsfilters/image-jpeg-xl.c

+39-25
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
6262
basic_info.xsize, basic_info.ysize,
6363
basic_info.bits_per_sample, basic_info.uses_original_profile));
6464

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+
6577
/* Determine the size needed for the output buffer.
6678
We request output as 16-bit unsigned integers to preserve high color depth. */
6779
status = JxlDecoderImageOutBufferSize(decoder, &pixel_format, &buffer_size);
@@ -84,6 +96,7 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
8496
if (status != JXL_DEC_SUCCESS) {
8597
DEBUG_printf("DEBUG: _cf_image_create_from_jxl_decoder: Failed to set output buffer.\n");
8698
free(output_buffer);
99+
free(img);
87100
return NULL;
88101
}
89102

@@ -92,27 +105,28 @@ _cf_image_create_from_jxl_decoder(JxlDecoder *decoder)
92105
if (status == JXL_DEC_ERROR) {
93106
DEBUG_printf("DEBUG: _cf_image_create_from_jxl_decoder: JPEG‑XL decoding error.\n");
94107
free(output_buffer);
108+
free(img);
95109
return NULL;
96110
}
97111
}
98112

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"));
106114

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);
111117

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);
116130
return img;
117131
}
118132

@@ -182,17 +196,17 @@ _cfImageReadJPEGXL(cf_image_t *img, FILE *fp,
182196
}
183197

184198
/* 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);
191209
}
192-
193-
/* Copy the new image into the provided structure */
194-
memcpy(img, new_img, sizeof(cf_image_t));
195-
free(new_img);
196210

197211
JxlDecoderDestroy(decoder);
198212
free(data);

0 commit comments

Comments
 (0)