home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume20 / imagemgc / part35 < prev    next >
Encoding:
Text File  |  1993-07-13  |  50.6 KB  |  1,804 lines

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i091:  imagemagic - X11 image processing and display, Part35/38
  4. Message-ID: <1993Jul14.232255.23575@sparky.sterling.com>
  5. X-Md4-Signature: 942fc65fdbded2ac9dd5310a2ec2b912
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 23:22:55 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 91
  13. Archive-name: imagemagic/part35
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.35 (part 35 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/encode.c continued
  21. #
  22. if test ! -r _shar_seq_.tmp; then
  23.     echo 'Please unpack part 1 first!'
  24.     exit 1
  25. fi
  26. (read Scheck
  27.  if test "$Scheck" != 35; then
  28.     echo Please unpack part "$Scheck" next!
  29.     exit 1
  30.  else
  31.     exit 0
  32.  fi
  33. ) < _shar_seq_.tmp || exit 1
  34. if test ! -f _shar_wnt_.tmp; then
  35.     echo 'x - still skipping ImageMagick/encode.c'
  36. else
  37. echo 'x - continuing file ImageMagick/encode.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/encode.c' &&
  39. %                                                                             %
  40. %                                                                             %
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. %
  43. %  Function WritePCXImage writes an image in the ZSoft IBM PC Paintbrush file
  44. %  format.
  45. %
  46. %  The format of the WritePCXImage routine is:
  47. %
  48. %      status=WritePCXImage(image)
  49. %
  50. %  A description of each parameter follows.
  51. %
  52. %    o status: Function WritePCXImage return True if the image is written.
  53. %      False is returned is there is a memory shortage or if the image file
  54. %      fails to write.
  55. %
  56. %    o image:  A pointer to a Image structure.
  57. %
  58. %
  59. */
  60. static unsigned int WritePCXImage(image)
  61. Image
  62. X  *image;
  63. {
  64. X  typedef struct _PCXHeader
  65. X  {
  66. X    unsigned char
  67. X      identifier,
  68. X      version,
  69. X      encoding,
  70. X      bits_per_pixel;
  71. X
  72. X    short int
  73. X      left,
  74. X      top,
  75. X      right,
  76. X      bottom,
  77. X      horizonal_resolution,
  78. X      vertical_resolution;
  79. X
  80. X    unsigned char
  81. X      reserved,
  82. X      planes;
  83. X
  84. X    short int
  85. X      bytes_per_line,
  86. X      palette_info;
  87. X
  88. X    unsigned char
  89. X      colormap_signature;
  90. X  } PCXHeader;
  91. X
  92. X  PCXHeader
  93. X    pcx_header;
  94. X
  95. X  register int
  96. X    i,
  97. X    j,
  98. X    x,
  99. X    y;
  100. X
  101. X  register RunlengthPacket
  102. X    *p;
  103. X
  104. X  register unsigned char
  105. X    *q;
  106. X
  107. X  unsigned char
  108. X    count,
  109. X    packet,
  110. X    *pcx_colormap,
  111. X    *pcx_pixels,
  112. X    previous;
  113. X
  114. X  unsigned int
  115. X    grayscale;
  116. X
  117. X  /*
  118. X    Open output image file.
  119. X  */
  120. X  OpenImage(image,"w");
  121. X  if (image->file == (FILE *) NULL)
  122. X    {
  123. X      Warning("unable to open file",image->filename);
  124. X      return(False);
  125. X    }
  126. X  /*
  127. X    PCX colormap must be 256 entries or less.
  128. X  */
  129. X  if ((image->class == DirectClass) || (image->colors > 256))
  130. X    QuantizeImage(image,256,8,False,RGBColorspace,True);
  131. X  /*
  132. X    Initialize PCX raster file header.
  133. X  */
  134. X  pcx_header.identifier=0x0a;
  135. X  pcx_header.version=5;
  136. X  pcx_header.encoding=1;
  137. X  /*
  138. X    Determine if image is grayscale.
  139. X  */
  140. X  grayscale=True;
  141. X  for (i=0; i < image->colors; i++)
  142. X    if ((image->colormap[i].red != image->colormap[i].green) ||
  143. X        (image->colormap[i].green != image->colormap[i].blue))
  144. X      {
  145. X        grayscale=False;
  146. X        break;
  147. X      }
  148. X  if ((image->colors > 2) || !grayscale)
  149. X    pcx_header.bits_per_pixel=8;
  150. X  else
  151. X    pcx_header.bits_per_pixel=1;
  152. X  pcx_header.left=0;
  153. X  pcx_header.top=0;
  154. X  pcx_header.right=image->columns-1;
  155. X  pcx_header.bottom=image->rows-1;
  156. X  pcx_header.horizonal_resolution=image->columns;
  157. X  pcx_header.vertical_resolution=image->rows;
  158. X  pcx_header.reserved=0;
  159. X  pcx_header.planes=1;
  160. X  pcx_header.bytes_per_line=(image->columns*pcx_header.bits_per_pixel+7)/8;
  161. X  pcx_header.palette_info=1;
  162. X  pcx_header.colormap_signature=0x0c;
  163. X  /*
  164. X    Write PCX header.
  165. X  */
  166. X  (void) fwrite(&pcx_header.identifier,1,1,image->file);
  167. X  (void) fwrite(&pcx_header.version,1,1,image->file);
  168. X  (void) fwrite(&pcx_header.encoding,1,1,image->file);
  169. X  (void) fwrite(&pcx_header.bits_per_pixel,1,1,image->file);
  170. X  LSBFirstWriteShort(pcx_header.left,image->file);
  171. X  LSBFirstWriteShort(pcx_header.top,image->file);
  172. X  LSBFirstWriteShort(pcx_header.right,image->file);
  173. X  LSBFirstWriteShort(pcx_header.bottom,image->file);
  174. X  LSBFirstWriteShort(pcx_header.horizonal_resolution,image->file);
  175. X  LSBFirstWriteShort(pcx_header.vertical_resolution,image->file);
  176. X  /*
  177. X    Dump colormap to file.
  178. X  */
  179. X  pcx_colormap=(unsigned char *) malloc(3*256*sizeof(unsigned char));
  180. X  if (pcx_colormap == (unsigned char *) NULL)
  181. X    {
  182. X      Warning("unable to allocate memory",(char *) NULL);
  183. X      return(False);
  184. X    }
  185. X  q=pcx_colormap;
  186. X  for (i=0; i < image->colors; i++)
  187. X  {
  188. X    *q++=image->colormap[i].red;
  189. X    *q++=image->colormap[i].green;
  190. X    *q++=image->colormap[i].blue;
  191. X  }
  192. X  (void) fwrite((char *) pcx_colormap,3,16,image->file);
  193. X  (void) fwrite(&pcx_header.reserved,1,1,image->file);
  194. X  (void) fwrite(&pcx_header.planes,1,1,image->file);
  195. X  LSBFirstWriteShort(pcx_header.bytes_per_line,image->file);
  196. X  LSBFirstWriteShort(pcx_header.palette_info,image->file);
  197. X  for (i=0; i < 58; i++)
  198. X    (void) fwrite("\0",1,1,image->file);
  199. X  /*
  200. X    Convert MIFF to PCX raster pixels.
  201. X  */
  202. X  pcx_pixels=(unsigned char *)
  203. X    malloc(pcx_header.bytes_per_line*image->rows*sizeof(unsigned char));
  204. X  if (pcx_pixels == (unsigned char *) NULL)
  205. X    {
  206. X      Warning("unable to allocate memory",(char *) NULL);
  207. X      return(False);
  208. X    }
  209. X  x=0;
  210. X  y=0;
  211. X  p=image->pixels;
  212. X  q=pcx_pixels;
  213. X  if (pcx_header.bits_per_pixel > 1)
  214. X    for (i=0; i < image->packets; i++)
  215. X    {
  216. X      for (j=0; j <= (int) p->length; j++)
  217. X      {
  218. X        *q++=p->index;
  219. X        x++;
  220. X        if (x == image->columns)
  221. X          {
  222. X            x=0;
  223. X            y++;
  224. X            q=pcx_pixels+y*pcx_header.bytes_per_line;
  225. X          }
  226. X      }
  227. X      p++;
  228. X    }
  229. X  else
  230. X    {
  231. X      register unsigned char
  232. X        bit,
  233. X        byte,
  234. X        polarity;
  235. X
  236. X      /*
  237. X        Convert PseudoClass image to a PCX monochrome image.
  238. X      */
  239. X      polarity=(Intensity(image->colormap[0]) <
  240. X        Intensity(image->colormap[1]) ? 1 : 0);
  241. X      bit=0;
  242. X      byte=0;
  243. X      for (i=0; i < image->packets; i++)
  244. X      {
  245. X        for (j=0; j <= (int) p->length; j++)
  246. X        {
  247. X          byte<<=1;
  248. X          if (p->index == polarity)
  249. X            byte|=0x01;
  250. X          bit++;
  251. X          if (bit == 8)
  252. X            {
  253. X              *q++=byte;
  254. X              bit=0;
  255. X              byte=0;
  256. X            }
  257. X          x++;
  258. X          if (x == image->columns)
  259. X            {
  260. X              /*
  261. X                Advance to the next scanline.
  262. X              */
  263. X              if (bit != 0)
  264. X                *q++=byte << (8-bit);
  265. X              bit=0;
  266. X              byte=0;
  267. X              x=0;
  268. X              y++;
  269. X              q=pcx_pixels+y*pcx_header.bytes_per_line;
  270. X           }
  271. X        }
  272. X        p++;
  273. X      }
  274. X    }
  275. X  /*
  276. X    Runlength-encoded PCX pixels.
  277. X  */
  278. X  for (y=0; y < image->rows; y++)
  279. X  {
  280. X    q=pcx_pixels+y*pcx_header.bytes_per_line;
  281. X    previous=(*q++);
  282. X    count=1;
  283. X    for (x=0; x < (pcx_header.bytes_per_line-1); x++)
  284. X    {
  285. X      packet=(*q++);
  286. X      if ((packet == previous) && (count < 63))
  287. X        {
  288. X          count++;
  289. X          continue;
  290. X        }
  291. X      if ((count > 1) || ((previous & 0xc0) == 0xc0))
  292. X        {
  293. X          count|=0xc0;
  294. X          (void) fwrite(&count,1,1,image->file);
  295. X        }
  296. X      (void) fwrite(&previous,1,1,image->file);
  297. X      previous=packet;
  298. X      count=1;
  299. X    }
  300. X    if ((count > 1) || ((previous & 0xc0) == 0xc0))
  301. X      {
  302. X        count|=0xc0;
  303. X        (void) fwrite(&count,1,1,image->file);
  304. X      }
  305. X    (void) fwrite(&previous,1,1,image->file);
  306. X  }
  307. X  if (image->colors > 16)
  308. X    {
  309. X      (void) fwrite(&pcx_header.colormap_signature,1,1,image->file);
  310. X      (void) fwrite((char *) pcx_colormap,3,256,image->file);
  311. X    }
  312. X  (void) free((char *) pcx_pixels);
  313. X  (void) free((char *) pcx_colormap);
  314. X  CloseImage(image);
  315. X  return(True);
  316. }
  317. X
  318. /*
  319. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  320. %                                                                             %
  321. %                                                                             %
  322. %                                                                             %
  323. %   W r i t e P I C T I m a g e                                               %
  324. %                                                                             %
  325. %                                                                             %
  326. %                                                                             %
  327. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  328. %
  329. %  Function WritePICTImage writes an image to a file in the Apple Macintosh
  330. %  QuickDraw/PICT image format.
  331. %
  332. %  The format of the WritePICTImage routine is:
  333. %
  334. %      status=WritePICTImage(image)
  335. %
  336. %  A description of each parameter follows.
  337. %
  338. %    o status: Function WritePICTImage return True if the image is written.
  339. %      False is returned is there is a memory shortage or if the image file
  340. %      fails to write.
  341. %
  342. %    o image:  A pointer to a Image structure.
  343. %
  344. %
  345. */
  346. static unsigned int WritePICTImage(image)
  347. Image
  348. X  *image;
  349. {
  350. #define MaxCount  128
  351. #define PictClipRegion  0x01
  352. #define PictEndOfPicture  0xff
  353. #define PictHeaderOp  0x0C00
  354. #define PictHeaderSize  512
  355. #define PictPackBitsRectangle  0x98
  356. #define PictVersion  0x11
  357. X
  358. X  int
  359. X    count;
  360. X
  361. X  register int
  362. X    i,
  363. X    j,
  364. X    x;
  365. X
  366. X  register RunlengthPacket
  367. X    *p;
  368. X
  369. X  register unsigned char
  370. X    *q;
  371. X
  372. X  unsigned char
  373. X    *buffer,
  374. X    *packed_scanline,
  375. X    *scanline;
  376. X
  377. X  unsigned short
  378. X    blue,
  379. X    red,
  380. X    green;
  381. X
  382. X  /*
  383. X    Open output image file.
  384. X  */
  385. X  OpenImage(image,"w");
  386. X  if (image->file == (FILE *) NULL)
  387. X    {
  388. X      Warning("unable to open file",image->filename);
  389. X      return(False);
  390. X    }
  391. X  /*
  392. X    PICT colormap must be 256 entries or less.
  393. X  */
  394. X  if ((image->class == DirectClass) || (image->colors > 256))
  395. X    QuantizeImage(image,256,8,False,RGBColorspace,True);
  396. X  /*
  397. X    Allocate memory.
  398. X  */
  399. X  buffer=(unsigned char *) malloc(PictHeaderSize*sizeof(unsigned char));
  400. X  packed_scanline=(unsigned char *)
  401. X    malloc((image->columns+image->columns/MaxCount+1)*sizeof(unsigned char));
  402. X  scanline=(unsigned char *) malloc(image->columns*sizeof(unsigned char));
  403. X  if ((buffer == (unsigned char *) NULL) ||
  404. X      (packed_scanline == (unsigned char *) NULL) ||
  405. X      (scanline == (unsigned char *) NULL))
  406. X    {
  407. X      Warning("unable to allocate memory",(char *) NULL);
  408. X      return(False);
  409. X    }
  410. X  /*
  411. X    Write header.
  412. X  */
  413. X  for (i=0; i < PictHeaderSize; i++)
  414. X    buffer[i]=0;
  415. X  (void) fwrite((char *) buffer,1,PictHeaderSize,image->file);
  416. X  /*
  417. X    Write picture size and frame.
  418. X  */
  419. X  MSBFirstWriteShort(0,image->file);
  420. X  MSBFirstWriteShort(0,image->file);
  421. X  MSBFirstWriteShort(0,image->file);
  422. X  MSBFirstWriteShort(image->rows,image->file);
  423. X  MSBFirstWriteShort(image->columns,image->file);
  424. X  /*
  425. X    Write version.
  426. X  */
  427. X  MSBFirstWriteShort(PictVersion,image->file);
  428. X  MSBFirstWriteShort(0x02FF,image->file);
  429. X  MSBFirstWriteShort(PictHeaderOp,image->file);
  430. X  MSBFirstWriteLong((unsigned long) -1L,image->file);
  431. X  MSBFirstWriteShort(0,image->file);
  432. X  MSBFirstWriteShort(0,image->file);
  433. X  MSBFirstWriteShort(0,image->file);
  434. X  MSBFirstWriteShort(0,image->file);
  435. X  MSBFirstWriteShort(image->columns,image->file);
  436. X  MSBFirstWriteShort(0,image->file);
  437. X  MSBFirstWriteShort(image->rows,image->file);
  438. X  MSBFirstWriteShort(0,image->file);
  439. X  MSBFirstWriteLong(0L,image->file);
  440. X  /*
  441. X    Write clipping region.
  442. X  */
  443. X  MSBFirstWriteShort(PictClipRegion,image->file);
  444. X  MSBFirstWriteShort(10,image->file);
  445. X  MSBFirstWriteShort(0,image->file);
  446. X  MSBFirstWriteShort(0,image->file);
  447. X  MSBFirstWriteShort(image->rows,image->file);
  448. X  MSBFirstWriteShort(image->columns,image->file);
  449. X  /*
  450. X    Write pack bits rectangle.
  451. X  */
  452. X  MSBFirstWriteShort(PictPackBitsRectangle,image->file);
  453. X  MSBFirstWriteShort((image->columns | 0x8000),image->file);
  454. X  MSBFirstWriteShort(0,image->file);
  455. X  MSBFirstWriteShort(0,image->file);
  456. X  MSBFirstWriteShort(image->rows,image->file);
  457. X  MSBFirstWriteShort(image->columns,image->file);
  458. X  MSBFirstWriteShort(0,image->file);
  459. X  MSBFirstWriteShort(0,image->file);
  460. X  MSBFirstWriteLong(0L,image->file);
  461. X  /*
  462. X    Write resolution.
  463. X  */
  464. X  MSBFirstWriteShort(72,image->file);
  465. X  MSBFirstWriteShort(0,image->file);
  466. X  MSBFirstWriteShort(72,image->file);
  467. X  MSBFirstWriteShort(0,image->file);
  468. X  /*
  469. X    Write miscellanious.
  470. X  */
  471. X  MSBFirstWriteShort(0,image->file);
  472. X  MSBFirstWriteShort(8,image->file);
  473. X  MSBFirstWriteShort(1,image->file);
  474. X  MSBFirstWriteShort(8,image->file);
  475. X  MSBFirstWriteLong(0L,image->file);
  476. X  MSBFirstWriteLong(0L,image->file);
  477. X  MSBFirstWriteLong(0L,image->file);
  478. X  MSBFirstWriteLong(0L,image->file);
  479. X  MSBFirstWriteShort(0,image->file);
  480. X  /*
  481. X    Write colormap.
  482. X  */
  483. X  MSBFirstWriteShort((unsigned short) (image->colors-1),image->file);
  484. X  for (i=0; i < image->colors; i++)
  485. X  {
  486. X    red=(image->colormap[i].red*65535)/(unsigned int) MaxRGB;
  487. X    green=(image->colormap[i].green*65535)/(unsigned int) MaxRGB;
  488. X    blue=(image->colormap[i].blue*65535)/(unsigned int) MaxRGB;
  489. X    MSBFirstWriteShort((unsigned int) i,image->file);
  490. X    MSBFirstWriteShort(red,image->file);
  491. X    MSBFirstWriteShort(green,image->file);
  492. X    MSBFirstWriteShort(blue,image->file);
  493. X  }
  494. X  /*
  495. X    Write source and destination rectangle.
  496. X  */
  497. X  MSBFirstWriteShort(0,image->file);
  498. X  MSBFirstWriteShort(0,image->file);
  499. X  MSBFirstWriteShort(image->rows,image->file);
  500. X  MSBFirstWriteShort(image->columns,image->file);
  501. X  MSBFirstWriteShort(0,image->file);
  502. X  MSBFirstWriteShort(0,image->file);
  503. X  MSBFirstWriteShort(image->rows,image->file);
  504. X  MSBFirstWriteShort(image->columns,image->file);
  505. X  MSBFirstWriteShort(0,image->file);
  506. X  /*
  507. X    Write picture data.
  508. X  */
  509. X  count=0;
  510. X  x=0;
  511. X  p=image->pixels;
  512. X  q=scanline;
  513. X  for (i=0; i < image->packets; i++)
  514. X  {
  515. X    for (j=0; j <= (int) p->length; j++)
  516. X    {
  517. X      *q++=(unsigned char) p->index;
  518. X      x++;
  519. X      if (x == image->columns)
  520. X        {
  521. X          count+=PackbitsEncodeImage(image,scanline,packed_scanline);
  522. X          q=scanline;
  523. X          x=0;
  524. X        }
  525. X    }
  526. X    p++;
  527. X  }
  528. X  if (count & 0x1)
  529. X    (void) fputc('\0',image->file);
  530. X  MSBFirstWriteShort(PictEndOfPicture,image->file);
  531. X  (void) free((char *) scanline);
  532. X  (void) free((char *) packed_scanline);
  533. X  (void) free((char *) buffer);
  534. X  CloseImage(image);
  535. X  return(True);
  536. }
  537. X
  538. /*
  539. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  540. %                                                                             %
  541. %                                                                             %
  542. %                                                                             %
  543. %   W r i t e P N M I m a g e                                                 %
  544. %                                                                             %
  545. %                                                                             %
  546. %                                                                             %
  547. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  548. %
  549. %  Procedure WritePNMImage writes an image to a file in the PNM rasterfile
  550. %  format.
  551. %
  552. %  The format of the WritePNMImage routine is:
  553. %
  554. %      status=WritePNMImage(image)
  555. %
  556. %  A description of each parameter follows.
  557. %
  558. %    o status: Function WritePNMImage return True if the image is written.
  559. %      False is returned is there is a memory shortage or if the image file
  560. %      fails to write.
  561. %
  562. %    o image:  A pointer to a Image structure.
  563. %
  564. %
  565. */
  566. static unsigned int WritePNMImage(image)
  567. Image
  568. X  *image;
  569. {
  570. X  register int
  571. X    i,
  572. X    j;
  573. X
  574. X  register RunlengthPacket
  575. X    *p;
  576. X
  577. X  register unsigned char
  578. X    *q;
  579. X
  580. X  unsigned char
  581. X    format,
  582. X    *pnm_pixels;
  583. X
  584. X  unsigned int
  585. X    packets;
  586. X
  587. X  /*
  588. X    Open output image file.
  589. X  */
  590. X  OpenImage(image,"w");
  591. X  if (image->file == (FILE *) NULL)
  592. X    {
  593. X      Warning("unable to open file",image->filename);
  594. X      return(False);
  595. X    }
  596. X  /*
  597. X    Promote/Demote image based on image type.
  598. X  */
  599. X  if (strcmp(image->magick,"PBM") == 0)
  600. X    QuantizeImage(image,2,8,False,GRAYColorspace,True);
  601. X  else
  602. X    if (strcmp(image->magick,"PGM") == 0)
  603. X      QuantizeImage(image,256,8,False,GRAYColorspace,True);
  604. X    else
  605. X      if (strcmp(image->magick,"PPM") == 0)
  606. X        image->class=DirectClass;
  607. X  /*
  608. X    Write PNM file header.
  609. X  */
  610. X  packets=image->columns*image->rows;
  611. X  if ((image->class == DirectClass) || (image->colors > 256))
  612. X    {
  613. X      format='6';
  614. X      packets*=3;
  615. X    }
  616. X  else
  617. X    {
  618. X      /*
  619. X        Determine if image is gray scale.
  620. X      */
  621. X      format='5';
  622. X      if (image->colors == 2)
  623. X        format='4';
  624. X      for (i=0; i < image->colors; i++)
  625. X        if ((image->colormap[i].red != image->colormap[i].green) ||
  626. X            (image->colormap[i].green != image->colormap[i].blue))
  627. X          {
  628. X            format='6';
  629. X            packets*=3;
  630. X            break;
  631. X          }
  632. X    }
  633. X  (void) fprintf(image->file,"P%c\n# created by ImageMagick\n%u %u\n",
  634. X    format,image->columns,image->rows);
  635. X  /*
  636. X    Convert MIFF to PNM raster pixels.
  637. X  */
  638. X  pnm_pixels=(unsigned char *) malloc(packets*sizeof(unsigned char));
  639. X  if (pnm_pixels == (unsigned char *) NULL)
  640. X    {
  641. X      Warning("unable to allocate memory",(char *) NULL);
  642. X      return(False);
  643. X    }
  644. X  p=image->pixels;
  645. X  q=pnm_pixels;
  646. X  switch (format)
  647. X  {
  648. X    case '4':
  649. X    {
  650. X      register unsigned char
  651. X        bit,
  652. X        byte,
  653. X        polarity;
  654. X
  655. X      unsigned int
  656. X        x;
  657. X
  658. X      /*
  659. X        Convert image to a PBM image.
  660. X      */
  661. X      polarity=(Intensity(image->colormap[0]) <
  662. X        Intensity(image->colormap[1]) ? 0 : 1);
  663. X      bit=0;
  664. X      byte=0;
  665. X      x=0;
  666. X      for (i=0; i < image->packets; i++)
  667. X      {
  668. X        for (j=0; j <= (int) p->length; j++)
  669. X        {
  670. X          byte<<=1;
  671. X          if (p->index == polarity)
  672. X            byte|=0x01;
  673. X          bit++;
  674. X          if (bit == 8)
  675. X            {
  676. X              *q++=byte;
  677. X              bit=0;
  678. X              byte=0;
  679. X            }
  680. X          x++;
  681. X          if (x == image->columns)
  682. X            {
  683. X              /*
  684. X                Advance to the next scanline.
  685. X              */
  686. X              if (bit != 0)
  687. X                *q++=byte << (8-bit);
  688. X              bit=0;
  689. X              byte=0;
  690. X              x=0;
  691. X           }
  692. X        }
  693. X        p++;
  694. X      }
  695. X      packets=q-pnm_pixels;
  696. X      break;
  697. X    }
  698. X    case '5':
  699. X    {
  700. X      /*
  701. X        Convert image to a PGM image.
  702. X      */
  703. X      (void) fprintf(image->file,"%d\n",MaxRGB);
  704. X      for (i=0; i < image->packets; i++)
  705. X      {
  706. X        for (j=0; j <= ((int) p->length); j++)
  707. X          *q++=image->colormap[p->index].red;
  708. X        p++;
  709. X      }
  710. X      break;
  711. X    }
  712. X    case '6':
  713. X    {
  714. X      /*
  715. X        Convert image to a PNM image.
  716. X      */
  717. X      (void) fprintf(image->file,"%d\n",MaxRGB);
  718. X      for (i=0; i < image->packets; i++)
  719. X      {
  720. X        for (j=0; j <= ((int) p->length); j++)
  721. X        {
  722. X          *q++=p->red;
  723. X          *q++=p->green;
  724. X          *q++=p->blue;
  725. X        }
  726. X        p++;
  727. X      }
  728. X      break;
  729. X    }
  730. X  }
  731. X  (void) fwrite((char *) pnm_pixels,1,(int) packets,image->file);
  732. X  (void) free((char *) pnm_pixels);
  733. X  CloseImage(image);
  734. X  return(True);
  735. }
  736. X
  737. /*
  738. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  739. %                                                                             %
  740. %                                                                             %
  741. %                                                                             %
  742. %   W r i t e R L E I m a g e                                                 %
  743. %                                                                             %
  744. %                                                                             %
  745. %                                                                             %
  746. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  747. %
  748. %  Function WriteRLEImage writes an image in the Utah Run length encoded image
  749. %  format.
  750. %
  751. %  The format of the WriteRLEImage routine is:
  752. %
  753. %      status=WriteRLEImage(image)
  754. %
  755. %  A description of each parameter follows.
  756. %
  757. %    o status: Function WriteRLEImage return True if the image is written.
  758. %      False is returned is there is a memory shortage or if the image file
  759. %      fails to write.
  760. %
  761. %    o image:  A pointer to a Image structure.
  762. %
  763. %
  764. */
  765. static unsigned int WriteRLEImage(image)
  766. Image
  767. X  *image;
  768. {
  769. X  unsigned int
  770. X    status;
  771. X
  772. X  Warning("Cannot write RLE images",image->filename);
  773. X  status=WriteMIFFImage(image);
  774. X  return(status);
  775. }
  776. X
  777. /*
  778. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  779. %                                                                             %
  780. %                                                                             %
  781. %                                                                             %
  782. %   W r i t e P S I m a g e                                                   %
  783. %                                                                             %
  784. %                                                                             %
  785. %                                                                             %
  786. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  787. %
  788. %  Function WritePSImage translates a MIFF image to encapsulated Postscript
  789. %  Level I for printing.  If the supplied geometry is null, the image is
  790. %  centered on the Postscript page.  Otherwise, the image is positioned as
  791. %  specified by the geometry.
  792. %
  793. %  The format of the WritePSImage routine is:
  794. %
  795. %      status=WritePSImage(image,geometry,density,encapsulated)
  796. %
  797. %  A description of each parameter follows:
  798. %
  799. %    o status: Function WritePSImage return True if the image is printed.
  800. %      False is returned if the image file cannot be opened for printing.
  801. %
  802. %    o image: The address of a structure of type Image;  returned from
  803. %      ReadImage.
  804. %
  805. %    o geometry: A pointer to a geometry string.  Specifies the width and
  806. %      height of the Postscript image.
  807. %
  808. %    o density: A pointer to a density string.  Specifies the dots-per-inch
  809. %      of the Postscript image.
  810. %
  811. %    o encapsulated:  A non-zero value indicates the output is Encapsulated
  812. %      Postscript.
  813. %
  814. %
  815. */
  816. static unsigned int WritePSImage(image,geometry,density,encapsulated)
  817. Image
  818. X  *image;
  819. X
  820. char
  821. X  *geometry,
  822. X  *density;
  823. X
  824. unsigned int
  825. X  encapsulated;
  826. {
  827. #define PageSideMargin 18
  828. #define PageTopMargin 94
  829. #define PageWidth  612
  830. #define PageHeight 792
  831. #define XResolution  72
  832. #define YResolution  72
  833. X
  834. X  static char
  835. X    *Postscript[]=
  836. X    {
  837. X      "%%BeginProlog",
  838. X      "%",
  839. X      "% Display a color image.  The image is displayed in color on",
  840. X      "% Postscript viewers or printers that support color, otherwise",
  841. X      "% it is displayed as grayscale.",
  842. X      "%",
  843. X      "/buffer 512 string def",
  844. X      "/byte 1 string def",
  845. X      "/color_packet 3 string def",
  846. X      "/pixels 768 string def",
  847. X      "",
  848. X      "/DirectClassPacket",
  849. X      "{",
  850. X      "  %",
  851. X      "  % Get a DirectClass packet.",
  852. X      "  %",
  853. X      "  % Parameters: ",
  854. X      "  %   red.",
  855. X      "  %   green.",
  856. X      "  %   blue.",
  857. X      "  %   length: number of pixels minus one of this color (optional).",
  858. X      "  %",
  859. X      "  currentfile color_packet readhexstring pop pop",
  860. X      "  compression 0 gt",
  861. X      "  {",
  862. X      "    /number_pixels 3 def",
  863. X      "  }",
  864. X      "  {",
  865. X      "    currentfile byte readhexstring pop 0 get",
  866. X      "    /number_pixels exch 1 add 3 mul def",
  867. X      "  } ifelse",
  868. X      "  0 3 number_pixels 1 sub",
  869. X      "  {",
  870. X      "    pixels exch color_packet putinterval",
  871. X      "  } for",
  872. X      "  pixels 0 number_pixels getinterval",
  873. X      "} bind def",
  874. X      "",
  875. X      "/DirectClassImage",
  876. X      "{",
  877. X      "  %",
  878. X      "  % Display a DirectClass image.",
  879. X      "  %",
  880. X      "  systemdict /colorimage known",
  881. X      "  {",
  882. X      "    columns rows 8",
  883. X      "    [",
  884. X      "      columns 0 0",
  885. X      "      rows neg 0 rows",
  886. X      "    ]",
  887. X      "    { DirectClassPacket } false 3 colorimage",
  888. X      "  }",
  889. X      "  {",
  890. X      "    %",
  891. X      "    % No colorimage operator;  convert to grayscale.",
  892. X      "    %",
  893. X      "    columns rows 8",
  894. X      "    [",
  895. X      "      columns 0 0",
  896. X      "      rows neg 0 rows",
  897. X      "    ]",
  898. X      "    { GrayDirectClassPacket } image",
  899. X      "  } ifelse",
  900. X      "} bind def",
  901. X      "",
  902. X      "/GrayDirectClassPacket",
  903. X      "{",
  904. X      "  %",
  905. X      "  % Get a DirectClass packet;  convert to grayscale.",
  906. X      "  %",
  907. X      "  % Parameters: ",
  908. X      "  %   red",
  909. X      "  %   green",
  910. X      "  %   blue",
  911. X      "  %   length: number of pixels minus one of this color (optional).",
  912. X      "  %",
  913. X      "  currentfile color_packet readhexstring pop pop",
  914. X      "  color_packet 0 get 0.299 mul",
  915. X      "  color_packet 1 get 0.587 mul add",
  916. X      "  color_packet 2 get 0.114 mul add",
  917. X      "  cvi",
  918. X      "  /gray_packet exch def",
  919. X      "  compression 0 gt",
  920. X      "  {",
  921. X      "    /number_pixels 1 def",
  922. X      "  }",
  923. X      "  {",
  924. X      "    currentfile byte readhexstring pop 0 get",
  925. X      "    /number_pixels exch 1 add def",
  926. X      "  } ifelse",
  927. X      "  0 1 number_pixels 1 sub",
  928. X      "  {",
  929. X      "    pixels exch gray_packet put",
  930. X      "  } for",
  931. X      "  pixels 0 number_pixels getinterval",
  932. X      "} bind def",
  933. X      "",
  934. X      "/GrayPseudoClassPacket",
  935. X      "{",
  936. X      "  %",
  937. X      "  % Get a PseudoClass packet;  convert to grayscale.",
  938. X      "  %",
  939. X      "  % Parameters: ",
  940. X      "  %   index: index into the colormap.",
  941. X      "  %   length: number of pixels minus one of this color (optional).",
  942. X      "  %",
  943. X      "  currentfile byte readhexstring pop 0 get",
  944. X      "  /offset exch 3 mul def",
  945. X      "  /color_packet colormap offset 3 getinterval def",
  946. X      "  color_packet 0 get 0.299 mul",
  947. X      "  color_packet 1 get 0.587 mul add",
  948. X      "  color_packet 2 get 0.114 mul add",
  949. X      "  cvi",
  950. X      "  /gray_packet exch def",
  951. X      "  compression 0 gt",
  952. X      "  {",
  953. X      "    /number_pixels 1 def",
  954. X      "  }",
  955. X      "  {",
  956. X      "    currentfile byte readhexstring pop 0 get",
  957. X      "    /number_pixels exch 1 add def",
  958. X      "  } ifelse",
  959. X      "  0 1 number_pixels 1 sub",
  960. X      "  {",
  961. X      "    pixels exch gray_packet put",
  962. X      "  } for",
  963. X      "  pixels 0 number_pixels getinterval",
  964. X      "} bind def",
  965. X      "",
  966. X      "/PseudoClassPacket",
  967. X      "{",
  968. X      "  %",
  969. X      "  % Get a PseudoClass packet.",
  970. X      "  %",
  971. X      "  % Parameters: ",
  972. X      "  %   index: index into the colormap.",
  973. X      "  %   length: number of pixels minus one of this color (optional).",
  974. X      "  %",
  975. X      "  currentfile byte readhexstring pop 0 get",
  976. X      "  /offset exch 3 mul def",
  977. X      "  /color_packet colormap offset 3 getinterval def",
  978. X      "  compression 0 gt",
  979. X      "  {",
  980. X      "    /number_pixels 3 def",
  981. X      "  }",
  982. X      "  {",
  983. X      "    currentfile byte readhexstring pop 0 get",
  984. X      "    /number_pixels exch 1 add 3 mul def",
  985. X      "  } ifelse",
  986. X      "  0 3 number_pixels 1 sub",
  987. X      "  {",
  988. X      "    pixels exch color_packet putinterval",
  989. X      "  } for",
  990. X      "  pixels 0 number_pixels getinterval",
  991. X      "} bind def",
  992. X      "",
  993. X      "/PseudoClassImage",
  994. X      "{",
  995. X      "  %",
  996. X      "  % Display a PseudoClass image.",
  997. X      "  %",
  998. X      "  % Parameters: ",
  999. X      "  %   colors: number of colors in the colormap.",
  1000. X      "  %   colormap: red, green, blue color packets.",
  1001. X      "  %",
  1002. X      "  currentfile buffer readline pop",
  1003. X      "  token pop /colors exch def pop",
  1004. X      "  /colors colors 3 mul def",
  1005. X      "  /colormap colors string def",
  1006. X      "  currentfile colormap readhexstring pop pop",
  1007. X      "  systemdict /colorimage known",
  1008. X      "  {",
  1009. X      "    columns rows 8",
  1010. X      "    [",
  1011. X      "      columns 0 0",
  1012. X      "      rows neg 0 rows",
  1013. X      "    ]",
  1014. X      "    { PseudoClassPacket } false 3 colorimage",
  1015. X      "  }",
  1016. X      "  {",
  1017. X      "    %",
  1018. X      "    % No colorimage operator;  convert to grayscale.",
  1019. X      "    %",
  1020. X      "    columns rows 8",
  1021. X      "    [",
  1022. X      "      columns 0 0",
  1023. X      "      rows neg 0 rows",
  1024. X      "    ]",
  1025. X      "    { GrayPseudoClassPacket } image",
  1026. X      "  } ifelse",
  1027. X      "} bind def",
  1028. X      "",
  1029. X      "/DisplayImage",
  1030. X      "{",
  1031. X      "  %",
  1032. X      "  % Display a DirectClass or PseudoClass image.",
  1033. X      "  %",
  1034. X      "  % Parameters: ",
  1035. X      "  %   x & y translation.",
  1036. X      "  %   x & y scale.",
  1037. X      "  %   image columns & rows.",
  1038. X      "  %   class: 0-DirectClass or 1-PseudoClass.",
  1039. X      "  %   compression: 0-RunlengthEncodedCompression or 1-NoCompression.",
  1040. X      "  %   hex color packets.",
  1041. X      "  %",
  1042. X      "  gsave",
  1043. X      "  currentfile buffer readline pop",
  1044. X      "  token pop /x exch def",
  1045. X      "  token pop /y exch def pop",
  1046. X      "  x y translate",
  1047. X      "  currentfile buffer readline pop",
  1048. X      "  token pop /x exch def",
  1049. X      "  token pop /y exch def pop",
  1050. X      "  x y scale",
  1051. X      "  currentfile buffer readline pop",
  1052. X      "  token pop /columns exch def",
  1053. X      "  token pop /rows exch def pop",
  1054. X      "  currentfile buffer readline pop",
  1055. X      "  token pop /class exch def pop",
  1056. X      "  currentfile buffer readline pop",
  1057. X      "  token pop /compression exch def pop",
  1058. X      "  class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
  1059. X      "  grestore",
  1060. X      "  showpage",
  1061. X      "} bind def",
  1062. X      "%%EndProlog",
  1063. X      "%%Page:  1 1",
  1064. X      NULL
  1065. X    };
  1066. X
  1067. X  char
  1068. X    **q;
  1069. X
  1070. X  int
  1071. X    flags,
  1072. X    x,
  1073. X    y;
  1074. X
  1075. X  register RunlengthPacket
  1076. X    *p;
  1077. X
  1078. X  register int
  1079. X    i,
  1080. X    j;
  1081. X
  1082. X  time_t
  1083. X    timer;
  1084. X
  1085. X  unsigned int
  1086. X    height,
  1087. X    page_width,
  1088. X    page_height,
  1089. X    x_resolution,
  1090. X    y_resolution,
  1091. X    width;
  1092. X
  1093. X  unsigned long
  1094. X    scale_factor;
  1095. X
  1096. X  /*
  1097. X    Open output image file.
  1098. X  */
  1099. X  OpenImage(image,"w");
  1100. X  if (image->file == (FILE *) NULL)
  1101. X    {
  1102. X      Warning("unable to open file",image->filename);
  1103. X      return(False);
  1104. X    }
  1105. X  /*
  1106. X    Scale image to size of Postscript page.
  1107. X  */
  1108. X  if (encapsulated)
  1109. X    {
  1110. X      x=0;
  1111. X      y=0;
  1112. X      width=image->columns;
  1113. X      height=image->rows;
  1114. X      if (geometry != (char *) NULL)
  1115. X        (void) XParseGeometry(geometry,&x,&y,&page_width,&page_height);
  1116. X    }
  1117. X  else
  1118. X    {
  1119. X      /*
  1120. X        Center image on Postscript page.
  1121. X      */
  1122. X      page_width=PageWidth;
  1123. X      page_height=PageHeight;
  1124. X      flags=XParseGeometry(geometry,&x,&y,&page_width,&page_height);
  1125. X      scale_factor=UpShift(page_width-(2*PageSideMargin))/image->columns;
  1126. X      if (scale_factor > (UpShift(page_height-(2*PageTopMargin))/image->rows))
  1127. X        scale_factor=UpShift(page_height-(2*PageTopMargin))/image->rows;
  1128. X      width=DownShift(image->columns*scale_factor);
  1129. X      height=DownShift(image->rows*scale_factor);
  1130. X      if (((flags & XValue) == 0) && ((flags & YValue) == 0))
  1131. X        {
  1132. X          int
  1133. X            delta_x,
  1134. X            delta_y;
  1135. X
  1136. X          delta_x=page_width-(width+(2*PageSideMargin));
  1137. X          delta_y=page_height-(height+(2*PageTopMargin));
  1138. X          if (delta_x >= 0)
  1139. X            x=delta_x/2+PageSideMargin;
  1140. X          else
  1141. X            x=PageSideMargin;
  1142. X          if (delta_y >= 0)
  1143. X            y=delta_y/2+PageTopMargin;
  1144. X          else
  1145. X            y=PageTopMargin;
  1146. X        }
  1147. X    }
  1148. X  /*
  1149. X    Scale relative to dots-per-inch.
  1150. X  */
  1151. X  x_resolution=XResolution;
  1152. X  y_resolution=YResolution;
  1153. X  if (density != (char *) NULL)
  1154. X    {
  1155. X      int
  1156. X        z;
  1157. X
  1158. X      /*
  1159. X        User specified density.
  1160. X      */
  1161. X      flags=XParseGeometry(density,&z,&z,&x_resolution,&y_resolution);
  1162. X      if ((flags & WidthValue) == 0)
  1163. X        x_resolution=XResolution;
  1164. X      if ((flags & HeightValue) == 0)
  1165. X        y_resolution=x_resolution;
  1166. X    }
  1167. X  scale_factor=UpShift(XResolution)/x_resolution;
  1168. X  width=DownShift(width*scale_factor);
  1169. X  scale_factor=UpShift(YResolution)/y_resolution;
  1170. X  height=DownShift(height*scale_factor);
  1171. X  /*
  1172. X    Output Postscript header.
  1173. X  */
  1174. X  if (!encapsulated)
  1175. X    (void) fprintf(image->file,"%%!PS-Adobe-3.0\n");
  1176. X  else
  1177. X    (void) fprintf(image->file,"%%!PS-Adobe-3.0 EPSF-3.0\n");
  1178. X  (void) fprintf(image->file,"%%%%Title: %s\n",image->filename);
  1179. X  (void) fprintf(image->file,"%%%%Creator: ImageMagick\n");
  1180. X  timer=time((time_t *) NULL);
  1181. X  (void) localtime(&timer);
  1182. X  (void) fprintf(image->file,"%%%%CreationDate: %s",ctime(&timer));
  1183. X  (void) fprintf(image->file,"%%%%BoundingBox: %d %d %d %d\n",x,y,x+(int) width,
  1184. X    y+(int) height);
  1185. X  if (!encapsulated)
  1186. X    {
  1187. X      (void) fprintf(image->file,"%%%%Orientation: Portrait\n");
  1188. X      (void) fprintf(image->file,"%%%%PageOrder: Ascend\n");
  1189. X    }
  1190. X  (void) fprintf(image->file,"%%%%Pages: %d\n",!encapsulated);
  1191. X  (void) fprintf(image->file,"%%%%EndComments\n");
  1192. X  /*
  1193. X    Output Postscript commands.
  1194. X  */
  1195. X  for (q=Postscript; *q; q++)
  1196. X    (void) fprintf(image->file,"%s\n",*q);
  1197. X  if (encapsulated)
  1198. X    (void) fprintf(image->file,"userdict begin\n");
  1199. X  (void) fprintf(image->file,"%%%%BeginData:\n");
  1200. X  (void) fprintf(image->file,"DisplayImage\n");
  1201. X  /*
  1202. X    Output image data.
  1203. X  */
  1204. X  if (image->compression == RunlengthEncodedCompression)
  1205. X    CompressImage(image);
  1206. X  p=image->pixels;
  1207. X  switch (image->class)
  1208. X  {
  1209. X    case DirectClass:
  1210. X    {
  1211. X      (void) fprintf(image->file,"%d %d\n%u %u\n%u %u\n%d\n%d\n",x,y,width,
  1212. X        height,image->columns,image->rows,(image->class == PseudoClass),
  1213. X        image->compression == NoCompression);
  1214. X      switch (image->compression)
  1215. X      {
  1216. X        case RunlengthEncodedCompression:
  1217. X        default:
  1218. X        {
  1219. X          /*
  1220. X            Dump runlength-encoded DirectColor packets.
  1221. X          */
  1222. X          x=0;
  1223. X          for (i=0; i < image->packets; i++)
  1224. X          {
  1225. X            x++;
  1226. X            (void) fprintf(image->file,"%02x%02x%02x%02x",p->red,p->green,
  1227. X              p->blue,p->length);
  1228. X            if (x == 9)
  1229. X              {
  1230. X                x=0;
  1231. X                (void) fprintf(image->file,"\n");
  1232. X              }
  1233. X            p++;
  1234. X          }
  1235. X          break;
  1236. X        }
  1237. X        case NoCompression:
  1238. X        {
  1239. X          /*
  1240. X            Dump uncompressed DirectColor packets.
  1241. X          */
  1242. X          x=0;
  1243. X          for (i=0; i < image->packets; i++)
  1244. X          {
  1245. X            for (j=0; j <= ((int) p->length); j++)
  1246. X            {
  1247. X              x++;
  1248. X              (void) fprintf(image->file,"%02x%02x%02x",p->red,p->green,
  1249. X                p->blue);
  1250. X              if (x == 12)
  1251. X                {
  1252. X                  x=0;
  1253. X                  (void) fprintf(image->file,"\n");
  1254. X                }
  1255. X            }
  1256. X            p++;
  1257. X          }
  1258. X          break;
  1259. X        }
  1260. X      }
  1261. X      break;
  1262. X    }
  1263. X    case PseudoClass:
  1264. X    {
  1265. X      (void) fprintf(image->file,"%d %d\n%u %u\n%u %u\n%d\n%d\n",x,y,width,
  1266. X        height,image->columns,image->rows,(image->class == PseudoClass),
  1267. X        image->compression == NoCompression);
  1268. X      /*
  1269. X        Dump number of colors and colormap.
  1270. X      */
  1271. X      (void) fprintf(image->file,"%u\n",image->colors);
  1272. X      for (i=0; i < image->colors; i++)
  1273. X        (void) fprintf(image->file,"%02x%02x%02x\n",image->colormap[i].red,
  1274. X          image->colormap[i].green,image->colormap[i].blue);
  1275. X      switch (image->compression)
  1276. X      {
  1277. X        case RunlengthEncodedCompression:
  1278. X        default:
  1279. X        {
  1280. X          /*
  1281. X            Dump runlength-encoded PseudoColor packets.
  1282. X          */
  1283. X          x=0;
  1284. X          for (i=0; i < image->packets; i++)
  1285. X          {
  1286. X            x++;
  1287. X            (void) fprintf(image->file,"%02x%02x",p->index,p->length);
  1288. X            if (x == 18)
  1289. X              {
  1290. X                x=0;
  1291. X                (void) fprintf(image->file,"\n");
  1292. X              }
  1293. X            p++;
  1294. X          }
  1295. X          break;
  1296. X        }
  1297. X        case NoCompression:
  1298. X        {
  1299. X          /*
  1300. X            Dump uncompressed PseudoColor packets.
  1301. X          */
  1302. X          x=0;
  1303. X          for (i=0; i < image->packets; i++)
  1304. X          {
  1305. X            for (j=0; j <= ((int) p->length); j++)
  1306. X            {
  1307. X              x++;
  1308. X              (void) fprintf(image->file,"%02x",p->index);
  1309. X              if (x == 36)
  1310. X                {
  1311. X                  x=0;
  1312. X                  (void) fprintf(image->file,"\n");
  1313. X                }
  1314. X            }
  1315. X            p++;
  1316. X          }
  1317. X          break;
  1318. X        }
  1319. X      }
  1320. X    }
  1321. X  }
  1322. X  (void) fprintf(image->file,"\n");
  1323. X  (void) fprintf(image->file,"%%%%EndData\n");
  1324. X  if (encapsulated)
  1325. X    (void) fprintf(image->file,"end\n");
  1326. X  (void) fprintf(image->file,"%%%%PageTrailer\n");
  1327. X  (void) fprintf(image->file,"%%%%Trailer\n");
  1328. X  (void) fprintf(image->file,"%%%%EOF\n");
  1329. X  CloseImage(image);
  1330. X  return(True);
  1331. }
  1332. X
  1333. /*
  1334. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1335. %                                                                             %
  1336. %                                                                             %
  1337. %                                                                             %
  1338. %   W r i t e P S 2 I m a g e                                                 %
  1339. %                                                                             %
  1340. %                                                                             %
  1341. %                                                                             %
  1342. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1343. %
  1344. %  Function WritePS2Image translates a MIFF image to encapsulated Postscript
  1345. %  Level II for printing.  If the supplied geometry is null, the image is
  1346. %  centered on the Postscript page.  Otherwise, the image is positioned as
  1347. %  specified by the geometry.
  1348. %
  1349. %  The format of the WritePS2Image routine is:
  1350. %
  1351. %      status=WritePS2Image(image,geometry,density,encapsulated)
  1352. %
  1353. %  A description of each parameter follows:
  1354. %
  1355. %    o status: Function WritePS2Image return True if the image is printed.
  1356. %      False is returned if the image file cannot be opened for printing.
  1357. %
  1358. %    o image: The address of a structure of type Image;  returned from
  1359. %      ReadImage.
  1360. %
  1361. %    o geometry: A pointer to a geometry string.  Specifies the width and
  1362. %      height of the Postscript image.
  1363. %
  1364. %    o density: A pointer to a density string.  Specifies the dots-per-inch
  1365. %      of the Postscript image.
  1366. %
  1367. %    o encapsulated:  A non-zero value indicates the output is Encapsulated
  1368. %      Postscript.
  1369. %
  1370. %
  1371. */
  1372. static unsigned int WritePS2Image(image,geometry,density,encapsulated)
  1373. Image
  1374. X  *image;
  1375. X
  1376. char
  1377. X  *geometry,
  1378. X  *density;
  1379. X
  1380. unsigned int
  1381. X  encapsulated;
  1382. {
  1383. #define PageSideMargin 18
  1384. #define PageTopMargin 94
  1385. #define PageWidth  612
  1386. #define PageHeight 792
  1387. #define XResolution  72
  1388. #define YResolution  72
  1389. X
  1390. X  static char
  1391. X    *Postscript[]=
  1392. X    {
  1393. X      "%%BeginProlog",
  1394. X      "%",
  1395. X      "% Display a color image.  The image is displayed in color on",
  1396. X      "% Postscript viewers or printers that support color, otherwise",
  1397. X      "% it is displayed as grayscale.",
  1398. X      "%",
  1399. X      "/buffer 512 string def",
  1400. X      "",
  1401. X      "/DirectClassImage",
  1402. X      "{",
  1403. X      "  %",
  1404. X      "  % Display a DirectClass image.",
  1405. X      "  %",
  1406. X      "  /DeviceRGB setcolorspace",
  1407. X      "  <<",
  1408. X      "    /ImageType 1",
  1409. X      "    /Interpolate true",
  1410. X      "    /Width columns",
  1411. X      "    /Height rows",
  1412. X      "    /BitsPerComponent 8",
  1413. X      "    /Decode [0 1 0 1 0 1]",
  1414. X      "    /ImageMatrix [columns 0 0 rows neg 0 rows]",
  1415. X      "    compression 0 gt",
  1416. X      "    {",
  1417. X      "      /DataSource currentfile /ASCIIHexDecode filter",
  1418. X      "    }",
  1419. X      "    {",
  1420. X      "      /DataSource currentfile /ASCIIHexDecode filter /LZWDecode filter",
  1421. X      "    } ifelse",
  1422. X      "  >> image",
  1423. X      "} bind def",
  1424. X      "",
  1425. X      "/PseudoClassImage",
  1426. X      "{",
  1427. X      "  %",
  1428. X      "  % Display a PseudoClass image.",
  1429. X      "  %",
  1430. X      "  % Parameters: ",
  1431. X      "  %   colors: number of colors in the colormap.",
  1432. X      "  %   colormap: red, green, blue color packets.",
  1433. X      "  %",
  1434. X      "  currentfile buffer readline pop",
  1435. X      "  token pop /colors exch def pop",
  1436. X      "  /colormap colors 3 mul string def",
  1437. X      "  currentfile colormap readhexstring pop pop",
  1438. X      "  [ /Indexed /DeviceRGB colors 1 sub colormap ] setcolorspace",
  1439. X      "  <<",
  1440. X      "    /ImageType 1",
  1441. X      "    /Interpolate true",
  1442. X      "    /Width columns",
  1443. X      "    /Height rows",
  1444. X      "    /BitsPerComponent 8",
  1445. X      "    /Decode [0 255]",
  1446. X      "    /ImageMatrix [columns 0 0 rows neg 0 rows]",
  1447. X      "    compression 0 gt",
  1448. X      "    {",
  1449. X      "      /DataSource currentfile /ASCIIHexDecode filter",
  1450. X      "    }",
  1451. X      "    {",
  1452. X      "      /DataSource currentfile /ASCIIHexDecode filter /LZWDecode filter",
  1453. X      "    } ifelse",
  1454. X      "  >> image",
  1455. X      "} bind def",
  1456. X      "",
  1457. X      "/DisplayImage",
  1458. X      "{",
  1459. X      "  %",
  1460. X      "  % Display a DirectClass or PseudoClass image.",
  1461. X      "  %",
  1462. X      "  % Parameters: ",
  1463. X      "  %   x & y translation.",
  1464. X      "  %   x & y scale.",
  1465. X      "  %   image columns & rows.",
  1466. X      "  %   class: 0-DirectClass or 1-PseudoClass.",
  1467. X      "  %   compression: 0-RunlengthEncodedCompression or 1-NoCompression.",
  1468. X      "  %   hex color packets.",
  1469. X      "  %",
  1470. X      "  gsave",
  1471. X      "  currentfile buffer readline pop",
  1472. X      "  token pop /x exch def",
  1473. X      "  token pop /y exch def pop",
  1474. X      "  x y translate",
  1475. X      "  currentfile buffer readline pop",
  1476. X      "  token pop /x exch def",
  1477. X      "  token pop /y exch def pop",
  1478. X      "  x y scale",
  1479. X      "  currentfile buffer readline pop",
  1480. X      "  token pop /columns exch def",
  1481. X      "  token pop /rows exch def pop",
  1482. X      "  currentfile buffer readline pop",
  1483. X      "  token pop /class exch def pop",
  1484. X      "  currentfile buffer readline pop",
  1485. X      "  token pop /compression exch def pop",
  1486. X      "  class 0 gt { PseudoClassImage } { DirectClassImage } ifelse",
  1487. X      "  grestore",
  1488. X      "  showpage",
  1489. X      "} bind def",
  1490. X      "%%EndProlog",
  1491. X      "%%Page:  1 1",
  1492. X      NULL
  1493. X    };
  1494. X
  1495. X  char
  1496. X    **q;
  1497. X
  1498. X  int
  1499. X    flags,
  1500. X    x,
  1501. X    y;
  1502. X
  1503. X  register RunlengthPacket
  1504. X    *p;
  1505. X
  1506. X  register int
  1507. X    i,
  1508. X    j;
  1509. X
  1510. X  time_t
  1511. X    timer;
  1512. X
  1513. X  unsigned char
  1514. X    *pixels;
  1515. X
  1516. X  unsigned int
  1517. X    height,
  1518. X    number_packets,
  1519. X    page_width,
  1520. X    page_height,
  1521. X    x_resolution,
  1522. X    y_resolution,
  1523. X    width;
  1524. X
  1525. X  unsigned long
  1526. X    scale_factor;
  1527. X
  1528. X  /*
  1529. X    Open output image file.
  1530. X  */
  1531. X  OpenImage(image,"w");
  1532. X  if (image->file == (FILE *) NULL)
  1533. X    {
  1534. X      Warning("unable to open file",image->filename);
  1535. X      return(False);
  1536. X    }
  1537. X  /*
  1538. X    Scale image to size of Postscript page.
  1539. X  */
  1540. X  if (encapsulated)
  1541. X    {
  1542. X      x=0;
  1543. X      y=0;
  1544. X      width=image->columns;
  1545. X      height=image->rows;
  1546. X      (void) XParseGeometry(geometry,&x,&y,&page_width,&page_height);
  1547. X    }
  1548. X  else
  1549. X    {
  1550. X      /*
  1551. X        Center image on Postscript page.
  1552. X      */
  1553. X      page_width=PageWidth;
  1554. X      page_height=PageHeight;
  1555. X      flags=XParseGeometry(geometry,&x,&y,&page_width,&page_height);
  1556. X      scale_factor=UpShift(page_width-(2*PageSideMargin))/image->columns;
  1557. X      if (scale_factor > (UpShift(page_height-(2*PageTopMargin))/image->rows))
  1558. X        scale_factor=UpShift(page_height-(2*PageTopMargin))/image->rows;
  1559. X      width=DownShift(image->columns*scale_factor);
  1560. X      height=DownShift(image->rows*scale_factor);
  1561. X      if (((flags & XValue) == 0) && ((flags & YValue) == 0))
  1562. X        {
  1563. X          int
  1564. X            delta_x,
  1565. X            delta_y;
  1566. X
  1567. X          delta_x=page_width-(width+(2*PageSideMargin));
  1568. X          delta_y=page_height-(height+(2*PageTopMargin));
  1569. X          if (delta_x >= 0)
  1570. X            x=delta_x/2+PageSideMargin;
  1571. X          else
  1572. X            x=PageSideMargin;
  1573. X          if (delta_y >= 0)
  1574. X            y=delta_y/2+PageTopMargin;
  1575. X          else
  1576. X            y=PageTopMargin;
  1577. X        }
  1578. X    }
  1579. X  /*
  1580. X    Scale relative to dots-per-inch.
  1581. X  */
  1582. X  x_resolution=XResolution;
  1583. X  y_resolution=YResolution;
  1584. X  if (density != (char *) NULL)
  1585. X    {
  1586. X      int
  1587. X        z;
  1588. X
  1589. X      /*
  1590. X        User specified density.
  1591. X      */
  1592. X      flags=XParseGeometry(density,&z,&z,&x_resolution,&y_resolution);
  1593. X      if ((flags & WidthValue) == 0)
  1594. X        x_resolution=XResolution;
  1595. X      if ((flags & HeightValue) == 0)
  1596. X        y_resolution=x_resolution;
  1597. X    }
  1598. X  scale_factor=UpShift(XResolution)/x_resolution;
  1599. X  width=DownShift(width*scale_factor);
  1600. X  scale_factor=UpShift(YResolution)/y_resolution;
  1601. X  height=DownShift(height*scale_factor);
  1602. X  /*
  1603. X    Output Postscript header.
  1604. X  */
  1605. X  if (!encapsulated)
  1606. X    (void) fprintf(image->file,"%%!PS-Adobe-3.0\n");
  1607. X  else
  1608. X    (void) fprintf(image->file,"%%!PS-Adobe-3.0 EPSF-3.0\n");
  1609. X  (void) fprintf(image->file,"%%%%Title: %s\n",image->filename);
  1610. X  (void) fprintf(image->file,"%%%%Creator: ImageMagick\n");
  1611. X  timer=time((time_t) NULL);
  1612. X  (void) localtime(&timer);
  1613. X  (void) fprintf(image->file,"%%%%CreationDate: %s",ctime(&timer));
  1614. X  (void) fprintf(image->file,"%%%%BoundingBox: %d %d %d %d\n",x,y,x+(int) width,
  1615. X    y+(int) height);
  1616. X  if (!encapsulated)
  1617. X    {
  1618. X      (void) fprintf(image->file,"%%%%Orientation: Portrait\n");
  1619. X      (void) fprintf(image->file,"%%%%PageOrder: Ascend\n");
  1620. X    }
  1621. X  (void) fprintf(image->file,"%%%%Pages: %d\n",!encapsulated);
  1622. X  (void) fprintf(image->file,"%%%%EndComments\n");
  1623. X  /*
  1624. X    Output Postscript commands.
  1625. X  */
  1626. X  for (q=Postscript; *q; q++)
  1627. X    (void) fprintf(image->file,"%s\n",*q);
  1628. X  if (encapsulated)
  1629. X    (void) fprintf(image->file,"userdict begin\n");
  1630. X  (void) fprintf(image->file,"%%%%BeginData:\n");
  1631. X  (void) fprintf(image->file,"DisplayImage\n");
  1632. X  /*
  1633. X    Output image data.
  1634. X  */
  1635. X  p=image->pixels;
  1636. X  if ((image->class == DirectClass) || (image->colors > 256))
  1637. X    {
  1638. X      (void) fprintf(image->file,"%d %d\n%u %u\n%u %u\n%d\n%d\n",x,y,width,
  1639. X        height,image->columns,image->rows,(image->class == PseudoClass),
  1640. X        image->compression == NoCompression);
  1641. X      switch (image->compression)
  1642. X      {
  1643. X        case RunlengthEncodedCompression:
  1644. X        default:
  1645. X        {
  1646. X          register unsigned char
  1647. X            *q;
  1648. X
  1649. X          /*
  1650. X            Allocate pixel array.
  1651. X          */
  1652. X          number_packets=3*image->columns*image->rows;
  1653. X          pixels=(unsigned char *) malloc(number_packets*sizeof(unsigned char));
  1654. X          if (pixels == (unsigned char *) NULL)
  1655. X            {
  1656. X              Warning("memory allocation error",(char *) NULL);
  1657. X              return(False);
  1658. X            }
  1659. X          /*
  1660. X            Dump LZW encoded pixels.
  1661. X          */
  1662. X          q=pixels;
  1663. X          for (i=0; i < image->packets; i++)
  1664. X          {
  1665. X            for (j=0; j <= (int) p->length; j++)
  1666. X            {
  1667. X              *q++=p->red;
  1668. X              *q++=p->green;
  1669. X              *q++=p->blue;
  1670. X            }
  1671. X            p++;
  1672. X          }
  1673. X          (void) LZWEncodeFilter(image->file,pixels,number_packets);
  1674. X          (void) fprintf(image->file,">");
  1675. X          (void) free((char *) pixels);
  1676. X          break;
  1677. X        }
  1678. X        case NoCompression:
  1679. X        {
  1680. X          /*
  1681. X            Dump uncompressed DirectColor packets.
  1682. X          */
  1683. X          x=0;
  1684. X          for (i=0; i < image->packets; i++)
  1685. X          {
  1686. X            for (j=0; j <= ((int) p->length); j++)
  1687. X            {
  1688. X              x++;
  1689. X              (void) fprintf(image->file,"%02x%02x%02x",p->red,p->green,
  1690. X                p->blue);
  1691. X              if (x == 12)
  1692. X                {
  1693. X                  x=0;
  1694. X                  (void) fprintf(image->file,"\n");
  1695. X                }
  1696. X            }
  1697. X            p++;
  1698. X          }
  1699. X          break;
  1700. X        }
  1701. X      }
  1702. X    }
  1703. X  else
  1704. X    {
  1705. X      (void) fprintf(image->file,"%d %d\n%u %u\n%u %u\n%d\n%d\n",x,y,width,
  1706. X        height,image->columns,image->rows,(image->class == PseudoClass),
  1707. X        image->compression == NoCompression);
  1708. X      /*
  1709. X        Dump number of colors and colormap.
  1710. X      */
  1711. X      (void) fprintf(image->file,"%u\n",image->colors);
  1712. X      for (i=0; i < image->colors; i++)
  1713. X        (void) fprintf(image->file,"%02x%02x%02x\n",image->colormap[i].red,
  1714. X          image->colormap[i].green,image->colormap[i].blue);
  1715. X      switch (image->compression)
  1716. X      {
  1717. X        case RunlengthEncodedCompression:
  1718. X        default:
  1719. X        {
  1720. X          register unsigned char
  1721. X            *q;
  1722. X
  1723. X          /*
  1724. X            Allocate pixel array.
  1725. X          */
  1726. X          number_packets=image->columns*image->rows;
  1727. X          pixels=(unsigned char *) malloc(number_packets*sizeof(unsigned char));
  1728. X          if (pixels == (unsigned char *) NULL)
  1729. X            {
  1730. X              Warning("memory allocation error",(char *) NULL);
  1731. X              return(False);
  1732. X            }
  1733. X          /*
  1734. X            Dump LZW encoded pixels.
  1735. X          */
  1736. X          q=pixels;
  1737. X          for (i=0; i < image->packets; i++)
  1738. X          {
  1739. X            for (j=0; j <= (int) p->length; j++)
  1740. X              *q++=(unsigned char) p->index;
  1741. X            p++;
  1742. X          }
  1743. X          (void) LZWEncodeFilter(image->file,pixels,number_packets);
  1744. X          (void) fprintf(image->file,">");
  1745. X          (void) free((char *) pixels);
  1746. X          break;
  1747. X        }
  1748. X        case NoCompression:
  1749. X        {
  1750. X          /*
  1751. X            Dump uncompressed PseudoColor packets.
  1752. X          */
  1753. X          x=0;
  1754. X          for (i=0; i < image->packets; i++)
  1755. X          {
  1756. X            for (j=0; j <= ((int) p->length); j++)
  1757. X            {
  1758. X              x++;
  1759. X              (void) fprintf(image->file,"%02x",p->index);
  1760. X              if (x == 36)
  1761. X                {
  1762. X                  x=0;
  1763. X                  (void) fprintf(image->file,"\n");
  1764. X                }
  1765. X            }
  1766. X            p++;
  1767. X          }
  1768. X          break;
  1769. X        }
  1770. X      }
  1771. X    }
  1772. X  (void) fprintf(image->file,"\n");
  1773. X  (void) fprintf(image->file,"%%%%EndData\n");
  1774. X  if (encapsulated)
  1775. X    (void) fprintf(image->file,"end\n");
  1776. X  (void) fprintf(image->file,"%%%%PageTrailer\n");
  1777. X  (void) fprintf(image->file,"%%%%Trailer\n");
  1778. X  (void) fprintf(image->file,"%%%%EOF\n");
  1779. X  CloseImage(image);
  1780. X  return(True);
  1781. }
  1782. X
  1783. /*
  1784. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1785. %                                                                             %
  1786. %                                                                             %
  1787. %                                                                             %
  1788. %   W r i t e R G B I m a g e                                                 %
  1789. %                                                                             %
  1790. SHAR_EOF
  1791. true || echo 'restore of ImageMagick/encode.c failed'
  1792. fi
  1793. echo 'End of ImageMagick part 35'
  1794. echo 'File ImageMagick/encode.c is continued in part 36'
  1795. echo 36 > _shar_seq_.tmp
  1796. exit 0
  1797.  
  1798. exit 0 # Just in case...
  1799. -- 
  1800.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1801. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1802.  "It's intuitively obvious to the |
  1803.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1804.