PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/coders/ps.c

https://gitlab.com/ImageMagick/ImageMagick
C | 2311 lines | 1919 code | 84 blank | 308 comment | 452 complexity | cdd1d70af24fe8f16b47fe67a77d5aa9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  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. % Cristy %
  17. % July 1992 %
  18. % %
  19. % %
  20. % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
  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. % https://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. Include declarations.
  40. */
  41. #include "MagickCore/studio.h"
  42. #include "MagickCore/artifact.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"
  50. #include "MagickCore/colorspace-private.h"
  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/module.h"
  64. #include "MagickCore/monitor.h"
  65. #include "MagickCore/monitor-private.h"
  66. #include "MagickCore/nt-base-private.h"
  67. #include "MagickCore/option.h"
  68. #include "MagickCore/profile.h"
  69. #include "MagickCore/resource_.h"
  70. #include "MagickCore/pixel-accessor.h"
  71. #include "MagickCore/property.h"
  72. #include "MagickCore/quantum-private.h"
  73. #include "MagickCore/static.h"
  74. #include "MagickCore/string_.h"
  75. #include "MagickCore/timer-private.h"
  76. #include "MagickCore/token.h"
  77. #include "MagickCore/transform.h"
  78. #include "MagickCore/utility.h"
  79. /*
  80. Forward declarations.
  81. */
  82. static MagickBooleanType
  83. WritePSImage(const ImageInfo *,Image *,ExceptionInfo *);
  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. %
  95. % InvokePostscriptDelegate() executes the Postscript interpreter with the
  96. % specified command.
  97. %
  98. % The format of the InvokePostscriptDelegate method is:
  99. %
  100. % MagickBooleanType InvokePostscriptDelegate(
  101. % const MagickBooleanType verbose,const char *command,
  102. % ExceptionInfo *exception)
  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. %
  112. % o exception: return any errors or warnings in this structure.
  113. %
  114. */
  115. #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
  116. static int MagickDLLCall PostscriptDelegateMessage(void *handle,
  117. const char *message,int length)
  118. {
  119. char
  120. **messages;
  121. ssize_t
  122. offset;
  123. offset=0;
  124. messages=(char **) handle;
  125. if (*messages == (char *) NULL)
  126. *messages=(char *) AcquireQuantumMemory((size_t) length+1,sizeof(char *));
  127. else
  128. {
  129. offset=(ssize_t) strlen(*messages);
  130. *messages=(char *) ResizeQuantumMemory(*messages,(size_t) offset+length+1,
  131. sizeof(char *));
  132. }
  133. if (*messages == (char *) NULL)
  134. return(0);
  135. (void) memcpy(*messages+offset,message,(size_t) length);
  136. (*messages)[length+offset] ='\0';
  137. return(length);
  138. }
  139. #endif
  140. static MagickBooleanType InvokePostscriptDelegate(
  141. const MagickBooleanType verbose,const char *command,char *message,
  142. ExceptionInfo *exception)
  143. {
  144. int
  145. status;
  146. #if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
  147. #define SetArgsStart(command,args_start) \
  148. if (args_start == (const char *) NULL) \
  149. { \
  150. if (*command != '"') \
  151. args_start=strchr(command,' '); \
  152. else \
  153. { \
  154. args_start=strchr(command+1,'"'); \
  155. if (args_start != (const char *) NULL) \
  156. args_start++; \
  157. } \
  158. }
  159. #define ExecuteGhostscriptCommand(command,status) \
  160. { \
  161. status=ExternalDelegateCommand(MagickFalse,verbose,command,message, \
  162. exception); \
  163. if (status == 0) \
  164. return(MagickTrue); \
  165. if (status < 0) \
  166. return(MagickFalse); \
  167. (void) ThrowMagickException(exception,GetMagickModule(),DelegateError, \
  168. "FailedToExecuteCommand","`%s' (%d)",command,status); \
  169. return(MagickFalse); \
  170. }
  171. char
  172. **argv,
  173. *errors;
  174. const char
  175. *args_start = (const char *) NULL;
  176. const GhostInfo
  177. *ghost_info;
  178. gs_main_instance
  179. *interpreter;
  180. gsapi_revision_t
  181. revision;
  182. int
  183. argc,
  184. code;
  185. register ssize_t
  186. i;
  187. #if defined(MAGICKCORE_WINDOWS_SUPPORT)
  188. ghost_info=NTGhostscriptDLLVectors();
  189. #else
  190. GhostInfo
  191. ghost_info_struct;
  192. ghost_info=(&ghost_info_struct);
  193. (void) memset(&ghost_info_struct,0,sizeof(ghost_info_struct));
  194. ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
  195. gsapi_delete_instance;
  196. ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
  197. ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
  198. gsapi_new_instance;
  199. ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
  200. gsapi_init_with_args;
  201. ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
  202. int *)) gsapi_run_string;
  203. ghost_info_struct.set_stdio=(int (*)(gs_main_instance *,int (*)(void *,char *,
  204. int),int (*)(void *,const char *,int),int (*)(void *, const char *, int)))
  205. gsapi_set_stdio;
  206. ghost_info_struct.revision=(int (*)(gsapi_revision_t *,int)) gsapi_revision;
  207. #endif
  208. if (ghost_info == (GhostInfo *) NULL)
  209. ExecuteGhostscriptCommand(command,status);
  210. if ((ghost_info->revision)(&revision,(int) sizeof(revision)) != 0)
  211. revision.revision=0;
  212. if (verbose != MagickFalse)
  213. {
  214. (void) fprintf(stdout,"[ghostscript library %.2f]",(double)
  215. revision.revision/100.0);
  216. SetArgsStart(command,args_start);
  217. (void) fputs(args_start,stdout);
  218. }
  219. interpreter=(gs_main_instance *) NULL;
  220. errors=(char *) NULL;
  221. status=(ghost_info->new_instance)(&interpreter,(void *) &errors);
  222. if (status < 0)
  223. ExecuteGhostscriptCommand(command,status);
  224. code=0;
  225. argv=StringToArgv(command,&argc);
  226. if (argv == (char **) NULL)
  227. {
  228. (ghost_info->delete_instance)(interpreter);
  229. return(MagickFalse);
  230. }
  231. (void) (ghost_info->set_stdio)(interpreter,(int (MagickDLLCall *)(void *,
  232. char *,int)) NULL,PostscriptDelegateMessage,PostscriptDelegateMessage);
  233. status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
  234. if (status == 0)
  235. status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
  236. 0,&code);
  237. (ghost_info->exit)(interpreter);
  238. (ghost_info->delete_instance)(interpreter);
  239. for (i=0; i < (ssize_t) argc; i++)
  240. argv[i]=DestroyString(argv[i]);
  241. argv=(char **) RelinquishMagickMemory(argv);
  242. if (status != 0)
  243. {
  244. SetArgsStart(command,args_start);
  245. if (status == -101) /* quit */
  246. (void) FormatLocaleString(message,MagickPathExtent,
  247. "[ghostscript library %.2f]%s: %s",(double) revision.revision/100.0,
  248. args_start,errors);
  249. else
  250. {
  251. (void) ThrowMagickException(exception,GetMagickModule(),
  252. DelegateError,"PostscriptDelegateFailed",
  253. "`[ghostscript library %.2f]%s': %s",(double) revision.revision/
  254. 100.0,args_start,errors);
  255. if (errors != (char *) NULL)
  256. errors=DestroyString(errors);
  257. (void) LogMagickEvent(CoderEvent,GetMagickModule(),
  258. "Ghostscript returns status %d, exit code %d",status,code);
  259. return(MagickFalse);
  260. }
  261. }
  262. if (errors != (char *) NULL)
  263. errors=DestroyString(errors);
  264. return(MagickTrue);
  265. #else
  266. status=ExternalDelegateCommand(MagickFalse,verbose,command,message,exception);
  267. return(status == 0 ? MagickTrue : MagickFalse);
  268. #endif
  269. }
  270. /*
  271. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  272. % %
  273. % %
  274. % %
  275. % I s P S %
  276. % %
  277. % %
  278. % %
  279. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  280. %
  281. % IsPS() returns MagickTrue if the image format type, identified by the
  282. % magick string, is PS.
  283. %
  284. % The format of the IsPS method is:
  285. %
  286. % MagickBooleanType IsPS(const unsigned char *magick,const size_t length)
  287. %
  288. % A description of each parameter follows:
  289. %
  290. % o magick: compare image format pattern against these bytes.
  291. %
  292. % o length: Specifies the length of the magick string.
  293. %
  294. */
  295. static MagickBooleanType IsPS(const unsigned char *magick,const size_t length)
  296. {
  297. if (length < 4)
  298. return(MagickFalse);
  299. if (memcmp(magick,"%!",2) == 0)
  300. return(MagickTrue);
  301. if (memcmp(magick,"\004%!",3) == 0)
  302. return(MagickTrue);
  303. return(MagickFalse);
  304. }
  305. /*
  306. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  307. % %
  308. % %
  309. % %
  310. % R e a d P S I m a g e %
  311. % %
  312. % %
  313. % %
  314. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  315. %
  316. % ReadPSImage() reads a Postscript image file and returns it. It allocates
  317. % the memory necessary for the new Image structure and returns a pointer
  318. % to the new image.
  319. %
  320. % The format of the ReadPSImage method is:
  321. %
  322. % Image *ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  323. %
  324. % A description of each parameter follows:
  325. %
  326. % o image_info: the image info.
  327. %
  328. % o exception: return any errors or warnings in this structure.
  329. %
  330. */
  331. static MagickBooleanType IsPostscriptRendered(const char *path)
  332. {
  333. MagickBooleanType
  334. status;
  335. struct stat
  336. attributes;
  337. if ((path == (const char *) NULL) || (*path == '\0'))
  338. return(MagickFalse);
  339. status=GetPathAttributes(path,&attributes);
  340. if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
  341. (attributes.st_size > 0))
  342. return(MagickTrue);
  343. return(MagickFalse);
  344. }
  345. static inline int ProfileInteger(Image *image,short int *hex_digits)
  346. {
  347. int
  348. c,
  349. l,
  350. value;
  351. register ssize_t
  352. i;
  353. l=0;
  354. value=0;
  355. for (i=0; i < 2; )
  356. {
  357. c=ReadBlobByte(image);
  358. if ((c == EOF) || ((c == '%') && (l == '%')))
  359. {
  360. value=(-1);
  361. break;
  362. }
  363. l=c;
  364. c&=0xff;
  365. if (isxdigit(c) == MagickFalse)
  366. continue;
  367. value=(int) ((size_t) value << 4)+hex_digits[c];
  368. i++;
  369. }
  370. return(value);
  371. }
  372. static Image *ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception)
  373. {
  374. #define BoundingBox "BoundingBox:"
  375. #define BeginDocument "BeginDocument:"
  376. #define BeginXMPPacket "<?xpacket begin="
  377. #define EndXMPPacket "<?xpacket end="
  378. #define ICCProfile "BeginICCProfile:"
  379. #define CMYKCustomColor "CMYKCustomColor:"
  380. #define CMYKProcessColor "CMYKProcessColor:"
  381. #define DocumentMedia "DocumentMedia:"
  382. #define DocumentCustomColors "DocumentCustomColors:"
  383. #define DocumentProcessColors "DocumentProcessColors:"
  384. #define EndDocument "EndDocument:"
  385. #define HiResBoundingBox "HiResBoundingBox:"
  386. #define ImageData "ImageData:"
  387. #define PageBoundingBox "PageBoundingBox:"
  388. #define LanguageLevel "LanguageLevel:"
  389. #define PageMedia "PageMedia:"
  390. #define Pages "Pages:"
  391. #define PhotoshopProfile "BeginPhotoshop:"
  392. #define PostscriptLevel "!PS-"
  393. #define RenderPostscriptText " Rendering Postscript... "
  394. #define SpotColor "+ "
  395. char
  396. command[MagickPathExtent],
  397. *density,
  398. filename[MagickPathExtent],
  399. geometry[MagickPathExtent],
  400. input_filename[MagickPathExtent],
  401. message[MagickPathExtent],
  402. *options,
  403. postscript_filename[MagickPathExtent];
  404. const char
  405. *option;
  406. const DelegateInfo
  407. *delegate_info;
  408. GeometryInfo
  409. geometry_info;
  410. Image
  411. *image,
  412. *next,
  413. *postscript_image;
  414. ImageInfo
  415. *read_info;
  416. int
  417. c,
  418. file;
  419. MagickBooleanType
  420. cmyk,
  421. fitPage,
  422. skip,
  423. status;
  424. MagickStatusType
  425. flags;
  426. PointInfo
  427. delta,
  428. resolution;
  429. RectangleInfo
  430. page;
  431. register char
  432. *p;
  433. register ssize_t
  434. i;
  435. SegmentInfo
  436. bounds,
  437. hires_bounds;
  438. short int
  439. hex_digits[256];
  440. size_t
  441. length;
  442. ssize_t
  443. count,
  444. priority;
  445. StringInfo
  446. *profile;
  447. unsigned long
  448. columns,
  449. extent,
  450. language_level,
  451. pages,
  452. rows,
  453. scene,
  454. spotcolor;
  455. /*
  456. Open image file.
  457. */
  458. assert(image_info != (const ImageInfo *) NULL);
  459. assert(image_info->signature == MagickCoreSignature);
  460. if (image_info->debug != MagickFalse)
  461. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  462. image_info->filename);
  463. assert(exception != (ExceptionInfo *) NULL);
  464. assert(exception->signature == MagickCoreSignature);
  465. image=AcquireImage(image_info,exception);
  466. status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
  467. if (status == MagickFalse)
  468. {
  469. image=DestroyImageList(image);
  470. return((Image *) NULL);
  471. }
  472. status=AcquireUniqueSymbolicLink(image_info->filename,input_filename);
  473. if (status == MagickFalse)
  474. {
  475. ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
  476. image_info->filename);
  477. image=DestroyImageList(image);
  478. return((Image *) NULL);
  479. }
  480. /*
  481. Initialize hex values.
  482. */
  483. (void) memset(hex_digits,0,sizeof(hex_digits));
  484. hex_digits[(int) '0']=0;
  485. hex_digits[(int) '1']=1;
  486. hex_digits[(int) '2']=2;
  487. hex_digits[(int) '3']=3;
  488. hex_digits[(int) '4']=4;
  489. hex_digits[(int) '5']=5;
  490. hex_digits[(int) '6']=6;
  491. hex_digits[(int) '7']=7;
  492. hex_digits[(int) '8']=8;
  493. hex_digits[(int) '9']=9;
  494. hex_digits[(int) 'a']=10;
  495. hex_digits[(int) 'b']=11;
  496. hex_digits[(int) 'c']=12;
  497. hex_digits[(int) 'd']=13;
  498. hex_digits[(int) 'e']=14;
  499. hex_digits[(int) 'f']=15;
  500. hex_digits[(int) 'A']=10;
  501. hex_digits[(int) 'B']=11;
  502. hex_digits[(int) 'C']=12;
  503. hex_digits[(int) 'D']=13;
  504. hex_digits[(int) 'E']=14;
  505. hex_digits[(int) 'F']=15;
  506. /*
  507. Set the page density.
  508. */
  509. delta.x=DefaultResolution;
  510. delta.y=DefaultResolution;
  511. if ((image->resolution.x == 0.0) || (image->resolution.y == 0.0))
  512. {
  513. flags=ParseGeometry(PSDensityGeometry,&geometry_info);
  514. image->resolution.x=geometry_info.rho;
  515. image->resolution.y=geometry_info.sigma;
  516. if ((flags & SigmaValue) == 0)
  517. image->resolution.y=image->resolution.x;
  518. }
  519. if (image_info->density != (char *) NULL)
  520. {
  521. flags=ParseGeometry(image_info->density,&geometry_info);
  522. image->resolution.x=geometry_info.rho;
  523. image->resolution.y=geometry_info.sigma;
  524. if ((flags & SigmaValue) == 0)
  525. image->resolution.y=image->resolution.x;
  526. }
  527. (void) ParseAbsoluteGeometry(PSPageGeometry,&page);
  528. if (image_info->page != (char *) NULL)
  529. (void) ParseAbsoluteGeometry(image_info->page,&page);
  530. resolution=image->resolution;
  531. page.width=(size_t) ceil((double) (page.width*resolution.x/delta.x)-0.5);
  532. page.height=(size_t) ceil((double) (page.height*resolution.y/delta.y)-0.5);
  533. /*
  534. Determine page geometry from the Postscript bounding box.
  535. */
  536. (void) memset(&bounds,0,sizeof(bounds));
  537. (void) memset(command,0,sizeof(command));
  538. cmyk=image_info->colorspace == CMYKColorspace ? MagickTrue : MagickFalse;
  539. (void) memset(&hires_bounds,0,sizeof(hires_bounds));
  540. columns=0;
  541. rows=0;
  542. priority=0;
  543. rows=0;
  544. extent=0;
  545. spotcolor=0;
  546. language_level=1;
  547. pages=(~0UL);
  548. skip=MagickFalse;
  549. p=command;
  550. for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
  551. {
  552. /*
  553. Note document structuring comments.
  554. */
  555. *p++=(char) c;
  556. if ((strchr("\n\r%",c) == (char *) NULL) &&
  557. ((size_t) (p-command) < (MagickPathExtent-1)))
  558. continue;
  559. *p='\0';
  560. p=command;
  561. /*
  562. Skip %%BeginDocument thru %%EndDocument.
  563. */
  564. if (LocaleNCompare(BeginDocument,command,strlen(BeginDocument)) == 0)
  565. skip=MagickTrue;
  566. if (LocaleNCompare(EndDocument,command,strlen(EndDocument)) == 0)
  567. skip=MagickFalse;
  568. if (skip != MagickFalse)
  569. continue;
  570. if (LocaleNCompare(PostscriptLevel,command,strlen(PostscriptLevel)) == 0)
  571. {
  572. (void) SetImageProperty(image,"ps:Level",command+4,exception);
  573. if (GlobExpression(command,"*EPSF-*",MagickTrue) != MagickFalse)
  574. pages=1;
  575. }
  576. if (LocaleNCompare(LanguageLevel,command,strlen(LanguageLevel)) == 0)
  577. (void) sscanf(command,LanguageLevel " %lu",&language_level);
  578. if (LocaleNCompare(Pages,command,strlen(Pages)) == 0)
  579. (void) sscanf(command,Pages " %lu",&pages);
  580. if (LocaleNCompare(ImageData,command,strlen(ImageData)) == 0)
  581. (void) sscanf(command,ImageData " %lu %lu",&columns,&rows);
  582. /*
  583. Is this a CMYK document?
  584. */
  585. length=strlen(DocumentProcessColors);
  586. if (LocaleNCompare(DocumentProcessColors,command,length) == 0)
  587. {
  588. if ((GlobExpression(command,"*Cyan*",MagickTrue) != MagickFalse) ||
  589. (GlobExpression(command,"*Magenta*",MagickTrue) != MagickFalse) ||
  590. (GlobExpression(command,"*Yellow*",MagickTrue) != MagickFalse))
  591. cmyk=MagickTrue;
  592. }
  593. if (LocaleNCompare(CMYKCustomColor,command,strlen(CMYKCustomColor)) == 0)
  594. cmyk=MagickTrue;
  595. if (LocaleNCompare(CMYKProcessColor,command,strlen(CMYKProcessColor)) == 0)
  596. cmyk=MagickTrue;
  597. length=strlen(DocumentCustomColors);
  598. if ((LocaleNCompare(DocumentCustomColors,command,length) == 0) ||
  599. (LocaleNCompare(CMYKCustomColor,command,strlen(CMYKCustomColor)) == 0) ||
  600. (LocaleNCompare(SpotColor,command,strlen(SpotColor)) == 0))
  601. {
  602. char
  603. property[MagickPathExtent],
  604. *value;
  605. register char
  606. *q;
  607. /*
  608. Note spot names.
  609. */
  610. (void) FormatLocaleString(property,MagickPathExtent,
  611. "ps:SpotColor-%.20g",(double) (spotcolor++));
  612. for (q=command; *q != '\0'; q++)
  613. if (isspace((int) (unsigned char) *q) != 0)
  614. break;
  615. value=ConstantString(q);
  616. (void) SubstituteString(&value,"(","");
  617. (void) SubstituteString(&value,")","");
  618. (void) StripString(value);
  619. if (*value != '\0')
  620. (void) SetImageProperty(image,property,value,exception);
  621. value=DestroyString(value);
  622. continue;
  623. }
  624. if (image_info->page != (char *) NULL)
  625. continue;
  626. /*
  627. Note region defined by bounding box.
  628. */
  629. count=0;
  630. i=0;
  631. if (LocaleNCompare(BoundingBox,command,strlen(BoundingBox)) == 0)
  632. {
  633. count=(ssize_t) sscanf(command,BoundingBox " %lf %lf %lf %lf",
  634. &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
  635. i=2;
  636. }
  637. if (LocaleNCompare(DocumentMedia,command,strlen(DocumentMedia)) == 0)
  638. {
  639. count=(ssize_t) sscanf(command,DocumentMedia " %lf %lf %lf %lf",
  640. &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
  641. i=1;
  642. }
  643. if (LocaleNCompare(HiResBoundingBox,command,strlen(HiResBoundingBox)) == 0)
  644. {
  645. count=(ssize_t) sscanf(command,HiResBoundingBox " %lf %lf %lf %lf",
  646. &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
  647. i=3;
  648. }
  649. if (LocaleNCompare(PageBoundingBox,command,strlen(PageBoundingBox)) == 0)
  650. {
  651. count=(ssize_t) sscanf(command,PageBoundingBox " %lf %lf %lf %lf",
  652. &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
  653. i=1;
  654. }
  655. if (LocaleNCompare(PageMedia,command,strlen(PageMedia)) == 0)
  656. {
  657. count=(ssize_t) sscanf(command,PageMedia " %lf %lf %lf %lf",
  658. &bounds.x1,&bounds.y1,&bounds.x2,&bounds.y2);
  659. i=1;
  660. }
  661. if ((count != 4) || (i < (ssize_t) priority))
  662. continue;
  663. if ((fabs(bounds.x2-bounds.x1) <= fabs(hires_bounds.x2-hires_bounds.x1)) ||
  664. (fabs(bounds.y2-bounds.y1) <= fabs(hires_bounds.y2-hires_bounds.y1)))
  665. if (i == (ssize_t) priority)
  666. continue;
  667. hires_bounds=bounds;
  668. priority=i;
  669. }
  670. if ((fabs(hires_bounds.x2-hires_bounds.x1) >= MagickEpsilon) &&
  671. (fabs(hires_bounds.y2-hires_bounds.y1) >= MagickEpsilon))
  672. {
  673. /*
  674. Set Postscript render geometry.
  675. */
  676. (void) FormatLocaleString(geometry,MagickPathExtent,"%gx%g%+.15g%+.15g",
  677. hires_bounds.x2-hires_bounds.x1,hires_bounds.y2-hires_bounds.y1,
  678. hires_bounds.x1,hires_bounds.y1);
  679. (void) SetImageProperty(image,"ps:HiResBoundingBox",geometry,exception);
  680. page.width=(size_t) ceil((double) ((hires_bounds.x2-hires_bounds.x1)*
  681. resolution.x/delta.x)-0.5);
  682. page.height=(size_t) ceil((double) ((hires_bounds.y2-hires_bounds.y1)*
  683. resolution.y/delta.y)-0.5);
  684. }
  685. fitPage=MagickFalse;
  686. option=GetImageOption(image_info,"eps:fit-page");
  687. if (option != (char *) NULL)
  688. {
  689. char
  690. *page_geometry;
  691. page_geometry=GetPageGeometry(option);
  692. flags=ParseMetaGeometry(page_geometry,&page.x,&page.y,&page.width,
  693. &page.height);
  694. if (flags == NoValue)
  695. {
  696. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  697. "InvalidGeometry","`%s'",option);
  698. image=DestroyImage(image);
  699. return((Image *) NULL);
  700. }
  701. page.width=(size_t) ceil((double) (page.width*image->resolution.x/delta.x)
  702. -0.5);
  703. page.height=(size_t) ceil((double) (page.height*image->resolution.y/
  704. delta.y) -0.5);
  705. page_geometry=DestroyString(page_geometry);
  706. fitPage=MagickTrue;
  707. }
  708. if (IssRGBCompatibleColorspace(image_info->colorspace) != MagickFalse)
  709. cmyk=MagickFalse;
  710. /*
  711. Create Ghostscript control file.
  712. */
  713. file=AcquireUniqueFileResource(postscript_filename);
  714. if (file == -1)
  715. {
  716. ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
  717. image_info->filename);
  718. image=DestroyImageList(image);
  719. return((Image *) NULL);
  720. }
  721. (void) CopyMagickString(command,"/setpagedevice {pop} bind 1 index where {"
  722. "dup wcheck {3 1 roll put} {pop def} ifelse} {def} ifelse\n"
  723. "<</UseCIEColor true>>setpagedevice\n",MagickPathExtent);
  724. count=write(file,command,(unsigned int) strlen(command));
  725. if (image_info->page == (char *) NULL)
  726. {
  727. char
  728. translate_geometry[MagickPathExtent];
  729. (void) FormatLocaleString(translate_geometry,MagickPathExtent,
  730. "%g %g translate\n",-bounds.x1,-bounds.y1);
  731. count=write(file,translate_geometry,(unsigned int)
  732. strlen(translate_geometry));
  733. }
  734. file=close(file)-1;
  735. /*
  736. Render Postscript with the Ghostscript delegate.
  737. */
  738. if (image_info->monochrome != MagickFalse)
  739. delegate_info=GetDelegateInfo("ps:mono",(char *) NULL,exception);
  740. else
  741. if (cmyk != MagickFalse)
  742. delegate_info=GetDelegateInfo("ps:cmyk",(char *) NULL,exception);
  743. else
  744. delegate_info=GetDelegateInfo("ps:alpha",(char *) NULL,exception);
  745. if (delegate_info == (const DelegateInfo *) NULL)
  746. {
  747. (void) RelinquishUniqueFileResource(postscript_filename);
  748. image=DestroyImageList(image);
  749. return((Image *) NULL);
  750. }
  751. density=AcquireString("");
  752. options=AcquireString("");
  753. (void) FormatLocaleString(density,MagickPathExtent,"%gx%g",resolution.x,
  754. resolution.y);
  755. (void) FormatLocaleString(options,MagickPathExtent,"-g%.20gx%.20g ",(double)
  756. page.width,(double) page.height);
  757. read_info=CloneImageInfo(image_info);
  758. *read_info->magick='\0';
  759. if (read_info->number_scenes != 0)
  760. {
  761. char
  762. pages[MagickPathExtent];
  763. (void) FormatLocaleString(pages,MagickPathExtent,"-dFirstPage=%.20g "
  764. "-dLastPage=%.20g ",(double) read_info->scene+1,(double)
  765. (read_info->scene+read_info->number_scenes));
  766. (void) ConcatenateMagickString(options,pages,MagickPathExtent);
  767. read_info->number_scenes=0;
  768. if (read_info->scenes != (char *) NULL)
  769. *read_info->scenes='\0';
  770. }
  771. if (*image_info->magick == 'E')
  772. {
  773. option=GetImageOption(image_info,"eps:use-cropbox");
  774. if ((option == (const char *) NULL) ||
  775. (IsStringTrue(option) != MagickFalse))
  776. (void) ConcatenateMagickString(options,"-dEPSCrop ",MagickPathExtent);
  777. if (fitPage != MagickFalse)
  778. (void) ConcatenateMagickString(options,"-dEPSFitPage ",
  779. MagickPathExtent);
  780. }
  781. (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
  782. (void) AcquireUniqueFilename(filename);
  783. (void) RelinquishUniqueFileResource(filename);
  784. (void) ConcatenateMagickString(filename,"%d",MagickPathExtent);
  785. (void) FormatLocaleString(command,MagickPathExtent,
  786. GetDelegateCommands(delegate_info),
  787. read_info->antialias != MagickFalse ? 4 : 1,
  788. read_info->antialias != MagickFalse ? 4 : 1,density,options,filename,
  789. postscript_filename,input_filename);
  790. options=DestroyString(options);
  791. density=DestroyString(density);
  792. *message='\0';
  793. status=InvokePostscriptDelegate(read_info->verbose,command,message,exception);
  794. (void) InterpretImageFilename(image_info,image,filename,1,
  795. read_info->filename,exception);
  796. if ((status == MagickFalse) ||
  797. (IsPostscriptRendered(read_info->filename) == MagickFalse))
  798. {
  799. (void) ConcatenateMagickString(command," -c showpage",MagickPathExtent);
  800. status=InvokePostscriptDelegate(read_info->verbose,command,message,
  801. exception);
  802. }
  803. (void) RelinquishUniqueFileResource(postscript_filename);
  804. (void) RelinquishUniqueFileResource(input_filename);
  805. postscript_image=(Image *) NULL;
  806. if (status == MagickFalse)
  807. for (i=1; ; i++)
  808. {
  809. (void) InterpretImageFilename(image_info,image,filename,(int) i,
  810. read_info->filename,exception);
  811. if (IsPostscriptRendered(read_info->filename) == MagickFalse)
  812. break;
  813. (void) RelinquishUniqueFileResource(read_info->filename);
  814. }
  815. else
  816. for (i=1; ; i++)
  817. {
  818. (void) InterpretImageFilename(image_info,image,filename,(int) i,
  819. read_info->filename,exception);
  820. if (IsPostscriptRendered(read_info->filename) == MagickFalse)
  821. break;
  822. read_info->blob=NULL;
  823. read_info->length=0;
  824. next=ReadImage(read_info,exception);
  825. (void) RelinquishUniqueFileResource(read_info->filename);
  826. if (next == (Image *) NULL)
  827. break;
  828. AppendImageToList(&postscript_image,next);
  829. }
  830. (void) RelinquishUniqueFileResource(read_info->filename);
  831. read_info=DestroyImageInfo(read_info);
  832. if (postscript_image == (Image *) NULL)
  833. {
  834. if (*message != '\0')
  835. (void) ThrowMagickException(exception,GetMagickModule(),
  836. DelegateError,"PostscriptDelegateFailed","`%s'",message);
  837. image=DestroyImageList(image);
  838. return((Image *) NULL);
  839. }
  840. if (LocaleCompare(postscript_image->magick,"BMP") == 0)
  841. {
  842. Image
  843. *cmyk_image;
  844. cmyk_image=ConsolidateCMYKImages(postscript_image,exception);
  845. if (cmyk_image != (Image *) NULL)
  846. {
  847. postscript_image=DestroyImageList(postscript_image);
  848. postscript_image=cmyk_image;
  849. }
  850. }
  851. (void) SeekBlob(image,0,SEEK_SET);
  852. for (c=ReadBlobByte(image); c != EOF; c=ReadBlobByte(image))
  853. {
  854. /*
  855. Note document structuring comments.
  856. */
  857. *p++=(char) c;
  858. if ((strchr("\n\r%",c) == (char *) NULL) &&
  859. ((size_t) (p-command) < (MagickPathExtent-1)))
  860. continue;
  861. *p='\0';
  862. p=command;
  863. /*
  864. Skip %%BeginDocument thru %%EndDocument.
  865. */
  866. if (LocaleNCompare(BeginDocument,command,strlen(BeginDocument)) == 0)
  867. skip=MagickTrue;
  868. if (LocaleNCompare(EndDocument,command,strlen(EndDocument)) == 0)
  869. skip=MagickFalse;
  870. if (skip != MagickFalse)
  871. continue;
  872. if (LocaleNCompare(ICCProfile,command,strlen(ICCProfile)) == 0)
  873. {
  874. unsigned char
  875. *datum;
  876. /*
  877. Read ICC profile.
  878. */
  879. profile=AcquireStringInfo(MagickPathExtent);
  880. datum=GetStringInfoDatum(profile);
  881. for (i=0; (c=ProfileInteger(image,hex_digits)) != EOF; i++)
  882. {
  883. if (i >= (ssize_t) GetStringInfoLength(profile))
  884. {
  885. SetStringInfoLength(profile,(size_t) i << 1);
  886. datum=GetStringInfoDatum(profile);
  887. }
  888. datum[i]=(unsigned char) c;
  889. }
  890. SetStringInfoLength(profile,(size_t) i+1);
  891. (void) SetImageProfile(image,"icc",profile,exception);
  892. profile=DestroyStringInfo(profile);
  893. continue;
  894. }
  895. if (LocaleNCompare(PhotoshopProfile,command,strlen(PhotoshopProfile)) == 0)
  896. {
  897. unsigned char
  898. *q;
  899. /*
  900. Read Photoshop profile.
  901. */
  902. count=(ssize_t) sscanf(command,PhotoshopProfile " %lu",&extent);
  903. if (count != 1)
  904. continue;
  905. length=extent;
  906. if ((MagickSizeType) length > GetBlobSize(image))
  907. ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
  908. profile=BlobToStringInfo((const void *) NULL,length);
  909. if (profile != (StringInfo *) NULL)
  910. {
  911. q=GetStringInfoDatum(profile);
  912. for (i=0; i < (ssize_t) length; i++)
  913. *q++=(unsigned char) ProfileInteger(image,hex_digits);
  914. (void) SetImageProfile(image,"8bim",profile,exception);
  915. profile=DestroyStringInfo(profile);
  916. }
  917. continue;
  918. }
  919. if (LocaleNCompare(BeginXMPPacket,command,strlen(BeginXMPPacket)) == 0)
  920. {
  921. /*
  922. Read XMP profile.
  923. */
  924. p=command;
  925. profile=StringToStringInfo(command);
  926. for (i=(ssize_t) GetStringInfoLength(profile)-1; c != EOF; i++)
  927. {
  928. SetStringInfoLength(profile,(size_t) (i+1));
  929. c=ReadBlobByte(image);
  930. GetStringInfoDatum(profile)[i]=(unsigned char) c;
  931. *p++=(char) c;
  932. if ((strchr("\n\r%",c) == (char *) NULL) &&
  933. ((size_t) (p-command) < (MagickPathExtent-1)))
  934. continue;
  935. *p='\0';
  936. p=command;
  937. if (LocaleNCompare(EndXMPPacket,command,strlen(EndXMPPacket)) == 0)
  938. break;
  939. }
  940. SetStringInfoLength(profile,(size_t) i);
  941. (void) SetImageProfile(image,"xmp",profile,exception);
  942. profile=DestroyStringInfo(profile);
  943. continue;
  944. }
  945. }
  946. (void) CloseBlob(image);
  947. if (image_info->number_scenes != 0)
  948. {
  949. Image
  950. *clone_image;
  951. /*
  952. Add place holder images to meet the subimage specification requirement.
  953. */
  954. for (i=0; i < (ssize_t) image_info->scene; i++)
  955. {
  956. clone_image=CloneImage(postscript_image,1,1,MagickTrue,exception);
  957. if (clone_image != (Image *) NULL)
  958. PrependImageToList(&postscript_image,clone_image);
  959. }
  960. }
  961. do
  962. {
  963. (void) CopyMagickString(postscript_image->filename,filename,
  964. MagickPathExtent);
  965. (void) CopyMagickString(postscript_image->magick,image->magick,
  966. MagickPathExtent);
  967. if (columns != 0)
  968. postscript_image->magick_columns=columns;
  969. if (rows != 0)
  970. postscript_image->magick_rows=rows;
  971. postscript_image->page=page;
  972. (void) CloneImageProfiles(postscript_image,image);
  973. (void) CloneImageProperties(postscript_image,image);
  974. next=SyncNextImageInList(postscript_image);
  975. if (next != (Image *) NULL)
  976. postscript_image=next;
  977. } while (next != (Image *) NULL);
  978. image=DestroyImageList(image);
  979. scene=0;
  980. for (next=GetFirstImageInList(postscript_image); next != (Image *) NULL; )
  981. {
  982. next->scene=scene++;
  983. next=GetNextImageInList(next);
  984. }
  985. return(GetFirstImageInList(postscript_image));
  986. }
  987. /*
  988. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  989. % %
  990. % %
  991. % %
  992. % R e g i s t e r P S I m a g e %
  993. % %
  994. % %
  995. % %
  996. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  997. %
  998. % RegisterPSImage() adds properties for the PS image format to
  999. % the list of supported formats. The properties include the image format
  1000. % tag, a method to read and/or write the format, whether the format
  1001. % supports the saving of more than one frame to the same file or blob,
  1002. % whether the format supports native in-memory I/O, and a brief
  1003. % description of the format.
  1004. %
  1005. % The format of the RegisterPSImage method is:
  1006. %
  1007. % size_t RegisterPSImage(void)
  1008. %
  1009. */
  1010. ModuleExport size_t RegisterPSImage(void)
  1011. {
  1012. MagickInfo
  1013. *entry;
  1014. entry=AcquireMagickInfo("PS","EPI",
  1015. "Encapsulated PostScript Interchange format");
  1016. entry->decoder=(DecodeImageHandler *) ReadPSImage;
  1017. entry->encoder=(EncodeImageHandler *) WritePSImage;
  1018. entry->magick=(IsImageFormatHandler *) IsPS;
  1019. entry->flags|=CoderDecoderSeekableStreamFlag;
  1020. entry->flags^=CoderAdjoinFlag;
  1021. entry->flags^=CoderBlobSupportFlag;
  1022. entry->mime_type=ConstantString("application/postscript");
  1023. (void) RegisterMagickInfo(entry);
  1024. entry=AcquireMagickInfo("PS","EPS","Encapsulated PostScript");
  1025. entry->decoder=(DecodeImageHandler *) ReadPSImage;
  1026. entry->encoder=(EncodeImageHandler *) WritePSImage;
  1027. entry->magick=(IsImageFormatHandler *) IsPS;
  1028. entry->flags|=CoderDecoderSeekableStreamFlag;
  1029. entry->flags^=CoderAdjoinFlag;
  1030. entry->flags^=CoderBlobSupportFlag;
  1031. entry->mime_type=ConstantString("application/postscript");
  1032. (void) RegisterMagickInfo(entry);
  1033. entry=AcquireMagickInfo("PS","EPSF","Encapsulated PostScript");
  1034. entry->decoder=(DecodeImageHandler *) ReadPSImage;
  1035. entry->encoder=(EncodeImageHandler *) WritePSImage;
  1036. entry->magick=(IsImageFormatHandler *) IsPS;
  1037. entry->flags|=CoderDecoderSeekableStreamFlag;
  1038. entry->flags^=CoderAdjoinFlag;
  1039. entry->flags^=CoderBlobSupportFlag;
  1040. entry->mime_type=ConstantString("application/postscript");
  1041. (void) RegisterMagickInfo(entry);
  1042. entry=AcquireMagickInfo("PS","EPSI",
  1043. "Encapsulated PostScript Interchange format");
  1044. entry->decoder=(DecodeImageHandler *) ReadPSImage;
  1045. entry->encoder=(EncodeImageHandler *) WritePSImage;
  1046. entry->magick=(IsImageFormatHandler *) IsPS;
  1047. entry->flags|=CoderDecoderSeekableStreamFlag;
  1048. entry->flags^=CoderAdjoinFlag;
  1049. entry->flags^=CoderBlobSupportFlag;
  1050. entry->mime_type=ConstantString("application/postscript");
  1051. (void) RegisterMagickInfo(entry);
  1052. entry=AcquireMagickInfo("PS","PS","PostScript");
  1053. entry->decoder=(DecodeImageHandler *) ReadPSImage;
  1054. entry->encoder=(EncodeImageHandler *) WritePSImage;
  1055. entry->magick=(IsImageFormatHandler *) IsPS;
  1056. entry->mime_type=ConstantString("application/postscript");
  1057. entry->flags|=CoderDecoderSeekableStreamFlag;
  1058. entry->flags^=CoderBlobSupportFlag;
  1059. (void) RegisterMagickInfo(entry);
  1060. return(MagickImageCoderSignature);
  1061. }
  1062. /*
  1063. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1064. % %
  1065. % %
  1066. % %
  1067. % U n r e g i s t e r P S I m a g e %
  1068. % %
  1069. % %
  1070. % %
  1071. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1072. %
  1073. % UnregisterPSImage() removes format registrations made by the
  1074. % PS module from the list of supported formats.
  1075. %
  1076. % The format of the UnregisterPSImage method is:
  1077. %
  1078. % UnregisterPSImage(void)
  1079. %
  1080. */
  1081. ModuleExport void UnregisterPSImage(void)
  1082. {
  1083. (void) UnregisterMagickInfo("EPI");
  1084. (void) UnregisterMagickInfo("EPS");
  1085. (void) UnregisterMagickInfo("EPSF");
  1086. (void) UnregisterMagickInfo("EPSI");
  1087. (void) UnregisterMagickInfo("PS");
  1088. }
  1089. /*
  1090. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1091. % %
  1092. % %
  1093. % %
  1094. % W r i t e P S I m a g e %
  1095. % %
  1096. % %
  1097. % %
  1098. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1099. %
  1100. % WritePSImage translates an image to encapsulated Postscript
  1101. % Level I for printing. If the supplied geometry is null, the image is
  1102. % centered on the Postscript page. Otherwise, the image is positioned as
  1103. % specified by the geometry.
  1104. %
  1105. % The format of the WritePSImage method is:
  1106. %
  1107. % MagickBooleanType WritePSImage(const ImageInfo *image_info,
  1108. % Image *image,ExceptionInfo *exception)
  1109. %
  1110. % A description of each parameter follows:
  1111. %
  1112. % o image_info: the image info.
  1113. %
  1114. % o image: the image.
  1115. %
  1116. % o exception: return any errors or warnings in this structure.
  1117. %
  1118. */
  1119. static inline unsigned char *PopHexPixel(const char hex_digits[][3],
  1120. const size_t pixel,unsigned char *pixels)
  1121. {
  1122. register const char
  1123. *hex;
  1124. hex=hex_digits[pixel];
  1125. *pixels++=(unsigned char) (*hex++ & 0xff);
  1126. *pixels++=(unsigned char) (*hex & 0xff);
  1127. return(pixels);
  1128. }
  1129. static MagickBooleanType WritePSImage(const ImageInfo *image_info,Image *image,
  1130. ExceptionInfo *exception)
  1131. {
  1132. #define WriteRunlengthPacket(image,pixel,length,p) \
  1133. { \
  1134. if ((image->alpha_trait != UndefinedPixelTrait) && (length != 0) && \
  1135. (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha)) \
  1136. { \
  1137. q=PopHexPixel(hex_digits,0xff,q); \
  1138. q=PopHexPixel(hex_digits,0xff,q); \
  1139. q=PopHexPixel(hex_digits,0xff,q); \
  1140. } \
  1141. else \
  1142. { \
  1143. q=PopHexPixel(hex_digits,ScaleQuantumToChar(ClampToQuantum(pixel.red)),q); \
  1144. q=PopHexPixel(hex_digits,ScaleQuantumToChar(ClampToQuantum(pixel.green)),q); \
  1145. q=PopHexPixel(hex_digits,ScaleQuantumToChar(ClampToQuantum(pixel.blue)),q); \
  1146. } \
  1147. q=PopHexPixel(hex_digits,(size_t) MagickMin(length,0xff),q); \
  1148. }
  1149. static const char
  1150. hex_digits[][3] =
  1151. {
  1152. "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B",
  1153. "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17",
  1154. "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23",
  1155. "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
  1156. "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B",
  1157. "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45", "46", "47",
  1158. "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53",
  1159. "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
  1160. "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B",
  1161. "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77",
  1162. "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81", "82", "83",
  1163. "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
  1164. "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B",
  1165. "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
  1166. "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3",
  1167. "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
  1168. "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
  1169. "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
  1170. "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3",
  1171. "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
  1172. "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB",
  1173. "FC", "FD", "FE", "FF"
  1174. },
  1175. PostscriptProlog[] =
  1176. "%%BeginProlog\n"
  1177. "%\n"
  1178. "% Display a color image. The image is displayed in color on\n"
  1179. "% Postscript viewers or printers that support color, otherwise\n"
  1180. "% it is displayed as grayscale.\n"
  1181. "%\n"
  1182. "/DirectClassPacket\n"
  1183. "{\n"
  1184. " %\n"
  1185. " % Get a DirectClass packet.\n"
  1186. " %\n"
  1187. " % Parameters:\n"
  1188. " % red.\n"
  1189. " % green.\n"
  1190. " % blue.\n"
  1191. " % length: number of pixels minus one of this color (optional).\n"
  1192. " %\n"
  1193. " currentfile color_packet readhexstring pop pop\n"
  1194. " compression 0 eq\n"
  1195. " {\n"
  1196. " /number_pixels 3 def\n"
  1197. " }\n"
  1198. " {\n"
  1199. " currentfile byte readhexstring pop 0 get\n"
  1200. " /number_pixels exch 1 add 3 mul def\n"
  1201. " } ifelse\n"
  1202. " 0 3 number_pixels 1 sub\n"
  1203. " {\n"
  1204. " pixels exch color_packet putinterval\n"
  1205. " } for\n"
  1206. " pixels 0 number_pixels getinterval\n"
  1207. "} bind def\n"
  1208. "\n"
  1209. "/DirectClassImage\n"
  1210. "{\n"
  1211. " %\n"
  1212. " % Display a DirectClass image.\n"
  1213. " %\n"
  1214. " systemdict /colorimage known\n"
  1215. " {\n"
  1216. " columns rows 8\n"
  1217. " [\n"
  1218. " columns 0 0\n"
  1219. " rows neg 0 rows\n"
  1220. " ]\n"
  1221. " { DirectClassPacket } false 3 colorimage\n"
  1222. " }\n"
  1223. " {\n"
  1224. " %\n"
  1225. " % No colorimage operator; convert to grayscale.\n"
  1226. " %\n"
  1227. " columns rows 8\n"
  1228. " [\n"
  1229. " columns 0 0\n"
  1230. " rows neg 0 rows\n"
  1231. " ]\n"
  1232. " { GrayDirectClassPacket } image\n"
  1233. " } ifelse\n"
  1234. "} bind def\n"
  1235. "\n"
  1236. "/GrayDirectClassPacket\n"
  1237. "{\n"
  1238. " %\n"
  1239. " % Get a DirectClass packet; convert to grayscale.\n"
  1240. " %\n"
  1241. " % Parameters:\n"
  1242. " % red\n"
  1243. " % green\n"
  1244. " % blue\n"
  1245. " % length: number of pixels minus one of this color (optional).\n"
  1246. " %\n"
  1247. " currentfile color_packet readhexstring pop pop\n"
  1248. " color_packet 0 get 0.299 mul\n"
  1249. " color_packet 1 get 0.587 mul add\n"
  1250. " color_packet 2 get 0.114 mul add\n"
  1251. " cvi\n"
  1252. " /gray_packet exch def\n"
  1253. " compression 0 eq\n"
  1254. " {\n"
  1255. " /number_pixels 1 def\n"
  1256. " }\n"
  1257. " {\n"
  1258. " currentfile byte readhexstring pop 0 get\n"
  1259. " /number_pixels exch 1 add def\n"
  1260. " } ifelse\n"
  1261. " 0 1 number_pixels 1 sub\n"
  1262. " {\n"
  1263. " pixels exch gray_packet put\n"
  1264. " } for\n"
  1265. " pixels 0 number_pixels getinterval\n"
  1266. "} bind def\n"
  1267. "\n"
  1268. "/GrayPseudoClassPacket\n"
  1269. "{\n"
  1270. " %\n"
  1271. " % Get a PseudoClass packet; convert to grayscale.\n"
  1272. " %\n"
  1273. " % Parameters:\n"
  1274. " % index: index into the colormap.\n"
  1275. " % length: number of pixels minus one of this color (optional).\n"
  1276. " %\n"
  1277. " currentfile byte readhexstring pop 0 get\n"
  1278. " /offset exch 3 mul def\n"
  1279. " /color_packet colormap offset 3 getinterval def\n"
  1280. " color_packet 0 get 0.299 mul\n"
  1281. " color_packet 1 get 0.587 mul add\n"
  1282. " color_packet 2 get 0.114 mul add\n"
  1283. " cvi\n"
  1284. " /gray_packet exch def\n"
  1285. " compression 0 eq\n"
  1286. " {\n"
  1287. " /number_pixels 1 def\n"
  1288. " }\n"
  1289. " {\n"
  1290. " currentfile byte readhexstring pop 0 get\n"
  1291. " /number_pixels exch 1 add def\n"
  1292. " } ifelse\n"
  1293. " 0 1 number_pixels 1 sub\n"
  1294. " {\n"
  1295. " pixels exch gray_packet put\n"
  1296. " } for\n"
  1297. " pixels 0 number_pixels getinterval\n"
  1298. "} bind def\n"
  1299. "\n"
  1300. "/PseudoClassPacket\n"
  1301. "{\n"
  1302. " %\n"
  1303. " % Get a PseudoClass packet.\n"
  1304. " %\n"
  1305. " % Parameters:\n"
  1306. " % index: index into the colormap.\n"
  1307. " % length: number of pixels minus one of this color (optional).\n"
  1308. " %\n"
  1309. " currentfile byte readhexstring pop 0 get\n"
  1310. " /offset exch 3 mul def\n"
  1311. " /color_packet colormap offset 3 getinterval def\n"
  1312. " compression 0 eq\n"
  1313. " {\n"
  1314. " /number_pixels 3 def\n"
  1315. " }\n"
  1316. " {\n"
  1317. " currentfile byte readhexstring pop 0 get\n"
  1318. " /number_pixels exch 1 add 3 mul def\n"
  1319. " } ifelse\n"
  1320. " 0 3 number_pixels 1 sub\n"
  1321. " {\n"
  1322. " pixels exch color_packet putinterval\n"
  1323. " } for\n"
  1324. " pixels 0 number_pixels getinterval\n"
  1325. "} bind def\n"
  1326. "\n"
  1327. "/PseudoClassImage\n"
  1328. "{\n"
  1329. " %\n"
  1330. " % Display a PseudoClass image.\n"
  1331. " %\n"
  1332. " % Parameters:\n"
  1333. " % class: 0-PseudoClass or 1-Grayscale.\n"
  1334. " %\n"
  1335. " currentfile buffer readline pop\n"
  1336. " token pop /class exch def pop\n"
  1337. " class 0 gt\n"
  1338. " {\n"
  1339. " currentfile buffer readline pop\n"
  1340. " token pop /depth exch def pop\n"
  1341. " /grays columns 8 add depth sub depth mul 8 idiv string def\n"
  1342. " columns rows depth\n"
  1343. " [\n"
  1344. " columns 0 0\n"
  1345. " rows neg 0 rows\n"
  1346. " ]\n"
  1347. " { currentfile grays readhexstring pop } image\n"
  1348. " }\n"
  1349. " {\n"
  1350. " %\n"
  1351. " % Parameters:\n"
  1352. " % colors: number of colors in the colormap.\n"
  1353. " % colormap: red, green, blue color packets.\n"
  1354. " %\n"
  1355. " currentfile buffer readline pop\n"
  1356. " token pop /colors exch def pop\n"
  1357. " /colors colors 3 mul def\n"
  1358. " /colormap colors string def\n"
  1359. " currentfile colormap readhexstring pop pop\n"
  1360. " systemdict /colorimage known\n"
  1361. " {\n"
  1362. " columns rows 8\n"
  1363. " [\n"
  1364. " columns 0 0\n"
  1365. " rows neg 0 rows\n"
  1366. " ]\n"
  1367. " { PseudoClassPacket } false 3 colorimage\n"
  1368. " }\n"
  1369. " {\n"
  1370. " %\n"
  1371. " % No colorimage operator; convert to grayscale.\n"
  1372. " %\n"
  1373. " columns rows 8\n"
  1374. " [\n"
  1375. " columns 0 0\n"
  1376. " rows neg 0 rows\n"
  1377. " ]\n"
  1378. " { G…

Large files files are truncated, but you can click here to view the full file