cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP DDDD BBBB % |
| 7 | % P P D D B B % |
| 8 | % PPPP D D BBBB % |
| 9 | % P D D B B % |
| 10 | % P DDDD BBBB % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Palm Database ImageViewer Image Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | f6ff9ea | 2016-12-05 09:53:35 -0500 | [diff] [blame] | 20 | % Copyright 1999-2017 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 | % |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 37 | % 20071202 TS * rewrote RLE decoder - old version could cause buffer overflows |
| 38 | % * failure of RLE decoding now thows error RLEDecoderError |
| 39 | % * fixed bug in RLE decoding - now all rows are decoded, not just |
| 40 | % the first one |
| 41 | % * fixed bug in reader - record offsets now handled correctly |
| 42 | % * fixed bug in reader - only bits 0..2 indicate compression type |
| 43 | % * in writer: now using image color count instead of depth |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | /* |
| 47 | Include declarations. |
| 48 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 49 | #include "MagickCore/studio.h" |
| 50 | #include "MagickCore/attribute.h" |
| 51 | #include "MagickCore/blob.h" |
| 52 | #include "MagickCore/blob-private.h" |
| 53 | #include "MagickCore/cache.h" |
| 54 | #include "MagickCore/colormap-private.h" |
| 55 | #include "MagickCore/color-private.h" |
| 56 | #include "MagickCore/colormap.h" |
| 57 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 58 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 59 | #include "MagickCore/constitute.h" |
| 60 | #include "MagickCore/exception.h" |
| 61 | #include "MagickCore/exception-private.h" |
| 62 | #include "MagickCore/image.h" |
| 63 | #include "MagickCore/image-private.h" |
| 64 | #include "MagickCore/list.h" |
| 65 | #include "MagickCore/magick.h" |
| 66 | #include "MagickCore/memory_.h" |
| 67 | #include "MagickCore/monitor.h" |
| 68 | #include "MagickCore/monitor-private.h" |
| 69 | #include "MagickCore/pixel-accessor.h" |
| 70 | #include "MagickCore/property.h" |
| 71 | #include "MagickCore/quantum-private.h" |
| 72 | #include "MagickCore/quantum-private.h" |
| 73 | #include "MagickCore/static.h" |
| 74 | #include "MagickCore/string_.h" |
| 75 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | Typedef declarations. |
| 79 | */ |
| 80 | typedef struct _PDBInfo |
| 81 | { |
| 82 | char |
| 83 | name[32]; |
| 84 | |
| 85 | short int |
| 86 | attributes, |
| 87 | version; |
| 88 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 89 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 90 | create_time, |
| 91 | modify_time, |
| 92 | archive_time, |
| 93 | modify_number, |
| 94 | application_info, |
| 95 | sort_info; |
| 96 | |
| 97 | char |
| 98 | type[4], /* database type identifier "vIMG" */ |
| 99 | id[4]; /* database creator identifier "View" */ |
| 100 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 101 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 102 | seed, |
| 103 | next_record; |
| 104 | |
| 105 | short int |
| 106 | number_records; |
| 107 | } PDBInfo; |
| 108 | |
| 109 | typedef struct _PDBImage |
| 110 | { |
| 111 | char |
| 112 | name[32], |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 113 | version; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 114 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 115 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 116 | reserved_1, |
| 117 | note; |
| 118 | |
| 119 | short int |
| 120 | x_last, |
| 121 | y_last; |
| 122 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 123 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 124 | reserved_2; |
| 125 | |
| 126 | short int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 127 | width, |
| 128 | height; |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 129 | |
| 130 | unsigned char |
| 131 | type; |
| 132 | |
| 133 | unsigned short |
| 134 | x_anchor, |
| 135 | y_anchor; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | } PDBImage; |
| 137 | /* |
| 138 | Forward declarations. |
| 139 | */ |
| 140 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 141 | WritePDBImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 145 | % % |
| 146 | % % |
| 147 | % % |
| 148 | % D e c o d e I m a g e % |
| 149 | % % |
| 150 | % % |
| 151 | % % |
| 152 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 153 | % |
| 154 | % DecodeImage unpacks the packed image pixels into runlength-encoded |
| 155 | % pixel packets. |
| 156 | % |
| 157 | % The format of the DecodeImage method is: |
| 158 | % |
| 159 | % MagickBooleanType DecodeImage(Image *image,unsigned char *pixels, |
| 160 | % const size_t length) |
| 161 | % |
| 162 | % A description of each parameter follows: |
| 163 | % |
| 164 | % o image: the address of a structure of type Image. |
| 165 | % |
| 166 | % o pixels: The address of a byte (8 bits) array of pixel data created by |
| 167 | % the decoding process. |
| 168 | % |
| 169 | % o length: Number of bytes to read into buffer 'pixels'. |
| 170 | % |
| 171 | */ |
| 172 | static MagickBooleanType DecodeImage(Image *image, unsigned char *pixels, |
| 173 | const size_t length) |
| 174 | { |
| 175 | #define RLE_MODE_NONE -1 |
| 176 | #define RLE_MODE_COPY 0 |
| 177 | #define RLE_MODE_RUN 1 |
| 178 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 179 | int data = 0, count = 0; |
| 180 | unsigned char *p; |
| 181 | int mode = RLE_MODE_NONE; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 183 | for (p = pixels; p < pixels + length; p++) { |
| 184 | if (0 == count) { |
| 185 | data = ReadBlobByte( image ); |
| 186 | if (-1 == data) return MagickFalse; |
| 187 | if (data > 128) { |
| 188 | mode = RLE_MODE_RUN; |
| 189 | count = data - 128 + 1; |
| 190 | data = ReadBlobByte( image ); |
| 191 | if (-1 == data) return MagickFalse; |
| 192 | } else { |
| 193 | mode = RLE_MODE_COPY; |
| 194 | count = data + 1; |
| 195 | } |
| 196 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | |
cristy | c5de699 | 2009-10-06 19:19:48 +0000 | [diff] [blame] | 198 | if (RLE_MODE_COPY == mode) { |
| 199 | data = ReadBlobByte( image ); |
| 200 | if (-1 == data) return MagickFalse; |
| 201 | } |
| 202 | *p = (unsigned char)data; |
| 203 | --count; |
| 204 | } |
| 205 | return MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
| 209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 210 | % % |
| 211 | % % |
| 212 | % % |
| 213 | % I s P D B % |
| 214 | % % |
| 215 | % % |
| 216 | % % |
| 217 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 218 | % |
| 219 | % IsPDB() returns MagickTrue if the image format type, identified by the |
| 220 | % magick string, is PDB. |
| 221 | % |
| 222 | % The format of the ReadPDBImage method is: |
| 223 | % |
| 224 | % MagickBooleanType IsPDB(const unsigned char *magick,const size_t length) |
| 225 | % |
| 226 | % A description of each parameter follows: |
| 227 | % |
| 228 | % o magick: compare image format pattern against these bytes. |
| 229 | % |
| 230 | % o length: Specifies the length of the magick string. |
| 231 | % |
| 232 | */ |
| 233 | static MagickBooleanType IsPDB(const unsigned char *magick,const size_t length) |
| 234 | { |
| 235 | if (length < 68) |
| 236 | return(MagickFalse); |
| 237 | if (memcmp(magick+60,"vIMGView",8) == 0) |
| 238 | return(MagickTrue); |
| 239 | return(MagickFalse); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 244 | % % |
| 245 | % % |
| 246 | % % |
| 247 | % R e a d P D B I m a g e % |
| 248 | % % |
| 249 | % % |
| 250 | % % |
| 251 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 252 | % |
| 253 | % ReadPDBImage() reads an Pilot image file and returns it. It |
| 254 | % allocates the memory necessary for the new Image structure and returns a |
| 255 | % pointer to the new image. |
| 256 | % |
| 257 | % The format of the ReadPDBImage method is: |
| 258 | % |
| 259 | % Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 260 | % |
| 261 | % A description of each parameter follows: |
| 262 | % |
| 263 | % o image_info: the image info. |
| 264 | % |
| 265 | % o exception: return any errors or warnings in this structure. |
| 266 | % |
| 267 | */ |
| 268 | static Image *ReadPDBImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 269 | { |
| 270 | unsigned char |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 271 | attributes, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | tag[3]; |
| 273 | |
| 274 | Image |
| 275 | *image; |
| 276 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | MagickBooleanType |
| 278 | status; |
| 279 | |
| 280 | PDBImage |
| 281 | pdb_image; |
| 282 | |
| 283 | PDBInfo |
| 284 | pdb_info; |
| 285 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 286 | Quantum |
| 287 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 288 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 289 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 290 | x; |
| 291 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 292 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 293 | *q; |
| 294 | |
| 295 | register unsigned char |
| 296 | *p; |
| 297 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 298 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | bits_per_pixel, |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 300 | num_pad_bytes, |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 301 | one, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | packets; |
| 303 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 304 | ssize_t |
| 305 | count, |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 306 | img_offset, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 307 | comment_offset = 0, |
| 308 | y; |
| 309 | |
| 310 | unsigned char |
| 311 | *pixels; |
| 312 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 313 | /* |
| 314 | Open image file. |
| 315 | */ |
| 316 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 317 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | if (image_info->debug != MagickFalse) |
| 319 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 320 | image_info->filename); |
| 321 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 322 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 323 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 324 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 325 | if (status == MagickFalse) |
| 326 | { |
| 327 | image=DestroyImageList(image); |
| 328 | return((Image *) NULL); |
| 329 | } |
| 330 | /* |
| 331 | Determine if this a PDB image file. |
| 332 | */ |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 333 | count=ReadBlob(image,sizeof(pdb_info.name),(unsigned char *) pdb_info.name); |
| 334 | if (count != sizeof(pdb_info.name)) |
| 335 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | pdb_info.attributes=(short) ReadBlobMSBShort(image); |
| 337 | pdb_info.version=(short) ReadBlobMSBShort(image); |
| 338 | pdb_info.create_time=ReadBlobMSBLong(image); |
| 339 | pdb_info.modify_time=ReadBlobMSBLong(image); |
| 340 | pdb_info.archive_time=ReadBlobMSBLong(image); |
| 341 | pdb_info.modify_number=ReadBlobMSBLong(image); |
| 342 | pdb_info.application_info=ReadBlobMSBLong(image); |
| 343 | pdb_info.sort_info=ReadBlobMSBLong(image); |
cristy | 7e5d1da | 2014-12-28 00:40:02 +0000 | [diff] [blame] | 344 | (void) ReadBlob(image,4,(unsigned char *) pdb_info.type); |
| 345 | (void) ReadBlob(image,4,(unsigned char *) pdb_info.id); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | pdb_info.seed=ReadBlobMSBLong(image); |
| 347 | pdb_info.next_record=ReadBlobMSBLong(image); |
| 348 | pdb_info.number_records=(short) ReadBlobMSBShort(image); |
cristy | 7e5d1da | 2014-12-28 00:40:02 +0000 | [diff] [blame] | 349 | if ((memcmp(pdb_info.type,"vIMG",4) != 0) || |
| 350 | (memcmp(pdb_info.id,"View",4) != 0)) |
| 351 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | if (pdb_info.next_record != 0) |
| 353 | ThrowReaderException(CoderError,"MultipleRecordListNotSupported"); |
| 354 | /* |
| 355 | Read record header. |
| 356 | */ |
Cristy | 1fe0b87 | 2016-04-23 10:48:31 -0400 | [diff] [blame] | 357 | img_offset=(ssize_t) ReadBlobMSBSignedLong(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 358 | attributes=(unsigned char) ReadBlobByte(image); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 359 | (void) attributes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | count=ReadBlob(image,3,(unsigned char *) tag); |
| 361 | if (count != 3 || memcmp(tag,"\x6f\x80\x00",3) != 0) |
| 362 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 363 | if (pdb_info.number_records > 1) |
| 364 | { |
Cristy | 1fe0b87 | 2016-04-23 10:48:31 -0400 | [diff] [blame] | 365 | comment_offset=(ssize_t) ReadBlobMSBSignedLong(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | attributes=(unsigned char) ReadBlobByte(image); |
| 367 | count=ReadBlob(image,3,(unsigned char *) tag); |
| 368 | if (count != 3 || memcmp(tag,"\x6f\x80\x01",3) != 0) |
| 369 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 370 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 371 | num_pad_bytes = (size_t) (img_offset - TellBlob( image )); |
cristy | 89f839d | 2015-01-25 17:32:32 +0000 | [diff] [blame] | 372 | while (num_pad_bytes-- != 0) |
| 373 | { |
| 374 | int |
| 375 | c; |
| 376 | |
| 377 | c=ReadBlobByte(image); |
| 378 | if (c == EOF) |
| 379 | break; |
| 380 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | /* |
| 382 | Read image header. |
| 383 | */ |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 384 | count=ReadBlob(image,sizeof(pdb_image.name),(unsigned char *) pdb_image.name); |
| 385 | if (count != sizeof(pdb_image.name)) |
| 386 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 387 | pdb_image.version=ReadBlobByte(image); |
Cristy | 1fe0b87 | 2016-04-23 10:48:31 -0400 | [diff] [blame] | 388 | pdb_image.type=(unsigned char) (ReadBlobByte(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 389 | pdb_image.reserved_1=ReadBlobMSBLong(image); |
| 390 | pdb_image.note=ReadBlobMSBLong(image); |
| 391 | pdb_image.x_last=(short) ReadBlobMSBShort(image); |
| 392 | pdb_image.y_last=(short) ReadBlobMSBShort(image); |
| 393 | pdb_image.reserved_2=ReadBlobMSBLong(image); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 394 | pdb_image.x_anchor=ReadBlobMSBShort(image); |
| 395 | pdb_image.y_anchor=ReadBlobMSBShort(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | pdb_image.width=(short) ReadBlobMSBShort(image); |
| 397 | pdb_image.height=(short) ReadBlobMSBShort(image); |
| 398 | /* |
| 399 | Initialize image structure. |
| 400 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 401 | image->columns=(size_t) pdb_image.width; |
| 402 | image->rows=(size_t) pdb_image.height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 403 | image->depth=8; |
| 404 | image->storage_class=PseudoClass; |
| 405 | bits_per_pixel=pdb_image.type == 0 ? 2UL : pdb_image.type == 2 ? 4UL : 1UL; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 406 | one=1; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 407 | if (AcquireImageColormap(image,one << bits_per_pixel,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 408 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 409 | if (image_info->ping != MagickFalse) |
| 410 | { |
| 411 | (void) CloseBlob(image); |
| 412 | return(GetFirstImageInList(image)); |
| 413 | } |
Cristy | ff9bf44 | 2016-07-06 08:51:47 -0400 | [diff] [blame] | 414 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 415 | if (status == MagickFalse) |
| 416 | return(DestroyImageList(image)); |
cristy | 55a26ef | 2014-11-29 17:16:29 +0000 | [diff] [blame] | 417 | packets=(bits_per_pixel*image->columns+7)/8; |
Cristy | 6d202a0 | 2016-03-07 07:13:10 -0500 | [diff] [blame] | 418 | pixels=(unsigned char *) AcquireQuantumMemory(packets+257UL,image->rows* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 419 | sizeof(*pixels)); |
| 420 | if (pixels == (unsigned char *) NULL) |
| 421 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 422 | switch (pdb_image.version & 0x07) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 423 | { |
| 424 | case 0: |
| 425 | { |
| 426 | image->compression=NoCompression; |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 427 | count=(ssize_t) ReadBlob(image,packets*image->rows,pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | case 1: |
| 431 | { |
| 432 | image->compression=RLECompression; |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 433 | if (!DecodeImage(image,pixels,packets*image->rows)) |
| 434 | { |
| 435 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 436 | ThrowReaderException(CorruptImageError,"RLEDecoderError"); |
| 437 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | default: |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 441 | { |
| 442 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | ThrowReaderException(CorruptImageError, |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 444 | "UnrecognizedImageCompressionType"); |
| 445 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | } |
| 447 | p=pixels; |
| 448 | switch (bits_per_pixel) |
| 449 | { |
| 450 | case 1: |
| 451 | { |
| 452 | int |
| 453 | bit; |
| 454 | |
| 455 | /* |
| 456 | Read 1-bit PDB image. |
| 457 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 458 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 459 | { |
| 460 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 461 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 462 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 463 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | { |
| 465 | for (bit=0; bit < 8; bit++) |
| 466 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 467 | index=(Quantum) (*p & (0x80 >> bit) ? 0x00 : 0x01); |
| 468 | SetPixelIndex(image,index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 469 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 470 | } |
| 471 | p++; |
| 472 | } |
| 473 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 474 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 475 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 476 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 477 | if (status == MagickFalse) |
| 478 | break; |
| 479 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 480 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | case 2: |
| 484 | { |
| 485 | /* |
| 486 | Read 2-bit PDB image. |
| 487 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 488 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | { |
| 490 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 491 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | break; |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 493 | for (x=0; x < (ssize_t) image->columns-3; x+=4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 494 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 495 | index=ConstrainColormapIndex(image,3UL-((*p >> 6) & 0x03),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 496 | SetPixelIndex(image,index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 497 | q+=GetPixelChannels(image); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 498 | index=ConstrainColormapIndex(image,3UL-((*p >> 4) & 0x03),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 499 | SetPixelIndex(image,index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 500 | q+=GetPixelChannels(image); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 501 | index=ConstrainColormapIndex(image,3UL-((*p >> 2) & 0x03),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 502 | SetPixelIndex(image,index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 503 | q+=GetPixelChannels(image); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 504 | index=ConstrainColormapIndex(image,3UL-((*p) & 0x03),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 505 | SetPixelIndex(image,index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 507 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | } |
| 509 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 510 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 511 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 512 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | if (status == MagickFalse) |
| 514 | break; |
| 515 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 516 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | case 4: |
| 520 | { |
| 521 | /* |
| 522 | Read 4-bit PDB image. |
| 523 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 524 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 525 | { |
| 526 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 527 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 528 | break; |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 529 | for (x=0; x < (ssize_t) image->columns-1; x+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 531 | index=ConstrainColormapIndex(image,15UL-((*p >> 4) & 0x0f),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 532 | SetPixelIndex(image,index,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 533 | q+=GetPixelChannels(image); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 534 | index=ConstrainColormapIndex(image,15UL-((*p) & 0x0f),exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 535 | SetPixelIndex(image,index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 537 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 538 | } |
| 539 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 540 | break; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 541 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 542 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | if (status == MagickFalse) |
| 544 | break; |
| 545 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 546 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | default: |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 550 | { |
| 551 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 552 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
Dirk Lemstra | 3690a82 | 2017-05-02 08:26:55 +0200 | [diff] [blame] | 553 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 555 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 556 | if (EOFBlob(image) != MagickFalse) |
| 557 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 558 | image->filename); |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 559 | if (pdb_info.number_records > 1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 560 | { |
| 561 | char |
| 562 | *comment; |
| 563 | |
| 564 | int |
| 565 | c; |
| 566 | |
| 567 | register char |
| 568 | *p; |
| 569 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 570 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 571 | length; |
| 572 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 573 | num_pad_bytes = (size_t) (comment_offset - TellBlob( image )); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 574 | while (num_pad_bytes--) ReadBlobByte( image ); |
| 575 | |
| 576 | /* |
| 577 | Read comment. |
| 578 | */ |
| 579 | c=ReadBlobByte(image); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 580 | length=MagickPathExtent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 581 | comment=AcquireString((char *) NULL); |
| 582 | for (p=comment; c != EOF; p++) |
| 583 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 584 | if ((size_t) (p-comment+MagickPathExtent) >= length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 585 | { |
| 586 | *p='\0'; |
| 587 | length<<=1; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 588 | length+=MagickPathExtent; |
| 589 | comment=(char *) ResizeQuantumMemory(comment,length+MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | sizeof(*comment)); |
| 591 | if (comment == (char *) NULL) |
| 592 | break; |
| 593 | p=comment+strlen(comment); |
| 594 | } |
| 595 | *p=c; |
| 596 | c=ReadBlobByte(image); |
| 597 | } |
| 598 | *p='\0'; |
| 599 | if (comment == (char *) NULL) |
| 600 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 601 | (void) SetImageProperty(image,"comment",comment,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 602 | comment=DestroyString(comment); |
| 603 | } |
| 604 | (void) CloseBlob(image); |
| 605 | return(GetFirstImageInList(image)); |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 610 | % % |
| 611 | % % |
| 612 | % % |
| 613 | % R e g i s t e r P D B I m a g e % |
| 614 | % % |
| 615 | % % |
| 616 | % % |
| 617 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 618 | % |
| 619 | % RegisterPDBImage() adds properties for the PDB image format to |
| 620 | % the list of supported formats. The properties include the image format |
| 621 | % tag, a method to read and/or write the format, whether the format |
| 622 | % supports the saving of more than one frame to the same file or blob, |
| 623 | % whether the format supports native in-memory I/O, and a brief |
| 624 | % description of the format. |
| 625 | % |
| 626 | % The format of the RegisterPDBImage method is: |
| 627 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 628 | % size_t RegisterPDBImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | % |
| 630 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 631 | ModuleExport size_t RegisterPDBImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 632 | { |
| 633 | MagickInfo |
| 634 | *entry; |
| 635 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 636 | entry=AcquireMagickInfo("PDB","PDB","Palm Database ImageViewer Format"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 637 | entry->decoder=(DecodeImageHandler *) ReadPDBImage; |
| 638 | entry->encoder=(EncodeImageHandler *) WritePDBImage; |
| 639 | entry->magick=(IsImageFormatHandler *) IsPDB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 640 | (void) RegisterMagickInfo(entry); |
| 641 | return(MagickImageCoderSignature); |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 646 | % % |
| 647 | % % |
| 648 | % % |
| 649 | % U n r e g i s t e r P D B I m a g e % |
| 650 | % % |
| 651 | % % |
| 652 | % % |
| 653 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 654 | % |
| 655 | % UnregisterPDBImage() removes format registrations made by the |
| 656 | % PDB module from the list of supported formats. |
| 657 | % |
| 658 | % The format of the UnregisterPDBImage method is: |
| 659 | % |
| 660 | % UnregisterPDBImage(void) |
| 661 | % |
| 662 | */ |
| 663 | ModuleExport void UnregisterPDBImage(void) |
| 664 | { |
| 665 | (void) UnregisterMagickInfo("PDB"); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 670 | % % |
| 671 | % % |
| 672 | % % |
| 673 | % W r i t e P D B I m a g e % |
| 674 | % % |
| 675 | % % |
| 676 | % % |
| 677 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 678 | % |
| 679 | % WritePDBImage() writes an image |
| 680 | % |
| 681 | % The format of the WritePDBImage method is: |
| 682 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 683 | % MagickBooleanType WritePDBImage(const ImageInfo *image_info, |
| 684 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 685 | % |
| 686 | % A description of each parameter follows. |
| 687 | % |
| 688 | % o image_info: the image info. |
| 689 | % |
| 690 | % o image: The image. |
| 691 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 692 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | % |
| 694 | */ |
| 695 | |
| 696 | static unsigned char *EncodeRLE(unsigned char *destination, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 697 | unsigned char *source,size_t literal,size_t repeat) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 698 | { |
| 699 | if (literal > 0) |
| 700 | *destination++=(unsigned char) (literal-1); |
| 701 | (void) CopyMagickMemory(destination,source,literal); |
| 702 | destination+=literal; |
| 703 | if (repeat > 0) |
| 704 | { |
| 705 | *destination++=(unsigned char) (0x80 | (repeat-1)); |
| 706 | *destination++=source[literal]; |
| 707 | } |
| 708 | return(destination); |
| 709 | } |
| 710 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 711 | static MagickBooleanType WritePDBImage(const ImageInfo *image_info,Image *image, |
| 712 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | { |
| 714 | const char |
| 715 | *comment; |
| 716 | |
| 717 | int |
| 718 | bits; |
| 719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 720 | MagickBooleanType |
| 721 | status; |
| 722 | |
| 723 | PDBImage |
| 724 | pdb_image; |
| 725 | |
| 726 | PDBInfo |
| 727 | pdb_info; |
| 728 | |
| 729 | QuantumInfo |
| 730 | *quantum_info; |
| 731 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 732 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 733 | *p; |
| 734 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 735 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | x; |
| 737 | |
| 738 | register unsigned char |
| 739 | *q; |
| 740 | |
| 741 | size_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 742 | bits_per_pixel, |
| 743 | literal, |
| 744 | packets, |
| 745 | packet_size, |
| 746 | repeat; |
| 747 | |
| 748 | ssize_t |
| 749 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 750 | |
| 751 | unsigned char |
| 752 | *buffer, |
| 753 | *runlength, |
| 754 | *scanline; |
| 755 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 756 | /* |
| 757 | Open output image file. |
| 758 | */ |
| 759 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 760 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 761 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 762 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | if (image->debug != MagickFalse) |
| 764 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 765 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 766 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 767 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 768 | if (status == MagickFalse) |
| 769 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 770 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
dirk | ab4f0bb | 2015-07-25 11:46:32 +0000 | [diff] [blame] | 771 | if (SetImageMonochrome(image,exception) != MagickFalse) { |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 772 | bits_per_pixel=1; |
| 773 | } else if (image->colors <= 4) { |
| 774 | bits_per_pixel=2; |
| 775 | } else if (image->colors <= 8) { |
| 776 | bits_per_pixel=3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 777 | } else { |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 778 | bits_per_pixel=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 779 | } |
cristy | 7243780 | 2015-03-23 00:42:27 +0000 | [diff] [blame] | 780 | (void) ResetMagickMemory(&pdb_info,0,sizeof(pdb_info)); |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 781 | (void) CopyMagickString(pdb_info.name,image_info->filename, |
| 782 | sizeof(pdb_info.name)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | pdb_info.attributes=0; |
| 784 | pdb_info.version=0; |
| 785 | pdb_info.create_time=time(NULL); |
| 786 | pdb_info.modify_time=pdb_info.create_time; |
| 787 | pdb_info.archive_time=0; |
| 788 | pdb_info.modify_number=0; |
| 789 | pdb_info.application_info=0; |
| 790 | pdb_info.sort_info=0; |
| 791 | (void) CopyMagickMemory(pdb_info.type,"vIMG",4); |
| 792 | (void) CopyMagickMemory(pdb_info.id,"View",4); |
| 793 | pdb_info.seed=0; |
| 794 | pdb_info.next_record=0; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 795 | comment=GetImageProperty(image,"comment",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 796 | pdb_info.number_records=(comment == (const char *) NULL ? 1 : 2); |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 797 | (void) WriteBlob(image,sizeof(pdb_info.name),(unsigned char *) pdb_info.name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.attributes); |
| 799 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.version); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 800 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.create_time); |
| 801 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_time); |
| 802 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.archive_time); |
| 803 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.modify_number); |
| 804 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.application_info); |
| 805 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.sort_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 806 | (void) WriteBlob(image,4,(unsigned char *) pdb_info.type); |
| 807 | (void) WriteBlob(image,4,(unsigned char *) pdb_info.id); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 808 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.seed); |
| 809 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_info.next_record); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_info.number_records); |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 811 | (void) CopyMagickString(pdb_image.name,pdb_info.name,sizeof(pdb_image.name)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | pdb_image.version=1; /* RLE Compressed */ |
| 813 | switch (bits_per_pixel) |
| 814 | { |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 815 | case 1: pdb_image.type=(unsigned char) 0xff; break; /* monochrome */ |
| 816 | case 2: pdb_image.type=(unsigned char) 0x00; break; /* 2 bit gray */ |
| 817 | default: pdb_image.type=(unsigned char) 0x02; /* 4 bit gray */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 818 | } |
| 819 | pdb_image.reserved_1=0; |
| 820 | pdb_image.note=0; |
| 821 | pdb_image.x_last=0; |
| 822 | pdb_image.y_last=0; |
| 823 | pdb_image.reserved_2=0; |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 824 | pdb_image.x_anchor=(unsigned short) 0xffff; |
| 825 | pdb_image.y_anchor=(unsigned short) 0xffff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 826 | pdb_image.width=(short) image->columns; |
| 827 | if (image->columns % 16) |
| 828 | pdb_image.width=(short) (16*(image->columns/16+1)); |
| 829 | pdb_image.height=(short) image->rows; |
cristy | 7e5d1da | 2014-12-28 00:40:02 +0000 | [diff] [blame] | 830 | packets=((bits_per_pixel*image->columns+7)/8); |
Cristy | 7b93db1 | 2016-02-20 09:26:04 -0500 | [diff] [blame] | 831 | runlength=(unsigned char *) AcquireQuantumMemory(9UL*packets, |
cristy | 7e5d1da | 2014-12-28 00:40:02 +0000 | [diff] [blame] | 832 | image->rows*sizeof(*runlength)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 833 | if (runlength == (unsigned char *) NULL) |
| 834 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
Cristy | 5a34d7a | 2016-03-30 18:47:29 -0400 | [diff] [blame] | 835 | buffer=(unsigned char *) AcquireQuantumMemory(512,sizeof(*buffer)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | if (buffer == (unsigned char *) NULL) |
| 837 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
Cristy | eedd0c3 | 2016-08-23 17:42:10 -0400 | [diff] [blame] | 838 | packet_size=(size_t) (image->depth > 8 ? 2 : 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 839 | scanline=(unsigned char *) AcquireQuantumMemory(image->columns,packet_size* |
| 840 | sizeof(*scanline)); |
| 841 | if (scanline == (unsigned char *) NULL) |
| 842 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 843 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 844 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 845 | /* |
| 846 | Convert to GRAY raster scanline. |
| 847 | */ |
cristy | 5f766ef | 2014-12-14 21:12:47 +0000 | [diff] [blame] | 848 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | if (quantum_info == (QuantumInfo *) NULL) |
| 850 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
Cristy | eedd0c3 | 2016-08-23 17:42:10 -0400 | [diff] [blame] | 851 | status=SetQuantumDepth(image,quantum_info,image->depth > 8 ? 16 : 8); |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 852 | bits=8/(int) bits_per_pixel-1; /* start at most significant bits */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | literal=0; |
| 854 | repeat=0; |
| 855 | q=runlength; |
| 856 | buffer[0]=0x00; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 857 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 858 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 859 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 860 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 861 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 862 | (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 863 | GrayQuantum,scanline,exception); |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 864 | for (x=0; x < (ssize_t) pdb_image.width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 865 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 866 | if (x < (ssize_t) image->columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 867 | buffer[literal+repeat]|=(0xff-scanline[x*packet_size]) >> |
| 868 | (8-bits_per_pixel) << bits*bits_per_pixel; |
| 869 | bits--; |
| 870 | if (bits < 0) |
| 871 | { |
| 872 | if (((literal+repeat) > 0) && |
| 873 | (buffer[literal+repeat] == buffer[literal+repeat-1])) |
| 874 | { |
| 875 | if (repeat == 0) |
| 876 | { |
| 877 | literal--; |
| 878 | repeat++; |
| 879 | } |
| 880 | repeat++; |
| 881 | if (0x7f < repeat) |
| 882 | { |
| 883 | q=EncodeRLE(q,buffer,literal,repeat); |
| 884 | literal=0; |
| 885 | repeat=0; |
| 886 | } |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | if (repeat >= 2) |
| 891 | literal+=repeat; |
| 892 | else |
| 893 | { |
| 894 | q=EncodeRLE(q,buffer,literal,repeat); |
| 895 | buffer[0]=buffer[literal+repeat]; |
| 896 | literal=0; |
| 897 | } |
| 898 | literal++; |
| 899 | repeat=0; |
| 900 | if (0x7f < literal) |
| 901 | { |
| 902 | q=EncodeRLE(q,buffer,(literal < 0x80 ? literal : 0x80),0); |
| 903 | (void) CopyMagickMemory(buffer,buffer+literal+repeat,0x80); |
| 904 | literal-=0x80; |
| 905 | } |
| 906 | } |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 907 | bits=8/(int) bits_per_pixel-1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | buffer[literal+repeat]=0x00; |
| 909 | } |
| 910 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 911 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 98cba56 | 2014-11-20 23:46:19 +0000 | [diff] [blame] | 912 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 913 | if (status == MagickFalse) |
| 914 | break; |
| 915 | } |
| 916 | q=EncodeRLE(q,buffer,literal,repeat); |
| 917 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 918 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 919 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 920 | /* |
| 921 | Write the Image record header. |
| 922 | */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 923 | (void) WriteBlobMSBLong(image,(unsigned int) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 924 | (TellBlob(image)+8*pdb_info.number_records)); |
| 925 | (void) WriteBlobByte(image,0x40); |
| 926 | (void) WriteBlobByte(image,0x6f); |
| 927 | (void) WriteBlobByte(image,0x80); |
| 928 | (void) WriteBlobByte(image,0); |
| 929 | if (pdb_info.number_records > 1) |
| 930 | { |
| 931 | /* |
| 932 | Write the comment record header. |
| 933 | */ |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 934 | (void) WriteBlobMSBLong(image,(unsigned int) (TellBlob(image)+8+58+q- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 935 | runlength)); |
| 936 | (void) WriteBlobByte(image,0x40); |
| 937 | (void) WriteBlobByte(image,0x6f); |
| 938 | (void) WriteBlobByte(image,0x80); |
| 939 | (void) WriteBlobByte(image,1); |
| 940 | } |
| 941 | /* |
| 942 | Write the Image data. |
| 943 | */ |
cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 944 | (void) WriteBlob(image,sizeof(pdb_image.name),(unsigned char *) |
| 945 | pdb_image.name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 946 | (void) WriteBlobByte(image,(unsigned char) pdb_image.version); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 947 | (void) WriteBlobByte(image,pdb_image.type); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 948 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_1); |
| 949 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.note); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.x_last); |
| 951 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.y_last); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 952 | (void) WriteBlobMSBLong(image,(unsigned int) pdb_image.reserved_2); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 953 | (void) WriteBlobMSBShort(image,pdb_image.x_anchor); |
| 954 | (void) WriteBlobMSBShort(image,pdb_image.y_anchor); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 955 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.width); |
| 956 | (void) WriteBlobMSBShort(image,(unsigned short) pdb_image.height); |
| 957 | (void) WriteBlob(image,(size_t) (q-runlength),runlength); |
| 958 | runlength=(unsigned char *) RelinquishMagickMemory(runlength); |
| 959 | if (pdb_info.number_records > 1) |
| 960 | (void) WriteBlobString(image,comment); |
| 961 | (void) CloseBlob(image); |
| 962 | return(MagickTrue); |
| 963 | } |