PageRenderTime 56ms CodeModel.GetById 15ms 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
  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. " { GrayPseudoClassPacket } image\n"
  1379. " } ifelse\n"
  1380. " } ifelse\n"
  1381. "} bind def\n"
  1382. "\n"
  1383. "/DisplayImage\n"
  1384. "{\n"
  1385. " %\n"
  1386. " % Display a DirectClass or PseudoClass image.\n"
  1387. " %\n"
  1388. " % Parameters:\n"
  1389. " % x & y translation.\n"
  1390. " % x & y scale.\n"
  1391. " % label pointsize.\n"
  1392. " % image label.\n"
  1393. " % image columns & rows.\n"
  1394. " % class: 0-DirectClass or 1-PseudoClass.\n"
  1395. " % compression: 0-none or 1-RunlengthEncoded.\n"
  1396. " % hex color packets.\n"
  1397. " %\n"
  1398. " gsave\n"
  1399. " /buffer 512 string def\n"
  1400. " /byte 1 string def\n"
  1401. " /color_packet 3 string def\n"
  1402. " /pixels 768 string def\n"
  1403. "\n"
  1404. " currentfile buffer readline pop\n"
  1405. " token pop /x exch def\n"
  1406. " token pop /y exch def pop\n"
  1407. " x y translate\n"
  1408. " currentfile buffer readline pop\n"
  1409. " token pop /x exch def\n"
  1410. " token pop /y exch def pop\n"
  1411. " currentfile buffer readline pop\n"
  1412. " token pop /pointsize exch def pop\n",
  1413. PostscriptEpilog[] =
  1414. " x y scale\n"
  1415. " currentfile buffer readline pop\n"
  1416. " token pop /columns exch def\n"
  1417. " token pop /rows exch def pop\n"
  1418. " currentfile buffer readline pop\n"
  1419. " token pop /class exch def pop\n"
  1420. " currentfile buffer readline pop\n"
  1421. " token pop /compression exch def pop\n"
  1422. " class 0 gt { PseudoClassImage } { DirectClassImage } ifelse\n"
  1423. " grestore\n";
  1424. char
  1425. buffer[MagickPathExtent],
  1426. date[MagickPathExtent],
  1427. **labels,
  1428. page_geometry[MagickPathExtent];
  1429. CompressionType
  1430. compression;
  1431. const char
  1432. *value;
  1433. const StringInfo
  1434. *profile;
  1435. double
  1436. pointsize;
  1437. GeometryInfo
  1438. geometry_info;
  1439. MagickBooleanType
  1440. status;
  1441. MagickOffsetType
  1442. scene;
  1443. MagickStatusType
  1444. flags;
  1445. PixelInfo
  1446. pixel;
  1447. PointInfo
  1448. delta,
  1449. resolution,
  1450. scale;
  1451. Quantum
  1452. index;
  1453. RectangleInfo
  1454. geometry,
  1455. media_info,
  1456. page_info;
  1457. register const Quantum
  1458. *p;
  1459. register ssize_t
  1460. i,
  1461. x;
  1462. register unsigned char
  1463. *q;
  1464. SegmentInfo
  1465. bounds;
  1466. size_t
  1467. bit,
  1468. byte,
  1469. imageListLength,
  1470. length,
  1471. page,
  1472. text_size;
  1473. ssize_t
  1474. j,
  1475. y;
  1476. time_t
  1477. timer;
  1478. unsigned char
  1479. pixels[2048];
  1480. /*
  1481. Open output image file.
  1482. */
  1483. assert(image_info != (const ImageInfo *) NULL);
  1484. assert(image_info->signature == MagickCoreSignature);
  1485. assert(image != (Image *) NULL);
  1486. assert(image->signature == MagickCoreSignature);
  1487. if (image->debug != MagickFalse)
  1488. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  1489. assert(exception != (ExceptionInfo *) NULL);
  1490. assert(exception->signature == MagickCoreSignature);
  1491. status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
  1492. if (status == MagickFalse)
  1493. return(status);
  1494. (void) memset(&bounds,0,sizeof(bounds));
  1495. compression=image->compression;
  1496. if (image_info->compression != UndefinedCompression)
  1497. compression=image_info->compression;
  1498. page=1;
  1499. scene=0;
  1500. imageListLength=GetImageListLength(image);
  1501. do
  1502. {
  1503. /*
  1504. Scale relative to dots-per-inch.
  1505. */
  1506. (void) TransformImageColorspace(image,sRGBColorspace,exception);
  1507. delta.x=DefaultResolution;
  1508. delta.y=DefaultResolution;
  1509. resolution.x=image->resolution.x;
  1510. resolution.y=image->resolution.y;
  1511. if ((resolution.x == 0.0) || (resolution.y == 0.0))
  1512. {
  1513. flags=ParseGeometry(PSDensityGeometry,&geometry_info);
  1514. resolution.x=geometry_info.rho;
  1515. resolution.y=geometry_info.sigma;
  1516. if ((flags & SigmaValue) == 0)
  1517. resolution.y=resolution.x;
  1518. }
  1519. if (image_info->density != (char *) NULL)
  1520. {
  1521. flags=ParseGeometry(image_info->density,&geometry_info);
  1522. resolution.x=geometry_info.rho;
  1523. resolution.y=geometry_info.sigma;
  1524. if ((flags & SigmaValue) == 0)
  1525. resolution.y=resolution.x;
  1526. }
  1527. if (image->units == PixelsPerCentimeterResolution)
  1528. {
  1529. resolution.x=(double) ((size_t) (100.0*2.54*resolution.x+0.5)/100.0);
  1530. resolution.y=(double) ((size_t) (100.0*2.54*resolution.y+0.5)/100.0);
  1531. }
  1532. SetGeometry(image,&geometry);
  1533. (void) FormatLocaleString(page_geometry,MagickPathExtent,"%.20gx%.20g",
  1534. (double) image->columns,(double) image->rows);
  1535. if (image_info->page != (char *) NULL)
  1536. (void) CopyMagickString(page_geometry,image_info->page,MagickPathExtent);
  1537. else
  1538. if ((image->page.width != 0) && (image->page.height != 0))
  1539. (void) FormatLocaleString(page_geometry,MagickPathExtent,
  1540. "%.20gx%.20g%+.20g%+.20g",(double) image->page.width,(double)
  1541. image->page.height,(double) image->page.x,(double) image->page.y);
  1542. else
  1543. if ((image->gravity != UndefinedGravity) &&
  1544. (LocaleCompare(image_info->magick,"PS") == 0))
  1545. (void) CopyMagickString(page_geometry,PSPageGeometry,
  1546. MagickPathExtent);
  1547. (void) ConcatenateMagickString(page_geometry,">",MagickPathExtent);
  1548. (void) ParseMetaGeometry(page_geometry,&geometry.x,&geometry.y,
  1549. &geometry.width,&geometry.height);
  1550. scale.x=PerceptibleReciprocal(resolution.x)*geometry.width*delta.x;
  1551. geometry.width=(size_t) floor(scale.x+0.5);
  1552. scale.y=PerceptibleReciprocal(resolution.y)*geometry.height*delta.y;
  1553. geometry.height=(size_t) floor(scale.y+0.5);
  1554. (void) ParseAbsoluteGeometry(page_geometry,&media_info);
  1555. (void) ParseGravityGeometry(image,page_geometry,&page_info,exception);
  1556. if (image->gravity != UndefinedGravity)
  1557. {
  1558. geometry.x=(-page_info.x);
  1559. geometry.y=(ssize_t) (media_info.height+page_info.y-image->rows);
  1560. }
  1561. pointsize=12.0;
  1562. if (image_info->pointsize != 0.0)
  1563. pointsize=image_info->pointsize;
  1564. text_size=0;
  1565. value=GetImageProperty(image,"label",exception);
  1566. if (value != (const char *) NULL)
  1567. text_size=(size_t) (MultilineCensus(value)*pointsize+12);
  1568. if (page == 1)
  1569. {
  1570. /*
  1571. Output Postscript header.
  1572. */
  1573. if (LocaleCompare(image_info->magick,"PS") == 0)
  1574. (void) CopyMagickString(buffer,"%!PS-Adobe-3.0\n",MagickPathExtent);
  1575. else
  1576. (void) CopyMagickString(buffer,"%!PS-Adobe-3.0 EPSF-3.0\n",
  1577. MagickPathExtent);
  1578. (void) WriteBlobString(image,buffer);
  1579. (void) WriteBlobString(image,"%%Creator: (ImageMagick)\n");
  1580. (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Title: (%s)\n",
  1581. image->filename);
  1582. (void) WriteBlobString(image,buffer);
  1583. timer=GetMagickTime();
  1584. (void) FormatMagickTime(timer,MagickPathExtent,date);
  1585. (void) FormatLocaleString(buffer,MagickPathExtent,
  1586. "%%%%CreationDate: (%s)\n",date);
  1587. (void) WriteBlobString(image,buffer);
  1588. bounds.x1=(double) geometry.x;
  1589. bounds.y1=(double) geometry.y;
  1590. bounds.x2=(double) geometry.x+scale.x;
  1591. bounds.y2=(double) geometry.y+(geometry.height+text_size);
  1592. if ((image_info->adjoin != MagickFalse) &&
  1593. (GetNextImageInList(image) != (Image *) NULL))
  1594. (void) CopyMagickString(buffer,"%%%%BoundingBox: (atend)\n",
  1595. MagickPathExtent);
  1596. else
  1597. {
  1598. (void) FormatLocaleString(buffer,MagickPathExtent,
  1599. "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
  1600. ceil(bounds.y1-0.5),floor(bounds.x2+0.5),floor(bounds.y2+0.5));
  1601. (void) WriteBlobString(image,buffer);
  1602. (void) FormatLocaleString(buffer,MagickPathExtent,
  1603. "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,
  1604. bounds.y1,bounds.x2,bounds.y2);
  1605. }
  1606. (void) WriteBlobString(image,buffer);
  1607. profile=GetImageProfile(image,"8bim");
  1608. if (profile != (StringInfo *) NULL)
  1609. {
  1610. /*
  1611. Embed Photoshop profile.
  1612. */
  1613. (void) FormatLocaleString(buffer,MagickPathExtent,
  1614. "%%BeginPhotoshop: %.20g",(double) GetStringInfoLength(profile));
  1615. (void) WriteBlobString(image,buffer);
  1616. for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++)
  1617. {
  1618. if ((i % 32) == 0)
  1619. (void) WriteBlobString(image,"\n% ");
  1620. (void) FormatLocaleString(buffer,MagickPathExtent,"%02X",
  1621. (unsigned int) (GetStringInfoDatum(profile)[i] & 0xff));
  1622. (void) WriteBlobString(image,buffer);
  1623. }
  1624. (void) WriteBlobString(image,"\n%EndPhotoshop\n");
  1625. }
  1626. profile=GetImageProfile(image,"xmp");
  1627. DisableMSCWarning(4127)
  1628. if (0 && (profile != (StringInfo *) NULL))
  1629. RestoreMSCWarning
  1630. {
  1631. /*
  1632. Embed XML profile.
  1633. */
  1634. (void) WriteBlobString(image,"\n%begin_xml_code\n");
  1635. (void) FormatLocaleString(buffer,MagickPathExtent,
  1636. "\n%%begin_xml_packet: %.20g\n",(double)
  1637. GetStringInfoLength(profile));
  1638. (void) WriteBlobString(image,buffer);
  1639. for (i=0; i < (ssize_t) GetStringInfoLength(profile); i++)
  1640. (void) WriteBlobByte(image,GetStringInfoDatum(profile)[i]);
  1641. (void) WriteBlobString(image,"\n%end_xml_packet\n%end_xml_code\n");
  1642. }
  1643. value=GetImageProperty(image,"label",exception);
  1644. if (value != (const char *) NULL)
  1645. (void) WriteBlobString(image,
  1646. "%%DocumentNeededResources: font Times-Roman\n");
  1647. (void) WriteBlobString(image,"%%DocumentData: Clean7Bit\n");
  1648. (void) WriteBlobString(image,"%%LanguageLevel: 1\n");
  1649. if (LocaleCompare(image_info->magick,"PS") != 0)
  1650. (void) WriteBlobString(image,"%%Pages: 1\n");
  1651. else
  1652. {
  1653. /*
  1654. Compute the number of pages.
  1655. */
  1656. (void) WriteBlobString(image,"%%Orientation: Portrait\n");
  1657. (void) WriteBlobString(image,"%%PageOrder: Ascend\n");
  1658. (void) FormatLocaleString(buffer,MagickPathExtent,
  1659. "%%%%Pages: %.20g\n",image_info->adjoin != MagickFalse ?
  1660. (double) imageListLength : 1.0);
  1661. (void) WriteBlobString(image,buffer);
  1662. }
  1663. (void) WriteBlobString(image,"%%EndComments\n");
  1664. (void) WriteBlobString(image,"\n%%BeginDefaults\n");
  1665. (void) WriteBlobString(image,"%%EndDefaults\n\n");
  1666. if ((LocaleCompare(image_info->magick,"EPI") == 0) ||
  1667. (LocaleCompare(image_info->magick,"EPSI") == 0) ||
  1668. (LocaleCompare(image_info->magick,"EPT") == 0))
  1669. {
  1670. Image
  1671. *preview_image;
  1672. Quantum
  1673. pixel;
  1674. register ssize_t
  1675. x;
  1676. ssize_t
  1677. y;
  1678. /*
  1679. Create preview image.
  1680. */
  1681. preview_image=CloneImage(image,0,0,MagickTrue,exception);
  1682. if (preview_image == (Image *) NULL)
  1683. ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
  1684. /*
  1685. Dump image as bitmap.
  1686. */
  1687. (void) FormatLocaleString(buffer,MagickPathExtent,
  1688. "%%%%BeginPreview: %.20g %.20g %.20g %.20g\n%% ",(double)
  1689. preview_image->columns,(double) preview_image->rows,1.0,
  1690. (double) ((((preview_image->columns+7) >> 3)*preview_image->rows+
  1691. 35)/36));
  1692. (void) WriteBlobString(image,buffer);
  1693. q=pixels;
  1694. for (y=0; y < (ssize_t) image->rows; y++)
  1695. {
  1696. p=GetVirtualPixels(preview_image,0,y,preview_image->columns,1,
  1697. exception);
  1698. if (p == (const Quantum *) NULL)
  1699. break;
  1700. bit=0;
  1701. byte=0;
  1702. for (x=0; x < (ssize_t) preview_image->columns; x++)
  1703. {
  1704. byte<<=1;
  1705. pixel=ClampToQuantum(GetPixelLuma(preview_image,p));
  1706. if (pixel >= (Quantum) (QuantumRange/2))
  1707. byte|=0x01;
  1708. bit++;
  1709. if (bit == 8)
  1710. {
  1711. q=PopHexPixel(hex_digits,byte,q);
  1712. if ((q-pixels+8) >= 80)
  1713. {
  1714. *q++='\n';
  1715. (void) WriteBlob(image,q-pixels,pixels);
  1716. q=pixels;
  1717. (void) WriteBlobString(image,"% ");
  1718. };
  1719. bit=0;
  1720. byte=0;
  1721. }
  1722. }
  1723. if (bit != 0)
  1724. {
  1725. byte<<=(8-bit);
  1726. q=PopHexPixel(hex_digits,byte,q);
  1727. if ((q-pixels+8) >= 80)
  1728. {
  1729. *q++='\n';
  1730. (void) WriteBlob(image,q-pixels,pixels);
  1731. q=pixels;
  1732. (void) WriteBlobString(image,"% ");
  1733. };
  1734. };
  1735. }
  1736. if (q != pixels)
  1737. {
  1738. *q++='\n';
  1739. (void) WriteBlob(image,q-pixels,pixels);
  1740. }
  1741. (void) WriteBlobString(image,"\n%%EndPreview\n");
  1742. preview_image=DestroyImage(preview_image);
  1743. }
  1744. /*
  1745. Output Postscript commands.
  1746. */
  1747. (void) WriteBlob(image,sizeof(PostscriptProlog)-1,
  1748. (const unsigned char *) PostscriptProlog);
  1749. value=GetImageProperty(image,"label",exception);
  1750. if (value != (const char *) NULL)
  1751. {
  1752. (void) WriteBlobString(image,
  1753. " /Times-Roman findfont pointsize scalefont setfont\n");
  1754. for (j=(ssize_t) MultilineCensus(value)-1; j >= 0; j--)
  1755. {
  1756. (void) WriteBlobString(image," /label 512 string def\n");
  1757. (void) WriteBlobString(image,
  1758. " currentfile label readline pop\n");
  1759. (void) FormatLocaleString(buffer,MagickPathExtent,
  1760. " 0 y %g add moveto label show pop\n",j*pointsize+12);
  1761. (void) WriteBlobString(image,buffer);
  1762. }
  1763. }
  1764. (void) WriteBlob(image,sizeof(PostscriptEpilog)-1,
  1765. (const unsigned char *) PostscriptEpilog);
  1766. if (LocaleCompare(image_info->magick,"PS") == 0)
  1767. (void) WriteBlobString(image," showpage\n");
  1768. (void) WriteBlobString(image,"} bind def\n");
  1769. (void) WriteBlobString(image,"%%EndProlog\n");
  1770. }
  1771. (void) FormatLocaleString(buffer,MagickPathExtent,"%%%%Page: 1 %.20g\n",
  1772. (double) (page++));
  1773. (void) WriteBlobString(image,buffer);
  1774. (void) FormatLocaleString(buffer,MagickPathExtent,
  1775. "%%%%PageBoundingBox: %.20g %.20g %.20g %.20g\n",(double) geometry.x,
  1776. (double) geometry.y,geometry.x+(double) geometry.width,geometry.y+(double)
  1777. (geometry.height+text_size));
  1778. (void) WriteBlobString(image,buffer);
  1779. if ((double) geometry.x < bounds.x1)
  1780. bounds.x1=(double) geometry.x;
  1781. if ((double) geometry.y < bounds.y1)
  1782. bounds.y1=(double) geometry.y;
  1783. if ((double) (geometry.x+geometry.width-1) > bounds.x2)
  1784. bounds.x2=(double) geometry.x+geometry.width-1;
  1785. if ((double) (geometry.y+(geometry.height+text_size)-1) > bounds.y2)
  1786. bounds.y2=(double) geometry.y+(geometry.height+text_size)-1;
  1787. value=GetImageProperty(image,"label",exception);
  1788. if (value != (const char *) NULL)
  1789. (void) WriteBlobString(image,"%%%%PageResources: font Times-Roman\n");
  1790. if (LocaleCompare(image_info->magick,"PS") != 0)
  1791. (void) WriteBlobString(image,"userdict begin\n");
  1792. (void) WriteBlobString(image,"DisplayImage\n");
  1793. /*
  1794. Output image data.
  1795. */
  1796. (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g %.20g\n%g %g\n%g\n",
  1797. (double) geometry.x,(double) geometry.y,scale.x,scale.y,pointsize);
  1798. (void) WriteBlobString(image,buffer);
  1799. labels=(char **) NULL;
  1800. value=GetImageProperty(image,"label",exception);
  1801. if (value != (const char *) NULL)
  1802. labels=StringToList(value);
  1803. if (labels != (char **) NULL)
  1804. {
  1805. for (i=0; labels[i] != (char *) NULL; i++)
  1806. {
  1807. (void) FormatLocaleString(buffer,MagickPathExtent,"%s \n",
  1808. labels[i]);
  1809. (void) WriteBlobString(image,buffer);
  1810. labels[i]=DestroyString(labels[i]);
  1811. }
  1812. labels=(char **) RelinquishMagickMemory(labels);
  1813. }
  1814. (void) memset(&pixel,0,sizeof(pixel));
  1815. pixel.alpha=(MagickRealType) TransparentAlpha;
  1816. index=(Quantum) 0;
  1817. x=0;
  1818. if ((image_info->type != TrueColorType) &&
  1819. (SetImageGray(image,exception) != MagickFalse))
  1820. {
  1821. if (SetImageMonochrome(image,exception) == MagickFalse)
  1822. {
  1823. Quantum
  1824. pixel;
  1825. /*
  1826. Dump image as grayscale.
  1827. */
  1828. (void) FormatLocaleString(buffer,MagickPathExtent,
  1829. "%.20g %.20g\n1\n1\n1\n8\n",(double) image->columns,(double)
  1830. image->rows);
  1831. (void) WriteBlobString(image,buffer);
  1832. q=pixels;
  1833. for (y=0; y < (ssize_t) image->rows; y++)
  1834. {
  1835. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  1836. if (p == (const Quantum *) NULL)
  1837. break;
  1838. for (x=0; x < (ssize_t) image->columns; x++)
  1839. {
  1840. pixel=(Quantum) ScaleQuantumToChar(ClampToQuantum(GetPixelLuma(
  1841. image,p)));
  1842. q=PopHexPixel(hex_digits,(size_t) pixel,q);
  1843. if ((q-pixels+8) >= 80)
  1844. {
  1845. *q++='\n';
  1846. (void) WriteBlob(image,q-pixels,pixels);
  1847. q=pixels;
  1848. }
  1849. p+=GetPixelChannels(image);
  1850. }
  1851. if (image->previous == (Image *) NULL)
  1852. {
  1853. status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
  1854. y,image->rows);
  1855. if (status == MagickFalse)
  1856. break;
  1857. }
  1858. }
  1859. if (q != pixels)
  1860. {
  1861. *q++='\n';
  1862. (void) WriteBlob(image,q-pixels,pixels);
  1863. }
  1864. }
  1865. else
  1866. {
  1867. ssize_t
  1868. y;
  1869. Quantum
  1870. pixel;
  1871. /*
  1872. Dump image as bitmap.
  1873. */
  1874. (void) FormatLocaleString(buffer,MagickPathExtent,
  1875. "%.20g %.20g\n1\n1\n1\n1\n",(double) image->columns,(double)
  1876. image->rows);
  1877. (void) WriteBlobString(image,buffer);
  1878. q=pixels;
  1879. for (y=0; y < (ssize_t) image->rows; y++)
  1880. {
  1881. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  1882. if (p == (const Quantum *) NULL)
  1883. break;
  1884. bit=0;
  1885. byte=0;
  1886. for (x=0; x < (ssize_t) image->columns; x++)
  1887. {
  1888. byte<<=1;
  1889. pixel=ClampToQuantum(GetPixelLuma(image,p));
  1890. if (pixel >= (Quantum) (QuantumRange/2))
  1891. byte|=0x01;
  1892. bit++;
  1893. if (bit == 8)
  1894. {
  1895. q=PopHexPixel(hex_digits,byte,q);
  1896. if ((q-pixels+2) >= 80)
  1897. {
  1898. *q++='\n';
  1899. (void) WriteBlob(image,q-pixels,pixels);
  1900. q=pixels;
  1901. };
  1902. bit=0;
  1903. byte=0;
  1904. }
  1905. p+=GetPixelChannels(image);
  1906. }
  1907. if (bit != 0)
  1908. {
  1909. byte<<=(8-bit);
  1910. q=PopHexPixel(hex_digits,byte,q);
  1911. if ((q-pixels+2) >= 80)
  1912. {
  1913. *q++='\n';
  1914. (void) WriteBlob(image,q-pixels,pixels);
  1915. q=pixels;
  1916. }
  1917. };
  1918. if (image->previous == (Image *) NULL)
  1919. {
  1920. status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
  1921. y,image->rows);
  1922. if (status == MagickFalse)
  1923. break;
  1924. }
  1925. }
  1926. if (q != pixels)
  1927. {
  1928. *q++='\n';
  1929. (void) WriteBlob(image,q-pixels,pixels);
  1930. }
  1931. }
  1932. }
  1933. else
  1934. if ((image->storage_class == DirectClass) ||
  1935. (image->colors > 256) || (image->alpha_trait != UndefinedPixelTrait))
  1936. {
  1937. /*
  1938. Dump DirectClass image.
  1939. */
  1940. (void) FormatLocaleString(buffer,MagickPathExtent,
  1941. "%.20g %.20g\n0\n%d\n",(double) image->columns,(double) image->rows,
  1942. compression == RLECompression ? 1 : 0);
  1943. (void) WriteBlobString(image,buffer);
  1944. switch (compression)
  1945. {
  1946. case RLECompression:
  1947. {
  1948. /*
  1949. Dump runlength-encoded DirectColor packets.
  1950. */
  1951. q=pixels;
  1952. for (y=0; y < (ssize_t) image->rows; y++)
  1953. {
  1954. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  1955. if (p == (const Quantum *) NULL)
  1956. break;
  1957. GetPixelInfoPixel(image,p,&pixel);
  1958. length=255;
  1959. for (x=0; x < (ssize_t) image->columns; x++)
  1960. {
  1961. if ((GetPixelRed(image,p) == ClampToQuantum(pixel.red)) &&
  1962. (GetPixelGreen(image,p) == ClampToQuantum(pixel.green)) &&
  1963. (GetPixelBlue(image,p) == ClampToQuantum(pixel.blue)) &&
  1964. (GetPixelAlpha(image,p) == ClampToQuantum(pixel.alpha)) &&
  1965. (length < 255) && (x < (ssize_t) (image->columns-1)))
  1966. length++;
  1967. else
  1968. {
  1969. if (x > 0)
  1970. {
  1971. WriteRunlengthPacket(image,pixel,length,p);
  1972. if ((q-pixels+10) >= 80)
  1973. {
  1974. *q++='\n';
  1975. (void) WriteBlob(image,q-pixels,pixels);
  1976. q=pixels;
  1977. }
  1978. }
  1979. length=0;
  1980. }
  1981. GetPixelInfoPixel(image,p,&pixel);
  1982. p+=GetPixelChannels(image);
  1983. }
  1984. WriteRunlengthPacket(image,pixel,length,p);
  1985. if ((q-pixels+10) >= 80)
  1986. {
  1987. *q++='\n';
  1988. (void) WriteBlob(image,q-pixels,pixels);
  1989. q=pixels;
  1990. }
  1991. if (image->previous == (Image *) NULL)
  1992. {
  1993. status=SetImageProgress(image,SaveImageTag,
  1994. (MagickOffsetType) y,image->rows);
  1995. if (status == MagickFalse)
  1996. break;
  1997. }
  1998. }
  1999. if (q != pixels)
  2000. {
  2001. *q++='\n';
  2002. (void) WriteBlob(image,q-pixels,pixels);
  2003. }
  2004. break;
  2005. }
  2006. case NoCompression:
  2007. default:
  2008. {
  2009. /*
  2010. Dump uncompressed DirectColor packets.
  2011. */
  2012. q=pixels;
  2013. for (y=0; y < (ssize_t) image->rows; y++)
  2014. {
  2015. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  2016. if (p == (const Quantum *) NULL)
  2017. break;
  2018. for (x=0; x < (ssize_t) image->columns; x++)
  2019. {
  2020. if ((image->alpha_trait != UndefinedPixelTrait) &&
  2021. (GetPixelAlpha(image,p) == (Quantum) TransparentAlpha))
  2022. {
  2023. q=PopHexPixel(hex_digits,0xff,q);
  2024. q=PopHexPixel(hex_digits,0xff,q);
  2025. q=PopHexPixel(hex_digits,0xff,q);
  2026. }
  2027. else
  2028. {
  2029. q=PopHexPixel(hex_digits,ScaleQuantumToChar(
  2030. GetPixelRed(image,p)),q);
  2031. q=PopHexPixel(hex_digits,ScaleQuantumToChar(
  2032. GetPixelGreen(image,p)),q);
  2033. q=PopHexPixel(hex_digits,ScaleQuantumToChar(
  2034. GetPixelBlue(image,p)),q);
  2035. }
  2036. if ((q-pixels+6) >= 80)
  2037. {
  2038. *q++='\n';
  2039. (void) WriteBlob(image,q-pixels,pixels);
  2040. q=pixels;
  2041. }
  2042. p+=GetPixelChannels(image);
  2043. }
  2044. if (image->previous == (Image *) NULL)
  2045. {
  2046. status=SetImageProgress(image,SaveImageTag,
  2047. (MagickOffsetType) y,image->rows);
  2048. if (status == MagickFalse)
  2049. break;
  2050. }
  2051. }
  2052. if (q != pixels)
  2053. {
  2054. *q++='\n';
  2055. (void) WriteBlob(image,q-pixels,pixels);
  2056. }
  2057. break;
  2058. }
  2059. }
  2060. (void) WriteBlobByte(image,'\n');
  2061. }
  2062. else
  2063. {
  2064. /*
  2065. Dump PseudoClass image.
  2066. */
  2067. (void) FormatLocaleString(buffer,MagickPathExtent,
  2068. "%.20g %.20g\n%d\n%d\n0\n",(double) image->columns,(double)
  2069. image->rows,image->storage_class == PseudoClass ? 1 : 0,
  2070. compression == RLECompression ? 1 : 0);
  2071. (void) WriteBlobString(image,buffer);
  2072. /*
  2073. Dump number of colors and colormap.
  2074. */
  2075. (void) FormatLocaleString(buffer,MagickPathExtent,"%.20g\n",(double)
  2076. image->colors);
  2077. (void) WriteBlobString(image,buffer);
  2078. for (i=0; i < (ssize_t) image->colors; i++)
  2079. {
  2080. (void) FormatLocaleString(buffer,MagickPathExtent,"%02X%02X%02X\n",
  2081. ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red)),
  2082. ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green)),
  2083. ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue)));
  2084. (void) WriteBlobString(image,buffer);
  2085. }
  2086. switch (compression)
  2087. {
  2088. case RLECompression:
  2089. {
  2090. /*
  2091. Dump runlength-encoded PseudoColor packets.
  2092. */
  2093. q=pixels;
  2094. for (y=0; y < (ssize_t) image->rows; y++)
  2095. {
  2096. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  2097. if (p == (const Quantum *) NULL)
  2098. break;
  2099. index=GetPixelIndex(image,p);
  2100. length=255;
  2101. for (x=0; x < (ssize_t) image->columns; x++)
  2102. {
  2103. if ((index == GetPixelIndex(image,p)) &&
  2104. (length < 255) && (x < ((ssize_t) image->columns-1)))
  2105. length++;
  2106. else
  2107. {
  2108. if (x > 0)
  2109. {
  2110. q=PopHexPixel(hex_digits,(size_t) index,q);
  2111. q=PopHexPixel(hex_digits,(size_t)
  2112. MagickMin(length,0xff),q);
  2113. i++;
  2114. if ((q-pixels+6) >= 80)
  2115. {
  2116. *q++='\n';
  2117. (void) WriteBlob(image,q-pixels,pixels);
  2118. q=pixels;
  2119. }
  2120. }
  2121. length=0;
  2122. }
  2123. index=GetPixelIndex(image,p);
  2124. pixel.red=(MagickRealType) GetPixelRed(image,p);
  2125. pixel.green=(MagickRealType) GetPixelGreen(image,p);
  2126. pixel.blue=(MagickRealType) GetPixelBlue(image,p);
  2127. pixel.alpha=(MagickRealType) GetPixelAlpha(image,p);
  2128. p+=GetPixelChannels(image);
  2129. }
  2130. q=PopHexPixel(hex_digits,(size_t) index,q);
  2131. q=PopHexPixel(hex_digits,(size_t) MagickMin(length,0xff),q);
  2132. if ((q-pixels+6) >= 80)
  2133. {
  2134. *q++='\n';
  2135. (void) WriteBlob(image,q-pixels,pixels);
  2136. q=pixels;
  2137. }
  2138. if (image->previous == (Image *) NULL)
  2139. {
  2140. status=SetImageProgress(image,SaveImageTag,
  2141. (MagickOffsetType) y,image->rows);
  2142. if (status == MagickFalse)
  2143. break;
  2144. }
  2145. }
  2146. if (q != pixels)
  2147. {
  2148. *q++='\n';
  2149. (void) WriteBlob(image,q-pixels,pixels);
  2150. }
  2151. break;
  2152. }
  2153. case NoCompression:
  2154. default:
  2155. {
  2156. /*
  2157. Dump uncompressed PseudoColor packets.
  2158. */
  2159. q=pixels;
  2160. for (y=0; y < (ssize_t) image->rows; y++)
  2161. {
  2162. p=GetVirtualPixels(image,0,y,image->columns,1,exception);
  2163. if (p == (const Quantum *) NULL)
  2164. break;
  2165. for (x=0; x < (ssize_t) image->columns; x++)
  2166. {
  2167. q=PopHexPixel(hex_digits,(size_t) GetPixelIndex(image,p),q);
  2168. if ((q-pixels+4) >= 80)
  2169. {
  2170. *q++='\n';
  2171. (void) WriteBlob(image,q-pixels,pixels);
  2172. q=pixels;
  2173. }
  2174. p+=GetPixelChannels(image);
  2175. }
  2176. if (image->previous == (Image *) NULL)
  2177. {
  2178. status=SetImageProgress(image,SaveImageTag,
  2179. (MagickOffsetType) y,image->rows);
  2180. if (status == MagickFalse)
  2181. break;
  2182. }
  2183. }
  2184. if (q != pixels)
  2185. {
  2186. *q++='\n';
  2187. (void) WriteBlob(image,q-pixels,pixels);
  2188. }
  2189. break;
  2190. }
  2191. }
  2192. (void) WriteBlobByte(image,'\n');
  2193. }
  2194. if (LocaleCompare(image_info->magick,"PS") != 0)
  2195. (void) WriteBlobString(image,"end\n");
  2196. (void) WriteBlobString(image,"%%PageTrailer\n");
  2197. if (GetNextImageInList(image) == (Image *) NULL)
  2198. break;
  2199. image=SyncNextImageInList(image);
  2200. status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
  2201. if (status == MagickFalse)
  2202. break;
  2203. } while (image_info->adjoin != MagickFalse);
  2204. (void) WriteBlobString(image,"%%Trailer\n");
  2205. if (page > 2)
  2206. {
  2207. (void) FormatLocaleString(buffer,MagickPathExtent,
  2208. "%%%%BoundingBox: %.20g %.20g %.20g %.20g\n",ceil(bounds.x1-0.5),
  2209. ceil(bounds.y1-0.5),floor(bounds.x2-0.5),floor(bounds.y2-0.5));
  2210. (void) WriteBlobString(image,buffer);
  2211. (void) FormatLocaleString(buffer,MagickPathExtent,
  2212. "%%%%HiResBoundingBox: %g %g %g %g\n",bounds.x1,bounds.y1,bounds.x2,
  2213. bounds.y2);
  2214. (void) WriteBlobString(image,buffer);
  2215. }
  2216. (void) WriteBlobString(image,"%%EOF\n");
  2217. (void) CloseBlob(image);
  2218. return(MagickTrue);
  2219. }