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