Debian Codegraph @

coders/jxl.c

raw symbols SCIP
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % JJJ X X L %
7 % J X X L %
8 % J X L %
9 % J J X X L %
10 % JJ X X LLLLL %
11 % %
12 % %
13 % JPEG XL (ISO/IEC 18181) %
14 % %
15 % Dirk Lemstra %
16 % December 2020 %
17 % %
18 % %
19 % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
21 % %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
24 % %
25 % https://imagemagick.org/license/ %
26 % %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
32 % %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %
36 */
37
38 /*
39 Include declarations.
40 */
41 #include "MagickCore/studio.h"
42 #include "MagickCore/attribute.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/channel.h"
47 #include "MagickCore/colorspace-private.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/layer.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/option.h"
59 #include "MagickCore/profile-private.h"
60 #include "MagickCore/property.h"
61 #include "MagickCore/resource_.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/string-private.h"
65 #include "MagickCore/module.h"
66 #if defined(MAGICKCORE_JXL_DELEGATE)
67 #include <jxl/decode.h>
68 #include <jxl/encode.h>
69 #include <jxl/thread_parallel_runner.h>
70 #include <jxl/version.h>
71 #endif
72
73 /*
74 Typedef declarations.
75 */
76 typedef struct MemoryManagerInfo
77 {
78 Image
79 *image;
80
81 ExceptionInfo
82 *exception;
83 } MemoryManagerInfo;
84
85 #if defined(MAGICKCORE_JXL_DELEGATE)
86 /*
87 Forward declarations.
88 */
89 static MagickBooleanType
90 WriteJXLImage(const ImageInfo *,Image *,ExceptionInfo *);
91
92 /*
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % %
95 % %
96 % %
97 % I s J X L %
98 % %
99 % %
100 % %
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 %
103 % IsJXL() returns MagickTrue if the image format type, identified by the
104 % magick string, is JXL.
105 %
106 % The format of the IsJXL method is:
107 %
108 % MagickBooleanType IsJXL(const unsigned char *magick,const size_t length)
109 %
110 % A description of each parameter follows:
111 %
112 % o magick: compare image format pattern against these bytes.
113 %
114 % o length: Specifies the length of the magick string.
115 %
116 */
117 static MagickBooleanType IsJXL(const unsigned char *magick,const size_t length)
118 {
119 JxlSignature
120 signature = JxlSignatureCheck(magick,length);
121
122 if ((signature == JXL_SIG_NOT_ENOUGH_BYTES) || (signature == JXL_SIG_INVALID))
123 return(MagickFalse);
124 return(MagickTrue);
125 }
126
127 /*
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % %
130 % %
131 % %
132 % R e a d J X L I m a g e %
133 % %
134 % %
135 % %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %
138 % ReadJXLImage() reads a JXL image file and returns it. It allocates
139 % the memory necessary for the new Image structure and returns a pointer to
140 % the new image.
141 %
142 % The format of the ReadJXLImage method is:
143 %
144 % Image *ReadJXLImage(const ImageInfo *image_info,
145 % ExceptionInfo *exception)
146 %
147 % A description of each parameter follows:
148 %
149 % o image_info: the image info.
150 %
151 % o exception: return any errors or warnings in this structure.
152 %
153 */
154
155 static void *JXLAcquireMemory(void *opaque,size_t size)
156 {
157 unsigned char
158 *data;
159
160 data=(unsigned char *) AcquireQuantumMemory(size,sizeof(*data));
161 if (data == (unsigned char *) NULL)
162 {
163 MemoryManagerInfo
164 *memory_manager_info;
165
166 memory_manager_info=(MemoryManagerInfo *) opaque;
167 (void) ThrowMagickException(memory_manager_info->exception,
168 GetMagickModule(),CoderError,"MemoryAllocationFailed","`%s'",
169 memory_manager_info->image->filename);
170 }
171 return(data);
172 }
173
174 static inline StorageType JXLDataTypeToStorageType(Image *image,
175 const JxlDataType data_type,ExceptionInfo *exception)
176 {
177 switch (data_type)
178 {
179 case JXL_TYPE_FLOAT:
180 return(FloatPixel);
181 case JXL_TYPE_FLOAT16:
182 (void) SetImageProperty(image,"quantum:format","floating-point",
183 exception);
184 return(FloatPixel);
185 case JXL_TYPE_UINT16:
186 return(ShortPixel);
187 case JXL_TYPE_UINT8:
188 return(CharPixel);
189 default:
190 return(UndefinedPixel);
191 }
192 }
193
194 static inline JxlOrientation OrientationToJXLOrientation(
195 const OrientationType orientation)
196 {
197 switch (orientation)
198 {
199 default:
200 case TopLeftOrientation:
201 return(JXL_ORIENT_IDENTITY);
202 case TopRightOrientation:
203 return(JXL_ORIENT_FLIP_HORIZONTAL);
204 case BottomRightOrientation:
205 return(JXL_ORIENT_ROTATE_180);
206 case BottomLeftOrientation:
207 return(JXL_ORIENT_FLIP_VERTICAL);
208 case LeftTopOrientation:
209 return(JXL_ORIENT_TRANSPOSE);
210 case RightTopOrientation:
211 return(JXL_ORIENT_ROTATE_90_CW);
212 case RightBottomOrientation:
213 return(JXL_ORIENT_ANTI_TRANSPOSE);
214 case LeftBottomOrientation:
215 return(JXL_ORIENT_ROTATE_90_CCW);
216 }
217 }
218
219 static inline OrientationType JXLOrientationToOrientation(
220 const JxlOrientation orientation)
221 {
222 switch (orientation)
223 {
224 default:
225 case JXL_ORIENT_IDENTITY:
226 return(TopLeftOrientation);
227 case JXL_ORIENT_FLIP_HORIZONTAL:
228 return(TopRightOrientation);
229 case JXL_ORIENT_ROTATE_180:
230 return(BottomRightOrientation);
231 case JXL_ORIENT_FLIP_VERTICAL:
232 return(BottomLeftOrientation);
233 case JXL_ORIENT_TRANSPOSE:
234 return(LeftTopOrientation);
235 case JXL_ORIENT_ROTATE_90_CW:
236 return(RightTopOrientation);
237 case JXL_ORIENT_ANTI_TRANSPOSE:
238 return(RightBottomOrientation);
239 case JXL_ORIENT_ROTATE_90_CCW:
240 return(LeftBottomOrientation);
241 }
242 }
243
244 static void JXLRelinquishMemory(void *magick_unused(opaque),void *address)
245 {
246 magick_unreferenced(opaque);
247 (void) RelinquishMagickMemory(address);
248 }
249
250 static inline void JXLSetMemoryManager(JxlMemoryManager *memory_manager,
251 MemoryManagerInfo *memory_manager_info,Image *image,ExceptionInfo *exception)
252 {
253 memory_manager_info->image=image;
254 memory_manager_info->exception=exception;
255 memory_manager->opaque=memory_manager_info;
256 memory_manager->alloc=JXLAcquireMemory;
257 memory_manager->free=JXLRelinquishMemory;
258 }
259
260 static inline void JXLSetFormat(Image *image,JxlPixelFormat *pixel_format,
261 ExceptionInfo *exception)
262 {
263 const char
264 *property;
265
266 pixel_format->num_channels=((image->alpha_trait & BlendPixelTrait) != 0) ?
267 4U : 3U;
268 if (IsGrayColorspace(image->colorspace) != MagickFalse)
269 pixel_format->num_channels=((image->alpha_trait & BlendPixelTrait) != 0) ?
270 2U : 1U;
271 pixel_format->data_type=(image->depth > 16) ? JXL_TYPE_FLOAT :
272 (image->depth > 8) ? JXL_TYPE_UINT16 : JXL_TYPE_UINT8;
273 property=GetImageProperty(image,"quantum:format",exception);
274 if (property != (char *) NULL)
275 {
276 QuantumFormatType format = (QuantumFormatType) ParseCommandOption(
277 MagickQuantumFormatOptions,MagickFalse,property);
278 if ((format == FloatingPointQuantumFormat) &&
279 (pixel_format->data_type == JXL_TYPE_UINT16))
280 {
281 pixel_format->data_type=JXL_TYPE_FLOAT16;
282 (void) SetImageProperty(image,"quantum:format","floating-point",
283 exception);
284 }
285 }
286 }
287
288 static inline void JXLInitImage(Image *image,JxlBasicInfo *basic_info)
289 {
290 image->columns=basic_info->xsize;
291 image->rows=basic_info->ysize;
292 image->depth=basic_info->bits_per_sample;
293 if (basic_info->alpha_bits != 0)
294 image->alpha_trait=BlendPixelTrait;
295 image->orientation=JXLOrientationToOrientation(basic_info->orientation);
296 if (basic_info->have_animation == JXL_TRUE)
297 {
298 if ((basic_info->animation.tps_numerator > 0) &&
299 (basic_info->animation.tps_denominator > 0))
300 image->ticks_per_second=basic_info->animation.tps_numerator /
301 basic_info->animation.tps_denominator;
302 image->iterations=basic_info->animation.num_loops;
303 }
304 }
305
306 static inline MagickBooleanType JXLPatchExifProfile(StringInfo *exif_profile)
307 {
308 size_t
309 exif_length;
310
311 StringInfo
312 *snippet;
313
314 unsigned char
315 *exif_datum;
316
317 unsigned int
318 offset=0;
319
320 if (GetStringInfoLength(exif_profile) < 4)
321 return(MagickFalse);
322
323 /*
324 Extract and cache Exif profile.
325 */
326 snippet=SplitStringInfo(exif_profile,4);
327 offset|=(unsigned int) (*(GetStringInfoDatum(snippet)+0)) << 24;
328 offset|=(unsigned int) (*(GetStringInfoDatum(snippet)+1)) << 16;
329 offset|=(unsigned int) (*(GetStringInfoDatum(snippet)+2)) << 8;
330 offset|=(unsigned int) (*(GetStringInfoDatum(snippet)+3)) << 0;
331 snippet=DestroyStringInfo(snippet);
332 /*
333 Strip any EOI marker if payload starts with a JPEG marker.
334 */
335 exif_length=GetStringInfoLength(exif_profile);
336 exif_datum=GetStringInfoDatum(exif_profile);
337 if ((exif_length > 2) &&
338 ((memcmp(exif_datum,"\xff\xd8",2) == 0) ||
339 (memcmp(exif_datum,"\xff\xe1",2) == 0)) &&
340 (memcmp(exif_datum+exif_length-2,"\xff\xd9",2) == 0))
341 SetStringInfoLength(exif_profile,exif_length-2);
342 /*
343 Skip to actual Exif payload.
344 */
345 if (offset < GetStringInfoLength(exif_profile))
346 (void) DestroyStringInfo(SplitStringInfo(exif_profile,offset));
347 return(MagickTrue);
348 }
349
350 static inline void JXLAddProfilesToImage(Image *image,
351 StringInfo **exif_profile,StringInfo **xmp_profile,ExceptionInfo *exception)
352 {
353 if (*exif_profile != (StringInfo *) NULL)
354 {
355 if (JXLPatchExifProfile(*exif_profile) != MagickFalse)
356 {
357 (void) SetImageProfilePrivate(image,*exif_profile,exception);
358 *exif_profile=(StringInfo *) NULL;
359 }
360 else
361 *exif_profile=DestroyStringInfo(*exif_profile);
362 }
363 if (*xmp_profile != (StringInfo *) NULL)
364 {
365 (void) SetImageProfilePrivate(image,*xmp_profile,exception);
366 *xmp_profile=(StringInfo *) NULL;
367 }
368 }
369
370 static Image *ReadJXLImage(const ImageInfo *image_info,
371 ExceptionInfo *exception)
372 {
373 Image
374 *image;
375
376 JxlBasicInfo
377 basic_info;
378
379 JxlDecoder
380 *jxl_info;
381
382 JxlDecoderStatus
383 events_wanted,
384 jxl_status;
385
386 JxlMemoryManager
387 memory_manager;
388
389 JxlPixelFormat
390 pixel_format;
391
392 MagickBooleanType
393 status;
394
395 MemoryManagerInfo
396 memory_manager_info;
397
398 size_t
399 extent = 0,
400 image_count = 0,
401 input_size;
402
403 StringInfo
404 *exif_profile = (StringInfo *) NULL,
405 *xmp_profile = (StringInfo *) NULL;
406
407 unsigned char
408 *pixels,
409 *output_buffer = (unsigned char *) NULL;
410
411 void
412 *runner = NULL;
413
414 /*
415 Open image file.
416 */
417 assert(image_info != (const ImageInfo *) NULL);
418 assert(image_info->signature == MagickCoreSignature);
419 assert(exception != (ExceptionInfo *) NULL);
420 assert(exception->signature == MagickCoreSignature);
421 if (IsEventLogging() != MagickFalse)
422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
423 image_info->filename);
424 image=AcquireImage(image_info, exception);
425 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
426 if (status == MagickFalse)
427 {
428 image=DestroyImageList(image);
429 return((Image *) NULL);
430 }
431 /*
432 Initialize JXL delegate library.
433 */
434 (void) memset(&basic_info,0,sizeof(basic_info));
435 (void) memset(&pixel_format,0,sizeof(pixel_format));
436 JXLSetMemoryManager(&memory_manager,&memory_manager_info,image,exception);
437 jxl_info=JxlDecoderCreate(&memory_manager);
438 if (jxl_info == (JxlDecoder *) NULL)
439 ThrowReaderException(CoderError,"MemoryAllocationFailed");
440 (void) JxlDecoderSetKeepOrientation(jxl_info,JXL_TRUE);
441 (void) JxlDecoderSetUnpremultiplyAlpha(jxl_info,JXL_TRUE);
442 events_wanted=(JxlDecoderStatus) (JXL_DEC_BASIC_INFO | JXL_DEC_BOX |
443 JXL_DEC_FRAME);
444 if (image_info->ping == MagickFalse)
445 {
446 events_wanted=(JxlDecoderStatus) (events_wanted | JXL_DEC_FULL_IMAGE |
447 JXL_DEC_COLOR_ENCODING);
448 runner=JxlThreadParallelRunnerCreate(NULL,(size_t) GetMagickResourceLimit(
449 ThreadResource));
450 if (runner == (void *) NULL)
451 {
452 JxlDecoderDestroy(jxl_info);
453 ThrowReaderException(CoderError,"MemoryAllocationFailed");
454 }
455 jxl_status=JxlDecoderSetParallelRunner(jxl_info,JxlThreadParallelRunner,
456 runner);
457 if (jxl_status != JXL_DEC_SUCCESS)
458 {
459 JxlThreadParallelRunnerDestroy(runner);
460 JxlDecoderDestroy(jxl_info);
461 ThrowReaderException(CoderError,"MemoryAllocationFailed");
462 }
463 }
464 if (JxlDecoderSubscribeEvents(jxl_info,(int) events_wanted) != JXL_DEC_SUCCESS)
465 {
466 if (runner != NULL)
467 JxlThreadParallelRunnerDestroy(runner);
468 JxlDecoderDestroy(jxl_info);
469 ThrowReaderException(CoderError,"UnableToReadImageData");
470 }
471 input_size=MagickMaxBufferExtent;
472 pixels=(unsigned char *) AcquireQuantumMemory(input_size,sizeof(*pixels));
473 if (pixels == (unsigned char *) NULL)
474 {
475 if (runner != NULL)
476 JxlThreadParallelRunnerDestroy(runner);
477 JxlDecoderDestroy(jxl_info);
478 ThrowReaderException(CoderError,"MemoryAllocationFailed");
479 }
480 /*
481 Decode JXL byte stream.
482 */
483 jxl_status=JXL_DEC_NEED_MORE_INPUT;
484 while ((jxl_status != JXL_DEC_SUCCESS) && (jxl_status != JXL_DEC_ERROR))
485 {
486 jxl_status=JxlDecoderProcessInput(jxl_info);
487 switch (jxl_status)
488 {
489 case JXL_DEC_NEED_MORE_INPUT:
490 {
491 size_t
492 remaining;
493
494 ssize_t
495 count;
496
497 remaining=JxlDecoderReleaseInput(jxl_info);
498 if (remaining > 0)
499 (void) memmove(pixels,pixels+input_size-remaining,remaining);
500 count=ReadBlob(image,input_size-remaining,pixels+remaining);
501 if (count <= 0)
502 {
503 JxlDecoderCloseInput(jxl_info);
504 break;
505 }
506 jxl_status=JxlDecoderSetInput(jxl_info,(const uint8_t *) pixels,
507 (size_t) count);
508 if (jxl_status == JXL_DEC_SUCCESS)
509 jxl_status=JXL_DEC_NEED_MORE_INPUT;
510 break;
511 }
512 case JXL_DEC_BASIC_INFO:
513 {
514 jxl_status=JxlDecoderGetBasicInfo(jxl_info,&basic_info);
515 if (jxl_status != JXL_DEC_SUCCESS)
516 break;
517 if ((basic_info.have_animation == JXL_TRUE) &&
518 (basic_info.animation.have_timecodes == JXL_TRUE))
519 {
520 /*
521 We currently don't support animations with time codes.
522 */
523 (void) ThrowMagickException(exception,GetMagickModule(),
524 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
525 image->filename);
526 break;
527 }
528 JXLInitImage(image,&basic_info);
529 jxl_status=JXL_DEC_BASIC_INFO;
530 status=SetImageExtent(image,image->columns,image->rows,exception);
531 if (status == MagickFalse)
532 {
533 jxl_status=JXL_DEC_ERROR;
534 break;
535 }
536 break;
537 }
538 case JXL_DEC_COLOR_ENCODING:
539 {
540 JxlColorEncoding
541 color_encoding;
542
543 size_t
544 profile_size;
545
546 StringInfo
547 *profile;
548
549 (void) memset(&color_encoding,0,sizeof(color_encoding));
550 JXLSetFormat(image,&pixel_format,exception);
551 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0,9,0)
552 jxl_status=JxlDecoderGetColorAsEncodedProfile(jxl_info,
553 JXL_COLOR_PROFILE_TARGET_DATA,&color_encoding);
554 #else
555 jxl_status=JxlDecoderGetColorAsEncodedProfile(jxl_info,&pixel_format,
556 JXL_COLOR_PROFILE_TARGET_DATA,&color_encoding);
557 #endif
558 if (jxl_status == JXL_DEC_SUCCESS)
559 {
560 if (color_encoding.transfer_function == JXL_TRANSFER_FUNCTION_LINEAR)
561 {
562 image->colorspace=RGBColorspace;
563 image->gamma=1.0;
564 }
565 if (color_encoding.color_space == JXL_COLOR_SPACE_GRAY)
566 {
567 image->colorspace=GRAYColorspace;
568 if (color_encoding.transfer_function == JXL_TRANSFER_FUNCTION_LINEAR)
569 {
570 image->colorspace=LinearGRAYColorspace;
571 image->gamma=1.0;
572 }
573 }
574 if (color_encoding.white_point == JXL_WHITE_POINT_CUSTOM)
575 {
576 image->chromaticity.white_point.x=
577 color_encoding.white_point_xy[0];
578 image->chromaticity.white_point.y=
579 color_encoding.white_point_xy[1];
580 }
581 if (color_encoding.primaries == JXL_PRIMARIES_CUSTOM)
582 {
583 image->chromaticity.red_primary.x=
584 color_encoding.primaries_red_xy[0];
585 image->chromaticity.red_primary.y=
586 color_encoding.primaries_red_xy[1];
587 image->chromaticity.green_primary.x=
588 color_encoding.primaries_green_xy[0];
589 image->chromaticity.green_primary.y=
590 color_encoding.primaries_green_xy[1];
591 image->chromaticity.blue_primary.x=
592 color_encoding.primaries_blue_xy[0];
593 image->chromaticity.blue_primary.y=
594 color_encoding.primaries_blue_xy[1];
595 }
596 if (color_encoding.transfer_function == JXL_TRANSFER_FUNCTION_GAMMA)
597 image->gamma=color_encoding.gamma;
598 switch (color_encoding.rendering_intent)
599 {
600 case JXL_RENDERING_INTENT_PERCEPTUAL:
601 {
602 image->rendering_intent=PerceptualIntent;
603 break;
604 }
605 case JXL_RENDERING_INTENT_RELATIVE:
606 {
607 image->rendering_intent=RelativeIntent;
608 break;
609 }
610 case JXL_RENDERING_INTENT_SATURATION:
611 {
612 image->rendering_intent=SaturationIntent;
613 break;
614 }
615 case JXL_RENDERING_INTENT_ABSOLUTE:
616 {
617 image->rendering_intent=AbsoluteIntent;
618 break;
619 }
620 default:
621 {
622 image->rendering_intent=UndefinedIntent;
623 break;
624 }
625 }
626 }
627 else
628 if (jxl_status != JXL_DEC_ERROR)
629 break;
630 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0,9,0)
631 jxl_status=JxlDecoderGetICCProfileSize(jxl_info,
632 JXL_COLOR_PROFILE_TARGET_ORIGINAL,&profile_size);
633 #else
634 jxl_status=JxlDecoderGetICCProfileSize(jxl_info,&pixel_format,
635 JXL_COLOR_PROFILE_TARGET_ORIGINAL,&profile_size);
636 #endif
637 if (jxl_status != JXL_DEC_SUCCESS)
638 break;
639 profile=AcquireProfileStringInfo("icc",profile_size,exception);
640 if (profile != (StringInfo *) NULL)
641 {
642 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0,9,0)
643 jxl_status=JxlDecoderGetColorAsICCProfile(jxl_info,
644 JXL_COLOR_PROFILE_TARGET_ORIGINAL,GetStringInfoDatum(profile),
645 profile_size);
646 #else
647 jxl_status=JxlDecoderGetColorAsICCProfile(jxl_info,&pixel_format,
648 JXL_COLOR_PROFILE_TARGET_ORIGINAL,GetStringInfoDatum(profile),
649 profile_size);
650 #endif
651 if (jxl_status == JXL_DEC_SUCCESS)
652 (void) SetImageProfilePrivate(image,profile,exception);
653 else
654 profile=DestroyStringInfo(profile);
655 }
656 if (jxl_status == JXL_DEC_SUCCESS)
657 jxl_status=JXL_DEC_COLOR_ENCODING;
658 break;
659 }
660 case JXL_DEC_FRAME:
661 {
662 JxlFrameHeader
663 frame_header;
664
665 if (image_count++ != 0)
666 {
667 JXLAddProfilesToImage(image,&exif_profile,&xmp_profile,exception);
668 /*
669 Allocate next image structure.
670 */
671 AcquireNextImage(image_info,image,exception);
672 if (GetNextImageInList(image) == (Image *) NULL)
673 break;
674 image=SyncNextImageInList(image);
675 JXLInitImage(image,&basic_info);
676 status=SetImageExtent(image,image->columns,image->rows,exception);
677 if (status == MagickFalse)
678 {
679 jxl_status=JXL_DEC_ERROR;
680 break;
681 }
682 }
683 (void) memset(&frame_header,0,sizeof(frame_header));
684 if (JxlDecoderGetFrameHeader(jxl_info,&frame_header) == JXL_DEC_SUCCESS)
685 image->delay=(size_t) frame_header.duration;
686 if ((basic_info.have_animation == JXL_TRUE) &&
687 (basic_info.alpha_bits != 0))
688 image->dispose=BackgroundDispose;
689 break;
690 }
691 case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
692 {
693 status=SetImageExtent(image,image->columns,image->rows,exception);
694 if (status == MagickFalse)
695 {
696 jxl_status=JXL_DEC_ERROR;
697 break;
698 }
699 (void) ResetImagePixels(image,exception);
700 JXLSetFormat(image,&pixel_format,exception);
701 if (extent == 0)
702 {
703 jxl_status=JxlDecoderImageOutBufferSize(jxl_info,&pixel_format,
704 &extent);
705 if (jxl_status != JXL_DEC_SUCCESS)
706 break;
707 output_buffer=(unsigned char *) AcquireQuantumMemory(extent,
708 sizeof(*output_buffer));
709 if (output_buffer == (unsigned char *) NULL)
710 {
711 (void) ThrowMagickException(exception,GetMagickModule(),
712 CoderError,"MemoryAllocationFailed","`%s'",image->filename);
713 break;
714 }
715 }
716 jxl_status=JxlDecoderSetImageOutBuffer(jxl_info,&pixel_format,
717 output_buffer,extent);
718 if (jxl_status == JXL_DEC_SUCCESS)
719 jxl_status=JXL_DEC_NEED_IMAGE_OUT_BUFFER;
720 break;
721 }
722 case JXL_DEC_FULL_IMAGE:
723 {
724 const char
725 *map = "RGB";
726
727 StorageType
728 type;
729
730 if (output_buffer == (unsigned char *) NULL)
731 {
732 (void) ThrowMagickException(exception,GetMagickModule(),
733 CorruptImageError,"UnableToReadImageData","`%s'",image->filename);
734 break;
735 }
736 type=JXLDataTypeToStorageType(image,pixel_format.data_type,exception);
737 if (type == UndefinedPixel)
738 {
739 (void) ThrowMagickException(exception,GetMagickModule(),
740 CorruptImageError,"Unsupported data type","`%s'",image->filename);
741 break;
742 }
743 if ((image->alpha_trait & BlendPixelTrait) != 0)
744 map="RGBA";
745 if (IsGrayColorspace(image->colorspace) != MagickFalse)
746 {
747 map="I";
748 if ((image->alpha_trait & BlendPixelTrait) != 0)
749 map="IA";
750 }
751 status=ImportImagePixels(image,0,0,image->columns,image->rows,map,
752 type,output_buffer,exception);
753 if (status == MagickFalse)
754 jxl_status=JXL_DEC_ERROR;
755 break;
756 }
757 case JXL_DEC_BOX:
758 {
759 JxlBoxType
760 type;
761
762 uint64_t
763 size;
764
765 (void) JxlDecoderReleaseBoxBuffer(jxl_info);
766 jxl_status=JxlDecoderGetBoxType(jxl_info,type,JXL_FALSE);
767 if (jxl_status != JXL_DEC_SUCCESS)
768 break;
769 jxl_status=JxlDecoderGetBoxSizeRaw(jxl_info,&size);
770 if ((jxl_status != JXL_DEC_SUCCESS) || (size <= 8))
771 break;
772 size-=8;
773 if (LocaleNCompare(type,"Exif",sizeof(type)) == 0)
774 {
775 /*
776 Read Exif profile.
777 */
778 if (exif_profile == (StringInfo *) NULL)
779 {
780 exif_profile=AcquireProfileStringInfo("exif",(size_t) size,
781 exception);
782 if (exif_profile != (StringInfo *) NULL)
783 jxl_status=JxlDecoderSetBoxBuffer(jxl_info,
784 GetStringInfoDatum(exif_profile),(size_t) size);
785 }
786 }
787 if (LocaleNCompare(type,"xml ",sizeof(type)) == 0)
788 {
789 /*
790 Read XMP profile.
791 */
792 if (xmp_profile == (StringInfo *) NULL)
793 {
794 xmp_profile=AcquireProfileStringInfo("xmp",(size_t) size,
795 exception);
796 if (xmp_profile != (StringInfo *) NULL)
797 jxl_status=JxlDecoderSetBoxBuffer(jxl_info,
798 GetStringInfoDatum(xmp_profile),(size_t) size);
799 }
800 }
801 if (jxl_status == JXL_DEC_SUCCESS)
802 jxl_status=JXL_DEC_BOX;
803 break;
804 }
805 case JXL_DEC_SUCCESS:
806 case JXL_DEC_ERROR:
807 break;
808 default:
809 {
810 (void) ThrowMagickException(exception,GetMagickModule(),
811 CorruptImageError,"Unsupported status type","`%d'",jxl_status);
812 jxl_status=JXL_DEC_ERROR;
813 break;
814 }
815 }
816 }
817 (void) JxlDecoderReleaseBoxBuffer(jxl_info);
818 JXLAddProfilesToImage(image,&exif_profile,&xmp_profile,exception);
819 output_buffer=(unsigned char *) RelinquishMagickMemory(output_buffer);
820 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
821 if (runner != NULL)
822 JxlThreadParallelRunnerDestroy(runner);
823 JxlDecoderDestroy(jxl_info);
824 if (jxl_status == JXL_DEC_ERROR)
825 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
826 if (CloseBlob(image) == MagickFalse)
827 status=MagickFalse;
828 if (status == MagickFalse)
829 return(DestroyImageList(image));
830 return(GetFirstImageInList(image));
831 }
832 #endif
833
834 /*
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836 % %
837 % %
838 % %
839 % R e g i s t e r J X L I m a g e %
840 % %
841 % %
842 % %
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 %
845 % RegisterJXLImage() adds properties for the JXL image format to
846 % the list of supported formats. The properties include the image format
847 % tag, a method to read and/or write the format, whether the format
848 % supports the saving of more than one frame to the same file or blob,
849 % whether the format supports native in-memory I/O, and a brief
850 % description of the format.
851 %
852 % The format of the RegisterJXLImage method is:
853 %
854 % size_t RegisterJXLImage(void)
855 %
856 */
857 ModuleExport size_t RegisterJXLImage(void)
858 {
859 char
860 version[MagickPathExtent];
861
862 MagickInfo
863 *entry;
864
865 *version='\0';
866 #if defined(MAGICKCORE_JXL_DELEGATE)
867 (void) FormatLocaleString(version,MagickPathExtent,"libjxl %u.%u.%u",
868 (JxlDecoderVersion()/1000000),(JxlDecoderVersion()/1000) % 1000,
869 (JxlDecoderVersion() % 1000));
870 #endif
871 entry=AcquireMagickInfo("JXL", "JXL", "JPEG XL (ISO/IEC 18181)");
872 #if defined(MAGICKCORE_JXL_DELEGATE)
873 entry->decoder=(DecodeImageHandler *) ReadJXLImage;
874 entry->encoder=(EncodeImageHandler *) WriteJXLImage;
875 entry->magick=(IsImageFormatHandler *) IsJXL;
876 #endif
877 entry->mime_type=ConstantString("image/jxl");
878 if (*version != '\0')
879 entry->version=ConstantString(version);
880 (void) RegisterMagickInfo(entry);
881 return(MagickImageCoderSignature);
882 }
883
884 /*
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % %
887 % %
888 % %
889 % U n r e g i s t e r J X L I m a g e %
890 % %
891 % %
892 % %
893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 %
895 % UnregisterJXLImage() removes format registrations made by the
896 % JXL module from the list of supported formats.
897 %
898 % The format of the UnregisterJXLImage method is:
899 %
900 % UnregisterJXLImage(void)
901 %
902 */
903 ModuleExport void UnregisterJXLImage(void)
904 {
905 (void) UnregisterMagickInfo("JXL");
906 }
907
908 #if defined(MAGICKCORE_JXL_DELEGATE)
909 /*
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % %
912 % %
913 % %
914 % W r i t e J X L I m a g e %
915 % %
916 % %
917 % %
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 %
920 % WriteJXLImage() writes a JXL image file and returns it. It
921 % allocates the memory necessary for the new Image structure and returns a
922 % pointer to the new image.
923 %
924 % The format of the WriteJXLImage method is:
925 %
926 % MagickBooleanType WriteJXLImage(const ImageInfo *image_info,
927 % Image *image)
928 %
929 % A description of each parameter follows:
930 %
931 % o image_info: the image info.
932 %
933 % o image: The image.
934 %
935 */
936
937 static JxlEncoderStatus JXLWriteMetadata(const Image *image,
938 JxlEncoder *jxl_info, const StringInfo *icc_profile)
939 {
940 JxlColorEncoding
941 color_encoding;
942
943 JxlEncoderStatus
944 jxl_status;
945
946 if (icc_profile != (StringInfo *) NULL)
947 {
948 jxl_status=JxlEncoderSetICCProfile(jxl_info,(const uint8_t *)
949 GetStringInfoDatum(icc_profile),GetStringInfoLength(icc_profile));
950 return(jxl_status);
951 }
952 (void) memset(&color_encoding,0,sizeof(color_encoding));
953 color_encoding.color_space=JXL_COLOR_SPACE_RGB;
954 if (IsRGBColorspace(image->colorspace) == MagickFalse)
955 JxlColorEncodingSetToSRGB(&color_encoding,
956 IsGrayColorspace(image->colorspace) != MagickFalse);
957 else
958 JxlColorEncodingSetToLinearSRGB(&color_encoding,
959 IsGrayColorspace(image->colorspace) != MagickFalse);
960 jxl_status=JxlEncoderSetColorEncoding(jxl_info,&color_encoding);
961 return(jxl_status);
962 }
963
964 static inline float JXLGetDistance(float quality)
965 {
966 return quality >= 100.0f ? 0.0f
967 : quality >= 30
968 ? 0.1f + (100 - quality) * 0.09f
969 : 53.0f / 3000.0f * quality * quality - 23.0f / 20.0f * quality + 25.0f;
970 }
971
972 static inline MagickBooleanType JXLSameFrameType(const Image *image,
973 const Image *frame)
974 {
975 if (image->depth != frame->depth)
976 return(MagickFalse);
977 if (image->alpha_trait != frame->alpha_trait)
978 return(MagickFalse);
979 if (image->colorspace != frame->colorspace)
980 return(MagickFalse);
981 return(MagickTrue);
982 }
983
984 static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image,
985 ExceptionInfo *exception)
986 {
987 const char
988 *option;
989
990 const StringInfo
991 *icc_profile = (StringInfo *) NULL,
992 *exif_profile = (StringInfo *) NULL,
993 *xmp_profile = (StringInfo *) NULL;
994
995 JxlBasicInfo
996 basic_info;
997
998 JxlEncoder
999 *jxl_info;
1000
1001 JxlEncoderFrameSettings
1002 *frame_settings;
1003
1004 JxlEncoderStatus
1005 jxl_status;
1006
1007 JxlFrameHeader
1008 frame_header;
1009
1010 JxlMemoryManager
1011 memory_manager;
1012
1013 JxlPixelFormat
1014 pixel_format;
1015
1016 MagickBooleanType
1017 status;
1018
1019 MemoryInfo
1020 *pixel_info = (MemoryInfo *) NULL;
1021
1022 MemoryManagerInfo
1023 memory_manager_info;
1024
1025 size_t
1026 channels_size,
1027 sample_size;
1028
1029 unsigned char
1030 *pixels;
1031
1032 void
1033 *runner;
1034
1035 /*
1036 Open output image file.
1037 */
1038 assert(image_info != (const ImageInfo *) NULL);
1039 assert(image_info->signature == MagickCoreSignature);
1040 assert(image != (Image *) NULL);
1041 assert(image->signature == MagickCoreSignature);
1042 assert(exception != (ExceptionInfo *) NULL);
1043 assert(exception->signature == MagickCoreSignature);
1044 if (IsEventLogging() != MagickFalse)
1045 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1046 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1047 if (status == MagickFalse)
1048 return(status);
1049 if ((IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) &&
1050 (IsCMYKColorspace(image->colorspace) == MagickFalse))
1051 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1052 if ((image_info->adjoin != MagickFalse) &&
1053 (GetNextImageInList(image) != (Image *) NULL))
1054 {
1055 Image
1056 *frame;
1057
1058 MagickBooleanType
1059 has_alpha;
1060
1061 size_t
1062 depth;
1063
1064 depth=image->depth;
1065 has_alpha=MagickFalse;
1066 for (frame=image; frame != (Image *) NULL; frame=GetNextImageInList(frame))
1067 {
1068 if ((frame->alpha_trait & BlendPixelTrait) != 0)
1069 has_alpha=MagickTrue;
1070 if (frame->depth > depth)
1071 depth=frame->depth;
1072 }
1073 for (frame=image; frame != (Image *) NULL; frame=GetNextImageInList(frame))
1074 {
1075 frame->depth=depth;
1076 if (has_alpha != MagickFalse)
1077 {
1078 if ((frame->alpha_trait & BlendPixelTrait) == 0)
1079 (void) SetImageAlphaChannel(frame,OpaqueAlphaChannel,exception);
1080 }
1081 if (frame->colorspace != image->colorspace)
1082 (void) TransformImageColorspace(frame,image->colorspace,exception);
1083 }
1084 }
1085 /*
1086 Initialize JXL delegate library.
1087 */
1088 (void) memset(&basic_info,0,sizeof(basic_info));
1089 (void) memset(&frame_header,0,sizeof(frame_header));
1090 (void) memset(&pixel_format,0,sizeof(pixel_format));
1091 JXLSetMemoryManager(&memory_manager,&memory_manager_info,image,exception);
1092 jxl_info=JxlEncoderCreate(&memory_manager);
1093 if (jxl_info == (JxlEncoder *) NULL)
1094 ThrowWriterException(CoderError,"MemoryAllocationFailed");
1095 runner=JxlThreadParallelRunnerCreate(NULL,(size_t) GetMagickResourceLimit(
1096 ThreadResource));
1097 if (runner == (void *) NULL)
1098 {
1099 JxlEncoderDestroy(jxl_info);
1100 ThrowWriterException(CoderError,"MemoryAllocationFailed");
1101 }
1102 jxl_status=JxlEncoderSetParallelRunner(jxl_info,JxlThreadParallelRunner,
1103 runner);
1104 if (jxl_status != JXL_ENC_SUCCESS)
1105 {
1106 JxlThreadParallelRunnerDestroy(runner);
1107 JxlEncoderDestroy(jxl_info);
1108 return(MagickFalse);
1109 }
1110 JXLSetFormat(image,&pixel_format,exception);
1111 JxlEncoderInitBasicInfo(&basic_info);
1112 basic_info.xsize=(uint32_t) image->columns;
1113 basic_info.ysize=(uint32_t) image->rows;
1114 basic_info.bits_per_sample=8;
1115 if (pixel_format.data_type == JXL_TYPE_UINT16)
1116 basic_info.bits_per_sample=16;
1117 else
1118 if (pixel_format.data_type == JXL_TYPE_FLOAT)
1119 {
1120 basic_info.bits_per_sample=32;
1121 basic_info.exponent_bits_per_sample=8;
1122 }
1123 else
1124 if (pixel_format.data_type == JXL_TYPE_FLOAT16)
1125 {
1126 basic_info.bits_per_sample=16;
1127 basic_info.exponent_bits_per_sample=8;
1128 }
1129 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1130 basic_info.num_color_channels=1;
1131 if ((image->alpha_trait & BlendPixelTrait) != 0)
1132 {
1133 basic_info.alpha_bits=basic_info.bits_per_sample;
1134 basic_info.alpha_exponent_bits=basic_info.exponent_bits_per_sample;
1135 basic_info.num_extra_channels=1;
1136 }
1137 if (image_info->quality == 100)
1138 {
1139 basic_info.uses_original_profile=JXL_TRUE;
1140 icc_profile=GetImageProfile(image,"icc");
1141 }
1142 if ((image_info->adjoin != MagickFalse) &&
1143 (GetNextImageInList(image) != (Image *) NULL))
1144 {
1145 basic_info.have_animation=JXL_TRUE;
1146 basic_info.animation.num_loops=(uint32_t) image->iterations;
1147 basic_info.animation.tps_numerator=(uint32_t) image->ticks_per_second;
1148 basic_info.animation.tps_denominator=1;
1149 JxlEncoderInitFrameHeader(&frame_header);
1150 }
1151 basic_info.orientation=OrientationToJXLOrientation(image->orientation);
1152 jxl_status=JxlEncoderSetBasicInfo(jxl_info,&basic_info);
1153 if (jxl_status != JXL_ENC_SUCCESS)
1154 {
1155 JxlThreadParallelRunnerDestroy(runner);
1156 JxlEncoderDestroy(jxl_info);
1157 ThrowWriterException(CoderError,"UnableToWriteImageData");
1158 }
1159 frame_settings=JxlEncoderFrameSettingsCreate(jxl_info,
1160 (JxlEncoderFrameSettings *) NULL);
1161 if (frame_settings == (JxlEncoderFrameSettings *) NULL)
1162 {
1163 JxlThreadParallelRunnerDestroy(runner);
1164 JxlEncoderDestroy(jxl_info);
1165 ThrowWriterException(CoderError,"MemoryAllocationFailed");
1166 }
1167 if (image_info->quality == 100)
1168 {
1169 (void) JxlEncoderSetFrameDistance(frame_settings,0.f);
1170 (void) JxlEncoderSetFrameLossless(frame_settings,JXL_TRUE);
1171 }
1172 else if (image_info->quality != 0)
1173 (void) JxlEncoderSetFrameDistance(frame_settings,
1174 JXLGetDistance((float) image_info->quality));
1175 option=GetImageOption(image_info,"jxl:effort");
1176 if (option != (const char *) NULL)
1177 (void) JxlEncoderFrameSettingsSetOption(frame_settings,
1178 JXL_ENC_FRAME_SETTING_EFFORT,StringToInteger(option));
1179 option=GetImageOption(image_info,"jxl:decoding-speed");
1180 if (option != (const char *) NULL)
1181 (void) JxlEncoderFrameSettingsSetOption(frame_settings,
1182 JXL_ENC_FRAME_SETTING_DECODING_SPEED,StringToInteger(option));
1183 exif_profile=GetImageProfile(image,"exif");
1184 xmp_profile=GetImageProfile(image,"xmp");
1185 if ((exif_profile != (StringInfo *) NULL) ||
1186 (xmp_profile != (StringInfo *) NULL))
1187 {
1188 (void) JxlEncoderUseBoxes(jxl_info);
1189 if ((exif_profile != (StringInfo *) NULL) &&
1190 (GetStringInfoLength(exif_profile) > 6))
1191 {
1192 /*
1193 Add Exif profile. Assumes "Exif\0\0" JPEG APP1 prefix.
1194 */
1195 StringInfo
1196 *profile;
1197
1198 profile=BlobToStringInfo("\0\0\0\6",4);
1199 if (profile != (StringInfo *) NULL)
1200 {
1201 ConcatenateStringInfo(profile,exif_profile);
1202 (void) JxlEncoderAddBox(jxl_info,"Exif",
1203 GetStringInfoDatum(profile),GetStringInfoLength(profile),0);
1204 profile=DestroyStringInfo(profile);
1205 }
1206 }
1207 if (xmp_profile != (StringInfo *) NULL)
1208 {
1209 /*
1210 Add XMP profile.
1211 */
1212 (void) JxlEncoderAddBox(jxl_info,"xml ",GetStringInfoDatum(
1213 xmp_profile),GetStringInfoLength(xmp_profile),0);
1214 }
1215 (void) JxlEncoderCloseBoxes(jxl_info);
1216 }
1217 jxl_status=JXLWriteMetadata(image,jxl_info,icc_profile);
1218 if (jxl_status != JXL_ENC_SUCCESS)
1219 {
1220 JxlThreadParallelRunnerDestroy(runner);
1221 JxlEncoderDestroy(jxl_info);
1222 ThrowWriterException(CoderError,"UnableToWriteImageData");
1223 }
1224 /*
1225 Write image as a JXL stream.
1226 */
1227 sample_size=sizeof(char);
1228 if ((pixel_format.data_type == JXL_TYPE_FLOAT) ||
1229 (pixel_format.data_type == JXL_TYPE_FLOAT16))
1230 sample_size=sizeof(float);
1231 else
1232 if (pixel_format.data_type == JXL_TYPE_UINT16)
1233 sample_size=sizeof(short);
1234 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1235 channels_size=(((image->alpha_trait & BlendPixelTrait) != 0) ? 2U : 1U)*
1236 sample_size;
1237 else
1238 channels_size=(((image->alpha_trait & BlendPixelTrait) != 0) ? 4U : 3U)*
1239 sample_size;
1240 do
1241 {
1242 Image
1243 *next;
1244
1245 size_t
1246 bytes_per_row;
1247
1248 if (HeapOverflowSanityCheckGetSize(image->columns,channels_size,&bytes_per_row) != MagickFalse)
1249 {
1250 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
1251 "MemoryAllocationFailed","`%s'",image->filename);
1252 status=MagickFalse;
1253 break;
1254 }
1255 pixel_info=AcquireVirtualMemory(bytes_per_row,image->rows*sizeof(*pixels));
1256 if (pixel_info == (MemoryInfo *) NULL)
1257 {
1258 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
1259 "MemoryAllocationFailed","`%s'",image->filename);
1260 status=MagickFalse;
1261 break;
1262 }
1263
1264 if (basic_info.have_animation == JXL_TRUE)
1265 {
1266 JxlBlendInfo
1267 alpha_blend_info;
1268
1269 frame_header.duration=(uint32_t) image->delay;
1270 if ((image->previous == (Image *) NULL) ||
1271 (image->previous->dispose == BackgroundDispose) ||
1272 (image->previous->dispose == PreviousDispose))
1273 {
1274 frame_header.layer_info.blend_info.blendmode=JXL_BLEND_REPLACE;
1275 frame_header.layer_info.blend_info.source=0;
1276 }
1277 else
1278 {
1279 frame_header.layer_info.blend_info.blendmode=JXL_BLEND_BLEND;
1280 frame_header.layer_info.blend_info.source=1;
1281 }
1282 frame_header.layer_info.save_as_reference=1;
1283 if ((image->page.width != 0) && (image->page.height != 0))
1284 {
1285 frame_header.layer_info.have_crop=JXL_TRUE;
1286 frame_header.layer_info.crop_x0=(int32_t) image->page.x;
1287 frame_header.layer_info.crop_y0=(int32_t) image->page.y;
1288 frame_header.layer_info.xsize=(uint32_t) image->columns;
1289 frame_header.layer_info.ysize=(uint32_t) image->rows;
1290 }
1291 jxl_status=JxlEncoderSetFrameHeader(frame_settings,&frame_header);
1292 if (jxl_status != JXL_ENC_SUCCESS)
1293 break;
1294 if (basic_info.num_extra_channels > 0)
1295 {
1296 JxlEncoderInitBlendInfo(&alpha_blend_info);
1297 alpha_blend_info.blendmode=frame_header.layer_info.blend_info.blendmode;
1298 alpha_blend_info.source=frame_header.layer_info.blend_info.source;
1299 (void) JxlEncoderSetExtraChannelBlendInfo(frame_settings,0,&alpha_blend_info);
1300 }
1301 }
1302 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1303 if (IsGrayColorspace(image->colorspace) != MagickFalse)
1304 status=ExportImagePixels(image,0,0,image->columns,image->rows,
1305 ((image->alpha_trait & BlendPixelTrait) != 0) ? "IA" : "I",
1306 JXLDataTypeToStorageType(image,pixel_format.data_type,exception),
1307 pixels,exception);
1308 else
1309 status=ExportImagePixels(image,0,0,image->columns,image->rows,
1310 ((image->alpha_trait & BlendPixelTrait) != 0) ? "RGBA" : "RGB",
1311 JXLDataTypeToStorageType(image,pixel_format.data_type,exception),
1312 pixels,exception);
1313 if (status == MagickFalse)
1314 {
1315 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
1316 "MemoryAllocationFailed","`%s'",image->filename);
1317 status=MagickFalse;
1318 break;
1319 }
1320 if (jxl_status != JXL_ENC_SUCCESS)
1321 break;
1322 jxl_status=JxlEncoderAddImageFrame(frame_settings,&pixel_format,pixels,
1323 bytes_per_row*image->rows);
1324 if (jxl_status != JXL_ENC_SUCCESS)
1325 break;
1326 next=GetNextImageInList(image);
1327 if (next == (Image*) NULL)
1328 break;
1329 if (JXLSameFrameType(image,next) == MagickFalse)
1330 {
1331 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1332 "FramesNotSameDimensions","`%s'",image->filename);
1333 status=MagickFalse;
1334 break;
1335 }
1336 pixel_info=RelinquishVirtualMemory(pixel_info);
1337 image=SyncNextImageInList(image);
1338 } while (image_info->adjoin != MagickFalse);
1339 if (pixel_info != (MemoryInfo *) NULL)
1340 pixel_info=RelinquishVirtualMemory(pixel_info);
1341 if (jxl_status == JXL_ENC_SUCCESS)
1342 {
1343 unsigned char
1344 *output_buffer;
1345
1346 JxlEncoderCloseInput(jxl_info);
1347 output_buffer=(unsigned char *) AcquireQuantumMemory(
1348 MagickMaxBufferExtent,sizeof(*output_buffer));
1349 if (output_buffer == (unsigned char *) NULL)
1350 {
1351 JxlThreadParallelRunnerDestroy(runner);
1352 JxlEncoderDestroy(jxl_info);
1353 ThrowWriterException(CoderError,"MemoryAllocationFailed");
1354 }
1355 jxl_status=JXL_ENC_NEED_MORE_OUTPUT;
1356 while (jxl_status == JXL_ENC_NEED_MORE_OUTPUT)
1357 {
1358 size_t
1359 extent;
1360
1361 ssize_t
1362 count;
1363
1364 unsigned char
1365 *p;
1366
1367 /*
1368 Encode the pixel stream.
1369 */
1370 extent=MagickMaxBufferExtent;
1371 p=output_buffer;
1372 jxl_status=JxlEncoderProcessOutput(jxl_info,&p,&extent);
1373 count=WriteBlob(image,MagickMaxBufferExtent-extent,output_buffer);
1374 if (count != (ssize_t) (MagickMaxBufferExtent-extent))
1375 {
1376 jxl_status=JXL_ENC_ERROR;
1377 break;
1378 }
1379 }
1380 output_buffer=(unsigned char *) RelinquishMagickMemory(output_buffer);
1381 }
1382 JxlThreadParallelRunnerDestroy(runner);
1383 JxlEncoderDestroy(jxl_info);
1384 if (jxl_status != JXL_ENC_SUCCESS)
1385 ThrowWriterException(CoderError,"UnableToWriteImageData");
1386 if (CloseBlob(image) == MagickFalse)
1387 status=MagickFalse;
1388 return(status);
1389 }
1390 #endif