cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP SSSSS DDDD % |
| 7 | % P P SS D D % |
| 8 | % PPPP SSS D D % |
| 9 | % P SS D D % |
| 10 | % P SSSSS DDDD % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Adobe Photoshop Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % Leonard Rosenthol % |
| 18 | % July 1992 % |
| 19 | % % |
| 20 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 21 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 22 | % dedicated to making software imaging solutions freely available. % |
| 23 | % % |
| 24 | % You may not use this file except in compliance with the License. You may % |
| 25 | % obtain a copy of the License at % |
| 26 | % % |
| 27 | % http://www.imagemagick.org/script/license.php % |
| 28 | % % |
| 29 | % Unless required by applicable law or agreed to in writing, software % |
| 30 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 31 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 32 | % See the License for the specific language governing permissions and % |
| 33 | % limitations under the License. % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/artifact.h" |
| 45 | #include "MagickCore/attribute.h" |
| 46 | #include "MagickCore/blob.h" |
| 47 | #include "MagickCore/blob-private.h" |
| 48 | #include "MagickCore/cache.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #include "MagickCore/constitute.h" |
| 53 | #include "MagickCore/enhance.h" |
| 54 | #include "MagickCore/exception.h" |
| 55 | #include "MagickCore/exception-private.h" |
| 56 | #include "MagickCore/image.h" |
| 57 | #include "MagickCore/image-private.h" |
| 58 | #include "MagickCore/list.h" |
| 59 | #include "MagickCore/log.h" |
| 60 | #include "MagickCore/magick.h" |
| 61 | #include "MagickCore/memory_.h" |
| 62 | #include "MagickCore/module.h" |
| 63 | #include "MagickCore/monitor-private.h" |
| 64 | #include "MagickCore/pixel.h" |
| 65 | #include "MagickCore/pixel-accessor.h" |
| 66 | #include "MagickCore/profile.h" |
| 67 | #include "MagickCore/property.h" |
| 68 | #include "MagickCore/quantum-private.h" |
| 69 | #include "MagickCore/static.h" |
| 70 | #include "MagickCore/string_.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 73 | Define declaractions. |
| 74 | */ |
cristy | aca3ec5 | 2010-03-02 14:55:01 +0000 | [diff] [blame] | 75 | #define MaxPSDChannels 56 |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 76 | #define PSDQuantum(x) (((ssize_t) (x)+1) & -2) |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | Enumerated declaractions. |
| 80 | */ |
| 81 | typedef enum |
| 82 | { |
| 83 | BitmapMode = 0, |
| 84 | GrayscaleMode = 1, |
| 85 | IndexedMode = 2, |
| 86 | RGBMode = 3, |
| 87 | CMYKMode = 4, |
| 88 | MultichannelMode = 7, |
| 89 | DuotoneMode = 8, |
| 90 | LabMode = 9 |
| 91 | } PSDImageType; |
| 92 | |
| 93 | /* |
| 94 | Typedef declaractions. |
| 95 | */ |
| 96 | typedef struct _ChannelInfo |
| 97 | { |
| 98 | short int |
| 99 | type; |
| 100 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 101 | size_t |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 102 | size; |
| 103 | } ChannelInfo; |
| 104 | |
| 105 | typedef struct _LayerInfo |
| 106 | { |
| 107 | RectangleInfo |
| 108 | page, |
| 109 | mask; |
| 110 | |
| 111 | unsigned short |
| 112 | channels; |
| 113 | |
| 114 | ChannelInfo |
| 115 | channel_info[MaxPSDChannels]; |
| 116 | |
| 117 | char |
| 118 | blendkey[4]; |
| 119 | |
| 120 | Quantum |
| 121 | opacity; |
| 122 | |
| 123 | unsigned char |
| 124 | clipping, |
| 125 | visible, |
| 126 | flags; |
| 127 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 128 | size_t |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 129 | offset_x, |
| 130 | offset_y; |
| 131 | |
| 132 | unsigned char |
| 133 | name[256]; |
| 134 | |
| 135 | Image |
| 136 | *image; |
| 137 | } LayerInfo; |
| 138 | |
| 139 | typedef struct _PSDInfo |
| 140 | { |
| 141 | char |
| 142 | signature[4]; |
| 143 | |
| 144 | unsigned short |
| 145 | channels, |
| 146 | version; |
| 147 | |
| 148 | unsigned char |
| 149 | reserved[6]; |
| 150 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 151 | size_t |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 152 | rows, |
| 153 | columns; |
| 154 | |
| 155 | unsigned short |
| 156 | depth, |
| 157 | mode; |
| 158 | } PSDInfo; |
| 159 | |
| 160 | /* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | Forward declarations. |
| 162 | */ |
| 163 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 164 | WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 168 | % % |
| 169 | % % |
| 170 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | % I s P S D % |
| 172 | % % |
| 173 | % % |
| 174 | % % |
| 175 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 176 | % |
| 177 | % IsPSD()() returns MagickTrue if the image format type, identified by the |
| 178 | % magick string, is PSD. |
| 179 | % |
| 180 | % The format of the IsPSD method is: |
| 181 | % |
| 182 | % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length) |
| 183 | % |
| 184 | % A description of each parameter follows: |
| 185 | % |
| 186 | % o magick: compare image format pattern against these bytes. |
| 187 | % |
| 188 | % o length: Specifies the length of the magick string. |
| 189 | % |
| 190 | */ |
| 191 | static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length) |
| 192 | { |
| 193 | if (length < 4) |
| 194 | return(MagickFalse); |
| 195 | if (LocaleNCompare((const char *) magick,"8BPS",4) == 0) |
| 196 | return(MagickTrue); |
| 197 | return(MagickFalse); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 202 | % % |
| 203 | % % |
| 204 | % % |
| 205 | % R e a d P S D I m a g e % |
| 206 | % % |
| 207 | % % |
| 208 | % % |
| 209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 210 | % |
| 211 | % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It |
| 212 | % allocates the memory necessary for the new Image structure and returns a |
| 213 | % pointer to the new image. |
| 214 | % |
| 215 | % The format of the ReadPSDImage method is: |
| 216 | % |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 217 | % Image *ReadPSDImage(image_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | % |
| 219 | % A description of each parameter follows: |
| 220 | % |
| 221 | % o image_info: the image info. |
| 222 | % |
| 223 | % o exception: return any errors or warnings in this structure. |
| 224 | % |
| 225 | */ |
| 226 | |
cristy | 19eb641 | 2010-04-23 14:42:29 +0000 | [diff] [blame] | 227 | static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op) |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 228 | { |
| 229 | const char |
| 230 | *blend_mode; |
| 231 | |
cristy | 19eb641 | 2010-04-23 14:42:29 +0000 | [diff] [blame] | 232 | switch (op) |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 233 | { |
| 234 | case OverCompositeOp: blend_mode = "norm"; break; |
| 235 | case MultiplyCompositeOp: blend_mode = "mul "; break; |
| 236 | case DissolveCompositeOp: blend_mode = "diss"; break; |
| 237 | case DifferenceCompositeOp: blend_mode = "diff"; break; |
| 238 | case DarkenCompositeOp: blend_mode = "dark"; break; |
| 239 | case LightenCompositeOp: blend_mode = "lite"; break; |
| 240 | case HueCompositeOp: blend_mode = "hue "; break; |
| 241 | case SaturateCompositeOp: blend_mode = "sat "; break; |
| 242 | case ColorizeCompositeOp: blend_mode = "colr"; break; |
| 243 | case LuminizeCompositeOp: blend_mode = "lum "; break; |
| 244 | case ScreenCompositeOp: blend_mode = "scrn"; break; |
| 245 | case OverlayCompositeOp: blend_mode = "over"; break; |
| 246 | default: |
| 247 | blend_mode = "norm"; |
| 248 | } |
| 249 | return(blend_mode); |
| 250 | } |
| 251 | |
| 252 | static ssize_t DecodePSDPixels(const size_t number_compact_pixels, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 253 | const unsigned char *compact_pixels,const ssize_t depth, |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 254 | const size_t number_pixels,unsigned char *pixels) |
| 255 | { |
| 256 | int |
| 257 | pixel; |
| 258 | |
| 259 | register ssize_t |
| 260 | i, |
| 261 | j; |
| 262 | |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 263 | size_t |
| 264 | length; |
| 265 | |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 266 | ssize_t |
| 267 | packets; |
| 268 | |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 269 | packets=(ssize_t) number_compact_pixels; |
| 270 | for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); ) |
| 271 | { |
| 272 | length=(*compact_pixels++); |
| 273 | packets--; |
| 274 | if (length == 128) |
| 275 | continue; |
| 276 | if (length > 128) |
| 277 | { |
| 278 | length=256-length+1; |
| 279 | pixel=(*compact_pixels++); |
| 280 | packets--; |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 281 | for (j=0; j < (ssize_t) length; j++) |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 282 | { |
| 283 | switch (depth) |
| 284 | { |
| 285 | case 1: |
| 286 | { |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 287 | *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U; |
| 288 | *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U; |
| 289 | *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U; |
| 290 | *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U; |
| 291 | *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U; |
| 292 | *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U; |
| 293 | *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U; |
| 294 | *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 295 | i+=8; |
| 296 | break; |
| 297 | } |
| 298 | case 4: |
| 299 | { |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 300 | *pixels++=(unsigned char) ((pixel >> 4) & 0xff); |
| 301 | *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff); |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 302 | i+=2; |
| 303 | break; |
| 304 | } |
| 305 | case 2: |
| 306 | { |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 307 | *pixels++=(unsigned char) ((pixel >> 6) & 0x03); |
| 308 | *pixels++=(unsigned char) ((pixel >> 4) & 0x03); |
| 309 | *pixels++=(unsigned char) ((pixel >> 2) & 0x03); |
| 310 | *pixels++=(unsigned char) ((pixel & 0x03) & 0x03); |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 311 | i+=4; |
| 312 | break; |
| 313 | } |
| 314 | default: |
| 315 | { |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 316 | *pixels++=(unsigned char) pixel; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 317 | i++; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | continue; |
| 323 | } |
| 324 | length++; |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 325 | for (j=0; j < (ssize_t) length; j++) |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 326 | { |
| 327 | switch (depth) |
| 328 | { |
| 329 | case 1: |
| 330 | { |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 331 | *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U; |
| 332 | *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U; |
| 333 | *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U; |
| 334 | *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U; |
| 335 | *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U; |
| 336 | *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U; |
| 337 | *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U; |
| 338 | *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 339 | i+=8; |
| 340 | break; |
| 341 | } |
| 342 | case 4: |
| 343 | { |
cristy | 618a966 | 2010-03-23 13:59:54 +0000 | [diff] [blame] | 344 | *pixels++=(*compact_pixels >> 4) & 0xff; |
| 345 | *pixels++=(*compact_pixels & 0x0f) & 0xff; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 346 | i+=2; |
| 347 | break; |
| 348 | } |
| 349 | case 2: |
| 350 | { |
cristy | 618a966 | 2010-03-23 13:59:54 +0000 | [diff] [blame] | 351 | *pixels++=(*compact_pixels >> 6) & 0x03; |
| 352 | *pixels++=(*compact_pixels >> 4) & 0x03; |
| 353 | *pixels++=(*compact_pixels >> 2) & 0x03; |
| 354 | *pixels++=(*compact_pixels & 0x03) & 0x03; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 355 | i+=4; |
| 356 | break; |
| 357 | } |
| 358 | default: |
| 359 | { |
| 360 | *pixels++=(*compact_pixels); |
| 361 | i++; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | compact_pixels++; |
| 366 | } |
| 367 | } |
| 368 | return(i); |
| 369 | } |
| 370 | |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 371 | static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image) |
| 372 | { |
| 373 | if (psd_info->version == 1) |
| 374 | return((MagickOffsetType) ReadBlobMSBShort(image)); |
| 375 | return((MagickOffsetType) ReadBlobMSBLong(image)); |
| 376 | } |
| 377 | |
| 378 | static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image) |
| 379 | { |
| 380 | if (psd_info->version == 1) |
| 381 | return((MagickSizeType) ReadBlobMSBLong(image)); |
| 382 | return((MagickSizeType) ReadBlobMSBLongLong(image)); |
| 383 | } |
| 384 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 385 | static inline ssize_t MagickAbsoluteValue(const ssize_t x) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | { |
| 387 | if (x < 0) |
| 388 | return(-x); |
| 389 | return(x); |
| 390 | } |
| 391 | |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 392 | static const char *ModeToString(PSDImageType type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 393 | { |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 394 | switch (type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 395 | { |
| 396 | case BitmapMode: return "Bitmap"; |
| 397 | case GrayscaleMode: return "Grayscale"; |
| 398 | case IndexedMode: return "Indexed"; |
| 399 | case RGBMode: return "RGB"; |
| 400 | case CMYKMode: return "CMYK"; |
| 401 | case MultichannelMode: return "Multichannel"; |
| 402 | case DuotoneMode: return "Duotone"; |
| 403 | case LabMode: return "L*A*B"; |
| 404 | default: return "unknown"; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static MagickBooleanType ParseImageResourceBlocks(Image *image, |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 409 | const unsigned char *blocks,size_t length,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 410 | { |
| 411 | const unsigned char |
| 412 | *p; |
| 413 | |
| 414 | StringInfo |
| 415 | *profile; |
| 416 | |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 417 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 418 | count, |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 419 | long_sans; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 420 | |
| 421 | unsigned short |
| 422 | id, |
| 423 | short_sans; |
| 424 | |
| 425 | if (length < 16) |
| 426 | return(MagickFalse); |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 427 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 428 | SetStringInfoDatum(profile,blocks); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 429 | (void) SetImageProfile(image,"8bim",profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 430 | profile=DestroyStringInfo(profile); |
| 431 | for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); ) |
| 432 | { |
| 433 | if (LocaleNCompare((const char *) p,"8BIM",4) != 0) |
| 434 | break; |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 435 | p=PushLongPixel(MSBEndian,p,&long_sans); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 436 | p=PushShortPixel(MSBEndian,p,&id); |
| 437 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 438 | p=PushLongPixel(MSBEndian,p,&count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 439 | switch (id) |
| 440 | { |
| 441 | case 0x03ed: |
| 442 | { |
cristy | 1e4a80b | 2010-05-14 16:18:26 +0000 | [diff] [blame] | 443 | char |
| 444 | value[MaxTextExtent]; |
| 445 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | unsigned short |
| 447 | resolution; |
| 448 | |
| 449 | /* |
| 450 | Resolution info. |
| 451 | */ |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 452 | p=PushShortPixel(MSBEndian,p,&resolution); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 453 | image->resolution.x=(double) resolution; |
| 454 | (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 455 | (void) SetImageProperty(image,"tiff:XResolution",value,exception); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 456 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 457 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 458 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 459 | p=PushShortPixel(MSBEndian,p,&resolution); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 460 | image->resolution.y=(double) resolution; |
| 461 | (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 462 | (void) SetImageProperty(image,"tiff:YResolution",value,exception); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 463 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 464 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 465 | p=PushShortPixel(MSBEndian,p,&short_sans); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | default: |
| 469 | { |
| 470 | p+=count; |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | if ((count & 0x01) != 0) |
| 475 | p++; |
| 476 | } |
| 477 | return(MagickTrue); |
| 478 | } |
| 479 | |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 480 | static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 481 | { |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 482 | if (mode == (const char *) NULL) |
| 483 | return(OverCompositeOp); |
| 484 | if (LocaleNCompare(mode,"norm",4) == 0) |
| 485 | return(OverCompositeOp); |
| 486 | if (LocaleNCompare(mode,"mul ",4) == 0) |
| 487 | return(MultiplyCompositeOp); |
| 488 | if (LocaleNCompare(mode,"diss",4) == 0) |
| 489 | return(DissolveCompositeOp); |
| 490 | if (LocaleNCompare(mode,"diff",4) == 0) |
| 491 | return(DifferenceCompositeOp); |
| 492 | if (LocaleNCompare(mode,"dark",4) == 0) |
| 493 | return(DarkenCompositeOp); |
| 494 | if (LocaleNCompare(mode,"lite",4) == 0) |
| 495 | return(LightenCompositeOp); |
| 496 | if (LocaleNCompare(mode,"hue ",4) == 0) |
| 497 | return(HueCompositeOp); |
| 498 | if (LocaleNCompare(mode,"sat ",4) == 0) |
| 499 | return(SaturateCompositeOp); |
| 500 | if (LocaleNCompare(mode,"colr",4) == 0) |
| 501 | return(ColorizeCompositeOp); |
| 502 | if (LocaleNCompare(mode,"lum ",4) == 0) |
| 503 | return(LuminizeCompositeOp); |
| 504 | if (LocaleNCompare(mode,"scrn",4) == 0) |
| 505 | return(ScreenCompositeOp); |
| 506 | if (LocaleNCompare(mode,"over",4) == 0) |
| 507 | return(OverlayCompositeOp); |
| 508 | if (LocaleNCompare(mode,"hLit",4) == 0) |
| 509 | return(OverCompositeOp); |
| 510 | if (LocaleNCompare(mode,"sLit",4) == 0) |
| 511 | return(OverCompositeOp); |
| 512 | if (LocaleNCompare(mode,"smud",4) == 0) |
| 513 | return(OverCompositeOp); |
| 514 | if (LocaleNCompare(mode,"div ",4) == 0) |
| 515 | return(OverCompositeOp); |
| 516 | if (LocaleNCompare(mode,"idiv",4) == 0) |
| 517 | return(OverCompositeOp); |
| 518 | return(OverCompositeOp); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 519 | } |
| 520 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 521 | static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels, |
| 522 | const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 523 | { |
cristy | d05dca1 | 2010-07-25 02:32:32 +0000 | [diff] [blame] | 524 | ColorspaceType |
| 525 | colorspace; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 526 | |
| 527 | Quantum |
| 528 | pixel; |
| 529 | |
cristy | 7753b2a | 2011-02-19 18:36:52 +0000 | [diff] [blame] | 530 | register const unsigned char |
| 531 | *p; |
| 532 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 533 | register Quantum |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 534 | *q; |
| 535 | |
cristy | 7753b2a | 2011-02-19 18:36:52 +0000 | [diff] [blame] | 536 | register ssize_t |
| 537 | x; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 538 | |
| 539 | size_t |
| 540 | packet_size; |
| 541 | |
| 542 | ssize_t |
cristy | d05dca1 | 2010-07-25 02:32:32 +0000 | [diff] [blame] | 543 | count, |
| 544 | y; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 545 | |
| 546 | unsigned char |
| 547 | *compact_pixels, |
| 548 | *pixels; |
| 549 | |
| 550 | unsigned short |
| 551 | nibble; |
| 552 | |
| 553 | packet_size=1; |
| 554 | if (image->storage_class == PseudoClass) |
| 555 | { |
| 556 | if (image->colors > 256) |
| 557 | packet_size++; |
| 558 | else |
| 559 | if (image->depth > 8) |
| 560 | packet_size++; |
| 561 | } |
| 562 | else |
| 563 | if (image->depth > 8) |
| 564 | packet_size++; |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 565 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size* |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 566 | sizeof(*pixels)); |
| 567 | if (pixels == (unsigned char *) NULL) |
| 568 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 569 | image->filename); |
cristy | cd08177 | 2010-03-22 17:19:12 +0000 | [diff] [blame] | 570 | (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels)); |
| 571 | compact_pixels=(unsigned char *) NULL; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 572 | if (image->compression == RLECompression) |
| 573 | { |
cristy | e195f26 | 2010-04-16 18:12:35 +0000 | [diff] [blame] | 574 | size_t |
| 575 | length; |
| 576 | |
| 577 | length=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 578 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | e195f26 | 2010-04-16 18:12:35 +0000 | [diff] [blame] | 579 | if ((MagickOffsetType) length < offsets[y]) |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 580 | length=(size_t) offsets[y]; |
cristy | e195f26 | 2010-04-16 18:12:35 +0000 | [diff] [blame] | 581 | compact_pixels=(unsigned char *) AcquireQuantumMemory(length, |
| 582 | sizeof(*pixels)); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 583 | if (compact_pixels == (unsigned char *) NULL) |
| 584 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 585 | image->filename); |
cristy | 0132ba8 | 2010-04-16 23:34:02 +0000 | [diff] [blame] | 586 | (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels)); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 587 | } |
cristy | d05dca1 | 2010-07-25 02:32:32 +0000 | [diff] [blame] | 588 | colorspace=image->colorspace; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 589 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 590 | { |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 591 | if (image->depth == 1) |
| 592 | { |
cristy | b6cabe8 | 2011-03-18 13:25:59 +0000 | [diff] [blame] | 593 | if (image->compression != RLECompression) |
| 594 | count=ReadBlob(image,(image->columns+7)/8,pixels); |
| 595 | else |
| 596 | { |
| 597 | count=ReadBlob(image,(size_t) offsets[y],compact_pixels); |
| 598 | if (count != (ssize_t) offsets[y]) |
| 599 | break; |
| 600 | count=DecodePSDPixels((size_t) offsets[y],compact_pixels, |
| 601 | (ssize_t) 123456,(size_t) ((image->columns+7)/8),pixels); |
| 602 | } |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 603 | if (count < (ssize_t) ((image->columns+7)/8)) |
| 604 | break; |
| 605 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 606 | else |
| 607 | { |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 608 | if (image->compression != RLECompression) |
| 609 | count=ReadBlob(image,packet_size*image->columns,pixels); |
| 610 | else |
| 611 | { |
| 612 | count=ReadBlob(image,(size_t) offsets[y],compact_pixels); |
| 613 | if (count != (ssize_t) offsets[y]) |
| 614 | break; |
| 615 | count=DecodePSDPixels((size_t) offsets[y],compact_pixels, |
| 616 | (ssize_t) image->depth,packet_size*image->columns,pixels); |
| 617 | } |
| 618 | if (count < (ssize_t) (packet_size*image->columns)) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 619 | break; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 620 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 621 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 622 | if (q == (Quantum *) NULL) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 623 | break; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 624 | p=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 625 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 626 | { |
| 627 | if (packet_size == 1) |
| 628 | pixel=ScaleCharToQuantum(*p++); |
| 629 | else |
| 630 | { |
| 631 | p=PushShortPixel(MSBEndian,p,&nibble); |
| 632 | pixel=ScaleShortToQuantum(nibble); |
| 633 | } |
| 634 | switch (type) |
| 635 | { |
| 636 | case -1: |
| 637 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 638 | SetPixelAlpha(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | case 0: |
| 642 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 643 | SetPixelRed(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 644 | if (channels == 1) |
| 645 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 646 | SetPixelGreen(image,GetPixelRed(image,q),q); |
| 647 | SetPixelBlue(image,GetPixelRed(image,q),q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 648 | } |
| 649 | if (image->storage_class == PseudoClass) |
| 650 | { |
| 651 | if (packet_size == 1) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 652 | SetPixelIndex(image,ScaleQuantumToChar(pixel),q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 653 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 654 | SetPixelIndex(image,ScaleQuantumToShort(pixel),q); |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 655 | SetPixelInfoPixel(image,image->colormap+(ssize_t) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 656 | GetPixelIndex(image,q),q); |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 657 | if (image->depth == 1) |
| 658 | { |
| 659 | ssize_t |
cristy | b6cabe8 | 2011-03-18 13:25:59 +0000 | [diff] [blame] | 660 | bit, |
| 661 | number_bits; |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 662 | |
cristy | b6cabe8 | 2011-03-18 13:25:59 +0000 | [diff] [blame] | 663 | number_bits=image->columns-x; |
| 664 | if (number_bits > 8) |
| 665 | number_bits=8; |
| 666 | for (bit=0; bit < number_bits; bit++) |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 667 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 668 | SetPixelIndex(image,(((unsigned char) pixel) & |
| 669 | (0x01 << (7-bit))) != 0 ? 0 : 255,q); |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 670 | SetPixelInfoPixel(image,image->colormap+(ssize_t) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 671 | GetPixelIndex(image,q),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 672 | q+=GetPixelChannels(image); |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 673 | x++; |
| 674 | } |
cristy | 3b00408 | 2010-08-12 19:56:38 +0000 | [diff] [blame] | 675 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 676 | } |
| 677 | break; |
| 678 | } |
| 679 | case 1: |
| 680 | { |
| 681 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 682 | SetPixelAlpha(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 683 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 684 | SetPixelGreen(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | case 2: |
| 688 | { |
cristy | dede49f | 2010-08-20 20:26:26 +0000 | [diff] [blame] | 689 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 690 | SetPixelAlpha(image,pixel,q); |
cristy | dede49f | 2010-08-20 20:26:26 +0000 | [diff] [blame] | 691 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 692 | SetPixelBlue(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | case 3: |
| 696 | { |
| 697 | if (image->colorspace == CMYKColorspace) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 698 | SetPixelBlack(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 699 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 700 | SetPixelAlpha(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | case 4: |
| 704 | { |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 705 | if ((IsRGBColorspace(image->colorspace) == MagickTrue) && (channels > 3)) |
cristy | a60c020 | 2010-07-31 21:36:45 +0000 | [diff] [blame] | 706 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 707 | SetPixelAlpha(image,pixel,q); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | default: |
| 711 | break; |
| 712 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 713 | q+=GetPixelChannels(image); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 714 | } |
| 715 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 716 | break; |
| 717 | } |
cristy | d05dca1 | 2010-07-25 02:32:32 +0000 | [diff] [blame] | 718 | image->colorspace=colorspace; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 719 | if (image->compression == RLECompression) |
| 720 | compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels); |
| 721 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 722 | return(MagickTrue); |
| 723 | } |
| 724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 725 | static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 726 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 727 | char |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 728 | message[MaxTextExtent], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 729 | type[4]; |
| 730 | |
| 731 | Image |
| 732 | *image; |
| 733 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 734 | LayerInfo |
| 735 | *layer_info; |
| 736 | |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 737 | MagickBooleanType |
| 738 | status; |
| 739 | |
| 740 | MagickOffsetType |
| 741 | offset, |
| 742 | *offsets; |
| 743 | |
| 744 | MagickSizeType |
| 745 | combinedlength, |
| 746 | length, |
| 747 | size; |
| 748 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | PSDInfo |
| 750 | psd_info; |
| 751 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 752 | register Quantum |
cristy | dede49f | 2010-08-20 20:26:26 +0000 | [diff] [blame] | 753 | *q; |
| 754 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 755 | register ssize_t |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 756 | i, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | x; |
| 758 | |
cristy | dc05fc2 | 2011-01-22 19:43:15 +0000 | [diff] [blame] | 759 | size_t |
| 760 | mask_size, |
| 761 | skip_first_alpha = 0; |
| 762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | ssize_t |
cristy | dc05fc2 | 2011-01-22 19:43:15 +0000 | [diff] [blame] | 764 | count, |
| 765 | j, |
| 766 | number_layers, |
| 767 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 768 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | unsigned char |
| 770 | *data; |
| 771 | |
| 772 | unsigned short |
| 773 | compression; |
| 774 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 775 | /* |
| 776 | Open image file. |
| 777 | */ |
| 778 | assert(image_info != (const ImageInfo *) NULL); |
| 779 | assert(image_info->signature == MagickSignature); |
| 780 | if (image_info->debug != MagickFalse) |
| 781 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 782 | image_info->filename); |
| 783 | assert(exception != (ExceptionInfo *) NULL); |
| 784 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 785 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 787 | if (status == MagickFalse) |
| 788 | { |
| 789 | image=DestroyImageList(image); |
| 790 | return((Image *) NULL); |
| 791 | } |
| 792 | /* |
| 793 | Read image header. |
| 794 | */ |
| 795 | count=ReadBlob(image,4,(unsigned char *) psd_info.signature); |
| 796 | psd_info.version=ReadBlobMSBShort(image); |
cristy | 50aea4a | 2010-03-09 17:37:44 +0000 | [diff] [blame] | 797 | if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) || |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 798 | ((psd_info.version != 1) && (psd_info.version != 2))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 800 | count=ReadBlob(image,6,psd_info.reserved); |
| 801 | psd_info.channels=ReadBlobMSBShort(image); |
| 802 | if (psd_info.channels > MaxPSDChannels) |
| 803 | ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded"); |
| 804 | psd_info.rows=ReadBlobMSBLong(image); |
| 805 | psd_info.columns=ReadBlobMSBLong(image); |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 806 | if ((psd_info.version == 1) && ((psd_info.rows > 30000) || |
| 807 | (psd_info.columns > 30000))) |
| 808 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 809 | psd_info.depth=ReadBlobMSBShort(image); |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 810 | if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16)) |
| 811 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | psd_info.mode=ReadBlobMSBShort(image); |
| 813 | if (image->debug != MagickFalse) |
| 814 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 815 | " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s", |
| 816 | (double) psd_info.columns,(double) psd_info.rows,(double) |
| 817 | psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType) |
| 818 | psd_info.mode)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 819 | /* |
| 820 | Initialize image. |
| 821 | */ |
| 822 | image->depth=psd_info.depth; |
| 823 | image->columns=psd_info.columns; |
| 824 | image->rows=psd_info.rows; |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 825 | if (SetImageBackgroundColor(image,exception) == MagickFalse) |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 826 | { |
cristy | 95524f9 | 2010-02-16 18:44:34 +0000 | [diff] [blame] | 827 | image=DestroyImageList(image); |
| 828 | return((Image *) NULL); |
| 829 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 830 | image->matte=psd_info.channels >= 4 ? MagickTrue : MagickFalse; |
| 831 | if (psd_info.mode == LabMode) |
| 832 | image->colorspace=LabColorspace; |
| 833 | if (psd_info.mode == CMYKMode) |
| 834 | { |
| 835 | image->colorspace=CMYKColorspace; |
| 836 | image->matte=psd_info.channels >= 5 ? MagickTrue : MagickFalse; |
| 837 | } |
| 838 | if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) || |
| 839 | (psd_info.mode == DuotoneMode)) |
| 840 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 841 | if (AcquireImageColormap(image,256,exception) == MagickFalse) |
cristy | 52cf7f1 | 2010-02-07 18:07:56 +0000 | [diff] [blame] | 842 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse; |
| 844 | if (image->debug != MagickFalse) |
| 845 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | dede49f | 2010-08-20 20:26:26 +0000 | [diff] [blame] | 846 | " Image colormap allocated"); |
cristy | 0132ba8 | 2010-04-16 23:34:02 +0000 | [diff] [blame] | 847 | image->colorspace=GRAYColorspace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 848 | } |
| 849 | if (image->debug != MagickFalse) |
| 850 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 851 | image->matte ? " image has matte" : " image has no matte"); |
| 852 | /* |
| 853 | Read PSD raster colormap only present for indexed and duotone images. |
| 854 | */ |
| 855 | length=ReadBlobMSBLong(image); |
| 856 | if (length != 0) |
| 857 | { |
| 858 | if (image->debug != MagickFalse) |
| 859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 860 | " reading colormap"); |
| 861 | if (psd_info.mode == DuotoneMode) |
| 862 | { |
| 863 | /* |
| 864 | Duotone image data; the format of this data is undocumented. |
| 865 | */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 866 | data=(unsigned char *) AcquireQuantumMemory((size_t) length, |
| 867 | sizeof(*data)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 868 | if (data == (unsigned char *) NULL) |
| 869 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 870 | count=ReadBlob(image,(size_t) length,data); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 871 | data=(unsigned char *) RelinquishMagickMemory(data); |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | /* |
| 876 | Read PSD raster colormap. |
| 877 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 878 | if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 879 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 880 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 881 | image->colormap[i].red=ScaleCharToQuantum((unsigned char) |
| 882 | ReadBlobByte(image)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 883 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 884 | image->colormap[i].green=ScaleCharToQuantum((unsigned char) |
| 885 | ReadBlobByte(image)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 886 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | image->colormap[i].blue=ScaleCharToQuantum((unsigned char) |
| 888 | ReadBlobByte(image)); |
| 889 | image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse; |
| 890 | } |
| 891 | } |
| 892 | length=ReadBlobMSBLong(image); |
| 893 | if (length != 0) |
| 894 | { |
| 895 | unsigned char |
| 896 | *blocks; |
| 897 | |
| 898 | /* |
| 899 | Image resources block. |
| 900 | */ |
| 901 | if (image->debug != MagickFalse) |
| 902 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 2b9582a | 2011-07-04 17:38:56 +0000 | [diff] [blame] | 903 | " reading image resource blocks - %.20g bytes",(double) |
| 904 | ((MagickOffsetType) length)); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 905 | blocks=(unsigned char *) AcquireQuantumMemory((size_t) length, |
| 906 | sizeof(*blocks)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 907 | if (blocks == (unsigned char *) NULL) |
| 908 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 909 | count=ReadBlob(image,(size_t) length,blocks); |
| 910 | if ((count != (ssize_t) length) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | (LocaleNCompare((char *) blocks,"8BIM",4) != 0)) |
| 912 | { |
| 913 | blocks=(unsigned char *) RelinquishMagickMemory(blocks); |
| 914 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 915 | } |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 916 | (void) ParseImageResourceBlocks(image,blocks,(size_t) length, |
| 917 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | blocks=(unsigned char *) RelinquishMagickMemory(blocks); |
| 919 | } |
| 920 | /* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | Layer and mask block. |
| 922 | */ |
| 923 | layer_info=(LayerInfo *) NULL; |
cristy | 4689cf0 | 2010-02-17 21:15:45 +0000 | [diff] [blame] | 924 | number_layers=1; |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 925 | length=GetPSDSize(&psd_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 926 | if (length == 8) |
| 927 | { |
| 928 | length=ReadBlobMSBLong(image); |
| 929 | length=ReadBlobMSBLong(image); |
| 930 | } |
| 931 | if ((image_info->number_scenes == 1) && (image_info->scene == 0)) |
cristy | d429702 | 2010-09-16 22:59:09 +0000 | [diff] [blame] | 932 | { |
| 933 | if (DiscardBlobBytes(image,length) == MagickFalse) |
| 934 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 935 | image->filename); |
| 936 | length=0; |
| 937 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | if (length == 0) |
| 939 | { |
| 940 | if (image->debug != MagickFalse) |
| 941 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 942 | " image has no layers"); |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | offset=TellBlob(image); |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 947 | size=GetPSDSize(&psd_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | if (size == 0) |
| 949 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 950 | size_t |
cristy | 2d3d87f | 2010-03-01 00:23:08 +0000 | [diff] [blame] | 951 | quantum; |
| 952 | |
cristy | 9fb55e8 | 2011-02-27 23:12:47 +0000 | [diff] [blame] | 953 | unsigned long |
| 954 | tag; |
| 955 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 956 | /* |
| 957 | Skip layers & masks. |
| 958 | */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 959 | quantum=psd_info.version == 1 ? 4UL : 8UL; |
cristy | 9fb55e8 | 2011-02-27 23:12:47 +0000 | [diff] [blame] | 960 | tag=ReadBlobMSBLong(image); |
cristy | 6c70aec | 2011-03-04 20:48:09 +0000 | [diff] [blame] | 961 | (void) tag; |
| 962 | count=ReadBlob(image,4,(unsigned char *) type); |
| 963 | if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0)) |
cristy | 9fb55e8 | 2011-02-27 23:12:47 +0000 | [diff] [blame] | 964 | { |
| 965 | if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse) |
| 966 | ThrowFileException(exception,CorruptImageError, |
| 967 | "UnexpectedEndOfFile",image->filename); |
| 968 | } |
| 969 | else |
| 970 | { |
cristy | 6c70aec | 2011-03-04 20:48:09 +0000 | [diff] [blame] | 971 | count=ReadBlob(image,4,(unsigned char *) type); |
| 972 | if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0)) |
cristy | 9fb55e8 | 2011-02-27 23:12:47 +0000 | [diff] [blame] | 973 | size=GetPSDSize(&psd_info,image); |
| 974 | else |
| 975 | if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse) |
| 976 | ThrowFileException(exception,CorruptImageError, |
| 977 | "UnexpectedEndOfFile",image->filename); |
| 978 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 979 | } |
cristy | 9fb55e8 | 2011-02-27 23:12:47 +0000 | [diff] [blame] | 980 | if (size != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 981 | { |
| 982 | MagickOffsetType |
| 983 | layer_offset; |
| 984 | |
| 985 | layer_offset=offset+length; |
| 986 | number_layers=(short) ReadBlobMSBShort(image); |
| 987 | if (image->debug != MagickFalse) |
| 988 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 989 | " image contains %.20g layers",(double) number_layers); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 990 | if (number_layers < 0) |
| 991 | { |
| 992 | /* |
| 993 | Weird hack in PSD format to ignore first alpha channel. |
| 994 | */ |
| 995 | skip_first_alpha=1; |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 996 | (void) skip_first_alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | number_layers=MagickAbsoluteValue(number_layers); |
| 998 | if (image->debug != MagickFalse) |
| 999 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1000 | " negative layer count corrected for"); |
| 1001 | } |
| 1002 | layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers, |
| 1003 | sizeof(*layer_info)); |
| 1004 | if (layer_info == (LayerInfo *) NULL) |
| 1005 | { |
| 1006 | if (image->debug != MagickFalse) |
| 1007 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1008 | " allocation of LayerInfo failed"); |
| 1009 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1010 | } |
| 1011 | (void) ResetMagickMemory(layer_info,0,(size_t) number_layers* |
| 1012 | sizeof(*layer_info)); |
| 1013 | for (i=0; i < number_layers; i++) |
| 1014 | { |
cristy | 4484e33 | 2010-10-08 23:46:21 +0000 | [diff] [blame] | 1015 | int |
| 1016 | x, |
| 1017 | y; |
| 1018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1019 | if (image->debug != MagickFalse) |
| 1020 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1021 | " reading layer #%.20g",(double) i+1); |
cristy | 6cff05d | 2010-09-02 11:22:46 +0000 | [diff] [blame] | 1022 | layer_info[i].page.y=(int) ReadBlobMSBLong(image); |
| 1023 | layer_info[i].page.x=(int) ReadBlobMSBLong(image); |
cristy | 4484e33 | 2010-10-08 23:46:21 +0000 | [diff] [blame] | 1024 | y=(int) ReadBlobMSBLong(image); |
| 1025 | x=(int) ReadBlobMSBLong(image); |
| 1026 | layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x); |
| 1027 | layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1028 | layer_info[i].channels=ReadBlobMSBShort(image); |
| 1029 | if (layer_info[i].channels > MaxPSDChannels) |
| 1030 | ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded"); |
| 1031 | if (image->debug != MagickFalse) |
| 1032 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1033 | " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g", |
| 1034 | (double) layer_info[i].page.x,(double) layer_info[i].page.y, |
| 1035 | (double) layer_info[i].page.height,(double) |
| 1036 | layer_info[i].page.width,(double) layer_info[i].channels); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1037 | for (j=0; j < (ssize_t) layer_info[i].channels; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1038 | { |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1039 | layer_info[i].channel_info[j].type=(short) |
| 1040 | ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1041 | layer_info[i].channel_info[j].size=(size_t) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1042 | GetPSDSize(&psd_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1043 | if (image->debug != MagickFalse) |
| 1044 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1045 | " channel[%.20g]: type=%.20g, size=%.20g",(double) j, |
| 1046 | (double) layer_info[i].channel_info[j].type, |
| 1047 | (double) layer_info[i].channel_info[j].size); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1048 | } |
| 1049 | count=ReadBlob(image,4,(unsigned char *) type); |
| 1050 | if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0)) |
| 1051 | { |
| 1052 | if (image->debug != MagickFalse) |
| 1053 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1054 | " layer type was %.4s instead of 8BIM", type); |
| 1055 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1056 | } |
| 1057 | count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1058 | layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char) |
| 1059 | ReadBlobByte(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | layer_info[i].clipping=(unsigned char) ReadBlobByte(image); |
| 1061 | layer_info[i].flags=(unsigned char) ReadBlobByte(image); |
| 1062 | layer_info[i].visible=!(layer_info[i].flags & 0x02); |
| 1063 | if (image->debug != MagickFalse) |
| 1064 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1065 | " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s", |
| 1066 | layer_info[i].blendkey,(double) layer_info[i].opacity, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1067 | layer_info[i].clipping ? "true" : "false",layer_info[i].flags, |
| 1068 | layer_info[i].visible ? "true" : "false"); |
| 1069 | (void) ReadBlobByte(image); /* filler */ |
| 1070 | combinedlength=0; |
| 1071 | size=ReadBlobMSBLong(image); |
| 1072 | if (size != 0) |
| 1073 | { |
| 1074 | if (image->debug != MagickFalse) |
| 1075 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1076 | " layer contains additional info"); |
| 1077 | length=ReadBlobMSBLong(image); |
| 1078 | if (length != 0) |
| 1079 | { |
| 1080 | /* |
| 1081 | Layer mask info. |
| 1082 | */ |
cristy | 6cff05d | 2010-09-02 11:22:46 +0000 | [diff] [blame] | 1083 | layer_info[i].mask.y=(int) ReadBlobMSBLong(image); |
| 1084 | layer_info[i].mask.x=(int) ReadBlobMSBLong(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1085 | layer_info[i].mask.height=(size_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1086 | (ReadBlobMSBLong(image)-layer_info[i].mask.y); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1087 | layer_info[i].mask.width=(size_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1088 | (ReadBlobMSBLong(image)-layer_info[i].mask.x); |
| 1089 | if (image->debug != MagickFalse) |
| 1090 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1091 | " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g", |
cristy | d429702 | 2010-09-16 22:59:09 +0000 | [diff] [blame] | 1092 | (double) layer_info[i].mask.x,(double) layer_info[i].mask.y, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1093 | (double) layer_info[i].mask.width,(double) |
cristy | 2b9582a | 2011-07-04 17:38:56 +0000 | [diff] [blame] | 1094 | layer_info[i].mask.height,(double) |
| 1095 | ((MagickOffsetType) length-16)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1096 | /* |
| 1097 | Skip over the rest of the layer mask information. |
| 1098 | */ |
cristy | c1af14f | 2010-09-16 20:01:21 +0000 | [diff] [blame] | 1099 | if (DiscardBlobBytes(image,length-16) == MagickFalse) |
cristy | d429702 | 2010-09-16 22:59:09 +0000 | [diff] [blame] | 1100 | ThrowFileException(exception,CorruptImageError, |
| 1101 | "UnexpectedEndOfFile",image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1102 | } |
| 1103 | combinedlength+=length+4; /* +4 for length */ |
| 1104 | length=ReadBlobMSBLong(image); |
| 1105 | if (length != 0) |
| 1106 | { |
| 1107 | /* |
| 1108 | Layer blending ranges info. |
| 1109 | */ |
| 1110 | if (image->debug != MagickFalse) |
| 1111 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1112 | " layer blending ranges: length=%.20g",(double) |
cristy | 2b9582a | 2011-07-04 17:38:56 +0000 | [diff] [blame] | 1113 | ((MagickOffsetType) length)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1114 | /* |
| 1115 | We read it, but don't use it... |
| 1116 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1117 | for (j=0; j < (ssize_t) (length); j+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1118 | { |
| 1119 | size_t blend_source=ReadBlobMSBLong(image); |
| 1120 | size_t blend_dest=ReadBlobMSBLong(image); |
| 1121 | if (image->debug != MagickFalse) |
| 1122 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1123 | " source(%x), dest(%x)",(unsigned int) |
| 1124 | blend_source,(unsigned int) blend_dest); |
| 1125 | } |
| 1126 | } |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1127 | combinedlength+=length+4; |
| 1128 | /* |
| 1129 | Layer name. |
| 1130 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1131 | length=(size_t) ReadBlobByte(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1132 | for (j=0; j < (ssize_t) length; j++) |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1133 | layer_info[i].name[j]=(unsigned char) ReadBlobByte(image); |
| 1134 | layer_info[i].name[j]='\0'; |
| 1135 | if (image->debug != MagickFalse) |
| 1136 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1137 | " layer name: %s",layer_info[i].name); |
| 1138 | combinedlength+=length+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1139 | |
| 1140 | #if 0 /* still in development */ |
| 1141 | /* |
| 1142 | Adjustment layers and other stuff... |
| 1143 | */ |
| 1144 | { |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1145 | char alsig[4], alkey[4]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1146 | |
| 1147 | count=ReadBlob(image,4,alsig); |
| 1148 | if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) { |
| 1149 | if (debug != MagickFalse) |
| 1150 | { |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1151 | if (image->debug != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1152 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig); |
| 1153 | } |
| 1154 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1155 | } |
| 1156 | count=ReadBlob(image,4,alkey); |
| 1157 | length=ReadBlobMSBLong(image); |
| 1158 | if (debug != MagickFalse) |
| 1159 | { |
cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1160 | if (image->debug != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1161 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1162 | " adjustment layer key: %.4s, data length=%.20g", |
| 1163 | alkey, (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if ( length ) { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1167 | for (j=0; j < (ssize_t) (length); j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1168 | (void) ReadBlobByte(image); |
| 1169 | } |
| 1170 | |
| 1171 | } |
| 1172 | combinedlength += 12 + length; /* sig, key, length + the actual length*/ |
| 1173 | #endif |
| 1174 | |
| 1175 | /* |
| 1176 | Skip the rest of the variable data until we support it. |
| 1177 | */ |
| 1178 | if (image->debug != MagickFalse) |
| 1179 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1180 | " unsupported data: length=%.20g",(double) |
cristy | 2b9582a | 2011-07-04 17:38:56 +0000 | [diff] [blame] | 1181 | ((MagickOffsetType) (size-combinedlength))); |
cristy | c1af14f | 2010-09-16 20:01:21 +0000 | [diff] [blame] | 1182 | if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse) |
cristy | d429702 | 2010-09-16 22:59:09 +0000 | [diff] [blame] | 1183 | ThrowFileException(exception,CorruptImageError, |
| 1184 | "UnexpectedEndOfFile",image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1185 | } |
| 1186 | /* |
| 1187 | Allocate layered image. |
| 1188 | */ |
| 1189 | layer_info[i].image=CloneImage(image,layer_info[i].page.width, |
cristy | 390c39f | 2010-10-05 23:41:44 +0000 | [diff] [blame] | 1190 | layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height, |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1191 | MagickFalse,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1192 | if (layer_info[i].image == (Image *) NULL) |
| 1193 | { |
| 1194 | for (j=0; j < i; j++) |
| 1195 | layer_info[j].image=DestroyImage(layer_info[j].image); |
| 1196 | if (image->debug != MagickFalse) |
| 1197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1198 | " allocation of image for layer %.20g failed",(double) i); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1199 | ThrowReaderException(ResourceLimitError, |
| 1200 | "MemoryAllocationFailed"); |
| 1201 | } |
| 1202 | if (image->debug != MagickFalse) |
| 1203 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1204 | " setting up new layer image"); |
cristy | 2e2e46f | 2010-10-12 18:55:20 +0000 | [diff] [blame] | 1205 | if (image_info->ping != MagickFalse) |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 1206 | (void) SetImageBackgroundColor(layer_info[i].image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1207 | layer_info[i].image->compose= |
| 1208 | PSDBlendModeToCompositeOperator(layer_info[i].blendkey); |
| 1209 | if (layer_info[i].visible == MagickFalse) |
| 1210 | layer_info[i].image->compose=NoCompositeOp; |
| 1211 | if (psd_info.mode == CMYKMode) |
cristy | 0132ba8 | 2010-04-16 23:34:02 +0000 | [diff] [blame] | 1212 | layer_info[i].image->colorspace=CMYKColorspace; |
| 1213 | if ((psd_info.mode == BitmapMode) || |
| 1214 | (psd_info.mode == GrayscaleMode) || |
| 1215 | (psd_info.mode == DuotoneMode)) |
| 1216 | layer_info[i].image->colorspace=GRAYColorspace; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1217 | for (j=0; j < (ssize_t) layer_info[i].channels; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1218 | if (layer_info[i].channel_info[j].type == -1) |
| 1219 | layer_info[i].image->matte=MagickTrue; |
| 1220 | /* |
| 1221 | Set up some hidden attributes for folks that need them. |
| 1222 | */ |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1223 | (void) FormatLocaleString(message,MaxTextExtent,"%.20gld", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1224 | (double) layer_info[i].page.x); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1225 | (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1226 | (void) FormatLocaleString(message,MaxTextExtent,"%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1227 | (double) layer_info[i].page.y); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1228 | (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1229 | (void) FormatLocaleString(message,MaxTextExtent,"%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1230 | (double) layer_info[i].opacity); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1231 | (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity", |
| 1232 | message); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1233 | (void) SetImageProperty(layer_info[i].image,"label",(char *) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1234 | layer_info[i].name,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1235 | } |
| 1236 | if (image->debug != MagickFalse) |
| 1237 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1238 | " reading image data for layers"); |
| 1239 | /* |
| 1240 | Read pixel data for each layer. |
| 1241 | */ |
| 1242 | for (i=0; i < number_layers; i++) |
| 1243 | { |
| 1244 | if (image->debug != MagickFalse) |
| 1245 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1246 | " reading data for layer %.20g",(double) i); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1247 | for (j=0; j < (ssize_t) layer_info[i].channels; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1248 | { |
| 1249 | if (image->debug != MagickFalse) |
| 1250 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1251 | " reading data for channel %.20g",(double) j); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1252 | #if 1 |
| 1253 | if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows)) |
| 1254 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1255 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1256 | k; |
| 1257 | |
| 1258 | if (image->debug != MagickFalse) |
| 1259 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1260 | " layer data is empty"); |
| 1261 | /* |
| 1262 | A layer without data. |
| 1263 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1264 | for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1265 | (void) ReadBlobByte(layer_info[i].image); |
| 1266 | continue; |
| 1267 | } |
| 1268 | #endif |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1269 | offsets=(MagickOffsetType *) NULL; |
| 1270 | layer_info[i].image->compression=NoCompression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1271 | compression=ReadBlobMSBShort(layer_info[i].image); |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1272 | if ((layer_info[i].page.height != 0) && |
| 1273 | (layer_info[i].page.width != 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1274 | { |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1275 | if (compression == 1) |
| 1276 | { |
| 1277 | /* |
| 1278 | Read RLE compressed data. |
| 1279 | */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1280 | layer_info[i].image->compression=RLECompression; |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1281 | if (image->debug != MagickFalse) |
| 1282 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1283 | " layer data is RLE compressed"); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1284 | offsets=(MagickOffsetType *) AcquireQuantumMemory( |
| 1285 | layer_info[i].image->rows,sizeof(*offsets)); |
| 1286 | if (offsets == (MagickOffsetType *) NULL) |
| 1287 | ThrowReaderException(ResourceLimitError, |
| 1288 | "MemoryAllocationFailed"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1289 | for (y=0; y < (ssize_t) layer_info[i].image->rows; y++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1290 | offsets[y]=GetPSDOffset(&psd_info,layer_info[i].image); |
cristy | 2cc3984 | 2009-10-10 20:04:02 +0000 | [diff] [blame] | 1291 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1292 | status=ReadPSDLayer(layer_info[i].image, |
cristy | e303898 | 2010-05-17 02:20:52 +0000 | [diff] [blame] | 1293 | layer_info[i].channels,layer_info[i].channel_info[j].type, |
| 1294 | offsets,exception); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1295 | if (compression == 1) |
| 1296 | offsets=(MagickOffsetType *) RelinquishMagickMemory( |
| 1297 | offsets); |
| 1298 | if (status == MagickFalse) |
| 1299 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1300 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1301 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1302 | if (layer_info[i].opacity != OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1303 | { |
| 1304 | /* |
| 1305 | Correct for opacity level. |
| 1306 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1307 | for (y=0; y < (ssize_t) layer_info[i].image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1308 | { |
| 1309 | q=GetAuthenticPixels(layer_info[i].image,0,y, |
| 1310 | layer_info[i].image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1311 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1312 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1313 | for (x=0; x < (ssize_t) layer_info[i].image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1314 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1315 | SetPixelAlpha(layer_info[i].image,(Quantum) |
| 1316 | (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))* |
| 1317 | layer_info[i].opacity),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1318 | q+=GetPixelChannels(layer_info[i].image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | } |
| 1320 | if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse) |
| 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 | if (layer_info[i].image->colorspace == CMYKColorspace) |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 1325 | (void) NegateImage(layer_info[i].image,MagickFalse,exception); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1326 | status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType) |
| 1327 | number_layers); |
cristy | 4689cf0 | 2010-02-17 21:15:45 +0000 | [diff] [blame] | 1328 | if (status == MagickFalse) |
| 1329 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1330 | } |
| 1331 | /* added by palf -> invisible group layer make layer of this group |
| 1332 | invisible I consider that all layer with width and height null are |
| 1333 | layer for group layer */ |
| 1334 | { |
| 1335 | short inside_layer = 0; |
| 1336 | short layer_visible = 0; |
| 1337 | for (i=number_layers-1; i >=0; i--) |
| 1338 | { |
| 1339 | if ((layer_info[i].page.width == 0) || |
| 1340 | (layer_info[i].page.height == 0)) |
| 1341 | { |
| 1342 | if (inside_layer == 0) |
| 1343 | { |
| 1344 | inside_layer=1; |
| 1345 | layer_visible=(short int) layer_info[i].visible; |
| 1346 | } |
| 1347 | else |
| 1348 | { |
| 1349 | inside_layer = 0; |
| 1350 | } |
| 1351 | } |
| 1352 | else |
| 1353 | if ((inside_layer == 1) && (layer_visible == 0)) |
| 1354 | { |
| 1355 | layer_info[i].visible=(unsigned char) layer_visible; |
| 1356 | layer_info[i].image->compose=NoCompositeOp; |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | /* added by palf -> suppression of empty layer */ |
| 1361 | /* I consider that all layer with width and height null are layer for group layer */ |
| 1362 | for (i=0; i < number_layers; i++) |
| 1363 | { |
| 1364 | if ((layer_info[i].page.width == 0) || |
| 1365 | (layer_info[i].page.height == 0)) |
| 1366 | { |
| 1367 | if (layer_info[i].image != (Image *) NULL) |
| 1368 | layer_info[i].image=DestroyImage(layer_info[i].image); |
| 1369 | for (j=i; j < number_layers - 1; j++) |
| 1370 | layer_info[j] = layer_info[j+1]; |
| 1371 | number_layers--; |
| 1372 | i--; |
| 1373 | } |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1374 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1375 | mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */ |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1376 | (void) mask_size; |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1377 | if (number_layers > 0) |
| 1378 | { |
| 1379 | if (image->debug != MagickFalse) |
| 1380 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1381 | " putting layers into image list"); |
| 1382 | for (i=0; i < number_layers; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | { |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1384 | if (i > 0) |
| 1385 | layer_info[i].image->previous=layer_info[i-1].image; |
| 1386 | if (i < (number_layers-1)) |
| 1387 | layer_info[i].image->next=layer_info[i+1].image; |
| 1388 | layer_info[i].image->page=layer_info[i].page; |
| 1389 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1390 | image->next=layer_info[0].image; |
| 1391 | layer_info[0].image->previous=image; |
| 1392 | layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1393 | } |
| 1394 | layer_offset-=TellBlob(image); |
| 1395 | offset=SeekBlob(image,layer_offset,SEEK_CUR); |
| 1396 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1397 | } |
| 1398 | /* |
| 1399 | Read the precombined layer, present for PSD < 4 compatibility |
| 1400 | */ |
| 1401 | if (image->debug != MagickFalse) |
| 1402 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1403 | " reading the precombined layer"); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1404 | offsets=(MagickOffsetType *) NULL; |
| 1405 | image->compression=NoCompression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1406 | compression=ReadBlobMSBShort(image); |
| 1407 | if (compression == 1) |
| 1408 | { |
| 1409 | /* |
cristy | 3f938c6 | 2010-03-07 00:53:52 +0000 | [diff] [blame] | 1410 | Read Packbit encoded pixel data as separate planes. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1411 | */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1412 | image->compression=RLECompression; |
| 1413 | offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows, |
| 1414 | psd_info.channels*sizeof(*offsets)); |
| 1415 | if (offsets == (MagickOffsetType *) NULL) |
| 1416 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1417 | for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1418 | offsets[i]=GetPSDOffset(&psd_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1419 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1420 | for (i=0; i < (ssize_t) psd_info.channels; i++) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1421 | { |
cristy | e303898 | 2010-05-17 02:20:52 +0000 | [diff] [blame] | 1422 | status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows, |
| 1423 | exception); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1424 | if (status == MagickFalse) |
| 1425 | break; |
| 1426 | status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels); |
| 1427 | if (status == MagickFalse) |
| 1428 | break; |
| 1429 | } |
| 1430 | if (compression == 1) |
| 1431 | offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1432 | if (image->colorspace == CMYKColorspace) |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 1433 | (void) NegateImage(image,MagickFalse,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1434 | (void) CloseBlob(image); |
| 1435 | return(GetFirstImageInList(image)); |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1440 | % % |
| 1441 | % % |
| 1442 | % % |
| 1443 | % R e g i s t e r P S D I m a g e % |
| 1444 | % % |
| 1445 | % % |
| 1446 | % % |
| 1447 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1448 | % |
| 1449 | % RegisterPSDImage() adds properties for the PSD image format to |
| 1450 | % the list of supported formats. The properties include the image format |
| 1451 | % tag, a method to read and/or write the format, whether the format |
| 1452 | % supports the saving of more than one frame to the same file or blob, |
| 1453 | % whether the format supports native in-memory I/O, and a brief |
| 1454 | % description of the format. |
| 1455 | % |
| 1456 | % The format of the RegisterPSDImage method is: |
| 1457 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1458 | % size_t RegisterPSDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1459 | % |
| 1460 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1461 | ModuleExport size_t RegisterPSDImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1462 | { |
| 1463 | MagickInfo |
| 1464 | *entry; |
| 1465 | |
cristy | b423301 | 2010-02-28 20:09:14 +0000 | [diff] [blame] | 1466 | entry=SetMagickInfo("PSB"); |
| 1467 | entry->decoder=(DecodeImageHandler *) ReadPSDImage; |
| 1468 | entry->encoder=(EncodeImageHandler *) WritePSDImage; |
| 1469 | entry->magick=(IsImageFormatHandler *) IsPSD; |
cristy | ffaf978 | 2011-04-13 19:50:51 +0000 | [diff] [blame] | 1470 | entry->seekable_stream=MagickTrue; |
cristy | b423301 | 2010-02-28 20:09:14 +0000 | [diff] [blame] | 1471 | entry->description=ConstantString("Adobe Large Document Format"); |
| 1472 | entry->module=ConstantString("PSD"); |
| 1473 | (void) RegisterMagickInfo(entry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1474 | entry=SetMagickInfo("PSD"); |
| 1475 | entry->decoder=(DecodeImageHandler *) ReadPSDImage; |
| 1476 | entry->encoder=(EncodeImageHandler *) WritePSDImage; |
| 1477 | entry->magick=(IsImageFormatHandler *) IsPSD; |
cristy | ffaf978 | 2011-04-13 19:50:51 +0000 | [diff] [blame] | 1478 | entry->seekable_stream=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1479 | entry->description=ConstantString("Adobe Photoshop bitmap"); |
| 1480 | entry->module=ConstantString("PSD"); |
| 1481 | (void) RegisterMagickInfo(entry); |
| 1482 | return(MagickImageCoderSignature); |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1487 | % % |
| 1488 | % % |
| 1489 | % % |
| 1490 | % U n r e g i s t e r P S D I m a g e % |
| 1491 | % % |
| 1492 | % % |
| 1493 | % % |
| 1494 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1495 | % |
| 1496 | % UnregisterPSDImage() removes format registrations made by the |
| 1497 | % PSD module from the list of supported formats. |
| 1498 | % |
| 1499 | % The format of the UnregisterPSDImage method is: |
| 1500 | % |
| 1501 | % UnregisterPSDImage(void) |
| 1502 | % |
| 1503 | */ |
| 1504 | ModuleExport void UnregisterPSDImage(void) |
| 1505 | { |
cristy | b423301 | 2010-02-28 20:09:14 +0000 | [diff] [blame] | 1506 | (void) UnregisterMagickInfo("PSB"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1507 | (void) UnregisterMagickInfo("PSD"); |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1512 | % % |
| 1513 | % % |
| 1514 | % % |
| 1515 | % W r i t e P S D I m a g e % |
| 1516 | % % |
| 1517 | % % |
| 1518 | % % |
| 1519 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1520 | % |
cristy | b1459bc | 2010-03-23 21:41:43 +0000 | [diff] [blame] | 1521 | % WritePSDImage() writes an image in the Adobe Photoshop encoded image format. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1522 | % |
| 1523 | % The format of the WritePSDImage method is: |
| 1524 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1525 | % MagickBooleanType WritePSDImage(const ImageInfo *image_info, |
| 1526 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1527 | % |
| 1528 | % A description of each parameter follows. |
| 1529 | % |
| 1530 | % o image_info: the image info. |
| 1531 | % |
| 1532 | % o image: The image. |
| 1533 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1534 | % o exception: return any errors or warnings in this structure. |
| 1535 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1536 | */ |
| 1537 | |
cristy | 50aea4a | 2010-03-09 17:37:44 +0000 | [diff] [blame] | 1538 | static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image, |
cristy | f0460ee | 2010-03-06 02:55:11 +0000 | [diff] [blame] | 1539 | const size_t offset) |
| 1540 | { |
| 1541 | if (psd_info->version == 1) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1542 | return(WriteBlobMSBShort(image,(unsigned short) offset)); |
| 1543 | return(WriteBlobMSBLong(image,(unsigned short) offset)); |
cristy | f0460ee | 2010-03-06 02:55:11 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
cristy | 50aea4a | 2010-03-09 17:37:44 +0000 | [diff] [blame] | 1546 | static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image, |
cristy | f0460ee | 2010-03-06 02:55:11 +0000 | [diff] [blame] | 1547 | const MagickSizeType size) |
| 1548 | { |
| 1549 | if (psd_info->version == 1) |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1550 | return(WriteBlobMSBLong(image,(unsigned int) size)); |
cristy | f0460ee | 2010-03-06 02:55:11 +0000 | [diff] [blame] | 1551 | return(WriteBlobMSBLongLong(image,size)); |
| 1552 | } |
| 1553 | |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1554 | static size_t PSDPackbitsEncodeImage(Image *image,const size_t length, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1555 | const unsigned char *pixels,unsigned char *compact_pixels, |
| 1556 | ExceptionInfo *exception) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1557 | { |
| 1558 | int |
| 1559 | count; |
| 1560 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1561 | register ssize_t |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1562 | i, |
| 1563 | j; |
| 1564 | |
| 1565 | register unsigned char |
| 1566 | *q; |
| 1567 | |
| 1568 | unsigned char |
| 1569 | *packbits; |
| 1570 | |
| 1571 | /* |
| 1572 | Compress pixels with Packbits encoding. |
| 1573 | */ |
| 1574 | assert(image != (Image *) NULL); |
| 1575 | assert(image->signature == MagickSignature); |
| 1576 | if (image->debug != MagickFalse) |
| 1577 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1578 | assert(pixels != (unsigned char *) NULL); |
| 1579 | packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits)); |
| 1580 | if (packbits == (unsigned char *) NULL) |
| 1581 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1582 | image->filename); |
cristy | b1459bc | 2010-03-23 21:41:43 +0000 | [diff] [blame] | 1583 | q=compact_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1584 | for (i=(ssize_t) length; i != 0; ) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1585 | { |
| 1586 | switch (i) |
| 1587 | { |
| 1588 | case 1: |
| 1589 | { |
| 1590 | i--; |
| 1591 | *q++=(unsigned char) 0; |
| 1592 | *q++=(*pixels); |
| 1593 | break; |
| 1594 | } |
| 1595 | case 2: |
| 1596 | { |
| 1597 | i-=2; |
| 1598 | *q++=(unsigned char) 1; |
| 1599 | *q++=(*pixels); |
| 1600 | *q++=pixels[1]; |
| 1601 | break; |
| 1602 | } |
| 1603 | case 3: |
| 1604 | { |
| 1605 | i-=3; |
| 1606 | if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2))) |
| 1607 | { |
| 1608 | *q++=(unsigned char) ((256-3)+1); |
| 1609 | *q++=(*pixels); |
| 1610 | break; |
| 1611 | } |
| 1612 | *q++=(unsigned char) 2; |
| 1613 | *q++=(*pixels); |
| 1614 | *q++=pixels[1]; |
| 1615 | *q++=pixels[2]; |
| 1616 | break; |
| 1617 | } |
| 1618 | default: |
| 1619 | { |
| 1620 | if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2))) |
| 1621 | { |
| 1622 | /* |
| 1623 | Packed run. |
| 1624 | */ |
| 1625 | count=3; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1626 | while (((ssize_t) count < i) && (*pixels == *(pixels+count))) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1627 | { |
| 1628 | count++; |
| 1629 | if (count >= 127) |
| 1630 | break; |
| 1631 | } |
| 1632 | i-=count; |
| 1633 | *q++=(unsigned char) ((256-count)+1); |
| 1634 | *q++=(*pixels); |
| 1635 | pixels+=count; |
| 1636 | break; |
| 1637 | } |
| 1638 | /* |
| 1639 | Literal run. |
| 1640 | */ |
| 1641 | count=0; |
| 1642 | while ((*(pixels+count) != *(pixels+count+1)) || |
| 1643 | (*(pixels+count+1) != *(pixels+count+2))) |
| 1644 | { |
| 1645 | packbits[count+1]=pixels[count]; |
| 1646 | count++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1647 | if (((ssize_t) count >= (i-3)) || (count >= 127)) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1648 | break; |
| 1649 | } |
| 1650 | i-=count; |
| 1651 | *packbits=(unsigned char) (count-1); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1652 | for (j=0; j <= (ssize_t) count; j++) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1653 | *q++=packbits[j]; |
| 1654 | pixels+=count; |
| 1655 | break; |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | *q++=(unsigned char) 128; /* EOD marker */ |
| 1660 | packbits=(unsigned char *) RelinquishMagickMemory(packbits); |
cristy | b1459bc | 2010-03-23 21:41:43 +0000 | [diff] [blame] | 1661 | return((size_t) (q-compact_pixels)); |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 1664 | static void WritePackbitsLength(const PSDInfo *psd_info, |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1665 | const ImageInfo *image_info,Image *image,Image *next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1666 | unsigned char *compact_pixels,const QuantumType quantum_type, |
| 1667 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1668 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1669 | QuantumInfo |
| 1670 | *quantum_info; |
| 1671 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1672 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1673 | *p; |
| 1674 | |
| 1675 | size_t |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1676 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1677 | packet_size; |
| 1678 | |
cristy | 75f85ae | 2010-09-25 03:01:06 +0000 | [diff] [blame] | 1679 | ssize_t |
| 1680 | y; |
| 1681 | |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1682 | unsigned char |
| 1683 | *pixels; |
| 1684 | |
| 1685 | if (next_image->depth > 8) |
| 1686 | next_image->depth=16; |
| 1687 | packet_size=next_image->depth > 8UL ? 2UL : 1UL; |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1688 | (void) packet_size; |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1689 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1690 | pixels=GetQuantumPixels(quantum_info); |
| 1691 | for (y=0; y < (ssize_t) next_image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1692 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1693 | p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1694 | if (p == (const Quantum *) NULL) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1695 | break; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1696 | length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info, |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1697 | quantum_type,pixels,exception); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1698 | length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels, |
| 1699 | exception); |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 1700 | (void) SetPSDOffset(psd_info,image,length); |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1701 | } |
| 1702 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 1703 | } |
| 1704 | |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 1705 | static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info, |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1706 | Image *image,Image *next_image,unsigned char *compact_pixels, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1707 | const QuantumType quantum_type,const MagickBooleanType compression_flag, |
| 1708 | ExceptionInfo *exception) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1709 | { |
| 1710 | int |
| 1711 | y; |
| 1712 | |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1713 | MagickBooleanType |
| 1714 | monochrome; |
| 1715 | |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1716 | QuantumInfo |
| 1717 | *quantum_info; |
| 1718 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1719 | register const Quantum |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1720 | *p; |
| 1721 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1722 | register ssize_t |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1723 | i; |
| 1724 | |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1725 | size_t |
| 1726 | length, |
| 1727 | packet_size; |
| 1728 | |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1729 | unsigned char |
| 1730 | *pixels; |
| 1731 | |
cristy | 50aea4a | 2010-03-09 17:37:44 +0000 | [diff] [blame] | 1732 | (void) psd_info; |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1733 | if ((compression_flag != MagickFalse) && |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1734 | (next_image->compression != RLECompression)) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1735 | (void) WriteBlobMSBShort(image,0); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1736 | if (next_image->depth > 8) |
| 1737 | next_image->depth=16; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1738 | monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ? |
| 1739 | MagickTrue : MagickFalse; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1740 | packet_size=next_image->depth > 8UL ? 2UL : 1UL; |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1741 | (void) packet_size; |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1742 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1743 | pixels=GetQuantumPixels(quantum_info); |
| 1744 | for (y=0; y < (ssize_t) next_image->rows; y++) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1745 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1746 | p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1747 | if (p == (const Quantum *) NULL) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1748 | break; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1749 | length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info, |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1750 | quantum_type,pixels,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1751 | if (monochrome != MagickFalse) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1752 | for (i=0; i < (ssize_t) length; i++) |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1753 | pixels[i]=(~pixels[i]); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1754 | if (next_image->compression != RLECompression) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1755 | (void) WriteBlob(image,length,pixels); |
| 1756 | else |
| 1757 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1758 | length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels, |
| 1759 | exception); |
cristy | b1459bc | 2010-03-23 21:41:43 +0000 | [diff] [blame] | 1760 | (void) WriteBlob(image,length,compact_pixels); |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1761 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1762 | } |
| 1763 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 1764 | } |
| 1765 | |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 1766 | static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info, |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1767 | const ImageInfo *image_info,Image *image,Image *next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1768 | const MagickBooleanType separate,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1769 | { |
| 1770 | int |
| 1771 | i; |
| 1772 | |
| 1773 | size_t |
| 1774 | channels, |
| 1775 | packet_size; |
| 1776 | |
| 1777 | unsigned char |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1778 | *compact_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1779 | |
| 1780 | /* |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 1781 | Write uncompressed pixels as separate planes. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1782 | */ |
| 1783 | channels=1; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1784 | packet_size=next_image->depth > 8UL ? 2UL : 1UL; |
| 1785 | compact_pixels=(unsigned char *) NULL; |
| 1786 | if (next_image->compression == RLECompression) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1787 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1788 | compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels* |
| 1789 | next_image->columns,packet_size*sizeof(*compact_pixels)); |
| 1790 | if (compact_pixels == (unsigned char *) NULL) |
| 1791 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1792 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1793 | i=0; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1794 | if (IsImageGray(next_image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1795 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1796 | if (next_image->compression == RLECompression) |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1797 | { |
| 1798 | /* |
| 1799 | Packbits compression. |
| 1800 | */ |
| 1801 | (void) WriteBlobMSBShort(image,1); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1802 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1803 | compact_pixels,GrayQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1804 | if (next_image->matte != MagickFalse) |
| 1805 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1806 | compact_pixels,AlphaQuantum,exception); |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 1807 | } |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1808 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1809 | GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1810 | MagickFalse,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1811 | if (next_image->matte != MagickFalse) |
| 1812 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1813 | AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1814 | MagickFalse,exception); |
cristy | 6886a75 | 2010-04-23 18:23:20 +0000 | [diff] [blame] | 1815 | (void) SetImageProgress(image,SaveImagesTag,0,1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1816 | } |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1817 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1818 | if (next_image->storage_class == PseudoClass) |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1819 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1820 | if (next_image->compression == RLECompression) |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1821 | { |
| 1822 | /* |
| 1823 | Packbits compression. |
| 1824 | */ |
| 1825 | (void) WriteBlobMSBShort(image,1); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1826 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1827 | compact_pixels,IndexQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1828 | if (next_image->matte != MagickFalse) |
| 1829 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1830 | compact_pixels,AlphaQuantum,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1831 | } |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1832 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1833 | IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1834 | MagickFalse,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1835 | if (next_image->matte != MagickFalse) |
| 1836 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1837 | AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1838 | MagickFalse,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1839 | (void) SetImageProgress(image,SaveImagesTag,0,1); |
| 1840 | } |
| 1841 | else |
| 1842 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1843 | if (next_image->colorspace == CMYKColorspace) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1844 | (void) NegateImage(next_image,MagickFalse,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1845 | if (next_image->compression == RLECompression) |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1846 | { |
| 1847 | /* |
| 1848 | Packbits compression. |
| 1849 | */ |
| 1850 | (void) WriteBlobMSBShort(image,1); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1851 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1852 | compact_pixels,RedQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1853 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1854 | compact_pixels,GreenQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1855 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1856 | compact_pixels,BlueQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1857 | if (next_image->colorspace == CMYKColorspace) |
| 1858 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1859 | compact_pixels,BlackQuantum,exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1860 | if (next_image->matte != MagickFalse) |
| 1861 | WritePackbitsLength(psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1862 | compact_pixels,AlphaQuantum,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1863 | } |
| 1864 | (void) SetImageProgress(image,SaveImagesTag,0,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1865 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1866 | RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1867 | MagickFalse,exception); |
cristy | 860cc73 | 2010-05-19 16:38:37 +0000 | [diff] [blame] | 1868 | (void) SetImageProgress(image,SaveImagesTag,1,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1869 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1870 | GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1871 | MagickFalse,exception); |
cristy | 860cc73 | 2010-05-19 16:38:37 +0000 | [diff] [blame] | 1872 | (void) SetImageProgress(image,SaveImagesTag,2,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1873 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1874 | BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1875 | MagickFalse,exception); |
cristy | 860cc73 | 2010-05-19 16:38:37 +0000 | [diff] [blame] | 1876 | (void) SetImageProgress(image,SaveImagesTag,3,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1877 | if (next_image->colorspace == CMYKColorspace) |
| 1878 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1879 | BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1880 | MagickFalse,exception); |
cristy | 860cc73 | 2010-05-19 16:38:37 +0000 | [diff] [blame] | 1881 | (void) SetImageProgress(image,SaveImagesTag,4,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1882 | if (next_image->matte != MagickFalse) |
| 1883 | WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels, |
| 1884 | AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue : |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1885 | MagickFalse,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1886 | (void) SetImageProgress(image,SaveImagesTag,5,6); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1887 | if (next_image->colorspace == CMYKColorspace) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1888 | (void) NegateImage(next_image,MagickFalse,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 1889 | } |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1890 | if (next_image->compression == RLECompression) |
| 1891 | compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1892 | return(MagickTrue); |
| 1893 | } |
| 1894 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1895 | static void WritePascalString(Image* inImage,const char *inString,int inPad) |
| 1896 | { |
| 1897 | size_t |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1898 | length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1899 | |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1900 | register ssize_t |
| 1901 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1902 | |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1903 | /* |
| 1904 | Max length is 255. |
| 1905 | */ |
| 1906 | length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString); |
| 1907 | if (length == 0) |
| 1908 | (void) WriteBlobByte(inImage,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1909 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1910 | { |
| 1911 | (void) WriteBlobByte(inImage,(unsigned char) length); |
| 1912 | (void) WriteBlob(inImage, length, (const unsigned char *) inString); |
| 1913 | } |
| 1914 | length++; |
| 1915 | if ((length % inPad) == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1916 | return; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 1917 | for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1918 | (void) WriteBlobByte(inImage,0); |
| 1919 | } |
| 1920 | |
| 1921 | static void WriteResolutionResourceBlock(Image *image) |
| 1922 | { |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1923 | double |
| 1924 | x_resolution, |
| 1925 | y_resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1926 | |
| 1927 | unsigned short |
| 1928 | units; |
| 1929 | |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1930 | x_resolution=65536.0*image->resolution.x+0.5; |
| 1931 | y_resolution=65536.0*image->resolution.y+0.5; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1932 | units=1; |
| 1933 | if (image->units == PixelsPerCentimeterResolution) |
| 1934 | { |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1935 | x_resolution=2.54*65536.0*image->resolution.x*0.5; |
| 1936 | y_resolution=2.54*65536.0*image->resolution.y+0.5; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1937 | units=2; |
| 1938 | } |
| 1939 | (void) WriteBlob(image,4,(const unsigned char *) "8BIM"); |
| 1940 | (void) WriteBlobMSBShort(image,0x03ED); |
| 1941 | (void) WriteBlobMSBShort(image,0); |
| 1942 | (void) WriteBlobMSBLong(image,16); /* resource size */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1943 | (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1944 | (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */ |
| 1945 | (void) WriteBlobMSBShort(image,units); /* width unit */ |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 1946 | (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1947 | (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */ |
| 1948 | (void) WriteBlobMSBShort(image,units); /* height unit */ |
| 1949 | } |
| 1950 | |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1951 | static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile) |
| 1952 | { |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 1953 | register const unsigned char |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1954 | *p; |
| 1955 | |
| 1956 | size_t |
| 1957 | length; |
| 1958 | |
| 1959 | unsigned char |
| 1960 | *datum; |
| 1961 | |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 1962 | unsigned int |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1963 | count, |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 1964 | long_sans; |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1965 | |
| 1966 | unsigned short |
| 1967 | id, |
| 1968 | short_sans; |
| 1969 | |
| 1970 | length=GetStringInfoLength(bim_profile); |
| 1971 | if (length < 16) |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 1972 | return; |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1973 | datum=GetStringInfoDatum(bim_profile); |
| 1974 | for (p=datum; (p >= datum) && (p < (datum+length-16)); ) |
| 1975 | { |
cristy | 4176bb2 | 2010-05-01 16:29:09 +0000 | [diff] [blame] | 1976 | register unsigned char |
| 1977 | *q; |
| 1978 | |
| 1979 | q=(unsigned char *) p; |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1980 | if (LocaleNCompare((const char *) p,"8BIM",4) != 0) |
| 1981 | break; |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 1982 | p=PushLongPixel(MSBEndian,p,&long_sans); |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1983 | p=PushShortPixel(MSBEndian,p,&id); |
| 1984 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 1985 | p=PushLongPixel(MSBEndian,p,&count); |
| 1986 | if (id == 0x0000040f) |
| 1987 | { |
cristy | 4176bb2 | 2010-05-01 16:29:09 +0000 | [diff] [blame] | 1988 | (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length- |
| 1989 | (PSDQuantum(count)+12)-(q-datum)); |
| 1990 | SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12)); |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 1991 | break; |
| 1992 | } |
| 1993 | p+=count; |
| 1994 | if ((count & 0x01) != 0) |
| 1995 | p++; |
| 1996 | } |
| 1997 | } |
| 1998 | |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 1999 | static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile) |
| 2000 | { |
| 2001 | register const unsigned char |
| 2002 | *p; |
| 2003 | |
| 2004 | size_t |
| 2005 | length; |
| 2006 | |
| 2007 | unsigned char |
| 2008 | *datum; |
| 2009 | |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 2010 | unsigned int |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2011 | count, |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 2012 | long_sans; |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2013 | |
| 2014 | unsigned short |
| 2015 | id, |
| 2016 | short_sans; |
| 2017 | |
| 2018 | length=GetStringInfoLength(bim_profile); |
| 2019 | if (length < 16) |
| 2020 | return; |
| 2021 | datum=GetStringInfoDatum(bim_profile); |
| 2022 | for (p=datum; (p >= datum) && (p < (datum+length-16)); ) |
| 2023 | { |
| 2024 | register unsigned char |
| 2025 | *q; |
| 2026 | |
| 2027 | q=(unsigned char *) p; |
| 2028 | if (LocaleNCompare((const char *) p,"8BIM",4) != 0) |
| 2029 | break; |
cristy | 6befb0f | 2010-05-31 14:33:15 +0000 | [diff] [blame] | 2030 | p=PushLongPixel(MSBEndian,p,&long_sans); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2031 | p=PushShortPixel(MSBEndian,p,&id); |
| 2032 | p=PushShortPixel(MSBEndian,p,&short_sans); |
| 2033 | p=PushLongPixel(MSBEndian,p,&count); |
cristy | 94b1183 | 2011-09-08 19:46:03 +0000 | [diff] [blame] | 2034 | if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12))) |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2035 | { |
| 2036 | (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length- |
| 2037 | (PSDQuantum(count)+12)-(q-datum)); |
| 2038 | SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12)); |
| 2039 | break; |
| 2040 | } |
| 2041 | p+=count; |
| 2042 | if ((count & 0x01) != 0) |
| 2043 | p++; |
| 2044 | } |
| 2045 | } |
| 2046 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2047 | static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image, |
| 2048 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2049 | { |
| 2050 | const char |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2051 | *property; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2052 | |
| 2053 | const StringInfo |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2054 | *icc_profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2055 | |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2056 | Image |
| 2057 | *base_image, |
| 2058 | *next_image; |
| 2059 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2060 | MagickBooleanType |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2061 | status; |
| 2062 | |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 2063 | PSDInfo |
| 2064 | psd_info; |
| 2065 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2066 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2067 | i; |
| 2068 | |
| 2069 | size_t |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2070 | channel_size, |
| 2071 | channelLength, |
| 2072 | layer_count, |
| 2073 | layer_info_size, |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2074 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2075 | num_channels, |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2076 | packet_size, |
| 2077 | rounded_layer_info_size; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2078 | |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 2079 | StringInfo |
| 2080 | *bim_profile; |
| 2081 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2082 | unsigned char |
| 2083 | layer_name[4]; |
| 2084 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2085 | /* |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 2086 | Open image file. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2087 | */ |
| 2088 | assert(image_info != (const ImageInfo *) NULL); |
| 2089 | assert(image_info->signature == MagickSignature); |
| 2090 | assert(image != (Image *) NULL); |
| 2091 | assert(image->signature == MagickSignature); |
| 2092 | if (image->debug != MagickFalse) |
| 2093 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2094 | assert(exception != (ExceptionInfo *) NULL); |
| 2095 | assert(exception->signature == MagickSignature); |
| 2096 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2097 | if (status == MagickFalse) |
| 2098 | return(status); |
| 2099 | packet_size=(size_t) (image->depth > 8 ? 6 : 3); |
| 2100 | if (image->matte != MagickFalse) |
| 2101 | packet_size+=image->depth > 8 ? 2 : 1; |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 2102 | psd_info.version=1; |
| 2103 | if ((LocaleCompare(image_info->magick,"PSB") == 0) || |
| 2104 | (image->columns > 30000) || (image->rows > 30000)) |
| 2105 | psd_info.version=2; |
cristy | 50aea4a | 2010-03-09 17:37:44 +0000 | [diff] [blame] | 2106 | (void) WriteBlob(image,4,(const unsigned char *) "8BPS"); |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 2107 | (void) WriteBlobMSBShort(image,psd_info.version); /* version */ |
| 2108 | for (i=1; i <= 6; i++) |
| 2109 | (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2110 | if (IsImageGray(image,exception) != MagickFalse) |
cristy | 6886a75 | 2010-04-23 18:23:20 +0000 | [diff] [blame] | 2111 | num_channels=(image->matte != MagickFalse ? 2UL : 1UL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2112 | else |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2113 | if (image->storage_class == PseudoClass) |
cristy | 6886a75 | 2010-04-23 18:23:20 +0000 | [diff] [blame] | 2114 | num_channels=(image->matte != MagickFalse ? 2UL : 1UL); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2115 | else |
| 2116 | { |
| 2117 | if (image->colorspace != CMYKColorspace) |
cristy | 6886a75 | 2010-04-23 18:23:20 +0000 | [diff] [blame] | 2118 | num_channels=(image->matte != MagickFalse ? 4UL : 3UL); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2119 | else |
cristy | 6886a75 | 2010-04-23 18:23:20 +0000 | [diff] [blame] | 2120 | num_channels=(image->matte != MagickFalse ? 5UL : 4UL); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2121 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2122 | (void) WriteBlobMSBShort(image,(unsigned short) num_channels); |
cristy | 56ed31c | 2010-03-22 00:46:21 +0000 | [diff] [blame] | 2123 | (void) WriteBlobMSBLong(image,(unsigned int) image->rows); |
| 2124 | (void) WriteBlobMSBLong(image,(unsigned int) image->columns); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2125 | if (IsImageGray(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2126 | { |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2127 | MagickBooleanType |
| 2128 | monochrome; |
| 2129 | |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2130 | /* |
| 2131 | Write depth & mode. |
| 2132 | */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2133 | monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ? |
| 2134 | MagickTrue : MagickFalse; |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 2135 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 2136 | (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8)); |
cristy | 2b9582a | 2011-07-04 17:38:56 +0000 | [diff] [blame] | 2137 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 2138 | (monochrome != MagickFalse ? BitmapMode : GrayscaleMode)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2139 | } |
| 2140 | else |
| 2141 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2142 | (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class == |
| 2143 | PseudoClass ? 8 : image->depth > 8 ? 16 : 8)); |
cristy | 4884539 | 2010-06-02 01:19:17 +0000 | [diff] [blame] | 2144 | if (((image_info->colorspace != UndefinedColorspace) || |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2145 | (image->colorspace != CMYKColorspace)) && |
cristy | 4884539 | 2010-06-02 01:19:17 +0000 | [diff] [blame] | 2146 | (image_info->colorspace != CMYKColorspace)) |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2147 | { |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 2148 | if (IsRGBColorspace(image->colorspace) == MagickFalse) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 2149 | (void) TransformImageColorspace(image,RGBColorspace,exception); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2150 | (void) WriteBlobMSBShort(image,(unsigned short) |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2151 | (image->storage_class == PseudoClass ? IndexedMode : RGBMode)); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2152 | } |
| 2153 | else |
| 2154 | { |
cristy | 4884539 | 2010-06-02 01:19:17 +0000 | [diff] [blame] | 2155 | if (image->colorspace != CMYKColorspace) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 2156 | (void) TransformImageColorspace(image,CMYKColorspace,exception); |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2157 | (void) WriteBlobMSBShort(image,CMYKMode); |
cristy | 0910f24 | 2010-04-01 18:55:09 +0000 | [diff] [blame] | 2158 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2159 | } |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2160 | if ((IsImageGray(image,exception) != MagickFalse) || |
cristy | ca9ddb0 | 2010-04-16 01:01:18 +0000 | [diff] [blame] | 2161 | (image->storage_class == DirectClass) || (image->colors > 256)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2162 | (void) WriteBlobMSBLong(image,0); |
| 2163 | else |
| 2164 | { |
| 2165 | /* |
| 2166 | Write PSD raster colormap. |
| 2167 | */ |
| 2168 | (void) WriteBlobMSBLong(image,768); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2169 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2170 | (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red)); |
| 2171 | for ( ; i < 256; i++) |
| 2172 | (void) WriteBlobByte(image,0); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2173 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2174 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
| 2175 | image->colormap[i].green)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2176 | for ( ; i < 256; i++) |
| 2177 | (void) WriteBlobByte(image,0); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2178 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2179 | (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue)); |
| 2180 | for ( ; i < 256; i++) |
| 2181 | (void) WriteBlobByte(image,0); |
| 2182 | } |
| 2183 | /* |
| 2184 | Image resource block. |
| 2185 | */ |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2186 | length=28; /* 0x03EB */ |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 2187 | bim_profile=(StringInfo *) GetImageProfile(image,"8bim"); |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2188 | icc_profile=GetImageProfile(image,"icc"); |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 2189 | if (bim_profile != (StringInfo *) NULL) |
| 2190 | { |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 2191 | bim_profile=CloneStringInfo(bim_profile); |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 2192 | if (icc_profile != (StringInfo *) NULL) |
| 2193 | RemoveICCProfileFromResourceBlock(bim_profile); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2194 | RemoveResolutionFromResourceBlock(bim_profile); |
cristy | d4d3f74 | 2010-04-25 20:36:50 +0000 | [diff] [blame] | 2195 | length+=PSDQuantum(GetStringInfoLength(bim_profile)); |
| 2196 | } |
cristy | 0b796e6 | 2010-04-29 00:38:45 +0000 | [diff] [blame] | 2197 | if (icc_profile != (const StringInfo *) NULL) |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2198 | length+=PSDQuantum(GetStringInfoLength(icc_profile))+12; |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 2199 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
cristy | f11065e | 2010-05-14 13:26:59 +0000 | [diff] [blame] | 2200 | WriteResolutionResourceBlock(image); |
cristy | 4176bb2 | 2010-05-01 16:29:09 +0000 | [diff] [blame] | 2201 | if (bim_profile != (StringInfo *) NULL) |
| 2202 | { |
| 2203 | (void) WriteBlob(image,GetStringInfoLength(bim_profile), |
| 2204 | GetStringInfoDatum(bim_profile)); |
| 2205 | bim_profile=DestroyStringInfo(bim_profile); |
| 2206 | } |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2207 | if (icc_profile != (StringInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2208 | { |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2209 | (void) WriteBlob(image,4,(const unsigned char *) "8BIM"); |
cristy | 4176bb2 | 2010-05-01 16:29:09 +0000 | [diff] [blame] | 2210 | (void) WriteBlobMSBShort(image,0x0000040F); |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2211 | (void) WriteBlobMSBShort(image,0); |
cristy | 284c7d8 | 2010-04-24 00:19:14 +0000 | [diff] [blame] | 2212 | (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength( |
| 2213 | icc_profile)); |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2214 | (void) WriteBlob(image,GetStringInfoLength(icc_profile), |
| 2215 | GetStringInfoDatum(icc_profile)); |
cristy | e195f26 | 2010-04-16 18:12:35 +0000 | [diff] [blame] | 2216 | if ((MagickOffsetType) GetStringInfoLength(icc_profile) != |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2217 | PSDQuantum(GetStringInfoLength(icc_profile))) |
cristy | 749d215 | 2010-04-04 23:47:33 +0000 | [diff] [blame] | 2218 | (void) WriteBlobByte(image,0); |
cristy | e636559 | 2010-04-02 17:31:23 +0000 | [diff] [blame] | 2219 | } |
cristy | 4884539 | 2010-06-02 01:19:17 +0000 | [diff] [blame] | 2220 | layer_count=0; |
| 2221 | layer_info_size=2; |
cristy | 2837bcc | 2010-08-07 23:57:39 +0000 | [diff] [blame] | 2222 | base_image=GetNextImageInList(image); |
| 2223 | if ((image->matte != MagickFalse) && (base_image == (Image *) NULL)) |
| 2224 | base_image=image; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2225 | next_image=base_image; |
| 2226 | while ( next_image != NULL ) |
| 2227 | { |
| 2228 | packet_size=next_image->depth > 8 ? 2UL : 1UL; |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2229 | if (IsImageGray(next_image,exception) != MagickFalse) |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2230 | num_channels=next_image->matte != MagickFalse ? 2UL : 1UL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2231 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2232 | if (next_image->storage_class == PseudoClass) |
| 2233 | num_channels=next_image->matte != MagickFalse ? 2UL : 1UL; |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2234 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2235 | if (next_image->colorspace != CMYKColorspace) |
| 2236 | num_channels=next_image->matte != MagickFalse ? 4UL : 3UL; |
cristy | 2045da3 | 2010-04-16 00:59:35 +0000 | [diff] [blame] | 2237 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2238 | num_channels=next_image->matte != MagickFalse ? 5UL : 4UL; |
| 2239 | channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2); |
cristy | 4884539 | 2010-06-02 01:19:17 +0000 | [diff] [blame] | 2240 | layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 : |
| 2241 | 16)+4*1+4+num_channels*channelLength); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2242 | property=(const char *) GetImageProperty(next_image,"label",exception); |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2243 | if (property == (const char *) NULL) |
| 2244 | layer_info_size+=16; |
cristy | de9b8f5 | 2010-03-12 01:17:00 +0000 | [diff] [blame] | 2245 | else |
| 2246 | { |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2247 | size_t |
| 2248 | length; |
| 2249 | |
| 2250 | length=strlen(property); |
| 2251 | layer_info_size+=8+length+(4-(length % 4)); |
cristy | de9b8f5 | 2010-03-12 01:17:00 +0000 | [diff] [blame] | 2252 | } |
cristy | 4aff157 | 2010-02-15 13:34:01 +0000 | [diff] [blame] | 2253 | layer_count++; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2254 | next_image=GetNextImageInList(next_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2255 | } |
cristy | 144f1b6 | 2010-05-18 00:52:09 +0000 | [diff] [blame] | 2256 | if (layer_count == 0) |
cristy | 875e28a | 2010-03-06 19:46:55 +0000 | [diff] [blame] | 2257 | (void) SetPSDSize(&psd_info,image,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2258 | else |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2259 | { |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2260 | CompressionType |
| 2261 | compression; |
| 2262 | |
| 2263 | (void) SetPSDSize(&psd_info,image,layer_info_size+ |
| 2264 | (psd_info.version == 1 ? 8 : 16)); |
| 2265 | if ((layer_info_size/2) != ((layer_info_size+1)/2)) |
| 2266 | rounded_layer_info_size=layer_info_size+1; |
cristy | a20214e | 2010-08-28 16:52:13 +0000 | [diff] [blame] | 2267 | else |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2268 | rounded_layer_info_size=layer_info_size; |
| 2269 | (void) SetPSDSize(&psd_info,image,rounded_layer_info_size); |
| 2270 | (void) WriteBlobMSBShort(image,(unsigned short) layer_count); |
| 2271 | layer_count=1; |
| 2272 | compression=base_image->compression; |
| 2273 | next_image=base_image; |
| 2274 | while (next_image != NULL) |
| 2275 | { |
| 2276 | next_image->compression=NoCompression; |
cristy | a4e5f47 | 2011-11-09 00:42:46 +0000 | [diff] [blame] | 2277 | (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y); |
| 2278 | (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x); |
| 2279 | (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+ |
| 2280 | next_image->rows); |
| 2281 | (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+ |
| 2282 | next_image->columns); |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2283 | packet_size=next_image->depth > 8 ? 2UL : 1UL; |
| 2284 | channel_size=(unsigned int) ((packet_size*next_image->rows* |
| 2285 | next_image->columns)+2); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2286 | if ((IsImageGray(next_image,exception) != MagickFalse) || |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2287 | (next_image->storage_class == PseudoClass)) |
| 2288 | { |
| 2289 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 2290 | (next_image->matte != MagickFalse ? 2 : 1)); |
| 2291 | (void) WriteBlobMSBShort(image,0); |
| 2292 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2293 | if (next_image->matte != MagickFalse) |
| 2294 | { |
| 2295 | (void) WriteBlobMSBShort(image,(unsigned short) -1); |
| 2296 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2297 | } |
| 2298 | } |
| 2299 | else |
| 2300 | if (next_image->colorspace != CMYKColorspace) |
| 2301 | { |
| 2302 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 2303 | (next_image->matte != MagickFalse ? 4 : 3)); |
| 2304 | (void) WriteBlobMSBShort(image,0); |
| 2305 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2306 | (void) WriteBlobMSBShort(image,1); |
| 2307 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2308 | (void) WriteBlobMSBShort(image,2); |
| 2309 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2310 | if (next_image->matte!= MagickFalse ) |
| 2311 | { |
| 2312 | (void) WriteBlobMSBShort(image,(unsigned short) -1); |
| 2313 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2314 | } |
| 2315 | } |
| 2316 | else |
| 2317 | { |
| 2318 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 2319 | (next_image->matte ? 5 : 4)); |
| 2320 | (void) WriteBlobMSBShort(image,0); |
| 2321 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2322 | (void) WriteBlobMSBShort(image,1); |
| 2323 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2324 | (void) WriteBlobMSBShort(image,2); |
| 2325 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2326 | (void) WriteBlobMSBShort(image,3); |
| 2327 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2328 | if (next_image->matte) |
| 2329 | { |
| 2330 | (void) WriteBlobMSBShort(image,(unsigned short) -1); |
| 2331 | (void) SetPSDSize(&psd_info,image,channel_size); |
| 2332 | } |
| 2333 | } |
| 2334 | (void) WriteBlob(image,4,(const unsigned char *) "8BIM"); |
| 2335 | (void) WriteBlob(image,4,(const unsigned char *) |
| 2336 | CompositeOperatorToPSDBlendMode(next_image->compose)); |
| 2337 | (void) WriteBlobByte(image,255); /* layer opacity */ |
| 2338 | (void) WriteBlobByte(image,0); |
| 2339 | (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */ |
| 2340 | (void) WriteBlobByte(image,0); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2341 | property=(const char *) GetImageProperty(next_image,"label",exception); |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2342 | if (property == (const char *) NULL) |
| 2343 | { |
| 2344 | (void) WriteBlobMSBLong(image,16); |
| 2345 | (void) WriteBlobMSBLong(image,0); |
| 2346 | (void) WriteBlobMSBLong(image,0); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2347 | (void) FormatLocaleString((char *) layer_name,MaxTextExtent, |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2348 | "L%06ld",(long) layer_count++); |
| 2349 | WritePascalString( image, (char*)layer_name, 4 ); |
| 2350 | } |
| 2351 | else |
| 2352 | { |
| 2353 | size_t |
| 2354 | length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2355 | |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2356 | length=strlen(property); |
| 2357 | (void) WriteBlobMSBLong(image,(unsigned int) (length+(4- |
| 2358 | (length % 4))+8)); |
| 2359 | (void) WriteBlobMSBLong(image,0); |
| 2360 | (void) WriteBlobMSBLong(image,0); |
| 2361 | WritePascalString(image,property,4); |
| 2362 | } |
| 2363 | next_image=GetNextImageInList(next_image); |
| 2364 | } |
| 2365 | /* |
| 2366 | Now the image data! |
| 2367 | */ |
| 2368 | next_image=base_image; |
| 2369 | while (next_image != NULL) |
| 2370 | { |
| 2371 | status=WriteImageChannels(&psd_info,image_info,image,next_image, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2372 | MagickTrue,exception); |
cristy | 8e9bd3e | 2010-08-29 00:03:56 +0000 | [diff] [blame] | 2373 | next_image=GetNextImageInList(next_image); |
| 2374 | } |
| 2375 | (void) WriteBlobMSBLong(image,0); /* user mask data */ |
| 2376 | base_image->compression=compression; |
cristy | 144f1b6 | 2010-05-18 00:52:09 +0000 | [diff] [blame] | 2377 | } |
cristy | 144f1b6 | 2010-05-18 00:52:09 +0000 | [diff] [blame] | 2378 | /* |
| 2379 | Write composite image. |
| 2380 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2381 | status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse, |
| 2382 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2383 | (void) CloseBlob(image); |
| 2384 | return(status); |
| 2385 | } |