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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/monitor.h"
55 #include "magick/monitor-private.h"
56 #include "magick/property.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/string-private.h"
61 #include "magick/module.h"
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % IsHDR() returns MagickTrue if the image format type, identified by the
75 % magick string, is Radiance RGBE image format.
77 % The format of the IsHDR method is:
79 % MagickBooleanType IsHDR(const unsigned char *magick,
80 % const size_t length)
82 % A description of each parameter follows:
84 % o magick: compare image format pattern against these bytes.
86 % o length: Specifies the length of the magick string.
89 static MagickBooleanType IsHDR(const unsigned char *magick,
94 if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % R e a d H D R I m a g e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % ReadHDRImage() reads the Radiance RGBE image format and returns it. It
111 % allocates the memory necessary for the new Image structure and returns a
112 % pointer to the new image.
114 % The format of the ReadHDRImage method is:
116 % Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
118 % A description of each parameter follows:
120 % o image_info: the image info.
122 % o exception: return any errors or warnings in this structure.
125 static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
128 format[MaxTextExtent],
129 keyword[MaxTextExtent],
131 value[MaxTextExtent];
149 register unsigned char
168 assert(image_info != (const ImageInfo *) NULL);
169 assert(image_info->signature == MagickSignature);
170 if (image_info->debug != MagickFalse)
171 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
172 image_info->filename);
173 assert(exception != (ExceptionInfo *) NULL);
174 assert(exception->signature == MagickSignature);
175 image=AcquireImage(image_info);
176 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
177 if (status == MagickFalse)
179 image=DestroyImageList(image);
180 return((Image *) NULL);
188 c=ReadBlobByte(image);
191 image=DestroyImage(image);
192 return((Image *) NULL);
194 while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
196 if (isalnum(c) == MagickFalse)
197 c=ReadBlobByte(image);
204 Determine a keyword and its value.
209 if ((size_t) (p-keyword) < (MaxTextExtent-1))
211 c=ReadBlobByte(image);
212 } while (isalnum(c) || (c == '_'));
214 value_expected=MagickFalse;
215 while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
218 value_expected=MagickTrue;
219 c=ReadBlobByte(image);
221 if (LocaleCompare(keyword,"Y") == 0)
222 value_expected=MagickTrue;
223 if (value_expected == MagickFalse)
226 while ((c != '\n') && (c != '\0'))
228 if ((size_t) (p-value) < (MaxTextExtent-1))
230 c=ReadBlobByte(image);
234 Assign a value to the specified keyword.
241 if (LocaleCompare(keyword,"format") == 0)
243 (void) CopyMagickString(format,value,MaxTextExtent);
246 (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
247 (void) SetImageProperty(image,tag,value);
253 if (LocaleCompare(keyword,"gamma") == 0)
255 image->gamma=StringToDouble(value);
258 (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
259 (void) SetImageProperty(image,tag,value);
265 if (LocaleCompare(keyword,"primaries") == 0)
271 (void) sscanf(value,"%g %g %g %g %g %g %g %g",&chromaticity[0],
272 &chromaticity[1],&chromaticity[2],&chromaticity[3],
273 &chromaticity[4],&chromaticity[5],&white_point[0],
275 image->chromaticity.red_primary.x=chromaticity[0];
276 image->chromaticity.red_primary.y=chromaticity[1];
277 image->chromaticity.green_primary.x=chromaticity[2];
278 image->chromaticity.green_primary.y=chromaticity[3];
279 image->chromaticity.blue_primary.x=chromaticity[4];
280 image->chromaticity.blue_primary.y=chromaticity[5];
281 image->chromaticity.white_point.x=white_point[0],
282 image->chromaticity.white_point.y=white_point[1];
285 (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
286 (void) SetImageProperty(image,tag,value);
292 if (strcmp(keyword,"Y") == 0)
298 (void) sscanf(value,"%d +X %d",&height,&width);
299 image->columns=(size_t) width;
300 image->rows=(size_t) height;
303 (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
304 (void) SetImageProperty(image,tag,value);
309 (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
310 (void) SetImageProperty(image,tag,value);
315 if ((image->columns == 0) && (image->rows == 0))
316 while (isspace((int) ((unsigned char) c)) != 0)
317 c=ReadBlobByte(image);
319 if (LocaleCompare(format,"32-bit_rle_rgbe") != 0)
320 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
321 if ((image->columns == 0) || (image->rows == 0))
322 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
323 if (image_info->ping != MagickFalse)
325 (void) CloseBlob(image);
326 return(GetFirstImageInList(image));
329 Read RGBE (red+green+blue+exponent) pixels.
331 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
333 if (pixels == (unsigned char *) NULL)
334 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
335 for (y=0; y < (ssize_t) image->rows; y++)
337 count=ReadBlob(image,4*sizeof(*pixel),pixel);
340 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
343 for (i=0; i < 4; i++)
345 end=&pixels[(i+1)*image->columns];
348 count=ReadBlob(image,2*sizeof(*pixel),pixel);
353 count=(ssize_t) pixel[0]-128;
354 if ((count == 0) || (count > (ssize_t) (end-p)))
361 count=(ssize_t) pixel[0];
362 if ((count == 0) || (count > (ssize_t) (end-p)))
367 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
375 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
376 if (q == (PixelPacket *) NULL)
378 for (x=0; x < (ssize_t) image->columns; x++)
381 pixel[1]=pixels[x+image->columns];
382 pixel[2]=pixels[x+2*image->columns];
383 pixel[3]=pixels[x+3*image->columns];
389 gamma=pow(2.0,pixel[3]-(128.0+8.0));
390 q->red=ClampToQuantum(QuantumRange*gamma*pixel[0]);
391 q->green=ClampToQuantum(QuantumRange*gamma*pixel[1]);
392 q->blue=ClampToQuantum(QuantumRange*gamma*pixel[2]);
396 if (SyncAuthenticPixels(image,exception) == MagickFalse)
398 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
400 if (status == MagickFalse)
403 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
404 if (EOFBlob(image) != MagickFalse)
405 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
407 (void) CloseBlob(image);
408 return(GetFirstImageInList(image));
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % R e g i s t e r H D R I m a g e %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
423 % list of supported formats. The attributes include the image format tag, a
424 % method to read and/or write the format, whether the format supports the
425 % saving of more than one frame to the same file or blob, whether the format
426 % supports native in-memory I/O, and a brief description of the format.
428 % The format of the RegisterHDRImage method is:
430 % size_t RegisterHDRImage(void)
433 ModuleExport size_t RegisterHDRImage(void)
438 entry=SetMagickInfo("HDR");
439 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
440 entry->description=ConstantString("Radiance RGBE image format");
441 entry->module=ConstantString("HDR");
442 entry->magick=(IsImageFormatHandler *) IsHDR;
443 (void) RegisterMagickInfo(entry);
444 return(MagickImageCoderSignature);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % U n r e g i s t e r H D R I m a g e %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % UnregisterHDRImage() removes format registrations made by the
459 % HDR module from the list of supported formats.
461 % The format of the UnregisterHDRImage method is:
463 % UnregisterHDRImage(void)
466 ModuleExport void UnregisterHDRImage(void)
468 (void) UnregisterMagickInfo("HDR");