2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Radiance RGBE Image Format %
20 % Copyright 1999-2011 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/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/property.h"
58 #include "MagickCore/quantum-private.h"
59 #include "MagickCore/static.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/string-private.h"
62 #include "MagickCore/module.h"
67 static MagickBooleanType
68 WriteHDRImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsHDR() returns MagickTrue if the image format type, identified by the
82 % magick string, is Radiance RGBE image format.
84 % The format of the IsHDR method is:
86 % MagickBooleanType IsHDR(const unsigned char *magick,
87 % const size_t length)
89 % A description of each parameter follows:
91 % o magick: compare image format pattern against these bytes.
93 % o length: Specifies the length of the magick string.
96 static MagickBooleanType IsHDR(const unsigned char *magick,
101 if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
103 if (LocaleNCompare((const char *) magick,"#?RGBE",6) == 0)
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % R e a d H D R I m a g e %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % ReadHDRImage() reads the Radiance RGBE image format and returns it. It
120 % allocates the memory necessary for the new Image structure and returns a
121 % pointer to the new image.
123 % The format of the ReadHDRImage method is:
125 % Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
127 % A description of each parameter follows:
129 % o image_info: the image info.
131 % o exception: return any errors or warnings in this structure.
134 static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
137 format[MaxTextExtent],
138 keyword[MaxTextExtent],
140 value[MaxTextExtent];
158 register unsigned char
177 assert(image_info != (const ImageInfo *) NULL);
178 assert(image_info->signature == MagickSignature);
179 if (image_info->debug != MagickFalse)
180 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
181 image_info->filename);
182 assert(exception != (ExceptionInfo *) NULL);
183 assert(exception->signature == MagickSignature);
184 image=AcquireImage(image_info);
185 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
186 if (status == MagickFalse)
188 image=DestroyImageList(image);
189 return((Image *) NULL);
197 c=ReadBlobByte(image);
200 image=DestroyImage(image);
201 return((Image *) NULL);
203 while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
217 Read comment-- any text between # and end-of-line.
219 length=MaxTextExtent;
220 comment=AcquireString((char *) NULL);
221 for (p=comment; comment != (char *) NULL; p++)
223 c=ReadBlobByte(image);
224 if ((c == EOF) || (c == (int) '\n'))
226 if ((size_t) (p-comment+1) >= length)
230 comment=(char *) ResizeQuantumMemory(comment,length+
231 MaxTextExtent,sizeof(*comment));
232 if (comment == (char *) NULL)
234 p=comment+strlen(comment);
238 if (comment == (char *) NULL)
239 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
241 (void) SetImageProperty(image,"comment",comment);
242 comment=DestroyString(comment);
243 c=ReadBlobByte(image);
246 if (isalnum(c) == MagickFalse)
247 c=ReadBlobByte(image);
254 Determine a keyword and its value.
259 if ((size_t) (p-keyword) < (MaxTextExtent-1))
261 c=ReadBlobByte(image);
262 } while (isalnum(c) || (c == '_'));
264 value_expected=MagickFalse;
265 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
268 value_expected=MagickTrue;
269 c=ReadBlobByte(image);
271 if (LocaleCompare(keyword,"Y") == 0)
272 value_expected=MagickTrue;
273 if (value_expected == MagickFalse)
276 while ((c != '\n') && (c != '\0'))
278 if ((size_t) (p-value) < (MaxTextExtent-1))
280 c=ReadBlobByte(image);
284 Assign a value to the specified keyword.
291 if (LocaleCompare(keyword,"format") == 0)
293 (void) CopyMagickString(format,value,MaxTextExtent);
296 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
297 (void) SetImageProperty(image,tag,value);
303 if (LocaleCompare(keyword,"gamma") == 0)
305 image->gamma=InterpretLocaleValue(value,(char **) NULL);
308 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
309 (void) SetImageProperty(image,tag,value);
315 if (LocaleCompare(keyword,"primaries") == 0)
321 (void) sscanf(value,"%g %g %g %g %g %g %g %g",
322 &chromaticity[0],&chromaticity[1],&chromaticity[2],
323 &chromaticity[3],&chromaticity[4],&chromaticity[5],
324 &white_point[0],&white_point[1]);
325 image->chromaticity.red_primary.x=chromaticity[0];
326 image->chromaticity.red_primary.y=chromaticity[1];
327 image->chromaticity.green_primary.x=chromaticity[2];
328 image->chromaticity.green_primary.y=chromaticity[3];
329 image->chromaticity.blue_primary.x=chromaticity[4];
330 image->chromaticity.blue_primary.y=chromaticity[5];
331 image->chromaticity.white_point.x=white_point[0],
332 image->chromaticity.white_point.y=white_point[1];
335 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
336 (void) SetImageProperty(image,tag,value);
342 if (strcmp(keyword,"Y") == 0)
348 (void) sscanf(value,"%d +X %d",&height,&width);
349 image->columns=(size_t) width;
350 image->rows=(size_t) height;
353 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
354 (void) SetImageProperty(image,tag,value);
359 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
360 (void) SetImageProperty(image,tag,value);
365 if ((image->columns == 0) && (image->rows == 0))
366 while (isspace((int) ((unsigned char) c)) != 0)
367 c=ReadBlobByte(image);
369 if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) &&
370 (LocaleCompare(format,"32-bit_rle_xyze") != 0))
371 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
372 if ((image->columns == 0) || (image->rows == 0))
373 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
374 if (LocaleCompare(format,"32-bit_rle_rgbe") == 0)
375 image->colorspace=XYZColorspace;
376 image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
377 NoCompression : RLECompression;
378 if (image_info->ping != MagickFalse)
380 (void) CloseBlob(image);
381 return(GetFirstImageInList(image));
384 Read RGBE (red+green+blue+exponent) pixels.
386 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
388 if (pixels == (unsigned char *) NULL)
389 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
390 for (y=0; y < (ssize_t) image->rows; y++)
392 if (image->compression != RLECompression)
394 count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
395 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
400 count=ReadBlob(image,4*sizeof(*pixel),pixel);
403 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
405 (void) memcpy(pixels,pixel,4*sizeof(*pixel));
406 count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
407 image->compression=NoCompression;
412 for (i=0; i < 4; i++)
414 end=&pixels[(i+1)*image->columns];
417 count=ReadBlob(image,2*sizeof(*pixel),pixel);
422 count=(ssize_t) pixel[0]-128;
423 if ((count == 0) || (count > (ssize_t) (end-p)))
430 count=(ssize_t) pixel[0];
431 if ((count == 0) || (count > (ssize_t) (end-p)))
436 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
446 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
447 if (q == (const Quantum *) NULL)
450 for (x=0; x < (ssize_t) image->columns; x++)
452 if (image->compression == RLECompression)
455 pixel[1]=pixels[x+image->columns];
456 pixel[2]=pixels[x+2*image->columns];
457 pixel[3]=pixels[x+3*image->columns];
461 pixel[0]=pixels[i++];
462 pixel[1]=pixels[i++];
463 pixel[2]=pixels[i++];
464 pixel[3]=pixels[i++];
466 SetPixelRed(image,0,q);
467 SetPixelGreen(image,0,q);
468 SetPixelBlue(image,0,q);
471 gamma=pow(2.0,pixel[3]-(128.0+8.0));
472 SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q);
473 SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q);
474 SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q);
476 q+=GetPixelChannels(image);
478 if (SyncAuthenticPixels(image,exception) == MagickFalse)
480 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
482 if (status == MagickFalse)
485 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
486 if (EOFBlob(image) != MagickFalse)
487 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
489 (void) CloseBlob(image);
490 return(GetFirstImageInList(image));
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % R e g i s t e r H D R I m a g e %
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
505 % list of supported formats. The attributes include the image format tag, a
506 % method to read and/or write the format, whether the format supports the
507 % saving of more than one frame to the same file or blob, whether the format
508 % supports native in-memory I/O, and a brief description of the format.
510 % The format of the RegisterHDRImage method is:
512 % size_t RegisterHDRImage(void)
515 ModuleExport size_t RegisterHDRImage(void)
520 entry=SetMagickInfo("HDR");
521 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
522 entry->encoder=(EncodeImageHandler *) WriteHDRImage;
523 entry->description=ConstantString("Radiance RGBE image format");
524 entry->module=ConstantString("HDR");
525 entry->magick=(IsImageFormatHandler *) IsHDR;
526 (void) RegisterMagickInfo(entry);
527 return(MagickImageCoderSignature);
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % U n r e g i s t e r H D R I m a g e %
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 % UnregisterHDRImage() removes format registrations made by the
542 % HDR module from the list of supported formats.
544 % The format of the UnregisterHDRImage method is:
546 % UnregisterHDRImage(void)
549 ModuleExport void UnregisterHDRImage(void)
551 (void) UnregisterMagickInfo("HDR");
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % W r i t e H D R I m a g e %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % WriteHDRImage() writes an image in the Radience RGBE image format.
567 % The format of the WriteHDRImage method is:
569 % MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
572 % A description of each parameter follows.
574 % o image_info: the image info.
576 % o image: The image.
580 static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
582 #define MinimumRunlength 4
598 for (p=0; p < image->columns; )
603 while ((runlength < MinimumRunlength) && (q < image->columns))
606 previous_count=(ssize_t) runlength;
608 while ((pixels[q] == pixels[q+runlength]) &&
609 ((q+runlength) < image->columns) && (runlength < 127))
612 if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
614 pixel[0]=(unsigned char) (128+previous_count);
616 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
622 count=(ssize_t) (q-p);
625 pixel[0]=(unsigned char) count;
626 if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
628 if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
632 if (runlength >= MinimumRunlength)
634 pixel[0]=(unsigned char) (128+runlength);
636 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
644 static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image)
647 header[MaxTextExtent];
655 register const Quantum
674 Open output image file.
676 assert(image_info != (const ImageInfo *) NULL);
677 assert(image_info->signature == MagickSignature);
678 assert(image != (Image *) NULL);
679 assert(image->signature == MagickSignature);
680 if (image->debug != MagickFalse)
681 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
682 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
683 if (status == MagickFalse)
685 if (image->colorspace != RGBColorspace)
686 (void) TransformImageColorspace(image,RGBColorspace);
690 (void) ResetMagickMemory(header,' ',MaxTextExtent);
691 length=CopyMagickString(header,"#?RGBE\n",MaxTextExtent);
692 (void) WriteBlob(image,length,(unsigned char *) header);
693 property=GetImageProperty(image,"comment");
694 if ((property != (const char *) NULL) &&
695 (strchr(property,'\n') == (char *) NULL))
697 count=FormatLocaleString(header,MaxTextExtent,"#%s\n",property);
698 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
700 property=GetImageProperty(image,"hdr:exposure");
701 if (property != (const char *) NULL)
703 count=FormatLocaleString(header,MaxTextExtent,"EXPOSURE=%g\n",
705 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
707 if (image->gamma != 0.0)
709 count=FormatLocaleString(header,MaxTextExtent,"GAMMA=%g\n",image->gamma);
710 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
712 count=FormatLocaleString(header,MaxTextExtent,
713 "PRIMARIES=%g %g %g %g %g %g %g %g\n",
714 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
715 image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
716 image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
717 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
718 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
719 length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MaxTextExtent);
720 (void) WriteBlob(image,length,(unsigned char *) header);
721 count=FormatLocaleString(header,MaxTextExtent,"-Y %.20g +X %.20g\n",
722 (double) image->rows,(double) image->columns);
723 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
727 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
729 if (pixels == (unsigned char *) NULL)
730 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
731 for (y=0; y < (ssize_t) image->rows; y++)
733 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
734 if (p == (const Quantum *) NULL)
736 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
740 pixel[2]=(unsigned char) (image->columns >> 8);
741 pixel[3]=(unsigned char) (image->columns & 0xff);
742 count=WriteBlob(image,4*sizeof(*pixel),pixel);
743 if (count != (ssize_t) (4*sizeof(*pixel)))
747 for (x=0; x < (ssize_t) image->columns; x++)
756 gamma=QuantumScale*GetPixelRed(image,p);
757 if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
758 gamma=QuantumScale*GetPixelGreen(image,p);
759 if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
760 gamma=QuantumScale*GetPixelBlue(image,p);
761 if (gamma > MagickEpsilon)
766 gamma=frexp(gamma,&exponent)*256.0/gamma;
767 pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
768 pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
769 pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
770 pixel[3]=(unsigned char) (exponent+128);
772 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
775 pixels[x+image->columns]=pixel[1];
776 pixels[x+2*image->columns]=pixel[2];
777 pixels[x+3*image->columns]=pixel[3];
781 pixels[i++]=pixel[0];
782 pixels[i++]=pixel[1];
783 pixels[i++]=pixel[2];
784 pixels[i++]=pixel[3];
786 p+=GetPixelChannels(image);
788 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
790 for (i=0; i < 4; i++)
791 length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
795 count=WriteBlob(image,4*image->columns*sizeof(*pixel),pixel);
796 if (count != (ssize_t) (4*image->columns*sizeof(*pixel)))
799 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
801 if (status == MagickFalse)
804 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
805 (void) CloseBlob(image);