blob: 9647afaed75faa1aa3fc32ccddb4853e681a5eb5 [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 %
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
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
69 WriteHDRImage(const ImageInfo *,Image *);
70
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
138 format[MaxTextExtent],
139 keyword[MaxTextExtent],
140 tag[MaxTextExtent],
141 value[MaxTextExtent];
142
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);
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);
186 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 */
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)
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';
242 (void) SetImageProperty(image,"comment",comment);
243 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 {
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 }
cristyb51dff52011-05-19 16:55:47 +0000297 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
cristy97bd7c32011-03-08 02:52:04 +0000298 (void) SetImageProperty(image,tag,value);
299 break;
300 }
301 case 'G':
302 case 'g':
303 {
304 if (LocaleCompare(keyword,"gamma") == 0)
305 {
cristyc1acd842011-05-19 23:05:47 +0000306 image->gamma=InterpretLocaleValue(value,(char **) NULL);
cristy97bd7c32011-03-08 02:52:04 +0000307 break;
308 }
cristyb51dff52011-05-19 16:55:47 +0000309 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
cristy97bd7c32011-03-08 02:52:04 +0000310 (void) SetImageProperty(image,tag,value);
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
cristyb84ca112011-03-30 14:16:18 +0000322 (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]);
cristy97bd7c32011-03-08 02:52:04 +0000326 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 }
cristyb51dff52011-05-19 16:55:47 +0000336 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
cristy97bd7c32011-03-08 02:52:04 +0000337 (void) SetImageProperty(image,tag,value);
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 }
cristyb51dff52011-05-19 16:55:47 +0000354 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
cristy97bd7c32011-03-08 02:52:04 +0000355 (void) SetImageProperty(image,tag,value);
356 break;
357 }
358 default:
359 {
cristyb51dff52011-05-19 16:55:47 +0000360 (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword);
cristy97bd7c32011-03-08 02:52:04 +0000361 (void) SetImageProperty(image,tag,value);
362 break;
363 }
364 }
365 }
cristy03533f22011-03-06 23:30:17 +0000366 if ((image->columns == 0) && (image->rows == 0))
367 while (isspace((int) ((unsigned char) c)) != 0)
368 c=ReadBlobByte(image);
369 }
cristya74b77f2011-04-17 01:59:47 +0000370 if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) &&
371 (LocaleCompare(format,"32-bit_rle_xyze") != 0))
cristy03533f22011-03-06 23:30:17 +0000372 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
373 if ((image->columns == 0) || (image->rows == 0))
374 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
cristya0d69f22011-08-25 19:10:47 +0000375 if (LocaleCompare(format,"32-bit_rle_xyze") == 0)
cristyf0e4a5a2011-04-17 02:15:18 +0000376 image->colorspace=XYZColorspace;
377 image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ?
378 NoCompression : RLECompression;
cristy03533f22011-03-06 23:30:17 +0000379 if (image_info->ping != MagickFalse)
380 {
381 (void) CloseBlob(image);
382 return(GetFirstImageInList(image));
383 }
384 /*
cristya8ff9422011-03-06 23:58:56 +0000385 Read RGBE (red+green+blue+exponent) pixels.
cristy03533f22011-03-06 23:30:17 +0000386 */
cristyb84ca112011-03-30 14:16:18 +0000387 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
388 sizeof(*pixels));
cristy03533f22011-03-06 23:30:17 +0000389 if (pixels == (unsigned char *) NULL)
390 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
391 for (y=0; y < (ssize_t) image->rows; y++)
392 {
cristyf0e4a5a2011-04-17 02:15:18 +0000393 if (image->compression != RLECompression)
cristy03533f22011-03-06 23:30:17 +0000394 {
cristya74b77f2011-04-17 01:59:47 +0000395 count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels);
396 if (count != (ssize_t) (4*image->columns*sizeof(*pixels)))
cristy03533f22011-03-06 23:30:17 +0000397 break;
cristy97bd7c32011-03-08 02:52:04 +0000398 }
399 else
400 {
401 count=ReadBlob(image,4*sizeof(*pixel),pixel);
402 if (count != 4)
403 break;
404 if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns)
cristy03533f22011-03-06 23:30:17 +0000405 {
cristyf0e4a5a2011-04-17 02:15:18 +0000406 (void) memcpy(pixels,pixel,4*sizeof(*pixel));
407 count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4);
408 image->compression=NoCompression;
409 }
410 else
411 {
412 p=pixels;
413 for (i=0; i < 4; i++)
414 {
415 end=&pixels[(i+1)*image->columns];
416 while (p < end)
cristy03533f22011-03-06 23:30:17 +0000417 {
cristyf0e4a5a2011-04-17 02:15:18 +0000418 count=ReadBlob(image,2*sizeof(*pixel),pixel);
419 if (count < 1)
cristy03533f22011-03-06 23:30:17 +0000420 break;
cristyf0e4a5a2011-04-17 02:15:18 +0000421 if (pixel[0] > 128)
cristy97bd7c32011-03-08 02:52:04 +0000422 {
cristyf0e4a5a2011-04-17 02:15:18 +0000423 count=(ssize_t) pixel[0]-128;
424 if ((count == 0) || (count > (ssize_t) (end-p)))
cristy97bd7c32011-03-08 02:52:04 +0000425 break;
cristyf0e4a5a2011-04-17 02:15:18 +0000426 while (count-- > 0)
427 *p++=pixel[1];
428 }
429 else
430 {
431 count=(ssize_t) pixel[0];
432 if ((count == 0) || (count > (ssize_t) (end-p)))
433 break;
434 *p++=pixel[1];
435 if (--count > 0)
436 {
437 count=ReadBlob(image,(size_t) count*sizeof(*p),p);
438 if (count < 1)
439 break;
440 p+=count;
441 }
cristy97bd7c32011-03-08 02:52:04 +0000442 }
cristy03533f22011-03-06 23:30:17 +0000443 }
cristyf0e4a5a2011-04-17 02:15:18 +0000444 }
cristy03533f22011-03-06 23:30:17 +0000445 }
446 }
cristy03533f22011-03-06 23:30:17 +0000447 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000448 if (q == (const Quantum *) NULL)
cristy03533f22011-03-06 23:30:17 +0000449 break;
cristy97bd7c32011-03-08 02:52:04 +0000450 i=0;
cristy03533f22011-03-06 23:30:17 +0000451 for (x=0; x < (ssize_t) image->columns; x++)
452 {
cristyf0e4a5a2011-04-17 02:15:18 +0000453 if (image->compression == RLECompression)
cristy97bd7c32011-03-08 02:52:04 +0000454 {
455 pixel[0]=pixels[x];
456 pixel[1]=pixels[x+image->columns];
457 pixel[2]=pixels[x+2*image->columns];
458 pixel[3]=pixels[x+3*image->columns];
459 }
460 else
461 {
462 pixel[0]=pixels[i++];
463 pixel[1]=pixels[i++];
464 pixel[2]=pixels[i++];
465 pixel[3]=pixels[i++];
466 }
cristy4c08aed2011-07-01 19:47:50 +0000467 SetPixelRed(image,0,q);
468 SetPixelGreen(image,0,q);
469 SetPixelBlue(image,0,q);
cristy03533f22011-03-06 23:30:17 +0000470 if (pixel[3] != 0)
471 {
472 gamma=pow(2.0,pixel[3]-(128.0+8.0));
cristy4c08aed2011-07-01 19:47:50 +0000473 SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q);
474 SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q);
475 SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q);
cristy03533f22011-03-06 23:30:17 +0000476 }
cristyed231572011-07-14 02:18:59 +0000477 q+=GetPixelChannels(image);
cristy03533f22011-03-06 23:30:17 +0000478 }
479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
480 break;
481 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
482 image->rows);
483 if (status == MagickFalse)
484 break;
485 }
486 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
487 if (EOFBlob(image) != MagickFalse)
488 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
489 image->filename);
cristy579bc8f2011-03-06 17:27:05 +0000490 (void) CloseBlob(image);
491 return(GetFirstImageInList(image));
492}
493
494/*
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496% %
497% %
498% %
499% R e g i s t e r H D R I m a g e %
500% %
501% %
502% %
503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504%
cristy03533f22011-03-06 23:30:17 +0000505% RegisterHDRImage() adds attributes for the Radiance RGBE image format to the
cristy21384232011-03-06 17:31:08 +0000506% list of supported formats. The attributes include the image format tag, a
cristy579bc8f2011-03-06 17:27:05 +0000507% method to read and/or write the format, whether the format supports the
508% saving of more than one frame to the same file or blob, whether the format
509% supports native in-memory I/O, and a brief description of the format.
510%
511% The format of the RegisterHDRImage method is:
512%
513% size_t RegisterHDRImage(void)
514%
515*/
516ModuleExport size_t RegisterHDRImage(void)
517{
518 MagickInfo
519 *entry;
520
521 entry=SetMagickInfo("HDR");
522 entry->decoder=(DecodeImageHandler *) ReadHDRImage;
cristy84c3d052011-03-07 19:22:02 +0000523 entry->encoder=(EncodeImageHandler *) WriteHDRImage;
cristy03533f22011-03-06 23:30:17 +0000524 entry->description=ConstantString("Radiance RGBE image format");
cristy579bc8f2011-03-06 17:27:05 +0000525 entry->module=ConstantString("HDR");
cristy03533f22011-03-06 23:30:17 +0000526 entry->magick=(IsImageFormatHandler *) IsHDR;
cristy579bc8f2011-03-06 17:27:05 +0000527 (void) RegisterMagickInfo(entry);
528 return(MagickImageCoderSignature);
529}
530
531/*
532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533% %
534% %
535% %
536% U n r e g i s t e r H D R I m a g e %
537% %
538% %
539% %
540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541%
542% UnregisterHDRImage() removes format registrations made by the
543% HDR module from the list of supported formats.
544%
545% The format of the UnregisterHDRImage method is:
546%
547% UnregisterHDRImage(void)
548%
549*/
550ModuleExport void UnregisterHDRImage(void)
551{
552 (void) UnregisterMagickInfo("HDR");
553}
cristy84c3d052011-03-07 19:22:02 +0000554
555/*
556%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557% %
558% %
559% %
560% W r i t e H D R I m a g e %
561% %
562% %
563% %
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565%
566% WriteHDRImage() writes an image in the Radience RGBE image format.
567%
568% The format of the WriteHDRImage method is:
569%
570% MagickBooleanType WriteHDRImage(const ImageInfo *image_info,
571% Image *image)
572%
573% A description of each parameter follows.
574%
575% o image_info: the image info.
576%
577% o image: The image.
578%
579*/
cristy97bd7c32011-03-08 02:52:04 +0000580
581static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels)
582{
583#define MinimumRunlength 4
584
585 register size_t
586 p,
587 q;
588
589 size_t
590 runlength;
591
592 ssize_t
593 count,
594 previous_count;
595
596 unsigned char
597 pixel[2];
598
599 for (p=0; p < image->columns; )
600 {
601 q=p;
602 runlength=0;
603 previous_count=0;
604 while ((runlength < MinimumRunlength) && (q < image->columns))
605 {
606 q+=runlength;
607 previous_count=(ssize_t) runlength;
608 runlength=1;
609 while ((pixels[q] == pixels[q+runlength]) &&
610 ((q+runlength) < image->columns) && (runlength < 127))
611 runlength++;
612 }
613 if ((previous_count > 1) && (previous_count == (ssize_t) (q-p)))
614 {
615 pixel[0]=(unsigned char) (128+previous_count);
616 pixel[1]=pixels[p];
617 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
618 break;
619 p=q;
620 }
621 while (p < q)
622 {
623 count=(ssize_t) (q-p);
624 if (count > 128)
625 count=128;
626 pixel[0]=(unsigned char) count;
627 if (WriteBlob(image,sizeof(*pixel),pixel) < 1)
628 break;
629 if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1)
630 break;
631 p+=count;
632 }
633 if (runlength >= MinimumRunlength)
634 {
635 pixel[0]=(unsigned char) (128+runlength);
636 pixel[1]=pixels[q];
637 if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1)
638 break;
639 p+=runlength;
640 }
641 }
642 return(p);
643}
644
cristy84c3d052011-03-07 19:22:02 +0000645static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image)
646{
647 char
648 header[MaxTextExtent];
649
cristy97bd7c32011-03-08 02:52:04 +0000650 const char
651 *property;
cristy84c3d052011-03-07 19:22:02 +0000652
653 MagickBooleanType
654 status;
655
cristy4c08aed2011-07-01 19:47:50 +0000656 register const Quantum
cristy84c3d052011-03-07 19:22:02 +0000657 *p;
658
cristy97bd7c32011-03-08 02:52:04 +0000659 register ssize_t
660 i,
661 x;
cristy84c3d052011-03-07 19:22:02 +0000662
663 size_t
664 length;
665
cristy97bd7c32011-03-08 02:52:04 +0000666 ssize_t
667 count,
668 y;
669
cristy84c3d052011-03-07 19:22:02 +0000670 unsigned char
cristy97bd7c32011-03-08 02:52:04 +0000671 pixel[4],
cristy84c3d052011-03-07 19:22:02 +0000672 *pixels;
673
674 /*
675 Open output image file.
676 */
677 assert(image_info != (const ImageInfo *) NULL);
678 assert(image_info->signature == MagickSignature);
679 assert(image != (Image *) NULL);
680 assert(image->signature == MagickSignature);
681 if (image->debug != MagickFalse)
682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
683 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
684 if (status == MagickFalse)
685 return(status);
cristy510d06a2011-07-06 23:43:54 +0000686 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy84c3d052011-03-07 19:22:02 +0000687 (void) TransformImageColorspace(image,RGBColorspace);
688 /*
689 Write header.
690 */
691 (void) ResetMagickMemory(header,' ',MaxTextExtent);
cristy97bd7c32011-03-08 02:52:04 +0000692 length=CopyMagickString(header,"#?RGBE\n",MaxTextExtent);
693 (void) WriteBlob(image,length,(unsigned char *) header);
694 property=GetImageProperty(image,"comment");
695 if ((property != (const char *) NULL) &&
696 (strchr(property,'\n') == (char *) NULL))
697 {
cristyb51dff52011-05-19 16:55:47 +0000698 count=FormatLocaleString(header,MaxTextExtent,"#%s\n",property);
cristy97bd7c32011-03-08 02:52:04 +0000699 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
700 }
701 property=GetImageProperty(image,"hdr:exposure");
702 if (property != (const char *) NULL)
703 {
cristyb51dff52011-05-19 16:55:47 +0000704 count=FormatLocaleString(header,MaxTextExtent,"EXPOSURE=%g\n",
cristy97bd7c32011-03-08 02:52:04 +0000705 atof(property));
706 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
707 }
708 if (image->gamma != 0.0)
709 {
cristyb51dff52011-05-19 16:55:47 +0000710 count=FormatLocaleString(header,MaxTextExtent,"GAMMA=%g\n",image->gamma);
cristy97bd7c32011-03-08 02:52:04 +0000711 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
712 }
cristyb51dff52011-05-19 16:55:47 +0000713 count=FormatLocaleString(header,MaxTextExtent,
cristy97bd7c32011-03-08 02:52:04 +0000714 "PRIMARIES=%g %g %g %g %g %g %g %g\n",
715 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
716 image->chromaticity.green_primary.x,image->chromaticity.green_primary.y,
717 image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y,
718 image->chromaticity.white_point.x,image->chromaticity.white_point.y);
719 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
720 length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MaxTextExtent);
721 (void) WriteBlob(image,length,(unsigned char *) header);
cristyb51dff52011-05-19 16:55:47 +0000722 count=FormatLocaleString(header,MaxTextExtent,"-Y %.20g +X %.20g\n",
cristy84c3d052011-03-07 19:22:02 +0000723 (double) image->rows,(double) image->columns);
cristy97bd7c32011-03-08 02:52:04 +0000724 (void) WriteBlob(image,(size_t) count,(unsigned char *) header);
cristy84c3d052011-03-07 19:22:02 +0000725 /*
726 Write HDR pixels.
727 */
cristyebc891a2011-04-24 23:04:16 +0000728 pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4*
729 sizeof(*pixels));
cristy97bd7c32011-03-08 02:52:04 +0000730 if (pixels == (unsigned char *) NULL)
cristy84c3d052011-03-07 19:22:02 +0000731 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy84c3d052011-03-07 19:22:02 +0000732 for (y=0; y < (ssize_t) image->rows; y++)
733 {
734 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +0000735 if (p == (const Quantum *) NULL)
cristy84c3d052011-03-07 19:22:02 +0000736 break;
cristy97bd7c32011-03-08 02:52:04 +0000737 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
738 {
739 pixel[0]=2;
740 pixel[1]=2;
741 pixel[2]=(unsigned char) (image->columns >> 8);
742 pixel[3]=(unsigned char) (image->columns & 0xff);
743 count=WriteBlob(image,4*sizeof(*pixel),pixel);
744 if (count != (ssize_t) (4*sizeof(*pixel)))
745 break;
746 }
747 i=0;
748 for (x=0; x < (ssize_t) image->columns; x++)
749 {
750 double
751 gamma;
752
753 pixel[0]=0;
754 pixel[1]=0;
755 pixel[2]=0;
756 pixel[3]=0;
cristy4c08aed2011-07-01 19:47:50 +0000757 gamma=QuantumScale*GetPixelRed(image,p);
758 if ((QuantumScale*GetPixelGreen(image,p)) > gamma)
759 gamma=QuantumScale*GetPixelGreen(image,p);
760 if ((QuantumScale*GetPixelBlue(image,p)) > gamma)
761 gamma=QuantumScale*GetPixelBlue(image,p);
cristy97bd7c32011-03-08 02:52:04 +0000762 if (gamma > MagickEpsilon)
763 {
764 int
765 exponent;
766
767 gamma=frexp(gamma,&exponent)*256.0/gamma;
cristy4c08aed2011-07-01 19:47:50 +0000768 pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p));
769 pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p));
770 pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p));
cristy97bd7c32011-03-08 02:52:04 +0000771 pixel[3]=(unsigned char) (exponent+128);
772 }
773 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
774 {
775 pixels[x]=pixel[0];
776 pixels[x+image->columns]=pixel[1];
777 pixels[x+2*image->columns]=pixel[2];
778 pixels[x+3*image->columns]=pixel[3];
779 }
780 else
781 {
782 pixels[i++]=pixel[0];
783 pixels[i++]=pixel[1];
784 pixels[i++]=pixel[2];
785 pixels[i++]=pixel[3];
786 }
cristyed231572011-07-14 02:18:59 +0000787 p+=GetPixelChannels(image);
cristy97bd7c32011-03-08 02:52:04 +0000788 }
789 if ((image->columns >= 8) && (image->columns <= 0x7ffff))
790 {
791 for (i=0; i < 4; i++)
792 length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]);
793 }
794 else
795 {
796 count=WriteBlob(image,4*image->columns*sizeof(*pixel),pixel);
797 if (count != (ssize_t) (4*image->columns*sizeof(*pixel)))
798 break;
799 }
cristy84c3d052011-03-07 19:22:02 +0000800 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
801 image->rows);
802 if (status == MagickFalse)
803 break;
804 }
cristy97bd7c32011-03-08 02:52:04 +0000805 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
cristy84c3d052011-03-07 19:22:02 +0000806 (void) CloseBlob(image);
807 return(MagickTrue);
808}