cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP CCCC L % |
| 7 | % P P C L % |
| 8 | % PPPP C L % |
| 9 | % P C L % |
| 10 | % P CCCC LLLLL % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write HP PCL Printer 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 | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 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" |
cristy | 76ce6e1 | 2013-04-05 14:33:38 +0000 | [diff] [blame] | 43 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/attribute.h" |
| 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/blob-private.h" |
| 47 | #include "MagickCore/cache.h" |
| 48 | #include "MagickCore/color.h" |
| 49 | #include "MagickCore/color-private.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/constitute.h" |
| 53 | #include "MagickCore/delegate.h" |
| 54 | #include "MagickCore/draw.h" |
| 55 | #include "MagickCore/exception.h" |
| 56 | #include "MagickCore/exception-private.h" |
| 57 | #include "MagickCore/geometry.h" |
| 58 | #include "MagickCore/image.h" |
| 59 | #include "MagickCore/image-private.h" |
| 60 | #include "MagickCore/list.h" |
| 61 | #include "MagickCore/magick.h" |
| 62 | #include "MagickCore/memory_.h" |
| 63 | #include "MagickCore/monitor.h" |
| 64 | #include "MagickCore/monitor-private.h" |
| 65 | #include "MagickCore/option.h" |
| 66 | #include "MagickCore/pixel-accessor.h" |
| 67 | #include "MagickCore/profile.h" |
| 68 | #include "MagickCore/property.h" |
| 69 | #include "MagickCore/resource_.h" |
| 70 | #include "MagickCore/quantum-private.h" |
| 71 | #include "MagickCore/static.h" |
| 72 | #include "MagickCore/string_.h" |
| 73 | #include "MagickCore/module.h" |
| 74 | #include "MagickCore/token.h" |
| 75 | #include "MagickCore/transform.h" |
| 76 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | Forward declarations. |
| 80 | */ |
| 81 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 82 | WritePCLImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % % |
| 87 | % % |
| 88 | % % |
| 89 | % I s P C L % |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | % |
| 95 | % IsPCL() returns MagickTrue if the image format type, identified by the |
| 96 | % magick string, is PCL. |
| 97 | % |
| 98 | % The format of the IsPCL method is: |
| 99 | % |
| 100 | % MagickBooleanType IsPCL(const unsigned char *magick,const size_t length) |
| 101 | % |
| 102 | % A description of each parameter follows: |
| 103 | % |
| 104 | % o magick: compare image format pattern against these bytes. |
| 105 | % |
| 106 | % o length: Specifies the length of the magick string. |
| 107 | % |
| 108 | */ |
| 109 | static MagickBooleanType IsPCL(const unsigned char *magick,const size_t length) |
| 110 | { |
| 111 | if (length < 4) |
| 112 | return(MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 113 | if (memcmp(magick,"\033E\033&",4) == 0) |
| 114 | return(MagickFalse); |
cristy | 7dca489 | 2010-09-21 16:10:30 +0000 | [diff] [blame] | 115 | if (memcmp(magick,"\033E\033",3) == 0) |
| 116 | return(MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 117 | return(MagickFalse); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 | % % |
| 123 | % % |
| 124 | % % |
| 125 | % R e a d P C L I m a g e % |
| 126 | % % |
| 127 | % % |
| 128 | % % |
| 129 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 | % |
| 131 | % ReadPCLImage() reads a Printer Control Language image file and returns it. |
| 132 | % It allocates the memory necessary for the new Image structure and returns a |
| 133 | % pointer to the new image. |
| 134 | % |
| 135 | % The format of the ReadPCLImage method is: |
| 136 | % |
| 137 | % Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 138 | % |
| 139 | % A description of each parameter follows: |
| 140 | % |
| 141 | % o image_info: the image info. |
| 142 | % |
| 143 | % o exception: return any errors or warnings in this structure. |
| 144 | % |
| 145 | */ |
| 146 | static Image *ReadPCLImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 147 | { |
| 148 | #define CropBox "CropBox" |
| 149 | #define DeviceCMYK "DeviceCMYK" |
| 150 | #define MediaBox "MediaBox" |
| 151 | #define RenderPCLText " Rendering PCL... " |
| 152 | |
| 153 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 154 | command[MagickPathExtent], |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 155 | *density, |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 156 | filename[MagickPathExtent], |
| 157 | geometry[MagickPathExtent], |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 158 | *options, |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 159 | input_filename[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 160 | |
anthony | 1afdc7a | 2011-10-05 11:54:28 +0000 | [diff] [blame] | 161 | const char |
| 162 | *option; |
| 163 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | const DelegateInfo |
| 165 | *delegate_info; |
| 166 | |
| 167 | Image |
| 168 | *image, |
| 169 | *next_image; |
| 170 | |
| 171 | ImageInfo |
| 172 | *read_info; |
| 173 | |
| 174 | MagickBooleanType |
| 175 | cmyk, |
| 176 | status; |
| 177 | |
| 178 | PointInfo |
| 179 | delta; |
| 180 | |
| 181 | RectangleInfo |
| 182 | bounding_box, |
| 183 | page; |
| 184 | |
| 185 | register char |
| 186 | *p; |
| 187 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 188 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | c; |
| 190 | |
| 191 | SegmentInfo |
| 192 | bounds; |
| 193 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 194 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | height, |
| 196 | width; |
| 197 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 198 | ssize_t |
| 199 | count; |
| 200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 202 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | if (image_info->debug != MagickFalse) |
| 204 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 205 | image_info->filename); |
| 206 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 207 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | /* |
| 209 | Open image file. |
| 210 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 211 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 213 | if (status == MagickFalse) |
| 214 | { |
| 215 | image=DestroyImageList(image); |
| 216 | return((Image *) NULL); |
| 217 | } |
| 218 | status=AcquireUniqueSymbolicLink(image_info->filename,input_filename); |
| 219 | if (status == MagickFalse) |
| 220 | { |
| 221 | ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile", |
| 222 | image_info->filename); |
| 223 | image=DestroyImageList(image); |
| 224 | return((Image *) NULL); |
| 225 | } |
| 226 | /* |
| 227 | Set the page density. |
| 228 | */ |
| 229 | delta.x=DefaultResolution; |
| 230 | delta.y=DefaultResolution; |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 231 | if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | { |
| 233 | GeometryInfo |
| 234 | geometry_info; |
| 235 | |
| 236 | MagickStatusType |
| 237 | flags; |
| 238 | |
| 239 | flags=ParseGeometry(PSDensityGeometry,&geometry_info); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 240 | image->resolution.x=geometry_info.rho; |
| 241 | image->resolution.y=geometry_info.sigma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 242 | if ((flags & SigmaValue) == 0) |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 243 | image->resolution.y=image->resolution.x; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | } |
| 245 | /* |
| 246 | Determine page geometry from the PCL media box. |
| 247 | */ |
| 248 | cmyk=image->colorspace == CMYKColorspace ? MagickTrue : MagickFalse; |
| 249 | count=0; |
| 250 | (void) ResetMagickMemory(&bounding_box,0,sizeof(bounding_box)); |
| 251 | (void) ResetMagickMemory(&bounds,0,sizeof(bounds)); |
| 252 | (void) ResetMagickMemory(&page,0,sizeof(page)); |
| 253 | (void) ResetMagickMemory(command,0,sizeof(command)); |
| 254 | p=command; |
| 255 | for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image)) |
| 256 | { |
| 257 | if (image_info->page != (char *) NULL) |
| 258 | continue; |
| 259 | /* |
| 260 | Note PCL elements. |
| 261 | */ |
| 262 | *p++=(char) c; |
| 263 | if ((c != (int) '/') && (c != '\n') && |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 264 | ((size_t) (p-command) < (MagickPathExtent-1))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | continue; |
| 266 | *p='\0'; |
| 267 | p=command; |
| 268 | /* |
| 269 | Is this a CMYK document? |
| 270 | */ |
| 271 | if (LocaleNCompare(DeviceCMYK,command,strlen(DeviceCMYK)) == 0) |
| 272 | cmyk=MagickTrue; |
| 273 | if (LocaleNCompare(CropBox,command,strlen(CropBox)) == 0) |
| 274 | { |
| 275 | /* |
| 276 | Note region defined by crop box. |
| 277 | */ |
| 278 | count=(ssize_t) sscanf(command,"CropBox [%lf %lf %lf %lf", |
| 279 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 280 | if (count != 4) |
| 281 | count=(ssize_t) sscanf(command,"CropBox[%lf %lf %lf %lf", |
| 282 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 283 | } |
| 284 | if (LocaleNCompare(MediaBox,command,strlen(MediaBox)) == 0) |
| 285 | { |
| 286 | /* |
| 287 | Note region defined by media box. |
| 288 | */ |
| 289 | count=(ssize_t) sscanf(command,"MediaBox [%lf %lf %lf %lf", |
| 290 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 291 | if (count != 4) |
| 292 | count=(ssize_t) sscanf(command,"MediaBox[%lf %lf %lf %lf", |
| 293 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 294 | } |
| 295 | if (count != 4) |
| 296 | continue; |
| 297 | /* |
| 298 | Set PCL render geometry. |
| 299 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 300 | width=(size_t) floor(bounds.x2-bounds.x1+0.5); |
| 301 | height=(size_t) floor(bounds.y2-bounds.y1+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | if (width > page.width) |
| 303 | page.width=width; |
| 304 | if (height > page.height) |
| 305 | page.height=height; |
| 306 | } |
| 307 | (void) CloseBlob(image); |
| 308 | /* |
| 309 | Render PCL with the GhostPCL delegate. |
| 310 | */ |
| 311 | if ((page.width == 0) || (page.height == 0)) |
| 312 | (void) ParseAbsoluteGeometry(PSPageGeometry,&page); |
| 313 | if (image_info->page != (char *) NULL) |
| 314 | (void) ParseAbsoluteGeometry(image_info->page,&page); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 315 | (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g",(double) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 316 | page.width,(double) page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 317 | if (image_info->monochrome != MagickFalse) |
| 318 | delegate_info=GetDelegateInfo("pcl:mono",(char *) NULL,exception); |
| 319 | else |
| 320 | if (cmyk != MagickFalse) |
| 321 | delegate_info=GetDelegateInfo("pcl:cmyk",(char *) NULL,exception); |
| 322 | else |
| 323 | delegate_info=GetDelegateInfo("pcl:color",(char *) NULL,exception); |
| 324 | if (delegate_info == (const DelegateInfo *) NULL) |
| 325 | return((Image *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | if ((page.width == 0) || (page.height == 0)) |
| 327 | (void) ParseAbsoluteGeometry(PSPageGeometry,&page); |
| 328 | if (image_info->page != (char *) NULL) |
| 329 | (void) ParseAbsoluteGeometry(image_info->page,&page); |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 330 | density=AcquireString(""); |
| 331 | options=AcquireString(""); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 332 | (void) FormatLocaleString(density,MagickPathExtent,"%gx%g",image->resolution.x, |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 333 | image->resolution.y); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 334 | page.width=(size_t) floor(page.width*image->resolution.x/delta.x+0.5); |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 335 | page.height=(size_t) floor(page.height*image->resolution.y/delta.y+0.5); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 336 | (void) FormatLocaleString(options,MagickPathExtent,"-g%.20gx%.20g ",(double) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 337 | page.width,(double) page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | image=DestroyImage(image); |
| 339 | read_info=CloneImageInfo(image_info); |
| 340 | *read_info->magick='\0'; |
| 341 | if (read_info->number_scenes != 0) |
| 342 | { |
| 343 | if (read_info->number_scenes != 1) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 344 | (void) FormatLocaleString(options,MagickPathExtent,"-dLastPage=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 345 | (double) (read_info->scene+read_info->number_scenes)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | else |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 347 | (void) FormatLocaleString(options,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 348 | "-dFirstPage=%.20g -dLastPage=%.20g",(double) read_info->scene+1, |
| 349 | (double) (read_info->scene+read_info->number_scenes)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 350 | read_info->number_scenes=0; |
| 351 | if (read_info->scenes != (char *) NULL) |
| 352 | *read_info->scenes='\0'; |
| 353 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 354 | option=GetImageOption(image_info,"authenticate"); |
anthony | 1afdc7a | 2011-10-05 11:54:28 +0000 | [diff] [blame] | 355 | if (option != (const char *) NULL) |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 356 | (void) FormatLocaleString(options+strlen(options),MagickPathExtent, |
anthony | 1afdc7a | 2011-10-05 11:54:28 +0000 | [diff] [blame] | 357 | " -sPCLPassword=%s",option); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 358 | (void) CopyMagickString(filename,read_info->filename,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 359 | (void) AcquireUniqueFilename(read_info->filename); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 360 | (void) FormatLocaleString(command,MagickPathExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | GetDelegateCommands(delegate_info), |
| 362 | read_info->antialias != MagickFalse ? 4 : 1, |
| 363 | read_info->antialias != MagickFalse ? 4 : 1,density,options, |
| 364 | read_info->filename,input_filename); |
cristy | 5181656 | 2015-04-12 13:18:38 +0000 | [diff] [blame] | 365 | options=DestroyString(options); |
| 366 | density=DestroyString(density); |
cristy | dfc19b6 | 2014-10-17 22:52:24 +0000 | [diff] [blame] | 367 | status=ExternalDelegateCommand(MagickFalse,read_info->verbose,command, |
| 368 | (char *) NULL,exception) != 0 ? MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 369 | image=ReadImage(read_info,exception); |
| 370 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 371 | (void) RelinquishUniqueFileResource(input_filename); |
| 372 | read_info=DestroyImageInfo(read_info); |
| 373 | if (image == (Image *) NULL) |
| 374 | ThrowReaderException(DelegateError,"PCLDelegateFailed"); |
| 375 | if (LocaleCompare(image->magick,"BMP") == 0) |
| 376 | { |
| 377 | Image |
| 378 | *cmyk_image; |
| 379 | |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 380 | cmyk_image=ConsolidateCMYKImages(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | if (cmyk_image != (Image *) NULL) |
| 382 | { |
| 383 | image=DestroyImageList(image); |
| 384 | image=cmyk_image; |
| 385 | } |
| 386 | } |
| 387 | do |
| 388 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 389 | (void) CopyMagickString(image->filename,filename,MagickPathExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 390 | image->page=page; |
| 391 | next_image=SyncNextImageInList(image); |
| 392 | if (next_image != (Image *) NULL) |
| 393 | image=next_image; |
| 394 | } while (next_image != (Image *) NULL); |
| 395 | return(GetFirstImageInList(image)); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 400 | % % |
| 401 | % % |
| 402 | % % |
| 403 | % R e g i s t e r P C L I m a g e % |
| 404 | % % |
| 405 | % % |
| 406 | % % |
| 407 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 408 | % |
| 409 | % RegisterPCLImage() adds attributes for the PCL image format to |
| 410 | % the list of supported formats. The attributes include the image format |
| 411 | % tag, a method to read and/or write the format, whether the format |
| 412 | % supports the saving of more than one frame to the i file or blob, |
| 413 | % whether the format supports native in-memory I/O, and a brief |
| 414 | % description of the format. |
| 415 | % |
| 416 | % The format of the RegisterPCLImage method is: |
| 417 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 418 | % size_t RegisterPCLImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 419 | % |
| 420 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 421 | ModuleExport size_t RegisterPCLImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 422 | { |
| 423 | MagickInfo |
| 424 | *entry; |
| 425 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 426 | entry=AcquireMagickInfo("PCL","PCL","Printer Control Language"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | entry->decoder=(DecodeImageHandler *) ReadPCLImage; |
| 428 | entry->encoder=(EncodeImageHandler *) WritePCLImage; |
| 429 | entry->magick=(IsImageFormatHandler *) IsPCL; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 430 | entry->flags^=CoderBlobSupportFlag; |
| 431 | entry->flags^=CoderDecoderThreadSupportFlag; |
| 432 | entry->flags|=CoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 433 | (void) RegisterMagickInfo(entry); |
| 434 | return(MagickImageCoderSignature); |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 439 | % % |
| 440 | % % |
| 441 | % % |
| 442 | % U n r e g i s t e r P C L I m a g e % |
| 443 | % % |
| 444 | % % |
| 445 | % % |
| 446 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 447 | % |
| 448 | % UnregisterPCLImage() removes format registrations made by the PCL module |
| 449 | % from the list of supported formats. |
| 450 | % |
| 451 | % The format of the UnregisterPCLImage method is: |
| 452 | % |
| 453 | % UnregisterPCLImage(void) |
| 454 | % |
| 455 | */ |
| 456 | ModuleExport void UnregisterPCLImage(void) |
| 457 | { |
| 458 | (void) UnregisterMagickInfo("PCL"); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 463 | % % |
| 464 | % % |
| 465 | % % |
| 466 | % W r i t e P C L I m a g e % |
| 467 | % % |
| 468 | % % |
| 469 | % % |
| 470 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 471 | % |
| 472 | % WritePCLImage() writes an image in the Page Control Language encoded |
| 473 | % image format. |
| 474 | % |
| 475 | % The format of the WritePCLImage method is: |
| 476 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 477 | % MagickBooleanType WritePCLImage(const ImageInfo *image_info, |
| 478 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 479 | % |
| 480 | % A description of each parameter follows. |
| 481 | % |
| 482 | % o image_info: the image info. |
| 483 | % |
| 484 | % o image: The image. |
| 485 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 486 | % o exception: return any errors or warnings in this structure. |
| 487 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | */ |
| 489 | |
| 490 | static size_t PCLDeltaCompressImage(const size_t length, |
| 491 | const unsigned char *previous_pixels,const unsigned char *pixels, |
| 492 | unsigned char *compress_pixels) |
| 493 | { |
| 494 | int |
| 495 | delta, |
| 496 | j, |
| 497 | replacement; |
| 498 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 499 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 500 | i, |
| 501 | x; |
| 502 | |
| 503 | register unsigned char |
| 504 | *q; |
| 505 | |
| 506 | q=compress_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 507 | for (x=0; x < (ssize_t) length; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | { |
| 509 | j=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 510 | for (i=0; x < (ssize_t) length; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | { |
| 512 | if (*pixels++ != *previous_pixels++) |
| 513 | { |
| 514 | i=1; |
| 515 | break; |
| 516 | } |
| 517 | j++; |
| 518 | } |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 519 | while (x < (ssize_t) length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | { |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 521 | x++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 522 | if (*pixels == *previous_pixels) |
| 523 | break; |
| 524 | i++; |
| 525 | previous_pixels++; |
| 526 | pixels++; |
| 527 | } |
| 528 | if (i == 0) |
| 529 | break; |
| 530 | replacement=j >= 31 ? 31 : j; |
| 531 | j-=replacement; |
| 532 | delta=i >= 8 ? 8 : i; |
| 533 | *q++=(unsigned char) (((delta-1) << 5) | replacement); |
| 534 | if (replacement == 31) |
| 535 | { |
| 536 | for (replacement=255; j != 0; ) |
| 537 | { |
| 538 | if (replacement > j) |
| 539 | replacement=j; |
| 540 | *q++=(unsigned char) replacement; |
| 541 | j-=replacement; |
| 542 | } |
| 543 | if (replacement == 255) |
| 544 | *q++='\0'; |
| 545 | } |
| 546 | for (pixels-=i; i != 0; ) |
| 547 | { |
| 548 | for (i-=delta; delta != 0; delta--) |
| 549 | *q++=(*pixels++); |
| 550 | if (i == 0) |
| 551 | break; |
| 552 | delta=i; |
| 553 | if (i >= 8) |
| 554 | delta=8; |
| 555 | *q++=(unsigned char) ((delta-1) << 5); |
| 556 | } |
| 557 | } |
| 558 | return((size_t) (q-compress_pixels)); |
| 559 | } |
| 560 | |
| 561 | static size_t PCLPackbitsCompressImage(const size_t length, |
| 562 | const unsigned char *pixels,unsigned char *compress_pixels) |
| 563 | { |
| 564 | int |
| 565 | count; |
| 566 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 567 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 568 | x; |
| 569 | |
| 570 | register unsigned char |
| 571 | *q; |
| 572 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 573 | ssize_t |
| 574 | j; |
| 575 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 576 | unsigned char |
| 577 | packbits[128]; |
| 578 | |
| 579 | /* |
| 580 | Compress pixels with Packbits encoding. |
| 581 | */ |
| 582 | q=compress_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 583 | for (x=(ssize_t) length; x != 0; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 584 | { |
| 585 | switch (x) |
| 586 | { |
| 587 | case 1: |
| 588 | { |
| 589 | x--; |
| 590 | *q++=0; |
| 591 | *q++=(*pixels); |
| 592 | break; |
| 593 | } |
| 594 | case 2: |
| 595 | { |
| 596 | x-=2; |
| 597 | *q++=1; |
| 598 | *q++=(*pixels); |
| 599 | *q++=pixels[1]; |
| 600 | break; |
| 601 | } |
| 602 | case 3: |
| 603 | { |
| 604 | x-=3; |
| 605 | if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2))) |
| 606 | { |
| 607 | *q++=(unsigned char) ((256-3)+1); |
| 608 | *q++=(*pixels); |
| 609 | break; |
| 610 | } |
| 611 | *q++=2; |
| 612 | *q++=(*pixels); |
| 613 | *q++=pixels[1]; |
| 614 | *q++=pixels[2]; |
| 615 | break; |
| 616 | } |
| 617 | default: |
| 618 | { |
| 619 | if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2))) |
| 620 | { |
| 621 | /* |
| 622 | Packed run. |
| 623 | */ |
| 624 | count=3; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 625 | while (((ssize_t) count < x) && (*pixels == *(pixels+count))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 626 | { |
| 627 | count++; |
| 628 | if (count >= 127) |
| 629 | break; |
| 630 | } |
| 631 | x-=count; |
| 632 | *q++=(unsigned char) ((256-count)+1); |
| 633 | *q++=(*pixels); |
| 634 | pixels+=count; |
| 635 | break; |
| 636 | } |
| 637 | /* |
| 638 | Literal run. |
| 639 | */ |
| 640 | count=0; |
| 641 | while ((*(pixels+count) != *(pixels+count+1)) || |
| 642 | (*(pixels+count+1) != *(pixels+count+2))) |
| 643 | { |
| 644 | packbits[count+1]=pixels[count]; |
| 645 | count++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 646 | if (((ssize_t) count >= (x-3)) || (count >= 127)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | x-=count; |
| 650 | *packbits=(unsigned char) (count-1); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 651 | for (j=0; j <= (ssize_t) count; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 652 | *q++=packbits[j]; |
| 653 | pixels+=count; |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | *q++=128; /* EOD marker */ |
| 659 | return((size_t) (q-compress_pixels)); |
| 660 | } |
| 661 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 662 | static MagickBooleanType WritePCLImage(const ImageInfo *image_info,Image *image, |
| 663 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 664 | { |
| 665 | char |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 666 | buffer[MagickPathExtent]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 667 | |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 668 | CompressionType |
| 669 | compression; |
| 670 | |
cristy | 688f07b | 2009-09-27 15:19:13 +0000 | [diff] [blame] | 671 | const char |
| 672 | *option; |
| 673 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 674 | MagickBooleanType |
| 675 | status; |
| 676 | |
| 677 | MagickOffsetType |
| 678 | scene; |
| 679 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 680 | register const Quantum *p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 681 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 682 | register ssize_t i, x; |
| 683 | |
| 684 | register unsigned char *q; |
| 685 | |
| 686 | size_t |
| 687 | density, |
| 688 | length, |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 689 | one, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | packets; |
| 691 | |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 692 | ssize_t |
| 693 | y; |
| 694 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 695 | unsigned char |
| 696 | bits_per_pixel, |
| 697 | *compress_pixels, |
| 698 | *pixels, |
| 699 | *previous_pixels; |
| 700 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 701 | /* |
| 702 | Open output image file. |
| 703 | */ |
| 704 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 705 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 706 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 707 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 708 | if (image->debug != MagickFalse) |
| 709 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 710 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 711 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 712 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | if (status == MagickFalse) |
| 714 | return(status); |
| 715 | density=75; |
| 716 | if (image_info->density != (char *) NULL) |
| 717 | { |
| 718 | GeometryInfo |
| 719 | geometry; |
| 720 | |
| 721 | (void) ParseGeometry(image_info->density,&geometry); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 722 | density=(size_t) geometry.rho; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | } |
| 724 | scene=0; |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 725 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 726 | do |
| 727 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | /* |
| 729 | Initialize the printer. |
| 730 | */ |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 731 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 732 | (void) WriteBlobString(image,"\033E"); /* printer reset */ |
| 733 | (void) WriteBlobString(image,"\033*r3F"); /* set presentation mode */ |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 734 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*r%.20gs%.20gT", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 735 | (double) image->columns,(double) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | (void) WriteBlobString(image,buffer); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 737 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*t%.20gR",(double) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 738 | density); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 739 | (void) WriteBlobString(image,buffer); |
| 740 | (void) WriteBlobString(image,"\033&l0E"); /* top margin 0 */ |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 741 | if (SetImageMonochrome(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 742 | { |
| 743 | /* |
cristy | fcbe33b | 2011-01-19 23:36:00 +0000 | [diff] [blame] | 744 | Monochrome image: use default printer monochrome setup. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 745 | */ |
| 746 | bits_per_pixel=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 747 | } |
| 748 | else |
| 749 | if (image->storage_class == DirectClass) |
| 750 | { |
| 751 | /* |
| 752 | DirectClass image. |
| 753 | */ |
| 754 | bits_per_pixel=24; |
| 755 | (void) WriteBlobString(image,"\033*v6W"); /* set color mode */ |
| 756 | (void) WriteBlobByte(image,0); /* RGB */ |
| 757 | (void) WriteBlobByte(image,3); /* direct by pixel */ |
| 758 | (void) WriteBlobByte(image,0); /* bits per index (ignored) */ |
| 759 | (void) WriteBlobByte(image,8); /* bits per red component */ |
| 760 | (void) WriteBlobByte(image,8); /* bits per green component */ |
| 761 | (void) WriteBlobByte(image,8); /* bits per blue component */ |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | /* |
| 766 | Colormapped image. |
| 767 | */ |
| 768 | bits_per_pixel=8; |
| 769 | (void) WriteBlobString(image,"\033*v6W"); /* set color mode... */ |
| 770 | (void) WriteBlobByte(image,0); /* RGB */ |
| 771 | (void) WriteBlobByte(image,1); /* indexed by pixel */ |
| 772 | (void) WriteBlobByte(image,bits_per_pixel); /* bits per index */ |
| 773 | (void) WriteBlobByte(image,8); /* bits per red component */ |
| 774 | (void) WriteBlobByte(image,8); /* bits per green component */ |
| 775 | (void) WriteBlobByte(image,8); /* bits per blue component */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 776 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 777 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 778 | (void) FormatLocaleString(buffer,MagickPathExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 779 | "\033*v%da%db%dc%.20gI", |
| 780 | ScaleQuantumToChar(image->colormap[i].red), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 781 | ScaleQuantumToChar(image->colormap[i].green), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 782 | ScaleQuantumToChar(image->colormap[i].blue),(double) i); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | (void) WriteBlobString(image,buffer); |
| 784 | } |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 785 | for (one=1; i < (ssize_t) (one << bits_per_pixel); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 787 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*v%.20gI", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 788 | (double) i); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 789 | (void) WriteBlobString(image,buffer); |
| 790 | } |
| 791 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 792 | option=GetImageOption(image_info,"pcl:fit-to-page"); |
anthony | 7bcfe7f | 2012-03-30 14:01:22 +0000 | [diff] [blame] | 793 | if (IfMagickTrue(IsStringTrue(option))) |
cristy | 688f07b | 2009-09-27 15:19:13 +0000 | [diff] [blame] | 794 | (void) WriteBlobString(image,"\033*r3A"); |
| 795 | else |
| 796 | (void) WriteBlobString(image,"\033*r1A"); /* start raster graphics */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | (void) WriteBlobString(image,"\033*b0Y"); /* set y offset */ |
| 798 | length=(image->columns*bits_per_pixel+7)/8; |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 799 | pixels=(unsigned char *) AcquireQuantumMemory(length+1,sizeof(*pixels)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 800 | if (pixels == (unsigned char *) NULL) |
| 801 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 802 | (void) ResetMagickMemory(pixels,0,(length+1)*sizeof(*pixels)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | compress_pixels=(unsigned char *) NULL; |
| 804 | previous_pixels=(unsigned char *) NULL; |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 805 | |
| 806 | compression=UndefinedCompression; |
| 807 | if (image_info->compression != UndefinedCompression) |
| 808 | compression=image_info->compression; |
| 809 | switch (compression) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | { |
| 811 | case NoCompression: |
| 812 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 813 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b0M"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 814 | (void) WriteBlobString(image,buffer); |
| 815 | break; |
| 816 | } |
| 817 | case RLECompression: |
| 818 | { |
| 819 | compress_pixels=(unsigned char *) AcquireQuantumMemory(length+256, |
| 820 | sizeof(*compress_pixels)); |
| 821 | if (compress_pixels == (unsigned char *) NULL) |
| 822 | { |
| 823 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 824 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 825 | } |
cristy | 84cb9f4 | 2010-11-02 16:06:30 +0000 | [diff] [blame] | 826 | (void) ResetMagickMemory(compress_pixels,0,(length+256)* |
| 827 | sizeof(*compress_pixels)); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 828 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b2M"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 829 | (void) WriteBlobString(image,buffer); |
| 830 | break; |
| 831 | } |
| 832 | default: |
| 833 | { |
cristy | 7086552 | 2011-03-30 14:36:48 +0000 | [diff] [blame] | 834 | compress_pixels=(unsigned char *) AcquireQuantumMemory(3*length+256, |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 835 | sizeof(*compress_pixels)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | if (compress_pixels == (unsigned char *) NULL) |
| 837 | { |
| 838 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 839 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 840 | } |
cristy | 7086552 | 2011-03-30 14:36:48 +0000 | [diff] [blame] | 841 | (void) ResetMagickMemory(compress_pixels,0,(3*length+256)* |
cristy | 84cb9f4 | 2010-11-02 16:06:30 +0000 | [diff] [blame] | 842 | sizeof(*compress_pixels)); |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 843 | previous_pixels=(unsigned char *) AcquireQuantumMemory(length+1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 844 | sizeof(*previous_pixels)); |
| 845 | if (previous_pixels == (unsigned char *) NULL) |
| 846 | { |
| 847 | compress_pixels=(unsigned char *) RelinquishMagickMemory( |
| 848 | compress_pixels); |
| 849 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 850 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 851 | } |
cristy | 91e5262 | 2010-11-03 01:05:41 +0000 | [diff] [blame] | 852 | (void) ResetMagickMemory(previous_pixels,0,(length+1)* |
cristy | 84cb9f4 | 2010-11-02 16:06:30 +0000 | [diff] [blame] | 853 | sizeof(*previous_pixels)); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 854 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b3M"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 855 | (void) WriteBlobString(image,buffer); |
| 856 | break; |
| 857 | } |
| 858 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 859 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 860 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 861 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 862 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 863 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | q=pixels; |
| 865 | switch (bits_per_pixel) |
| 866 | { |
| 867 | case 1: |
| 868 | { |
| 869 | register unsigned char |
| 870 | bit, |
| 871 | byte; |
| 872 | |
| 873 | /* |
| 874 | Monochrome image. |
| 875 | */ |
| 876 | bit=0; |
| 877 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 878 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 879 | { |
| 880 | byte<<=1; |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 881 | if (GetPixelLuma(image,p) < (QuantumRange/2.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 882 | byte|=0x01; |
| 883 | bit++; |
| 884 | if (bit == 8) |
| 885 | { |
| 886 | *q++=byte; |
| 887 | bit=0; |
| 888 | byte=0; |
| 889 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 890 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | } |
| 892 | if (bit != 0) |
| 893 | *q++=byte << (8-bit); |
| 894 | break; |
| 895 | } |
| 896 | case 8: |
| 897 | { |
| 898 | /* |
| 899 | Colormapped image. |
| 900 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 901 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 902 | { |
| 903 | *q++=(unsigned char) GetPixelIndex(image,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 904 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 905 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | break; |
| 907 | } |
| 908 | case 24: |
| 909 | case 32: |
| 910 | { |
| 911 | /* |
| 912 | Truecolor image. |
| 913 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 914 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 916 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 917 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 918 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 919 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 920 | } |
| 921 | break; |
| 922 | } |
| 923 | } |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 924 | switch (compression) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | { |
| 926 | case NoCompression: |
| 927 | { |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 928 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b%.20gW", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 929 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | (void) WriteBlobString(image,buffer); |
| 931 | (void) WriteBlob(image,length,pixels); |
| 932 | break; |
| 933 | } |
| 934 | case RLECompression: |
| 935 | { |
cristy | aff6d80 | 2011-04-26 01:46:31 +0000 | [diff] [blame] | 936 | packets=PCLPackbitsCompressImage(length,pixels,compress_pixels); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 937 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b%.20gW", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 938 | (double) packets); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | (void) WriteBlobString(image,buffer); |
| 940 | (void) WriteBlob(image,packets,compress_pixels); |
| 941 | break; |
| 942 | } |
| 943 | default: |
| 944 | { |
| 945 | if (y == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 946 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 947 | previous_pixels[i]=(~pixels[i]); |
| 948 | packets=PCLDeltaCompressImage(length,previous_pixels,pixels, |
| 949 | compress_pixels); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 950 | (void) FormatLocaleString(buffer,MagickPathExtent,"\033*b%.20gW", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 951 | (double) packets); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 952 | (void) WriteBlobString(image,buffer); |
| 953 | (void) WriteBlob(image,packets,compress_pixels); |
| 954 | (void) CopyMagickMemory(previous_pixels,pixels,length* |
| 955 | sizeof(*pixels)); |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | } |
cristy | 8bc68ee | 2009-09-24 18:39:29 +0000 | [diff] [blame] | 960 | (void) WriteBlobString(image,"\033*rB"); /* end graphics */ |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 961 | switch (compression) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | { |
| 963 | case NoCompression: |
| 964 | break; |
| 965 | case RLECompression: |
| 966 | { |
| 967 | compress_pixels=(unsigned char *) RelinquishMagickMemory( |
| 968 | compress_pixels); |
| 969 | break; |
| 970 | } |
| 971 | default: |
| 972 | { |
| 973 | previous_pixels=(unsigned char *) RelinquishMagickMemory( |
| 974 | previous_pixels); |
| 975 | compress_pixels=(unsigned char *) RelinquishMagickMemory( |
| 976 | compress_pixels); |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 981 | if (GetNextImageInList(image) == (Image *) NULL) |
| 982 | break; |
| 983 | image=SyncNextImageInList(image); |
| 984 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 985 | GetImageListLength(image)); |
| 986 | if (status == MagickFalse) |
| 987 | break; |
| 988 | } while (image_info->adjoin != MagickFalse); |
| 989 | (void) WriteBlobString(image,"\033E"); |
| 990 | (void) CloseBlob(image); |
| 991 | return(MagickTrue); |
| 992 | } |