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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 47 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 66 | Forward declarations. |
| 67 | */ |
| 68 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 69 | WriteHDRImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 70 | |
| 71 | /* |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 | % % |
| 74 | % % |
| 75 | % % |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 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); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 104 | if (LocaleNCompare((const char *) magick,"#?RGBE",6) == 0) |
| 105 | return(MagickTrue); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 106 | return(MagickFalse); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 111 | % % |
| 112 | % % |
| 113 | % % |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 114 | % R e a d H D R I m a g e % |
| 115 | % % |
| 116 | % % |
| 117 | % % |
| 118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 119 | % |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 120 | % ReadHDRImage() reads the Radiance RGBE image format and returns it. It |
cristy | 2138423 | 2011-03-06 17:31:08 +0000 | [diff] [blame] | 121 | % allocates the memory necessary for the new Image structure and returns a |
| 122 | % pointer to the new image. |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 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 | { |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 137 | char |
| 138 | format[MaxTextExtent], |
| 139 | keyword[MaxTextExtent], |
| 140 | tag[MaxTextExtent], |
| 141 | value[MaxTextExtent]; |
| 142 | |
| 143 | double |
| 144 | gamma; |
| 145 | |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 146 | Image |
| 147 | *image; |
| 148 | |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 149 | int |
| 150 | c; |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 151 | |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 152 | MagickBooleanType |
| 153 | status, |
| 154 | value_expected; |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 155 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 156 | register Quantum |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 157 | *q; |
| 158 | |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 159 | register ssize_t |
| 160 | i, |
| 161 | x; |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 162 | |
cristy | 19c9c3c | 2011-08-25 18:51:30 +0000 | [diff] [blame] | 163 | register unsigned char |
| 164 | *p; |
| 165 | |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 166 | ssize_t |
| 167 | count, |
| 168 | y; |
| 169 | |
| 170 | unsigned char |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 171 | *end, |
| 172 | pixel[4], |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 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); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 185 | image=AcquireImage(image_info,exception); |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 186 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 187 | if (status == MagickFalse) |
| 188 | { |
| 189 | image=DestroyImageList(image); |
| 190 | return((Image *) NULL); |
| 191 | } |
| 192 | /* |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 193 | Decode image header. |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 194 | */ |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 195 | image->columns=0; |
| 196 | image->rows=0; |
| 197 | *format='\0'; |
| 198 | c=ReadBlobByte(image); |
| 199 | if (c == EOF) |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 200 | { |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 201 | image=DestroyImage(image); |
| 202 | return((Image *) NULL); |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 203 | } |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 204 | while (isgraph(c) && (image->columns == 0) && (image->rows == 0)) |
| 205 | { |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 206 | if (c == (int) '#') |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 207 | { |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 208 | char |
| 209 | *comment; |
| 210 | |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 211 | register char |
| 212 | *p; |
| 213 | |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 214 | size_t |
| 215 | length; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 216 | |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 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) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 234 | break; |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 235 | p=comment+strlen(comment); |
| 236 | } |
| 237 | *p=(char) c; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 238 | } |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 239 | if (comment == (char *) NULL) |
| 240 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 241 | *p='\0'; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 242 | (void) SetImageProperty(image,"comment",comment,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 243 | comment=DestroyString(comment); |
| 244 | c=ReadBlobByte(image); |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 245 | } |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 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 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 297 | (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 298 | (void) SetImageProperty(image,tag,value,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | case 'G': |
| 302 | case 'g': |
| 303 | { |
| 304 | if (LocaleCompare(keyword,"gamma") == 0) |
| 305 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 306 | image->gamma=StringToDouble(value,(char **) NULL); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 309 | (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 310 | (void) SetImageProperty(image,tag,value,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 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 | |
cristy | b84ca11 | 2011-03-30 14:16:18 +0000 | [diff] [blame] | 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]); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 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 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 336 | (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 337 | (void) SetImageProperty(image,tag,value,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | case 'Y': |
| 341 | case 'y': |
| 342 | { |
cristy | 587235d | 2014-12-20 23:29:55 +0000 | [diff] [blame] | 343 | char |
| 344 | target[] = "Y"; |
| 345 | |
| 346 | if (strcmp(keyword,target) == 0) |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 347 | { |
| 348 | int |
| 349 | height, |
| 350 | width; |
| 351 | |
| 352 | (void) sscanf(value,"%d +X %d",&height,&width); |
| 353 | image->columns=(size_t) width; |
| 354 | image->rows=(size_t) height; |
| 355 | break; |
| 356 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 357 | (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 358 | (void) SetImageProperty(image,tag,value,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | default: |
| 362 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 363 | (void) FormatLocaleString(tag,MaxTextExtent,"hdr:%s",keyword); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 364 | (void) SetImageProperty(image,tag,value,exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
| 368 | } |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 369 | if ((image->columns == 0) && (image->rows == 0)) |
| 370 | while (isspace((int) ((unsigned char) c)) != 0) |
| 371 | c=ReadBlobByte(image); |
| 372 | } |
cristy | a74b77f | 2011-04-17 01:59:47 +0000 | [diff] [blame] | 373 | if ((LocaleCompare(format,"32-bit_rle_rgbe") != 0) && |
| 374 | (LocaleCompare(format,"32-bit_rle_xyze") != 0)) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 375 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 376 | if ((image->columns == 0) || (image->rows == 0)) |
| 377 | ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); |
cristy | 74d59d9 | 2012-07-12 23:48:47 +0000 | [diff] [blame] | 378 | (void) SetImageColorspace(image,RGBColorspace,exception); |
cristy | a0d69f2 | 2011-08-25 19:10:47 +0000 | [diff] [blame] | 379 | if (LocaleCompare(format,"32-bit_rle_xyze") == 0) |
cristy | 74d59d9 | 2012-07-12 23:48:47 +0000 | [diff] [blame] | 380 | (void) SetImageColorspace(image,XYZColorspace,exception); |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 381 | image->compression=(image->columns < 8) || (image->columns > 0x7ffff) ? |
| 382 | NoCompression : RLECompression; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 383 | if (image_info->ping != MagickFalse) |
| 384 | { |
| 385 | (void) CloseBlob(image); |
| 386 | return(GetFirstImageInList(image)); |
| 387 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 388 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 389 | if (status == MagickFalse) |
| 390 | return(DestroyImageList(image)); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 391 | /* |
cristy | a8ff942 | 2011-03-06 23:58:56 +0000 | [diff] [blame] | 392 | Read RGBE (red+green+blue+exponent) pixels. |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 393 | */ |
cristy | b84ca11 | 2011-03-30 14:16:18 +0000 | [diff] [blame] | 394 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4* |
| 395 | sizeof(*pixels)); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 396 | if (pixels == (unsigned char *) NULL) |
| 397 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 398 | for (y=0; y < (ssize_t) image->rows; y++) |
| 399 | { |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 400 | if (image->compression != RLECompression) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 401 | { |
cristy | a74b77f | 2011-04-17 01:59:47 +0000 | [diff] [blame] | 402 | count=ReadBlob(image,4*image->columns*sizeof(*pixels),pixels); |
| 403 | if (count != (ssize_t) (4*image->columns*sizeof(*pixels))) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 404 | break; |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 405 | } |
| 406 | else |
| 407 | { |
| 408 | count=ReadBlob(image,4*sizeof(*pixel),pixel); |
| 409 | if (count != 4) |
| 410 | break; |
| 411 | if ((size_t) ((((size_t) pixel[2]) << 8) | pixel[3]) != image->columns) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 412 | { |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 413 | (void) memcpy(pixels,pixel,4*sizeof(*pixel)); |
| 414 | count=ReadBlob(image,4*(image->columns-1)*sizeof(*pixels),pixels+4); |
| 415 | image->compression=NoCompression; |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | p=pixels; |
| 420 | for (i=0; i < 4; i++) |
| 421 | { |
| 422 | end=&pixels[(i+1)*image->columns]; |
| 423 | while (p < end) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 424 | { |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 425 | count=ReadBlob(image,2*sizeof(*pixel),pixel); |
| 426 | if (count < 1) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 427 | break; |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 428 | if (pixel[0] > 128) |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 429 | { |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 430 | count=(ssize_t) pixel[0]-128; |
| 431 | if ((count == 0) || (count > (ssize_t) (end-p))) |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 432 | break; |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 433 | while (count-- > 0) |
| 434 | *p++=pixel[1]; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | count=(ssize_t) pixel[0]; |
| 439 | if ((count == 0) || (count > (ssize_t) (end-p))) |
| 440 | break; |
| 441 | *p++=pixel[1]; |
| 442 | if (--count > 0) |
| 443 | { |
| 444 | count=ReadBlob(image,(size_t) count*sizeof(*p),p); |
| 445 | if (count < 1) |
| 446 | break; |
| 447 | p+=count; |
| 448 | } |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 449 | } |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 450 | } |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 451 | } |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 454 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 455 | if (q == (Quantum *) NULL) |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 456 | break; |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 457 | i=0; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 458 | for (x=0; x < (ssize_t) image->columns; x++) |
| 459 | { |
cristy | f0e4a5a | 2011-04-17 02:15:18 +0000 | [diff] [blame] | 460 | if (image->compression == RLECompression) |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 461 | { |
| 462 | pixel[0]=pixels[x]; |
| 463 | pixel[1]=pixels[x+image->columns]; |
| 464 | pixel[2]=pixels[x+2*image->columns]; |
| 465 | pixel[3]=pixels[x+3*image->columns]; |
| 466 | } |
| 467 | else |
| 468 | { |
| 469 | pixel[0]=pixels[i++]; |
| 470 | pixel[1]=pixels[i++]; |
| 471 | pixel[2]=pixels[i++]; |
| 472 | pixel[3]=pixels[i++]; |
| 473 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 474 | SetPixelRed(image,0,q); |
| 475 | SetPixelGreen(image,0,q); |
| 476 | SetPixelBlue(image,0,q); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 477 | if (pixel[3] != 0) |
| 478 | { |
| 479 | gamma=pow(2.0,pixel[3]-(128.0+8.0)); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 480 | SetPixelRed(image,ClampToQuantum(QuantumRange*gamma*pixel[0]),q); |
| 481 | SetPixelGreen(image,ClampToQuantum(QuantumRange*gamma*pixel[1]),q); |
| 482 | SetPixelBlue(image,ClampToQuantum(QuantumRange*gamma*pixel[2]),q); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 483 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 484 | q+=GetPixelChannels(image); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 485 | } |
| 486 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 487 | break; |
| 488 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 489 | image->rows); |
| 490 | if (status == MagickFalse) |
| 491 | break; |
| 492 | } |
| 493 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 494 | if (EOFBlob(image) != MagickFalse) |
| 495 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 496 | image->filename); |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 497 | (void) CloseBlob(image); |
| 498 | return(GetFirstImageInList(image)); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 503 | % % |
| 504 | % % |
| 505 | % % |
| 506 | % R e g i s t e r H D R I m a g e % |
| 507 | % % |
| 508 | % % |
| 509 | % % |
| 510 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 511 | % |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 512 | % RegisterHDRImage() adds attributes for the Radiance RGBE image format to the |
cristy | 2138423 | 2011-03-06 17:31:08 +0000 | [diff] [blame] | 513 | % list of supported formats. The attributes include the image format tag, a |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 514 | % method to read and/or write the format, whether the format supports the |
| 515 | % saving of more than one frame to the same file or blob, whether the format |
| 516 | % supports native in-memory I/O, and a brief description of the format. |
| 517 | % |
| 518 | % The format of the RegisterHDRImage method is: |
| 519 | % |
| 520 | % size_t RegisterHDRImage(void) |
| 521 | % |
| 522 | */ |
| 523 | ModuleExport size_t RegisterHDRImage(void) |
| 524 | { |
| 525 | MagickInfo |
| 526 | *entry; |
| 527 | |
| 528 | entry=SetMagickInfo("HDR"); |
| 529 | entry->decoder=(DecodeImageHandler *) ReadHDRImage; |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 530 | entry->encoder=(EncodeImageHandler *) WriteHDRImage; |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 531 | entry->description=ConstantString("Radiance RGBE image format"); |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 532 | entry->module=ConstantString("HDR"); |
cristy | 03533f2 | 2011-03-06 23:30:17 +0000 | [diff] [blame] | 533 | entry->magick=(IsImageFormatHandler *) IsHDR; |
cristy | 579bc8f | 2011-03-06 17:27:05 +0000 | [diff] [blame] | 534 | (void) RegisterMagickInfo(entry); |
| 535 | return(MagickImageCoderSignature); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 540 | % % |
| 541 | % % |
| 542 | % % |
| 543 | % U n r e g i s t e r H D R I m a g e % |
| 544 | % % |
| 545 | % % |
| 546 | % % |
| 547 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 548 | % |
| 549 | % UnregisterHDRImage() removes format registrations made by the |
| 550 | % HDR module from the list of supported formats. |
| 551 | % |
| 552 | % The format of the UnregisterHDRImage method is: |
| 553 | % |
| 554 | % UnregisterHDRImage(void) |
| 555 | % |
| 556 | */ |
| 557 | ModuleExport void UnregisterHDRImage(void) |
| 558 | { |
| 559 | (void) UnregisterMagickInfo("HDR"); |
| 560 | } |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 564 | % % |
| 565 | % % |
| 566 | % % |
| 567 | % W r i t e H D R I m a g e % |
| 568 | % % |
| 569 | % % |
| 570 | % % |
| 571 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 572 | % |
| 573 | % WriteHDRImage() writes an image in the Radience RGBE image format. |
| 574 | % |
| 575 | % The format of the WriteHDRImage method is: |
| 576 | % |
| 577 | % MagickBooleanType WriteHDRImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 578 | % Image *image,ExceptionInfo *exception) |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 579 | % |
| 580 | % A description of each parameter follows. |
| 581 | % |
| 582 | % o image_info: the image info. |
| 583 | % |
| 584 | % o image: The image. |
| 585 | % |
| 586 | */ |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 587 | |
| 588 | static size_t HDRWriteRunlengthPixels(Image *image,unsigned char *pixels) |
| 589 | { |
| 590 | #define MinimumRunlength 4 |
| 591 | |
| 592 | register size_t |
| 593 | p, |
| 594 | q; |
| 595 | |
| 596 | size_t |
| 597 | runlength; |
| 598 | |
| 599 | ssize_t |
| 600 | count, |
| 601 | previous_count; |
| 602 | |
| 603 | unsigned char |
| 604 | pixel[2]; |
| 605 | |
| 606 | for (p=0; p < image->columns; ) |
| 607 | { |
| 608 | q=p; |
| 609 | runlength=0; |
| 610 | previous_count=0; |
| 611 | while ((runlength < MinimumRunlength) && (q < image->columns)) |
| 612 | { |
| 613 | q+=runlength; |
| 614 | previous_count=(ssize_t) runlength; |
| 615 | runlength=1; |
| 616 | while ((pixels[q] == pixels[q+runlength]) && |
| 617 | ((q+runlength) < image->columns) && (runlength < 127)) |
| 618 | runlength++; |
| 619 | } |
| 620 | if ((previous_count > 1) && (previous_count == (ssize_t) (q-p))) |
| 621 | { |
| 622 | pixel[0]=(unsigned char) (128+previous_count); |
| 623 | pixel[1]=pixels[p]; |
| 624 | if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1) |
| 625 | break; |
| 626 | p=q; |
| 627 | } |
| 628 | while (p < q) |
| 629 | { |
| 630 | count=(ssize_t) (q-p); |
| 631 | if (count > 128) |
| 632 | count=128; |
| 633 | pixel[0]=(unsigned char) count; |
| 634 | if (WriteBlob(image,sizeof(*pixel),pixel) < 1) |
| 635 | break; |
| 636 | if (WriteBlob(image,(size_t) count*sizeof(*pixel),&pixels[p]) < 1) |
| 637 | break; |
| 638 | p+=count; |
| 639 | } |
| 640 | if (runlength >= MinimumRunlength) |
| 641 | { |
| 642 | pixel[0]=(unsigned char) (128+runlength); |
| 643 | pixel[1]=pixels[q]; |
| 644 | if (WriteBlob(image,2*sizeof(*pixel),pixel) < 1) |
| 645 | break; |
| 646 | p+=runlength; |
| 647 | } |
| 648 | } |
| 649 | return(p); |
| 650 | } |
| 651 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 652 | static MagickBooleanType WriteHDRImage(const ImageInfo *image_info,Image *image, |
| 653 | ExceptionInfo *exception) |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 654 | { |
| 655 | char |
| 656 | header[MaxTextExtent]; |
| 657 | |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 658 | const char |
| 659 | *property; |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 660 | |
| 661 | MagickBooleanType |
| 662 | status; |
| 663 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 664 | register const Quantum |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 665 | *p; |
| 666 | |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 667 | register ssize_t |
| 668 | i, |
| 669 | x; |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 670 | |
| 671 | size_t |
| 672 | length; |
| 673 | |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 674 | ssize_t |
| 675 | count, |
| 676 | y; |
| 677 | |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 678 | unsigned char |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 679 | pixel[4], |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 680 | *pixels; |
| 681 | |
| 682 | /* |
| 683 | Open output image file. |
| 684 | */ |
| 685 | assert(image_info != (const ImageInfo *) NULL); |
| 686 | assert(image_info->signature == MagickSignature); |
| 687 | assert(image != (Image *) NULL); |
| 688 | assert(image->signature == MagickSignature); |
| 689 | if (image->debug != MagickFalse) |
| 690 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 691 | assert(exception != (ExceptionInfo *) NULL); |
| 692 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 693 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 694 | if (status == MagickFalse) |
| 695 | return(status); |
cristy | 74d59d9 | 2012-07-12 23:48:47 +0000 | [diff] [blame] | 696 | if (IsRGBColorspace(image->colorspace) == MagickFalse) |
| 697 | (void) TransformImageColorspace(image,RGBColorspace,exception); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 698 | /* |
| 699 | Write header. |
| 700 | */ |
| 701 | (void) ResetMagickMemory(header,' ',MaxTextExtent); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 702 | length=CopyMagickString(header,"#?RGBE\n",MaxTextExtent); |
| 703 | (void) WriteBlob(image,length,(unsigned char *) header); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 704 | property=GetImageProperty(image,"comment",exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 705 | if ((property != (const char *) NULL) && |
| 706 | (strchr(property,'\n') == (char *) NULL)) |
| 707 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 708 | count=FormatLocaleString(header,MaxTextExtent,"#%s\n",property); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 709 | (void) WriteBlob(image,(size_t) count,(unsigned char *) header); |
| 710 | } |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 711 | property=GetImageProperty(image,"hdr:exposure",exception); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 712 | if (property != (const char *) NULL) |
| 713 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 714 | count=FormatLocaleString(header,MaxTextExtent,"EXPOSURE=%g\n", |
cristy | 79d0531 | 2014-12-25 18:13:29 +0000 | [diff] [blame^] | 715 | strtod(property,(char **) NULL)); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 716 | (void) WriteBlob(image,(size_t) count,(unsigned char *) header); |
| 717 | } |
| 718 | if (image->gamma != 0.0) |
| 719 | { |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 720 | count=FormatLocaleString(header,MaxTextExtent,"GAMMA=%g\n",image->gamma); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 721 | (void) WriteBlob(image,(size_t) count,(unsigned char *) header); |
| 722 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 723 | count=FormatLocaleString(header,MaxTextExtent, |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 724 | "PRIMARIES=%g %g %g %g %g %g %g %g\n", |
| 725 | image->chromaticity.red_primary.x,image->chromaticity.red_primary.y, |
| 726 | image->chromaticity.green_primary.x,image->chromaticity.green_primary.y, |
| 727 | image->chromaticity.blue_primary.x,image->chromaticity.blue_primary.y, |
| 728 | image->chromaticity.white_point.x,image->chromaticity.white_point.y); |
| 729 | (void) WriteBlob(image,(size_t) count,(unsigned char *) header); |
| 730 | length=CopyMagickString(header,"FORMAT=32-bit_rle_rgbe\n\n",MaxTextExtent); |
| 731 | (void) WriteBlob(image,length,(unsigned char *) header); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 732 | count=FormatLocaleString(header,MaxTextExtent,"-Y %.20g +X %.20g\n", |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 733 | (double) image->rows,(double) image->columns); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 734 | (void) WriteBlob(image,(size_t) count,(unsigned char *) header); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 735 | /* |
| 736 | Write HDR pixels. |
| 737 | */ |
cristy | ebc891a | 2011-04-24 23:04:16 +0000 | [diff] [blame] | 738 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns,4* |
| 739 | sizeof(*pixels)); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 740 | if (pixels == (unsigned char *) NULL) |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 741 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 742 | for (y=0; y < (ssize_t) image->rows; y++) |
| 743 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 744 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 745 | if (p == (const Quantum *) NULL) |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 746 | break; |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 747 | if ((image->columns >= 8) && (image->columns <= 0x7ffff)) |
| 748 | { |
| 749 | pixel[0]=2; |
| 750 | pixel[1]=2; |
| 751 | pixel[2]=(unsigned char) (image->columns >> 8); |
| 752 | pixel[3]=(unsigned char) (image->columns & 0xff); |
| 753 | count=WriteBlob(image,4*sizeof(*pixel),pixel); |
| 754 | if (count != (ssize_t) (4*sizeof(*pixel))) |
| 755 | break; |
| 756 | } |
| 757 | i=0; |
| 758 | for (x=0; x < (ssize_t) image->columns; x++) |
| 759 | { |
| 760 | double |
| 761 | gamma; |
| 762 | |
| 763 | pixel[0]=0; |
| 764 | pixel[1]=0; |
| 765 | pixel[2]=0; |
| 766 | pixel[3]=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 767 | gamma=QuantumScale*GetPixelRed(image,p); |
| 768 | if ((QuantumScale*GetPixelGreen(image,p)) > gamma) |
| 769 | gamma=QuantumScale*GetPixelGreen(image,p); |
| 770 | if ((QuantumScale*GetPixelBlue(image,p)) > gamma) |
| 771 | gamma=QuantumScale*GetPixelBlue(image,p); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 772 | if (gamma > MagickEpsilon) |
| 773 | { |
| 774 | int |
| 775 | exponent; |
| 776 | |
| 777 | gamma=frexp(gamma,&exponent)*256.0/gamma; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 778 | pixel[0]=(unsigned char) (gamma*QuantumScale*GetPixelRed(image,p)); |
| 779 | pixel[1]=(unsigned char) (gamma*QuantumScale*GetPixelGreen(image,p)); |
| 780 | pixel[2]=(unsigned char) (gamma*QuantumScale*GetPixelBlue(image,p)); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 781 | pixel[3]=(unsigned char) (exponent+128); |
| 782 | } |
| 783 | if ((image->columns >= 8) && (image->columns <= 0x7ffff)) |
| 784 | { |
| 785 | pixels[x]=pixel[0]; |
| 786 | pixels[x+image->columns]=pixel[1]; |
| 787 | pixels[x+2*image->columns]=pixel[2]; |
| 788 | pixels[x+3*image->columns]=pixel[3]; |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | pixels[i++]=pixel[0]; |
| 793 | pixels[i++]=pixel[1]; |
| 794 | pixels[i++]=pixel[2]; |
| 795 | pixels[i++]=pixel[3]; |
| 796 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 797 | p+=GetPixelChannels(image); |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 798 | } |
| 799 | if ((image->columns >= 8) && (image->columns <= 0x7ffff)) |
| 800 | { |
| 801 | for (i=0; i < 4; i++) |
| 802 | length=HDRWriteRunlengthPixels(image,&pixels[i*image->columns]); |
| 803 | } |
| 804 | else |
| 805 | { |
cristy | 100a056 | 2014-04-18 01:27:37 +0000 | [diff] [blame] | 806 | count=WriteBlob(image,4*image->columns*sizeof(*pixels),pixels); |
| 807 | if (count != (ssize_t) (4*image->columns*sizeof(*pixels))) |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 808 | break; |
| 809 | } |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 810 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 811 | image->rows); |
| 812 | if (status == MagickFalse) |
| 813 | break; |
| 814 | } |
cristy | 97bd7c3 | 2011-03-08 02:52:04 +0000 | [diff] [blame] | 815 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
cristy | 84c3d05 | 2011-03-07 19:22:02 +0000 | [diff] [blame] | 816 | (void) CloseBlob(image); |
| 817 | return(MagickTrue); |
| 818 | } |