2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Radiance RGBE Image Format %
20 % Copyright 1999-2017 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 % https://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[MagickPathExtent],
139 keyword[MagickPathExtent],
140 tag[MagickPathExtent],
141 value[MagickPathExtent];
163 register unsigned char
178 assert(image_info != (const ImageInfo *) NULL);
179 assert(image_info->signature == MagickCoreSignature);
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 == MagickCoreSignature);
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=MagickPathExtent;
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 MagickPathExtent,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) < (MagickPathExtent-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') && (c != EOF))
279 if ((size_t) (p-value) < (MagickPathExtent-1))
281 c=ReadBlobByte(image);
285 Assign a value to the specified keyword.
292 if (LocaleCompare(keyword,"format") == 0)
294 (void) CopyMagickString(format,value,MagickPathExtent);
297 (void) FormatLocaleString(tag,MagickPathExtent,"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,MagickPathExtent,"hdr:%s",keyword);
310 (void) SetImageProperty(image,tag,value,exception);
316 if (LocaleCompare(keyword,"primaries") == 0)
325 count=sscanf(value,"%g %g %g %g %g %g %g %g",&chromaticity[0],
326 &chromaticity[1],&chromaticity[2],&chromaticity[3],
327 &chromaticity[4],&chromaticity[5],&white_point[0],
331 image->chromaticity.red_primary.x=chromaticity[0];
332 image->chromaticity.red_primary.y=chromaticity[1];
333 image->chromaticity.green_primary.x=chromaticity[2];
334 image->chromaticity.green_primary.y=chromaticity[3];
335 image->chromaticity.blue_primary.x=chromaticity[4];
336 image->chromaticity.blue_primary.y=chromaticity[5];
337 image->chromaticity.white_point.x=white_point[0],
338 image->chromaticity.white_point.y=white_point[1];
342 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
343 (void) SetImageProperty(image,tag,value,exception);
352 if (strcmp(keyword,target) == 0)
358 if (sscanf(value,"%d +X %d",&height,&width) == 2)
360 image->columns=(size_t) width;
361 image->rows=(size_t) height;
365 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
366 (void) SetImageProperty(image,tag,value,exception);
371 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
372 (void) SetImageProperty(image,tag,value,exception);
377 if ((image->columns == 0) && (image->rows == 0))
378 while (isspace((int) ((unsigned char) c)) != 0)
379 c=ReadBlobByte(image);
381 if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) &&
382 (LocaleCompare(format,"32-bit_rle_xyze") != 0))
383 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
384 if ((image->columns == 0) || (image->rows == 0))
385 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
386 (void) SetImageColorspace(image,RGBColorspace,exception);
387 if (LocaleCompare(format,"32-bit_rle_xyze") == 0)
388 (void) SetImageColorspace(image,XYZColorspace,exception);
389 image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
390 NoCompression : RLECompression;
391 if (image_info->ping != MagickFalse)
393 (void) CloseBlob(image);
394 return(GetFirstImageInList(image));
396 status=SetImageExtent(image,image->columns,image->rows,exception);
397 if (status == MagickFalse)
398 return(DestroyImageList(image));
400 Read RGBE (red+green+blue+exponent) pixels.
402 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
404 if (pixels == (unsigned char *) NULL)
405 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
406 for (y=0; y < (ssize_t) image->rows; y++)
408 if (image->compression != RLECompression)
410 count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
411 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
416 count=ReadBlob(image,4*sizeof(*pixel),pixel);
419 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
421 (void) memcpy(pixels,pixel,4*sizeof(*pixel));
422 count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
423 image->compression=NoCompression;
428 for (i=0; i < 4; i++)
430 end=&pixels[(i+1)*image->columns];
433 count=ReadBlob(image,2*sizeof(*pixel),pixel);
438 count=(ssize_t) pixel[0]-128;
439 if ((count == 0) || (count > (ssize_t) (end-p)))
446 count=(ssize_t) pixel[0];
447 if ((count == 0) || (count > (ssize_t) (end-p)))
452 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
462 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
463 if (q == (Quantum *) NULL)
466 for (x=0; x < (ssize_t) image->columns; x++)
468 if (image->compression == RLECompression)
471 pixel[1]=pixels[x+image->columns];
472 pixel[2]=pixels[x+2*image->columns];
473 pixel[3]=pixels[x+3*image->columns];
477 pixel[0]=pixels[i++];
478 pixel[1]=pixels[i++];
479 pixel[2]=pixels[i++];
480 pixel[3]=pixels[i++];
482 SetPixelRed(image,0,q);
483 SetPixelGreen(image,0,q);
484 SetPixelBlue(image,0,q);
487 gamma=pow(2.0,pixel[3]-(128.0+8.0));
488 SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q);
489 SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q);
490 SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q);
492 q+=GetPixelChannels(image);
494 if (SyncAuthenticPixels(image,exception) == MagickFalse)
496 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
498 if (status == MagickFalse)
501 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
502 if (EOFBlob(image) != MagickFalse)
503 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
505 (void) CloseBlob(image);
506 return(GetFirstImageInList(image));
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 % R e g i s t e r H D R I m a g e %
518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 % RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
521 % list of supported formats. The attributes include the image format tag, a
522 % method to read and/or write the format, whether the format supports the
523 % saving of more than one frame to the same file or blob, whether the format
524 % supports native in-memory I/O, and a brief description of the format.
526 % The format of the RegisterHDRImage method is:
528 % size_t RegisterHDRImage(void)
531 ModuleExport size_t RegisterHDRImage(void)
536 entry=AcquireMagickInfo("HDR","HDR","Radiance RGBE image format");
537 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
538 entry->encoder=(EncodeImageHandler *) WriteHDRImage;
539 entry->magick=(IsImageFormatHandler *) IsHDR;
540 (void) RegisterMagickInfo(entry);
541 return(MagickImageCoderSignature);
545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 % U n r e g i s t e r H D R I m a g e %
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 % UnregisterHDRImage() removes format registrations made by the
556 % HDR module from the list of supported formats.
558 % The format of the UnregisterHDRImage method is:
560 % UnregisterHDRImage(void)
563 ModuleExport void UnregisterHDRImage(void)
565 (void) UnregisterMagickInfo("HDR");
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % W r i t e H D R I m a g e %
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % WriteHDRImage() writes an image in the Radience RGBE image format.
581 % The format of the WriteHDRImage method is:
583 % MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
584 % Image *image,ExceptionInfo *exception)
586 % A description of each parameter follows.
588 % o image_info: the image info.
590 % o image: The image.
594 static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
596 #define MinimumRunlength 4
612 for (p=0; p < image->columns; )
617 while ((runlength < MinimumRunlength) && (q < image->columns))
620 previous_count=(ssize_t) runlength;
622 while ((pixels[q] == pixels[q+runlength]) &&
623 ((q+runlength) < image->columns) && (runlength < 127))
626 if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
628 pixel[0]=(unsigned char) (128+previous_count);
630 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
636 count=(ssize_t) (q-p);
639 pixel[0]=(unsigned char) count;
640 if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
642 if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
646 if (runlength >= MinimumRunlength)
648 pixel[0]=(unsigned char) (128+runlength);
650 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
658 static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image,
659 ExceptionInfo *exception)
662 header[MagickPathExtent];
670 register const Quantum
689 Open output image file.
691 assert(image_info != (const ImageInfo *) NULL);
692 assert(image_info->signature == MagickCoreSignature);
693 assert(image != (Image *) NULL);
694 assert(image->signature == MagickCoreSignature);
695 if (image->debug != MagickFalse)
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
697 assert(exception != (ExceptionInfo *) NULL);
698 assert(exception->signature == MagickCoreSignature);
699 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
700 if (status == MagickFalse)
702 if (IsRGBColorspace(image->colorspace) == MagickFalse)
703 (void) TransformImageColorspace(image,RGBColorspace,exception);
707 (void) ResetMagickMemory(header,' ',MagickPathExtent);
708 length=CopyMagickString(header,"#?RGBE\n",MagickPathExtent);
709 (void) WriteBlob(image,length,(unsigned char *) header);
710 property=GetImageProperty(image,"comment",exception);
711 if ((property != (const char *) NULL) &&
712 (strchr(property,'\n') == (char *) NULL))
714 count=FormatLocaleString(header,MagickPathExtent,"#%s\n",property);
715 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
717 property=GetImageProperty(image,"hdr:exposure",exception);
718 if (property != (const char *) NULL)
720 count=FormatLocaleString(header,MagickPathExtent,"EXPOSURE=%g\n",
721 strtod(property,(char **) NULL));
722 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
724 if (image->gamma != 0.0)
726 count=FormatLocaleString(header,MagickPathExtent,"GAMMA=%g\n",
728 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
730 count=FormatLocaleString(header,MagickPathExtent,
731 "PRIMARIES=%g %g %g %g %g %g %g %g\n",
732 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
733 image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
734 image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
735 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
736 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
737 length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MagickPathExtent);
738 (void) WriteBlob(image,length,(unsigned char *) header);
739 count=FormatLocaleString(header,MagickPathExtent,"-Y %.20g +X %.20g\n",
740 (double) image->rows,(double) image->columns);
741 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
745 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+128,4*
747 if (pixels == (unsigned char *) NULL)
748 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
749 (void) ResetMagickMemory(pixels,0,4*(image->columns+128)*sizeof(*pixels));
750 for (y=0; y < (ssize_t) image->rows; y++)
752 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
753 if (p == (const Quantum *) NULL)
755 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
759 pixel[2]=(unsigned char) (image->columns >> 8);
760 pixel[3]=(unsigned char) (image->columns & 0xff);
761 count=WriteBlob(image,4*sizeof(*pixel),pixel);
762 if (count != (ssize_t) (4*sizeof(*pixel)))
766 for (x=0; x < (ssize_t) image->columns; x++)
775 gamma=QuantumScale*GetPixelRed(image,p);
776 if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
777 gamma=QuantumScale*GetPixelGreen(image,p);
778 if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
779 gamma=QuantumScale*GetPixelBlue(image,p);
780 if (gamma > MagickEpsilon)
785 gamma=frexp(gamma,&exponent)*256.0/gamma;
786 pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
787 pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
788 pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
789 pixel[3]=(unsigned char) (exponent+128);
791 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
794 pixels[x+image->columns]=pixel[1];
795 pixels[x+2*image->columns]=pixel[2];
796 pixels[x+3*image->columns]=pixel[3];
800 pixels[i++]=pixel[0];
801 pixels[i++]=pixel[1];
802 pixels[i++]=pixel[2];
803 pixels[i++]=pixel[3];
805 p+=GetPixelChannels(image);
807 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
809 for (i=0; i < 4; i++)
810 length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
814 count=WriteBlob(image,4*image->columns*sizeof(*pixels),pixels);
815 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
818 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
820 if (status == MagickFalse)
823 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
824 (void) CloseBlob(image);