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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 118 119 120 121 122 123 124 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 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 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 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 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
|
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% EEEEE X X RRRR %
% E X X R R %
% EEE X RRRR %
% E X X R R %
% EEEEE X X R R %
% %
% %
% Read/Write High Dynamic-Range (HDR) Image File Format %
% %
% Software Design %
% Cristy %
% April 2007 %
% %
% %
% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% https://imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "MagickCore/studio.h"
#include "MagickCore/blob.h"
#include "MagickCore/blob-private.h"
#include "MagickCore/cache.h"
#include "MagickCore/exception.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/image.h"
#include "MagickCore/image-private.h"
#include "MagickCore/list.h"
#include "MagickCore/magick.h"
#include "MagickCore/memory_.h"
#include "MagickCore/option.h"
#include "MagickCore/pixel-accessor.h"
#include "MagickCore/property.h"
#include "MagickCore/quantum-private.h"
#include "MagickCore/static.h"
#include "MagickCore/string_.h"
#include "MagickCore/string-private.h"
#include "MagickCore/module.h"
#include "MagickCore/resource_.h"
#include "MagickCore/utility.h"
#if defined(MAGICKCORE_OPENEXR_DELEGATE)
#include <OpenEXRConfig.h>
#include <ImfCRgbaFile.h>
/*
Version 3.1.0 was released on 2021-07-21
We can remove backwards compatibility in a couple years.
*/
#define OPENEXR_VERSION_HACK \
(((OPENEXR_VERSION_MAJOR) << 24) | \
((OPENEXR_VERSION_MINOR) << 16) | \
((OPENEXR_VERSION_PATCH) << 8))
#if (OPENEXR_VERSION_HACK) >= 0x3010000
#define USE_OPENEXR_CORE
#include <openexr.h>
#endif
/*
Typedef declarations.
*/
#if defined(USE_OPENEXR_CORE)
typedef struct _EXRUserData
{
ExceptionInfo
*exception;
} EXRUserData;
#else
typedef struct _EXRWindowInfo
{
int
max_x,
max_y,
min_x,
min_y;
} EXRWindowInfo;
#endif
/*
Forward declarations.
*/
static MagickBooleanType
WriteEXRImage(const ImageInfo *,Image *,ExceptionInfo *);
#endif
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% I s E X R %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% IsEXR() returns MagickTrue if the image format type, identified by the
% magick string, is EXR.
%
% The format of the IsEXR method is:
%
% MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
%
% A description of each parameter follows:
%
% o magick: compare image format pattern against these bytes.
%
% o length: Specifies the length of the magick string.
%
*/
static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length)
{
if (length < 4)
return(MagickFalse);
if (memcmp(magick,"\166\057\061\001",4) == 0)
return(MagickTrue);
return(MagickFalse);
}
#if defined(MAGICKCORE_OPENEXR_DELEGATE)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e a d E X R I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ReadEXRImage reads an image in the high dynamic-range (HDR) file format
% developed by Industrial Light & Magic. It allocates the memory necessary
% for the new Image structure and returns a pointer to the new image.
%
% The format of the ReadEXRImage method is:
%
% Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image_info: the image info.
%
% o exception: return any errors or warnings in this structure.
%
*/
#if defined(USE_OPENEXR_CORE)
static void EXRError(exr_const_context_t ctxt,exr_result_t magick_unused(code),
const char* msg)
{
EXRUserData
*user_data;
magick_unreferenced(code);
exr_get_user_data(ctxt,(void **) &user_data);
(void) ThrowMagickException(user_data->exception,GetMagickModule(),
CoderError,"CorruptImage","`%s'",msg);
}
static MagickBooleanType InitializeEXRChannels(Image *image,exr_context_t ctxt,
exr_decode_pipeline_t decoder,size_t pixel_count,size_t columns,
PixelChannel *pixel_channels,size_t *pixel_size,uint8_t **data,
ExceptionInfo *exception)
{
char
channel_name[MagickPathExtent];
const char
*prefix;
exr_coding_channel_info_t
*channel;
int16_t
c;
MagickBooleanType
status;
size_t
prefix_length,
number_meta_channels;
uint8_t
*p;
*data=(uint8_t *) NULL;
if (decoder.channel_count >= (MaxPixelChannels-MetaPixelChannels))
ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
image->filename);
channel=decoder.channels;
prefix_length=0;
prefix=strrchr(decoder.channels[0].channel_name,'.');
if (prefix != (const char*) NULL)
{
/*
* When all channel names have the same prefix, we will skip that
* part when determining the channel type.
*/
prefix_length=1+(size_t)(prefix-decoder.channels[0].channel_name);
if (prefix_length < MagickPathExtent)
{
(void) CopyMagickString(channel_name,decoder.channels[0].channel_name
,prefix_length+1);
channel=decoder.channels;
for (c = 0; c < decoder.channel_count; ++c)
{
if (strncmp(channel->channel_name,channel_name,
prefix_length) != 0)
{
prefix_length=0;
break;
}
channel++;
}
}
}
channel=decoder.channels;
*pixel_size=0;
number_meta_channels=0;
status=MagickTrue;
for (c = 0; c < decoder.channel_count; ++c)
{
if ((channel->data_type != EXR_PIXEL_HALF) &&
(channel->data_type != EXR_PIXEL_FLOAT))
{
status=MagickFalse;
(void) ThrowMagickException(exception,GetMagickModule(),
CorruptImageError,"Unsupported channel data type","`%d'",
(int) channel->data_type );
break;
}
*pixel_size+=(size_t) channel->bytes_per_element;
if (LocaleNCompare(channel->channel_name+prefix_length,"R",1) == 0)
pixel_channels[c]=RedPixelChannel;
else if (LocaleNCompare(channel->channel_name+prefix_length,"G",1) == 0)
pixel_channels[c]=GreenPixelChannel;
else if (LocaleNCompare(channel->channel_name+prefix_length,"B",1) == 0)
pixel_channels[c]=BluePixelChannel;
else if (LocaleNCompare(channel->channel_name+prefix_length,"A",1) == 0)
{
pixel_channels[c]=AlphaPixelChannel;
image->alpha_trait=BlendPixelTrait;
}
else if (LocaleNCompare(channel->channel_name+prefix_length,"Y",1) == 0)
pixel_channels[c]=IndexPixelChannel; /* Gray channel */
else
{
pixel_channels[c]=(PixelChannel) (MetaPixelChannels+
number_meta_channels);
FormatLocaleString(channel_name,MagickPathExtent,
"exr:meta-channel.%d",(int) number_meta_channels);
number_meta_channels++;
SetImageProperty(image,channel_name,channel->channel_name,
exception);
}
channel++;
}
if ((status != MagickFalse) && (number_meta_channels > 0))
status=SetPixelMetaChannels(image,number_meta_channels,exception);
if (status == MagickFalse)
return(status);
if ((decoder.channel_count == 1) && (pixel_channels[0] == IndexPixelChannel))
image->colorspace=GRAYColorspace;
*data=(uint8_t *) AcquireQuantumMemory(*pixel_size,pixel_count);
if (*data == (uint8_t*)NULL)
{
exr_decoding_destroy(ctxt, &decoder);
ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
image->filename);
}
memset(*data,0,*pixel_size*pixel_count);
p=*data;
channel=decoder.channels;
for (c = 0; c < decoder.channel_count; ++c)
{
channel->decode_to_ptr=p;
channel->user_pixel_stride=(int32_t) *pixel_size;
channel->user_line_stride=(int32_t) (columns*(*pixel_size));
p+=(ptrdiff_t) channel->bytes_per_element;
channel++;
}
return(MagickTrue);
}
static inline MagickBooleanType ReadEXRPixels(Image *image,
exr_decode_pipeline_t decoder,PixelChannel *pixel_channels,uint8_t *p,
Quantum* q,size_t pixel_count,size_t columns,size_t stride,
ExceptionInfo *exception)
{
while (pixel_count > 0)
{
exr_coding_channel_info_t
*channel;
int16_t
c;
channel=decoder.channels;
for (c = 0; c < decoder.channel_count; ++c)
{
float
value;
PixelChannel
pixel_channel;
if (channel->data_type == EXR_PIXEL_HALF)
value=HalfToSinglePrecision(*(unsigned short *) p);
else
value=*(float *) p;
pixel_channel=pixel_channels[c];
if (pixel_channel == IndexPixelChannel)
SetPixelGray(image,ClampToQuantum((double) QuantumRange*value),q);
else
SetPixelChannel(image,pixel_channel,ClampToQuantum(
(double) QuantumRange*value),q);
p+=(ptrdiff_t) channel->bytes_per_element;
channel++;
}
q+=(ptrdiff_t) GetPixelChannels(image);
pixel_count--;
if ((stride != 0) && (pixel_count % columns == 0))
p+=(ptrdiff_t) stride;
}
return(SyncAuthenticPixels(image, exception));
}
static MagickBooleanType ReadEXRScanlineImage(exr_context_t ctxt,int part_index,
exr_attr_box2i_t data_window,Image *image,ExceptionInfo* exception)
{
exr_chunk_info_t
cinfo;
exr_decode_pipeline_t
decoder = EXR_DECODE_PIPELINE_INITIALIZER;
exr_result_t
result;
int32_t
scans_per_chunk;
MagickBooleanType
status;
PixelChannel
pixel_channels[MaxPixelChannels] = { UndefinedPixelChannel };
size_t
pixel_size,
pixel_count;
ssize_t
y;
uint8_t
*data;
result=exr_get_scanlines_per_chunk(ctxt,part_index,&scans_per_chunk);
if (result == EXR_ERR_SUCCESS)
result=exr_read_scanline_chunk_info(ctxt,part_index,data_window.min.y,
&cinfo);
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_initialize(ctxt,part_index,&cinfo,&decoder);
if (result != EXR_ERR_SUCCESS)
ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
pixel_count=(size_t) scans_per_chunk*image->columns;
status=InitializeEXRChannels(image,ctxt,decoder,pixel_count,image->columns,
pixel_channels,&pixel_size,&data,exception);
if (status == MagickFalse)
{
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
return(status);
}
result=exr_decoding_choose_default_routines(ctxt,part_index,&decoder);
if (result != EXR_ERR_SUCCESS)
{
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
}
for (y=0; y < (ssize_t) image->rows; y+= (ssize_t) scans_per_chunk)
{
Quantum
*q;
size_t
scans_count_to_read,
pixel_count_to_read;
if (y != 0)
{
int
yy = (int) y + data_window.min.y;
result=exr_read_scanline_chunk_info(ctxt,part_index,yy,&cinfo);
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_update(ctxt,part_index,&cinfo,&decoder);
}
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_run(ctxt,part_index,&decoder);
if (result != EXR_ERR_SUCCESS)
break;
scans_count_to_read=MagickMin((size_t) scans_per_chunk,image->rows-y);
pixel_count_to_read=scans_count_to_read*image->columns;
q=QueueAuthenticPixels(image,0,y,image->columns,scans_count_to_read,
exception);
if (q == (Quantum *) NULL)
break;
status=ReadEXRPixels(image,decoder,pixel_channels,data,q,pixel_count_to_read,
image->columns,0,exception);
if (status == MagickFalse)
break;
}
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
if ((status != MagickFalse) && (result != EXR_ERR_SUCCESS))
status=MagickFalse;
return(status);
}
static MagickBooleanType ReadEXRTiledImage(exr_context_t ctxt,int part_index,
Image *image,ExceptionInfo* exception)
{
exr_chunk_info_t
cinfo;
exr_decode_pipeline_t
decoder = EXR_DECODE_PIPELINE_INITIALIZER;
exr_result_t
result;
int
tilex = 0,
tiley = 0;
MagickBooleanType
status;
PixelChannel
pixel_channels[MaxPixelChannels] = { UndefinedPixelChannel };
int32_t
levels_x,
levels_y,
tile_width = 0,
tile_height = 0;
size_t
pixel_size,
pixel_count;
ssize_t
y;
uint8_t
*data;
result=exr_get_tile_levels(ctxt,part_index,&levels_x,&levels_y);
if (result == EXR_ERR_SUCCESS)
result=exr_get_tile_sizes(ctxt,part_index,0,0,&tile_width,&tile_height);
if (result == EXR_ERR_SUCCESS)
result=exr_read_tile_chunk_info(ctxt,0,tilex,tiley,0,0,&cinfo);
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_initialize(ctxt,part_index,&cinfo,&decoder);
if (result != EXR_ERR_SUCCESS)
ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
if ((levels_x != 1) || (levels_y != 1))
{
exr_decoding_destroy(ctxt,&decoder);
(void) ThrowMagickException(exception,GetMagickModule(),
CorruptImageError,"Unsupported number of levels","`%d %d'",
levels_x,levels_y);
return(MagickFalse);
}
pixel_count=(size_t)tile_width*tile_height;
status=InitializeEXRChannels(image,ctxt,decoder,pixel_count,tile_width,
pixel_channels,&pixel_size,&data,exception);
if (status == MagickFalse)
{
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
return(status);
}
result=exr_decoding_choose_default_routines(ctxt,part_index,&decoder);
if (result != EXR_ERR_SUCCESS)
{
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
}
for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) tile_height,tiley++)
{
Quantum
*q;
ssize_t
x;
tilex=0;
for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) tile_width,tilex++)
{
size_t
columns,
rows,
stride;
if ((y != 0) || (x != 0))
{
result=exr_read_tile_chunk_info(ctxt,0,tilex,tiley,0,0,&cinfo);
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_update(ctxt,part_index,&cinfo,&decoder);
}
if (result == EXR_ERR_SUCCESS)
result=exr_decoding_run(ctxt,part_index,&decoder);
if (result != EXR_ERR_SUCCESS)
break;
columns=MagickMin((size_t) tile_width,(size_t) image->columns-x);
rows=MagickMin((size_t) tile_height,(size_t) image->rows-y);
pixel_count=columns*rows;
q=QueueAuthenticPixels(image,x,y,columns,rows,exception);
if (q == (Quantum *) NULL)
break;
stride=(size_t) tile_width-columns;
if (stride != 0)
stride*=pixel_size;
status=ReadEXRPixels(image,decoder,pixel_channels,data,q,pixel_count,
columns,stride,exception);
if (status == MagickFalse)
break;
}
if ((status == MagickFalse) || (result != EXR_ERR_SUCCESS))
break;
}
data=(uint8_t *) RelinquishMagickMemory(data);
exr_decoding_destroy(ctxt,&decoder);
if ((status != MagickFalse) && (result != EXR_ERR_SUCCESS))
status=MagickFalse;
return(status);
}
static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
exr_attr_box2i_t
data_window;
exr_compression_t
compression = EXR_COMPRESSION_NONE;
exr_context_t
ctxt;
exr_context_initializer_t
initializer = EXR_DEFAULT_CONTEXT_INITIALIZER;
exr_result_t
result;
exr_storage_t
storage_type = EXR_STORAGE_LAST_TYPE;
EXRUserData
user_data = { 0 };
Image
*image;
int
part_index = 0;
MagickBooleanType
status;
/*
Open image.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
image=AcquireImage(image_info,exception);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
image=DestroyImageList(image);
return((Image *) NULL);
}
user_data.exception=exception;
initializer.error_handler_fn=EXRError;
initializer.user_data=(void *) &user_data;
initializer.max_image_height=(int) MagickMin(INT_MAX,
GetMagickResourceLimit(HeightResource));
initializer.max_image_width=(int) MagickMin(INT_MAX,
GetMagickResourceLimit(WidthResource));
result=exr_start_read(&ctxt,image->filename,&initializer);
if (result != EXR_ERR_SUCCESS)
{
exr_finish(&ctxt);
ThrowReaderException(BlobError,"UnableToOpenBlob");
}
result=exr_get_data_window(ctxt,part_index,&data_window);
if (result == EXR_ERR_SUCCESS)
result=exr_get_compression(ctxt,part_index,&compression);
if (result == EXR_ERR_SUCCESS)
result=exr_get_storage(ctxt,part_index,&storage_type);
if ((result != EXR_ERR_SUCCESS) ||
(data_window.min.x >= data_window.max.x) ||
(data_window.min.y >= data_window.max.y))
{
exr_finish(&ctxt);
ThrowReaderException(CorruptImageError,"CorruptImage");
}
image->columns=(size_t) (data_window.max.x-data_window.min.x+1L);
image->rows=(size_t) (data_window.max.y-data_window.min.y+1L);
image->gamma=1.0;
image->compression=NoCompression;
switch (compression)
{
case EXR_COMPRESSION_RLE: image->compression=RLECompression; break;
case EXR_COMPRESSION_ZIPS: image->compression=ZipSCompression; break;
case EXR_COMPRESSION_ZIP: image->compression=ZipCompression; break;
case EXR_COMPRESSION_PIZ: image->compression=PizCompression; break;
case EXR_COMPRESSION_PXR24: image->compression=Pxr24Compression; break;
case EXR_COMPRESSION_B44: image->compression=B44Compression; break;
case EXR_COMPRESSION_B44A: image->compression=B44ACompression; break;
case EXR_COMPRESSION_DWAA: image->compression=DWAACompression; break;
case EXR_COMPRESSION_DWAB: image->compression=DWABCompression; break;
default: break;
}
if (image_info->ping != MagickFalse)
{
exr_finish(&ctxt);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
status=SetImageExtent(image,image->columns,image->rows,exception);
if (status == MagickFalse)
{
exr_finish(&ctxt);
(void) CloseBlob(image);
return(DestroyImageList(image));
}
switch (storage_type)
{
case EXR_STORAGE_SCANLINE:
case EXR_STORAGE_DEEP_SCANLINE:
status=ReadEXRScanlineImage(ctxt,part_index,data_window,image,exception);
break;
case EXR_STORAGE_TILED:
case EXR_STORAGE_DEEP_TILED:
status=ReadEXRTiledImage(ctxt,part_index,image,exception);
break;
default:
(void) ThrowMagickException(exception,GetMagickModule(),
CorruptImageError,"Unsupported storage type","`%d'",
(int) storage_type);
status=MagickFalse;
break;
}
exr_finish(&ctxt);
(void) CloseBlob(image);
if (status == MagickFalse)
return(DestroyImageList(image));
return(GetFirstImageInList(image));
}
#else
static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
const ImfHeader
*hdr_info;
EXRWindowInfo
data_window,
display_window;
Image
*image;
ImfInputFile
*file;
ImfRgba
*scanline;
int
compression;
MagickBooleanType
status;
Quantum
*q;
size_t
columns;
ssize_t
y;
/*
Open image.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
image=AcquireImage(image_info,exception);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
image=DestroyImageList(image);
return((Image *) NULL);
}
file=ImfOpenInputFile(image->filename);
if (file == (ImfInputFile *) NULL)
{
ThrowFileException(exception,BlobError,"UnableToOpenBlob",
ImfErrorMessage());
image=DestroyImageList(image);
return((Image *) NULL);
}
hdr_info=ImfInputHeader(file);
ImfHeaderDataWindow(hdr_info,&display_window.min_x,&display_window.min_y,
&display_window.max_x,&display_window.max_y);
image->columns=(size_t) (display_window.max_x-display_window.min_x+1L);
image->rows=(size_t) (display_window.max_y-display_window.min_y+1L);
image->alpha_trait=BlendPixelTrait;
(void) SetImageColorspace(image,RGBColorspace,exception);
image->gamma=1.0;
image->compression=NoCompression;
compression=ImfHeaderCompression(hdr_info);
if (compression == IMF_RLE_COMPRESSION)
image->compression=RLECompression;
if (compression == IMF_ZIPS_COMPRESSION)
image->compression=ZipSCompression;
if (compression == IMF_ZIP_COMPRESSION)
image->compression=ZipCompression;
if (compression == IMF_PIZ_COMPRESSION)
image->compression=PizCompression;
if (compression == IMF_PXR24_COMPRESSION)
image->compression=Pxr24Compression;
#if defined(IMF_B44_COMPRESSION)
if (compression == IMF_B44_COMPRESSION)
image->compression=B44Compression;
#endif
#if defined(IMF_B44A_COMPRESSION)
if (compression == IMF_B44A_COMPRESSION)
image->compression=B44ACompression;
#endif
#if defined(IMF_DWAA_COMPRESSION)
if (compression == IMF_DWAA_COMPRESSION)
image->compression=DWAACompression;
#endif
#if defined(IMF_DWAB_COMPRESSION)
if (compression == IMF_DWAB_COMPRESSION)
image->compression=DWABCompression;
#endif
if (image_info->ping != MagickFalse)
{
(void) ImfCloseInputFile(file);
(void) CloseBlob(image);
return(GetFirstImageInList(image));
}
status=SetImageExtent(image,image->columns,image->rows,exception);
if (status == MagickFalse)
{
(void) ImfCloseInputFile(file);
return(DestroyImageList(image));
}
ImfHeaderDataWindow(hdr_info,&data_window.min_x,&data_window.min_y,
&data_window.max_x,&data_window.max_y);
columns=(size_t) (data_window.max_x-data_window.min_x+1L);
if ((display_window.min_x > data_window.max_x) ||
(display_window.min_x+(int) image->columns <= data_window.min_x))
scanline=(ImfRgba *) NULL;
else
{
scanline=(ImfRgba *) AcquireQuantumMemory(columns,sizeof(*scanline));
if (scanline == (ImfRgba *) NULL)
{
(void) ImfCloseInputFile(file);
image=DestroyImageList(image);
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
}
}
for (y=0; y < (ssize_t) image->rows; y++)
{
int
yy;
ssize_t
x;
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (Quantum *) NULL)
break;
yy=(int) (display_window.min_y+y);
if ((yy < data_window.min_y) || (yy > data_window.max_y) ||
(scanline == (ImfRgba *) NULL))
{
for (x=0; x < (ssize_t) image->columns; x++)
{
SetPixelViaPixelInfo(image,&image->background_color,q);
q+=(ptrdiff_t) GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
continue;
}
(void) memset(scanline,0,columns*sizeof(*scanline));
if (ImfInputSetFrameBuffer(file,scanline-data_window.min_x-(ssize_t) columns*yy,1,columns) == 0)
{
status=MagickFalse;
break;
}
if (ImfInputReadPixels(file,yy,yy) == 0)
{
status=MagickFalse;
break;
}
for (x=0; x < (ssize_t) image->columns; x++)
{
int
xx;
xx=(int) (display_window.min_x+x-data_window.min_x);
if ((xx < 0) || (display_window.min_x+(int) x > data_window.max_x))
SetPixelViaPixelInfo(image,&image->background_color,q);
else
{
SetPixelRed(image,ClampToQuantum((double) QuantumRange*
(double) ImfHalfToFloat(scanline[xx].r)),q);
SetPixelGreen(image,ClampToQuantum((double) QuantumRange*
(double) ImfHalfToFloat(scanline[xx].g)),q);
SetPixelBlue(image,ClampToQuantum((double) QuantumRange*
(double) ImfHalfToFloat(scanline[xx].b)),q);
SetPixelAlpha(image,ClampToQuantum((double) QuantumRange*
(double) ImfHalfToFloat(scanline[xx].a)),q);
}
q+=(ptrdiff_t) GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
break;
}
scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
(void) ImfCloseInputFile(file);
if (status == MagickFalse)
ThrowReaderException(CorruptImageError,"UnableToReadImageData");
if (CloseBlob(image) == MagickFalse)
status=MagickFalse;
if (status == MagickFalse)
return(DestroyImageList(image));
return(GetFirstImageInList(image));
}
#endif
#endif
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e g i s t e r E X R I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RegisterEXRImage() adds properties for the EXR image format
% to the list of supported formats. The properties include the image format
% tag, a method to read and/or write the format, whether the format
% supports the saving of more than one frame to the same file or blob,
% whether the format supports native in-memory I/O, and a brief
% description of the format.
%
% The format of the RegisterEXRImage method is:
%
% size_t RegisterEXRImage(void)
%
*/
ModuleExport size_t RegisterEXRImage(void)
{
char
version[MagickPathExtent];
MagickInfo
*entry;
*version='\0';
entry=AcquireMagickInfo("EXR","EXR","High Dynamic-range (HDR)");
#if defined(MAGICKCORE_OPENEXR_DELEGATE)
entry->decoder=(DecodeImageHandler *) ReadEXRImage;
entry->encoder=(EncodeImageHandler *) WriteEXRImage;
#if defined( OPENEXR_PACKAGE_STRING)
(void) FormatLocaleString(version,MagickPathExtent,OPENEXR_PACKAGE_STRING);
#endif
#endif
entry->magick=(IsImageFormatHandler *) IsEXR;
if (*version != '\0')
entry->version=ConstantString(version);
entry->flags|=CoderDecoderSeekableStreamFlag;
entry->flags^=CoderAdjoinFlag;
entry->flags^=CoderBlobSupportFlag;
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U n r e g i s t e r E X R I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% UnregisterEXRImage() removes format registrations made by the
% EXR module from the list of supported formats.
%
% The format of the UnregisterEXRImage method is:
%
% UnregisterEXRImage(void)
%
*/
ModuleExport void UnregisterEXRImage(void)
{
(void) UnregisterMagickInfo("EXR");
}
#if defined(MAGICKCORE_OPENEXR_DELEGATE)
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% W r i t e E X R I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% WriteEXRImage() writes an image to a file the in the high dynamic-range
% (HDR) file format developed by Industrial Light & Magic.
%
% The format of the WriteEXRImage method is:
%
% MagickBooleanType WriteEXRImage(const ImageInfo *image_info,
% Image *image,ExceptionInfo *exception)
%
% A description of each parameter follows.
%
% o image_info: the image info.
%
% o image: The image.
%
% o exception: return any errors or warnings in this structure.
%
*/
static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image,
ExceptionInfo *exception)
{
const char
*sampling_factor,
*value;
ImageInfo
*write_info;
ImfHalf
half_quantum;
ImfHeader
*hdr_info;
ImfOutputFile
*file;
ImfRgba
*scanline;
int
channels,
compression,
factors[3];
MagickBooleanType
status;
const Quantum
*p;
ssize_t
x;
ssize_t
y;
/*
Open output image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
assert(image != (Image *) NULL);
assert(image->signature == MagickCoreSignature);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
if (status == MagickFalse)
return(status);
(void) SetImageColorspace(image,RGBColorspace,exception);
write_info=CloneImageInfo(image_info);
(void) AcquireUniqueFilename(write_info->filename);
hdr_info=ImfNewHeader();
ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int)
image->rows-1);
ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int)
image->rows-1);
compression=IMF_NO_COMPRESSION;
if (write_info->compression == RLECompression)
compression=IMF_RLE_COMPRESSION;
if (write_info->compression == ZipSCompression)
compression=IMF_ZIPS_COMPRESSION;
if (write_info->compression == ZipCompression)
compression=IMF_ZIP_COMPRESSION;
if (write_info->compression == PizCompression)
compression=IMF_PIZ_COMPRESSION;
if (write_info->compression == Pxr24Compression)
compression=IMF_PXR24_COMPRESSION;
#if defined(IMF_B44_COMPRESSION)
if (write_info->compression == B44Compression)
compression=IMF_B44_COMPRESSION;
#endif
#if defined(IMF_B44A_COMPRESSION)
if (write_info->compression == B44ACompression)
compression=IMF_B44A_COMPRESSION;
#endif
#if defined(IMF_DWAA_COMPRESSION)
if (write_info->compression == DWAACompression)
compression=IMF_DWAA_COMPRESSION;
#endif
#if defined(IMF_DWAB_COMPRESSION)
if (write_info->compression == DWABCompression)
compression=IMF_DWAB_COMPRESSION;
#endif
channels=0;
value=GetImageOption(image_info,"exr:color-type");
if (value != (const char *) NULL)
{
if (LocaleCompare(value,"RGB") == 0)
channels=IMF_WRITE_RGB;
else if (LocaleCompare(value,"RGBA") == 0)
channels=IMF_WRITE_RGBA;
else if (LocaleCompare(value,"YC") == 0)
channels=IMF_WRITE_YC;
else if (LocaleCompare(value,"YCA") == 0)
channels=IMF_WRITE_YCA;
else if (LocaleCompare(value,"Y") == 0)
channels=IMF_WRITE_Y;
else if (LocaleCompare(value,"YA") == 0)
channels=IMF_WRITE_YA;
else if (LocaleCompare(value,"R") == 0)
channels=IMF_WRITE_R;
else if (LocaleCompare(value,"G") == 0)
channels=IMF_WRITE_G;
else if (LocaleCompare(value,"B") == 0)
channels=IMF_WRITE_B;
else if (LocaleCompare(value,"A") == 0)
channels=IMF_WRITE_A;
else
(void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
"ignoring invalid defined exr:color-type","=%s",value);
}
sampling_factor=(const char *) NULL;
factors[0]=0;
if (image_info->sampling_factor != (char *) NULL)
sampling_factor=image_info->sampling_factor;
if (sampling_factor != NULL)
{
/*
Sampling factors, valid values are 1x1 or 2x2.
*/
if (MagickSscanf(sampling_factor,"%d:%d:%d",factors,factors+1,factors+2) == 3)
{
if ((factors[0] == factors[1]) && (factors[1] == factors[2]))
factors[0]=1;
else
if ((factors[0] == (2*factors[1])) && (factors[2] == 0))
factors[0]=2;
}
else
if (MagickSscanf(sampling_factor,"%dx%d",factors,factors+1) == 2)
{
if (factors[0] != factors[1])
factors[0]=0;
}
if ((factors[0] != 1) && (factors[0] != 2))
(void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
"ignoring sampling-factor","=%s",sampling_factor);
else if (channels != 0)
{
/*
Cross check given color type and subsampling.
*/
factors[1]=((channels == IMF_WRITE_YCA) ||
(channels == IMF_WRITE_YC)) ? 2 : 1;
if (factors[0] != factors[1])
(void) ThrowMagickException(exception,GetMagickModule(),
CoderWarning,"sampling-factor and color type mismatch","=%s",
sampling_factor);
}
}
if (channels == 0)
{
/*
If no color type given, select it now.
*/
if (factors[0] == 2)
channels=image->alpha_trait != UndefinedPixelTrait ? IMF_WRITE_YCA :
IMF_WRITE_YC;
else
channels=image->alpha_trait != UndefinedPixelTrait ? IMF_WRITE_RGBA :
IMF_WRITE_RGB;
}
ImfHeaderSetCompression(hdr_info,compression);
ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y);
file=ImfOpenOutputFile(write_info->filename,hdr_info,channels);
ImfDeleteHeader(hdr_info);
if (file == (ImfOutputFile *) NULL)
{
(void) RelinquishUniqueFileResource(write_info->filename);
write_info=DestroyImageInfo(write_info);
ThrowFileException(exception,BlobError,"UnableToOpenBlob",
ImfErrorMessage());
return(MagickFalse);
}
scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline));
if (scanline == (ImfRgba *) NULL)
{
(void) ImfCloseOutputFile(file);
(void) RelinquishUniqueFileResource(write_info->filename);
write_info=DestroyImageInfo(write_info);
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
}
memset(scanline,0,image->columns*sizeof(*scanline));
for (y=0; y < (ssize_t) image->rows; y++)
{
p=GetVirtualPixels(image,0,y,image->columns,1,exception);
if (p == (const Quantum *) NULL)
break;
for (x=0; x < (ssize_t) image->columns; x++)
{
ImfFloatToHalf((float) (QuantumScale*(double) GetPixelRed(image,p)),
&half_quantum);
scanline[x].r=half_quantum;
ImfFloatToHalf((float) (QuantumScale*(double) GetPixelGreen(image,p)),
&half_quantum);
scanline[x].g=half_quantum;
ImfFloatToHalf((float) (QuantumScale*(double) GetPixelBlue(image,p)),
&half_quantum);
scanline[x].b=half_quantum;
if ((image->alpha_trait & BlendPixelTrait) == 0)
ImfFloatToHalf(1.0,&half_quantum);
else
ImfFloatToHalf((float) (QuantumScale*(double) GetPixelAlpha(image,p)),
&half_quantum);
scanline[x].a=half_quantum;
p+=(ptrdiff_t) GetPixelChannels(image);
}
ImfOutputSetFrameBuffer(file,scanline-(y*(ssize_t) image->columns),1,
image->columns);
ImfOutputWritePixels(file,1);
}
(void) ImfCloseOutputFile(file);
scanline=(ImfRgba *) RelinquishMagickMemory(scanline);
(void) FileToImage(image,write_info->filename,exception);
(void) RelinquishUniqueFileResource(write_info->filename);
write_info=DestroyImageInfo(write_info);
if (CloseBlob(image) == MagickFalse)
status=MagickFalse;
return(status);
}
#endif
|