]> granicus.if.org Git - imagemagick/blob - coders/hdr.c
(no commit message)
[imagemagick] / coders / hdr.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            H   H  DDDD   RRRR                               %
7 %                            H   H  D   D  R   R                              %
8 %                            HHHHH  D   D  RRRR                               %
9 %                            H   H  D   D  R R                                %
10 %                            H   H  DDDD   R  R                               %
11 %                                                                             %
12 %                                                                             %
13 %                   Read/Write Radiance RGBE Image Format                     %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
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/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"
62 \f
63 /*
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %                                                                             %
66 %                                                                             %
67 %                                                                             %
68 %   I s H D R                                                                 %
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %
74 %  IsHDR() returns MagickTrue if the image format type, identified by the
75 %  magick string, is Radiance RGBE image format.
76 %
77 %  The format of the IsHDR method is:
78 %
79 %      MagickBooleanType IsHDR(const unsigned char *magick,
80 %        const size_t length)
81 %
82 %  A description of each parameter follows:
83 %
84 %    o magick: compare image format pattern against these bytes.
85 %
86 %    o length: Specifies the length of the magick string.
87 %
88 */
89 static MagickBooleanType IsHDR(const unsigned char *magick,
90   const size_t length)
91 {
92   if (length < 10)
93     return(MagickFalse);
94   if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
95     return(MagickTrue);
96   return(MagickFalse);
97 }
98 \f
99 /*
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 %                                                                             %
102 %                                                                             %
103 %                                                                             %
104 %   R e a d H D R I m a g e                                                   %
105 %                                                                             %
106 %                                                                             %
107 %                                                                             %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %
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.
113 %
114 %  The format of the ReadHDRImage method is:
115 %
116 %      Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
117 %
118 %  A description of each parameter follows:
119 %
120 %    o image_info: the image info.
121 %
122 %    o exception: return any errors or warnings in this structure.
123 %
124 */
125 static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
126 {
127   char
128     format[MaxTextExtent],
129     keyword[MaxTextExtent],
130     tag[MaxTextExtent],
131     value[MaxTextExtent];
132
133   double
134     gamma;
135
136   Image
137     *image;
138
139   int
140     c;
141
142   MagickBooleanType
143     status,
144     value_expected;
145
146   register PixelPacket
147     *q;
148
149   register unsigned char
150     *p;
151
152   register ssize_t
153     i,
154     x;
155
156   ssize_t
157     count,
158     y;
159
160   unsigned char
161     *end,
162     pixel[4],
163     *pixels;
164
165   /*
166     Open image file.
167   */
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)
178     {
179       image=DestroyImageList(image);
180       return((Image *) NULL);
181     }
182   /*
183     Decode image header.
184   */
185   image->columns=0;
186   image->rows=0;
187   *format='\0';
188   c=ReadBlobByte(image);
189   if (c == EOF)
190     {
191       image=DestroyImage(image);
192       return((Image *) NULL);
193     }
194   while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
195   {
196     if (isalnum(c) == MagickFalse)
197       c=ReadBlobByte(image);
198     else
199       {
200         register char
201           *p;
202
203         /*
204           Determine a keyword and its value.
205         */
206         p=keyword;
207         do
208         {
209           if ((size_t) (p-keyword) < (MaxTextExtent-1))
210             *p++=c;
211           c=ReadBlobByte(image);
212         } while (isalnum(c) || (c == '_'));
213         *p='\0';
214         value_expected=MagickFalse;
215         while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
216         {
217           if (c == '=')
218             value_expected=MagickTrue;
219           c=ReadBlobByte(image);
220         }
221         if (LocaleCompare(keyword,"Y") == 0)
222           value_expected=MagickTrue;
223         if (value_expected == MagickFalse)
224           continue;
225         p=value;
226         while ((c != '\n') && (c != '\0'))
227         {
228           if ((size_t) (p-value) < (MaxTextExtent-1))
229             *p++=c;
230           c=ReadBlobByte(image);
231         }
232         *p='\0';
233         /*
234           Assign a value to the specified keyword.
235         */
236         switch (*keyword)
237         {
238           case 'F':
239           case 'f':
240           {
241             if (LocaleCompare(keyword,"format") == 0)
242               {
243                 (void) CopyMagickString(format,value,MaxTextExtent);
244                 break;
245               }
246             (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
247             (void) SetImageProperty(image,tag,value);
248             break;
249           }
250           case 'G':
251           case 'g':
252           {
253             if (LocaleCompare(keyword,"gamma") == 0)
254               {
255                 image->gamma=StringToDouble(value);
256                 break;
257               }
258             (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
259             (void) SetImageProperty(image,tag,value);
260             break;
261           }
262           case 'P':
263           case 'p':
264           {
265             if (LocaleCompare(keyword,"primaries") == 0)
266               {
267                 float
268                   chromaticity[6],
269                   white_point[2];
270
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],
274                   &white_point[1]);
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];
283                 break;
284               }
285             (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
286             (void) SetImageProperty(image,tag,value);
287             break;
288           }
289           case 'Y':
290           case 'y':
291           {
292             if (strcmp(keyword,"Y") == 0)
293               {
294                 int
295                   height,
296                   width;
297
298                 (void) sscanf(value,"%d +X %d",&height,&width);
299                 image->columns=(size_t) width;
300                 image->rows=(size_t) height;
301                 break;
302               }
303             (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
304             (void) SetImageProperty(image,tag,value);
305             break;
306           }
307           default:
308           {
309             (void) FormatMagickString(tag,MaxTextExtent,"hdr:%s",keyword);
310             (void) SetImageProperty(image,tag,value);
311             break;
312           }
313         }
314       }
315     if ((image->columns == 0) && (image->rows == 0))
316       while (isspace((int) ((unsigned char) c)) != 0)
317         c=ReadBlobByte(image);
318   }
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)
324     {
325       (void) CloseBlob(image);
326       return(GetFirstImageInList(image));
327     }
328   /*
329     Read RGBE (red+green+blue+exponent) pixels.
330   */
331   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,
332     4*sizeof(*pixels));
333   if (pixels == (unsigned char *) NULL)
334     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
335   for (y=0; y < (ssize_t) image->rows; y++)
336   {
337     count=ReadBlob(image,4*sizeof(*pixel),pixel);
338     if (count != 4)
339       break;
340     if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
341       break;
342     p=pixels;
343     for (i=0; i < 4; i++)
344     {
345       end=&pixels[(i+1)*image->columns];
346       while (p < end)
347       {
348         count=ReadBlob(image,2*sizeof(*pixel),pixel);
349         if (count < 1)
350           break;
351         if (pixel[0] > 128)
352           {
353             count=(ssize_t) pixel[0]-128;
354             if ((count == 0) || (count > (ssize_t) (end-p)))
355               break;
356             while (count-- > 0)
357               *p++=pixel[1];
358           }
359         else
360           {
361             count=(ssize_t) pixel[0];
362             if ((count == 0) || (count > (ssize_t) (end-p)))
363               break;
364             *p++=pixel[1];
365             if (--count > 0)
366               {
367                 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
368                 if (count < 1)
369                   break;
370                 p+=count;
371               }
372           }
373       }
374     }
375     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
376     if (q == (PixelPacket *) NULL)
377       break;
378     for (x=0; x < (ssize_t) image->columns; x++)
379     {
380       pixel[0]=pixels[x];
381       pixel[1]=pixels[x+image->columns];
382       pixel[2]=pixels[x+2*image->columns];
383       pixel[3]=pixels[x+3*image->columns];
384       q->red=0;
385       q->green=0;
386       q->blue=0;
387       if (pixel[3] != 0)
388         {
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]);
393         }
394       q++;
395     }
396     if (SyncAuthenticPixels(image,exception) == MagickFalse)
397       break;
398     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
399       image->rows);
400     if (status == MagickFalse)
401       break;
402   }
403   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
404   if (EOFBlob(image) != MagickFalse)
405     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
406       image->filename);
407   (void) CloseBlob(image);
408   return(GetFirstImageInList(image));
409 }
410 \f
411 /*
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 %                                                                             %
414 %                                                                             %
415 %                                                                             %
416 %   R e g i s t e r H D R I m a g e                                           %
417 %                                                                             %
418 %                                                                             %
419 %                                                                             %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 %
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.
427 %
428 %  The format of the RegisterHDRImage method is:
429 %
430 %      size_t RegisterHDRImage(void)
431 %
432 */
433 ModuleExport size_t RegisterHDRImage(void)
434 {
435   MagickInfo
436     *entry;
437
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);
445 }
446 \f
447 /*
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
449 %                                                                             %
450 %                                                                             %
451 %                                                                             %
452 %   U n r e g i s t e r H D R I m a g e                                       %
453 %                                                                             %
454 %                                                                             %
455 %                                                                             %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 %
458 %  UnregisterHDRImage() removes format registrations made by the
459 %  HDR module from the list of supported formats.
460 %
461 %  The format of the UnregisterHDRImage method is:
462 %
463 %      UnregisterHDRImage(void)
464 %
465 */
466 ModuleExport void UnregisterHDRImage(void)
467 {
468   (void) UnregisterMagickInfo("HDR");
469 }