| 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 | #include "magick/studio.h" |
|---|
| 43 | #include "magick/blob.h" |
|---|
| 44 | #include "magick/blob-private.h" |
|---|
| 45 | #include "magick/cache.h" |
|---|
| 46 | #include "magick/exception.h" |
|---|
| 47 | #include "magick/exception-private.h" |
|---|
| 48 | #include "magick/image.h" |
|---|
| 49 | #include "magick/image-private.h" |
|---|
| 50 | #include "magick/list.h" |
|---|
| 51 | #include "magick/magick.h" |
|---|
| 52 | #include "magick/memory_.h" |
|---|
| 53 | #include "magick/property.h" |
|---|
| 54 | #include "magick/quantum-private.h" |
|---|
| 55 | #include "magick/static.h" |
|---|
| 56 | #include "magick/string_.h" |
|---|
| 57 | #include "magick/module.h" |
|---|
| 58 | #include "magick/resource_.h" |
|---|
| 59 | #include "magick/utility.h" |
|---|
| 60 | #if defined(MAGICKCORE_OPENEXR_DELEGATE) |
|---|
| 61 | #include <ImfCRgbaFile.h> |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | static MagickBooleanType |
|---|
| 67 | WriteEXRImage(const ImageInfo *,Image *); |
|---|
| 68 | #endif |
|---|
| 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 | static MagickBooleanType IsEXR(const unsigned char *magick,const size_t length) |
|---|
| 96 | { |
|---|
| 97 | if (length < 4) |
|---|
| 98 | return(MagickFalse); |
|---|
| 99 | if (memcmp(magick,"\166\057\061\001",4) == 0) |
|---|
| 100 | return(MagickTrue); |
|---|
| 101 | return(MagickFalse); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #if defined(MAGICKCORE_OPENEXR_DELEGATE) |
|---|
| 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 | static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception) |
|---|
| 132 | { |
|---|
| 133 | const ImfHeader |
|---|
| 134 | *hdr_info; |
|---|
| 135 | |
|---|
| 136 | Image |
|---|
| 137 | *image; |
|---|
| 138 | |
|---|
| 139 | ImageInfo |
|---|
| 140 | *read_info; |
|---|
| 141 | |
|---|
| 142 | ImfInputFile |
|---|
| 143 | *file; |
|---|
| 144 | |
|---|
| 145 | ImfRgba |
|---|
| 146 | *scanline; |
|---|
| 147 | |
|---|
| 148 | int |
|---|
| 149 | max_x, |
|---|
| 150 | max_y, |
|---|
| 151 | min_x, |
|---|
| 152 | min_y; |
|---|
| 153 | |
|---|
| 154 | long |
|---|
| 155 | y; |
|---|
| 156 | |
|---|
| 157 | register long |
|---|
| 158 | x; |
|---|
| 159 | |
|---|
| 160 | register PixelPacket |
|---|
| 161 | *q; |
|---|
| 162 | |
|---|
| 163 | MagickBooleanType |
|---|
| 164 | status; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 170 | assert(image_info->signature == MagickSignature); |
|---|
| 171 | if (image_info->debug != MagickFalse) |
|---|
| 172 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
|---|
| 173 | image_info->filename); |
|---|
| 174 | assert(exception != (ExceptionInfo *) NULL); |
|---|
| 175 | assert(exception->signature == MagickSignature); |
|---|
| 176 | image=AcquireImage(image_info); |
|---|
| 177 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
|---|
| 178 | if (status == MagickFalse) |
|---|
| 179 | { |
|---|
| 180 | image=DestroyImageList(image); |
|---|
| 181 | return((Image *) NULL); |
|---|
| 182 | } |
|---|
| 183 | read_info=CloneImageInfo(image_info); |
|---|
| 184 | if (IsPathAccessible(read_info->filename) == MagickFalse) |
|---|
| 185 | { |
|---|
| 186 | (void) AcquireUniqueFilename(read_info->filename); |
|---|
| 187 | (void) ImageToFile(image,read_info->filename,exception); |
|---|
| 188 | } |
|---|
| 189 | file=ImfOpenInputFile(read_info->filename); |
|---|
| 190 | if (file == (ImfInputFile *) NULL) |
|---|
| 191 | { |
|---|
| 192 | ThrowFileException(exception,BlobError,"UnableToOpenBlob", |
|---|
| 193 | ImfErrorMessage()); |
|---|
| 194 | read_info=DestroyImageInfo(read_info); |
|---|
| 195 | return((Image *) NULL); |
|---|
| 196 | } |
|---|
| 197 | hdr_info=ImfInputHeader(file); |
|---|
| 198 | ImfHeaderDataWindow(hdr_info,&min_x,&min_y,&max_x,&max_y); |
|---|
| 199 | image->columns=max_x-min_x+1UL; |
|---|
| 200 | image->rows=max_y-min_y+1UL; |
|---|
| 201 | image->matte=MagickTrue; |
|---|
| 202 | if (image_info->ping != MagickFalse) |
|---|
| 203 | { |
|---|
| 204 | (void) ImfCloseInputFile(file); |
|---|
| 205 | if (LocaleCompare(image_info->filename,read_info->filename) != 0) |
|---|
| 206 | (void) RelinquishUniqueFileResource(read_info->filename); |
|---|
| 207 | read_info=DestroyImageInfo(read_info); |
|---|
| 208 | (void) CloseBlob(image); |
|---|
| 209 | return(GetFirstImageInList(image)); |
|---|
| 210 | } |
|---|
| 211 | scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline)); |
|---|
| 212 | if (scanline == (ImfRgba *) NULL) |
|---|
| 213 | { |
|---|
| 214 | (void) ImfCloseInputFile(file); |
|---|
| 215 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 216 | } |
|---|
| 217 | for (y=0; y < (long) image->rows; y++) |
|---|
| 218 | { |
|---|
| 219 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
|---|
| 220 | if (q == (PixelPacket *) NULL) |
|---|
| 221 | break; |
|---|
| 222 | ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1, |
|---|
| 223 | image->columns); |
|---|
| 224 | ImfInputReadPixels(file,min_y+y,min_y+y); |
|---|
| 225 | for (x=0; x < (long) image->columns; x++) |
|---|
| 226 | { |
|---|
| 227 | q->red=RoundToQuantum((MagickRealType) QuantumRange*ImfHalfToFloat( |
|---|
| 228 | scanline[x].r)); |
|---|
| 229 | q->green=RoundToQuantum((MagickRealType) QuantumRange*ImfHalfToFloat( |
|---|
| 230 | scanline[x].g)); |
|---|
| 231 | q->blue=RoundToQuantum((MagickRealType) QuantumRange*ImfHalfToFloat( |
|---|
| 232 | scanline[x].b)); |
|---|
| 233 | q->opacity=RoundToQuantum((MagickRealType) QuantumRange-QuantumRange* |
|---|
| 234 | ImfHalfToFloat(scanline[x].a)); |
|---|
| 235 | q++; |
|---|
| 236 | } |
|---|
| 237 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
|---|
| 238 | break; |
|---|
| 239 | } |
|---|
| 240 | scanline=(ImfRgba *) RelinquishMagickMemory(scanline); |
|---|
| 241 | (void) ImfCloseInputFile(file); |
|---|
| 242 | if (LocaleCompare(image_info->filename,read_info->filename) != 0) |
|---|
| 243 | (void) RelinquishUniqueFileResource(read_info->filename); |
|---|
| 244 | read_info=DestroyImageInfo(read_info); |
|---|
| 245 | (void) CloseBlob(image); |
|---|
| 246 | return(GetFirstImageInList(image)); |
|---|
| 247 | } |
|---|
| 248 | #endif |
|---|
| 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 | ModuleExport unsigned long RegisterEXRImage(void) |
|---|
| 274 | { |
|---|
| 275 | MagickInfo |
|---|
| 276 | *entry; |
|---|
| 277 | |
|---|
| 278 | entry=SetMagickInfo("EXR"); |
|---|
| 279 | #if defined(MAGICKCORE_OPENEXR_DELEGATE) |
|---|
| 280 | entry->decoder=(DecodeImageHandler *) ReadEXRImage; |
|---|
| 281 | entry->encoder=(EncodeImageHandler *) WriteEXRImage; |
|---|
| 282 | #endif |
|---|
| 283 | entry->magick=(IsImageFormatHandler *) IsEXR; |
|---|
| 284 | entry->adjoin=MagickFalse; |
|---|
| 285 | entry->description=ConstantString("High Dynamic-range (HDR)"); |
|---|
| 286 | entry->blob_support=MagickFalse; |
|---|
| 287 | entry->module=ConstantString("EXR"); |
|---|
| 288 | (void) RegisterMagickInfo(entry); |
|---|
| 289 | return(MagickImageCoderSignature); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | ModuleExport void UnregisterEXRImage(void) |
|---|
| 312 | { |
|---|
| 313 | (void) UnregisterMagickInfo("EXR"); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | #if defined(MAGICKCORE_OPENEXR_DELEGATE) |
|---|
| 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 | static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image) |
|---|
| 343 | { |
|---|
| 344 | ImageInfo |
|---|
| 345 | *write_info; |
|---|
| 346 | |
|---|
| 347 | ImfHalf |
|---|
| 348 | half_quantum; |
|---|
| 349 | |
|---|
| 350 | ImfHeader |
|---|
| 351 | *hdr_info; |
|---|
| 352 | |
|---|
| 353 | ImfOutputFile |
|---|
| 354 | *file; |
|---|
| 355 | |
|---|
| 356 | ImfRgba |
|---|
| 357 | *scanline; |
|---|
| 358 | |
|---|
| 359 | int |
|---|
| 360 | compression; |
|---|
| 361 | |
|---|
| 362 | long |
|---|
| 363 | y; |
|---|
| 364 | |
|---|
| 365 | MagickBooleanType |
|---|
| 366 | status; |
|---|
| 367 | |
|---|
| 368 | register const PixelPacket |
|---|
| 369 | *p; |
|---|
| 370 | |
|---|
| 371 | register long |
|---|
| 372 | x; |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | assert(image_info != (const ImageInfo *) NULL); |
|---|
| 378 | assert(image_info->signature == MagickSignature); |
|---|
| 379 | assert(image != (Image *) NULL); |
|---|
| 380 | assert(image->signature == MagickSignature); |
|---|
| 381 | if (image->debug != MagickFalse) |
|---|
| 382 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
|---|
| 383 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
|---|
| 384 | if (status == MagickFalse) |
|---|
| 385 | return(status); |
|---|
| 386 | write_info=CloneImageInfo(image_info); |
|---|
| 387 | (void) AcquireUniqueFilename(write_info->filename); |
|---|
| 388 | hdr_info=ImfNewHeader(); |
|---|
| 389 | ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int) |
|---|
| 390 | image->rows-1); |
|---|
| 391 | ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int) |
|---|
| 392 | image->rows-1); |
|---|
| 393 | compression=IMF_NO_COMPRESSION; |
|---|
| 394 | if (write_info->compression == ZipSCompression) |
|---|
| 395 | compression=IMF_ZIPS_COMPRESSION; |
|---|
| 396 | if (write_info->compression == ZipCompression) |
|---|
| 397 | compression=IMF_ZIP_COMPRESSION; |
|---|
| 398 | if (write_info->compression == PizCompression) |
|---|
| 399 | compression=IMF_PIZ_COMPRESSION; |
|---|
| 400 | if (write_info->compression == Pxr24Compression) |
|---|
| 401 | compression=IMF_PXR24_COMPRESSION; |
|---|
| 402 | #if defined(B44Compression) |
|---|
| 403 | if (write_info->compression == B44Compression) |
|---|
| 404 | compression=IMF_B44_COMPRESSION; |
|---|
| 405 | #endif |
|---|
| 406 | #if defined(B44ACompression) |
|---|
| 407 | if (write_info->compression == B44ACompression) |
|---|
| 408 | compression=IMF_B44A_COMPRESSION; |
|---|
| 409 | #endif |
|---|
| 410 | ImfHeaderSetCompression(hdr_info,compression); |
|---|
| 411 | ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y); |
|---|
| 412 | file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA); |
|---|
| 413 | ImfDeleteHeader(hdr_info); |
|---|
| 414 | if (file == (ImfOutputFile *) NULL) |
|---|
| 415 | { |
|---|
| 416 | ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob", |
|---|
| 417 | ImfErrorMessage()); |
|---|
| 418 | write_info=DestroyImageInfo(write_info); |
|---|
| 419 | return(MagickFalse); |
|---|
| 420 | } |
|---|
| 421 | scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline)); |
|---|
| 422 | if (scanline == (ImfRgba *) NULL) |
|---|
| 423 | { |
|---|
| 424 | (void) ImfCloseOutputFile(file); |
|---|
| 425 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
|---|
| 426 | } |
|---|
| 427 | for (y=0; y < (long) image->rows; y++) |
|---|
| 428 | { |
|---|
| 429 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
|---|
| 430 | if (p == (const PixelPacket *) NULL) |
|---|
| 431 | break; |
|---|
| 432 | for (x=0; x < (long) image->columns; x++) |
|---|
| 433 | { |
|---|
| 434 | ImfFloatToHalf(QuantumScale*p->red,&half_quantum); |
|---|
| 435 | scanline[x].r=half_quantum; |
|---|
| 436 | ImfFloatToHalf(QuantumScale*p->green,&half_quantum); |
|---|
| 437 | scanline[x].g=half_quantum; |
|---|
| 438 | ImfFloatToHalf(QuantumScale*p->blue,&half_quantum); |
|---|
| 439 | scanline[x].b=half_quantum; |
|---|
| 440 | if (image->matte == MagickFalse) |
|---|
| 441 | ImfFloatToHalf(1.0,&half_quantum); |
|---|
| 442 | else |
|---|
| 443 | ImfFloatToHalf(1.0-QuantumScale*p->opacity,&half_quantum); |
|---|
| 444 | scanline[x].a=half_quantum; |
|---|
| 445 | p++; |
|---|
| 446 | } |
|---|
| 447 | ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns); |
|---|
| 448 | ImfOutputWritePixels(file,1); |
|---|
| 449 | } |
|---|
| 450 | (void) ImfCloseOutputFile(file); |
|---|
| 451 | scanline=(ImfRgba *) RelinquishMagickMemory(scanline); |
|---|
| 452 | (void) FileToImage(image,write_info->filename); |
|---|
| 453 | (void) RelinquishUniqueFileResource(write_info->filename); |
|---|
| 454 | write_info=DestroyImageInfo(write_info); |
|---|
| 455 | (void) CloseBlob(image); |
|---|
| 456 | return(MagickTrue); |
|---|
| 457 | } |
|---|
| 458 | #endif |
|---|