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