blob: 0b61dbaafd53bf197c404f033f777e793bb1665a [file] [log] [blame]
cristy579bc8f2011-03-06 17:27:05 +00001/*
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% %
cristy03533f22011-03-06 23:30:17 +000013% Read/Write Radiance RGBE Image Format %
cristy579bc8f2011-03-06 17:27:05 +000014% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy579bc8f2011-03-06 17:27:05 +000017% July 1992 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy579bc8f2011-03-06 17:27:05 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000047#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristy579bc8f2011-03-06 17:27:05 +000064
65/*
cristy84c3d052011-03-07 19:22:02 +000066 Forward declarations.
67*/
68static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000069 WriteHDRImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy84c3d052011-03-07 19:22:02 +000070
71/*
cristy579bc8f2011-03-06 17:27:05 +000072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
cristy03533f22011-03-06 23:30:17 +000076% 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*/
97static 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);
cristy84c3d052011-03-07 19:22:02 +0000104 if (LocaleNCompare((const char *) magick,"#?RGBE",6) == 0)
105 return(MagickTrue);
cristy03533f22011-03-06 23:30:17 +0000106 return(MagickFalse);
107}
108
109/*
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111% %
112% %
113% %
cristy579bc8f2011-03-06 17:27:05 +0000114% R e a d H D R I m a g e %
115% %
116% %
117% %
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119%
cristy03533f22011-03-06 23:30:17 +0000120% ReadHDRImage() reads the Radiance RGBE image format and returns it. It
cristy21384232011-03-06 17:31:08 +0000121% allocates the memory necessary for the new Image structure and returns a
122% pointer to the new image.
cristy579bc8f2011-03-06 17:27:05 +0000123%
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*/
135static Image *ReadHDRImage(const ImageInfo *image_info,ExceptionInfo *exception)
136{
cristy03533f22011-03-06 23:30:17 +0000137 char
cristy151b66d2015-04-15 10:50:31 +0000138 format[MagickPathExtent],
139 keyword[MagickPathExtent],
140 tag[MagickPathExtent],
141 value[MagickPathExtent];
cristy03533f22011-03-06 23:30:17 +0000142
143 double
144 gamma;
145
cristy579bc8f2011-03-06 17:27:05 +0000146 Image
147 *image;
148
cristy03533f22011-03-06 23:30:17 +0000149 int
150 c;
cristy579bc8f2011-03-06 17:27:05 +0000151
cristy03533f22011-03-06 23:30:17 +0000152 MagickBooleanType
153 status,
154 value_expected;
cristy579bc8f2011-03-06 17:27:05 +0000155
cristy4c08aed2011-07-01 19:47:50 +0000156 register Quantum
cristy579bc8f2011-03-06 17:27:05 +0000157 *q;
158
cristy03533f22011-03-06 23:30:17 +0000159 register ssize_t
160 i,
161 x;
cristy579bc8f2011-03-06 17:27:05 +0000162
cristy19c9c3c2011-08-25 18:51:30 +0000163 register unsigned char
164 *p;
165
cristy579bc8f2011-03-06 17:27:05 +0000166 ssize_t
167 count,
168 y;
169
170 unsigned char
cristy03533f22011-03-06 23:30:17 +0000171 *end,
172 pixel[4],
cristy579bc8f2011-03-06 17:27:05 +0000173 *pixels;
174
175 /*
176 Open image file.
177 */
178 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000179 assert(image_info->signature == MagickCoreSignature);
cristy579bc8f2011-03-06 17:27:05 +0000180 if (image_info->debug != MagickFalse)
181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
182 image_info->filename);
183 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000184 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000185 image=AcquireImage(image_info,exception);
cristy579bc8f2011-03-06 17:27:05 +0000186 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
187 if (status == MagickFalse)
188 {
189 image=DestroyImageList(image);
190 return((Image *) NULL);
191 }
192 /*
cristy03533f22011-03-06 23:30:17 +0000193 Decode image header.
cristy579bc8f2011-03-06 17:27:05 +0000194 */
cristy03533f22011-03-06 23:30:17 +0000195 image->columns=0;
196 image->rows=0;
197 *format='\0';
198 c=ReadBlobByte(image);
199 if (c == EOF)
cristy579bc8f2011-03-06 17:27:05 +0000200 {
cristy03533f22011-03-06 23:30:17 +0000201 image=DestroyImage(image);
202 return((Image *) NULL);
cristy579bc8f2011-03-06 17:27:05 +0000203 }
cristy03533f22011-03-06 23:30:17 +0000204 while (isgraph(c) && (image->columns == 0) && (image->rows == 0))
205 {
cristy97bd7c32011-03-08 02:52:04 +0000206 if (c == (int) '#')
cristy579bc8f2011-03-06 17:27:05 +0000207 {
cristy97bd7c32011-03-08 02:52:04 +0000208 char
209 *comment;
210
cristy03533f22011-03-06 23:30:17 +0000211 register char
212 *p;
213
cristy97bd7c32011-03-08 02:52:04 +0000214 size_t
215 length;
cristy03533f22011-03-06 23:30:17 +0000216
cristy97bd7c32011-03-08 02:52:04 +0000217 /*
218 Read comment-- any text between # and end-of-line.
219 */
cristy151b66d2015-04-15 10:50:31 +0000220 length=MagickPathExtent;
cristy97bd7c32011-03-08 02:52:04 +0000221 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+
cristy151b66d2015-04-15 10:50:31 +0000232 MagickPathExtent,sizeof(*comment));
cristy97bd7c32011-03-08 02:52:04 +0000233 if (comment == (char *) NULL)
cristy03533f22011-03-06 23:30:17 +0000234 break;
cristy97bd7c32011-03-08 02:52:04 +0000235 p=comment+strlen(comment);
236 }
237 *p=(char) c;
cristy03533f22011-03-06 23:30:17 +0000238 }
cristy97bd7c32011-03-08 02:52:04 +0000239 if (comment == (char *) NULL)
240 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
241 *p='\0';
cristyd15e6592011-10-15 00:13:06 +0000242 (void) SetImageProperty(image,"comment",comment,exception);
cristy97bd7c32011-03-08 02:52:04 +0000243 comment=DestroyString(comment);
244 c=ReadBlobByte(image);
cristy579bc8f2011-03-06 17:27:05 +0000245 }
cristy97bd7c32011-03-08 02:52:04 +0000246 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 {
cristy151b66d2015-04-15 10:50:31 +0000260 if ((size_t) (p-keyword) < (MagickPathExtent-1))
cristy97bd7c32011-03-08 02:52:04 +0000261 *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;
dirk97aa7d72015-01-25 07:27:09 +0000277 while ((c != '\n') && (c != '\0') && (c != EOF))
cristy97bd7c32011-03-08 02:52:04 +0000278 {
cristy151b66d2015-04-15 10:50:31 +0000279 if ((size_t) (p-value) < (MagickPathExtent-1))
cristy97bd7c32011-03-08 02:52:04 +0000280 *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 {
cristy151b66d2015-04-15 10:50:31 +0000294 (void) CopyMagickString(format,value,MagickPathExtent);
cristy97bd7c32011-03-08 02:52:04 +0000295 break;
296 }
cristy151b66d2015-04-15 10:50:31 +0000297 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000298 (void) SetImageProperty(image,tag,value,exception);
cristy97bd7c32011-03-08 02:52:04 +0000299 break;
300 }
301 case 'G':
302 case 'g':
303 {
304 if (LocaleCompare(keyword,"gamma") == 0)
305 {
cristydbdd0e32011-11-04 23:29:40 +0000306 image->gamma=StringToDouble(value,(char **) NULL);
cristy97bd7c32011-03-08 02:52:04 +0000307 break;
308 }
cristy151b66d2015-04-15 10:50:31 +0000309 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000310 (void) SetImageProperty(image,tag,value,exception);
cristy97bd7c32011-03-08 02:52:04 +0000311 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
cristy49c1cb32015-01-25 14:42:21 +0000322 int
323 count;
324
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],
328 &white_point[1]);
329 if (count == 8)
dirk97aa7d72015-01-25 07:27:09 +0000330 {
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];
339 }
cristy97bd7c32011-03-08 02:52:04 +0000340 break;
341 }
cristy151b66d2015-04-15 10:50:31 +0000342 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000343 (void) SetImageProperty(image,tag,value,exception);
cristy97bd7c32011-03-08 02:52:04 +0000344 break;
345 }
346 case 'Y':
347 case 'y':
348 {
cristy587235d2014-12-20 23:29:55 +0000349 char
350 target[] = "Y";
351
352 if (strcmp(keyword,target) == 0)
cristy97bd7c32011-03-08 02:52:04 +0000353 {
354 int
355 height,
356 width;
357
dirk97aa7d72015-01-25 07:27:09 +0000358 if (sscanf(value,"%d +X %d",&height,&width) == 2)
359 {
360 image->columns=(size_t) width;
361 image->rows=(size_t) height;
362 }
cristy97bd7c32011-03-08 02:52:04 +0000363 break;
364 }
cristy151b66d2015-04-15 10:50:31 +0000365 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000366 (void) SetImageProperty(image,tag,value,exception);
cristy97bd7c32011-03-08 02:52:04 +0000367 break;
368 }
369 default:
370 {
cristy151b66d2015-04-15 10:50:31 +0000371 (void) FormatLocaleString(tag,MagickPathExtent,"hdr:%s",keyword);
cristyd15e6592011-10-15 00:13:06 +0000372 (void) SetImageProperty(image,tag,value,exception);
cristy97bd7c32011-03-08 02:52:04 +0000373 break;
374 }
375 }
376 }
cristy03533f22011-03-06 23:30:17 +0000377 if ((image->columns == 0) && (image->rows == 0))
378 while (isspace((int) ((unsigned char) c)) != 0)
379 c=ReadBlobByte(image);
380 }
cristya74b77f2011-04-17 01:59:47 +0000381 if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) &&
382 (LocaleCompare(format,"32-bit_rle_xyze") != 0))
cristy03533f22011-03-06 23:30:17 +0000383 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
384 if ((image->columns == 0) || (image->rows == 0))
385 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
cristy74d59d92012-07-12 23:48:47 +0000386 (void) SetImageColorspace(image,RGBColorspace,exception);
cristya0d69f22011-08-25 19:10:47 +0000387 if (LocaleCompare(format,"32-bit_rle_xyze") == 0)
cristy74d59d92012-07-12 23:48:47 +0000388 (void) SetImageColorspace(image,XYZColorspace,exception);
cristyf0e4a5a2011-04-17 02:15:18 +0000389 image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
390 NoCompression : RLECompression;
cristy03533f22011-03-06 23:30:17 +0000391 if (image_info->ping != MagickFalse)
392 {
393 (void) CloseBlob(image);
394 return(GetFirstImageInList(image));
395 }
cristyacabb842014-12-14 23:36:33 +0000396 status=SetImageExtent(image,image->columns,image->rows,exception);
397 if (status == MagickFalse)
398 return(DestroyImageList(image));
cristy03533f22011-03-06 23:30:17 +0000399 /*
cristya8ff9422011-03-06 23:58:56 +0000400 Read RGBE (red+green+blue+exponent) pixels.
cristy03533f22011-03-06 23:30:17 +0000401 */
cristyb84ca112011-03-30 14:16:18 +0000402 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
403 sizeof(*pixels));
cristy03533f22011-03-06 23:30:17 +0000404 if (pixels == (unsigned char *) NULL)
405 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
406 for (y=0; y < (ssize_t) image->rows; y++)
407 {
cristyf0e4a5a2011-04-17 02:15:18 +0000408 if (image->compression != RLECompression)
cristy03533f22011-03-06 23:30:17 +0000409 {
cristya74b77f2011-04-17 01:59:47 +0000410 count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
411 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
cristy03533f22011-03-06 23:30:17 +0000412 break;
cristy97bd7c32011-03-08 02:52:04 +0000413 }
414 else
415 {
416 count=ReadBlob(image,4*sizeof(*pixel),pixel);
417 if (count != 4)
418 break;
419 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
cristy03533f22011-03-06 23:30:17 +0000420 {
cristyf0e4a5a2011-04-17 02:15:18 +0000421 (void) memcpy(pixels,pixel,4*sizeof(*pixel));
422 count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
423 image->compression=NoCompression;
424 }
425 else
426 {
427 p=pixels;
428 for (i=0; i < 4; i++)
429 {
430 end=&pixels[(i+1)*image->columns];
431 while (p < end)
cristy03533f22011-03-06 23:30:17 +0000432 {
cristyf0e4a5a2011-04-17 02:15:18 +0000433 count=ReadBlob(image,2*sizeof(*pixel),pixel);
434 if (count < 1)
cristy03533f22011-03-06 23:30:17 +0000435 break;
cristyf0e4a5a2011-04-17 02:15:18 +0000436 if (pixel[0] > 128)
cristy97bd7c32011-03-08 02:52:04 +0000437 {
cristyf0e4a5a2011-04-17 02:15:18 +0000438 count=(ssize_t) pixel[0]-128;
439 if ((count == 0) || (count > (ssize_t) (end-p)))
cristy97bd7c32011-03-08 02:52:04 +0000440 break;
cristyf0e4a5a2011-04-17 02:15:18 +0000441 while (count-- > 0)
442 *p++=pixel[1];
443 }
444 else
445 {
446 count=(ssize_t) pixel[0];
447 if ((count == 0) || (count > (ssize_t) (end-p)))
448 break;
449 *p++=pixel[1];
450 if (--count > 0)
451 {
452 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
453 if (count < 1)
454 break;
455 p+=count;
456 }
cristy97bd7c32011-03-08 02:52:04 +0000457 }
cristy03533f22011-03-06 23:30:17 +0000458 }
cristyf0e4a5a2011-04-17 02:15:18 +0000459 }
cristy03533f22011-03-06 23:30:17 +0000460 }
461 }
cristy03533f22011-03-06 23:30:17 +0000462 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000463 if (q == (Quantum *) NULL)
cristy03533f22011-03-06 23:30:17 +0000464 break;
cristy97bd7c32011-03-08 02:52:04 +0000465 i=0;
cristy03533f22011-03-06 23:30:17 +0000466 for (x=0; x < (ssize_t) image->columns; x++)
467 {
cristyf0e4a5a2011-04-17 02:15:18 +0000468 if (image->compression == RLECompression)
cristy97bd7c32011-03-08 02:52:04 +0000469 {
470 pixel[0]=pixels[x];
471 pixel[1]=pixels[x+image->columns];
472 pixel[2]=pixels[x+2*image->columns];
473 pixel[3]=pixels[x+3*image->columns];
474 }
475 else
476 {
477 pixel[0]=pixels[i++];
478 pixel[1]=pixels[i++];
479 pixel[2]=pixels[i++];
480 pixel[3]=pixels[i++];
481 }
cristy4c08aed2011-07-01 19:47:50 +0000482 SetPixelRed(image,0,q);
483 SetPixelGreen(image,0,q);
484 SetPixelBlue(image,0,q);
cristy03533f22011-03-06 23:30:17 +0000485 if (pixel[3] != 0)
486 {
487 gamma=pow(2.0,pixel[3]-(128.0+8.0));
cristy4c08aed2011-07-01 19:47:50 +0000488 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);
cristy03533f22011-03-06 23:30:17 +0000491 }
cristyed231572011-07-14 02:18:59 +0000492 q+=GetPixelChannels(image);
cristy03533f22011-03-06 23:30:17 +0000493 }
494 if (SyncAuthenticPixels(image,exception) == MagickFalse)
495 break;
496 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
497 image->rows);
498 if (status == MagickFalse)
499 break;
500 }
501 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
502 if (EOFBlob(image) != MagickFalse)
503 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
504 image->filename);
cristy579bc8f2011-03-06 17:27:05 +0000505 (void) CloseBlob(image);
506 return(GetFirstImageInList(image));
507}
508
509/*
510%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511% %
512% %
513% %
514% R e g i s t e r H D R I m a g e %
515% %
516% %
517% %
518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519%
cristy03533f22011-03-06 23:30:17 +0000520% RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
cristy21384232011-03-06 17:31:08 +0000521% list of supported formats. The attributes include the image format tag, a
cristy579bc8f2011-03-06 17:27:05 +0000522% 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.
525%
526% The format of the RegisterHDRImage method is:
527%
528% size_t RegisterHDRImage(void)
529%
530*/
531ModuleExport size_t RegisterHDRImage(void)
532{
533 MagickInfo
534 *entry;
535
dirk06b627a2015-04-06 18:59:17 +0000536 entry=AcquireMagickInfo("HDR","HDR","Radiance RGBE image format");
cristy579bc8f2011-03-06 17:27:05 +0000537 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
cristy84c3d052011-03-07 19:22:02 +0000538 entry->encoder=(EncodeImageHandler *) WriteHDRImage;
cristy03533f22011-03-06 23:30:17 +0000539 entry->magick=(IsImageFormatHandler *) IsHDR;
cristy579bc8f2011-03-06 17:27:05 +0000540 (void) RegisterMagickInfo(entry);
541 return(MagickImageCoderSignature);
542}
543
544/*
545%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546% %
547% %
548% %
549% U n r e g i s t e r H D R I m a g e %
550% %
551% %
552% %
553%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554%
555% UnregisterHDRImage() removes format registrations made by the
556% HDR module from the list of supported formats.
557%
558% The format of the UnregisterHDRImage method is:
559%
560% UnregisterHDRImage(void)
561%
562*/
563ModuleExport void UnregisterHDRImage(void)
564{
565 (void) UnregisterMagickInfo("HDR");
566}
cristy84c3d052011-03-07 19:22:02 +0000567
568/*
569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570% %
571% %
572% %
573% W r i t e H D R I m a g e %
574% %
575% %
576% %
577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578%
579% WriteHDRImage() writes an image in the Radience RGBE image format.
580%
581% The format of the WriteHDRImage method is:
582%
583% MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000584% Image *image,ExceptionInfo *exception)
cristy84c3d052011-03-07 19:22:02 +0000585%
586% A description of each parameter follows.
587%
588% o image_info: the image info.
589%
590% o image: The image.
591%
592*/
cristy97bd7c32011-03-08 02:52:04 +0000593
594static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
595{
596#define MinimumRunlength 4
597
598 register size_t
599 p,
600 q;
601
602 size_t
603 runlength;
604
605 ssize_t
606 count,
607 previous_count;
608
609 unsigned char
610 pixel[2];
611
612 for (p=0; p < image->columns; )
613 {
614 q=p;
615 runlength=0;
616 previous_count=0;
617 while ((runlength < MinimumRunlength) && (q < image->columns))
618 {
619 q+=runlength;
620 previous_count=(ssize_t) runlength;
621 runlength=1;
622 while ((pixels[q] == pixels[q+runlength]) &&
623 ((q+runlength) < image->columns) && (runlength < 127))
624 runlength++;
625 }
626 if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
627 {
628 pixel[0]=(unsigned char) (128+previous_count);
629 pixel[1]=pixels[p];
630 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
631 break;
632 p=q;
633 }
634 while (p < q)
635 {
636 count=(ssize_t) (q-p);
637 if (count > 128)
638 count=128;
639 pixel[0]=(unsigned char) count;
640 if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
641 break;
642 if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
643 break;
644 p+=count;
645 }
646 if (runlength >= MinimumRunlength)
647 {
648 pixel[0]=(unsigned char) (128+runlength);
649 pixel[1]=pixels[q];
650 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
651 break;
652 p+=runlength;
653 }
654 }
655 return(p);
656}
657
cristy1e178e72011-08-28 19:44:34 +0000658static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image,
659 ExceptionInfo *exception)
cristy84c3d052011-03-07 19:22:02 +0000660{
661 char
cristy151b66d2015-04-15 10:50:31 +0000662 header[MagickPathExtent];
cristy84c3d052011-03-07 19:22:02 +0000663
cristy97bd7c32011-03-08 02:52:04 +0000664 const char
665 *property;
cristy84c3d052011-03-07 19:22:02 +0000666
667 MagickBooleanType
668 status;
669
cristy4c08aed2011-07-01 19:47:50 +0000670 register const Quantum
cristy84c3d052011-03-07 19:22:02 +0000671 *p;
672
cristy97bd7c32011-03-08 02:52:04 +0000673 register ssize_t
674 i,
675 x;
cristy84c3d052011-03-07 19:22:02 +0000676
677 size_t
678 length;
679
cristy97bd7c32011-03-08 02:52:04 +0000680 ssize_t
681 count,
682 y;
683
cristy84c3d052011-03-07 19:22:02 +0000684 unsigned char
cristy97bd7c32011-03-08 02:52:04 +0000685 pixel[4],
cristy84c3d052011-03-07 19:22:02 +0000686 *pixels;
687
688 /*
689 Open output image file.
690 */
691 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000692 assert(image_info->signature == MagickCoreSignature);
cristy84c3d052011-03-07 19:22:02 +0000693 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000694 assert(image->signature == MagickCoreSignature);
cristy84c3d052011-03-07 19:22:02 +0000695 if (image->debug != MagickFalse)
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000697 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000698 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +0000699 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy84c3d052011-03-07 19:22:02 +0000700 if (status == MagickFalse)
701 return(status);
cristy74d59d92012-07-12 23:48:47 +0000702 if (IsRGBColorspace(image->colorspace) == MagickFalse)
703 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy84c3d052011-03-07 19:22:02 +0000704 /*
705 Write header.
706 */
cristy151b66d2015-04-15 10:50:31 +0000707 (void) ResetMagickMemory(header,' ',MagickPathExtent);
708 length=CopyMagickString(header,"#?RGBE\n",MagickPathExtent);
cristy97bd7c32011-03-08 02:52:04 +0000709 (void) WriteBlob(image,length,(unsigned char *) header);
cristyd15e6592011-10-15 00:13:06 +0000710 property=GetImageProperty(image,"comment",exception);
cristy97bd7c32011-03-08 02:52:04 +0000711 if ((property != (const char *) NULL) &&
712 (strchr(property,'\n') == (char *) NULL))
713 {
cristy151b66d2015-04-15 10:50:31 +0000714 count=FormatLocaleString(header,MagickPathExtent,"#%s\n",property);
cristy97bd7c32011-03-08 02:52:04 +0000715 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
716 }
cristyd15e6592011-10-15 00:13:06 +0000717 property=GetImageProperty(image,"hdr:exposure",exception);
cristy97bd7c32011-03-08 02:52:04 +0000718 if (property != (const char *) NULL)
719 {
cristy151b66d2015-04-15 10:50:31 +0000720 count=FormatLocaleString(header,MagickPathExtent,"EXPOSURE=%g\n",
cristy79d05312014-12-25 18:13:29 +0000721 strtod(property,(char **) NULL));
cristy97bd7c32011-03-08 02:52:04 +0000722 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
723 }
724 if (image->gamma != 0.0)
725 {
cristy151b66d2015-04-15 10:50:31 +0000726 count=FormatLocaleString(header,MagickPathExtent,"GAMMA=%g\n",image->gamma);
cristy97bd7c32011-03-08 02:52:04 +0000727 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
728 }
cristy151b66d2015-04-15 10:50:31 +0000729 count=FormatLocaleString(header,MagickPathExtent,
cristy97bd7c32011-03-08 02:52:04 +0000730 "PRIMARIES=%g %g %g %g %g %g %g %g\n",
731 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
732 image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
733 image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
734 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
735 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
cristy151b66d2015-04-15 10:50:31 +0000736 length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MagickPathExtent);
cristy97bd7c32011-03-08 02:52:04 +0000737 (void) WriteBlob(image,length,(unsigned char *) header);
cristy151b66d2015-04-15 10:50:31 +0000738 count=FormatLocaleString(header,MagickPathExtent,"-Y %.20g +X %.20g\n",
cristy84c3d052011-03-07 19:22:02 +0000739 (double) image->rows,(double) image->columns);
cristy97bd7c32011-03-08 02:52:04 +0000740 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
cristy84c3d052011-03-07 19:22:02 +0000741 /*
742 Write HDR pixels.
743 */
cristyebc891a2011-04-24 23:04:16 +0000744 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
745 sizeof(*pixels));
cristy97bd7c32011-03-08 02:52:04 +0000746 if (pixels == (unsigned char *) NULL)
cristy84c3d052011-03-07 19:22:02 +0000747 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy84c3d052011-03-07 19:22:02 +0000748 for (y=0; y < (ssize_t) image->rows; y++)
749 {
cristy1e178e72011-08-28 19:44:34 +0000750 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000751 if (p == (const Quantum *) NULL)
cristy84c3d052011-03-07 19:22:02 +0000752 break;
cristy97bd7c32011-03-08 02:52:04 +0000753 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
754 {
755 pixel[0]=2;
756 pixel[1]=2;
757 pixel[2]=(unsigned char) (image->columns >> 8);
758 pixel[3]=(unsigned char) (image->columns & 0xff);
759 count=WriteBlob(image,4*sizeof(*pixel),pixel);
760 if (count != (ssize_t) (4*sizeof(*pixel)))
761 break;
762 }
763 i=0;
764 for (x=0; x < (ssize_t) image->columns; x++)
765 {
766 double
767 gamma;
768
769 pixel[0]=0;
770 pixel[1]=0;
771 pixel[2]=0;
772 pixel[3]=0;
cristy4c08aed2011-07-01 19:47:50 +0000773 gamma=QuantumScale*GetPixelRed(image,p);
774 if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
775 gamma=QuantumScale*GetPixelGreen(image,p);
776 if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
777 gamma=QuantumScale*GetPixelBlue(image,p);
cristy97bd7c32011-03-08 02:52:04 +0000778 if (gamma > MagickEpsilon)
779 {
780 int
781 exponent;
782
783 gamma=frexp(gamma,&exponent)*256.0/gamma;
cristy4c08aed2011-07-01 19:47:50 +0000784 pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
785 pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
786 pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
cristy97bd7c32011-03-08 02:52:04 +0000787 pixel[3]=(unsigned char) (exponent+128);
788 }
789 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
790 {
791 pixels[x]=pixel[0];
792 pixels[x+image->columns]=pixel[1];
793 pixels[x+2*image->columns]=pixel[2];
794 pixels[x+3*image->columns]=pixel[3];
795 }
796 else
797 {
798 pixels[i++]=pixel[0];
799 pixels[i++]=pixel[1];
800 pixels[i++]=pixel[2];
801 pixels[i++]=pixel[3];
802 }
cristyed231572011-07-14 02:18:59 +0000803 p+=GetPixelChannels(image);
cristy97bd7c32011-03-08 02:52:04 +0000804 }
805 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
806 {
807 for (i=0; i < 4; i++)
808 length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
809 }
810 else
811 {
cristy100a0562014-04-18 01:27:37 +0000812 count=WriteBlob(image,4*image->columns*sizeof(*pixels),pixels);
813 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
cristy97bd7c32011-03-08 02:52:04 +0000814 break;
815 }
cristy84c3d052011-03-07 19:22:02 +0000816 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
817 image->rows);
818 if (status == MagickFalse)
819 break;
820 }
cristy97bd7c32011-03-08 02:52:04 +0000821 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
cristy84c3d052011-03-07 19:22:02 +0000822 (void) CloseBlob(image);
823 return(MagickTrue);
824}