2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Radiance RGBE Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colorspace.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/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/property.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/string-private.h"
63 #include "MagickCore/module.h"
68 static MagickBooleanType
69 WriteHDRImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % IsHDR() returns MagickTrue if the image format type, identified by the
83 % magick string, is Radiance RGBE image format.
85 % The format of the IsHDR method is:
87 % MagickBooleanType IsHDR(const unsigned char *magick,
88 % const size_t length)
90 % A description of each parameter follows:
92 % o magick: compare image format pattern against these bytes.
94 % o length: Specifies the length of the magick string.
97 static MagickBooleanType IsHDR(const unsigned char *magick,
102 if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
104 if (LocaleNCompare((const char *) magick,"#?RGBE",6) == 0)
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % R e a d H D R I m a g e %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % ReadHDRImage() reads the Radiance RGBE image format and returns it. It
121 % allocates the memory necessary for the new Image structure and returns a
122 % pointer to the new image.
124 % The format of the ReadHDRImage method is:
126 % Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
128 % A description of each parameter follows:
130 % o image_info: the image info.
132 % o exception: return any errors or warnings in this structure.
135 static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
138 format[MaxTextExtent],
139 keyword[MaxTextExtent],
141 value[MaxTextExtent];
163 register unsigned char
178 assert(image_info != (const ImageInfo *) NULL);
179 assert(image_info->signature == MagickSignature);
180 if (image_info->debug != MagickFalse)
181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
182 image_info->filename);
183 assert(exception != (ExceptionInfo *) NULL);
184 assert(exception->signature == MagickSignature);
185 image=AcquireImage(image_info,exception);
186 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
187 if (status == MagickFalse)
189 image=DestroyImageList(image);
190 return((Image *) NULL);
198 c=ReadBlobByte(image);
201 image=DestroyImage(image);
202 return((Image *) NULL);
204 while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
218 Read comment-- any text between # and end-of-line.
220 length=MaxTextExtent;
221 comment=AcquireString((char *) NULL);
222 for (p=comment; comment != (char *) NULL; p++)
224 c=ReadBlobByte(image);
225 if ((c == EOF) || (c == (int) '\n'))
227 if ((size_t) (p-comment+1) >= length)
231 comment=(char *) ResizeQuantumMemory(comment,length+
232 MaxTextExtent,sizeof(*comment));
233 if (comment == (char *) NULL)
235 p=comment+strlen(comment);
239 if (comment == (char *) NULL)
240 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
242 (void) SetImageProperty(image,"comment",comment,exception);
243 comment=DestroyString(comment);
244 c=ReadBlobByte(image);
247 if (isalnum(c) == MagickFalse)
248 c=ReadBlobByte(image);
255 Determine a keyword and its value.
260 if ((size_t) (p-keyword) < (MaxTextExtent-1))
262 c=ReadBlobByte(image);
263 } while (isalnum(c) || (c == '_'));
265 value_expected=MagickFalse;
266 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
269 value_expected=MagickTrue;
270 c=ReadBlobByte(image);
272 if (LocaleCompare(keyword,"Y") == 0)
273 value_expected=MagickTrue;
274 if (value_expected == MagickFalse)
277 while ((c != '\n') && (c != '\0'))
279 if ((size_t) (p-value) < (MaxTextExtent-1))
281 c=ReadBlobByte(image);
285 Assign a value to the specified keyword.
292 if (LocaleCompare(keyword,"format") == 0)
294 (void) CopyMagickString(format,value,MaxTextExtent);
297 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
298 (void) SetImageProperty(image,tag,value,exception);
304 if (LocaleCompare(keyword,"gamma") == 0)
306 image->gamma=StringToDouble(value,(char **) NULL);
309 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
310 (void) SetImageProperty(image,tag,value,exception);
316 if (LocaleCompare(keyword,"primaries") == 0)
322 (void) sscanf(value,"%g %g %g %g %g %g %g %g",
323 &chromaticity[0],&chromaticity[1],&chromaticity[2],
324 &chromaticity[3],&chromaticity[4],&chromaticity[5],
325 &white_point[0],&white_point[1]);
326 image->chromaticity.red_primary.x=chromaticity[0];
327 image->chromaticity.red_primary.y=chromaticity[1];
328 image->chromaticity.green_primary.x=chromaticity[2];
329 image->chromaticity.green_primary.y=chromaticity[3];
330 image->chromaticity.blue_primary.x=chromaticity[4];
331 image->chromaticity.blue_primary.y=chromaticity[5];
332 image->chromaticity.white_point.x=white_point[0],
333 image->chromaticity.white_point.y=white_point[1];
336 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
337 (void) SetImageProperty(image,tag,value,exception);
343 if (strcmp(keyword,"Y") == 0)
349 (void) sscanf(value,"%d +X %d",&height,&width);
350 image->columns=(size_t) width;
351 image->rows=(size_t) height;
354 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
355 (void) SetImageProperty(image,tag,value,exception);
360 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
361 (void) SetImageProperty(image,tag,value,exception);
366 if ((image->columns == 0) && (image->rows == 0))
367 while (isspace((int) ((unsigned char) c)) != 0)
368 c=ReadBlobByte(image);
370 if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) &&
371 (LocaleCompare(format,"32-bit_rle_xyze") != 0))
372 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
373 if ((image->columns == 0) || (image->rows == 0))
374 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
375 if (LocaleCompare(format,"32-bit_rle_xyze") == 0)
376 image->colorspace=XYZColorspace;
377 image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
378 NoCompression : RLECompression;
379 if (image_info->ping != MagickFalse)
381 (void) CloseBlob(image);
382 return(GetFirstImageInList(image));
385 Read RGBE (red+green+blue+exponent) pixels.
387 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
389 if (pixels == (unsigned char *) NULL)
390 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
391 for (y=0; y < (ssize_t) image->rows; y++)
393 if (image->compression != RLECompression)
395 count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
396 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
401 count=ReadBlob(image,4*sizeof(*pixel),pixel);
404 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
406 (void) memcpy(pixels,pixel,4*sizeof(*pixel));
407 count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
408 image->compression=NoCompression;
413 for (i=0; i < 4; i++)
415 end=&pixels[(i+1)*image->columns];
418 count=ReadBlob(image,2*sizeof(*pixel),pixel);
423 count=(ssize_t) pixel[0]-128;
424 if ((count == 0) || (count > (ssize_t) (end-p)))
431 count=(ssize_t) pixel[0];
432 if ((count == 0) || (count > (ssize_t) (end-p)))
437 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
447 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
448 if (q == (Quantum *) NULL)
451 for (x=0; x < (ssize_t) image->columns; x++)
453 if (image->compression == RLECompression)
456 pixel[1]=pixels[x+image->columns];
457 pixel[2]=pixels[x+2*image->columns];
458 pixel[3]=pixels[x+3*image->columns];
462 pixel[0]=pixels[i++];
463 pixel[1]=pixels[i++];
464 pixel[2]=pixels[i++];
465 pixel[3]=pixels[i++];
467 SetPixelRed(image,0,q);
468 SetPixelGreen(image,0,q);
469 SetPixelBlue(image,0,q);
472 gamma=pow(2.0,pixel[3]-(128.0+8.0));
473 SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q);
474 SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q);
475 SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q);
477 q+=GetPixelChannels(image);
479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
481 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
483 if (status == MagickFalse)
486 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
487 if (EOFBlob(image) != MagickFalse)
488 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
490 (void) CloseBlob(image);
491 return(GetFirstImageInList(image));
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % R e g i s t e r H D R I m a g e %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
506 % list of supported formats. The attributes include the image format tag, a
507 % method to read and/or write the format, whether the format supports the
508 % saving of more than one frame to the same file or blob, whether the format
509 % supports native in-memory I/O, and a brief description of the format.
511 % The format of the RegisterHDRImage method is:
513 % size_t RegisterHDRImage(void)
516 ModuleExport size_t RegisterHDRImage(void)
521 entry=SetMagickInfo("HDR");
522 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
523 entry->encoder=(EncodeImageHandler *) WriteHDRImage;
524 entry->description=ConstantString("Radiance RGBE image format");
525 entry->module=ConstantString("HDR");
526 entry->magick=(IsImageFormatHandler *) IsHDR;
527 (void) RegisterMagickInfo(entry);
528 return(MagickImageCoderSignature);
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 % U n r e g i s t e r H D R I m a g e %
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 % UnregisterHDRImage() removes format registrations made by the
543 % HDR module from the list of supported formats.
545 % The format of the UnregisterHDRImage method is:
547 % UnregisterHDRImage(void)
550 ModuleExport void UnregisterHDRImage(void)
552 (void) UnregisterMagickInfo("HDR");
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % W r i t e H D R I m a g e %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % WriteHDRImage() writes an image in the Radience RGBE image format.
568 % The format of the WriteHDRImage method is:
570 % MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
571 % Image *image,ExceptionInfo *exception)
573 % A description of each parameter follows.
575 % o image_info: the image info.
577 % o image: The image.
581 static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
583 #define MinimumRunlength 4
599 for (p=0; p < image->columns; )
604 while ((runlength < MinimumRunlength) && (q < image->columns))
607 previous_count=(ssize_t) runlength;
609 while ((pixels[q] == pixels[q+runlength]) &&
610 ((q+runlength) < image->columns) && (runlength < 127))
613 if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
615 pixel[0]=(unsigned char) (128+previous_count);
617 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
623 count=(ssize_t) (q-p);
626 pixel[0]=(unsigned char) count;
627 if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
629 if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
633 if (runlength >= MinimumRunlength)
635 pixel[0]=(unsigned char) (128+runlength);
637 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
645 static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image,
646 ExceptionInfo *exception)
649 header[MaxTextExtent];
657 register const Quantum
676 Open output image file.
678 assert(image_info != (const ImageInfo *) NULL);
679 assert(image_info->signature == MagickSignature);
680 assert(image != (Image *) NULL);
681 assert(image->signature == MagickSignature);
682 if (image->debug != MagickFalse)
683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
684 assert(exception != (ExceptionInfo *) NULL);
685 assert(exception->signature == MagickSignature);
686 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
687 if (status == MagickFalse)
689 if (IsRGBColorspace(image->colorspace) == MagickFalse)
690 (void) TransformImageColorspace(image,sRGBColorspace,exception);
694 (void) ResetMagickMemory(header,' ',MaxTextExtent);
695 length=CopyMagickString(header,"#?RGBE\n",MaxTextExtent);
696 (void) WriteBlob(image,length,(unsigned char *) header);
697 property=GetImageProperty(image,"comment",exception);
698 if ((property != (const char *) NULL) &&
699 (strchr(property,'\n') == (char *) NULL))
701 count=FormatLocaleString(header,MaxTextExtent,"#%s\n",property);
702 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
704 property=GetImageProperty(image,"hdr:exposure",exception);
705 if (property != (const char *) NULL)
707 count=FormatLocaleString(header,MaxTextExtent,"EXPOSURE=%g\n",
709 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
711 if (image->gamma != 0.0)
713 count=FormatLocaleString(header,MaxTextExtent,"GAMMA=%g\n",image->gamma);
714 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
716 count=FormatLocaleString(header,MaxTextExtent,
717 "PRIMARIES=%g %g %g %g %g %g %g %g\n",
718 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
719 image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
720 image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
721 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
722 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
723 length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MaxTextExtent);
724 (void) WriteBlob(image,length,(unsigned char *) header);
725 count=FormatLocaleString(header,MaxTextExtent,"-Y %.20g +X %.20g\n",
726 (double) image->rows,(double) image->columns);
727 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
731 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
733 if (pixels == (unsigned char *) NULL)
734 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
735 for (y=0; y < (ssize_t) image->rows; y++)
737 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
738 if (p == (const Quantum *) NULL)
740 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
744 pixel[2]=(unsigned char) (image->columns >> 8);
745 pixel[3]=(unsigned char) (image->columns & 0xff);
746 count=WriteBlob(image,4*sizeof(*pixel),pixel);
747 if (count != (ssize_t) (4*sizeof(*pixel)))
751 for (x=0; x < (ssize_t) image->columns; x++)
760 gamma=QuantumScale*GetPixelRed(image,p);
761 if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
762 gamma=QuantumScale*GetPixelGreen(image,p);
763 if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
764 gamma=QuantumScale*GetPixelBlue(image,p);
765 if (gamma > MagickEpsilon)
770 gamma=frexp(gamma,&exponent)*256.0/gamma;
771 pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
772 pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
773 pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
774 pixel[3]=(unsigned char) (exponent+128);
776 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
779 pixels[x+image->columns]=pixel[1];
780 pixels[x+2*image->columns]=pixel[2];
781 pixels[x+3*image->columns]=pixel[3];
785 pixels[i++]=pixel[0];
786 pixels[i++]=pixel[1];
787 pixels[i++]=pixel[2];
788 pixels[i++]=pixel[3];
790 p+=GetPixelChannels(image);
792 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
794 for (i=0; i < 4; i++)
795 length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
799 count=WriteBlob(image,4*image->columns*sizeof(*pixel),pixel);
800 if (count != (ssize_t) (4*image->columns*sizeof(*pixel)))
803 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
805 if (status == MagickFalse)
808 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
809 (void) CloseBlob(image);