]> granicus.if.org Git - imagemagick/blob - coders/hdr.c
c8175c759dbbed81e4f268add2ca0016ae471b1b
[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-2012 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 "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"
64 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteHDRImage(const ImageInfo *,Image *,ExceptionInfo *);
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   I s H D R                                                                 %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  IsHDR() returns MagickTrue if the image format type, identified by the
83 %  magick string, is Radiance RGBE image format.
84 %
85 %  The format of the IsHDR method is:
86 %
87 %      MagickBooleanType IsHDR(const unsigned char *magick,
88 %        const size_t length)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o magick: compare image format pattern against these bytes.
93 %
94 %    o length: Specifies the length of the magick string.
95 %
96 */
97 static MagickBooleanType IsHDR(const unsigned char *magick,
98   const size_t length)
99 {
100   if (length < 10)
101     return(MagickFalse);
102   if (LocaleNCompare((const char *) magick,"#?RADIANCE",10) == 0)
103     return(MagickTrue);
104   if (LocaleNCompare((const char *) magick,"#?RGBE",6) == 0)
105     return(MagickTrue);
106   return(MagickFalse);
107 }
108 \f
109 /*
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %   R e a d H D R I m a g e                                                   %
115 %                                                                             %
116 %                                                                             %
117 %                                                                             %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %
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.
123 %
124 %  The format of the ReadHDRImage method is:
125 %
126 %      Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
127 %
128 %  A description of each parameter follows:
129 %
130 %    o image_info: the image info.
131 %
132 %    o exception: return any errors or warnings in this structure.
133 %
134 */
135 static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
136 {
137   char
138     format[MaxTextExtent],
139     keyword[MaxTextExtent],
140     tag[MaxTextExtent],
141     value[MaxTextExtent];
142
143   double
144     gamma;
145
146   Image
147     *image;
148
149   int
150     c;
151
152   MagickBooleanType
153     status,
154     value_expected;
155
156   register Quantum
157     *q;
158
159   register ssize_t
160     i,
161     x;
162
163   register unsigned char
164     *p;
165
166   ssize_t
167     count,
168     y;
169
170   unsigned char
171     *end,
172     pixel[4],
173     *pixels;
174
175   /*
176     Open image file.
177   */
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)
188     {
189       image=DestroyImageList(image);
190       return((Image *) NULL);
191     }
192   /*
193     Decode image header.
194   */
195   image->columns=0;
196   image->rows=0;
197   *format='\0';
198   c=ReadBlobByte(image);
199   if (c == EOF)
200     {
201       image=DestroyImage(image);
202       return((Image *) NULL);
203     }
204   while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
205   {
206     if (c == (int) '#')
207       {
208         char
209           *comment;
210
211         register char
212           *p;
213
214         size_t
215           length;
216
217         /*
218           Read comment-- any text between # and end-of-line.
219         */
220         length=MaxTextExtent;
221         comment=AcquireString((char *) NULL);
222         for (p=comment; comment != (char *) NULL; p++)
223         {
224           c=ReadBlobByte(image);
225           if ((c == EOF) || (c == (int) '\n'))
226             break;
227           if ((size_t) (p-comment+1) >= length)
228             {
229               *p='\0';
230               length<<=1;
231               comment=(char *) ResizeQuantumMemory(comment,length+
232                 MaxTextExtent,sizeof(*comment));
233               if (comment == (char *) NULL)
234                 break;
235               p=comment+strlen(comment);
236             }
237           *p=(char) c;
238         }
239         if (comment == (char *) NULL)
240           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
241         *p='\0';
242         (void) SetImageProperty(image,"comment",comment,exception);
243         comment=DestroyString(comment);
244         c=ReadBlobByte(image);
245       }
246     else
247       if (isalnum(c) == MagickFalse)
248         c=ReadBlobByte(image);
249       else
250         {
251           register char
252             *p;
253
254           /*
255             Determine a keyword and its value.
256           */
257           p=keyword;
258           do
259           {
260             if ((size_t) (p-keyword) < (MaxTextExtent-1))
261               *p++=c;
262             c=ReadBlobByte(image);
263           } while (isalnum(c) || (c == '_'));
264           *p='\0';
265           value_expected=MagickFalse;
266           while ((isspace((int) ((unsigned char) c)) != 0) || (c == '='))
267           {
268             if (c == '=')
269               value_expected=MagickTrue;
270             c=ReadBlobByte(image);
271           }
272           if (LocaleCompare(keyword,"Y") == 0)
273             value_expected=MagickTrue;
274           if (value_expected == MagickFalse)
275             continue;
276           p=value;
277           while ((c != '\n') && (c != '\0'))
278           {
279             if ((size_t) (p-value) < (MaxTextExtent-1))
280               *p++=c;
281             c=ReadBlobByte(image);
282           }
283           *p='\0';
284           /*
285             Assign a value to the specified keyword.
286           */
287           switch (*keyword)
288           {
289             case 'F':
290             case 'f':
291             {
292               if (LocaleCompare(keyword,"format") == 0)
293                 {
294                   (void) CopyMagickString(format,value,MaxTextExtent);
295                   break;
296                 }
297               (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
298               (void) SetImageProperty(image,tag,value,exception);
299               break;
300             }
301             case 'G':
302             case 'g':
303             {
304               if (LocaleCompare(keyword,"gamma") == 0)
305                 {
306                   image->gamma=StringToDouble(value,(char **) NULL);
307                   break;
308                 }
309               (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
310               (void) SetImageProperty(image,tag,value,exception);
311               break;
312             }
313             case 'P':
314             case 'p':
315             {
316               if (LocaleCompare(keyword,"primaries") == 0)
317                 {
318                   float
319                     chromaticity[6],
320                     white_point[2];
321
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];
334                   break;
335                 }
336               (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
337               (void) SetImageProperty(image,tag,value,exception);
338               break;
339             }
340             case 'Y':
341             case 'y':
342             {
343               if (strcmp(keyword,"Y") == 0)
344                 {
345                   int
346                     height,
347                     width;
348
349                   (void) sscanf(value,"%d +X %d",&height,&width);
350                   image->columns=(size_t) width;
351                   image->rows=(size_t) height;
352                   break;
353                 }
354               (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
355               (void) SetImageProperty(image,tag,value,exception);
356               break;
357             }
358             default:
359             {
360               (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
361               (void) SetImageProperty(image,tag,value,exception);
362               break;
363             }
364           }
365         }
366     if ((image->columns == 0) && (image->rows == 0))
367       while (isspace((int) ((unsigned char) c)) != 0)
368         c=ReadBlobByte(image);
369   }
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   (void) SetImageColorspace(image,RGBColorspace,exception);
376   if (LocaleCompare(format,"32-bit_rle_xyze") == 0)
377     (void) SetImageColorspace(image,XYZColorspace,exception);
378   image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
379     NoCompression : RLECompression;
380   if (image_info->ping != MagickFalse)
381     {
382       (void) CloseBlob(image);
383       return(GetFirstImageInList(image));
384     }
385   /*
386     Read RGBE (red+green+blue+exponent) pixels.
387   */
388   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
389     sizeof(*pixels));
390   if (pixels == (unsigned char *) NULL)
391     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
392   for (y=0; y < (ssize_t) image->rows; y++)
393   {
394     if (image->compression != RLECompression)
395       {
396         count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
397         if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
398           break;
399       }
400     else
401       {
402         count=ReadBlob(image,4*sizeof(*pixel),pixel);
403         if (count != 4)
404           break;
405         if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
406           {
407             (void) memcpy(pixels,pixel,4*sizeof(*pixel));
408             count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
409             image->compression=NoCompression;
410           }
411         else
412           {
413             p=pixels;
414             for (i=0; i < 4; i++)
415             {
416               end=&pixels[(i+1)*image->columns];
417               while (p < end)
418               {
419                 count=ReadBlob(image,2*sizeof(*pixel),pixel);
420                 if (count < 1)
421                   break;
422                 if (pixel[0] > 128)
423                   {
424                     count=(ssize_t) pixel[0]-128;
425                     if ((count == 0) || (count > (ssize_t) (end-p)))
426                       break;
427                     while (count-- > 0)
428                       *p++=pixel[1];
429                   }
430                 else
431                   {
432                     count=(ssize_t) pixel[0];
433                     if ((count == 0) || (count > (ssize_t) (end-p)))
434                       break;
435                     *p++=pixel[1];
436                     if (--count > 0)
437                       {
438                         count=ReadBlob(image,(size_t) count*sizeof(*p),p);
439                         if (count < 1)
440                           break;
441                         p+=count;
442                       }
443                   }
444               }
445             }
446           }
447       }
448     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
449     if (q == (Quantum *) NULL)
450       break;
451     i=0;
452     for (x=0; x < (ssize_t) image->columns; x++)
453     {
454       if (image->compression == RLECompression)
455         {
456           pixel[0]=pixels[x];
457           pixel[1]=pixels[x+image->columns];
458           pixel[2]=pixels[x+2*image->columns];
459           pixel[3]=pixels[x+3*image->columns];
460         }
461       else
462         {
463           pixel[0]=pixels[i++];
464           pixel[1]=pixels[i++];
465           pixel[2]=pixels[i++];
466           pixel[3]=pixels[i++];
467         }
468       SetPixelRed(image,0,q);
469       SetPixelGreen(image,0,q);
470       SetPixelBlue(image,0,q);
471       if (pixel[3] != 0)
472         {
473           gamma=pow(2.0,pixel[3]-(128.0+8.0));
474           SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q);
475           SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q);
476           SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q);
477         }
478       q+=GetPixelChannels(image);
479     }
480     if (SyncAuthenticPixels(image,exception) == MagickFalse)
481       break;
482     status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
483       image->rows);
484     if (status == MagickFalse)
485       break;
486   }
487   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
488   if (EOFBlob(image) != MagickFalse)
489     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
490       image->filename);
491   (void) CloseBlob(image);
492   return(GetFirstImageInList(image));
493 }
494 \f
495 /*
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 %                                                                             %
498 %                                                                             %
499 %                                                                             %
500 %   R e g i s t e r H D R I m a g e                                           %
501 %                                                                             %
502 %                                                                             %
503 %                                                                             %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 %
506 %  RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
507 %  list of supported formats.  The attributes include the image format tag, a
508 %  method to read and/or write the format, whether the format supports the
509 %  saving of more than one frame to the same file or blob, whether the format
510 %  supports native in-memory I/O, and a brief description of the format.
511 %
512 %  The format of the RegisterHDRImage method is:
513 %
514 %      size_t RegisterHDRImage(void)
515 %
516 */
517 ModuleExport size_t RegisterHDRImage(void)
518 {
519   MagickInfo
520     *entry;
521
522   entry=SetMagickInfo("HDR");
523   entry->decoder=(DecodeImageHandler *) ReadHDRImage;
524   entry->encoder=(EncodeImageHandler *) WriteHDRImage;
525   entry->description=ConstantString("Radiance RGBE image format");
526   entry->module=ConstantString("HDR");
527   entry->magick=(IsImageFormatHandler *) IsHDR;
528   (void) RegisterMagickInfo(entry);
529   return(MagickImageCoderSignature);
530 }
531 \f
532 /*
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 %                                                                             %
535 %                                                                             %
536 %                                                                             %
537 %   U n r e g i s t e r H D R I m a g e                                       %
538 %                                                                             %
539 %                                                                             %
540 %                                                                             %
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 %
543 %  UnregisterHDRImage() removes format registrations made by the
544 %  HDR module from the list of supported formats.
545 %
546 %  The format of the UnregisterHDRImage method is:
547 %
548 %      UnregisterHDRImage(void)
549 %
550 */
551 ModuleExport void UnregisterHDRImage(void)
552 {
553   (void) UnregisterMagickInfo("HDR");
554 }
555 \f
556 /*
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 %                                                                             %
559 %                                                                             %
560 %                                                                             %
561 %   W r i t e H D R I m a g e                                                 %
562 %                                                                             %
563 %                                                                             %
564 %                                                                             %
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 %
567 %  WriteHDRImage() writes an image in the Radience RGBE image format.
568 %
569 %  The format of the WriteHDRImage method is:
570 %
571 %      MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
572 %        Image *image,ExceptionInfo *exception)
573 %
574 %  A description of each parameter follows.
575 %
576 %    o image_info: the image info.
577 %
578 %    o image:  The image.
579 %
580 */
581
582 static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
583 {
584 #define MinimumRunlength 4
585
586   register size_t
587     p,
588     q;
589
590   size_t
591     runlength;
592
593   ssize_t
594     count,
595     previous_count;
596
597   unsigned char
598     pixel[2];
599
600   for (p=0; p < image->columns; )
601   {
602     q=p;
603     runlength=0;
604     previous_count=0;
605     while ((runlength < MinimumRunlength) && (q < image->columns))
606     {
607       q+=runlength;
608       previous_count=(ssize_t) runlength;
609       runlength=1;
610       while ((pixels[q] == pixels[q+runlength]) &&
611              ((q+runlength) < image->columns) && (runlength < 127))
612        runlength++;
613     }
614     if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
615       {
616         pixel[0]=(unsigned char) (128+previous_count);
617         pixel[1]=pixels[p];
618         if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
619           break;
620         p=q;
621       }
622     while (p < q)
623     {
624       count=(ssize_t) (q-p);
625       if (count > 128)
626         count=128;
627       pixel[0]=(unsigned char) count;
628       if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
629         break;
630       if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
631         break;
632       p+=count;
633     }
634     if (runlength >= MinimumRunlength)
635       {
636         pixel[0]=(unsigned char) (128+runlength);
637         pixel[1]=pixels[q];
638         if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
639           break;
640         p+=runlength;
641       }
642   }
643   return(p);
644 }
645
646 static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image,
647   ExceptionInfo *exception)
648 {
649   char
650     header[MaxTextExtent];
651
652   const char
653     *property;
654
655   MagickBooleanType
656     status;
657
658   register const Quantum
659     *p;
660
661   register ssize_t
662     i,
663     x;
664
665   size_t
666     length;
667
668   ssize_t
669     count,
670     y;
671
672   unsigned char
673     pixel[4],
674     *pixels;
675
676   /*
677     Open output image file.
678   */
679   assert(image_info != (const ImageInfo *) NULL);
680   assert(image_info->signature == MagickSignature);
681   assert(image != (Image *) NULL);
682   assert(image->signature == MagickSignature);
683   if (image->debug != MagickFalse)
684     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
685   assert(exception != (ExceptionInfo *) NULL);
686   assert(exception->signature == MagickSignature);
687   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
688   if (status == MagickFalse)
689     return(status);
690   if (IsRGBColorspace(image->colorspace) == MagickFalse)
691     (void) TransformImageColorspace(image,RGBColorspace,exception);
692   /*
693     Write header.
694   */
695   (void) ResetMagickMemory(header,' ',MaxTextExtent);
696   length=CopyMagickString(header,"#?RGBE\n",MaxTextExtent);
697   (void) WriteBlob(image,length,(unsigned char *) header);
698   property=GetImageProperty(image,"comment",exception);
699   if ((property != (const char *) NULL) &&
700       (strchr(property,'\n') == (char *) NULL))
701     {
702       count=FormatLocaleString(header,MaxTextExtent,"#%s\n",property);
703       (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
704     }
705   property=GetImageProperty(image,"hdr:exposure",exception);
706   if (property != (const char *) NULL)
707     {
708       count=FormatLocaleString(header,MaxTextExtent,"EXPOSURE=%g\n",
709         atof(property));
710       (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
711     }
712   if (image->gamma != 0.0)
713     {
714       count=FormatLocaleString(header,MaxTextExtent,"GAMMA=%g\n",image->gamma);
715       (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
716     }
717   count=FormatLocaleString(header,MaxTextExtent,
718     "PRIMARIES=%g %g %g %g %g %g %g %g\n",
719     image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
720     image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
721     image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
722     image->chromaticity.white_point.x,image->chromaticity.white_point.y);
723   (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
724   length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MaxTextExtent);
725   (void) WriteBlob(image,length,(unsigned char *) header);
726   count=FormatLocaleString(header,MaxTextExtent,"-Y %.20g +X %.20g\n",
727     (double) image->rows,(double) image->columns);
728   (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
729   /*
730     Write HDR pixels.
731   */
732   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
733     sizeof(*pixels));
734   if (pixels == (unsigned char *) NULL)
735     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
736   for (y=0; y < (ssize_t) image->rows; y++)
737   {
738     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
739     if (p == (const Quantum *) NULL)
740       break;
741     if ((image->columns >= 8) && (image->columns <= 0x7ffff))
742       {
743         pixel[0]=2;
744         pixel[1]=2;
745         pixel[2]=(unsigned char) (image->columns >> 8);
746         pixel[3]=(unsigned char) (image->columns & 0xff);
747         count=WriteBlob(image,4*sizeof(*pixel),pixel);
748         if (count != (ssize_t) (4*sizeof(*pixel)))
749           break;
750       }
751     i=0;
752     for (x=0; x < (ssize_t) image->columns; x++)
753     {
754       double
755         gamma;
756
757       pixel[0]=0;
758       pixel[1]=0;
759       pixel[2]=0;
760       pixel[3]=0;
761       gamma=QuantumScale*GetPixelRed(image,p);
762       if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
763         gamma=QuantumScale*GetPixelGreen(image,p);
764       if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
765         gamma=QuantumScale*GetPixelBlue(image,p);
766       if (gamma > MagickEpsilon)
767         {
768           int
769             exponent;
770
771           gamma=frexp(gamma,&exponent)*256.0/gamma;
772           pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
773           pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
774           pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
775           pixel[3]=(unsigned char) (exponent+128);
776         }
777       if ((image->columns >= 8) && (image->columns <= 0x7ffff))
778         {
779           pixels[x]=pixel[0];
780           pixels[x+image->columns]=pixel[1];
781           pixels[x+2*image->columns]=pixel[2];
782           pixels[x+3*image->columns]=pixel[3];
783         }
784       else
785         {
786           pixels[i++]=pixel[0];
787           pixels[i++]=pixel[1];
788           pixels[i++]=pixel[2];
789           pixels[i++]=pixel[3];
790         }
791       p+=GetPixelChannels(image);
792     }
793     if ((image->columns >= 8) && (image->columns <= 0x7ffff))
794       {
795         for (i=0; i < 4; i++)
796           length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
797       }
798     else
799       {
800         count=WriteBlob(image,4*image->columns*sizeof(*pixel),pixel);
801         if (count != (ssize_t) (4*image->columns*sizeof(*pixel)))
802           break;
803       }
804     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
805       image->rows);
806     if (status == MagickFalse)
807       break;
808   }
809   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
810   (void) CloseBlob(image);
811   return(MagickTrue);
812 }