cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP CCCC X X % |
| 7 | % P P C X X % |
| 8 | % PPPP C X % |
| 9 | % P C X X % |
| 10 | % P CCCC X X % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write ZSoft IBM PC Paintbrush 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 | % |
| 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/attribute.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #include "MagickCore/exception.h" |
| 53 | #include "MagickCore/exception-private.h" |
| 54 | #include "MagickCore/image.h" |
| 55 | #include "MagickCore/image-private.h" |
| 56 | #include "MagickCore/list.h" |
| 57 | #include "MagickCore/magick.h" |
| 58 | #include "MagickCore/memory_.h" |
Cristy | 8909c0f | 2016-05-29 16:15:13 -0400 | [diff] [blame] | 59 | #include "MagickCore/memory-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 60 | #include "MagickCore/monitor.h" |
| 61 | #include "MagickCore/monitor-private.h" |
| 62 | #include "MagickCore/pixel-accessor.h" |
| 63 | #include "MagickCore/quantum-private.h" |
| 64 | #include "MagickCore/static.h" |
| 65 | #include "MagickCore/string_.h" |
| 66 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | Typedef declarations. |
| 70 | */ |
| 71 | typedef struct _PCXInfo |
| 72 | { |
| 73 | unsigned char |
| 74 | identifier, |
| 75 | version, |
| 76 | encoding, |
| 77 | bits_per_pixel; |
| 78 | |
| 79 | unsigned short |
| 80 | left, |
| 81 | top, |
| 82 | right, |
| 83 | bottom, |
| 84 | horizontal_resolution, |
| 85 | vertical_resolution; |
| 86 | |
| 87 | unsigned char |
| 88 | reserved, |
| 89 | planes; |
| 90 | |
| 91 | unsigned short |
| 92 | bytes_per_line, |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 93 | palette_info, |
| 94 | horizontal_screensize, |
| 95 | vertical_screensize; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 96 | |
| 97 | unsigned char |
| 98 | colormap_signature; |
| 99 | } PCXInfo; |
| 100 | |
| 101 | /* |
| 102 | Forward declarations. |
| 103 | */ |
| 104 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 105 | WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 109 | % % |
| 110 | % % |
| 111 | % % |
| 112 | % I s D C X % |
| 113 | % % |
| 114 | % % |
| 115 | % % |
| 116 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 117 | % |
| 118 | % IsDCX() returns MagickTrue if the image format type, identified by the |
| 119 | % magick string, is DCX. |
| 120 | % |
| 121 | % The format of the IsDCX method is: |
| 122 | % |
| 123 | % MagickBooleanType IsDCX(const unsigned char *magick,const size_t length) |
| 124 | % |
| 125 | % A description of each parameter follows: |
| 126 | % |
| 127 | % o magick: compare image format pattern against these bytes. |
| 128 | % |
| 129 | % o length: Specifies the length of the magick string. |
| 130 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | */ |
| 132 | static MagickBooleanType IsDCX(const unsigned char *magick,const size_t length) |
| 133 | { |
| 134 | if (length < 4) |
| 135 | return(MagickFalse); |
| 136 | if (memcmp(magick,"\261\150\336\72",4) == 0) |
| 137 | return(MagickTrue); |
| 138 | return(MagickFalse); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 143 | % % |
| 144 | % % |
| 145 | % % |
| 146 | % I s P C X % |
| 147 | % % |
| 148 | % % |
| 149 | % % |
| 150 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 151 | % |
| 152 | % IsPCX() returns MagickTrue if the image format type, identified by the |
| 153 | % magick string, is PCX. |
| 154 | % |
| 155 | % The format of the IsPCX method is: |
| 156 | % |
| 157 | % MagickBooleanType IsPCX(const unsigned char *magick,const size_t length) |
| 158 | % |
| 159 | % A description of each parameter follows: |
| 160 | % |
| 161 | % o magick: compare image format pattern against these bytes. |
| 162 | % |
| 163 | % o length: Specifies the length of the magick string. |
| 164 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 165 | */ |
| 166 | static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length) |
| 167 | { |
| 168 | if (length < 2) |
| 169 | return(MagickFalse); |
| 170 | if (memcmp(magick,"\012\002",2) == 0) |
| 171 | return(MagickTrue); |
| 172 | if (memcmp(magick,"\012\005",2) == 0) |
| 173 | return(MagickTrue); |
| 174 | return(MagickFalse); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 179 | % % |
| 180 | % % |
| 181 | % % |
| 182 | % R e a d P C X I m a g e % |
| 183 | % % |
| 184 | % % |
| 185 | % % |
| 186 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 187 | % |
| 188 | % ReadPCXImage() reads a ZSoft IBM PC Paintbrush file and returns it. |
| 189 | % It allocates the memory necessary for the new Image structure and returns |
| 190 | % a pointer to the new image. |
| 191 | % |
| 192 | % The format of the ReadPCXImage method is: |
| 193 | % |
| 194 | % Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 195 | % |
| 196 | % A description of each parameter follows: |
| 197 | % |
| 198 | % o image_info: the image info. |
| 199 | % |
| 200 | % o exception: return any errors or warnings in this structure. |
| 201 | % |
| 202 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 204 | { |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 205 | #define ThrowPCXException(severity,tag) \ |
| 206 | { \ |
| 207 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); \ |
| 208 | pixel_info=RelinquishVirtualMemory(pixel_info); \ |
| 209 | ThrowReaderException(severity,tag); \ |
| 210 | } |
| 211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | Image |
| 213 | *image; |
| 214 | |
| 215 | int |
| 216 | bits, |
| 217 | id, |
| 218 | mask; |
| 219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 220 | MagickBooleanType |
| 221 | status; |
| 222 | |
| 223 | MagickOffsetType |
| 224 | offset, |
| 225 | *page_table; |
| 226 | |
cristy | 0acf7e1 | 2013-06-30 23:35:36 +0000 | [diff] [blame] | 227 | MemoryInfo |
| 228 | *pixel_info; |
| 229 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 230 | PCXInfo |
| 231 | pcx_info; |
| 232 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 233 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | x; |
| 235 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 236 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 237 | *q; |
| 238 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 239 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | i; |
| 241 | |
| 242 | register unsigned char |
| 243 | *p, |
| 244 | *r; |
| 245 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 246 | size_t |
| 247 | one, |
| 248 | pcx_packets; |
| 249 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 250 | ssize_t |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 251 | count, |
| 252 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | |
| 254 | unsigned char |
| 255 | packet, |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 256 | pcx_colormap[768], |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 257 | *pixels, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | *scanline; |
| 259 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 260 | /* |
| 261 | Open image file. |
| 262 | */ |
| 263 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 264 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | if (image_info->debug != MagickFalse) |
| 266 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 267 | image_info->filename); |
| 268 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 269 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 270 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 272 | if (status == MagickFalse) |
| 273 | { |
| 274 | image=DestroyImageList(image); |
| 275 | return((Image *) NULL); |
| 276 | } |
| 277 | /* |
| 278 | Determine if this a PCX file. |
| 279 | */ |
| 280 | page_table=(MagickOffsetType *) NULL; |
| 281 | if (LocaleCompare(image_info->magick,"DCX") == 0) |
| 282 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 283 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 284 | magic; |
| 285 | |
| 286 | /* |
| 287 | Read the DCX page table. |
| 288 | */ |
| 289 | magic=ReadBlobLSBLong(image); |
| 290 | if (magic != 987654321) |
| 291 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 292 | page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL, |
| 293 | sizeof(*page_table)); |
| 294 | if (page_table == (MagickOffsetType *) NULL) |
| 295 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 296 | for (id=0; id < 1024; id++) |
| 297 | { |
| 298 | page_table[id]=(MagickOffsetType) ReadBlobLSBLong(image); |
| 299 | if (page_table[id] == 0) |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (page_table != (MagickOffsetType *) NULL) |
| 304 | { |
| 305 | offset=SeekBlob(image,(MagickOffsetType) page_table[0],SEEK_SET); |
| 306 | if (offset < 0) |
| 307 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 308 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 309 | count=ReadBlob(image,1,&pcx_info.identifier); |
| 310 | for (id=1; id < 1024; id++) |
| 311 | { |
cristy | c99e4c2 | 2014-04-30 23:37:58 +0000 | [diff] [blame] | 312 | int |
| 313 | bits_per_pixel; |
| 314 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 315 | /* |
| 316 | Verify PCX identifier. |
| 317 | */ |
| 318 | pcx_info.version=(unsigned char) ReadBlobByte(image); |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 319 | if ((count != 1) || (pcx_info.identifier != 0x0a)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 320 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 321 | pcx_info.encoding=(unsigned char) ReadBlobByte(image); |
cristy | c99e4c2 | 2014-04-30 23:37:58 +0000 | [diff] [blame] | 322 | bits_per_pixel=ReadBlobByte(image); |
| 323 | if (bits_per_pixel == -1) |
| 324 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 325 | pcx_info.bits_per_pixel=(unsigned char) bits_per_pixel; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | pcx_info.left=ReadBlobLSBShort(image); |
| 327 | pcx_info.top=ReadBlobLSBShort(image); |
| 328 | pcx_info.right=ReadBlobLSBShort(image); |
| 329 | pcx_info.bottom=ReadBlobLSBShort(image); |
| 330 | pcx_info.horizontal_resolution=ReadBlobLSBShort(image); |
| 331 | pcx_info.vertical_resolution=ReadBlobLSBShort(image); |
| 332 | /* |
| 333 | Read PCX raster colormap. |
| 334 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 335 | image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | pcx_info.left)+1UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 337 | image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | pcx_info.top)+1UL; |
| 339 | if ((image->columns == 0) || (image->rows == 0) || |
Cristy | f41bcd1 | 2017-03-28 05:59:57 -0400 | [diff] [blame] | 340 | ((pcx_info.bits_per_pixel != 1) && (pcx_info.bits_per_pixel != 2) && |
| 341 | (pcx_info.bits_per_pixel != 4) && (pcx_info.bits_per_pixel != 8))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 342 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 343 | image->depth=pcx_info.bits_per_pixel; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | image->units=PixelsPerInchResolution; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 345 | image->resolution.x=(double) pcx_info.horizontal_resolution; |
| 346 | image->resolution.y=(double) pcx_info.vertical_resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | image->colors=16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 348 | count=ReadBlob(image,3*image->colors,pcx_colormap); |
cristy | 7c44354 | 2015-01-05 23:19:48 +0000 | [diff] [blame] | 349 | if (count != (ssize_t) (3*image->colors)) |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 350 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | pcx_info.reserved=(unsigned char) ReadBlobByte(image); |
| 352 | pcx_info.planes=(unsigned char) ReadBlobByte(image); |
Cristy | f41bcd1 | 2017-03-28 05:59:57 -0400 | [diff] [blame] | 353 | if (pcx_info.planes == 0) |
| 354 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 7163b3e | 2014-05-24 22:18:20 +0000 | [diff] [blame] | 355 | if ((pcx_info.bits_per_pixel*pcx_info.planes) >= 64) |
| 356 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 357 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 358 | if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1)) |
| 359 | if ((pcx_info.version == 3) || (pcx_info.version == 5) || |
| 360 | ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)) |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 361 | image->colors=(size_t) MagickMin(one << (1UL* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 362 | (pcx_info.bits_per_pixel*pcx_info.planes)),256UL); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 363 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 365 | if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1)) |
| 366 | image->storage_class=DirectClass; |
| 367 | p=pcx_colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 368 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 369 | { |
| 370 | image->colormap[i].red=ScaleCharToQuantum(*p++); |
| 371 | image->colormap[i].green=ScaleCharToQuantum(*p++); |
| 372 | image->colormap[i].blue=ScaleCharToQuantum(*p++); |
| 373 | } |
| 374 | pcx_info.bytes_per_line=ReadBlobLSBShort(image); |
| 375 | pcx_info.palette_info=ReadBlobLSBShort(image); |
cristy | 29d4033 | 2014-12-30 23:48:22 +0000 | [diff] [blame] | 376 | pcx_info.horizontal_screensize=ReadBlobLSBShort(image); |
| 377 | pcx_info.vertical_screensize=ReadBlobLSBShort(image); |
| 378 | for (i=0; i < 54; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 379 | (void) ReadBlobByte(image); |
| 380 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 381 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 382 | break; |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 383 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 384 | if (status == MagickFalse) |
| 385 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | /* |
| 387 | Read image data. |
| 388 | */ |
dirk | 11358a5 | 2016-05-29 21:59:45 +0200 | [diff] [blame] | 389 | if (HeapOverflowSanityCheck(image->rows, (size_t) pcx_info.bytes_per_line) != MagickFalse) |
| 390 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 391 | pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line; |
Cristy | f41bcd1 | 2017-03-28 05:59:57 -0400 | [diff] [blame] | 392 | if (HeapOverflowSanityCheck(pcx_packets, (size_t) pcx_info.planes) != MagickFalse) |
dirk | 11358a5 | 2016-05-29 21:59:45 +0200 | [diff] [blame] | 393 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 394 | pcx_packets=(size_t) pcx_packets*pcx_info.planes; |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 395 | if ((size_t) (pcx_info.bits_per_pixel*pcx_info.planes*image->columns) > |
| 396 | (pcx_packets*8U)) |
| 397 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 398 | scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns, |
| 399 | pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline)); |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 400 | pixel_info=AcquireVirtualMemory(pcx_packets,2*sizeof(*pixels)); |
cristy | 0acf7e1 | 2013-06-30 23:35:36 +0000 | [diff] [blame] | 401 | if ((scanline == (unsigned char *) NULL) || |
| 402 | (pixel_info == (MemoryInfo *) NULL)) |
| 403 | { |
| 404 | if (scanline != (unsigned char *) NULL) |
| 405 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 406 | if (pixel_info != (MemoryInfo *) NULL) |
| 407 | pixel_info=RelinquishVirtualMemory(pixel_info); |
| 408 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 409 | } |
| 410 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | /* |
| 412 | Uncompress image data. |
| 413 | */ |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 414 | p=pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 415 | if (pcx_info.encoding == 0) |
| 416 | while (pcx_packets != 0) |
| 417 | { |
| 418 | packet=(unsigned char) ReadBlobByte(image); |
| 419 | if (EOFBlob(image) != MagickFalse) |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 420 | ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | *p++=packet; |
| 422 | pcx_packets--; |
| 423 | } |
| 424 | else |
| 425 | while (pcx_packets != 0) |
| 426 | { |
| 427 | packet=(unsigned char) ReadBlobByte(image); |
| 428 | if (EOFBlob(image) != MagickFalse) |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 429 | ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 430 | if ((packet & 0xc0) != 0xc0) |
| 431 | { |
| 432 | *p++=packet; |
| 433 | pcx_packets--; |
| 434 | continue; |
| 435 | } |
| 436 | count=(ssize_t) (packet & 0x3f); |
| 437 | packet=(unsigned char) ReadBlobByte(image); |
| 438 | if (EOFBlob(image) != MagickFalse) |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 439 | ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | for ( ; count != 0; count--) |
| 441 | { |
| 442 | *p++=packet; |
| 443 | pcx_packets--; |
| 444 | if (pcx_packets == 0) |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | if (image->storage_class == DirectClass) |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 449 | image->alpha_trait=pcx_info.planes > 3 ? BlendPixelTrait : |
| 450 | UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 451 | else |
| 452 | if ((pcx_info.version == 5) || |
| 453 | ((pcx_info.bits_per_pixel*pcx_info.planes) == 1)) |
| 454 | { |
| 455 | /* |
| 456 | Initialize image colormap. |
| 457 | */ |
| 458 | if (image->colors > 256) |
dirk | 08f033c | 2014-10-23 21:53:50 +0000 | [diff] [blame] | 459 | ThrowPCXException(CorruptImageError,"ColormapExceeds256Colors"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1) |
| 461 | { |
| 462 | /* |
| 463 | Monochrome colormap. |
| 464 | */ |
| 465 | image->colormap[0].red=(Quantum) 0; |
| 466 | image->colormap[0].green=(Quantum) 0; |
| 467 | image->colormap[0].blue=(Quantum) 0; |
cristy | 6e963d8 | 2012-06-19 15:23:24 +0000 | [diff] [blame] | 468 | image->colormap[1].red=QuantumRange; |
| 469 | image->colormap[1].green=QuantumRange; |
| 470 | image->colormap[1].blue=QuantumRange; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 471 | } |
| 472 | else |
| 473 | if (image->colors > 16) |
| 474 | { |
| 475 | /* |
| 476 | 256 color images have their color map at the end of the file. |
| 477 | */ |
| 478 | pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image); |
| 479 | count=ReadBlob(image,3*image->colors,pcx_colormap); |
| 480 | p=pcx_colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 481 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 482 | { |
| 483 | image->colormap[i].red=ScaleCharToQuantum(*p++); |
| 484 | image->colormap[i].green=ScaleCharToQuantum(*p++); |
| 485 | image->colormap[i].blue=ScaleCharToQuantum(*p++); |
| 486 | } |
| 487 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | } |
| 489 | /* |
| 490 | Convert PCX raster image to pixel packets. |
| 491 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 492 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 493 | { |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 494 | p=pixels+(y*pcx_info.bytes_per_line*pcx_info.planes); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 496 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 498 | r=scanline; |
| 499 | if (image->storage_class == DirectClass) |
| 500 | for (i=0; i < pcx_info.planes; i++) |
| 501 | { |
| 502 | r=scanline+i; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 503 | for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | { |
| 505 | switch (i) |
| 506 | { |
| 507 | case 0: |
| 508 | { |
| 509 | *r=(*p++); |
| 510 | break; |
| 511 | } |
| 512 | case 1: |
| 513 | { |
| 514 | *r=(*p++); |
| 515 | break; |
| 516 | } |
| 517 | case 2: |
| 518 | { |
| 519 | *r=(*p++); |
| 520 | break; |
| 521 | } |
| 522 | case 3: |
| 523 | default: |
| 524 | { |
| 525 | *r=(*p++); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | r+=pcx_info.planes; |
| 530 | } |
| 531 | } |
| 532 | else |
| 533 | if (pcx_info.planes > 1) |
| 534 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 535 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | *r++=0; |
| 537 | for (i=0; i < pcx_info.planes; i++) |
| 538 | { |
| 539 | r=scanline; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 540 | for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | { |
| 542 | bits=(*p++); |
| 543 | for (mask=0x80; mask != 0; mask>>=1) |
| 544 | { |
| 545 | if (bits & mask) |
| 546 | *r|=1 << i; |
| 547 | r++; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | else |
| 553 | switch (pcx_info.bits_per_pixel) |
| 554 | { |
| 555 | case 1: |
| 556 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 557 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | bit; |
| 559 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 560 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | { |
| 562 | for (bit=7; bit >= 0; bit--) |
| 563 | *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00); |
| 564 | p++; |
| 565 | } |
| 566 | if ((image->columns % 8) != 0) |
| 567 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 568 | for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00); |
| 570 | p++; |
| 571 | } |
| 572 | break; |
| 573 | } |
| 574 | case 2: |
| 575 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 576 | for (x=0; x < ((ssize_t) image->columns-3); x+=4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 577 | { |
| 578 | *r++=(*p >> 6) & 0x3; |
| 579 | *r++=(*p >> 4) & 0x3; |
| 580 | *r++=(*p >> 2) & 0x3; |
| 581 | *r++=(*p) & 0x3; |
| 582 | p++; |
| 583 | } |
| 584 | if ((image->columns % 4) != 0) |
| 585 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 586 | for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 587 | *r++=(unsigned char) ((*p >> (i*2)) & 0x03); |
| 588 | p++; |
| 589 | } |
| 590 | break; |
| 591 | } |
| 592 | case 4: |
| 593 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 594 | for (x=0; x < ((ssize_t) image->columns-1); x+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 595 | { |
| 596 | *r++=(*p >> 4) & 0xf; |
| 597 | *r++=(*p) & 0xf; |
| 598 | p++; |
| 599 | } |
| 600 | if ((image->columns % 2) != 0) |
| 601 | *r++=(*p++ >> 4) & 0xf; |
| 602 | break; |
| 603 | } |
| 604 | case 8: |
| 605 | { |
| 606 | (void) CopyMagickMemory(r,p,image->columns); |
| 607 | break; |
| 608 | } |
| 609 | default: |
| 610 | break; |
| 611 | } |
| 612 | /* |
| 613 | Transfer image scanline. |
| 614 | */ |
| 615 | r=scanline; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 616 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | { |
| 618 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 619 | SetPixelIndex(image,*r++,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 620 | else |
| 621 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 622 | SetPixelRed(image,ScaleCharToQuantum(*r++),q); |
| 623 | SetPixelGreen(image,ScaleCharToQuantum(*r++),q); |
| 624 | SetPixelBlue(image,ScaleCharToQuantum(*r++),q); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 625 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 626 | SetPixelAlpha(image,ScaleCharToQuantum(*r++),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 627 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 628 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | } |
| 630 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 631 | break; |
| 632 | if (image->previous == (Image *) NULL) |
| 633 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 634 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 635 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | if (status == MagickFalse) |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | if (image->storage_class == PseudoClass) |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 641 | (void) SyncImage(image,exception); |
cristy | 0acf7e1 | 2013-06-30 23:35:36 +0000 | [diff] [blame] | 642 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 643 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | if (EOFBlob(image) != MagickFalse) |
| 645 | { |
| 646 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 647 | image->filename); |
| 648 | break; |
| 649 | } |
| 650 | /* |
| 651 | Proceed to next image. |
| 652 | */ |
| 653 | if (image_info->number_scenes != 0) |
| 654 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 655 | break; |
| 656 | if (page_table == (MagickOffsetType *) NULL) |
| 657 | break; |
| 658 | if (page_table[id] == 0) |
| 659 | break; |
| 660 | offset=SeekBlob(image,(MagickOffsetType) page_table[id],SEEK_SET); |
| 661 | if (offset < 0) |
| 662 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 663 | count=ReadBlob(image,1,&pcx_info.identifier); |
| 664 | if ((count != 0) && (pcx_info.identifier == 0x0a)) |
| 665 | { |
| 666 | /* |
| 667 | Allocate next image structure. |
| 668 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 669 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | if (GetNextImageInList(image) == (Image *) NULL) |
| 671 | { |
| 672 | image=DestroyImageList(image); |
| 673 | return((Image *) NULL); |
| 674 | } |
| 675 | image=SyncNextImageInList(image); |
| 676 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 677 | GetBlobSize(image)); |
| 678 | if (status == MagickFalse) |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | if (page_table != (MagickOffsetType *) NULL) |
| 683 | page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table); |
| 684 | (void) CloseBlob(image); |
| 685 | return(GetFirstImageInList(image)); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 690 | % % |
| 691 | % % |
| 692 | % % |
| 693 | % R e g i s t e r P C X I m a g e % |
| 694 | % % |
| 695 | % % |
| 696 | % % |
| 697 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 698 | % |
| 699 | % RegisterPCXImage() adds attributes for the PCX image format to |
| 700 | % the list of supported formats. The attributes include the image format |
| 701 | % tag, a method to read and/or write the format, whether the format |
| 702 | % supports the saving of more than one frame to the same file or blob, |
| 703 | % whether the format supports native in-memory I/O, and a brief |
| 704 | % description of the format. |
| 705 | % |
| 706 | % The format of the RegisterPCXImage method is: |
| 707 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 708 | % size_t RegisterPCXImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 709 | % |
| 710 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 711 | ModuleExport size_t RegisterPCXImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 712 | { |
| 713 | MagickInfo |
| 714 | *entry; |
| 715 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 716 | entry=AcquireMagickInfo("PCX","DCX","ZSoft IBM PC multi-page Paintbrush"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 717 | entry->decoder=(DecodeImageHandler *) ReadPCXImage; |
| 718 | entry->encoder=(EncodeImageHandler *) WritePCXImage; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 719 | entry->flags|=CoderDecoderSeekableStreamFlag; |
| 720 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 721 | entry->magick=(IsImageFormatHandler *) IsDCX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 722 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 723 | entry=AcquireMagickInfo("PCX","PCX","ZSoft IBM PC Paintbrush"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 724 | entry->decoder=(DecodeImageHandler *) ReadPCXImage; |
| 725 | entry->encoder=(EncodeImageHandler *) WritePCXImage; |
| 726 | entry->magick=(IsImageFormatHandler *) IsPCX; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 727 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 728 | entry->flags|=CoderDecoderSeekableStreamFlag; |
| 729 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 730 | (void) RegisterMagickInfo(entry); |
| 731 | return(MagickImageCoderSignature); |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 736 | % % |
| 737 | % % |
| 738 | % % |
| 739 | % U n r e g i s t e r P C X I m a g e % |
| 740 | % % |
| 741 | % % |
| 742 | % % |
| 743 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 744 | % |
| 745 | % UnregisterPCXImage() removes format registrations made by the |
| 746 | % PCX module from the list of supported formats. |
| 747 | % |
| 748 | % The format of the UnregisterPCXImage method is: |
| 749 | % |
| 750 | % UnregisterPCXImage(void) |
| 751 | % |
| 752 | */ |
| 753 | ModuleExport void UnregisterPCXImage(void) |
| 754 | { |
| 755 | (void) UnregisterMagickInfo("DCX"); |
| 756 | (void) UnregisterMagickInfo("PCX"); |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 761 | % % |
| 762 | % % |
| 763 | % % |
| 764 | % W r i t e P C X I m a g e % |
| 765 | % % |
| 766 | % % |
| 767 | % % |
| 768 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 769 | % |
| 770 | % WritePCXImage() writes an image in the ZSoft IBM PC Paintbrush file |
| 771 | % format. |
| 772 | % |
| 773 | % The format of the WritePCXImage method is: |
| 774 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 775 | % MagickBooleanType WritePCXImage(const ImageInfo *image_info, |
| 776 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 777 | % |
| 778 | % A description of each parameter follows. |
| 779 | % |
| 780 | % o image_info: the image info. |
| 781 | % |
| 782 | % o image: The image. |
| 783 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 784 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 785 | % |
| 786 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 787 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 788 | static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info, |
| 789 | const unsigned char *pixels,Image *image) |
| 790 | { |
| 791 | register const unsigned char |
| 792 | *q; |
| 793 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 794 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 795 | i, |
| 796 | x; |
| 797 | |
| 798 | ssize_t |
| 799 | count; |
| 800 | |
| 801 | unsigned char |
| 802 | packet, |
| 803 | previous; |
| 804 | |
| 805 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 806 | for (i=0; i < (ssize_t) pcx_info->planes; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 807 | { |
cristy | 1f9852b | 2010-09-04 15:05:36 +0000 | [diff] [blame] | 808 | if (pcx_info->encoding == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 809 | { |
cristy | 1f9852b | 2010-09-04 15:05:36 +0000 | [diff] [blame] | 810 | for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++) |
| 811 | (void) WriteBlobByte(image,(unsigned char) (*q++)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | } |
cristy | 1f9852b | 2010-09-04 15:05:36 +0000 | [diff] [blame] | 813 | else |
| 814 | { |
| 815 | previous=(*q++); |
| 816 | count=1; |
| 817 | for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++) |
| 818 | { |
| 819 | packet=(*q++); |
| 820 | if ((packet == previous) && (count < 63)) |
| 821 | { |
| 822 | count++; |
| 823 | continue; |
| 824 | } |
| 825 | if ((count > 1) || ((previous & 0xc0) == 0xc0)) |
| 826 | { |
| 827 | count|=0xc0; |
| 828 | (void) WriteBlobByte(image,(unsigned char) count); |
| 829 | } |
| 830 | (void) WriteBlobByte(image,previous); |
| 831 | previous=packet; |
| 832 | count=1; |
| 833 | } |
| 834 | if ((count > 1) || ((previous & 0xc0) == 0xc0)) |
| 835 | { |
| 836 | count|=0xc0; |
| 837 | (void) WriteBlobByte(image,(unsigned char) count); |
| 838 | } |
| 839 | (void) WriteBlobByte(image,previous); |
| 840 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 841 | } |
| 842 | return (MagickTrue); |
| 843 | } |
| 844 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 845 | static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image, |
| 846 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 847 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 848 | MagickBooleanType |
| 849 | status; |
| 850 | |
| 851 | MagickOffsetType |
| 852 | offset, |
| 853 | *page_table, |
| 854 | scene; |
| 855 | |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 856 | MemoryInfo |
| 857 | *pixel_info; |
| 858 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 859 | PCXInfo |
| 860 | pcx_info; |
| 861 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 862 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 863 | *p; |
| 864 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 865 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 866 | i, |
| 867 | x; |
| 868 | |
| 869 | register unsigned char |
| 870 | *q; |
| 871 | |
| 872 | size_t |
| 873 | length; |
| 874 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 875 | ssize_t |
| 876 | y; |
| 877 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 878 | unsigned char |
| 879 | *pcx_colormap, |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 880 | *pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | Open output image file. |
| 884 | */ |
| 885 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 886 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 888 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 889 | if (image->debug != MagickFalse) |
| 890 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 891 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 892 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 893 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 894 | if (status == MagickFalse) |
| 895 | return(status); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 896 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 897 | page_table=(MagickOffsetType *) NULL; |
| 898 | if ((LocaleCompare(image_info->magick,"DCX") == 0) || |
| 899 | ((GetNextImageInList(image) != (Image *) NULL) && |
| 900 | (image_info->adjoin != MagickFalse))) |
| 901 | { |
| 902 | /* |
| 903 | Write the DCX page table. |
| 904 | */ |
| 905 | (void) WriteBlobLSBLong(image,0x3ADE68B1L); |
| 906 | page_table=(MagickOffsetType *) AcquireQuantumMemory(1024UL, |
| 907 | sizeof(*page_table)); |
| 908 | if (page_table == (MagickOffsetType *) NULL) |
| 909 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 910 | for (scene=0; scene < 1024; scene++) |
| 911 | (void) WriteBlobLSBLong(image,0x00000000L); |
| 912 | } |
| 913 | scene=0; |
| 914 | do |
| 915 | { |
| 916 | if (page_table != (MagickOffsetType *) NULL) |
| 917 | page_table[scene]=TellBlob(image); |
| 918 | /* |
| 919 | Initialize PCX raster file header. |
| 920 | */ |
| 921 | pcx_info.identifier=0x0a; |
| 922 | pcx_info.version=5; |
cristy | 1f9852b | 2010-09-04 15:05:36 +0000 | [diff] [blame] | 923 | pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 924 | pcx_info.bits_per_pixel=8; |
| 925 | if ((image->storage_class == PseudoClass) && |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 926 | (SetImageMonochrome(image,exception) != MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | pcx_info.bits_per_pixel=1; |
| 928 | pcx_info.left=0; |
| 929 | pcx_info.top=0; |
| 930 | pcx_info.right=(unsigned short) (image->columns-1); |
| 931 | pcx_info.bottom=(unsigned short) (image->rows-1); |
| 932 | switch (image->units) |
| 933 | { |
| 934 | case UndefinedResolution: |
| 935 | case PixelsPerInchResolution: |
| 936 | default: |
| 937 | { |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 938 | pcx_info.horizontal_resolution=(unsigned short) image->resolution.x; |
| 939 | pcx_info.vertical_resolution=(unsigned short) image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | case PixelsPerCentimeterResolution: |
| 943 | { |
| 944 | pcx_info.horizontal_resolution=(unsigned short) |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 945 | (2.54*image->resolution.x+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 946 | pcx_info.vertical_resolution=(unsigned short) |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 947 | (2.54*image->resolution.y+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | } |
| 951 | pcx_info.reserved=0; |
| 952 | pcx_info.planes=1; |
| 953 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
| 954 | { |
| 955 | pcx_info.planes=3; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 956 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 957 | pcx_info.planes++; |
| 958 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 959 | pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 960 | pcx_info.bits_per_pixel+7)/8); |
| 961 | pcx_info.palette_info=1; |
| 962 | pcx_info.colormap_signature=0x0c; |
| 963 | /* |
| 964 | Write PCX header. |
| 965 | */ |
| 966 | (void) WriteBlobByte(image,pcx_info.identifier); |
| 967 | (void) WriteBlobByte(image,pcx_info.version); |
| 968 | (void) WriteBlobByte(image,pcx_info.encoding); |
| 969 | (void) WriteBlobByte(image,pcx_info.bits_per_pixel); |
| 970 | (void) WriteBlobLSBShort(image,pcx_info.left); |
| 971 | (void) WriteBlobLSBShort(image,pcx_info.top); |
| 972 | (void) WriteBlobLSBShort(image,pcx_info.right); |
| 973 | (void) WriteBlobLSBShort(image,pcx_info.bottom); |
| 974 | (void) WriteBlobLSBShort(image,pcx_info.horizontal_resolution); |
| 975 | (void) WriteBlobLSBShort(image,pcx_info.vertical_resolution); |
| 976 | /* |
| 977 | Dump colormap to file. |
| 978 | */ |
| 979 | pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL, |
| 980 | 3*sizeof(*pcx_colormap)); |
| 981 | if (pcx_colormap == (unsigned char *) NULL) |
| 982 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 983 | (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap)); |
| 984 | q=pcx_colormap; |
| 985 | if ((image->storage_class == PseudoClass) && (image->colors <= 256)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 986 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 987 | { |
| 988 | *q++=ScaleQuantumToChar(image->colormap[i].red); |
| 989 | *q++=ScaleQuantumToChar(image->colormap[i].green); |
| 990 | *q++=ScaleQuantumToChar(image->colormap[i].blue); |
| 991 | } |
| 992 | (void) WriteBlob(image,3*16,(const unsigned char *) pcx_colormap); |
| 993 | (void) WriteBlobByte(image,pcx_info.reserved); |
| 994 | (void) WriteBlobByte(image,pcx_info.planes); |
| 995 | (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line); |
| 996 | (void) WriteBlobLSBShort(image,pcx_info.palette_info); |
| 997 | for (i=0; i < 58; i++) |
| 998 | (void) WriteBlobByte(image,'\0'); |
| 999 | length=(size_t) pcx_info.bytes_per_line; |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1000 | pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels)); |
| 1001 | if (pixel_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1003 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
| 1004 | q=pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1005 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
| 1006 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1007 | /* |
| 1008 | Convert DirectClass image to PCX raster pixels. |
| 1009 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1010 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1011 | { |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1012 | q=pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1013 | for (i=0; i < pcx_info.planes; i++) |
| 1014 | { |
cristy | 0acf7e1 | 2013-06-30 23:35:36 +0000 | [diff] [blame] | 1015 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 1016 | if (p == (const Quantum *) NULL) |
| 1017 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1018 | switch ((int) i) |
| 1019 | { |
| 1020 | case 0: |
| 1021 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1022 | for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1023 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1024 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1025 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1026 | } |
| 1027 | break; |
| 1028 | } |
| 1029 | case 1: |
| 1030 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1031 | for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1032 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1033 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1034 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1035 | } |
| 1036 | break; |
| 1037 | } |
| 1038 | case 2: |
| 1039 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1040 | for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1041 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1042 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1043 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1044 | } |
| 1045 | break; |
| 1046 | } |
| 1047 | case 3: |
| 1048 | default: |
| 1049 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1050 | for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1052 | *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p))); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1053 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1054 | } |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | } |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1059 | if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | if (image->previous == (Image *) NULL) |
| 1062 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1063 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 1064 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1065 | if (status == MagickFalse) |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | if (pcx_info.bits_per_pixel > 1) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1073 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1075 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1076 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1077 | break; |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1078 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1079 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1080 | { |
| 1081 | *q++=(unsigned char) GetPixelIndex(image,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1082 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1083 | } |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1084 | if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | if (image->previous == (Image *) NULL) |
| 1087 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1088 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 1089 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1090 | if (status == MagickFalse) |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | else |
| 1095 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1096 | register unsigned char |
| 1097 | bit, |
| 1098 | byte; |
| 1099 | |
| 1100 | /* |
| 1101 | Convert PseudoClass image to a PCX monochrome image. |
| 1102 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1103 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1104 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1105 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1106 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1107 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1108 | bit=0; |
| 1109 | byte=0; |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1110 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1111 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1112 | { |
| 1113 | byte<<=1; |
cristy | 85636be | 2014-04-04 19:16:53 +0000 | [diff] [blame] | 1114 | if (GetPixelLuma(image,p) >= (QuantumRange/2.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1115 | byte|=0x01; |
| 1116 | bit++; |
| 1117 | if (bit == 8) |
| 1118 | { |
| 1119 | *q++=byte; |
| 1120 | bit=0; |
| 1121 | byte=0; |
| 1122 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1123 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1124 | } |
| 1125 | if (bit != 0) |
| 1126 | *q++=byte << (8-bit); |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1127 | if (PCXWritePixels(&pcx_info,pixels,image) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1128 | break; |
| 1129 | if (image->previous == (Image *) NULL) |
| 1130 | { |
cristy | 1f9852b | 2010-09-04 15:05:36 +0000 | [diff] [blame] | 1131 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) |
| 1132 | y,image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1133 | if (status == MagickFalse) |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | (void) WriteBlobByte(image,pcx_info.colormap_signature); |
| 1139 | (void) WriteBlob(image,3*256,pcx_colormap); |
| 1140 | } |
cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 1141 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1142 | pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap); |
cristy | c044c48 | 2010-03-03 03:21:55 +0000 | [diff] [blame] | 1143 | if (page_table == (MagickOffsetType *) NULL) |
| 1144 | break; |
| 1145 | if (scene >= 1023) |
| 1146 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1147 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1148 | break; |
| 1149 | image=SyncNextImageInList(image); |
| 1150 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 1151 | GetImageListLength(image)); |
| 1152 | if (status == MagickFalse) |
| 1153 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1154 | } while (image_info->adjoin != MagickFalse); |
| 1155 | if (page_table != (MagickOffsetType *) NULL) |
| 1156 | { |
| 1157 | /* |
| 1158 | Write the DCX page table. |
| 1159 | */ |
| 1160 | page_table[scene+1]=0; |
| 1161 | offset=SeekBlob(image,0L,SEEK_SET); |
| 1162 | if (offset < 0) |
| 1163 | ThrowWriterException(CorruptImageError,"ImproperImageHeader"); |
| 1164 | (void) WriteBlobLSBLong(image,0x3ADE68B1L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1165 | for (i=0; i <= (ssize_t) scene; i++) |
cristy | 0b29b25 | 2010-05-30 01:59:46 +0000 | [diff] [blame] | 1166 | (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1167 | page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table); |
| 1168 | } |
| 1169 | if (status == MagickFalse) |
| 1170 | { |
| 1171 | char |
| 1172 | *message; |
| 1173 | |
| 1174 | message=GetExceptionMessage(errno); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1175 | (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError, |
| 1176 | "UnableToWriteFile","`%s': %s",image->filename,message); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1177 | message=DestroyString(message); |
| 1178 | } |
| 1179 | (void) CloseBlob(image); |
| 1180 | return(MagickTrue); |
| 1181 | } |