| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 88 |
|
| 89 |
static MagickBooleanType |
| 90 |
WriteJXLImage(const ImageInfo *,Image *,ExceptionInfo *); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 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 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 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 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
static inline float JXLGetDistance(float quality) |
| 938 |
{ |
| 939 |
#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0,9,0) |
| 940 |
return(JxlEncoderDistanceFromQuality(quality)); |
| 941 |
#else |
| 942 |
return(quality >= 100.0f ? 0.0f : quality >= 30.0 ? 0.1f+(100.0f-quality)* |
| 943 |
0.09f : 53.0f/3000.0f*quality*quality-23.0f/20.0f*quality+25.0f); |
| 944 |
#endif |
| 945 |
} |
| 946 |
|
| 947 |
static inline MagickBooleanType JXLParseOption(const char *option, |
| 948 |
const double minimum,const double maximum,double *value) |
| 949 |
{ |
| 950 |
char |
| 951 |
*sentinel; |
| 952 |
|
| 953 |
double |
| 954 |
number; |
| 955 |
|
| 956 |
number=StringToDouble(option,&sentinel); |
| 957 |
if ((sentinel == option) || (number < minimum) || (number > maximum)) |
| 958 |
return(MagickFalse); |
| 959 |
*value=number; |
| 960 |
return(MagickTrue); |
| 961 |
} |
| 962 |
|
| 963 |
static inline MagickBooleanType JXLSameFrameType(const Image *image, |
| 964 |
const Image *frame) |
| 965 |
{ |
| 966 |
if (image->depth != frame->depth) |
| 967 |
return(MagickFalse); |
| 968 |
if (image->alpha_trait != frame->alpha_trait) |
| 969 |
return(MagickFalse); |
| 970 |
if (image->colorspace != frame->colorspace) |
| 971 |
return(MagickFalse); |
| 972 |
return(MagickTrue); |
| 973 |
} |
| 974 |
|
| 975 |
static JxlEncoderStatus JXLWriteMetadata(const Image *image, |
| 976 |
JxlEncoder *jxl_info, const StringInfo *icc_profile) |
| 977 |
{ |
| 978 |
JxlColorEncoding |
| 979 |
color_encoding; |
| 980 |
|
| 981 |
JxlEncoderStatus |
| 982 |
jxl_status; |
| 983 |
|
| 984 |
if (icc_profile != (StringInfo *) NULL) |
| 985 |
{ |
| 986 |
jxl_status=JxlEncoderSetICCProfile(jxl_info,(const uint8_t *) |
| 987 |
GetStringInfoDatum(icc_profile),GetStringInfoLength(icc_profile)); |
| 988 |
return(jxl_status); |
| 989 |
} |
| 990 |
(void) memset(&color_encoding,0,sizeof(color_encoding)); |
| 991 |
color_encoding.color_space=JXL_COLOR_SPACE_RGB; |
| 992 |
if (IsRGBColorspace(image->colorspace) == MagickFalse) |
| 993 |
JxlColorEncodingSetToSRGB(&color_encoding, |
| 994 |
IsGrayColorspace(image->colorspace) != MagickFalse); |
| 995 |
else |
| 996 |
JxlColorEncodingSetToLinearSRGB(&color_encoding, |
| 997 |
IsGrayColorspace(image->colorspace) != MagickFalse); |
| 998 |
jxl_status=JxlEncoderSetColorEncoding(jxl_info,&color_encoding); |
| 999 |
return(jxl_status); |
| 1000 |
} |
| 1001 |
|
| 1002 |
static MagickBooleanType WriteJXLImage(const ImageInfo *image_info,Image *image, |
| 1003 |
ExceptionInfo *exception) |
| 1004 |
{ |
| 1005 |
const char |
| 1006 |
*option; |
| 1007 |
|
| 1008 |
const StringInfo |
| 1009 |
*icc_profile = (StringInfo *) NULL, |
| 1010 |
*exif_profile = (StringInfo *) NULL, |
| 1011 |
*xmp_profile = (StringInfo *) NULL; |
| 1012 |
|
| 1013 |
double |
| 1014 |
distance = -1.0; |
| 1015 |
|
| 1016 |
JxlBasicInfo |
| 1017 |
basic_info; |
| 1018 |
|
| 1019 |
JxlEncoder |
| 1020 |
*jxl_info; |
| 1021 |
|
| 1022 |
JxlEncoderFrameSettings |
| 1023 |
*frame_settings; |
| 1024 |
|
| 1025 |
JxlEncoderStatus |
| 1026 |
jxl_status; |
| 1027 |
|
| 1028 |
JxlFrameHeader |
| 1029 |
frame_header; |
| 1030 |
|
| 1031 |
JxlMemoryManager |
| 1032 |
memory_manager; |
| 1033 |
|
| 1034 |
JxlPixelFormat |
| 1035 |
pixel_format; |
| 1036 |
|
| 1037 |
MagickBooleanType |
| 1038 |
status; |
| 1039 |
|
| 1040 |
MemoryInfo |
| 1041 |
*pixel_info = (MemoryInfo *) NULL; |
| 1042 |
|
| 1043 |
MemoryManagerInfo |
| 1044 |
memory_manager_info; |
| 1045 |
|
| 1046 |
size_t |
| 1047 |
channels_size, |
| 1048 |
sample_size; |
| 1049 |
|
| 1050 |
unsigned char |
| 1051 |
*pixels; |
| 1052 |
|
| 1053 |
void |
| 1054 |
*runner; |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
assert(image_info != (const ImageInfo *) NULL); |
| 1060 |
assert(image_info->signature == MagickCoreSignature); |
| 1061 |
assert(image != (Image *) NULL); |
| 1062 |
assert(image->signature == MagickCoreSignature); |
| 1063 |
assert(exception != (ExceptionInfo *) NULL); |
| 1064 |
assert(exception->signature == MagickCoreSignature); |
| 1065 |
if (IsEventLogging() != MagickFalse) |
| 1066 |
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1067 |
status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| 1068 |
if (status == MagickFalse) |
| 1069 |
return(status); |
| 1070 |
if ((IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) && |
| 1071 |
(IsCMYKColorspace(image->colorspace) == MagickFalse)) |
| 1072 |
(void) TransformImageColorspace(image,sRGBColorspace,exception); |
| 1073 |
if ((image_info->adjoin != MagickFalse) && |
| 1074 |
(GetNextImageInList(image) != (Image *) NULL)) |
| 1075 |
{ |
| 1076 |
Image |
| 1077 |
*frame; |
| 1078 |
|
| 1079 |
MagickBooleanType |
| 1080 |
has_alpha; |
| 1081 |
|
| 1082 |
size_t |
| 1083 |
depth; |
| 1084 |
|
| 1085 |
depth=image->depth; |
| 1086 |
has_alpha=MagickFalse; |
| 1087 |
for (frame=image; frame != (Image *) NULL; frame=GetNextImageInList(frame)) |
| 1088 |
{ |
| 1089 |
if ((frame->alpha_trait & BlendPixelTrait) != 0) |
| 1090 |
has_alpha=MagickTrue; |
| 1091 |
if (frame->depth > depth) |
| 1092 |
depth=frame->depth; |
| 1093 |
} |
| 1094 |
for (frame=image; frame != (Image *) NULL; frame=GetNextImageInList(frame)) |
| 1095 |
{ |
| 1096 |
frame->depth=depth; |
| 1097 |
if (has_alpha != MagickFalse) |
| 1098 |
{ |
| 1099 |
if ((frame->alpha_trait & BlendPixelTrait) == 0) |
| 1100 |
(void) SetImageAlphaChannel(frame,OpaqueAlphaChannel,exception); |
| 1101 |
} |
| 1102 |
if (frame->colorspace != image->colorspace) |
| 1103 |
(void) TransformImageColorspace(frame,image->colorspace,exception); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
(void) memset(&basic_info,0,sizeof(basic_info)); |
| 1110 |
(void) memset(&frame_header,0,sizeof(frame_header)); |
| 1111 |
(void) memset(&pixel_format,0,sizeof(pixel_format)); |
| 1112 |
JXLSetMemoryManager(&memory_manager,&memory_manager_info,image,exception); |
| 1113 |
jxl_info=JxlEncoderCreate(&memory_manager); |
| 1114 |
if (jxl_info == (JxlEncoder *) NULL) |
| 1115 |
ThrowWriterException(CoderError,"MemoryAllocationFailed"); |
| 1116 |
runner=JxlThreadParallelRunnerCreate(NULL,(size_t) GetMagickResourceLimit( |
| 1117 |
ThreadResource)); |
| 1118 |
if (runner == (void *) NULL) |
| 1119 |
{ |
| 1120 |
JxlEncoderDestroy(jxl_info); |
| 1121 |
ThrowWriterException(CoderError,"MemoryAllocationFailed"); |
| 1122 |
} |
| 1123 |
jxl_status=JxlEncoderSetParallelRunner(jxl_info,JxlThreadParallelRunner, |
| 1124 |
runner); |
| 1125 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1126 |
{ |
| 1127 |
JxlThreadParallelRunnerDestroy(runner); |
| 1128 |
JxlEncoderDestroy(jxl_info); |
| 1129 |
return(MagickFalse); |
| 1130 |
} |
| 1131 |
JXLSetFormat(image,&pixel_format,exception); |
| 1132 |
option=GetImageOption(image_info,"jxl:distance"); |
| 1133 |
if (option != (const char *) NULL) |
| 1134 |
{ |
| 1135 |
if (JXLParseOption(option,0.0,25.0,&distance) == MagickFalse) |
| 1136 |
(void) ThrowMagickException(exception,GetMagickModule(),OptionWarning, |
| 1137 |
"InvalidSetting","`%s'",option); |
| 1138 |
} |
| 1139 |
else |
| 1140 |
{ |
| 1141 |
option=GetImageOption(image_info,"jxl:quality"); |
| 1142 |
if (option != (const char *) NULL) |
| 1143 |
{ |
| 1144 |
double |
| 1145 |
quality; |
| 1146 |
|
| 1147 |
if (JXLParseOption(option,0.0,100.0,&quality) == MagickFalse) |
| 1148 |
(void) ThrowMagickException(exception,GetMagickModule(), |
| 1149 |
OptionWarning,"InvalidSetting","`%s'",option); |
| 1150 |
else |
| 1151 |
distance=(double) JXLGetDistance((float) quality); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
if (distance < 0.0) |
| 1155 |
{ |
| 1156 |
if (image_info->quality == 100) |
| 1157 |
distance=0.0; |
| 1158 |
else |
| 1159 |
if (image_info->quality != 0) |
| 1160 |
distance=(double) JXLGetDistance((float) image_info->quality); |
| 1161 |
} |
| 1162 |
JxlEncoderInitBasicInfo(&basic_info); |
| 1163 |
basic_info.xsize=(uint32_t) image->columns; |
| 1164 |
basic_info.ysize=(uint32_t) image->rows; |
| 1165 |
basic_info.bits_per_sample=8; |
| 1166 |
if (pixel_format.data_type == JXL_TYPE_UINT16) |
| 1167 |
basic_info.bits_per_sample=16; |
| 1168 |
else |
| 1169 |
if (pixel_format.data_type == JXL_TYPE_FLOAT) |
| 1170 |
{ |
| 1171 |
basic_info.bits_per_sample=32; |
| 1172 |
basic_info.exponent_bits_per_sample=8; |
| 1173 |
} |
| 1174 |
else |
| 1175 |
if (pixel_format.data_type == JXL_TYPE_FLOAT16) |
| 1176 |
{ |
| 1177 |
basic_info.bits_per_sample=16; |
| 1178 |
basic_info.exponent_bits_per_sample=8; |
| 1179 |
} |
| 1180 |
if (IsGrayColorspace(image->colorspace) != MagickFalse) |
| 1181 |
basic_info.num_color_channels=1; |
| 1182 |
if ((image->alpha_trait & BlendPixelTrait) != 0) |
| 1183 |
{ |
| 1184 |
basic_info.alpha_bits=basic_info.bits_per_sample; |
| 1185 |
basic_info.alpha_exponent_bits=basic_info.exponent_bits_per_sample; |
| 1186 |
basic_info.num_extra_channels=1; |
| 1187 |
} |
| 1188 |
if (distance == 0.0) |
| 1189 |
{ |
| 1190 |
basic_info.uses_original_profile=JXL_TRUE; |
| 1191 |
icc_profile=GetImageProfile(image,"icc"); |
| 1192 |
} |
| 1193 |
if ((image_info->adjoin != MagickFalse) && |
| 1194 |
(GetNextImageInList(image) != (Image *) NULL)) |
| 1195 |
{ |
| 1196 |
basic_info.have_animation=JXL_TRUE; |
| 1197 |
basic_info.animation.num_loops=(uint32_t) image->iterations; |
| 1198 |
basic_info.animation.tps_numerator=(uint32_t) image->ticks_per_second; |
| 1199 |
basic_info.animation.tps_denominator=1; |
| 1200 |
JxlEncoderInitFrameHeader(&frame_header); |
| 1201 |
} |
| 1202 |
basic_info.orientation=OrientationToJXLOrientation(image->orientation); |
| 1203 |
jxl_status=JxlEncoderSetBasicInfo(jxl_info,&basic_info); |
| 1204 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1205 |
{ |
| 1206 |
JxlThreadParallelRunnerDestroy(runner); |
| 1207 |
JxlEncoderDestroy(jxl_info); |
| 1208 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 1209 |
} |
| 1210 |
frame_settings=JxlEncoderFrameSettingsCreate(jxl_info, |
| 1211 |
(JxlEncoderFrameSettings *) NULL); |
| 1212 |
if (frame_settings == (JxlEncoderFrameSettings *) NULL) |
| 1213 |
{ |
| 1214 |
JxlThreadParallelRunnerDestroy(runner); |
| 1215 |
JxlEncoderDestroy(jxl_info); |
| 1216 |
ThrowWriterException(CoderError,"MemoryAllocationFailed"); |
| 1217 |
} |
| 1218 |
if (distance == 0.0) |
| 1219 |
{ |
| 1220 |
(void) JxlEncoderSetFrameDistance(frame_settings,0.0f); |
| 1221 |
(void) JxlEncoderSetFrameLossless(frame_settings,JXL_TRUE); |
| 1222 |
} |
| 1223 |
else |
| 1224 |
if (distance > 0.0) |
| 1225 |
(void) JxlEncoderSetFrameDistance(frame_settings, |
| 1226 |
JXLGetDistance((float) distance)); |
| 1227 |
option=GetImageOption(image_info,"jxl:effort"); |
| 1228 |
if (option != (const char *) NULL) |
| 1229 |
(void) JxlEncoderFrameSettingsSetOption(frame_settings, |
| 1230 |
JXL_ENC_FRAME_SETTING_EFFORT,StringToInteger(option)); |
| 1231 |
option=GetImageOption(image_info,"jxl:decoding-speed"); |
| 1232 |
if (option != (const char *) NULL) |
| 1233 |
(void) JxlEncoderFrameSettingsSetOption(frame_settings, |
| 1234 |
JXL_ENC_FRAME_SETTING_DECODING_SPEED,StringToInteger(option)); |
| 1235 |
exif_profile=GetImageProfile(image,"exif"); |
| 1236 |
xmp_profile=GetImageProfile(image,"xmp"); |
| 1237 |
if ((exif_profile != (StringInfo *) NULL) || |
| 1238 |
(xmp_profile != (StringInfo *) NULL)) |
| 1239 |
{ |
| 1240 |
(void) JxlEncoderUseBoxes(jxl_info); |
| 1241 |
if ((exif_profile != (StringInfo *) NULL) && |
| 1242 |
(GetStringInfoLength(exif_profile) > 6)) |
| 1243 |
{ |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
StringInfo |
| 1248 |
*profile; |
| 1249 |
|
| 1250 |
profile=BlobToStringInfo("\0\0\0\6",4); |
| 1251 |
if (profile != (StringInfo *) NULL) |
| 1252 |
{ |
| 1253 |
ConcatenateStringInfo(profile,exif_profile); |
| 1254 |
(void) JxlEncoderAddBox(jxl_info,"Exif", |
| 1255 |
GetStringInfoDatum(profile),GetStringInfoLength(profile),0); |
| 1256 |
profile=DestroyStringInfo(profile); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
if (xmp_profile != (StringInfo *) NULL) |
| 1260 |
{ |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
(void) JxlEncoderAddBox(jxl_info,"xml ",GetStringInfoDatum( |
| 1265 |
xmp_profile),GetStringInfoLength(xmp_profile),0); |
| 1266 |
} |
| 1267 |
(void) JxlEncoderCloseBoxes(jxl_info); |
| 1268 |
} |
| 1269 |
jxl_status=JXLWriteMetadata(image,jxl_info,icc_profile); |
| 1270 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1271 |
{ |
| 1272 |
JxlThreadParallelRunnerDestroy(runner); |
| 1273 |
JxlEncoderDestroy(jxl_info); |
| 1274 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
sample_size=sizeof(char); |
| 1280 |
if ((pixel_format.data_type == JXL_TYPE_FLOAT) || |
| 1281 |
(pixel_format.data_type == JXL_TYPE_FLOAT16)) |
| 1282 |
sample_size=sizeof(float); |
| 1283 |
else |
| 1284 |
if (pixel_format.data_type == JXL_TYPE_UINT16) |
| 1285 |
sample_size=sizeof(short); |
| 1286 |
if (IsGrayColorspace(image->colorspace) != MagickFalse) |
| 1287 |
channels_size=(((image->alpha_trait & BlendPixelTrait) != 0) ? 2U : 1U)* |
| 1288 |
sample_size; |
| 1289 |
else |
| 1290 |
channels_size=(((image->alpha_trait & BlendPixelTrait) != 0) ? 4U : 3U)* |
| 1291 |
sample_size; |
| 1292 |
do |
| 1293 |
{ |
| 1294 |
Image |
| 1295 |
*next; |
| 1296 |
|
| 1297 |
size_t |
| 1298 |
bytes_per_row; |
| 1299 |
|
| 1300 |
if (HeapOverflowSanityCheckGetSize(image->columns,channels_size,&bytes_per_row) != MagickFalse) |
| 1301 |
{ |
| 1302 |
(void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 1303 |
"MemoryAllocationFailed","`%s'",image->filename); |
| 1304 |
status=MagickFalse; |
| 1305 |
break; |
| 1306 |
} |
| 1307 |
pixel_info=AcquireVirtualMemory(bytes_per_row,image->rows*sizeof(*pixels)); |
| 1308 |
if (pixel_info == (MemoryInfo *) NULL) |
| 1309 |
{ |
| 1310 |
(void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 1311 |
"MemoryAllocationFailed","`%s'",image->filename); |
| 1312 |
status=MagickFalse; |
| 1313 |
break; |
| 1314 |
} |
| 1315 |
|
| 1316 |
if (basic_info.have_animation == JXL_TRUE) |
| 1317 |
{ |
| 1318 |
JxlBlendInfo |
| 1319 |
alpha_blend_info; |
| 1320 |
|
| 1321 |
frame_header.duration=(uint32_t) image->delay; |
| 1322 |
if ((image->previous == (Image *) NULL) || |
| 1323 |
(image->previous->dispose == BackgroundDispose) || |
| 1324 |
(image->previous->dispose == PreviousDispose)) |
| 1325 |
{ |
| 1326 |
frame_header.layer_info.blend_info.blendmode=JXL_BLEND_REPLACE; |
| 1327 |
frame_header.layer_info.blend_info.source=0; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
frame_header.layer_info.blend_info.blendmode=JXL_BLEND_BLEND; |
| 1332 |
frame_header.layer_info.blend_info.source=1; |
| 1333 |
} |
| 1334 |
frame_header.layer_info.save_as_reference=1; |
| 1335 |
if ((image->page.width != 0) && (image->page.height != 0)) |
| 1336 |
{ |
| 1337 |
frame_header.layer_info.have_crop=JXL_TRUE; |
| 1338 |
frame_header.layer_info.crop_x0=(int32_t) image->page.x; |
| 1339 |
frame_header.layer_info.crop_y0=(int32_t) image->page.y; |
| 1340 |
frame_header.layer_info.xsize=(uint32_t) image->columns; |
| 1341 |
frame_header.layer_info.ysize=(uint32_t) image->rows; |
| 1342 |
} |
| 1343 |
jxl_status=JxlEncoderSetFrameHeader(frame_settings,&frame_header); |
| 1344 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1345 |
break; |
| 1346 |
if (basic_info.num_extra_channels > 0) |
| 1347 |
{ |
| 1348 |
JxlEncoderInitBlendInfo(&alpha_blend_info); |
| 1349 |
alpha_blend_info.blendmode=frame_header.layer_info.blend_info.blendmode; |
| 1350 |
alpha_blend_info.source=frame_header.layer_info.blend_info.source; |
| 1351 |
(void) JxlEncoderSetExtraChannelBlendInfo(frame_settings,0,&alpha_blend_info); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
| 1355 |
if (IsGrayColorspace(image->colorspace) != MagickFalse) |
| 1356 |
status=ExportImagePixels(image,0,0,image->columns,image->rows, |
| 1357 |
((image->alpha_trait & BlendPixelTrait) != 0) ? "IA" : "I", |
| 1358 |
JXLDataTypeToStorageType(image,pixel_format.data_type,exception), |
| 1359 |
pixels,exception); |
| 1360 |
else |
| 1361 |
status=ExportImagePixels(image,0,0,image->columns,image->rows, |
| 1362 |
((image->alpha_trait & BlendPixelTrait) != 0) ? "RGBA" : "RGB", |
| 1363 |
JXLDataTypeToStorageType(image,pixel_format.data_type,exception), |
| 1364 |
pixels,exception); |
| 1365 |
if (status == MagickFalse) |
| 1366 |
{ |
| 1367 |
(void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 1368 |
"MemoryAllocationFailed","`%s'",image->filename); |
| 1369 |
status=MagickFalse; |
| 1370 |
break; |
| 1371 |
} |
| 1372 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1373 |
break; |
| 1374 |
jxl_status=JxlEncoderAddImageFrame(frame_settings,&pixel_format,pixels, |
| 1375 |
bytes_per_row*image->rows); |
| 1376 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1377 |
break; |
| 1378 |
next=GetNextImageInList(image); |
| 1379 |
if (next == (Image*) NULL) |
| 1380 |
break; |
| 1381 |
if (JXLSameFrameType(image,next) == MagickFalse) |
| 1382 |
{ |
| 1383 |
(void) ThrowMagickException(exception,GetMagickModule(),ImageError, |
| 1384 |
"FramesNotSameDimensions","`%s'",image->filename); |
| 1385 |
status=MagickFalse; |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
pixel_info=RelinquishVirtualMemory(pixel_info); |
| 1389 |
image=SyncNextImageInList(image); |
| 1390 |
} while (image_info->adjoin != MagickFalse); |
| 1391 |
if (pixel_info != (MemoryInfo *) NULL) |
| 1392 |
pixel_info=RelinquishVirtualMemory(pixel_info); |
| 1393 |
if (jxl_status == JXL_ENC_SUCCESS) |
| 1394 |
{ |
| 1395 |
unsigned char |
| 1396 |
*output_buffer; |
| 1397 |
|
| 1398 |
JxlEncoderCloseInput(jxl_info); |
| 1399 |
output_buffer=(unsigned char *) AcquireQuantumMemory( |
| 1400 |
MagickMaxBufferExtent,sizeof(*output_buffer)); |
| 1401 |
if (output_buffer == (unsigned char *) NULL) |
| 1402 |
{ |
| 1403 |
JxlThreadParallelRunnerDestroy(runner); |
| 1404 |
JxlEncoderDestroy(jxl_info); |
| 1405 |
ThrowWriterException(CoderError,"MemoryAllocationFailed"); |
| 1406 |
} |
| 1407 |
jxl_status=JXL_ENC_NEED_MORE_OUTPUT; |
| 1408 |
while (jxl_status == JXL_ENC_NEED_MORE_OUTPUT) |
| 1409 |
{ |
| 1410 |
size_t |
| 1411 |
extent; |
| 1412 |
|
| 1413 |
ssize_t |
| 1414 |
count; |
| 1415 |
|
| 1416 |
unsigned char |
| 1417 |
*p; |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
extent=MagickMaxBufferExtent; |
| 1423 |
p=output_buffer; |
| 1424 |
jxl_status=JxlEncoderProcessOutput(jxl_info,&p,&extent); |
| 1425 |
count=WriteBlob(image,MagickMaxBufferExtent-extent,output_buffer); |
| 1426 |
if (count != (ssize_t) (MagickMaxBufferExtent-extent)) |
| 1427 |
{ |
| 1428 |
jxl_status=JXL_ENC_ERROR; |
| 1429 |
break; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
output_buffer=(unsigned char *) RelinquishMagickMemory(output_buffer); |
| 1433 |
} |
| 1434 |
JxlThreadParallelRunnerDestroy(runner); |
| 1435 |
JxlEncoderDestroy(jxl_info); |
| 1436 |
if (jxl_status != JXL_ENC_SUCCESS) |
| 1437 |
ThrowWriterException(CoderError,"UnableToWriteImageData"); |
| 1438 |
if (CloseBlob(image) == MagickFalse) |
| 1439 |
status=MagickFalse; |
| 1440 |
return(status); |
| 1441 |
} |
| 1442 |
#endif |