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