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