| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP SSSSS % |
| 7 | % P P SS % |
| 8 | % PPPP SSS % |
| 9 | % P SS % |
| 10 | % P SSSSS % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Postscript Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
| cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 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/colorspace.h" |
| cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 50 | #include "MagickCore/colorspace-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 51 | #include "MagickCore/constitute.h" |
| 52 | #include "MagickCore/delegate.h" |
| 53 | #include "MagickCore/delegate-private.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/profile.h" |
| 67 | #include "MagickCore/resource_.h" |
| 68 | #include "MagickCore/pixel-accessor.h" |
| 69 | #include "MagickCore/property.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 | WritePSImage(const ImageInfo *,Image *,ExceptionInfo *); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % % |
| 87 | % % |
| 88 | % % |
| 89 | % I n v o k e P o s t s r i p t D e l e g a t e % |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | % |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 95 | % InvokePostscriptDelegate() executes the Postscript interpreter with the |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 96 | % specified command. |
| 97 | % |
| 98 | % The format of the InvokePostscriptDelegate method is: |
| 99 | % |
| 100 | % MagickBooleanType InvokePostscriptDelegate( |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 101 | % const MagickBooleanType verbose,const char *command, |
| 102 | % ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 103 | % |
| 104 | % A description of each parameter follows: |
| 105 | % |
| 106 | % o verbose: A value other than zero displays the command prior to |
| 107 | % executing it. |
| 108 | % |
| 109 | % o command: the address of a character string containing the command to |
| 110 | % execute. |
| 111 | % |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 112 | % o exception: return any errors or warnings in this structure. |
| 113 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 114 | */ |
| 115 | static MagickBooleanType InvokePostscriptDelegate( |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 116 | const MagickBooleanType verbose,const char *command,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 117 | { |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 118 | int |
| 119 | status; |
| 120 | |
| cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 121 | #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 122 | char |
| 123 | **argv; |
| 124 | |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 125 | const GhostInfo |
| 126 | *ghost_info; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 127 | |
| 128 | gs_main_instance |
| 129 | *interpreter; |
| 130 | |
| 131 | int |
| 132 | argc, |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 133 | code; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 134 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 135 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | i; |
| 137 | |
| cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 138 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 139 | ghost_info=NTGhostscriptDLLVectors(); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 140 | #else |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 141 | GhostInfo |
| 142 | ghost_info_struct; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 144 | ghost_info=(&ghost_info_struct); |
| 145 | (void) ResetMagickMemory(&ghost_info,0,sizeof(ghost_info)); |
| 146 | ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | gsapi_new_instance; |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 148 | ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 149 | gsapi_init_with_args; |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 150 | ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int, |
| 151 | int *)) gsapi_run_string; |
| 152 | ghost_info_struct.delete_instance=(void (*)(gs_main_instance *)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 153 | gsapi_delete_instance; |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 154 | ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | #endif |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 156 | if (ghost_info == (GhostInfo *) NULL) |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 157 | { |
| cristy | 6de4bc2 | 2010-01-12 17:10:35 +0000 | [diff] [blame] | 158 | status=SystemCommand(MagickFalse,verbose,command,exception); |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 159 | return(status == 0 ? MagickTrue : MagickFalse); |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 160 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | if (verbose != MagickFalse) |
| 162 | { |
| 163 | (void) fputs("[ghostscript library]",stdout); |
| 164 | (void) fputs(strchr(command,' '),stdout); |
| 165 | } |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 166 | status=(ghost_info->new_instance)(&interpreter,(void *) NULL); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 167 | if (status < 0) |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 168 | { |
| cristy | 6de4bc2 | 2010-01-12 17:10:35 +0000 | [diff] [blame] | 169 | status=SystemCommand(MagickFalse,verbose,command,exception); |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 170 | return(status == 0 ? MagickTrue : MagickFalse); |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 171 | } |
| cristy | a73c0f8 | 2010-12-20 15:57:05 +0000 | [diff] [blame] | 172 | code=0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | argv=StringToArgv(command,&argc); |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 174 | status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | if (status == 0) |
| cristy | defb3f0 | 2009-09-10 02:18:35 +0000 | [diff] [blame] | 176 | status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n", |
| 177 | 0,&code); |
| 178 | (ghost_info->exit)(interpreter); |
| 179 | (ghost_info->delete_instance)(interpreter); |
| cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 180 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | NTGhostscriptUnLoadDLL(); |
| 182 | #endif |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 183 | for (i=0; i < (ssize_t) argc; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | argv[i]=DestroyString(argv[i]); |
| 185 | argv=(char **) RelinquishMagickMemory(argv); |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 186 | if ((status != 0) && (status != -101)) |
| 187 | { |
| 188 | char |
| 189 | *message; |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 190 | |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 191 | message=GetExceptionMessage(errno); |
| 192 | (void) ThrowMagickException(exception,GetMagickModule(),DelegateError, |
| 193 | "`%s': %s",command,message); |
| 194 | message=DestroyString(message); |
| 195 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 196 | "Ghostscript returns status %d, exit code %d",status,code); |
| 197 | return(MagickFalse); |
| 198 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | return(MagickTrue); |
| 200 | #else |
| cristy | 6de4bc2 | 2010-01-12 17:10:35 +0000 | [diff] [blame] | 201 | status=SystemCommand(MagickFalse,verbose,command,exception); |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 202 | return(status == 0 ? MagickTrue : MagickFalse); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | #endif |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 208 | % % |
| 209 | % % |
| 210 | % % |
| 211 | % I s P S % |
| 212 | % % |
| 213 | % % |
| 214 | % % |
| 215 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 216 | % |
| 217 | % IsPS() returns MagickTrue if the image format type, identified by the |
| 218 | % magick string, is PS. |
| 219 | % |
| 220 | % The format of the IsPS method is: |
| 221 | % |
| 222 | % MagickBooleanType IsPS(const unsigned char *magick,const size_t length) |
| 223 | % |
| 224 | % A description of each parameter follows: |
| 225 | % |
| 226 | % o magick: compare image format pattern against these bytes. |
| 227 | % |
| 228 | % o length: Specifies the length of the magick string. |
| 229 | % |
| 230 | */ |
| 231 | static MagickBooleanType IsPS(const unsigned char *magick,const size_t length) |
| 232 | { |
| 233 | if (length < 4) |
| 234 | return(MagickFalse); |
| 235 | if (memcmp(magick,"%!",2) == 0) |
| 236 | return(MagickTrue); |
| 237 | if (memcmp(magick,"\004%!",3) == 0) |
| 238 | return(MagickTrue); |
| 239 | return(MagickFalse); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 244 | % % |
| 245 | % % |
| 246 | % % |
| 247 | % R e a d P S I m a g e % |
| 248 | % % |
| 249 | % % |
| 250 | % % |
| 251 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 252 | % |
| 253 | % ReadPSImage() reads a Postscript image file and returns it. It allocates |
| 254 | % the memory necessary for the new Image structure and returns a pointer |
| 255 | % to the new image. |
| 256 | % |
| 257 | % The format of the ReadPSImage method is: |
| 258 | % |
| 259 | % Image *ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 260 | % |
| 261 | % A description of each parameter follows: |
| 262 | % |
| 263 | % o image_info: the image info. |
| 264 | % |
| 265 | % o exception: return any errors or warnings in this structure. |
| 266 | % |
| 267 | */ |
| 268 | |
| 269 | static MagickBooleanType IsPostscriptRendered(const char *path) |
| 270 | { |
| 271 | MagickBooleanType |
| 272 | status; |
| 273 | |
| 274 | struct stat |
| 275 | attributes; |
| 276 | |
| 277 | if ((path == (const char *) NULL) || (*path == '\0')) |
| 278 | return(MagickFalse); |
| 279 | status=GetPathAttributes(path,&attributes); |
| 280 | if ((status != MagickFalse) && S_ISREG(attributes.st_mode) && |
| 281 | (attributes.st_size > 0)) |
| 282 | return(MagickTrue); |
| 283 | return(MagickFalse); |
| 284 | } |
| 285 | |
| 286 | static inline int ProfileInteger(Image *image,short int *hex_digits) |
| 287 | { |
| 288 | int |
| 289 | c, |
| 290 | l, |
| 291 | value; |
| 292 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 293 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 294 | i; |
| 295 | |
| 296 | l=0; |
| 297 | value=0; |
| 298 | for (i=0; i < 2; ) |
| 299 | { |
| 300 | c=ReadBlobByte(image); |
| 301 | if ((c == EOF) || ((c == '%') && (l == '%'))) |
| 302 | { |
| 303 | value=(-1); |
| 304 | break; |
| 305 | } |
| 306 | l=c; |
| 307 | c&=0xff; |
| 308 | if (isxdigit(c) == MagickFalse) |
| 309 | continue; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 310 | value=(int) ((size_t) value << 4)+hex_digits[c]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 311 | i++; |
| 312 | } |
| 313 | return(value); |
| 314 | } |
| 315 | |
| 316 | static Image *ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 317 | { |
| 318 | #define BoundingBox "BoundingBox:" |
| 319 | #define BeginDocument "BeginDocument:" |
| 320 | #define BeginXMPPacket "<?xpacket begin=" |
| 321 | #define EndXMPPacket "<?xpacket end=" |
| 322 | #define ICCProfile "BeginICCProfile:" |
| 323 | #define CMYKCustomColor "CMYKCustomColor:" |
| cristy | 01ca892 | 2010-03-17 12:20:37 +0000 | [diff] [blame] | 324 | #define CMYKProcessColor "CMYKProcessColor:" |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 325 | #define DocumentMedia "DocumentMedia:" |
| 326 | #define DocumentCustomColors "DocumentCustomColors:" |
| 327 | #define DocumentProcessColors "DocumentProcessColors:" |
| 328 | #define EndDocument "EndDocument:" |
| 329 | #define HiResBoundingBox "HiResBoundingBox:" |
| 330 | #define ImageData "ImageData:" |
| 331 | #define PageBoundingBox "PageBoundingBox:" |
| 332 | #define LanguageLevel "LanguageLevel:" |
| 333 | #define PageMedia "PageMedia:" |
| 334 | #define Pages "Pages:" |
| 335 | #define PhotoshopProfile "BeginPhotoshop:" |
| 336 | #define PostscriptLevel "!PS-" |
| 337 | #define RenderPostscriptText " Rendering Postscript... " |
| 338 | #define SpotColor "+ " |
| 339 | |
| 340 | char |
| 341 | command[MaxTextExtent], |
| 342 | density[MaxTextExtent], |
| 343 | filename[MaxTextExtent], |
| 344 | geometry[MaxTextExtent], |
| 345 | input_filename[MaxTextExtent], |
| 346 | options[MaxTextExtent], |
| cristy | c39e3d6 | 2010-10-14 16:52:01 +0000 | [diff] [blame] | 347 | postscript_filename[MaxTextExtent]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 348 | |
| 349 | const char |
| 350 | *option; |
| 351 | |
| 352 | const DelegateInfo |
| 353 | *delegate_info; |
| 354 | |
| 355 | GeometryInfo |
| 356 | geometry_info; |
| 357 | |
| 358 | Image |
| 359 | *image, |
| 360 | *next, |
| 361 | *postscript_image; |
| 362 | |
| 363 | ImageInfo |
| 364 | *read_info; |
| 365 | |
| 366 | int |
| 367 | c, |
| 368 | file; |
| 369 | |
| 370 | MagickBooleanType |
| 371 | cmyk, |
| 372 | skip, |
| 373 | status; |
| 374 | |
| 375 | MagickStatusType |
| 376 | flags; |
| 377 | |
| 378 | PointInfo |
| 379 | delta; |
| 380 | |
| 381 | RectangleInfo |
| 382 | page; |
| 383 | |
| 384 | register char |
| 385 | *p; |
| 386 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 387 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 388 | i; |
| 389 | |
| 390 | SegmentInfo |
| 391 | bounds, |
| 392 | hires_bounds; |
| 393 | |
| 394 | short int |
| 395 | hex_digits[256]; |
| 396 | |
| 397 | size_t |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 398 | length, |
| 399 | priority; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | |
| 401 | ssize_t |
| 402 | count; |
| 403 | |
| 404 | StringInfo |
| 405 | *profile; |
| 406 | |
| cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 407 | unsigned long |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 408 | columns, |
| 409 | extent, |
| 410 | language_level, |
| 411 | pages, |
| 412 | rows, |
| 413 | scene, |
| 414 | spotcolor; |
| 415 | |
| 416 | /* |
| 417 | Open image file. |
| 418 | */ |
| 419 | assert(image_info != (const ImageInfo *) NULL); |
| 420 | assert(image_info->signature == MagickSignature); |
| 421 | if (image_info->debug != MagickFalse) |
| 422 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 423 | image_info->filename); |
| 424 | assert(exception != (ExceptionInfo *) NULL); |
| 425 | assert(exception->signature == MagickSignature); |
| cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 426 | image=AcquireImage(image_info,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 428 | if (status == MagickFalse) |
| 429 | { |
| 430 | image=DestroyImageList(image); |
| 431 | return((Image *) NULL); |
| 432 | } |
| 433 | status=AcquireUniqueSymbolicLink(image_info->filename,input_filename); |
| 434 | if (status == MagickFalse) |
| 435 | { |
| 436 | ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile", |
| 437 | image_info->filename); |
| 438 | image=DestroyImageList(image); |
| 439 | return((Image *) NULL); |
| 440 | } |
| 441 | /* |
| 442 | Initialize hex values. |
| 443 | */ |
| 444 | (void) ResetMagickMemory(hex_digits,0,sizeof(hex_digits)); |
| 445 | hex_digits[(int) '0']=0; |
| 446 | hex_digits[(int) '1']=1; |
| 447 | hex_digits[(int) '2']=2; |
| 448 | hex_digits[(int) '3']=3; |
| 449 | hex_digits[(int) '4']=4; |
| 450 | hex_digits[(int) '5']=5; |
| 451 | hex_digits[(int) '6']=6; |
| 452 | hex_digits[(int) '7']=7; |
| 453 | hex_digits[(int) '8']=8; |
| 454 | hex_digits[(int) '9']=9; |
| 455 | hex_digits[(int) 'a']=10; |
| 456 | hex_digits[(int) 'b']=11; |
| 457 | hex_digits[(int) 'c']=12; |
| 458 | hex_digits[(int) 'd']=13; |
| 459 | hex_digits[(int) 'e']=14; |
| 460 | hex_digits[(int) 'f']=15; |
| 461 | hex_digits[(int) 'A']=10; |
| 462 | hex_digits[(int) 'B']=11; |
| 463 | hex_digits[(int) 'C']=12; |
| 464 | hex_digits[(int) 'D']=13; |
| 465 | hex_digits[(int) 'E']=14; |
| 466 | hex_digits[(int) 'F']=15; |
| 467 | /* |
| 468 | Set the page density. |
| 469 | */ |
| 470 | delta.x=DefaultResolution; |
| 471 | delta.y=DefaultResolution; |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 472 | if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | { |
| 474 | flags=ParseGeometry(PSDensityGeometry,&geometry_info); |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 475 | image->resolution.x=geometry_info.rho; |
| 476 | image->resolution.y=geometry_info.sigma; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 477 | if ((flags & SigmaValue) == 0) |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 478 | image->resolution.y=image->resolution.x; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 479 | } |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 480 | if (image_info->density != (char *) NULL) |
| 481 | { |
| 482 | flags=ParseGeometry(image_info->density,&geometry_info); |
| 483 | image->resolution.x=geometry_info.rho; |
| 484 | image->resolution.y=geometry_info.sigma; |
| 485 | if ((flags & SigmaValue) == 0) |
| 486 | image->resolution.y=image->resolution.x; |
| 487 | } |
| 488 | (void) ParseAbsoluteGeometry(PSPageGeometry,&page); |
| 489 | if (image_info->page != (char *) NULL) |
| 490 | (void) ParseAbsoluteGeometry(image_info->page,&page); |
| 491 | page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)- |
| 492 | 0.5); |
| 493 | page.height=(size_t) ceil((double) (page.height*image->resolution.y/delta.y)- |
| 494 | 0.5); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | /* |
| 496 | Determine page geometry from the Postscript bounding box. |
| 497 | */ |
| 498 | (void) ResetMagickMemory(&bounds,0,sizeof(bounds)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | (void) ResetMagickMemory(command,0,sizeof(command)); |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 500 | cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse; |
| 501 | (void) ResetMagickMemory(&hires_bounds,0,sizeof(hires_bounds)); |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 502 | priority=0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 503 | rows=0; |
| 504 | extent=0; |
| 505 | spotcolor=0; |
| 506 | language_level=1; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 507 | pages=(~0UL); |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 508 | skip=MagickFalse; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | p=command; |
| 510 | for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image)) |
| 511 | { |
| 512 | /* |
| 513 | Note document structuring comments. |
| 514 | */ |
| 515 | *p++=(char) c; |
| 516 | if ((strchr("\n\r%",c) == (char *) NULL) && |
| 517 | ((size_t) (p-command) < (MaxTextExtent-1))) |
| 518 | continue; |
| 519 | *p='\0'; |
| 520 | p=command; |
| 521 | /* |
| 522 | Skip %%BeginDocument thru %%EndDocument. |
| 523 | */ |
| 524 | if (LocaleNCompare(BeginDocument,command,strlen(BeginDocument)) == 0) |
| 525 | skip=MagickTrue; |
| 526 | if (LocaleNCompare(EndDocument,command,strlen(EndDocument)) == 0) |
| 527 | skip=MagickFalse; |
| 528 | if (skip != MagickFalse) |
| 529 | continue; |
| 530 | if (LocaleNCompare(PostscriptLevel,command,strlen(PostscriptLevel)) == 0) |
| 531 | { |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 532 | (void) SetImageProperty(image,"ps:Level",command+4,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | if (GlobExpression(command,"*EPSF-*",MagickTrue) != MagickFalse) |
| 534 | pages=1; |
| 535 | } |
| 536 | if (LocaleNCompare(LanguageLevel,command,strlen(LanguageLevel)) == 0) |
| 537 | (void) sscanf(command,LanguageLevel " %lu",&language_level); |
| 538 | if (LocaleNCompare(Pages,command,strlen(Pages)) == 0) |
| 539 | (void) sscanf(command,Pages " %lu",&pages); |
| cristy | 97841ba | 2010-09-02 15:48:08 +0000 | [diff] [blame] | 540 | if (LocaleNCompare(ImageData,command,strlen(ImageData)) == 0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | (void) sscanf(command,ImageData " %lu %lu",&columns,&rows); |
| cristy | 97841ba | 2010-09-02 15:48:08 +0000 | [diff] [blame] | 542 | if (LocaleNCompare(ICCProfile,command,strlen(ICCProfile)) == 0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | { |
| 544 | unsigned char |
| 545 | *datum; |
| 546 | |
| 547 | /* |
| 548 | Read ICC profile. |
| 549 | */ |
| 550 | profile=AcquireStringInfo(65536); |
| 551 | for (i=0; (c=ProfileInteger(image,hex_digits)) != EOF; i++) |
| 552 | { |
| 553 | SetStringInfoLength(profile,(size_t) i+1); |
| 554 | datum=GetStringInfoDatum(profile); |
| 555 | datum[i]=(unsigned char) c; |
| 556 | } |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 557 | (void) SetImageProfile(image,"icc",profile,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | profile=DestroyStringInfo(profile); |
| 559 | continue; |
| 560 | } |
| 561 | if (LocaleNCompare(PhotoshopProfile,command,strlen(PhotoshopProfile)) == 0) |
| 562 | { |
| 563 | unsigned char |
| 564 | *p; |
| 565 | |
| 566 | /* |
| 567 | Read Photoshop profile. |
| 568 | */ |
| 569 | count=(ssize_t) sscanf(command,PhotoshopProfile " %lu",&extent); |
| 570 | if (count != 1) |
| 571 | continue; |
| 572 | length=extent; |
| cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 573 | profile=BlobToStringInfo((const void *) NULL,length); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 574 | p=GetStringInfoDatum(profile); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 575 | for (i=0; i < (ssize_t) length; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 576 | *p++=(unsigned char) ProfileInteger(image,hex_digits); |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 577 | (void) SetImageProfile(image,"8bim",profile,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | profile=DestroyStringInfo(profile); |
| 579 | continue; |
| 580 | } |
| 581 | if (LocaleNCompare(BeginXMPPacket,command,strlen(BeginXMPPacket)) == 0) |
| 582 | { |
| 583 | register size_t |
| 584 | i; |
| 585 | |
| 586 | /* |
| 587 | Read XMP profile. |
| 588 | */ |
| 589 | p=command; |
| 590 | profile=StringToStringInfo(command); |
| 591 | for (i=GetStringInfoLength(profile)-1; c != EOF; i++) |
| 592 | { |
| 593 | SetStringInfoLength(profile,i+1); |
| 594 | c=ReadBlobByte(image); |
| 595 | GetStringInfoDatum(profile)[i]=(unsigned char) c; |
| 596 | *p++=(char) c; |
| 597 | if ((strchr("\n\r%",c) == (char *) NULL) && |
| 598 | ((size_t) (p-command) < (MaxTextExtent-1))) |
| 599 | continue; |
| 600 | *p='\0'; |
| 601 | p=command; |
| 602 | if (LocaleNCompare(EndXMPPacket,command,strlen(EndXMPPacket)) == 0) |
| 603 | break; |
| 604 | } |
| 605 | SetStringInfoLength(profile,i); |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 606 | (void) SetImageProfile(image,"xmp",profile,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | profile=DestroyStringInfo(profile); |
| 608 | continue; |
| 609 | } |
| 610 | /* |
| 611 | Is this a CMYK document? |
| 612 | */ |
| 613 | length=strlen(DocumentProcessColors); |
| 614 | if (LocaleNCompare(DocumentProcessColors,command,length) == 0) |
| 615 | { |
| 616 | if ((GlobExpression(command,"*Cyan*",MagickTrue) != MagickFalse) || |
| 617 | (GlobExpression(command,"*Magenta*",MagickTrue) != MagickFalse) || |
| 618 | (GlobExpression(command,"*Yellow*",MagickTrue) != MagickFalse)) |
| 619 | cmyk=MagickTrue; |
| 620 | } |
| 621 | if (LocaleNCompare(CMYKCustomColor,command,strlen(CMYKCustomColor)) == 0) |
| 622 | cmyk=MagickTrue; |
| cristy | 01ca892 | 2010-03-17 12:20:37 +0000 | [diff] [blame] | 623 | if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0) |
| 624 | cmyk=MagickTrue; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 625 | length=strlen(DocumentCustomColors); |
| 626 | if ((LocaleNCompare(DocumentCustomColors,command,length) == 0) || |
| 627 | (LocaleNCompare(CMYKCustomColor,command,strlen(CMYKCustomColor)) == 0) || |
| 628 | (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0)) |
| 629 | { |
| 630 | char |
| 631 | property[MaxTextExtent], |
| 632 | *value; |
| 633 | |
| 634 | register char |
| 635 | *p; |
| 636 | |
| 637 | /* |
| 638 | Note spot names. |
| 639 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 640 | (void) FormatLocaleString(property,MaxTextExtent,"ps:SpotColor-%.20g", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 641 | (double) (spotcolor++)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 642 | for (p=command; *p != '\0'; p++) |
| 643 | if (isspace((int) (unsigned char) *p) != 0) |
| 644 | break; |
| 645 | value=AcquireString(p); |
| 646 | (void) SubstituteString(&value,"(",""); |
| 647 | (void) SubstituteString(&value,")",""); |
| 648 | (void) StripString(value); |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 649 | (void) SetImageProperty(image,property,value,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 650 | value=DestroyString(value); |
| 651 | continue; |
| 652 | } |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 653 | if (image_info->page != (char *) NULL) |
| 654 | continue; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | /* |
| 656 | Note region defined by bounding box. |
| 657 | */ |
| 658 | count=0; |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 659 | i=0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 660 | if (LocaleNCompare(BoundingBox,command,strlen(BoundingBox)) == 0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 661 | { |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 662 | count=(ssize_t) sscanf(command,BoundingBox " %lf %lf %lf %lf", |
| 663 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 664 | i=2; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 665 | } |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 666 | if (LocaleNCompare(DocumentMedia,command,strlen(DocumentMedia)) == 0) |
| 667 | { |
| 668 | count=(ssize_t) sscanf(command,DocumentMedia " %lf %lf %lf %lf", |
| 669 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 670 | i=1; |
| 671 | } |
| 672 | if (LocaleNCompare(HiResBoundingBox,command,strlen(HiResBoundingBox)) == 0) |
| 673 | { |
| 674 | count=(ssize_t) sscanf(command,HiResBoundingBox " %lf %lf %lf %lf", |
| 675 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 676 | i=3; |
| 677 | } |
| 678 | if (LocaleNCompare(PageBoundingBox,command,strlen(PageBoundingBox)) == 0) |
| 679 | { |
| 680 | count=(ssize_t) sscanf(command,PageBoundingBox " %lf %lf %lf %lf", |
| 681 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 682 | i=1; |
| 683 | } |
| 684 | if (LocaleNCompare(PageMedia,command,strlen(PageMedia)) == 0) |
| 685 | { |
| 686 | count=(ssize_t) sscanf(command,PageMedia " %lf %lf %lf %lf", |
| 687 | &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2); |
| 688 | i=1; |
| 689 | } |
| cristy | 3fac9ec | 2011-11-17 18:04:39 +0000 | [diff] [blame] | 690 | if ((count != 4) || (i < (ssize_t) priority)) |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 691 | continue; |
| cristy | ad29a8c | 2012-01-25 23:06:43 +0000 | [diff] [blame] | 692 | if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) || |
| 693 | (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1))) |
| 694 | continue; |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 695 | hires_bounds=bounds; |
| 696 | priority=i; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 697 | } |
| cristy | 7b0fcf1 | 2011-11-24 00:24:07 +0000 | [diff] [blame] | 698 | if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) && |
| cristy | 614bc08 | 2011-11-24 00:49:08 +0000 | [diff] [blame] | 699 | (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon)) |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 700 | { |
| 701 | /* |
| 702 | Set Postscript render geometry. |
| 703 | */ |
| 704 | (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g%+.15g%+.15g", |
| 705 | hires_bounds.x2-hires_bounds.x1,hires_bounds.y2-hires_bounds.y1, |
| 706 | hires_bounds.x1,hires_bounds.y1); |
| 707 | (void) SetImageProperty(image,"ps:HiResBoundingBox",geometry,exception); |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 708 | page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)* |
| 709 | image->resolution.x/delta.x)-0.5); |
| 710 | page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)* |
| 711 | image->resolution.y/delta.y)-0.5); |
| cristy | 4b1c78f | 2011-11-11 15:09:30 +0000 | [diff] [blame] | 712 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | (void) CloseBlob(image); |
| cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 714 | if (IsRGBColorspace(image_info->colorspace) != MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 715 | cmyk=MagickFalse; |
| 716 | /* |
| 717 | Create Ghostscript control file. |
| 718 | */ |
| 719 | file=AcquireUniqueFileResource(postscript_filename); |
| 720 | if (file == -1) |
| 721 | { |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 722 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | image_info->filename); |
| 724 | image=DestroyImageList(image); |
| 725 | return((Image *) NULL); |
| 726 | } |
| 727 | (void) CopyMagickString(command,"/setpagedevice {pop} bind 1 index where {" |
| 728 | "dup wcheck {3 1 roll put} {pop def} ifelse} {def} ifelse\n" |
| 729 | "<</UseCIEColor true>>setpagedevice\n",MaxTextExtent); |
| 730 | count=write(file,command,(unsigned int) strlen(command)); |
| cristy | c39e3d6 | 2010-10-14 16:52:01 +0000 | [diff] [blame] | 731 | if (image_info->page == (char *) NULL) |
| 732 | { |
| 733 | char |
| 734 | translate_geometry[MaxTextExtent]; |
| 735 | |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 736 | (void) FormatLocaleString(translate_geometry,MaxTextExtent, |
| cristy | c39e3d6 | 2010-10-14 16:52:01 +0000 | [diff] [blame] | 737 | "%g %g translate\n",-bounds.x1,-bounds.y1); |
| 738 | count=write(file,translate_geometry,(unsigned int) |
| 739 | strlen(translate_geometry)); |
| 740 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | file=close(file)-1; |
| 742 | /* |
| 743 | Render Postscript with the Ghostscript delegate. |
| 744 | */ |
| 745 | if (image_info->monochrome != MagickFalse) |
| 746 | delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception); |
| 747 | else |
| 748 | if (cmyk != MagickFalse) |
| 749 | delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception); |
| 750 | else |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 751 | delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | if (delegate_info == (const DelegateInfo *) NULL) |
| 753 | { |
| 754 | (void) RelinquishUniqueFileResource(postscript_filename); |
| 755 | image=DestroyImageList(image); |
| 756 | return((Image *) NULL); |
| 757 | } |
| 758 | *options='\0'; |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 759 | (void) FormatLocaleString(density,MaxTextExtent,"%gx%g", |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 760 | image->resolution.x,image->resolution.y); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 761 | (void) FormatLocaleString(options,MaxTextExtent,"-g%.20gx%.20g ",(double) |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 762 | page.width,(double) page.height); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | read_info=CloneImageInfo(image_info); |
| 764 | *read_info->magick='\0'; |
| 765 | if (read_info->number_scenes != 0) |
| 766 | { |
| 767 | char |
| 768 | pages[MaxTextExtent]; |
| 769 | |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 770 | (void) FormatLocaleString(pages,MaxTextExtent,"-dFirstPage=%.20g " |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 771 | "-dLastPage=%.20g",(double) read_info->scene+1,(double) |
| cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 772 | (read_info->scene+read_info->number_scenes)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 773 | (void) ConcatenateMagickString(options,pages,MaxTextExtent); |
| 774 | read_info->number_scenes=0; |
| 775 | if (read_info->scenes != (char *) NULL) |
| 776 | *read_info->scenes='\0'; |
| 777 | } |
| 778 | option=GetImageOption(image_info,"ps:use-cropbox"); |
| 779 | if ((option != (const char *) NULL) && (IsMagickTrue(option) != MagickFalse)) |
| 780 | (void) ConcatenateMagickString(options,"-dEPSCrop ",MaxTextExtent); |
| 781 | (void) CopyMagickString(filename,read_info->filename,MaxTextExtent); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 782 | (void) AcquireUniqueFilename(filename); |
| 783 | (void) ConcatenateMagickString(filename,"-%08d",MaxTextExtent); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 784 | (void) FormatLocaleString(command,MaxTextExtent, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 785 | GetDelegateCommands(delegate_info), |
| 786 | read_info->antialias != MagickFalse ? 4 : 1, |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 787 | read_info->antialias != MagickFalse ? 4 : 1,density,options,filename, |
| 788 | postscript_filename,input_filename); |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 789 | status=InvokePostscriptDelegate(read_info->verbose,command,exception); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 790 | (void) InterpretImageFilename(image_info,image,filename,1, |
| cristy | 6fccee1 | 2011-10-20 18:43:18 +0000 | [diff] [blame] | 791 | read_info->filename,exception); |
| cristy | 41083a4 | 2009-09-07 23:47:59 +0000 | [diff] [blame] | 792 | if ((status == MagickFalse) || |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 793 | (IsPostscriptRendered(read_info->filename) == MagickFalse)) |
| 794 | { |
| 795 | (void) ConcatenateMagickString(command," -c showpage",MaxTextExtent); |
| cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 796 | status=InvokePostscriptDelegate(read_info->verbose,command,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | (void) RelinquishUniqueFileResource(postscript_filename); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | (void) RelinquishUniqueFileResource(input_filename); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 800 | postscript_image=(Image *) NULL; |
| 801 | if (status == MagickFalse) |
| 802 | for (i=1; ; i++) |
| 803 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 804 | (void) InterpretImageFilename(image_info,image,filename,(int) i, |
| cristy | 6fccee1 | 2011-10-20 18:43:18 +0000 | [diff] [blame] | 805 | read_info->filename,exception); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 806 | if (IsPostscriptRendered(read_info->filename) == MagickFalse) |
| 807 | break; |
| 808 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 809 | } |
| 810 | else |
| 811 | for (i=1; ; i++) |
| 812 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 813 | (void) InterpretImageFilename(image_info,image,filename,(int) i, |
| cristy | 6fccee1 | 2011-10-20 18:43:18 +0000 | [diff] [blame] | 814 | read_info->filename,exception); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 815 | if (IsPostscriptRendered(read_info->filename) == MagickFalse) |
| 816 | break; |
| 817 | next=ReadImage(read_info,exception); |
| 818 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 819 | if (next == (Image *) NULL) |
| 820 | break; |
| 821 | AppendImageToList(&postscript_image,next); |
| 822 | } |
| 823 | (void) RelinquishUniqueFileResource(read_info->filename); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | read_info=DestroyImageInfo(read_info); |
| 825 | if (postscript_image == (Image *) NULL) |
| 826 | { |
| 827 | image=DestroyImageList(image); |
| 828 | ThrowFileException(exception,DelegateError,"PostscriptDelegateFailed", |
| 829 | image_info->filename); |
| 830 | return((Image *) NULL); |
| 831 | } |
| 832 | if (LocaleCompare(postscript_image->magick,"BMP") == 0) |
| 833 | { |
| 834 | Image |
| 835 | *cmyk_image; |
| 836 | |
| 837 | cmyk_image=ConsolidateCMYKImages(postscript_image,exception); |
| 838 | if (cmyk_image != (Image *) NULL) |
| 839 | { |
| 840 | postscript_image=DestroyImageList(postscript_image); |
| 841 | postscript_image=cmyk_image; |
| 842 | } |
| 843 | } |
| 844 | if (image_info->number_scenes != 0) |
| 845 | { |
| 846 | Image |
| 847 | *clone_image; |
| 848 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 849 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 850 | i; |
| 851 | |
| 852 | /* |
| 853 | Add place holder images to meet the subimage specification requirement. |
| 854 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 855 | for (i=0; i < (ssize_t) image_info->scene; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | { |
| 857 | clone_image=CloneImage(postscript_image,1,1,MagickTrue,exception); |
| 858 | if (clone_image != (Image *) NULL) |
| 859 | PrependImageToList(&postscript_image,clone_image); |
| 860 | } |
| 861 | } |
| 862 | do |
| 863 | { |
| 864 | (void) CopyMagickString(postscript_image->filename,filename,MaxTextExtent); |
| 865 | if (columns != 0) |
| 866 | postscript_image->magick_columns=columns; |
| 867 | if (rows != 0) |
| 868 | postscript_image->magick_rows=rows; |
| 869 | postscript_image->page=page; |
| 870 | (void) CloneImageProfiles(postscript_image,image); |
| 871 | (void) CloneImageProperties(postscript_image,image); |
| 872 | next=SyncNextImageInList(postscript_image); |
| 873 | if (next != (Image *) NULL) |
| 874 | postscript_image=next; |
| 875 | } while (next != (Image *) NULL); |
| 876 | image=DestroyImageList(image); |
| 877 | scene=0; |
| 878 | for (next=GetFirstImageInList(postscript_image); next != (Image *) NULL; ) |
| 879 | { |
| 880 | next->scene=scene++; |
| 881 | next=GetNextImageInList(next); |
| 882 | } |
| 883 | return(GetFirstImageInList(postscript_image)); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 888 | % % |
| 889 | % % |
| 890 | % % |
| 891 | % R e g i s t e r P S I m a g e % |
| 892 | % % |
| 893 | % % |
| 894 | % % |
| 895 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 896 | % |
| 897 | % RegisterPSImage() adds properties for the PS image format to |
| 898 | % the list of supported formats. The properties include the image format |
| 899 | % tag, a method to read and/or write the format, whether the format |
| 900 | % supports the saving of more than one frame to the same file or blob, |
| 901 | % whether the format supports native in-memory I/O, and a brief |
| 902 | % description of the format. |
| 903 | % |
| 904 | % The format of the RegisterPSImage method is: |
| 905 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 906 | % size_t RegisterPSImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 907 | % |
| 908 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 909 | ModuleExport size_t RegisterPSImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 910 | { |
| 911 | MagickInfo |
| 912 | *entry; |
| 913 | |
| 914 | entry=SetMagickInfo("EPI"); |
| 915 | entry->decoder=(DecodeImageHandler *) ReadPSImage; |
| 916 | entry->encoder=(EncodeImageHandler *) WritePSImage; |
| 917 | entry->magick=(IsImageFormatHandler *) IsPS; |
| 918 | entry->adjoin=MagickFalse; |
| 919 | entry->blob_support=MagickFalse; |
| 920 | entry->seekable_stream=MagickTrue; |
| 921 | entry->thread_support=EncoderThreadSupport; |
| 922 | entry->description=ConstantString( |
| 923 | "Encapsulated PostScript Interchange format"); |
| 924 | entry->module=ConstantString("PS"); |
| 925 | (void) RegisterMagickInfo(entry); |
| 926 | entry=SetMagickInfo("EPS"); |
| 927 | entry->decoder=(DecodeImageHandler *) ReadPSImage; |
| 928 | entry->encoder=(EncodeImageHandler *) WritePSImage; |
| 929 | entry->magick=(IsImageFormatHandler *) IsPS; |
| 930 | entry->adjoin=MagickFalse; |
| 931 | entry->blob_support=MagickFalse; |
| 932 | entry->seekable_stream=MagickTrue; |
| 933 | entry->thread_support=EncoderThreadSupport; |
| 934 | entry->description=ConstantString("Encapsulated PostScript"); |
| 935 | entry->module=ConstantString("PS"); |
| 936 | (void) RegisterMagickInfo(entry); |
| 937 | entry=SetMagickInfo("EPSF"); |
| 938 | entry->decoder=(DecodeImageHandler *) ReadPSImage; |
| 939 | entry->encoder=(EncodeImageHandler *) WritePSImage; |
| 940 | entry->magick=(IsImageFormatHandler *) IsPS; |
| 941 | entry->adjoin=MagickFalse; |
| 942 | entry->blob_support=MagickFalse; |
| 943 | entry->seekable_stream=MagickTrue; |
| 944 | entry->description=ConstantString("Encapsulated PostScript"); |
| 945 | entry->module=ConstantString("PS"); |
| 946 | (void) RegisterMagickInfo(entry); |
| 947 | entry=SetMagickInfo("EPSI"); |
| 948 | entry->decoder=(DecodeImageHandler *) ReadPSImage; |
| 949 | entry->encoder=(EncodeImageHandler *) WritePSImage; |
| 950 | entry->magick=(IsImageFormatHandler *) IsPS; |
| 951 | entry->adjoin=MagickFalse; |
| 952 | entry->blob_support=MagickFalse; |
| 953 | entry->seekable_stream=MagickTrue; |
| 954 | entry->thread_support=EncoderThreadSupport; |
| 955 | entry->description=ConstantString( |
| 956 | "Encapsulated PostScript Interchange format"); |
| 957 | entry->module=ConstantString("PS"); |
| 958 | (void) RegisterMagickInfo(entry); |
| 959 | entry=SetMagickInfo("PS"); |
| 960 | entry->decoder=(DecodeImageHandler *) ReadPSImage; |
| 961 | entry->encoder=(EncodeImageHandler *) WritePSImage; |
| 962 | entry->magick=(IsImageFormatHandler *) IsPS; |
| 963 | entry->module=ConstantString("PS"); |
| 964 | entry->blob_support=MagickFalse; |
| 965 | entry->seekable_stream=MagickTrue; |
| 966 | entry->thread_support=EncoderThreadSupport; |
| 967 | entry->description=ConstantString("PostScript"); |
| 968 | (void) RegisterMagickInfo(entry); |
| 969 | return(MagickImageCoderSignature); |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 974 | % % |
| 975 | % % |
| 976 | % % |
| 977 | % U n r e g i s t e r P S I m a g e % |
| 978 | % % |
| 979 | % % |
| 980 | % % |
| 981 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 982 | % |
| 983 | % UnregisterPSImage() removes format registrations made by the |
| 984 | % PS module from the list of supported formats. |
| 985 | % |
| 986 | % The format of the UnregisterPSImage method is: |
| 987 | % |
| 988 | % UnregisterPSImage(void) |
| 989 | % |
| 990 | */ |
| 991 | ModuleExport void UnregisterPSImage(void) |
| 992 | { |
| 993 | (void) UnregisterMagickInfo("EPI"); |
| 994 | (void) UnregisterMagickInfo("EPS"); |
| 995 | (void) UnregisterMagickInfo("EPSF"); |
| 996 | (void) UnregisterMagickInfo("EPSI"); |
| 997 | (void) UnregisterMagickInfo("PS"); |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1002 | % % |
| 1003 | % % |
| 1004 | % % |
| 1005 | % W r i t e P S I m a g e % |
| 1006 | % % |
| 1007 | % % |
| 1008 | % % |
| 1009 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1010 | % |
| 1011 | % WritePSImage translates an image to encapsulated Postscript |
| 1012 | % Level I for printing. If the supplied geometry is null, the image is |
| 1013 | % centered on the Postscript page. Otherwise, the image is positioned as |
| 1014 | % specified by the geometry. |
| 1015 | % |
| 1016 | % The format of the WritePSImage method is: |
| 1017 | % |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1018 | % MagickBooleanType WritePSImage(const ImageInfo *image_info, |
| 1019 | % Image *image,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1020 | % |
| 1021 | % A description of each parameter follows: |
| 1022 | % |
| 1023 | % o image_info: the image info. |
| 1024 | % |
| 1025 | % o image: the image. |
| 1026 | % |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1027 | % o exception: return any errors or warnings in this structure. |
| 1028 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1029 | */ |
| 1030 | |
| 1031 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 1032 | { |
| 1033 | if (x < y) |
| 1034 | return(x); |
| 1035 | return(y); |
| 1036 | } |
| 1037 | |
| 1038 | static inline unsigned char *PopHexPixel(const char **hex_digits, |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1039 | const size_t pixel,unsigned char *pixels) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | { |
| 1041 | register const char |
| 1042 | *hex; |
| 1043 | |
| 1044 | hex=hex_digits[pixel]; |
| 1045 | *pixels++=(unsigned char) (*hex++); |
| 1046 | *pixels++=(unsigned char) (*hex); |
| 1047 | return(pixels); |
| 1048 | } |
| 1049 | |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1050 | static MagickBooleanType WritePSImage(const ImageInfo *image_info,Image *image, |
| 1051 | ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1052 | { |
| 1053 | #define WriteRunlengthPacket(image,pixel,length,p) \ |
| 1054 | { \ |
| 1055 | if ((image->matte != MagickFalse) && \ |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1056 | (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)) \ |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | { \ |
| 1058 | q=PopHexPixel(hex_digits,0xff,q); \ |
| 1059 | q=PopHexPixel(hex_digits,0xff,q); \ |
| 1060 | q=PopHexPixel(hex_digits,0xff,q); \ |
| 1061 | } \ |
| 1062 | else \ |
| 1063 | { \ |
| 1064 | q=PopHexPixel(hex_digits,ScaleQuantumToChar(pixel.red),q); \ |
| 1065 | q=PopHexPixel(hex_digits,ScaleQuantumToChar(pixel.green),q); \ |
| 1066 | q=PopHexPixel(hex_digits,ScaleQuantumToChar(pixel.blue),q); \ |
| 1067 | } \ |
| cristy | 9f027d1 | 2011-09-21 01:17:17 +0000 | [diff] [blame] | 1068 | q=PopHexPixel(hex_digits,(size_t) MagickMin(length,0xff),q); \ |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | static const char |
| 1072 | *hex_digits[] = |
| 1073 | { |
| 1074 | "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", |
| 1075 | "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", |
| 1076 | "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", |
| 1077 | "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", |
| 1078 | "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", |
| 1079 | "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47", |
| 1080 | "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", |
| 1081 | "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", |
| 1082 | "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", |
| 1083 | "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", |
| 1084 | "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83", |
| 1085 | "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", |
| 1086 | "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", |
| 1087 | "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", |
| 1088 | "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", |
| 1089 | "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", |
| 1090 | "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", |
| 1091 | "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", |
| 1092 | "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", |
| 1093 | "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF", |
| 1094 | "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", |
| 1095 | "FC", "FD", "FE", "FF", (char *) NULL |
| 1096 | }, |
| 1097 | *PostscriptProlog[]= |
| 1098 | { |
| 1099 | "%%BeginProlog", |
| 1100 | "%", |
| 1101 | "% Display a color image. The image is displayed in color on", |
| 1102 | "% Postscript viewers or printers that support color, otherwise", |
| 1103 | "% it is displayed as grayscale.", |
| 1104 | "%", |
| 1105 | "/DirectClassPacket", |
| 1106 | "{", |
| 1107 | " %", |
| 1108 | " % Get a DirectClass packet.", |
| 1109 | " %", |
| 1110 | " % Parameters:", |
| 1111 | " % red.", |
| 1112 | " % green.", |
| 1113 | " % blue.", |
| 1114 | " % length: number of pixels minus one of this color (optional).", |
| 1115 | " %", |
| 1116 | " currentfile color_packet readhexstring pop pop", |
| 1117 | " compression 0 eq", |
| 1118 | " {", |
| 1119 | " /number_pixels 3 def", |
| 1120 | " }", |
| 1121 | " {", |
| 1122 | " currentfile byte readhexstring pop 0 get", |
| 1123 | " /number_pixels exch 1 add 3 mul def", |
| 1124 | " } ifelse", |
| 1125 | " 0 3 number_pixels 1 sub", |
| 1126 | " {", |
| 1127 | " pixels exch color_packet putinterval", |
| 1128 | " } for", |
| 1129 | " pixels 0 number_pixels getinterval", |
| 1130 | "} bind def", |
| 1131 | "", |
| 1132 | "/DirectClassImage", |
| 1133 | "{", |
| 1134 | " %", |
| 1135 | " % Display a DirectClass image.", |
| 1136 | " %", |
| 1137 | " systemdict /colorimage known", |
| 1138 | " {", |
| 1139 | " columns rows 8", |
| 1140 | " [", |
| 1141 | " columns 0 0", |
| 1142 | " rows neg 0 rows", |
| 1143 | " ]", |
| 1144 | " { DirectClassPacket } false 3 colorimage", |
| 1145 | " }", |
| 1146 | " {", |
| 1147 | " %", |
| 1148 | " % No colorimage operator; convert to grayscale.", |
| 1149 | " %", |
| 1150 | " columns rows 8", |
| 1151 | " [", |
| 1152 | " columns 0 0", |
| 1153 | " rows neg 0 rows", |
| 1154 | " ]", |
| 1155 | " { GrayDirectClassPacket } image", |
| 1156 | " } ifelse", |
| 1157 | "} bind def", |
| 1158 | "", |
| 1159 | "/GrayDirectClassPacket", |
| 1160 | "{", |
| 1161 | " %", |
| 1162 | " % Get a DirectClass packet; convert to grayscale.", |
| 1163 | " %", |
| 1164 | " % Parameters:", |
| 1165 | " % red", |
| 1166 | " % green", |
| 1167 | " % blue", |
| 1168 | " % length: number of pixels minus one of this color (optional).", |
| 1169 | " %", |
| 1170 | " currentfile color_packet readhexstring pop pop", |
| 1171 | " color_packet 0 get 0.299 mul", |
| 1172 | " color_packet 1 get 0.587 mul add", |
| 1173 | " color_packet 2 get 0.114 mul add", |
| 1174 | " cvi", |
| 1175 | " /gray_packet exch def", |
| 1176 | " compression 0 eq", |
| 1177 | " {", |
| 1178 | " /number_pixels 1 def", |
| 1179 | " }", |
| 1180 | " {", |
| 1181 | " currentfile byte readhexstring pop 0 get", |
| 1182 | " /number_pixels exch 1 add def", |
| 1183 | " } ifelse", |
| 1184 | " 0 1 number_pixels 1 sub", |
| 1185 | " {", |
| 1186 | " pixels exch gray_packet put", |
| 1187 | " } for", |
| 1188 | " pixels 0 number_pixels getinterval", |
| 1189 | "} bind def", |
| 1190 | "", |
| 1191 | "/GrayPseudoClassPacket", |
| 1192 | "{", |
| 1193 | " %", |
| 1194 | " % Get a PseudoClass packet; convert to grayscale.", |
| 1195 | " %", |
| 1196 | " % Parameters:", |
| 1197 | " % index: index into the colormap.", |
| 1198 | " % length: number of pixels minus one of this color (optional).", |
| 1199 | " %", |
| 1200 | " currentfile byte readhexstring pop 0 get", |
| 1201 | " /offset exch 3 mul def", |
| 1202 | " /color_packet colormap offset 3 getinterval def", |
| 1203 | " color_packet 0 get 0.299 mul", |
| 1204 | " color_packet 1 get 0.587 mul add", |
| 1205 | " color_packet 2 get 0.114 mul add", |
| 1206 | " cvi", |
| 1207 | " /gray_packet exch def", |
| 1208 | " compression 0 eq", |
| 1209 | " {", |
| 1210 | " /number_pixels 1 def", |
| 1211 | " }", |
| 1212 | " {", |
| 1213 | " currentfile byte readhexstring pop 0 get", |
| 1214 | " /number_pixels exch 1 add def", |
| 1215 | " } ifelse", |
| 1216 | " 0 1 number_pixels 1 sub", |
| 1217 | " {", |
| 1218 | " pixels exch gray_packet put", |
| 1219 | " } for", |
| 1220 | " pixels 0 number_pixels getinterval", |
| 1221 | "} bind def", |
| 1222 | "", |
| 1223 | "/PseudoClassPacket", |
| 1224 | "{", |
| 1225 | " %", |
| 1226 | " % Get a PseudoClass packet.", |
| 1227 | " %", |
| 1228 | " % Parameters:", |
| 1229 | " % index: index into the colormap.", |
| 1230 | " % length: number of pixels minus one of this color (optional).", |
| 1231 | " %", |
| 1232 | " currentfile byte readhexstring pop 0 get", |
| 1233 | " /offset exch 3 mul def", |
| 1234 | " /color_packet colormap offset 3 getinterval def", |
| 1235 | " compression 0 eq", |
| 1236 | " {", |
| 1237 | " /number_pixels 3 def", |
| 1238 | " }", |
| 1239 | " {", |
| 1240 | " currentfile byte readhexstring pop 0 get", |
| 1241 | " /number_pixels exch 1 add 3 mul def", |
| 1242 | " } ifelse", |
| 1243 | " 0 3 number_pixels 1 sub", |
| 1244 | " {", |
| 1245 | " pixels exch color_packet putinterval", |
| 1246 | " } for", |
| 1247 | " pixels 0 number_pixels getinterval", |
| 1248 | "} bind def", |
| 1249 | "", |
| 1250 | "/PseudoClassImage", |
| 1251 | "{", |
| 1252 | " %", |
| 1253 | " % Display a PseudoClass image.", |
| 1254 | " %", |
| 1255 | " % Parameters:", |
| 1256 | " % class: 0-PseudoClass or 1-Grayscale.", |
| 1257 | " %", |
| 1258 | " currentfile buffer readline pop", |
| 1259 | " token pop /class exch def pop", |
| 1260 | " class 0 gt", |
| 1261 | " {", |
| 1262 | " currentfile buffer readline pop", |
| 1263 | " token pop /depth exch def pop", |
| 1264 | " /grays columns 8 add depth sub depth mul 8 idiv string def", |
| 1265 | " columns rows depth", |
| 1266 | " [", |
| 1267 | " columns 0 0", |
| 1268 | " rows neg 0 rows", |
| 1269 | " ]", |
| 1270 | " { currentfile grays readhexstring pop } image", |
| 1271 | " }", |
| 1272 | " {", |
| 1273 | " %", |
| 1274 | " % Parameters:", |
| 1275 | " % colors: number of colors in the colormap.", |
| 1276 | " % colormap: red, green, blue color packets.", |
| 1277 | " %", |
| 1278 | " currentfile buffer readline pop", |
| 1279 | " token pop /colors exch def pop", |
| 1280 | " /colors colors 3 mul def", |
| 1281 | " /colormap colors string def", |
| 1282 | " currentfile colormap readhexstring pop pop", |
| 1283 | " systemdict /colorimage known", |
| 1284 | " {", |
| 1285 | " columns rows 8", |
| 1286 | " [", |
| 1287 | " columns 0 0", |
| 1288 | " rows neg 0 rows", |
| 1289 | " ]", |
| 1290 | " { PseudoClassPacket } false 3 colorimage", |
| 1291 | " }", |
| 1292 | " {", |
| 1293 | " %", |
| 1294 | " % No colorimage operator; convert to grayscale.", |
| 1295 | " %", |
| 1296 | " columns rows 8", |
| 1297 | " [", |
| 1298 | " columns 0 0", |
| 1299 | " rows neg 0 rows", |
| 1300 | " ]", |
| 1301 | " { GrayPseudoClassPacket } image", |
| 1302 | " } ifelse", |
| 1303 | " } ifelse", |
| 1304 | "} bind def", |
| 1305 | "", |
| 1306 | "/DisplayImage", |
| 1307 | "{", |
| 1308 | " %", |
| 1309 | " % Display a DirectClass or PseudoClass image.", |
| 1310 | " %", |
| 1311 | " % Parameters:", |
| 1312 | " % x & y translation.", |
| 1313 | " % x & y scale.", |
| 1314 | " % label pointsize.", |
| 1315 | " % image label.", |
| 1316 | " % image columns & rows.", |
| 1317 | " % class: 0-DirectClass or 1-PseudoClass.", |
| 1318 | " % compression: 0-none or 1-RunlengthEncoded.", |
| 1319 | " % hex color packets.", |
| 1320 | " %", |
| 1321 | " gsave", |
| 1322 | " /buffer 512 string def", |
| 1323 | " /byte 1 string def", |
| 1324 | " /color_packet 3 string def", |
| 1325 | " /pixels 768 string def", |
| 1326 | "", |
| 1327 | " currentfile buffer readline pop", |
| 1328 | " token pop /x exch def", |
| 1329 | " token pop /y exch def pop", |
| 1330 | " x y translate", |
| 1331 | " currentfile buffer readline pop", |
| 1332 | " token pop /x exch def", |
| 1333 | " token pop /y exch def pop", |
| 1334 | " currentfile buffer readline pop", |
| 1335 | " token pop /pointsize exch def pop", |
| 1336 | " /Times-Roman findfont pointsize scalefont setfont", |
| 1337 | (char *) NULL |
| 1338 | }, |
| 1339 | *PostscriptEpilog[]= |
| 1340 | { |
| 1341 | " x y scale", |
| 1342 | " currentfile buffer readline pop", |
| 1343 | " token pop /columns exch def", |
| 1344 | " token pop /rows exch def pop", |
| 1345 | " currentfile buffer readline pop", |
| 1346 | " token pop /class exch def pop", |
| 1347 | " currentfile buffer readline pop", |
| 1348 | " token pop /compression exch def pop", |
| 1349 | " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse", |
| 1350 | (char *) NULL |
| 1351 | }; |
| 1352 | |
| 1353 | char |
| 1354 | buffer[MaxTextExtent], |
| 1355 | date[MaxTextExtent], |
| 1356 | **labels, |
| 1357 | page_geometry[MaxTextExtent]; |
| 1358 | |
| 1359 | const char |
| 1360 | **s, |
| 1361 | *value; |
| 1362 | |
| 1363 | const StringInfo |
| 1364 | *profile; |
| 1365 | |
| 1366 | double |
| 1367 | pointsize; |
| 1368 | |
| 1369 | GeometryInfo |
| 1370 | geometry_info; |
| 1371 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1372 | MagickBooleanType |
| 1373 | status; |
| 1374 | |
| 1375 | MagickOffsetType |
| 1376 | scene; |
| 1377 | |
| 1378 | MagickStatusType |
| 1379 | flags; |
| 1380 | |
| cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1381 | PixelInfo |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1382 | pixel; |
| 1383 | |
| 1384 | PointInfo |
| 1385 | delta, |
| 1386 | resolution, |
| 1387 | scale; |
| 1388 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1389 | Quantum |
| 1390 | index; |
| 1391 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1392 | RectangleInfo |
| 1393 | geometry, |
| 1394 | media_info, |
| 1395 | page_info; |
| 1396 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1397 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1398 | *p; |
| 1399 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1400 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1401 | i, |
| 1402 | x; |
| 1403 | |
| 1404 | register unsigned char |
| 1405 | *q; |
| 1406 | |
| 1407 | SegmentInfo |
| 1408 | bounds; |
| 1409 | |
| 1410 | size_t |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1411 | bit, |
| 1412 | byte, |
| 1413 | length, |
| 1414 | page, |
| 1415 | text_size; |
| 1416 | |
| 1417 | ssize_t |
| 1418 | j, |
| 1419 | y; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1420 | |
| 1421 | time_t |
| 1422 | timer; |
| 1423 | |
| 1424 | unsigned char |
| 1425 | pixels[2048]; |
| 1426 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | Open output image file. |
| 1429 | */ |
| 1430 | assert(image_info != (const ImageInfo *) NULL); |
| 1431 | assert(image_info->signature == MagickSignature); |
| 1432 | assert(image != (Image *) NULL); |
| 1433 | assert(image->signature == MagickSignature); |
| 1434 | if (image->debug != MagickFalse) |
| 1435 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1436 | assert(exception != (ExceptionInfo *) NULL); |
| 1437 | assert(exception->signature == MagickSignature); |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1438 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1439 | if (status == MagickFalse) |
| 1440 | return(status); |
| 1441 | (void) ResetMagickMemory(&bounds,0,sizeof(bounds)); |
| 1442 | page=1; |
| 1443 | scene=0; |
| 1444 | do |
| 1445 | { |
| 1446 | /* |
| 1447 | Scale relative to dots-per-inch. |
| 1448 | */ |
| cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 1449 | if ((IsRGBColorspace(image->colorspace) == MagickFalse) && |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1450 | (image->colorspace != CMYKColorspace)) |
| cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame^] | 1451 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1452 | delta.x=DefaultResolution; |
| 1453 | delta.y=DefaultResolution; |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1454 | resolution.x=image->resolution.x; |
| 1455 | resolution.y=image->resolution.y; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1456 | if ((resolution.x == 0.0) || (resolution.y == 0.0)) |
| 1457 | { |
| 1458 | flags=ParseGeometry(PSDensityGeometry,&geometry_info); |
| 1459 | resolution.x=geometry_info.rho; |
| 1460 | resolution.y=geometry_info.sigma; |
| 1461 | if ((flags & SigmaValue) == 0) |
| 1462 | resolution.y=resolution.x; |
| 1463 | } |
| 1464 | if (image_info->density != (char *) NULL) |
| 1465 | { |
| 1466 | flags=ParseGeometry(image_info->density,&geometry_info); |
| 1467 | resolution.x=geometry_info.rho; |
| 1468 | resolution.y=geometry_info.sigma; |
| 1469 | if ((flags & SigmaValue) == 0) |
| 1470 | resolution.y=resolution.x; |
| 1471 | } |
| 1472 | if (image->units == PixelsPerCentimeterResolution) |
| 1473 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 1474 | resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0); |
| 1475 | resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1476 | } |
| 1477 | SetGeometry(image,&geometry); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1478 | (void) FormatLocaleString(page_geometry,MaxTextExtent,"%.20gx%.20g", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1479 | (double) image->columns,(double) image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1480 | if (image_info->page != (char *) NULL) |
| 1481 | (void) CopyMagickString(page_geometry,image_info->page,MaxTextExtent); |
| 1482 | else |
| 1483 | if ((image->page.width != 0) && (image->page.height != 0)) |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1484 | (void) FormatLocaleString(page_geometry,MaxTextExtent, |
| cristy | 6d8abba | 2010-06-03 01:10:47 +0000 | [diff] [blame] | 1485 | "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double) |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1486 | image->page.height,(double) image->page.x,(double) image->page.y); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1487 | else |
| 1488 | if ((image->gravity != UndefinedGravity) && |
| 1489 | (LocaleCompare(image_info->magick,"PS") == 0)) |
| 1490 | (void) CopyMagickString(page_geometry,PSPageGeometry,MaxTextExtent); |
| 1491 | (void) ConcatenateMagickString(page_geometry,">",MaxTextExtent); |
| 1492 | (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y, |
| 1493 | &geometry.width,&geometry.height); |
| 1494 | scale.x=(double) (geometry.width*delta.x)/resolution.x; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1495 | geometry.width=(size_t) floor(scale.x+0.5); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1496 | scale.y=(double) (geometry.height*delta.y)/resolution.y; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1497 | geometry.height=(size_t) floor(scale.y+0.5); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1498 | (void) ParseAbsoluteGeometry(page_geometry,&media_info); |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1499 | (void) ParseGravityGeometry(image,page_geometry,&page_info,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1500 | if (image->gravity != UndefinedGravity) |
| 1501 | { |
| 1502 | geometry.x=(-page_info.x); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1503 | geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1504 | } |
| 1505 | pointsize=12.0; |
| 1506 | if (image_info->pointsize != 0.0) |
| 1507 | pointsize=image_info->pointsize; |
| 1508 | text_size=0; |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1509 | value=GetImageProperty(image,"label",exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1510 | if (value != (const char *) NULL) |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1511 | text_size=(size_t) (MultilineCensus(value)*pointsize+12); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1512 | if (page == 1) |
| 1513 | { |
| 1514 | /* |
| 1515 | Output Postscript header. |
| 1516 | */ |
| 1517 | if (LocaleCompare(image_info->magick,"PS") == 0) |
| 1518 | (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MaxTextExtent); |
| 1519 | else |
| 1520 | (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n", |
| 1521 | MaxTextExtent); |
| 1522 | (void) WriteBlobString(image,buffer); |
| 1523 | (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n"); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1524 | (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Title: (%s)\n", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1525 | image->filename); |
| 1526 | (void) WriteBlobString(image,buffer); |
| 1527 | timer=time((time_t *) NULL); |
| 1528 | (void) FormatMagickTime(timer,MaxTextExtent,date); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1529 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1530 | "%%%%CreationDate: (%s)\n",date); |
| 1531 | (void) WriteBlobString(image,buffer); |
| 1532 | bounds.x1=(double) geometry.x; |
| 1533 | bounds.y1=(double) geometry.y; |
| 1534 | bounds.x2=(double) geometry.x+scale.x; |
| 1535 | bounds.y2=(double) geometry.y+(geometry.height+text_size); |
| 1536 | if ((image_info->adjoin != MagickFalse) && |
| 1537 | (GetNextImageInList(image) != (Image *) NULL)) |
| 1538 | (void) CopyMagickString(buffer,"%%%%BoundingBox: (atend)\n", |
| 1539 | MaxTextExtent); |
| 1540 | else |
| 1541 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1542 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1543 | "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5), |
| 1544 | ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1545 | (void) WriteBlobString(image,buffer); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1546 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1547 | "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1, |
| cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1548 | bounds.y1,bounds.x2,bounds.y2); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1549 | } |
| 1550 | (void) WriteBlobString(image,buffer); |
| 1551 | profile=GetImageProfile(image,"8bim"); |
| 1552 | if (profile != (StringInfo *) NULL) |
| 1553 | { |
| 1554 | /* |
| 1555 | Embed Photoshop profile. |
| 1556 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1557 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1558 | "%%BeginPhotoshop: %.20g",(double) GetStringInfoLength(profile)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1559 | (void) WriteBlobString(image,buffer); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | { |
| 1562 | if ((i % 32) == 0) |
| 1563 | (void) WriteBlobString(image,"\n% "); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1564 | (void) FormatLocaleString(buffer,MaxTextExtent,"%02X", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1565 | (unsigned int) (GetStringInfoDatum(profile)[i] & 0xff)); |
| 1566 | (void) WriteBlobString(image,buffer); |
| 1567 | } |
| 1568 | (void) WriteBlobString(image,"\n%EndPhotoshop\n"); |
| 1569 | } |
| 1570 | profile=GetImageProfile(image,"xmp"); |
| cristy | 2b2eb91 | 2010-03-03 14:56:40 +0000 | [diff] [blame] | 1571 | if (0 && (profile != (StringInfo *) NULL)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1572 | { |
| 1573 | /* |
| 1574 | Embed XML profile. |
| 1575 | */ |
| 1576 | (void) WriteBlobString(image,"\n%begin_xml_code\n"); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1577 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1578 | "\n%%begin_xml_packet: %.20g\n",(double) |
| cristy | eec18db | 2010-03-03 21:15:45 +0000 | [diff] [blame] | 1579 | GetStringInfoLength(profile)); |
| cristy | 2b2eb91 | 2010-03-03 14:56:40 +0000 | [diff] [blame] | 1580 | (void) WriteBlobString(image,buffer); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1581 | for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1582 | (void) WriteBlobByte(image,GetStringInfoDatum(profile)[i]); |
| cristy | 2b2eb91 | 2010-03-03 14:56:40 +0000 | [diff] [blame] | 1583 | (void) WriteBlobString(image,"\n%end_xml_packet\n%end_xml_code\n"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1584 | } |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1585 | value=GetImageProperty(image,"label",exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1586 | if (value != (const char *) NULL) |
| 1587 | (void) WriteBlobString(image, |
| 1588 | "%%DocumentNeededResources: font Times-Roman\n"); |
| 1589 | (void) WriteBlobString(image,"%%DocumentData: Clean7Bit\n"); |
| 1590 | (void) WriteBlobString(image,"%%LanguageLevel: 1\n"); |
| 1591 | if (LocaleCompare(image_info->magick,"PS") != 0) |
| 1592 | (void) WriteBlobString(image,"%%Pages: 1\n"); |
| 1593 | else |
| 1594 | { |
| 1595 | /* |
| 1596 | Compute the number of pages. |
| 1597 | */ |
| 1598 | (void) WriteBlobString(image,"%%Orientation: Portrait\n"); |
| 1599 | (void) WriteBlobString(image,"%%PageOrder: Ascend\n"); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1600 | (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Pages: %.20g\n", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1601 | image_info->adjoin != MagickFalse ? (double) |
| 1602 | GetImageListLength(image) : 1.0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1603 | (void) WriteBlobString(image,buffer); |
| 1604 | } |
| 1605 | (void) WriteBlobString(image,"%%EndComments\n"); |
| 1606 | (void) WriteBlobString(image,"\n%%BeginDefaults\n"); |
| 1607 | (void) WriteBlobString(image,"%%EndDefaults\n\n"); |
| 1608 | if ((LocaleCompare(image_info->magick,"EPI") == 0) || |
| 1609 | (LocaleCompare(image_info->magick,"EPSI") == 0) || |
| 1610 | (LocaleCompare(image_info->magick,"EPT") == 0)) |
| 1611 | { |
| 1612 | Image |
| 1613 | *preview_image; |
| 1614 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | Quantum |
| 1616 | pixel; |
| 1617 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1618 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1619 | x; |
| 1620 | |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1621 | ssize_t |
| 1622 | y; |
| 1623 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1624 | /* |
| 1625 | Create preview image. |
| 1626 | */ |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1627 | preview_image=CloneImage(image,0,0,MagickTrue,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1628 | if (preview_image == (Image *) NULL) |
| 1629 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1630 | /* |
| 1631 | Dump image as bitmap. |
| 1632 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1633 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1634 | "%%%%BeginPreview: %.20g %.20g %.20g %.20g\n%% ",(double) |
| 1635 | preview_image->columns,(double) preview_image->rows,1.0, |
| 1636 | (double) ((((preview_image->columns+7) >> 3)*preview_image->rows+ |
| 1637 | 35)/36)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | (void) WriteBlobString(image,buffer); |
| 1639 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1640 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1641 | { |
| 1642 | p=GetVirtualPixels(preview_image,0,y,preview_image->columns,1, |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1643 | exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1644 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1645 | break; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1646 | bit=0; |
| 1647 | byte=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1648 | for (x=0; x < (ssize_t) preview_image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1649 | { |
| 1650 | byte<<=1; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1651 | pixel=GetPixelIntensity(preview_image,p); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1652 | if (pixel >= (Quantum) (QuantumRange/2)) |
| 1653 | byte|=0x01; |
| 1654 | bit++; |
| 1655 | if (bit == 8) |
| 1656 | { |
| 1657 | q=PopHexPixel(hex_digits,byte,q); |
| 1658 | if ((q-pixels+8) >= 80) |
| 1659 | { |
| 1660 | *q++='\n'; |
| 1661 | (void) WriteBlob(image,q-pixels,pixels); |
| 1662 | q=pixels; |
| 1663 | (void) WriteBlobString(image,"% "); |
| 1664 | }; |
| 1665 | bit=0; |
| 1666 | byte=0; |
| 1667 | } |
| 1668 | } |
| 1669 | if (bit != 0) |
| 1670 | { |
| 1671 | byte<<=(8-bit); |
| 1672 | q=PopHexPixel(hex_digits,byte,q); |
| 1673 | if ((q-pixels+8) >= 80) |
| 1674 | { |
| 1675 | *q++='\n'; |
| 1676 | (void) WriteBlob(image,q-pixels,pixels); |
| 1677 | q=pixels; |
| 1678 | (void) WriteBlobString(image,"% "); |
| 1679 | }; |
| 1680 | }; |
| 1681 | } |
| 1682 | if (q != pixels) |
| 1683 | { |
| 1684 | *q++='\n'; |
| 1685 | (void) WriteBlob(image,q-pixels,pixels); |
| 1686 | } |
| 1687 | (void) WriteBlobString(image,"\n%%EndPreview\n"); |
| 1688 | preview_image=DestroyImage(preview_image); |
| 1689 | } |
| 1690 | /* |
| 1691 | Output Postscript commands. |
| 1692 | */ |
| 1693 | for (s=PostscriptProlog; *s != (char *) NULL; s++) |
| 1694 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1695 | (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*s); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1696 | (void) WriteBlobString(image,buffer); |
| 1697 | } |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1698 | value=GetImageProperty(image,"label",exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1699 | if (value != (const char *) NULL) |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1700 | for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1701 | { |
| 1702 | (void) WriteBlobString(image," /label 512 string def\n"); |
| 1703 | (void) WriteBlobString(image," currentfile label readline pop\n"); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1704 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1705 | " 0 y %g add moveto label show pop\n",j*pointsize+12); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1706 | (void) WriteBlobString(image,buffer); |
| 1707 | } |
| 1708 | for (s=PostscriptEpilog; *s != (char *) NULL; s++) |
| 1709 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1710 | (void) FormatLocaleString(buffer,MaxTextExtent,"%s\n",*s); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1711 | (void) WriteBlobString(image,buffer); |
| 1712 | } |
| 1713 | if (LocaleCompare(image_info->magick,"PS") == 0) |
| 1714 | (void) WriteBlobString(image," showpage\n"); |
| 1715 | (void) WriteBlobString(image,"} bind def\n"); |
| 1716 | (void) WriteBlobString(image,"%%EndProlog\n"); |
| 1717 | } |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1718 | (void) FormatLocaleString(buffer,MaxTextExtent,"%%%%Page: 1 %.20g\n", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1719 | (double) (page++)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1720 | (void) WriteBlobString(image,buffer); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1721 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1722 | "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x, |
| 1723 | (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double) |
| cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 1724 | (geometry.height+text_size)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1725 | (void) WriteBlobString(image,buffer); |
| 1726 | if ((double) geometry.x < bounds.x1) |
| 1727 | bounds.x1=(double) geometry.x; |
| 1728 | if ((double) geometry.y < bounds.y1) |
| 1729 | bounds.y1=(double) geometry.y; |
| 1730 | if ((double) (geometry.x+geometry.width-1) > bounds.x2) |
| 1731 | bounds.x2=(double) geometry.x+geometry.width-1; |
| 1732 | if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2) |
| 1733 | bounds.y2=(double) geometry.y+(geometry.height+text_size)-1; |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1734 | value=GetImageProperty(image,"label",exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1735 | if (value != (const char *) NULL) |
| 1736 | (void) WriteBlobString(image,"%%%%PageResources: font Times-Roman\n"); |
| 1737 | if (LocaleCompare(image_info->magick,"PS") != 0) |
| 1738 | (void) WriteBlobString(image,"userdict begin\n"); |
| 1739 | (void) WriteBlobString(image,"DisplayImage\n"); |
| 1740 | /* |
| 1741 | Output image data. |
| 1742 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1743 | (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n%g %g\n%g\n", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1744 | (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1745 | (void) WriteBlobString(image,buffer); |
| 1746 | labels=(char **) NULL; |
| cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1747 | value=GetImageProperty(image,"label",exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1748 | if (value != (const char *) NULL) |
| 1749 | labels=StringToList(value); |
| 1750 | if (labels != (char **) NULL) |
| 1751 | { |
| 1752 | for (i=0; labels[i] != (char *) NULL; i++) |
| 1753 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1754 | (void) FormatLocaleString(buffer,MaxTextExtent,"%s \n", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1755 | labels[i]); |
| 1756 | (void) WriteBlobString(image,buffer); |
| 1757 | labels[i]=DestroyString(labels[i]); |
| 1758 | } |
| 1759 | labels=(char **) RelinquishMagickMemory(labels); |
| 1760 | } |
| 1761 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1762 | pixel.alpha=(Quantum) TransparentAlpha; |
| 1763 | index=0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1764 | x=0; |
| 1765 | if ((image_info->type != TrueColorType) && |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1766 | (IsImageGray(image,exception) != MagickFalse)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1767 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1768 | if (IsImageMonochrome(image,exception) == MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1769 | { |
| 1770 | Quantum |
| 1771 | pixel; |
| 1772 | |
| 1773 | /* |
| 1774 | Dump image as grayscale. |
| 1775 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1776 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1777 | "%.20g %.20g\n1\n1\n1\n8\n",(double) image->columns,(double) |
| 1778 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1779 | (void) WriteBlobString(image,buffer); |
| 1780 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1781 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1782 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1783 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1784 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1785 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1786 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1787 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1788 | pixel=(Quantum) ScaleQuantumToChar(GetPixelIntensity(image,p)); |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 1789 | q=PopHexPixel(hex_digits,(size_t) pixel,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | i++; |
| 1791 | if ((q-pixels+8) >= 80) |
| 1792 | { |
| 1793 | *q++='\n'; |
| 1794 | (void) WriteBlob(image,q-pixels,pixels); |
| 1795 | q=pixels; |
| 1796 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1797 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1798 | } |
| 1799 | if (image->previous == (Image *) NULL) |
| 1800 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 1801 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) |
| 1802 | y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1803 | if (status == MagickFalse) |
| 1804 | break; |
| 1805 | } |
| 1806 | } |
| 1807 | if (q != pixels) |
| 1808 | { |
| 1809 | *q++='\n'; |
| 1810 | (void) WriteBlob(image,q-pixels,pixels); |
| 1811 | } |
| 1812 | } |
| 1813 | else |
| 1814 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1815 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1816 | y; |
| 1817 | |
| 1818 | Quantum |
| 1819 | pixel; |
| 1820 | |
| 1821 | /* |
| 1822 | Dump image as bitmap. |
| 1823 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1824 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1825 | "%.20g %.20g\n1\n1\n1\n1\n",(double) image->columns,(double) |
| 1826 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1827 | (void) WriteBlobString(image,buffer); |
| 1828 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1829 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1830 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1831 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1832 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1833 | break; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1834 | bit=0; |
| 1835 | byte=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1836 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1837 | { |
| 1838 | byte<<=1; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1839 | pixel=GetPixelIntensity(image,p); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1840 | if (pixel >= (Quantum) (QuantumRange/2)) |
| 1841 | byte|=0x01; |
| 1842 | bit++; |
| 1843 | if (bit == 8) |
| 1844 | { |
| 1845 | q=PopHexPixel(hex_digits,byte,q); |
| 1846 | if ((q-pixels+2) >= 80) |
| 1847 | { |
| 1848 | *q++='\n'; |
| 1849 | (void) WriteBlob(image,q-pixels,pixels); |
| 1850 | q=pixels; |
| 1851 | }; |
| 1852 | bit=0; |
| 1853 | byte=0; |
| 1854 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1855 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1856 | } |
| 1857 | if (bit != 0) |
| 1858 | { |
| 1859 | byte<<=(8-bit); |
| 1860 | q=PopHexPixel(hex_digits,byte,q); |
| 1861 | if ((q-pixels+2) >= 80) |
| 1862 | { |
| 1863 | *q++='\n'; |
| 1864 | (void) WriteBlob(image,q-pixels,pixels); |
| 1865 | q=pixels; |
| 1866 | } |
| 1867 | }; |
| 1868 | if (image->previous == (Image *) NULL) |
| 1869 | { |
| cristy | 6cde06a | 2011-11-24 00:08:43 +0000 | [diff] [blame] | 1870 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1871 | y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1872 | if (status == MagickFalse) |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | if (q != pixels) |
| 1877 | { |
| 1878 | *q++='\n'; |
| 1879 | (void) WriteBlob(image,q-pixels,pixels); |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | else |
| 1884 | if ((image->storage_class == DirectClass) || |
| 1885 | (image->colors > 256) || (image->matte != MagickFalse)) |
| 1886 | { |
| 1887 | /* |
| 1888 | Dump DirectClass image. |
| 1889 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1890 | (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g %.20g\n0\n%d\n", |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1891 | (double) image->columns,(double) image->rows, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1892 | image_info->compression == RLECompression ? 1 : 0); |
| 1893 | (void) WriteBlobString(image,buffer); |
| 1894 | switch (image_info->compression) |
| 1895 | { |
| 1896 | case RLECompression: |
| 1897 | { |
| 1898 | /* |
| 1899 | Dump runlength-encoded DirectColor packets. |
| 1900 | */ |
| 1901 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1902 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1903 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1904 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1905 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1906 | break; |
| cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1907 | GetPixelInfoPixel(image,p,&pixel); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1908 | length=255; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1909 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1910 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1911 | if ((GetPixelRed(image,p) == pixel.red) && |
| 1912 | (GetPixelGreen(image,p) == pixel.green) && |
| 1913 | (GetPixelBlue(image,p) == pixel.blue) && |
| 1914 | (GetPixelAlpha(image,p) == pixel.alpha) && |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1915 | (length < 255) && (x < (ssize_t) (image->columns-1))) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1916 | length++; |
| 1917 | else |
| 1918 | { |
| 1919 | if (x > 0) |
| 1920 | { |
| 1921 | WriteRunlengthPacket(image,pixel,length,p); |
| 1922 | if ((q-pixels+10) >= 80) |
| 1923 | { |
| 1924 | *q++='\n'; |
| 1925 | (void) WriteBlob(image,q-pixels,pixels); |
| 1926 | q=pixels; |
| 1927 | } |
| 1928 | } |
| 1929 | length=0; |
| 1930 | } |
| cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1931 | GetPixelInfoPixel(image,p,&pixel); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1932 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1933 | } |
| 1934 | WriteRunlengthPacket(image,pixel,length,p); |
| 1935 | if ((q-pixels+10) >= 80) |
| 1936 | { |
| 1937 | *q++='\n'; |
| 1938 | (void) WriteBlob(image,q-pixels,pixels); |
| 1939 | q=pixels; |
| 1940 | } |
| 1941 | if (image->previous == (Image *) NULL) |
| 1942 | { |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1943 | status=SetImageProgress(image,SaveImageTag, |
| 1944 | (MagickOffsetType) y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1945 | if (status == MagickFalse) |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | if (q != pixels) |
| 1950 | { |
| 1951 | *q++='\n'; |
| 1952 | (void) WriteBlob(image,q-pixels,pixels); |
| 1953 | } |
| 1954 | break; |
| 1955 | } |
| 1956 | case NoCompression: |
| 1957 | default: |
| 1958 | { |
| 1959 | /* |
| 1960 | Dump uncompressed DirectColor packets. |
| 1961 | */ |
| 1962 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1963 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1964 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1965 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1966 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1967 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1968 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1969 | { |
| 1970 | if ((image->matte != MagickFalse) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1971 | (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1972 | { |
| 1973 | q=PopHexPixel(hex_digits,0xff,q); |
| 1974 | q=PopHexPixel(hex_digits,0xff,q); |
| 1975 | q=PopHexPixel(hex_digits,0xff,q); |
| 1976 | } |
| 1977 | else |
| 1978 | { |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1979 | q=PopHexPixel(hex_digits,ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1980 | GetPixelRed(image,p)),q); |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1981 | q=PopHexPixel(hex_digits,ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1982 | GetPixelGreen(image,p)),q); |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1983 | q=PopHexPixel(hex_digits,ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1984 | GetPixelBlue(image,p)),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1985 | } |
| 1986 | if ((q-pixels+6) >= 80) |
| 1987 | { |
| 1988 | *q++='\n'; |
| 1989 | (void) WriteBlob(image,q-pixels,pixels); |
| 1990 | q=pixels; |
| 1991 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1992 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1993 | } |
| 1994 | if (image->previous == (Image *) NULL) |
| 1995 | { |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 1996 | status=SetImageProgress(image,SaveImageTag, |
| 1997 | (MagickOffsetType) y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1998 | if (status == MagickFalse) |
| 1999 | break; |
| 2000 | } |
| 2001 | } |
| 2002 | if (q != pixels) |
| 2003 | { |
| 2004 | *q++='\n'; |
| 2005 | (void) WriteBlob(image,q-pixels,pixels); |
| 2006 | } |
| 2007 | break; |
| 2008 | } |
| 2009 | } |
| 2010 | (void) WriteBlobByte(image,'\n'); |
| 2011 | } |
| 2012 | else |
| 2013 | { |
| 2014 | /* |
| 2015 | Dump PseudoClass image. |
| 2016 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2017 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2018 | "%.20g %.20g\n%d\n%d\n0\n",(double) image->columns,(double) |
| 2019 | image->rows,image->storage_class == PseudoClass ? 1 : 0, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2020 | image_info->compression == RLECompression ? 1 : 0); |
| 2021 | (void) WriteBlobString(image,buffer); |
| 2022 | /* |
| 2023 | Dump number of colors and colormap. |
| 2024 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2025 | (void) FormatLocaleString(buffer,MaxTextExtent,"%.20g\n",(double) |
| cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 2026 | image->colors); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2027 | (void) WriteBlobString(image,buffer); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2028 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2029 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2030 | (void) FormatLocaleString(buffer,MaxTextExtent,"%02X%02X%02X\n", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2031 | ScaleQuantumToChar(image->colormap[i].red), |
| 2032 | ScaleQuantumToChar(image->colormap[i].green), |
| 2033 | ScaleQuantumToChar(image->colormap[i].blue)); |
| 2034 | (void) WriteBlobString(image,buffer); |
| 2035 | } |
| 2036 | switch (image_info->compression) |
| 2037 | { |
| 2038 | case RLECompression: |
| 2039 | { |
| 2040 | /* |
| 2041 | Dump runlength-encoded PseudoColor packets. |
| 2042 | */ |
| 2043 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2044 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2045 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2046 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2047 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2048 | break; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2049 | index=GetPixelIndex(image,p); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2050 | length=255; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2051 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2052 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2053 | if ((index == GetPixelIndex(image,p)) && |
| cristy | 802d364 | 2011-04-27 02:02:41 +0000 | [diff] [blame] | 2054 | (length < 255) && (x < ((ssize_t) image->columns-1))) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2055 | length++; |
| 2056 | else |
| 2057 | { |
| 2058 | if (x > 0) |
| 2059 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 2060 | q=PopHexPixel(hex_digits,(size_t) index,q); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2061 | q=PopHexPixel(hex_digits,(size_t) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2062 | MagickMin(length,0xff),q); |
| 2063 | i++; |
| 2064 | if ((q-pixels+6) >= 80) |
| 2065 | { |
| 2066 | *q++='\n'; |
| 2067 | (void) WriteBlob(image,q-pixels,pixels); |
| 2068 | q=pixels; |
| 2069 | } |
| 2070 | } |
| 2071 | length=0; |
| 2072 | } |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2073 | index=GetPixelIndex(image,p); |
| 2074 | pixel.red=GetPixelRed(image,p); |
| 2075 | pixel.green=GetPixelGreen(image,p); |
| 2076 | pixel.blue=GetPixelBlue(image,p); |
| 2077 | pixel.alpha=GetPixelAlpha(image,p); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2078 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2079 | } |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 2080 | q=PopHexPixel(hex_digits,(size_t) index,q); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2081 | q=PopHexPixel(hex_digits,(size_t) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2082 | MagickMin(length,0xff),q); |
| 2083 | if (image->previous == (Image *) NULL) |
| 2084 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 2085 | status=SetImageProgress(image,SaveImageTag, |
| 2086 | (MagickOffsetType) y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2087 | if (status == MagickFalse) |
| 2088 | break; |
| 2089 | } |
| 2090 | } |
| 2091 | if (q != pixels) |
| 2092 | { |
| 2093 | *q++='\n'; |
| 2094 | (void) WriteBlob(image,q-pixels,pixels); |
| 2095 | } |
| 2096 | break; |
| 2097 | } |
| 2098 | case NoCompression: |
| 2099 | default: |
| 2100 | { |
| 2101 | /* |
| 2102 | Dump uncompressed PseudoColor packets. |
| 2103 | */ |
| 2104 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2105 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2106 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 2107 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2108 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2109 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2110 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2111 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2112 | q=PopHexPixel(hex_digits,(size_t) GetPixelIndex(image,p),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2113 | if ((q-pixels+4) >= 80) |
| 2114 | { |
| 2115 | *q++='\n'; |
| 2116 | (void) WriteBlob(image,q-pixels,pixels); |
| 2117 | q=pixels; |
| 2118 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2119 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2120 | } |
| 2121 | if (image->previous == (Image *) NULL) |
| 2122 | { |
| cristy | a97426c | 2011-02-04 01:41:27 +0000 | [diff] [blame] | 2123 | status=SetImageProgress(image,SaveImageTag, |
| 2124 | (MagickOffsetType) y,image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2125 | if (status == MagickFalse) |
| 2126 | break; |
| 2127 | } |
| 2128 | } |
| 2129 | if (q != pixels) |
| 2130 | { |
| 2131 | *q++='\n'; |
| 2132 | (void) WriteBlob(image,q-pixels,pixels); |
| 2133 | } |
| 2134 | break; |
| 2135 | } |
| 2136 | } |
| 2137 | (void) WriteBlobByte(image,'\n'); |
| 2138 | } |
| 2139 | if (LocaleCompare(image_info->magick,"PS") != 0) |
| 2140 | (void) WriteBlobString(image,"end\n"); |
| 2141 | (void) WriteBlobString(image,"%%PageTrailer\n"); |
| 2142 | if (GetNextImageInList(image) == (Image *) NULL) |
| 2143 | break; |
| 2144 | image=SyncNextImageInList(image); |
| 2145 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 2146 | GetImageListLength(image)); |
| 2147 | if (status == MagickFalse) |
| 2148 | break; |
| 2149 | } while (image_info->adjoin != MagickFalse); |
| 2150 | (void) WriteBlobString(image,"%%Trailer\n"); |
| 2151 | if (page > 2) |
| 2152 | { |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2153 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2154 | "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5), |
| 2155 | ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2156 | (void) WriteBlobString(image,buffer); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2157 | (void) FormatLocaleString(buffer,MaxTextExtent, |
| cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 2158 | "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1, |
| cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 2159 | bounds.x2,bounds.y2); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2160 | (void) WriteBlobString(image,buffer); |
| 2161 | } |
| 2162 | (void) WriteBlobString(image,"%%EOF\n"); |
| 2163 | (void) CloseBlob(image); |
| 2164 | return(MagickTrue); |
| 2165 | } |