cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP IIIII CCCC TTTTT % |
| 7 | % P P I C T % |
| 8 | % PPPP I C T % |
| 9 | % P I C T % |
| 10 | % P IIIII CCCC T % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Apple Macintosh QuickDraw/PICT 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 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | 93b707b | 2017-12-06 07:05:51 -0500 | [diff] [blame] | 20 | % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
Cristy | f19d414 | 2017-04-24 11:34:30 -0400 | [diff] [blame] | 26 | % https://www.imagemagick.org/script/license.php % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/blob.h" |
| 44 | #include "MagickCore/blob-private.h" |
| 45 | #include "MagickCore/cache.h" |
| 46 | #include "MagickCore/color-private.h" |
| 47 | #include "MagickCore/colormap.h" |
| 48 | #include "MagickCore/colormap-private.h" |
| 49 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 50 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 51 | #include "MagickCore/composite.h" |
| 52 | #include "MagickCore/constitute.h" |
| 53 | #include "MagickCore/exception.h" |
| 54 | #include "MagickCore/exception-private.h" |
| 55 | #include "MagickCore/image.h" |
| 56 | #include "MagickCore/image-private.h" |
| 57 | #include "MagickCore/list.h" |
| 58 | #include "MagickCore/log.h" |
| 59 | #include "MagickCore/magick.h" |
| 60 | #include "MagickCore/memory_.h" |
| 61 | #include "MagickCore/monitor.h" |
| 62 | #include "MagickCore/monitor-private.h" |
| 63 | #include "MagickCore/pixel-accessor.h" |
| 64 | #include "MagickCore/profile.h" |
| 65 | #include "MagickCore/resource_.h" |
| 66 | #include "MagickCore/quantum-private.h" |
| 67 | #include "MagickCore/static.h" |
| 68 | #include "MagickCore/string_.h" |
| 69 | #include "MagickCore/module.h" |
| 70 | #include "MagickCore/transform.h" |
| 71 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | ImageMagick Macintosh PICT Methods. |
| 75 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 76 | typedef struct _PICTCode |
| 77 | { |
| 78 | const char |
| 79 | *name; |
| 80 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 81 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 82 | length; |
| 83 | |
| 84 | const char |
| 85 | *description; |
| 86 | } PICTCode; |
| 87 | |
| 88 | typedef struct _PICTPixmap |
| 89 | { |
| 90 | short |
| 91 | version, |
| 92 | pack_type; |
| 93 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 94 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 95 | pack_size, |
| 96 | horizontal_resolution, |
| 97 | vertical_resolution; |
| 98 | |
| 99 | short |
| 100 | pixel_type, |
| 101 | bits_per_pixel, |
| 102 | component_count, |
| 103 | component_size; |
| 104 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 105 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | plane_bytes, |
| 107 | table, |
| 108 | reserved; |
| 109 | } PICTPixmap; |
| 110 | |
| 111 | typedef struct _PICTRectangle |
| 112 | { |
| 113 | short |
| 114 | top, |
| 115 | left, |
| 116 | bottom, |
| 117 | right; |
| 118 | } PICTRectangle; |
| 119 | |
| 120 | static const PICTCode |
| 121 | codes[] = |
| 122 | { |
| 123 | /* 0x00 */ { "NOP", 0, "nop" }, |
| 124 | /* 0x01 */ { "Clip", 0, "clip" }, |
| 125 | /* 0x02 */ { "BkPat", 8, "background pattern" }, |
| 126 | /* 0x03 */ { "TxFont", 2, "text font (word)" }, |
| 127 | /* 0x04 */ { "TxFace", 1, "text face (byte)" }, |
| 128 | /* 0x05 */ { "TxMode", 2, "text mode (word)" }, |
| 129 | /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" }, |
| 130 | /* 0x07 */ { "PnSize", 4, "pen size (point)" }, |
| 131 | /* 0x08 */ { "PnMode", 2, "pen mode (word)" }, |
| 132 | /* 0x09 */ { "PnPat", 8, "pen pattern" }, |
| 133 | /* 0x0a */ { "FillPat", 8, "fill pattern" }, |
| 134 | /* 0x0b */ { "OvSize", 4, "oval size (point)" }, |
| 135 | /* 0x0c */ { "Origin", 4, "dh, dv (word)" }, |
| 136 | /* 0x0d */ { "TxSize", 2, "text size (word)" }, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 137 | /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" }, |
| 138 | /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" }, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" }, |
| 140 | /* 0x11 */ { "Version", 1, "version (byte)" }, |
| 141 | /* 0x12 */ { "BkPixPat", 0, "color background pattern" }, |
| 142 | /* 0x13 */ { "PnPixPat", 0, "color pen pattern" }, |
| 143 | /* 0x14 */ { "FillPixPat", 0, "color fill pattern" }, |
| 144 | /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" }, |
| 145 | /* 0x16 */ { "ChExtra", 2, "extra for each character" }, |
| 146 | /* 0x17 */ { "reserved", 0, "reserved for Apple use" }, |
| 147 | /* 0x18 */ { "reserved", 0, "reserved for Apple use" }, |
| 148 | /* 0x19 */ { "reserved", 0, "reserved for Apple use" }, |
| 149 | /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" }, |
| 150 | /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" }, |
| 151 | /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" }, |
| 152 | /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" }, |
| 153 | /* 0x1e */ { "DefHilite", 0, "Use default hilite color" }, |
| 154 | /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" }, |
| 155 | /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" }, |
| 156 | /* 0x21 */ { "LineFrom", 4, "newPt (point)" }, |
| 157 | /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" }, |
| 158 | /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" }, |
| 159 | /* 0x24 */ { "reserved", -1, "reserved for Apple use" }, |
| 160 | /* 0x25 */ { "reserved", -1, "reserved for Apple use" }, |
| 161 | /* 0x26 */ { "reserved", -1, "reserved for Apple use" }, |
| 162 | /* 0x27 */ { "reserved", -1, "reserved for Apple use" }, |
| 163 | /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" }, |
| 164 | /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" }, |
| 165 | /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" }, |
| 166 | /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" }, |
| 167 | /* 0x2c */ { "reserved", -1, "reserved for Apple use" }, |
| 168 | /* 0x2d */ { "reserved", -1, "reserved for Apple use" }, |
| 169 | /* 0x2e */ { "reserved", -1, "reserved for Apple use" }, |
| 170 | /* 0x2f */ { "reserved", -1, "reserved for Apple use" }, |
| 171 | /* 0x30 */ { "frameRect", 8, "rect" }, |
| 172 | /* 0x31 */ { "paintRect", 8, "rect" }, |
| 173 | /* 0x32 */ { "eraseRect", 8, "rect" }, |
| 174 | /* 0x33 */ { "invertRect", 8, "rect" }, |
| 175 | /* 0x34 */ { "fillRect", 8, "rect" }, |
| 176 | /* 0x35 */ { "reserved", 8, "reserved for Apple use" }, |
| 177 | /* 0x36 */ { "reserved", 8, "reserved for Apple use" }, |
| 178 | /* 0x37 */ { "reserved", 8, "reserved for Apple use" }, |
| 179 | /* 0x38 */ { "frameSameRect", 0, "rect" }, |
| 180 | /* 0x39 */ { "paintSameRect", 0, "rect" }, |
| 181 | /* 0x3a */ { "eraseSameRect", 0, "rect" }, |
| 182 | /* 0x3b */ { "invertSameRect", 0, "rect" }, |
| 183 | /* 0x3c */ { "fillSameRect", 0, "rect" }, |
| 184 | /* 0x3d */ { "reserved", 0, "reserved for Apple use" }, |
| 185 | /* 0x3e */ { "reserved", 0, "reserved for Apple use" }, |
| 186 | /* 0x3f */ { "reserved", 0, "reserved for Apple use" }, |
| 187 | /* 0x40 */ { "frameRRect", 8, "rect" }, |
| 188 | /* 0x41 */ { "paintRRect", 8, "rect" }, |
| 189 | /* 0x42 */ { "eraseRRect", 8, "rect" }, |
| 190 | /* 0x43 */ { "invertRRect", 8, "rect" }, |
| 191 | /* 0x44 */ { "fillRRrect", 8, "rect" }, |
| 192 | /* 0x45 */ { "reserved", 8, "reserved for Apple use" }, |
| 193 | /* 0x46 */ { "reserved", 8, "reserved for Apple use" }, |
| 194 | /* 0x47 */ { "reserved", 8, "reserved for Apple use" }, |
| 195 | /* 0x48 */ { "frameSameRRect", 0, "rect" }, |
| 196 | /* 0x49 */ { "paintSameRRect", 0, "rect" }, |
| 197 | /* 0x4a */ { "eraseSameRRect", 0, "rect" }, |
| 198 | /* 0x4b */ { "invertSameRRect", 0, "rect" }, |
| 199 | /* 0x4c */ { "fillSameRRect", 0, "rect" }, |
| 200 | /* 0x4d */ { "reserved", 0, "reserved for Apple use" }, |
| 201 | /* 0x4e */ { "reserved", 0, "reserved for Apple use" }, |
| 202 | /* 0x4f */ { "reserved", 0, "reserved for Apple use" }, |
| 203 | /* 0x50 */ { "frameOval", 8, "rect" }, |
| 204 | /* 0x51 */ { "paintOval", 8, "rect" }, |
| 205 | /* 0x52 */ { "eraseOval", 8, "rect" }, |
| 206 | /* 0x53 */ { "invertOval", 8, "rect" }, |
| 207 | /* 0x54 */ { "fillOval", 8, "rect" }, |
| 208 | /* 0x55 */ { "reserved", 8, "reserved for Apple use" }, |
| 209 | /* 0x56 */ { "reserved", 8, "reserved for Apple use" }, |
| 210 | /* 0x57 */ { "reserved", 8, "reserved for Apple use" }, |
| 211 | /* 0x58 */ { "frameSameOval", 0, "rect" }, |
| 212 | /* 0x59 */ { "paintSameOval", 0, "rect" }, |
| 213 | /* 0x5a */ { "eraseSameOval", 0, "rect" }, |
| 214 | /* 0x5b */ { "invertSameOval", 0, "rect" }, |
| 215 | /* 0x5c */ { "fillSameOval", 0, "rect" }, |
| 216 | /* 0x5d */ { "reserved", 0, "reserved for Apple use" }, |
| 217 | /* 0x5e */ { "reserved", 0, "reserved for Apple use" }, |
| 218 | /* 0x5f */ { "reserved", 0, "reserved for Apple use" }, |
| 219 | /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" }, |
| 220 | /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" }, |
| 221 | /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" }, |
| 222 | /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" }, |
| 223 | /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" }, |
| 224 | /* 0x65 */ { "reserved", 12, "reserved for Apple use" }, |
| 225 | /* 0x66 */ { "reserved", 12, "reserved for Apple use" }, |
| 226 | /* 0x67 */ { "reserved", 12, "reserved for Apple use" }, |
| 227 | /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" }, |
| 228 | /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" }, |
| 229 | /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" }, |
| 230 | /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" }, |
| 231 | /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" }, |
| 232 | /* 0x6d */ { "reserved", 4, "reserved for Apple use" }, |
| 233 | /* 0x6e */ { "reserved", 4, "reserved for Apple use" }, |
| 234 | /* 0x6f */ { "reserved", 4, "reserved for Apple use" }, |
| 235 | /* 0x70 */ { "framePoly", 0, "poly" }, |
| 236 | /* 0x71 */ { "paintPoly", 0, "poly" }, |
| 237 | /* 0x72 */ { "erasePoly", 0, "poly" }, |
| 238 | /* 0x73 */ { "invertPoly", 0, "poly" }, |
| 239 | /* 0x74 */ { "fillPoly", 0, "poly" }, |
| 240 | /* 0x75 */ { "reserved", 0, "reserved for Apple use" }, |
| 241 | /* 0x76 */ { "reserved", 0, "reserved for Apple use" }, |
| 242 | /* 0x77 */ { "reserved", 0, "reserved for Apple use" }, |
| 243 | /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" }, |
| 244 | /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" }, |
| 245 | /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" }, |
| 246 | /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" }, |
| 247 | /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" }, |
| 248 | /* 0x7d */ { "reserved", 0, "reserved for Apple use" }, |
| 249 | /* 0x7e */ { "reserved", 0, "reserved for Apple use" }, |
| 250 | /* 0x7f */ { "reserved", 0, "reserved for Apple use" }, |
| 251 | /* 0x80 */ { "frameRgn", 0, "region" }, |
| 252 | /* 0x81 */ { "paintRgn", 0, "region" }, |
| 253 | /* 0x82 */ { "eraseRgn", 0, "region" }, |
| 254 | /* 0x83 */ { "invertRgn", 0, "region" }, |
| 255 | /* 0x84 */ { "fillRgn", 0, "region" }, |
| 256 | /* 0x85 */ { "reserved", 0, "reserved for Apple use" }, |
| 257 | /* 0x86 */ { "reserved", 0, "reserved for Apple use" }, |
| 258 | /* 0x87 */ { "reserved", 0, "reserved for Apple use" }, |
| 259 | /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" }, |
| 260 | /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" }, |
| 261 | /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" }, |
| 262 | /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" }, |
| 263 | /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" }, |
| 264 | /* 0x8d */ { "reserved", 0, "reserved for Apple use" }, |
| 265 | /* 0x8e */ { "reserved", 0, "reserved for Apple use" }, |
| 266 | /* 0x8f */ { "reserved", 0, "reserved for Apple use" }, |
| 267 | /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" }, |
| 268 | /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" }, |
| 269 | /* 0x92 */ { "reserved", -1, "reserved for Apple use" }, |
| 270 | /* 0x93 */ { "reserved", -1, "reserved for Apple use" }, |
| 271 | /* 0x94 */ { "reserved", -1, "reserved for Apple use" }, |
| 272 | /* 0x95 */ { "reserved", -1, "reserved for Apple use" }, |
| 273 | /* 0x96 */ { "reserved", -1, "reserved for Apple use" }, |
| 274 | /* 0x97 */ { "reserved", -1, "reserved for Apple use" }, |
| 275 | /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" }, |
| 276 | /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" }, |
| 277 | /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" }, |
| 278 | /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" }, |
| 279 | /* 0x9c */ { "reserved", -1, "reserved for Apple use" }, |
| 280 | /* 0x9d */ { "reserved", -1, "reserved for Apple use" }, |
| 281 | /* 0x9e */ { "reserved", -1, "reserved for Apple use" }, |
| 282 | /* 0x9f */ { "reserved", -1, "reserved for Apple use" }, |
| 283 | /* 0xa0 */ { "ShortComment", 2, "kind (word)" }, |
| 284 | /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" } |
| 285 | }; |
| 286 | |
| 287 | /* |
| 288 | Forward declarations. |
| 289 | */ |
| 290 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 291 | WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 295 | % % |
| 296 | % % |
| 297 | % % |
| 298 | % D e c o d e I m a g e % |
| 299 | % % |
| 300 | % % |
| 301 | % % |
| 302 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 303 | % |
| 304 | % DecodeImage decompresses an image via Macintosh pack bits decoding for |
| 305 | % Macintosh PICT images. |
| 306 | % |
| 307 | % The format of the DecodeImage method is: |
| 308 | % |
| 309 | % unsigned char *DecodeImage(Image *blob,Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 310 | % size_t bytes_per_line,const int bits_per_pixel, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 311 | % unsigned size_t extent) |
| 312 | % |
| 313 | % A description of each parameter follows: |
| 314 | % |
| 315 | % o image_info: the image info. |
| 316 | % |
| 317 | % o blob,image: the address of a structure of type Image. |
| 318 | % |
| 319 | % o bytes_per_line: This integer identifies the number of bytes in a |
| 320 | % scanline. |
| 321 | % |
| 322 | % o bits_per_pixel: the number of bits in a pixel. |
| 323 | % |
| 324 | % o extent: the number of pixels allocated. |
| 325 | % |
| 326 | */ |
| 327 | |
| 328 | static unsigned char *ExpandBuffer(unsigned char *pixels, |
| 329 | MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel) |
| 330 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 331 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | i; |
| 333 | |
| 334 | register unsigned char |
| 335 | *p, |
| 336 | *q; |
| 337 | |
| 338 | static unsigned char |
| 339 | scanline[8*256]; |
| 340 | |
| 341 | p=pixels; |
| 342 | q=scanline; |
| 343 | switch (bits_per_pixel) |
| 344 | { |
| 345 | case 8: |
| 346 | case 16: |
| 347 | case 32: |
| 348 | return(pixels); |
| 349 | case 4: |
| 350 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 351 | for (i=0; i < (ssize_t) *bytes_per_line; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | { |
| 353 | *q++=(*p >> 4) & 0xff; |
| 354 | *q++=(*p & 15); |
| 355 | p++; |
| 356 | } |
| 357 | *bytes_per_line*=2; |
| 358 | break; |
| 359 | } |
| 360 | case 2: |
| 361 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 362 | for (i=0; i < (ssize_t) *bytes_per_line; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 363 | { |
| 364 | *q++=(*p >> 6) & 0x03; |
| 365 | *q++=(*p >> 4) & 0x03; |
| 366 | *q++=(*p >> 2) & 0x03; |
| 367 | *q++=(*p & 3); |
| 368 | p++; |
| 369 | } |
| 370 | *bytes_per_line*=4; |
| 371 | break; |
| 372 | } |
| 373 | case 1: |
| 374 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 375 | for (i=0; i < (ssize_t) *bytes_per_line; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | { |
| 377 | *q++=(*p >> 7) & 0x01; |
| 378 | *q++=(*p >> 6) & 0x01; |
| 379 | *q++=(*p >> 5) & 0x01; |
| 380 | *q++=(*p >> 4) & 0x01; |
| 381 | *q++=(*p >> 3) & 0x01; |
| 382 | *q++=(*p >> 2) & 0x01; |
| 383 | *q++=(*p >> 1) & 0x01; |
| 384 | *q++=(*p & 0x01); |
| 385 | p++; |
| 386 | } |
| 387 | *bytes_per_line*=8; |
| 388 | break; |
| 389 | } |
| 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | return(scanline); |
| 394 | } |
| 395 | |
| 396 | static unsigned char *DecodeImage(Image *blob,Image *image, |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 397 | size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent, |
| 398 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 399 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | MagickSizeType |
| 401 | number_pixels; |
| 402 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 403 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 404 | i; |
| 405 | |
| 406 | register unsigned char |
| 407 | *p, |
| 408 | *q; |
| 409 | |
| 410 | size_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 411 | bytes_per_pixel, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 412 | length, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 413 | row_bytes, |
| 414 | scanline_length, |
| 415 | width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | |
| 417 | ssize_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 418 | count, |
| 419 | j, |
| 420 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | |
| 422 | unsigned char |
| 423 | *pixels, |
| 424 | *scanline; |
| 425 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 426 | /* |
| 427 | Determine pixel buffer size. |
| 428 | */ |
| 429 | if (bits_per_pixel <= 8) |
| 430 | bytes_per_line&=0x7fff; |
| 431 | width=image->columns; |
| 432 | bytes_per_pixel=1; |
| 433 | if (bits_per_pixel == 16) |
| 434 | { |
| 435 | bytes_per_pixel=2; |
| 436 | width*=2; |
| 437 | } |
| 438 | else |
| 439 | if (bits_per_pixel == 32) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 440 | width*=image->alpha_trait ? 4 : 3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | if (bytes_per_line == 0) |
| 442 | bytes_per_line=width; |
| 443 | row_bytes=(size_t) (image->columns | 0x8000); |
| 444 | if (image->storage_class == DirectClass) |
| 445 | row_bytes=(size_t) ((4*image->columns) | 0x8000); |
| 446 | /* |
| 447 | Allocate pixel and scanline buffer. |
| 448 | */ |
| 449 | pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes* |
| 450 | sizeof(*pixels)); |
| 451 | if (pixels == (unsigned char *) NULL) |
| 452 | return((unsigned char *) NULL); |
| 453 | *extent=row_bytes*image->rows*sizeof(*pixels); |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 454 | (void) memset(pixels,0,*extent); |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 455 | scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,2* |
| 456 | sizeof(*scanline)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 457 | if (scanline == (unsigned char *) NULL) |
Cristy | 5ebe27c | 2017-09-30 11:30:43 -0400 | [diff] [blame] | 458 | { |
| 459 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 460 | return((unsigned char *) NULL); |
| 461 | } |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 462 | (void) memset(scanline,0,2*row_bytes*sizeof(*scanline)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | if (bytes_per_line < 8) |
| 464 | { |
| 465 | /* |
| 466 | Pixels are already uncompressed. |
| 467 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 468 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 469 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 470 | q=pixels+y*width*GetPixelChannels(image);; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 471 | number_pixels=bytes_per_line; |
| 472 | count=ReadBlob(blob,(size_t) number_pixels,scanline); |
cristy | 6d62e4b | 2015-01-11 14:07:17 +0000 | [diff] [blame] | 473 | if (count != (ssize_t) number_pixels) |
| 474 | { |
| 475 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 476 | CorruptImageError,"UnableToUncompressImage","`%s'", |
| 477 | image->filename); |
| 478 | break; |
| 479 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 480 | p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel); |
| 481 | if ((q+number_pixels) > (pixels+(*extent))) |
| 482 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 483 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 484 | CorruptImageError,"UnableToUncompressImage","`%s'", |
| 485 | image->filename); |
| 486 | break; |
| 487 | } |
Cristy | fc31dc5 | 2018-03-10 10:42:46 -0500 | [diff] [blame] | 488 | (void) memcpy(q,p,(size_t) number_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | } |
| 490 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 491 | return(pixels); |
| 492 | } |
| 493 | /* |
| 494 | Uncompress RLE pixels into uncompressed pixel buffer. |
| 495 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 496 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | { |
| 498 | q=pixels+y*width; |
| 499 | if (bytes_per_line > 200) |
| 500 | scanline_length=ReadBlobMSBShort(blob); |
| 501 | else |
| 502 | scanline_length=1UL*ReadBlobByte(blob); |
Dirk Lemstra | c2c1622 | 2018-03-16 12:06:03 +0100 | [diff] [blame^] | 503 | if ((scanline_length >= row_bytes) || (scanline_length == 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 505 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | CorruptImageError,"UnableToUncompressImage","`%s'",image->filename); |
| 507 | break; |
| 508 | } |
| 509 | count=ReadBlob(blob,scanline_length,scanline); |
cristy | 6d62e4b | 2015-01-11 14:07:17 +0000 | [diff] [blame] | 510 | if (count != (ssize_t) scanline_length) |
| 511 | { |
| 512 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 513 | CorruptImageError,"UnableToUncompressImage","`%s'",image->filename); |
| 514 | break; |
| 515 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 516 | for (j=0; j < (ssize_t) scanline_length; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | if ((scanline[j] & 0x80) == 0) |
| 518 | { |
| 519 | length=(size_t) ((scanline[j] & 0xff)+1); |
| 520 | number_pixels=length*bytes_per_pixel; |
| 521 | p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel); |
| 522 | if ((q-pixels+number_pixels) <= *extent) |
Cristy | fc31dc5 | 2018-03-10 10:42:46 -0500 | [diff] [blame] | 523 | (void) memcpy(q,p,(size_t) number_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | q+=number_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 525 | j+=(ssize_t) (length*bytes_per_pixel+1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | } |
| 527 | else |
| 528 | { |
| 529 | length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2); |
| 530 | number_pixels=bytes_per_pixel; |
| 531 | p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 532 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | { |
| 534 | if ((q-pixels+number_pixels) <= *extent) |
Cristy | fc31dc5 | 2018-03-10 10:42:46 -0500 | [diff] [blame] | 535 | (void) memcpy(q,p,(size_t) number_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | q+=number_pixels; |
| 537 | } |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 538 | j+=(ssize_t) bytes_per_pixel+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 542 | return(pixels); |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 547 | % % |
| 548 | % % |
| 549 | % % |
| 550 | % E n c o d e I m a g e % |
| 551 | % % |
| 552 | % % |
| 553 | % % |
| 554 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 555 | % |
| 556 | % EncodeImage compresses an image via Macintosh pack bits encoding |
| 557 | % for Macintosh PICT images. |
| 558 | % |
| 559 | % The format of the EncodeImage method is: |
| 560 | % |
| 561 | % size_t EncodeImage(Image *image,const unsigned char *scanline, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 562 | % const size_t bytes_per_line,unsigned char *pixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 563 | % |
| 564 | % A description of each parameter follows: |
| 565 | % |
| 566 | % o image: the address of a structure of type Image. |
| 567 | % |
| 568 | % o scanline: A pointer to an array of characters to pack. |
| 569 | % |
| 570 | % o bytes_per_line: the number of bytes in a scanline. |
| 571 | % |
| 572 | % o pixels: A pointer to an array of characters where the packed |
| 573 | % characters are stored. |
| 574 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | */ |
| 576 | static size_t EncodeImage(Image *image,const unsigned char *scanline, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 577 | const size_t bytes_per_line,unsigned char *pixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | { |
| 579 | #define MaxCount 128 |
| 580 | #define MaxPackbitsRunlength 128 |
| 581 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 582 | register const unsigned char |
| 583 | *p; |
| 584 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 585 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | i; |
| 587 | |
| 588 | register unsigned char |
| 589 | *q; |
| 590 | |
| 591 | size_t |
| 592 | length; |
| 593 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 594 | ssize_t |
| 595 | count, |
| 596 | repeat_count, |
| 597 | runlength; |
| 598 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | unsigned char |
| 600 | index; |
| 601 | |
| 602 | /* |
| 603 | Pack scanline. |
| 604 | */ |
| 605 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 606 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | if (image->debug != MagickFalse) |
| 608 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 609 | assert(scanline != (unsigned char *) NULL); |
| 610 | assert(pixels != (unsigned char *) NULL); |
| 611 | count=0; |
| 612 | runlength=0; |
| 613 | p=scanline+(bytes_per_line-1); |
| 614 | q=pixels; |
| 615 | index=(*p); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 616 | for (i=(ssize_t) bytes_per_line-1; i >= 0; i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | { |
| 618 | if (index == *p) |
| 619 | runlength++; |
| 620 | else |
| 621 | { |
| 622 | if (runlength < 3) |
| 623 | while (runlength > 0) |
| 624 | { |
| 625 | *q++=(unsigned char) index; |
| 626 | runlength--; |
| 627 | count++; |
| 628 | if (count == MaxCount) |
| 629 | { |
| 630 | *q++=(unsigned char) (MaxCount-1); |
| 631 | count-=MaxCount; |
| 632 | } |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | if (count > 0) |
| 637 | *q++=(unsigned char) (count-1); |
| 638 | count=0; |
| 639 | while (runlength > 0) |
| 640 | { |
| 641 | repeat_count=runlength; |
| 642 | if (repeat_count > MaxPackbitsRunlength) |
| 643 | repeat_count=MaxPackbitsRunlength; |
| 644 | *q++=(unsigned char) index; |
| 645 | *q++=(unsigned char) (257-repeat_count); |
| 646 | runlength-=repeat_count; |
| 647 | } |
| 648 | } |
| 649 | runlength=1; |
| 650 | } |
| 651 | index=(*p); |
| 652 | p--; |
| 653 | } |
| 654 | if (runlength < 3) |
| 655 | while (runlength > 0) |
| 656 | { |
| 657 | *q++=(unsigned char) index; |
| 658 | runlength--; |
| 659 | count++; |
| 660 | if (count == MaxCount) |
| 661 | { |
| 662 | *q++=(unsigned char) (MaxCount-1); |
| 663 | count-=MaxCount; |
| 664 | } |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | if (count > 0) |
| 669 | *q++=(unsigned char) (count-1); |
| 670 | count=0; |
| 671 | while (runlength > 0) |
| 672 | { |
| 673 | repeat_count=runlength; |
| 674 | if (repeat_count > MaxPackbitsRunlength) |
| 675 | repeat_count=MaxPackbitsRunlength; |
| 676 | *q++=(unsigned char) index; |
| 677 | *q++=(unsigned char) (257-repeat_count); |
| 678 | runlength-=repeat_count; |
| 679 | } |
| 680 | } |
| 681 | if (count > 0) |
| 682 | *q++=(unsigned char) (count-1); |
| 683 | /* |
| 684 | Write the number of and the packed length. |
| 685 | */ |
| 686 | length=(size_t) (q-pixels); |
| 687 | if (bytes_per_line > 200) |
| 688 | { |
| 689 | (void) WriteBlobMSBShort(image,(unsigned short) length); |
| 690 | length+=2; |
| 691 | } |
| 692 | else |
| 693 | { |
| 694 | (void) WriteBlobByte(image,(unsigned char) length); |
| 695 | length++; |
| 696 | } |
| 697 | while (q != pixels) |
| 698 | { |
| 699 | q--; |
| 700 | (void) WriteBlobByte(image,*q); |
| 701 | } |
| 702 | return(length); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 707 | % % |
| 708 | % % |
| 709 | % % |
| 710 | % I s P I C T % |
| 711 | % % |
| 712 | % % |
| 713 | % % |
| 714 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 715 | % |
| 716 | % IsPICT()() returns MagickTrue if the image format type, identified by the |
| 717 | % magick string, is PICT. |
| 718 | % |
| 719 | % The format of the ReadPICTImage method is: |
| 720 | % |
| 721 | % MagickBooleanType IsPICT(const unsigned char *magick,const size_t length) |
| 722 | % |
| 723 | % A description of each parameter follows: |
| 724 | % |
| 725 | % o magick: compare image format pattern against these bytes. |
| 726 | % |
| 727 | % o length: Specifies the length of the magick string. |
| 728 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 729 | */ |
| 730 | static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length) |
| 731 | { |
cristy | 80af6b6 | 2012-11-12 12:48:16 +0000 | [diff] [blame] | 732 | if (length < 12) |
| 733 | return(MagickFalse); |
| 734 | /* |
| 735 | Embedded OLE2 macintosh have "PICT" instead of 512 platform header. |
| 736 | */ |
| 737 | if (memcmp(magick,"PICT",4) == 0) |
| 738 | return(MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 739 | if (length < 528) |
| 740 | return(MagickFalse); |
| 741 | if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0) |
| 742 | return(MagickTrue); |
| 743 | return(MagickFalse); |
| 744 | } |
| 745 | |
| 746 | #if !defined(macintosh) |
| 747 | /* |
| 748 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 749 | % % |
| 750 | % % |
| 751 | % % |
| 752 | % R e a d P I C T I m a g e % |
| 753 | % % |
| 754 | % % |
| 755 | % % |
| 756 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 757 | % |
| 758 | % ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file |
| 759 | % and returns it. It allocates the memory necessary for the new Image |
| 760 | % structure and returns a pointer to the new image. |
| 761 | % |
| 762 | % The format of the ReadPICTImage method is: |
| 763 | % |
| 764 | % Image *ReadPICTImage(const ImageInfo *image_info, |
| 765 | % ExceptionInfo *exception) |
| 766 | % |
| 767 | % A description of each parameter follows: |
| 768 | % |
| 769 | % o image_info: the image info. |
| 770 | % |
| 771 | % o exception: return any errors or warnings in this structure. |
| 772 | % |
| 773 | */ |
| 774 | |
Cristy | ffcb8f8 | 2017-07-25 07:46:37 -0400 | [diff] [blame] | 775 | static MagickBooleanType ReadPixmap(Image *image,PICTPixmap *pixmap) |
| 776 | { |
| 777 | pixmap->version=(short) ReadBlobMSBShort(image); |
| 778 | pixmap->pack_type=(short) ReadBlobMSBShort(image); |
| 779 | pixmap->pack_size=ReadBlobMSBLong(image); |
| 780 | pixmap->horizontal_resolution=1UL*ReadBlobMSBShort(image); |
| 781 | (void) ReadBlobMSBShort(image); |
| 782 | pixmap->vertical_resolution=1UL*ReadBlobMSBShort(image); |
| 783 | (void) ReadBlobMSBShort(image); |
| 784 | pixmap->pixel_type=(short) ReadBlobMSBShort(image); |
| 785 | pixmap->bits_per_pixel=(short) ReadBlobMSBShort(image); |
| 786 | pixmap->component_count=(short) ReadBlobMSBShort(image); |
| 787 | pixmap->component_size=(short) ReadBlobMSBShort(image); |
| 788 | pixmap->plane_bytes=ReadBlobMSBLong(image); |
| 789 | pixmap->table=ReadBlobMSBLong(image); |
| 790 | pixmap->reserved=ReadBlobMSBLong(image); |
| 791 | if ((EOFBlob(image) != MagickFalse) || (pixmap->bits_per_pixel <= 0) || |
| 792 | (pixmap->bits_per_pixel > 32) || (pixmap->component_count <= 0) || |
| 793 | (pixmap->component_count > 4) || (pixmap->component_size <= 0)) |
| 794 | return(MagickFalse); |
| 795 | return(MagickTrue); |
| 796 | } |
| 797 | |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 798 | static MagickBooleanType ReadRectangle(Image *image,PICTRectangle *rectangle) |
| 799 | { |
| 800 | rectangle->top=(short) ReadBlobMSBShort(image); |
| 801 | rectangle->left=(short) ReadBlobMSBShort(image); |
| 802 | rectangle->bottom=(short) ReadBlobMSBShort(image); |
| 803 | rectangle->right=(short) ReadBlobMSBShort(image); |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 804 | if ((EOFBlob(image) != MagickFalse) || |
Cristy | d813fea | 2018-02-26 20:57:21 -0500 | [diff] [blame] | 805 | ((rectangle->bottom-rectangle->top) <= 0) || |
| 806 | ((rectangle->right-rectangle->left) <= 0)) |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 807 | return(MagickFalse); |
| 808 | return(MagickTrue); |
| 809 | } |
| 810 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | static Image *ReadPICTImage(const ImageInfo *image_info, |
| 812 | ExceptionInfo *exception) |
| 813 | { |
| 814 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 815 | geometry[MagickPathExtent], |
cristy | 80af6b6 | 2012-11-12 12:48:16 +0000 | [diff] [blame] | 816 | header_ole[4]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 817 | |
| 818 | Image |
| 819 | *image; |
| 820 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 821 | int |
| 822 | c, |
| 823 | code; |
| 824 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 825 | MagickBooleanType |
| 826 | jpeg, |
| 827 | status; |
| 828 | |
| 829 | PICTRectangle |
| 830 | frame; |
| 831 | |
| 832 | PICTPixmap |
| 833 | pixmap; |
| 834 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 835 | Quantum |
| 836 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 838 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 839 | *q; |
| 840 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 841 | register ssize_t |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 842 | i, |
| 843 | x; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 844 | |
| 845 | size_t |
| 846 | extent, |
| 847 | length; |
| 848 | |
| 849 | ssize_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 850 | count, |
| 851 | flags, |
| 852 | j, |
| 853 | version, |
| 854 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 855 | |
| 856 | StringInfo |
| 857 | *profile; |
| 858 | |
| 859 | /* |
| 860 | Open image file. |
| 861 | */ |
| 862 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 863 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | if (image_info->debug != MagickFalse) |
| 865 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 866 | image_info->filename); |
| 867 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 868 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 869 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 870 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 871 | if (status == MagickFalse) |
| 872 | { |
| 873 | image=DestroyImageList(image); |
| 874 | return((Image *) NULL); |
| 875 | } |
| 876 | /* |
| 877 | Read PICT header. |
| 878 | */ |
| 879 | pixmap.bits_per_pixel=0; |
| 880 | pixmap.component_count=0; |
cristy | 80af6b6 | 2012-11-12 12:48:16 +0000 | [diff] [blame] | 881 | /* |
| 882 | Skip header : 512 for standard PICT and 4, ie "PICT" for OLE2. |
| 883 | */ |
| 884 | header_ole[0]=ReadBlobByte(image); |
| 885 | header_ole[1]=ReadBlobByte(image); |
| 886 | header_ole[2]=ReadBlobByte(image); |
| 887 | header_ole[3]=ReadBlobByte(image); |
| 888 | if (!((header_ole[0] == 0x50) && (header_ole[1] == 0x49) && |
| 889 | (header_ole[2] == 0x43) && (header_ole[3] == 0x54 ))) |
| 890 | for (i=0; i < 508; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 891 | if (ReadBlobByte(image) == EOF) |
| 892 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 893 | (void) ReadBlobMSBShort(image); /* skip picture size */ |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 894 | if (ReadRectangle(image,&frame) == MagickFalse) |
| 895 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | while ((c=ReadBlobByte(image)) == 0) ; |
| 897 | if (c != 0x11) |
| 898 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 899 | version=ReadBlobByte(image); |
| 900 | if (version == 2) |
| 901 | { |
| 902 | c=ReadBlobByte(image); |
| 903 | if (c != 0xff) |
| 904 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 905 | } |
| 906 | else |
| 907 | if (version != 1) |
| 908 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 909 | if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) || |
| 910 | (frame.bottom < 0) || (frame.left >= frame.right) || |
| 911 | (frame.top >= frame.bottom)) |
| 912 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 913 | /* |
| 914 | Create black canvas. |
| 915 | */ |
| 916 | flags=0; |
cristy | 65cb963 | 2012-12-08 00:34:29 +0000 | [diff] [blame] | 917 | image->depth=8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | image->columns=1UL*(frame.right-frame.left); |
| 919 | image->rows=1UL*(frame.bottom-frame.top); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 920 | image->resolution.x=DefaultResolution; |
| 921 | image->resolution.y=DefaultResolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 922 | image->units=UndefinedResolution; |
cristy | d6d3b0d | 2015-01-01 17:46:31 +0000 | [diff] [blame] | 923 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 924 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 925 | { |
| 926 | (void) CloseBlob(image); |
| 927 | return(GetFirstImageInList(image)); |
| 928 | } |
| 929 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 930 | if (status == MagickFalse) |
| 931 | return(DestroyImageList(image)); |
Cristy | 820e636 | 2018-02-09 07:49:07 -0500 | [diff] [blame] | 932 | image->alpha_trait=BlendPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 933 | /* |
| 934 | Interpret PICT opcodes. |
| 935 | */ |
| 936 | jpeg=MagickFalse; |
| 937 | for (code=0; EOFBlob(image) == MagickFalse; ) |
| 938 | { |
| 939 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 940 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 941 | break; |
| 942 | if ((version == 1) || ((TellBlob(image) % 2) != 0)) |
| 943 | code=ReadBlobByte(image); |
| 944 | if (version == 2) |
Cristy | 1fe0b87 | 2016-04-23 10:48:31 -0400 | [diff] [blame] | 945 | code=ReadBlobMSBSignedShort(image); |
cristy | f9f8502 | 2014-05-18 12:46:29 +0000 | [diff] [blame] | 946 | if (code < 0) |
| 947 | break; |
Dirk Lemstra | c2c1622 | 2018-03-16 12:06:03 +0100 | [diff] [blame^] | 948 | if (code == 0) |
| 949 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | if (code > 0xa1) |
| 951 | { |
| 952 | if (image->debug != MagickFalse) |
| 953 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code); |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | if (image->debug != MagickFalse) |
| 958 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 959 | " %04X %s: %s",code,codes[code].name,codes[code].description); |
| 960 | switch (code) |
| 961 | { |
| 962 | case 0x01: |
| 963 | { |
| 964 | /* |
| 965 | Clipping rectangle. |
| 966 | */ |
| 967 | length=ReadBlobMSBShort(image); |
| 968 | if (length != 0x000a) |
| 969 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 970 | for (i=0; i < (ssize_t) (length-2); i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 971 | if (ReadBlobByte(image) == EOF) |
| 972 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 975 | if (ReadRectangle(image,&frame) == MagickFalse) |
| 976 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 977 | if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0)) |
| 978 | break; |
| 979 | image->columns=1UL*(frame.right-frame.left); |
| 980 | image->rows=1UL*(frame.bottom-frame.top); |
Cristy | 2b008a8 | 2015-09-07 09:51:07 -0400 | [diff] [blame] | 981 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 982 | if (status == MagickFalse) |
| 983 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 984 | break; |
| 985 | } |
| 986 | case 0x12: |
| 987 | case 0x13: |
| 988 | case 0x14: |
| 989 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 990 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | pattern; |
| 992 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 993 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 994 | height, |
| 995 | width; |
| 996 | |
| 997 | /* |
| 998 | Skip pattern definition. |
| 999 | */ |
| 1000 | pattern=1L*ReadBlobMSBShort(image); |
| 1001 | for (i=0; i < 8; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1002 | if (ReadBlobByte(image) == EOF) |
| 1003 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | if (pattern == 2) |
| 1005 | { |
| 1006 | for (i=0; i < 5; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1007 | if (ReadBlobByte(image) == EOF) |
| 1008 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | if (pattern != 1) |
| 1012 | ThrowReaderException(CorruptImageError,"UnknownPatternType"); |
| 1013 | length=ReadBlobMSBShort(image); |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1014 | if (ReadRectangle(image,&frame) == MagickFalse) |
| 1015 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
Cristy | ffcb8f8 | 2017-07-25 07:46:37 -0400 | [diff] [blame] | 1016 | if (ReadPixmap(image,&pixmap) == MagickFalse) |
| 1017 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1018 | image->depth=1UL*pixmap.component_size; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1019 | image->resolution.x=1.0*pixmap.horizontal_resolution; |
| 1020 | image->resolution.y=1.0*pixmap.vertical_resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1021 | image->units=PixelsPerInchResolution; |
| 1022 | (void) ReadBlobMSBLong(image); |
| 1023 | flags=1L*ReadBlobMSBShort(image); |
| 1024 | length=ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1025 | for (i=0; i <= (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1026 | (void) ReadBlobMSBLong(image); |
| 1027 | width=1UL*(frame.bottom-frame.top); |
| 1028 | height=1UL*(frame.right-frame.left); |
| 1029 | if (pixmap.bits_per_pixel <= 8) |
| 1030 | length&=0x7fff; |
| 1031 | if (pixmap.bits_per_pixel == 16) |
| 1032 | width<<=1; |
| 1033 | if (length == 0) |
| 1034 | length=width; |
| 1035 | if (length < 8) |
| 1036 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1037 | for (i=0; i < (ssize_t) (length*height); i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1038 | if (ReadBlobByte(image) == EOF) |
| 1039 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | } |
| 1041 | else |
MITSUNARI Shigeo | de99d49 | 2017-11-17 21:54:07 +0900 | [diff] [blame] | 1042 | for (i=0; i < (int) height; i++) |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 1043 | { |
| 1044 | if (EOFBlob(image) != MagickFalse) |
| 1045 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1046 | if (length > 200) |
cristy | 46a5434 | 2015-03-29 00:16:16 +0000 | [diff] [blame] | 1047 | { |
| 1048 | for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++) |
| 1049 | if (ReadBlobByte(image) == EOF) |
| 1050 | break; |
| 1051 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1052 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1053 | for (j=0; j < (ssize_t) ReadBlobByte(image); j++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1054 | if (ReadBlobByte(image) == EOF) |
| 1055 | break; |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 1056 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
| 1059 | case 0x1b: |
| 1060 | { |
| 1061 | /* |
| 1062 | Initialize image background color. |
| 1063 | */ |
| 1064 | image->background_color.red=(Quantum) |
| 1065 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1066 | image->background_color.green=(Quantum) |
| 1067 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1068 | image->background_color.blue=(Quantum) |
| 1069 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1070 | break; |
| 1071 | } |
| 1072 | case 0x70: |
| 1073 | case 0x71: |
| 1074 | case 0x72: |
| 1075 | case 0x73: |
| 1076 | case 0x74: |
| 1077 | case 0x75: |
| 1078 | case 0x76: |
| 1079 | case 0x77: |
| 1080 | { |
| 1081 | /* |
| 1082 | Skip polygon or region. |
| 1083 | */ |
| 1084 | length=ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1085 | for (i=0; i < (ssize_t) (length-2); i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1086 | if (ReadBlobByte(image) == EOF) |
| 1087 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | } |
| 1090 | case 0x90: |
| 1091 | case 0x91: |
| 1092 | case 0x98: |
| 1093 | case 0x99: |
| 1094 | case 0x9a: |
| 1095 | case 0x9b: |
| 1096 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1097 | Image |
| 1098 | *tile_image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1099 | |
| 1100 | PICTRectangle |
| 1101 | source, |
| 1102 | destination; |
| 1103 | |
| 1104 | register unsigned char |
| 1105 | *p; |
| 1106 | |
| 1107 | size_t |
| 1108 | j; |
| 1109 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1110 | ssize_t |
| 1111 | bytes_per_line; |
| 1112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1113 | unsigned char |
| 1114 | *pixels; |
| 1115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1116 | /* |
| 1117 | Pixmap clipped by a rectangle. |
| 1118 | */ |
| 1119 | bytes_per_line=0; |
| 1120 | if ((code != 0x9a) && (code != 0x9b)) |
| 1121 | bytes_per_line=1L*ReadBlobMSBShort(image); |
| 1122 | else |
| 1123 | { |
| 1124 | (void) ReadBlobMSBShort(image); |
| 1125 | (void) ReadBlobMSBShort(image); |
| 1126 | (void) ReadBlobMSBShort(image); |
| 1127 | } |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1128 | if (ReadRectangle(image,&frame) == MagickFalse) |
| 1129 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | /* |
| 1131 | Initialize tile image. |
| 1132 | */ |
| 1133 | tile_image=CloneImage(image,1UL*(frame.right-frame.left), |
| 1134 | 1UL*(frame.bottom-frame.top),MagickTrue,exception); |
| 1135 | if (tile_image == (Image *) NULL) |
Cristy | 2ba0857 | 2017-11-24 09:17:19 -0500 | [diff] [blame] | 1136 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1137 | if ((code == 0x9a) || (code == 0x9b) || |
| 1138 | ((bytes_per_line & 0x8000) != 0)) |
| 1139 | { |
Cristy | ffcb8f8 | 2017-07-25 07:46:37 -0400 | [diff] [blame] | 1140 | if (ReadPixmap(image,&pixmap) == MagickFalse) |
| 1141 | { |
| 1142 | tile_image=DestroyImage(tile_image); |
| 1143 | ThrowReaderException(CorruptImageError, |
| 1144 | "ImproperImageHeader"); |
| 1145 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1146 | tile_image->depth=1UL*pixmap.component_size; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1147 | tile_image->alpha_trait=pixmap.component_count == 4 ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 1148 | BlendPixelTrait : UndefinedPixelTrait; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1149 | tile_image->resolution.x=(double) pixmap.horizontal_resolution; |
| 1150 | tile_image->resolution.y=(double) pixmap.vertical_resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1151 | tile_image->units=PixelsPerInchResolution; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1152 | if (tile_image->alpha_trait != UndefinedPixelTrait) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1153 | image->alpha_trait=tile_image->alpha_trait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1154 | } |
| 1155 | if ((code != 0x9a) && (code != 0x9b)) |
| 1156 | { |
| 1157 | /* |
| 1158 | Initialize colormap. |
| 1159 | */ |
| 1160 | tile_image->colors=2; |
| 1161 | if ((bytes_per_line & 0x8000) != 0) |
| 1162 | { |
| 1163 | (void) ReadBlobMSBLong(image); |
| 1164 | flags=1L*ReadBlobMSBShort(image); |
| 1165 | tile_image->colors=1UL*ReadBlobMSBShort(image)+1; |
| 1166 | } |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1167 | status=AcquireImageColormap(tile_image,tile_image->colors, |
| 1168 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1169 | if (status == MagickFalse) |
| 1170 | { |
| 1171 | tile_image=DestroyImage(tile_image); |
| 1172 | ThrowReaderException(ResourceLimitError, |
| 1173 | "MemoryAllocationFailed"); |
| 1174 | } |
| 1175 | if ((bytes_per_line & 0x8000) != 0) |
| 1176 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1177 | for (i=0; i < (ssize_t) tile_image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1178 | { |
| 1179 | j=ReadBlobMSBShort(image) % tile_image->colors; |
| 1180 | if ((flags & 0x8000) != 0) |
| 1181 | j=(size_t) i; |
| 1182 | tile_image->colormap[j].red=(Quantum) |
| 1183 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1184 | tile_image->colormap[j].green=(Quantum) |
| 1185 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1186 | tile_image->colormap[j].blue=(Quantum) |
| 1187 | ScaleShortToQuantum(ReadBlobMSBShort(image)); |
| 1188 | } |
| 1189 | } |
| 1190 | else |
| 1191 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1192 | for (i=0; i < (ssize_t) tile_image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1193 | { |
| 1194 | tile_image->colormap[i].red=(Quantum) (QuantumRange- |
| 1195 | tile_image->colormap[i].red); |
| 1196 | tile_image->colormap[i].green=(Quantum) (QuantumRange- |
| 1197 | tile_image->colormap[i].green); |
| 1198 | tile_image->colormap[i].blue=(Quantum) (QuantumRange- |
| 1199 | tile_image->colormap[i].blue); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1203 | if (ReadRectangle(image,&source) == MagickFalse) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1204 | { |
| 1205 | tile_image=DestroyImage(tile_image); |
| 1206 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1207 | } |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1208 | if (ReadRectangle(image,&destination) == MagickFalse) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1209 | { |
| 1210 | tile_image=DestroyImage(tile_image); |
| 1211 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1212 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1213 | (void) ReadBlobMSBShort(image); |
| 1214 | if ((code == 0x91) || (code == 0x99) || (code == 0x9b)) |
| 1215 | { |
| 1216 | /* |
| 1217 | Skip region. |
| 1218 | */ |
| 1219 | length=ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1220 | for (i=0; i < (ssize_t) (length-2); i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1221 | if (ReadBlobByte(image) == EOF) |
| 1222 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | } |
| 1224 | if ((code != 0x9a) && (code != 0x9b) && |
| 1225 | (bytes_per_line & 0x8000) == 0) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1226 | pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent, |
| 1227 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1228 | else |
| 1229 | pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U* |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1230 | pixmap.bits_per_pixel,&extent,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1231 | if (pixels == (unsigned char *) NULL) |
| 1232 | { |
| 1233 | tile_image=DestroyImage(tile_image); |
| 1234 | ThrowReaderException(ResourceLimitError, |
| 1235 | "MemoryAllocationFailed"); |
| 1236 | } |
| 1237 | /* |
| 1238 | Convert PICT tile image to pixel packets. |
| 1239 | */ |
| 1240 | p=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1241 | for (y=0; y < (ssize_t) tile_image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1242 | { |
| 1243 | if (p > (pixels+extent+image->columns)) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1244 | { |
| 1245 | tile_image=DestroyImage(tile_image); |
| 1246 | ThrowReaderException(CorruptImageError,"NotEnoughPixelData"); |
| 1247 | } |
cristy | 3669d04 | 2009-10-16 01:36:31 +0000 | [diff] [blame] | 1248 | q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1, |
| 1249 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1250 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1251 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1252 | for (x=0; x < (ssize_t) tile_image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1253 | { |
| 1254 | if (tile_image->storage_class == PseudoClass) |
| 1255 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1256 | index=ConstrainColormapIndex(tile_image,*p,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1257 | SetPixelIndex(tile_image,index,q); |
| 1258 | SetPixelRed(tile_image, |
| 1259 | tile_image->colormap[(ssize_t) index].red,q); |
| 1260 | SetPixelGreen(tile_image, |
| 1261 | tile_image->colormap[(ssize_t) index].green,q); |
| 1262 | SetPixelBlue(tile_image, |
| 1263 | tile_image->colormap[(ssize_t) index].blue,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1264 | } |
| 1265 | else |
| 1266 | { |
| 1267 | if (pixmap.bits_per_pixel == 16) |
| 1268 | { |
| 1269 | i=(*p++); |
| 1270 | j=(*p); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1271 | SetPixelRed(tile_image,ScaleCharToQuantum( |
| 1272 | (unsigned char) ((i & 0x7c) << 1)),q); |
| 1273 | SetPixelGreen(tile_image,ScaleCharToQuantum( |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 1274 | (unsigned char) (((i & 0x03) << 6) | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1275 | ((j & 0xe0) >> 2))),q); |
| 1276 | SetPixelBlue(tile_image,ScaleCharToQuantum( |
| 1277 | (unsigned char) ((j & 0x1f) << 3)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1278 | } |
| 1279 | else |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1280 | if (tile_image->alpha_trait == UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1281 | { |
| 1282 | if (p > (pixels+extent+2*image->columns)) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1283 | { |
| 1284 | tile_image=DestroyImage(tile_image); |
| 1285 | ThrowReaderException(CorruptImageError, |
| 1286 | "NotEnoughPixelData"); |
| 1287 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1288 | SetPixelRed(tile_image,ScaleCharToQuantum(*p),q); |
| 1289 | SetPixelGreen(tile_image,ScaleCharToQuantum( |
| 1290 | *(p+tile_image->columns)),q); |
| 1291 | SetPixelBlue(tile_image,ScaleCharToQuantum( |
| 1292 | *(p+2*tile_image->columns)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1293 | } |
| 1294 | else |
| 1295 | { |
| 1296 | if (p > (pixels+extent+3*image->columns)) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1297 | { |
| 1298 | tile_image=DestroyImage(tile_image); |
| 1299 | ThrowReaderException(CorruptImageError, |
| 1300 | "NotEnoughPixelData"); |
| 1301 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1302 | SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q); |
| 1303 | SetPixelRed(tile_image,ScaleCharToQuantum( |
| 1304 | *(p+tile_image->columns)),q); |
| 1305 | SetPixelGreen(tile_image,ScaleCharToQuantum( |
| 1306 | *(p+2*tile_image->columns)),q); |
| 1307 | SetPixelBlue(tile_image,ScaleCharToQuantum( |
| 1308 | *(p+3*tile_image->columns)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1312 | q+=GetPixelChannels(tile_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1313 | } |
| 1314 | if (SyncAuthenticPixels(tile_image,exception) == MagickFalse) |
| 1315 | break; |
| 1316 | if ((tile_image->storage_class == DirectClass) && |
| 1317 | (pixmap.bits_per_pixel != 16)) |
| 1318 | { |
| 1319 | p+=(pixmap.component_count-1)*tile_image->columns; |
| 1320 | if (p < pixels) |
| 1321 | break; |
| 1322 | } |
| 1323 | status=SetImageProgress(image,LoadImageTag,y,tile_image->rows); |
| 1324 | if (status == MagickFalse) |
| 1325 | break; |
| 1326 | } |
| 1327 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 1328 | if (jpeg == MagickFalse) |
| 1329 | if ((code == 0x9a) || (code == 0x9b) || |
| 1330 | ((bytes_per_line & 0x8000) != 0)) |
cristy | feb3e96 | 2012-03-29 17:25:55 +0000 | [diff] [blame] | 1331 | (void) CompositeImage(image,tile_image,CopyCompositeOp, |
cristy | 3917240 | 2012-03-30 13:04:39 +0000 | [diff] [blame] | 1332 | MagickTrue,destination.left,destination.top,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1333 | tile_image=DestroyImage(tile_image); |
| 1334 | break; |
| 1335 | } |
| 1336 | case 0xa1: |
| 1337 | { |
| 1338 | unsigned char |
| 1339 | *info; |
| 1340 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1341 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1342 | type; |
| 1343 | |
| 1344 | /* |
| 1345 | Comment. |
| 1346 | */ |
| 1347 | type=ReadBlobMSBShort(image); |
| 1348 | length=ReadBlobMSBShort(image); |
| 1349 | if (length == 0) |
| 1350 | break; |
| 1351 | (void) ReadBlobMSBLong(image); |
Cristy | 70293c7 | 2017-10-01 21:03:00 -0400 | [diff] [blame] | 1352 | length-=MagickMin(length,4); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1353 | if (length == 0) |
| 1354 | break; |
| 1355 | info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info)); |
| 1356 | if (info == (unsigned char *) NULL) |
| 1357 | break; |
| 1358 | count=ReadBlob(image,length,info); |
cristy | 6d62e4b | 2015-01-11 14:07:17 +0000 | [diff] [blame] | 1359 | if (count != (ssize_t) length) |
root | c94962c | 2017-09-15 15:19:43 +0000 | [diff] [blame] | 1360 | { |
| 1361 | info=(unsigned char *) RelinquishMagickMemory(info); |
Cristy | 70293c7 | 2017-10-01 21:03:00 -0400 | [diff] [blame] | 1362 | ThrowReaderException(ResourceLimitError, |
| 1363 | "UnableToReadImageData"); |
root | c94962c | 2017-09-15 15:19:43 +0000 | [diff] [blame] | 1364 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1365 | switch (type) |
| 1366 | { |
| 1367 | case 0xe0: |
| 1368 | { |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 1369 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1370 | SetStringInfoDatum(profile,info); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1371 | status=SetImageProfile(image,"icc",profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1372 | profile=DestroyStringInfo(profile); |
| 1373 | if (status == MagickFalse) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1374 | { |
| 1375 | info=(unsigned char *) RelinquishMagickMemory(info); |
| 1376 | ThrowReaderException(ResourceLimitError, |
| 1377 | "MemoryAllocationFailed"); |
| 1378 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } |
| 1381 | case 0x1f2: |
| 1382 | { |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 1383 | profile=BlobToStringInfo((const void *) NULL,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1384 | SetStringInfoDatum(profile,info); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1385 | status=SetImageProfile(image,"iptc",profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1386 | if (status == MagickFalse) |
Cristy | a537c72 | 2017-04-26 18:14:02 -0400 | [diff] [blame] | 1387 | { |
| 1388 | info=(unsigned char *) RelinquishMagickMemory(info); |
| 1389 | ThrowReaderException(ResourceLimitError, |
| 1390 | "MemoryAllocationFailed"); |
| 1391 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1392 | profile=DestroyStringInfo(profile); |
| 1393 | break; |
| 1394 | } |
| 1395 | default: |
| 1396 | break; |
| 1397 | } |
| 1398 | info=(unsigned char *) RelinquishMagickMemory(info); |
| 1399 | break; |
| 1400 | } |
| 1401 | default: |
| 1402 | { |
| 1403 | /* |
| 1404 | Skip to next op code. |
| 1405 | */ |
| 1406 | if (codes[code].length == -1) |
| 1407 | (void) ReadBlobMSBShort(image); |
| 1408 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1409 | for (i=0; i < (ssize_t) codes[code].length; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1410 | if (ReadBlobByte(image) == EOF) |
| 1411 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | if (code == 0xc00) |
| 1416 | { |
| 1417 | /* |
| 1418 | Skip header. |
| 1419 | */ |
| 1420 | for (i=0; i < 24; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1421 | if (ReadBlobByte(image) == EOF) |
| 1422 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1423 | continue; |
| 1424 | } |
| 1425 | if (((code >= 0xb0) && (code <= 0xcf)) || |
| 1426 | ((code >= 0x8000) && (code <= 0x80ff))) |
| 1427 | continue; |
| 1428 | if (code == 0x8200) |
| 1429 | { |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 1430 | char |
| 1431 | filename[MaxTextExtent]; |
| 1432 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1433 | FILE |
| 1434 | *file; |
| 1435 | |
| 1436 | Image |
| 1437 | *tile_image; |
| 1438 | |
| 1439 | ImageInfo |
| 1440 | *read_info; |
| 1441 | |
| 1442 | int |
| 1443 | unique_file; |
| 1444 | |
| 1445 | /* |
| 1446 | Embedded JPEG. |
| 1447 | */ |
| 1448 | jpeg=MagickTrue; |
| 1449 | read_info=CloneImageInfo(image_info); |
| 1450 | SetImageInfoBlob(read_info,(void *) NULL,0); |
| 1451 | file=(FILE *) NULL; |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 1452 | unique_file=AcquireUniqueFileResource(filename); |
| 1453 | (void) FormatLocaleString(read_info->filename,MaxTextExtent,"jpeg:%s", |
| 1454 | filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1455 | if (unique_file != -1) |
cristy | d0305b9 | 2009-10-19 13:12:21 +0000 | [diff] [blame] | 1456 | file=fdopen(unique_file,"wb"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1457 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 1458 | { |
dirk | ce6b122 | 2014-11-06 21:09:46 +0000 | [diff] [blame] | 1459 | (void) RelinquishUniqueFileResource(read_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1460 | (void) CopyMagickString(image->filename,read_info->filename, |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1461 | MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1462 | ThrowFileException(exception,FileOpenError, |
| 1463 | "UnableToCreateTemporaryFile",image->filename); |
| 1464 | image=DestroyImageList(image); |
| 1465 | return((Image *) NULL); |
| 1466 | } |
| 1467 | length=ReadBlobMSBLong(image); |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1468 | if (length > 154) |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1469 | { |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1470 | for (i=0; i < 6; i++) |
| 1471 | (void) ReadBlobMSBLong(image); |
| 1472 | if (ReadRectangle(image,&frame) == MagickFalse) |
| 1473 | { |
| 1474 | (void) fclose(file); |
| 1475 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 1476 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1477 | } |
| 1478 | for (i=0; i < 122; i++) |
| 1479 | if (ReadBlobByte(image) == EOF) |
| 1480 | break; |
| 1481 | for (i=0; i < (ssize_t) (length-154); i++) |
| 1482 | { |
| 1483 | c=ReadBlobByte(image); |
| 1484 | if (c == EOF) |
| 1485 | break; |
| 1486 | (void) fputc(c,file); |
| 1487 | } |
cristy | c944c57 | 2014-05-12 00:19:16 +0000 | [diff] [blame] | 1488 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1489 | (void) fclose(file); |
cristy | 100a056 | 2014-04-18 01:27:37 +0000 | [diff] [blame] | 1490 | (void) close(unique_file); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | tile_image=ReadImage(read_info,exception); |
Cristy | 6876875 | 2018-02-26 20:32:42 -0500 | [diff] [blame] | 1492 | (void) RelinquishUniqueFileResource(filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1493 | read_info=DestroyImageInfo(read_info); |
| 1494 | if (tile_image == (Image *) NULL) |
| 1495 | continue; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1496 | (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1497 | (double) MagickMax(image->columns,tile_image->columns), |
| 1498 | (double) MagickMax(image->rows,tile_image->rows)); |
cristy | a051173 | 2010-02-17 02:38:35 +0000 | [diff] [blame] | 1499 | (void) SetImageExtent(image, |
| 1500 | MagickMax(image->columns,tile_image->columns), |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 1501 | MagickMax(image->rows,tile_image->rows),exception); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 1502 | (void) TransformImageColorspace(image,tile_image->colorspace,exception); |
cristy | 3917240 | 2012-03-30 13:04:39 +0000 | [diff] [blame] | 1503 | (void) CompositeImage(image,tile_image,CopyCompositeOp,MagickTrue, |
cristy | feb3e96 | 2012-03-29 17:25:55 +0000 | [diff] [blame] | 1504 | frame.left,frame.right,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1505 | image->compression=tile_image->compression; |
| 1506 | tile_image=DestroyImage(tile_image); |
| 1507 | continue; |
| 1508 | } |
| 1509 | if ((code == 0xff) || (code == 0xffff)) |
| 1510 | break; |
| 1511 | if (((code >= 0xd0) && (code <= 0xfe)) || |
| 1512 | ((code >= 0x8100) && (code <= 0xffff))) |
| 1513 | { |
| 1514 | /* |
| 1515 | Skip reserved. |
| 1516 | */ |
| 1517 | length=ReadBlobMSBShort(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1518 | for (i=0; i < (ssize_t) length; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1519 | if (ReadBlobByte(image) == EOF) |
| 1520 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1521 | continue; |
| 1522 | } |
| 1523 | if ((code >= 0x100) && (code <= 0x7fff)) |
| 1524 | { |
| 1525 | /* |
| 1526 | Skip reserved. |
| 1527 | */ |
| 1528 | length=(size_t) ((code >> 7) & 0xff); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1529 | for (i=0; i < (ssize_t) length; i++) |
cristy | e11ddbd | 2015-03-20 14:34:55 +0000 | [diff] [blame] | 1530 | if (ReadBlobByte(image) == EOF) |
| 1531 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1532 | continue; |
| 1533 | } |
| 1534 | } |
| 1535 | (void) CloseBlob(image); |
| 1536 | return(GetFirstImageInList(image)); |
| 1537 | } |
| 1538 | #endif |
| 1539 | |
| 1540 | /* |
| 1541 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1542 | % % |
| 1543 | % % |
| 1544 | % % |
| 1545 | % R e g i s t e r P I C T I m a g e % |
| 1546 | % % |
| 1547 | % % |
| 1548 | % % |
| 1549 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1550 | % |
| 1551 | % RegisterPICTImage() adds attributes for the PICT image format to |
| 1552 | % the list of supported formats. The attributes include the image format |
| 1553 | % tag, a method to read and/or write the format, whether the format |
| 1554 | % supports the saving of more than one frame to the same file or blob, |
| 1555 | % whether the format supports native in-memory I/O, and a brief |
| 1556 | % description of the format. |
| 1557 | % |
| 1558 | % The format of the RegisterPICTImage method is: |
| 1559 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | % size_t RegisterPICTImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | % |
| 1562 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1563 | ModuleExport size_t RegisterPICTImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1564 | { |
| 1565 | MagickInfo |
| 1566 | *entry; |
| 1567 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1568 | entry=AcquireMagickInfo("PICT","PCT","Apple Macintosh QuickDraw/PICT"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1569 | entry->decoder=(DecodeImageHandler *) ReadPICTImage; |
| 1570 | entry->encoder=(EncodeImageHandler *) WritePICTImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1571 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 1572 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1573 | entry->magick=(IsImageFormatHandler *) IsPICT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1574 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 1575 | entry=AcquireMagickInfo("PICT","PICT","Apple Macintosh QuickDraw/PICT"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1576 | entry->decoder=(DecodeImageHandler *) ReadPICTImage; |
| 1577 | entry->encoder=(EncodeImageHandler *) WritePICTImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 1578 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 1579 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1580 | entry->magick=(IsImageFormatHandler *) IsPICT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1581 | (void) RegisterMagickInfo(entry); |
| 1582 | return(MagickImageCoderSignature); |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1587 | % % |
| 1588 | % % |
| 1589 | % % |
| 1590 | % U n r e g i s t e r P I C T I m a g e % |
| 1591 | % % |
| 1592 | % % |
| 1593 | % % |
| 1594 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1595 | % |
| 1596 | % UnregisterPICTImage() removes format registrations made by the |
| 1597 | % PICT module from the list of supported formats. |
| 1598 | % |
| 1599 | % The format of the UnregisterPICTImage method is: |
| 1600 | % |
| 1601 | % UnregisterPICTImage(void) |
| 1602 | % |
| 1603 | */ |
| 1604 | ModuleExport void UnregisterPICTImage(void) |
| 1605 | { |
| 1606 | (void) UnregisterMagickInfo("PCT"); |
| 1607 | (void) UnregisterMagickInfo("PICT"); |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1612 | % % |
| 1613 | % % |
| 1614 | % % |
| 1615 | % W r i t e P I C T I m a g e % |
| 1616 | % % |
| 1617 | % % |
| 1618 | % % |
| 1619 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1620 | % |
| 1621 | % WritePICTImage() writes an image to a file in the Apple Macintosh |
| 1622 | % QuickDraw/PICT image format. |
| 1623 | % |
| 1624 | % The format of the WritePICTImage method is: |
| 1625 | % |
cristy | 3669d04 | 2009-10-16 01:36:31 +0000 | [diff] [blame] | 1626 | % MagickBooleanType WritePICTImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1627 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1628 | % |
| 1629 | % A description of each parameter follows. |
| 1630 | % |
| 1631 | % o image_info: the image info. |
| 1632 | % |
| 1633 | % o image: The image. |
| 1634 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1635 | % o exception: return any errors or warnings in this structure. |
| 1636 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1637 | */ |
| 1638 | static MagickBooleanType WritePICTImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1639 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1640 | { |
| 1641 | #define MaxCount 128 |
| 1642 | #define PictCropRegionOp 0x01 |
| 1643 | #define PictEndOfPictureOp 0xff |
| 1644 | #define PictJPEGOp 0x8200 |
| 1645 | #define PictInfoOp 0x0C00 |
| 1646 | #define PictInfoSize 512 |
| 1647 | #define PictPixmapOp 0x9A |
| 1648 | #define PictPICTOp 0x98 |
| 1649 | #define PictVersion 0x11 |
| 1650 | |
| 1651 | const StringInfo |
| 1652 | *profile; |
| 1653 | |
| 1654 | double |
| 1655 | x_resolution, |
| 1656 | y_resolution; |
| 1657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1658 | MagickBooleanType |
| 1659 | status; |
| 1660 | |
| 1661 | MagickOffsetType |
| 1662 | offset; |
| 1663 | |
| 1664 | PICTPixmap |
| 1665 | pixmap; |
| 1666 | |
| 1667 | PICTRectangle |
| 1668 | bounds, |
| 1669 | crop_rectangle, |
| 1670 | destination_rectangle, |
| 1671 | frame_rectangle, |
| 1672 | size_rectangle, |
| 1673 | source_rectangle; |
| 1674 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1675 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1676 | *p; |
| 1677 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1678 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1679 | i, |
| 1680 | x; |
| 1681 | |
| 1682 | size_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 1683 | bytes_per_line, |
| 1684 | count, |
cristy | 0f6fc2d | 2015-05-30 00:49:11 +0000 | [diff] [blame] | 1685 | row_bytes, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 1686 | storage_class; |
| 1687 | |
| 1688 | ssize_t |
| 1689 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1690 | |
| 1691 | unsigned char |
| 1692 | *buffer, |
| 1693 | *packed_scanline, |
| 1694 | *scanline; |
| 1695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1696 | unsigned short |
| 1697 | base_address, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1698 | transfer_mode; |
| 1699 | |
| 1700 | /* |
| 1701 | Open output image file. |
| 1702 | */ |
| 1703 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1704 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1705 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1706 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1707 | if (image->debug != MagickFalse) |
| 1708 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1709 | if ((image->columns > 65535L) || (image->rows > 65535L)) |
| 1710 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1711 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1712 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1713 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1714 | if (status == MagickFalse) |
| 1715 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 1716 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1717 | /* |
| 1718 | Initialize image info. |
| 1719 | */ |
| 1720 | size_rectangle.top=0; |
| 1721 | size_rectangle.left=0; |
| 1722 | size_rectangle.bottom=(short) image->rows; |
| 1723 | size_rectangle.right=(short) image->columns; |
| 1724 | frame_rectangle=size_rectangle; |
| 1725 | crop_rectangle=size_rectangle; |
| 1726 | source_rectangle=size_rectangle; |
| 1727 | destination_rectangle=size_rectangle; |
| 1728 | base_address=0xff; |
cristy | 0f6fc2d | 2015-05-30 00:49:11 +0000 | [diff] [blame] | 1729 | row_bytes=image->columns; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1730 | bounds.top=0; |
| 1731 | bounds.left=0; |
| 1732 | bounds.bottom=(short) image->rows; |
| 1733 | bounds.right=(short) image->columns; |
| 1734 | pixmap.version=0; |
| 1735 | pixmap.pack_type=0; |
| 1736 | pixmap.pack_size=0; |
| 1737 | pixmap.pixel_type=0; |
| 1738 | pixmap.bits_per_pixel=8; |
| 1739 | pixmap.component_count=1; |
| 1740 | pixmap.component_size=8; |
| 1741 | pixmap.plane_bytes=0; |
| 1742 | pixmap.table=0; |
| 1743 | pixmap.reserved=0; |
| 1744 | transfer_mode=0; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1745 | x_resolution=image->resolution.x != 0.0 ? image->resolution.x : |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1746 | DefaultResolution; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1747 | y_resolution=image->resolution.y != 0.0 ? image->resolution.y : |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1748 | DefaultResolution; |
| 1749 | storage_class=image->storage_class; |
| 1750 | if (image_info->compression == JPEGCompression) |
| 1751 | storage_class=DirectClass; |
cristy | 213770a | 2014-09-27 00:12:23 +0000 | [diff] [blame] | 1752 | if (storage_class == DirectClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1753 | { |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1754 | pixmap.component_count=image->alpha_trait != UndefinedPixelTrait ? 4 : 3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1755 | pixmap.pixel_type=16; |
| 1756 | pixmap.bits_per_pixel=32; |
| 1757 | pixmap.pack_type=0x04; |
| 1758 | transfer_mode=0x40; |
cristy | 0f6fc2d | 2015-05-30 00:49:11 +0000 | [diff] [blame] | 1759 | row_bytes=4*image->columns; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1760 | } |
| 1761 | /* |
| 1762 | Allocate memory. |
| 1763 | */ |
| 1764 | bytes_per_line=image->columns; |
cristy | 213770a | 2014-09-27 00:12:23 +0000 | [diff] [blame] | 1765 | if (storage_class == DirectClass) |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1766 | bytes_per_line*=image->alpha_trait != UndefinedPixelTrait ? 4 : 3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1767 | buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer)); |
| 1768 | packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t) |
| 1769 | (row_bytes+MaxCount),sizeof(*packed_scanline)); |
| 1770 | scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline)); |
| 1771 | if ((buffer == (unsigned char *) NULL) || |
| 1772 | (packed_scanline == (unsigned char *) NULL) || |
| 1773 | (scanline == (unsigned char *) NULL)) |
Cristy | c1b09bb | 2017-07-17 19:20:45 -0400 | [diff] [blame] | 1774 | { |
| 1775 | if (scanline != (unsigned char *) NULL) |
| 1776 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 1777 | if (packed_scanline != (unsigned char *) NULL) |
| 1778 | packed_scanline=(unsigned char *) RelinquishMagickMemory( |
| 1779 | packed_scanline); |
| 1780 | if (buffer != (unsigned char *) NULL) |
| 1781 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 1782 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1783 | } |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 1784 | (void) memset(scanline,0,row_bytes); |
| 1785 | (void) memset(packed_scanline,0,(size_t) (row_bytes+MaxCount)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1786 | /* |
| 1787 | Write header, header size, size bounding box, version, and reserved. |
| 1788 | */ |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 1789 | (void) memset(buffer,0,PictInfoSize); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | (void) WriteBlob(image,PictInfoSize,buffer); |
| 1791 | (void) WriteBlobMSBShort(image,0); |
| 1792 | (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top); |
| 1793 | (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left); |
| 1794 | (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom); |
| 1795 | (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right); |
| 1796 | (void) WriteBlobMSBShort(image,PictVersion); |
| 1797 | (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */ |
| 1798 | (void) WriteBlobMSBShort(image,PictInfoOp); |
| 1799 | (void) WriteBlobMSBLong(image,0xFFFE0000UL); |
| 1800 | /* |
| 1801 | Write full size of the file, resolution, frame bounding box, and reserved. |
| 1802 | */ |
| 1803 | (void) WriteBlobMSBShort(image,(unsigned short) x_resolution); |
| 1804 | (void) WriteBlobMSBShort(image,0x0000); |
| 1805 | (void) WriteBlobMSBShort(image,(unsigned short) y_resolution); |
| 1806 | (void) WriteBlobMSBShort(image,0x0000); |
| 1807 | (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top); |
| 1808 | (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left); |
| 1809 | (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom); |
| 1810 | (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right); |
| 1811 | (void) WriteBlobMSBLong(image,0x00000000L); |
| 1812 | profile=GetImageProfile(image,"iptc"); |
| 1813 | if (profile != (StringInfo *) NULL) |
| 1814 | { |
| 1815 | (void) WriteBlobMSBShort(image,0xa1); |
| 1816 | (void) WriteBlobMSBShort(image,0x1f2); |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 1817 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 1818 | (GetStringInfoLength(profile)+4)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1819 | (void) WriteBlobString(image,"8BIM"); |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 1820 | (void) WriteBlob(image,GetStringInfoLength(profile), |
| 1821 | GetStringInfoDatum(profile)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1822 | } |
| 1823 | profile=GetImageProfile(image,"icc"); |
| 1824 | if (profile != (StringInfo *) NULL) |
| 1825 | { |
| 1826 | (void) WriteBlobMSBShort(image,0xa1); |
| 1827 | (void) WriteBlobMSBShort(image,0xe0); |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 1828 | (void) WriteBlobMSBShort(image,(unsigned short) |
| 1829 | (GetStringInfoLength(profile)+4)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1830 | (void) WriteBlobMSBLong(image,0x00000000UL); |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 1831 | (void) WriteBlob(image,GetStringInfoLength(profile), |
| 1832 | GetStringInfoDatum(profile)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1833 | (void) WriteBlobMSBShort(image,0xa1); |
| 1834 | (void) WriteBlobMSBShort(image,0xe0); |
| 1835 | (void) WriteBlobMSBShort(image,4); |
| 1836 | (void) WriteBlobMSBLong(image,0x00000002UL); |
| 1837 | } |
| 1838 | /* |
| 1839 | Write crop region opcode and crop bounding box. |
| 1840 | */ |
| 1841 | (void) WriteBlobMSBShort(image,PictCropRegionOp); |
| 1842 | (void) WriteBlobMSBShort(image,0xa); |
| 1843 | (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top); |
| 1844 | (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left); |
| 1845 | (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom); |
| 1846 | (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right); |
| 1847 | if (image_info->compression == JPEGCompression) |
| 1848 | { |
| 1849 | Image |
| 1850 | *jpeg_image; |
| 1851 | |
| 1852 | ImageInfo |
| 1853 | *jpeg_info; |
| 1854 | |
| 1855 | size_t |
| 1856 | length; |
| 1857 | |
| 1858 | unsigned char |
| 1859 | *blob; |
| 1860 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1861 | jpeg_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1862 | if (jpeg_image == (Image *) NULL) |
| 1863 | { |
| 1864 | (void) CloseBlob(image); |
| 1865 | return(MagickFalse); |
| 1866 | } |
| 1867 | jpeg_info=CloneImageInfo(image_info); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 1868 | (void) CopyMagickString(jpeg_info->magick,"JPEG",MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1869 | length=0; |
| 1870 | blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1871 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1872 | jpeg_info=DestroyImageInfo(jpeg_info); |
| 1873 | if (blob == (unsigned char *) NULL) |
| 1874 | return(MagickFalse); |
| 1875 | jpeg_image=DestroyImage(jpeg_image); |
| 1876 | (void) WriteBlobMSBShort(image,PictJPEGOp); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1877 | (void) WriteBlobMSBLong(image,(unsigned int) length+154); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1878 | (void) WriteBlobMSBShort(image,0x0000); |
| 1879 | (void) WriteBlobMSBLong(image,0x00010000UL); |
| 1880 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1881 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1882 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1883 | (void) WriteBlobMSBLong(image,0x00010000UL); |
| 1884 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1885 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1886 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1887 | (void) WriteBlobMSBLong(image,0x40000000UL); |
| 1888 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1889 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1890 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1891 | (void) WriteBlobMSBLong(image,0x00400000UL); |
| 1892 | (void) WriteBlobMSBShort(image,0x0000); |
| 1893 | (void) WriteBlobMSBShort(image,(unsigned short) image->rows); |
| 1894 | (void) WriteBlobMSBShort(image,(unsigned short) image->columns); |
| 1895 | (void) WriteBlobMSBShort(image,0x0000); |
| 1896 | (void) WriteBlobMSBShort(image,768); |
| 1897 | (void) WriteBlobMSBShort(image,0x0000); |
| 1898 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1899 | (void) WriteBlobMSBLong(image,0x00566A70UL); |
| 1900 | (void) WriteBlobMSBLong(image,0x65670000UL); |
| 1901 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1902 | (void) WriteBlobMSBLong(image,0x00000001UL); |
| 1903 | (void) WriteBlobMSBLong(image,0x00016170UL); |
| 1904 | (void) WriteBlobMSBLong(image,0x706C0000UL); |
| 1905 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1906 | (void) WriteBlobMSBShort(image,768); |
| 1907 | (void) WriteBlobMSBShort(image,(unsigned short) image->columns); |
| 1908 | (void) WriteBlobMSBShort(image,(unsigned short) image->rows); |
| 1909 | (void) WriteBlobMSBShort(image,(unsigned short) x_resolution); |
| 1910 | (void) WriteBlobMSBShort(image,0x0000); |
| 1911 | (void) WriteBlobMSBShort(image,(unsigned short) y_resolution); |
| 1912 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1913 | (void) WriteBlobMSBLong(image,0x87AC0001UL); |
| 1914 | (void) WriteBlobMSBLong(image,0x0B466F74UL); |
| 1915 | (void) WriteBlobMSBLong(image,0x6F202D20UL); |
| 1916 | (void) WriteBlobMSBLong(image,0x4A504547UL); |
| 1917 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1918 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1919 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1920 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1921 | (void) WriteBlobMSBLong(image,0x00000000UL); |
| 1922 | (void) WriteBlobMSBLong(image,0x0018FFFFUL); |
| 1923 | (void) WriteBlob(image,length,blob); |
| 1924 | if ((length & 0x01) != 0) |
| 1925 | (void) WriteBlobByte(image,'\0'); |
| 1926 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 1927 | } |
| 1928 | /* |
| 1929 | Write picture opcode, row bytes, and picture bounding box, and version. |
| 1930 | */ |
| 1931 | if (storage_class == PseudoClass) |
| 1932 | (void) WriteBlobMSBShort(image,PictPICTOp); |
| 1933 | else |
| 1934 | { |
| 1935 | (void) WriteBlobMSBShort(image,PictPixmapOp); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1936 | (void) WriteBlobMSBLong(image,(size_t) base_address); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1937 | } |
| 1938 | (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000)); |
| 1939 | (void) WriteBlobMSBShort(image,(unsigned short) bounds.top); |
| 1940 | (void) WriteBlobMSBShort(image,(unsigned short) bounds.left); |
| 1941 | (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom); |
| 1942 | (void) WriteBlobMSBShort(image,(unsigned short) bounds.right); |
| 1943 | /* |
| 1944 | Write pack type, pack size, resolution, pixel type, and pixel size. |
| 1945 | */ |
| 1946 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version); |
| 1947 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1948 | (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1949 | (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5)); |
| 1950 | (void) WriteBlobMSBShort(image,0x0000); |
| 1951 | (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5)); |
| 1952 | (void) WriteBlobMSBShort(image,0x0000); |
| 1953 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type); |
| 1954 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel); |
| 1955 | /* |
| 1956 | Write component count, size, plane bytes, table size, and reserved. |
| 1957 | */ |
| 1958 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count); |
| 1959 | (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1960 | (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes); |
| 1961 | (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table); |
| 1962 | (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1963 | if (storage_class == PseudoClass) |
| 1964 | { |
| 1965 | /* |
| 1966 | Write image colormap. |
| 1967 | */ |
| 1968 | (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */ |
| 1969 | (void) WriteBlobMSBShort(image,0L); /* color flags */ |
| 1970 | (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1971 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1972 | { |
| 1973 | (void) WriteBlobMSBShort(image,(unsigned short) i); |
| 1974 | (void) WriteBlobMSBShort(image,ScaleQuantumToShort( |
| 1975 | image->colormap[i].red)); |
| 1976 | (void) WriteBlobMSBShort(image,ScaleQuantumToShort( |
| 1977 | image->colormap[i].green)); |
| 1978 | (void) WriteBlobMSBShort(image,ScaleQuantumToShort( |
| 1979 | image->colormap[i].blue)); |
| 1980 | } |
| 1981 | } |
| 1982 | /* |
| 1983 | Write source and destination rectangle. |
| 1984 | */ |
| 1985 | (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top); |
| 1986 | (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left); |
| 1987 | (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom); |
| 1988 | (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right); |
| 1989 | (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top); |
| 1990 | (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left); |
| 1991 | (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom); |
| 1992 | (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right); |
| 1993 | (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode); |
| 1994 | /* |
| 1995 | Write picture data. |
| 1996 | */ |
| 1997 | count=0; |
cristy | 213770a | 2014-09-27 00:12:23 +0000 | [diff] [blame] | 1998 | if (storage_class == PseudoClass) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1999 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2000 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2001 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2002 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2003 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2004 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2005 | { |
| 2006 | scanline[x]=(unsigned char) GetPixelIndex(image,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2007 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2008 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2009 | count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2010 | packed_scanline); |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 2011 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2012 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2013 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 2014 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2015 | if (status == MagickFalse) |
| 2016 | break; |
| 2017 | } |
| 2018 | } |
| 2019 | else |
| 2020 | if (image_info->compression == JPEGCompression) |
| 2021 | { |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 2022 | (void) memset(scanline,0,row_bytes); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2023 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 2024 | count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF), |
| 2025 | packed_scanline); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2026 | } |
| 2027 | else |
| 2028 | { |
| 2029 | register unsigned char |
| 2030 | *blue, |
| 2031 | *green, |
| 2032 | *opacity, |
| 2033 | *red; |
| 2034 | |
| 2035 | red=scanline; |
| 2036 | green=scanline+image->columns; |
| 2037 | blue=scanline+2*image->columns; |
| 2038 | opacity=scanline+3*image->columns; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2039 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2040 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2041 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2042 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2043 | break; |
| 2044 | red=scanline; |
| 2045 | green=scanline+image->columns; |
| 2046 | blue=scanline+2*image->columns; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 2047 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2048 | { |
| 2049 | opacity=scanline; |
| 2050 | red=scanline+image->columns; |
| 2051 | green=scanline+2*image->columns; |
| 2052 | blue=scanline+3*image->columns; |
| 2053 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2054 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2055 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2056 | *red++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 2057 | *green++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 2058 | *blue++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 2059 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2060 | *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p))); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2061 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2062 | } |
Cristy | 2174484 | 2016-01-13 18:31:21 -0500 | [diff] [blame] | 2063 | count+=EncodeImage(image,scanline,bytes_per_line,packed_scanline); |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 2064 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2065 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2066 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2067 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2068 | if (status == MagickFalse) |
| 2069 | break; |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | if ((count & 0x01) != 0) |
| 2074 | (void) WriteBlobByte(image,'\0'); |
| 2075 | (void) WriteBlobMSBShort(image,PictEndOfPictureOp); |
| 2076 | offset=TellBlob(image); |
| 2077 | offset=SeekBlob(image,512,SEEK_SET); |
| 2078 | (void) WriteBlobMSBShort(image,(unsigned short) offset); |
| 2079 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 2080 | packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline); |
| 2081 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 2082 | (void) CloseBlob(image); |
| 2083 | return(MagickTrue); |
| 2084 | } |