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