home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume34 / imagemagick / part22 < prev    next >
Text File  |  1992-12-14  |  59KB  |  1,983 lines

  1. Newsgroups: comp.sources.misc
  2. From: cristy@eplrx7.es.duPont.com (John Cristy)
  3. Subject:  v34i050:  imagemagick - X11 image processing and display v2.2, Part22/26
  4. Message-ID: <1992Dec15.035815.22938@sparky.imd.sterling.com>
  5. X-Md4-Signature: 53d648fcb196c409bfbafa46ed01d323
  6. Date: Tue, 15 Dec 1992 03:58:15 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: cristy@eplrx7.es.duPont.com (John Cristy)
  10. Posting-number: Volume 34, Issue 50
  11. Archive-name: imagemagick/part22
  12. Environment: UNIX, VMS, X11, SGI, DEC, Cray, Sun, Vax
  13.  
  14. #!/bin/sh
  15. # this is Part.22 (part 22 of a multipart archive)
  16. # do not concatenate these parts, unpack them in order with /bin/sh
  17. # file ImageMagick/alien.c continued
  18. #
  19. if test ! -r _shar_seq_.tmp; then
  20.     echo 'Please unpack part 1 first!'
  21.     exit 1
  22. fi
  23. (read Scheck
  24.  if test "$Scheck" != 22; then
  25.     echo Please unpack part "$Scheck" next!
  26.     exit 1
  27.  else
  28.     exit 0
  29.  fi
  30. ) < _shar_seq_.tmp || exit 1
  31. if test ! -f _shar_wnt_.tmp; then
  32.     echo 'x - still skipping ImageMagick/alien.c'
  33. else
  34. echo 'x - continuing file ImageMagick/alien.c'
  35. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/alien.c' &&
  36. X              q->length=0;
  37. X              q++;
  38. X            }
  39. X            p++;
  40. X          }
  41. X        if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
  42. X          p++;
  43. X      }
  44. X    else
  45. X      if (image->class == PseudoClass)
  46. X        for (y=0; y < image->rows; y++)
  47. X        {
  48. X          /*
  49. X            Convert PseudoColor scanline to runlength-encoded color packets.
  50. X          */
  51. X          for (x=0; x < image->columns; x++)
  52. X          {
  53. X            index=(*p++);
  54. X            q->red=image->colormap[index].red;
  55. X            q->green=image->colormap[index].green;
  56. X            q->blue=image->colormap[index].blue;
  57. X            q->index=index;
  58. X            q->length=0;
  59. X            q++;
  60. X          }
  61. X          if ((image->columns % 2) != 0)
  62. X            p++;
  63. X        }
  64. X      else
  65. X        for (y=0; y < image->rows; y++)
  66. X        {
  67. X          /*
  68. X            Convert DirectColor scanline to runlength-encoded color packets.
  69. X          */
  70. X          for (x=0; x < image->columns; x++)
  71. X          {
  72. X            q->index=(unsigned short) (image->alpha ? (*p++) : 0);
  73. X            if (sun_header.type == RT_STANDARD)
  74. X              {
  75. X                q->blue=(*p++);
  76. X                q->green=(*p++);
  77. X                q->red=(*p++);
  78. X              }
  79. X            else
  80. X              {
  81. X                q->red=(*p++);
  82. X                q->green=(*p++);
  83. X                q->blue=(*p++);
  84. X              }
  85. X            if (image->colors > 0)
  86. X              {
  87. X                q->red=image->colormap[q->red].red;
  88. X                q->green=image->colormap[q->green].green;
  89. X                q->blue=image->colormap[q->blue].blue;
  90. X              }
  91. X            q->length=0;
  92. X            q++;
  93. X          }
  94. X          if (((image->columns % 2) != 0) && (image->alpha == False))
  95. X            p++;
  96. X        }
  97. X    (void) free((char *) sun_pixels);
  98. X    if (image->class == PseudoClass)
  99. X      CompressColormap(image);
  100. X    /*
  101. X      Proceed to next image.
  102. X    */
  103. X    status=ReadData((char *) &sun_header,1,sizeof(Rasterfile),image->file);
  104. X    if (status == True)
  105. X      {
  106. X        /*
  107. X          Allocate image structure.
  108. X        */
  109. X        image->next=AllocateImage("SUN");
  110. X        if (image->next == (Image *) NULL)
  111. X          {
  112. X            DestroyImages(image);
  113. X            return((Image *) NULL);
  114. X          }
  115. X        image->next->file=image->file;
  116. X        (void) sprintf(image->next->filename,"%s.%u\0",alien_info->filename,
  117. X          image->scene+1);
  118. X        image->next->scene=image->scene+1;
  119. X        image->next->last=image;
  120. X        image=image->next;
  121. X      }
  122. X  } while (status == True);
  123. X  while (image->last != (Image *) NULL)
  124. X    image=image->last;
  125. X  CloseImage(image);
  126. X  return(image);
  127. }
  128. X
  129. #ifdef AlienTIFF
  130. #include "tiff.h"
  131. #include "tiffio.h"
  132. X
  133. /*
  134. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  135. %                                                                             %
  136. %                                                                             %
  137. %                                                                             %
  138. %  R e a d T I F F I m a g e                                                  %
  139. %                                                                             %
  140. %                                                                             %
  141. %                                                                             %
  142. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  143. %
  144. %  Function ReadTIFFImage reads an image file and returns it.  It allocates the
  145. %  memory necessary for the new Image structure and returns a pointer to the
  146. %  new image.
  147. %
  148. %  The format of the ReadTIFFImage routine is:
  149. %
  150. %      image=ReadTIFFImage(alien_info)
  151. %
  152. %  A description of each parameter follows:
  153. %
  154. %    o image:  Function ReadTIFFImage returns a pointer to the image after
  155. %      reading.  A null image is returned if there is a a memory shortage or
  156. %      if the image cannot be read.
  157. %
  158. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  159. %
  160. %
  161. */
  162. static Image *ReadTIFFImage(alien_info)
  163. AlienInfo
  164. X  *alien_info;
  165. {
  166. X  Image
  167. X    *image;
  168. X
  169. X  int
  170. X    range;
  171. X
  172. X  register int
  173. X    i,
  174. X    quantum,
  175. X    x,
  176. X    y;
  177. X
  178. X  register RunlengthPacket
  179. X    *q;
  180. X
  181. X  TIFF
  182. X    *tiff;
  183. X
  184. X  unsigned int
  185. X    status;
  186. X
  187. X  unsigned long
  188. X    height,
  189. X    width;
  190. X
  191. X  unsigned short
  192. X    bits_per_sample,
  193. X    max_sample_value,
  194. X    min_sample_value,
  195. X    photometric,
  196. X    samples_per_pixel;
  197. X
  198. X  /*
  199. X    Allocate image structure.
  200. X  */
  201. X  image=AllocateImage("TIFF");
  202. X  if (image == (Image *) NULL)
  203. X    return((Image *) NULL);
  204. X  /*
  205. X    Open TIFF image tiff.
  206. X  */
  207. X  (void) strcpy(image->filename,alien_info->filename);
  208. X  tiff=TIFFOpen(image->filename,"r");
  209. X  if (tiff == (TIFF *) NULL)
  210. X    {
  211. X      Warning("unable to open tiff image",image->filename);
  212. X      DestroyImage(image);
  213. X      return((Image *) NULL);
  214. X    }
  215. X  do
  216. X  {
  217. X    if (alien_info->verbose)
  218. X      TIFFPrintDirectory(tiff,stderr,False);
  219. X    /*
  220. X      Allocate memory for the image and pixel buffer.
  221. X    */
  222. X    TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width);
  223. X    TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height);
  224. X    for (quantum=1; quantum <= 16; quantum<<=1)
  225. X    {
  226. X      image->columns=width/quantum;
  227. X      image->rows=height/quantum;
  228. X      image->packets=image->columns*image->rows;
  229. X      image->pixels=(RunlengthPacket *)
  230. X        malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  231. X      if ((image->pixels != (RunlengthPacket *) NULL))
  232. X        break;
  233. X    }
  234. X    image->comments=(char *)
  235. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  236. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  237. X        (image->comments == (char *) NULL))
  238. X      {
  239. X        Warning("unable to allocate memory",(char *) NULL);
  240. X        DestroyImages(image);
  241. X        TIFFClose(tiff);
  242. X        return((Image *) NULL);
  243. X      }
  244. X    (void) sprintf(image->comments,"\n  Imported from TIFF image %s.\n\0",
  245. X      image->filename);
  246. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
  247. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
  248. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
  249. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
  250. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel);
  251. X    range=max_sample_value-min_sample_value;
  252. X    if ((bits_per_sample > 8) || (samples_per_pixel > 1) || TIFFIsTiled(tiff))
  253. X      {
  254. X        register unsigned long
  255. X          *p,
  256. X          *pixels;
  257. X
  258. X        /*
  259. X          Convert TIFF image to DirectClass MIFF image.
  260. X        */
  261. X        image->alpha=samples_per_pixel > 3;
  262. X        pixels=(unsigned long *)
  263. X          malloc(image->columns*image->rows*sizeof(unsigned long));
  264. X        if (pixels == (unsigned long *) NULL)
  265. X          {
  266. X            Warning("unable to allocate memory",(char *) NULL);
  267. X            DestroyImages(image);
  268. X            TIFFClose(tiff);
  269. X            return((Image *) NULL);
  270. X          }
  271. X        if (quantum > 1)
  272. X          Warning("not enough memory","cropping required");
  273. X        status=TIFFReadRGBAImage(tiff,image->columns,image->rows,pixels,0);
  274. X        if (status == False)
  275. X          {
  276. X            Warning("unable to read TIFF image",(char *) NULL);
  277. X            (void) free((char *) pixels);
  278. X            DestroyImages(image);
  279. X            TIFFClose(tiff);
  280. X            return((Image *) NULL);
  281. X          }
  282. X        /*
  283. X          Convert image to DirectClass runlength-encoded packets.
  284. X        */
  285. X        q=image->pixels;
  286. X        for (y=image->rows-1; y >= 0; y--)
  287. X        {
  288. X          p=pixels+y*image->columns;
  289. X          for (x=0; x < image->columns; x++)
  290. X          {
  291. X            q->red=TIFFGetR(*p);
  292. X            q->green=TIFFGetG(*p);
  293. X            q->blue=TIFFGetB(*p);
  294. X            q->index=(unsigned short) (image->alpha ? TIFFGetA(*p) : 0);
  295. X            q->length=0;
  296. X            p++;
  297. X            q++;
  298. X          }
  299. X        }
  300. X        (void) free((char *) pixels);
  301. X        if (samples_per_pixel == 1)
  302. X          QuantizeImage(image,(unsigned int) range,8,False,RGBColorspace,True);
  303. X      }
  304. X    else
  305. X      {
  306. X        unsigned char
  307. X          *p,
  308. X          *scanline;
  309. X
  310. X        /*
  311. X          Convert TIFF image to PseudoClass MIFF image.
  312. X        */
  313. X        image->class=PseudoClass;
  314. X        image->colors=range+1;
  315. X        image->colormap=(ColorPacket *)
  316. X          malloc(image->colors*sizeof(ColorPacket));
  317. X        scanline=(unsigned char *) malloc(TIFFScanlineSize(tiff));
  318. X        if ((image->colormap == (ColorPacket *) NULL) ||
  319. X            (scanline == (unsigned char *) NULL))
  320. X          {
  321. X            Warning("unable to allocate memory",(char *) NULL);
  322. X            DestroyImages(image);
  323. X            TIFFClose(tiff);
  324. X            return((Image *) NULL);
  325. X          }
  326. X        /*
  327. X          Create colormap.
  328. X        */
  329. X        switch (photometric)
  330. X        {
  331. X          case PHOTOMETRIC_MINISBLACK:
  332. X          {
  333. X            for (i=0; i < image->colors; i++)
  334. X            {
  335. X              image->colormap[i].red=(MaxRGB*i)/range;
  336. X              image->colormap[i].green=(MaxRGB*i)/range;
  337. X              image->colormap[i].blue=(MaxRGB*i)/range;
  338. X            }
  339. X            break;
  340. X          }
  341. X          case PHOTOMETRIC_MINISWHITE:
  342. X          {
  343. X            for (i=0; i < image->colors; i++)
  344. X            {
  345. X              image->colormap[i].red=((range-i)*MaxRGB)/range;
  346. X              image->colormap[i].green=((range-i)*MaxRGB)/range;
  347. X              image->colormap[i].blue=((range-i)*MaxRGB)/range;
  348. X            }
  349. X            break;
  350. X          }
  351. X          case PHOTOMETRIC_PALETTE:
  352. X          {
  353. X            unsigned short
  354. X              *blue_colormap,
  355. X              *green_colormap,
  356. X              *red_colormap;
  357. X
  358. X            TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,&green_colormap,
  359. X              &blue_colormap);
  360. X            for (i=0; i < image->colors; i++)
  361. X            {
  362. X              image->colormap[i].red=((int) red_colormap[i]*MaxRGB)/65535;
  363. X              image->colormap[i].green=((int) green_colormap[i]*MaxRGB)/65535;
  364. X              image->colormap[i].blue=((int) blue_colormap[i]*MaxRGB)/65535;
  365. X            }
  366. X            break;
  367. X          }
  368. X          default:
  369. X            break;
  370. X        }
  371. X        /*
  372. X          Convert image to PseudoClass runlength-encoded packets.
  373. X        */
  374. X        if (quantum > 1)
  375. X          Warning("not enough memory","subsampling required");
  376. X        q=image->pixels;
  377. X        for (y=0; y < image->rows; y++)
  378. X        {
  379. X          for (i=0; i < quantum; i++)
  380. X            TIFFReadScanline(tiff,scanline,y*quantum+i,0);
  381. X          p=scanline;
  382. X          switch (photometric)
  383. X          {
  384. X            case PHOTOMETRIC_MINISBLACK:
  385. X            case PHOTOMETRIC_MINISWHITE:
  386. X            {
  387. X              switch (bits_per_sample)
  388. X              {
  389. X                case 1:
  390. X                {
  391. X                  register int
  392. X                    bit;
  393. X
  394. X                  for (x=0; x < (image->columns-7); x+=8)
  395. X                  {
  396. X                    for (bit=7; bit >= 0; bit--)
  397. X                    {
  398. X                      q->index=((*p) & (0x01 << bit) ? 0x01 : 0x00);
  399. X                      q->red=image->colormap[q->index].red;
  400. X                      q->green=image->colormap[q->index].green;
  401. X                      q->blue=image->colormap[q->index].blue;
  402. X                      q->length=0;
  403. X                      q++;
  404. X                    }
  405. X                    p+=quantum;
  406. X                  }
  407. X                  if ((image->columns % 8) != 0)
  408. X                    {
  409. X                      for (bit=7; bit >= (8-(image->columns % 8)); bit--)
  410. X                      {
  411. X                        q->index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
  412. X                        q->red=image->colormap[q->index].red;
  413. X                        q->green=image->colormap[q->index].green;
  414. X                        q->blue=image->colormap[q->index].blue;
  415. X                        q->length=0;
  416. X                        q++;
  417. X                      }
  418. X                      p+=quantum;
  419. X                    }
  420. X                  break;
  421. X                }
  422. X                case 2:
  423. X                {
  424. X                  for (x=0; x < image->columns; x+=4)
  425. X                  {
  426. X                    q->index=(*p >> 6) & 0x3;
  427. X                    q->red=image->colormap[q->index].red;
  428. X                    q->green=image->colormap[q->index].green;
  429. X                    q->blue=image->colormap[q->index].blue;
  430. X                    q->length=0;
  431. X                    q++;
  432. X                    q->index=(*p >> 4) & 0x3;
  433. X                    q->red=image->colormap[q->index].red;
  434. X                    q->green=image->colormap[q->index].green;
  435. X                    q->blue=image->colormap[q->index].blue;
  436. X                    q->length=0;
  437. X                    q++;
  438. X                    q->index=(*p >> 2) & 0x3;
  439. X                    q->red=image->colormap[q->index].red;
  440. X                    q->green=image->colormap[q->index].green;
  441. X                    q->blue=image->colormap[q->index].blue;
  442. X                    q->length=0;
  443. X                    q++;
  444. X                    q->index=(*p) & 0x3;
  445. X                    q->red=image->colormap[q->index].red;
  446. X                    q->green=image->colormap[q->index].green;
  447. X                    q->blue=image->colormap[q->index].blue;
  448. X                    q->length=0;
  449. X                    q++;
  450. X                    p+=quantum;
  451. X                  }
  452. X                  break;
  453. X                }
  454. X                case 4:
  455. X                {
  456. X                  for (x=0; x < image->columns; x+=2)
  457. X                  {
  458. X                    q->index=(*p >> 4) & 0xf;
  459. X                    q->red=image->colormap[q->index].red;
  460. X                    q->green=image->colormap[q->index].green;
  461. X                    q->blue=image->colormap[q->index].blue;
  462. X                    q->length=0;
  463. X                    q++;
  464. X                    q->index=(*p) & 0xf;
  465. X                    q->red=image->colormap[q->index].red;
  466. X                    q->green=image->colormap[q->index].green;
  467. X                    q->blue=image->colormap[q->index].blue;
  468. X                    q->length=0;
  469. X                    q++;
  470. X                    p+=quantum;
  471. X                  }
  472. X                  break;
  473. X                }
  474. X                case 8:
  475. X                {
  476. X                  for (x=0; x < image->columns; x++)
  477. X                  {
  478. X                    q->index=(*p);
  479. X                    q->red=image->colormap[q->index].red;
  480. X                    q->green=image->colormap[q->index].green;
  481. X                    q->blue=image->colormap[q->index].blue;
  482. X                    q->length=0;
  483. X                    q++;
  484. X                    p+=quantum;
  485. X                  }
  486. X                  break;
  487. X                }
  488. X                default:
  489. X                  break;
  490. X              }
  491. X              break;
  492. X            }
  493. X            case PHOTOMETRIC_PALETTE:
  494. X            {
  495. X              for (x=0; x < image->columns; x++)
  496. X              {
  497. X                q->index=(*p);
  498. X                q->red=image->colormap[q->index].red;
  499. X                q->green=image->colormap[q->index].green;
  500. X                q->blue=image->colormap[q->index].blue;
  501. X                q->length=0;
  502. X                q++;
  503. X                p+=quantum;
  504. X              }
  505. X              break;
  506. X            }
  507. X            default:
  508. X              break;
  509. X          }
  510. X        }
  511. X        (void) free((char *) scanline);
  512. X        CompressColormap(image);
  513. X      }
  514. X    /*
  515. X      Proceed to next image.
  516. X    */
  517. X    status=TIFFReadDirectory(tiff);
  518. X    if (status == True)
  519. X      {
  520. X        /*
  521. X          Allocate image structure.
  522. X        */
  523. X        image->next=AllocateImage("TIFF");
  524. X        if (image->next == (Image *) NULL)
  525. X          {
  526. X            DestroyImages(image);
  527. X            return((Image *) NULL);
  528. X          }
  529. X        image->next->file=image->file;
  530. X        (void) sprintf(image->next->filename,"%s.%u\0",alien_info->filename,
  531. X          image->scene+1);
  532. X        image->next->scene=image->scene+1;
  533. X        image->next->last=image;
  534. X        image=image->next;
  535. X      }
  536. X  } while (status == True);
  537. X  TIFFClose(tiff);
  538. X  while (image->last != (Image *) NULL)
  539. X    image=image->last;
  540. X  return(image);
  541. }
  542. #else
  543. static Image *ReadTIFFImage(alien_info)
  544. AlienInfo
  545. X  *alien_info;
  546. {
  547. X  Warning("TIFF library is not available",alien_info->filename);
  548. X  return(ReadImage(alien_info->filename));
  549. }
  550. #endif
  551. X
  552. /*
  553. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  554. %                                                                             %
  555. %                                                                             %
  556. %                                                                             %
  557. %  R e a d Y U V I m a g e                                                    %
  558. %                                                                             %
  559. %                                                                             %
  560. %                                                                             %
  561. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  562. %
  563. %  Function ReadYUVImage reads an image file and returns it.  It allocates the
  564. %  memory necessary for the new Image structure and returns a pointer to the
  565. %  new image.  U and V, normally -0.5 through 0.5, are expected to be
  566. %  normalized to the range 0 through 255 fit withing a byte.
  567. %
  568. %  The format of the ReadYUVImage routine is:
  569. %
  570. %      image=ReadYUVImage(alien_info)
  571. %
  572. %  A description of each parameter follows:
  573. %
  574. %    o image:  Function ReadYUVImage returns a pointer to the image after
  575. %      reading.  A null image is returned if there is a a memory shortage or
  576. %      if the image cannot be read.
  577. %
  578. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  579. %
  580. %
  581. */
  582. static Image *ReadYUVImage(alien_info)
  583. AlienInfo
  584. X  *alien_info;
  585. {
  586. X  Image
  587. X    *image;
  588. X
  589. X  int
  590. X    x,
  591. X    y;
  592. X
  593. X  register int
  594. X    i;
  595. X
  596. X  register RunlengthPacket
  597. X    *q;
  598. X
  599. X  register unsigned char
  600. X    *p;
  601. X
  602. X  unsigned char
  603. X    *rgb_pixels;
  604. X
  605. X  unsigned int
  606. X    height,
  607. X    width;
  608. X
  609. X  /*
  610. X    Allocate image structure.
  611. X  */
  612. X  image=AllocateImage("YUV");
  613. X  if (image == (Image *) NULL)
  614. X    return((Image *) NULL);
  615. X  /*
  616. X    Open image file.
  617. X  */
  618. X  (void) strcpy(image->filename,alien_info->filename);
  619. X  OpenImage(image,"r");
  620. X  if (image->file == (FILE *) NULL)
  621. X    {
  622. X      Warning("unable to open file",image->filename);
  623. X      DestroyImage(image);
  624. X      return((Image *) NULL);
  625. X    }
  626. X  /*
  627. X    Create image.
  628. X  */
  629. X  width=512;
  630. X  height=512;
  631. X  if (alien_info->geometry != (char *) NULL)
  632. X    (void) XParseGeometry(alien_info->geometry,&x,&y,&width,&height);
  633. X  image->columns=width;
  634. X  image->rows=height;
  635. X  image->packets=image->columns*image->rows;
  636. X  rgb_pixels=(unsigned char *)
  637. X    malloc((unsigned int) image->packets*3*sizeof(unsigned char));
  638. X  image->pixels=(RunlengthPacket *)
  639. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  640. X  if ((rgb_pixels == (unsigned char *) NULL) ||
  641. X      (image->pixels == (RunlengthPacket *) NULL))
  642. X    {
  643. X      Warning("memory allocation error",(char *) NULL);
  644. X      DestroyImage(image);
  645. X      return((Image *) NULL);
  646. X    }
  647. X  /*
  648. X    Convert raster image to runlength-encoded packets.
  649. X  */
  650. X  (void) ReadData((char *) rgb_pixels,3,(int) (image->columns*image->rows),
  651. X    image->file);
  652. X  p=rgb_pixels;
  653. X  q=image->pixels;
  654. X  for (i=0; i < (image->columns*image->rows); i++)
  655. X  {
  656. X    q->red=(*p++);
  657. X    q->green=(*p++);
  658. X    q->blue=(*p++);
  659. X    q->index=0;
  660. X    q->length=0;
  661. X    q++;
  662. X  }
  663. X  (void) free((char *) rgb_pixels);
  664. X  TransformRGBImage(image,YUVColorspace);
  665. X  CloseImage(image);
  666. X  return(image);
  667. }
  668. X
  669. /*
  670. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  671. %                                                                             %
  672. %                                                                             %
  673. %                                                                             %
  674. %  R e a d X B M I m a g e                                                    %
  675. %                                                                             %
  676. %                                                                             %
  677. %                                                                             %
  678. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  679. %
  680. %  Function ReadXBMImage reads an image file and returns it.  It allocates the
  681. %  memory necessary for the new Image structure and returns a pointer to the
  682. %  new image.
  683. %
  684. %  The format of the ReadXBMImage routine is:
  685. %
  686. %      image=ReadXBMImage(alien_info)
  687. %
  688. %  A description of each parameter follows:
  689. %
  690. %    o image:  Function ReadXBMImage returns a pointer to the image after
  691. %      reading.  A null image is returned if there is a a memory shortage or
  692. %      if the image cannot be read.
  693. %
  694. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  695. %
  696. %
  697. */
  698. static Image *ReadXBMImage(alien_info)
  699. AlienInfo
  700. X  *alien_info;
  701. {
  702. X  char
  703. X    data[2048];
  704. X
  705. X  Image
  706. X    *image;
  707. X
  708. X  register int
  709. X    x,
  710. X    y;
  711. X
  712. X  register RunlengthPacket
  713. X    *p;
  714. X
  715. X  register unsigned char
  716. X    bit;
  717. X
  718. X  unsigned int
  719. X    byte;
  720. X
  721. X  /*
  722. X    Allocate image structure.
  723. X  */
  724. X  image=AllocateImage("XBM");
  725. X  if (image == (Image *) NULL)
  726. X    return((Image *) NULL);
  727. X  /*
  728. X    Open image file.
  729. X  */
  730. X  (void) strcpy(image->filename,alien_info->filename);
  731. X  OpenImage(image,"r");
  732. X  if (image->file == (FILE *) NULL)
  733. X    {
  734. X      Warning("unable to open file",image->filename);
  735. X      DestroyImage(image);
  736. X      return((Image *) NULL);
  737. X    }
  738. X  /*
  739. X    Read X bitmap header.
  740. X  */
  741. X  while (fgets(data,sizeof(data)-1,image->file) != (char *) NULL)
  742. X    if (sscanf(data,"#define %*32s %u",&image->columns) == 1)
  743. X      break;
  744. X  while (fgets(data,sizeof(data)-1,image->file) != (char *) NULL)
  745. X    if (sscanf(data,"#define %*32s %u",&image->rows) == 1)
  746. X      break;
  747. X  if ((image->columns == 0) || (image->rows == 0))
  748. X    {
  749. X      Warning("XBM file is not in the correct format",image->filename);
  750. X      DestroyImage(image);
  751. X      return((Image *) NULL);
  752. X    }
  753. X  while (fgets(data,sizeof(data)-1,image->file) != (char *) NULL)
  754. X    if (sscanf(data,"%*[^#] char"))
  755. X      break;
  756. X  if (feof(image->file))
  757. X    {
  758. X      Warning("XBM file is not in the correct format",image->filename);
  759. X      DestroyImage(image);
  760. X      return((Image *) NULL);
  761. X    }
  762. X  /*
  763. X    Create image.
  764. X  */
  765. X  image->packets=image->columns*image->rows;
  766. X  image->pixels=(RunlengthPacket *)
  767. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  768. X  if (image->pixels == (RunlengthPacket *) NULL)
  769. X    {
  770. X      Warning("memory allocation error",(char *) NULL);
  771. X      DestroyImage(image);
  772. X      return((Image *) NULL);
  773. X    }
  774. X  /*
  775. X    Create colormap.
  776. X  */
  777. X  image->class=PseudoClass;
  778. X  image->colors=2;
  779. X  image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  780. X  if (image->colormap == (ColorPacket *) NULL)
  781. X    {
  782. X      Warning("memory allocation error",(char *) NULL);
  783. X      DestroyImage(image);
  784. X      return((Image *) NULL);
  785. X    }
  786. X  image->colormap[0].red=0;
  787. X  image->colormap[0].green=0;
  788. X  image->colormap[0].blue=0;
  789. X  image->colormap[1].red=255;
  790. X  image->colormap[1].green=255;
  791. X  image->colormap[1].blue=255;
  792. X  /*
  793. X    Initial image comment.
  794. X  */
  795. X  image->comments=(char *) malloc((strlen(image->filename)+2048)*sizeof(char));
  796. X  if (image->comments == (char *) NULL)
  797. X    {
  798. X      Warning("memory allocation error",(char *) NULL);
  799. X      DestroyImage(image);
  800. X      return((Image *) NULL);
  801. X    }
  802. X  (void) sprintf(image->comments,"\n  Imported from X11 bitmap file:  %s\n\0",
  803. X    image->filename);
  804. X  /*
  805. X    Convert X bitmap image to runlength-encoded packets.
  806. X  */
  807. X  p=image->pixels;
  808. X  for (y=0; y < image->rows; y++)
  809. X  {
  810. X    bit=0;
  811. X    for (x=0; x < image->columns; x++)
  812. X    {
  813. X      if (bit == 0)
  814. X        (void) fscanf(image->file,"%i,",&byte);
  815. X      p->index=(byte & 0x01) ? 0 : 1;
  816. X      byte>>=1;
  817. X      p->red=image->colormap[p->index].red;
  818. X      p->green=image->colormap[p->index].green;
  819. X      p->blue=image->colormap[p->index].blue;
  820. X      p->length=0;
  821. X      p++;
  822. X      bit++;
  823. X      if (bit == 8)
  824. X        bit=0;
  825. X    }
  826. X  }
  827. X  CloseImage(image);
  828. X  return(image);
  829. }
  830. X
  831. /*
  832. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  833. %                                                                             %
  834. %                                                                             %
  835. %                                                                             %
  836. %   R e a d V I C A R I m a g e                                               %
  837. %                                                                             %
  838. %                                                                             %
  839. %                                                                             %
  840. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  841. %
  842. %  Function ReadVICARImage reads an image file and returns it.  It allocates
  843. %  the memory necessary for the new Image structure and returns a pointer to
  844. %  the new image.
  845. %
  846. %  The format of the ReadVICARImage routine is:
  847. %
  848. %      image=ReadVICARImage(alien_info)
  849. %
  850. %  A description of each parameter follows:
  851. %
  852. %    o image: Function ReadVICARImage returns a pointer to the image after
  853. %      reading.  A null image is returned if there is a a memory shortage or if
  854. %      the image cannot be read.
  855. %
  856. %    o filename: Specifies the name of the image to read.
  857. %
  858. %
  859. */
  860. static Image *ReadVICARImage(alien_info)
  861. AlienInfo
  862. X  *alien_info;
  863. {
  864. #define MaxKeywordLength  2048
  865. X
  866. X  char
  867. X    keyword[MaxKeywordLength],
  868. X    value[MaxKeywordLength];
  869. X
  870. X  Image
  871. X    *image;
  872. X
  873. X  long int
  874. X    count;
  875. X
  876. X  register int
  877. X    c,
  878. X    i;
  879. X
  880. X  register RunlengthPacket
  881. X    *q;
  882. X
  883. X  register unsigned char
  884. X    *p;
  885. X
  886. X  unsigned char
  887. X    *vicar_pixels;
  888. X
  889. X  unsigned int
  890. X    header_length,
  891. X    status,
  892. X    value_expected;
  893. X
  894. X  /*
  895. X    Allocate image structure.
  896. X  */
  897. X  image=AllocateImage("VICAR");
  898. X  if (image == (Image *) NULL)
  899. X    return((Image *) NULL);
  900. X  /*
  901. X    Open image file.
  902. X  */
  903. X  (void) strcpy(image->filename,alien_info->filename);
  904. X  OpenImage(image,"r");
  905. X  if (image->file == (FILE *) NULL)
  906. X    {
  907. X      Warning("unable to open file",image->filename);
  908. X      DestroyImage(image);
  909. X      return((Image *) NULL);
  910. X    }
  911. X  /*
  912. X    Decode image header.
  913. X  */
  914. X  c=fgetc(image->file);
  915. X  count=1;
  916. X  if (c == EOF)
  917. X    {
  918. X      DestroyImage(image);
  919. X      return((Image *) NULL);
  920. X    }
  921. X  while (isgraph(c) && ((image->columns*image->rows) == 0))
  922. X  {
  923. X    register char
  924. X      *p;
  925. X
  926. X    if (!isalnum(c))
  927. X      {
  928. X        c=fgetc(image->file);
  929. X        count++;
  930. X      }
  931. X    else
  932. X      {
  933. X        /*
  934. X          Determine a keyword and its value.
  935. X        */
  936. X        p=keyword;
  937. X        do
  938. X        {
  939. X          if ((p-keyword) < (MaxKeywordLength-1))
  940. X            *p++=(char) c;
  941. X          c=fgetc(image->file);
  942. X          count++;
  943. X        } while (isalnum(c) || (c == '_'));
  944. X        *p='\0';
  945. X        value_expected=False;
  946. X        while (isspace(c) || (c == '='))
  947. X        {
  948. X          if (c == '=')
  949. X            value_expected=True;
  950. X          c=fgetc(image->file);
  951. X          count++;
  952. X        }
  953. X        if (value_expected == False)
  954. X          continue;
  955. X        p=value;
  956. X        while (isalnum(c))
  957. X        {
  958. X          if ((p-value) < (MaxKeywordLength-1))
  959. X            *p++=(char) c;
  960. X          c=fgetc(image->file);
  961. X          count++;
  962. X        }
  963. X        *p='\0';
  964. X        /*
  965. X          Assign a value to the specified keyword.
  966. X        */
  967. X        if (strcmp(keyword,"LABEL_RECORDS") == 0)
  968. X          header_length=(unsigned int) atoi(value);
  969. X        if (strcmp(keyword,"LBLSIZE") == 0)
  970. X          header_length=(unsigned int) atoi(value);
  971. X        if (strcmp(keyword,"RECORD_BYTES") == 0)
  972. X          image->columns=(unsigned int) atoi(value);
  973. X        if (strcmp(keyword,"NS") == 0)
  974. X          image->columns=(unsigned int) atoi(value);
  975. X        if (strcmp(keyword,"LINES") == 0)
  976. X          image->rows=(unsigned int) atoi(value);
  977. X        if (strcmp(keyword,"NL") == 0)
  978. X          image->rows=(unsigned int) atoi(value);
  979. X      }
  980. X    while (isspace(c))
  981. X    {
  982. X      c=fgetc(image->file);
  983. X      count++;
  984. X    }
  985. X  }
  986. X  /*
  987. X    Read the rest of the header.
  988. X  */
  989. X  while (count < header_length)
  990. X  {
  991. X    c=fgetc(image->file);
  992. X    count++;
  993. X  }
  994. X  /*
  995. X    Verify that required image information is defined.
  996. X  */
  997. X  if ((image->columns*image->rows) == 0)
  998. X    {
  999. X      Warning("incorrect image header in file",image->filename);
  1000. X      DestroyImage(image);
  1001. X      return((Image *) NULL);
  1002. X    }
  1003. X  /*
  1004. X    Create linear colormap.
  1005. X  */
  1006. X  image->class=PseudoClass;
  1007. X  image->colors=256;
  1008. X  image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  1009. X  if (image->colormap == (ColorPacket *) NULL)
  1010. X    {
  1011. X      Warning("memory allocation error",(char *) NULL);
  1012. X      DestroyImage(image);
  1013. X      return((Image *) NULL);
  1014. X    }
  1015. X  for (i=0; i < image->colors; i++)
  1016. X  {
  1017. X    image->colormap[i].red=(unsigned char) i;
  1018. X    image->colormap[i].green=(unsigned char) i;
  1019. X    image->colormap[i].blue=(unsigned char) i;
  1020. X  }
  1021. X  /*
  1022. X    Create image.
  1023. X  */
  1024. X  image->packets=image->columns*image->rows;
  1025. X  image->pixels=(RunlengthPacket *)
  1026. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1027. X  image->comments=(char *)
  1028. X    malloc((strlen(image->filename)+2048)*sizeof(char));
  1029. X  vicar_pixels=(unsigned char *)
  1030. X    malloc((unsigned int) image->packets*sizeof(unsigned char));
  1031. X  if ((image->pixels == (RunlengthPacket *) NULL) ||
  1032. X      (image->comments == (char *) NULL) ||
  1033. X      (vicar_pixels == (unsigned char *) NULL))
  1034. X    {
  1035. X      Warning("memory allocation error",(char *) NULL);
  1036. X      DestroyImage(image);
  1037. X      return((Image *) NULL);
  1038. X    }
  1039. X  (void) sprintf(image->comments,"\n  Imported from VICAR image:  %s\n",
  1040. X    image->filename);
  1041. X  /*
  1042. X    Convert vicar pixels to runlength-encoded packets.
  1043. X  */
  1044. X  status=ReadData((char *) vicar_pixels,1,(int) image->packets,image->file);
  1045. X  if (status == False)
  1046. X    {
  1047. X      Warning("insufficient image data in file",image->filename);
  1048. X      DestroyImage(image);
  1049. X      return((Image *) NULL);
  1050. X    }
  1051. X  /*
  1052. X    Convert vicar pixels to runlength-encoded packets.
  1053. X  */
  1054. X  p=vicar_pixels;
  1055. X  q=image->pixels;
  1056. X  for (i=0; i < image->packets; i++)
  1057. X  {
  1058. X    q->red=(*p);
  1059. X    q->green=(*p);
  1060. X    q->blue=(*p);
  1061. X    q->index=(unsigned short) *p;
  1062. X    q->length=0;
  1063. X    p++;
  1064. X    q++;
  1065. X  }
  1066. X  (void) free((char *) vicar_pixels);
  1067. X  CompressColormap(image);
  1068. X  CloseImage(image);
  1069. X  return(image);
  1070. }
  1071. X
  1072. /*
  1073. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1074. %                                                                             %
  1075. %                                                                             %
  1076. %                                                                             %
  1077. %  R e a d X W D I m a g e                                                    %
  1078. %                                                                             %
  1079. %                                                                             %
  1080. %                                                                             %
  1081. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1082. %
  1083. %  Function ReadXWDImage reads an image file and returns it.  It allocates the
  1084. %  memory necessary for the new Image structure and returns a pointer to the
  1085. %  new image.
  1086. %
  1087. %  The format of the ReadXWDImage routine is:
  1088. %
  1089. %      image=ReadXWDImage(alien_info)
  1090. %
  1091. %  A description of each parameter follows:
  1092. %
  1093. %    o image:  Function ReadXWDImage returns a pointer to the image after
  1094. %      reading.  A null image is returned if there is a a memory shortage or
  1095. %      if the image cannot be read.
  1096. %
  1097. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  1098. %
  1099. %
  1100. */
  1101. static Image *ReadXWDImage(alien_info)
  1102. AlienInfo
  1103. X  *alien_info;
  1104. {
  1105. #include "XWDFile.h"
  1106. X
  1107. X  char
  1108. X    *window_name;
  1109. X
  1110. X  Display
  1111. X    display;
  1112. X
  1113. X  Image
  1114. X    *image;
  1115. X
  1116. X  int
  1117. X    status,
  1118. X    x,
  1119. X    y;
  1120. X
  1121. X  register int
  1122. X    i;
  1123. X
  1124. X  register RunlengthPacket
  1125. X    *p;
  1126. X
  1127. X  register unsigned long
  1128. X    pixel;
  1129. X
  1130. X  unsigned long int
  1131. X    lsb_first,
  1132. X    packets;
  1133. X
  1134. X  ScreenFormat
  1135. X    screen_format;
  1136. X
  1137. X  XColor
  1138. X    *colors;
  1139. X
  1140. X  XImage
  1141. X    *ximage;
  1142. X
  1143. X  XWDFileHeader
  1144. X    header;
  1145. X
  1146. X  /*
  1147. X    Allocate image structure.
  1148. X  */
  1149. X  image=AllocateImage("XWD");
  1150. X  if (image == (Image *) NULL)
  1151. X    return((Image *) NULL);
  1152. X  /*
  1153. X    Open image file.
  1154. X  */
  1155. X  (void) strcpy(image->filename,alien_info->filename);
  1156. X  OpenImage(image,"r");
  1157. X  if (image->file == (FILE *) NULL)
  1158. X    {
  1159. X      Warning("unable to open file",image->filename);
  1160. X      DestroyImage(image);
  1161. X      return((Image *) NULL);
  1162. X    }
  1163. X  /*
  1164. X     Read in header information.
  1165. X  */
  1166. X  status=ReadData((char *) &header,sizeof(header),1,image->file);
  1167. X  if (status == False)
  1168. X    {
  1169. X      Warning("Unable to read dump file header",image->filename);
  1170. X      DestroyImage(image);
  1171. X      return((Image *) NULL);
  1172. X    }
  1173. X  /*
  1174. X    Ensure the header byte-order is most-significant byte first.
  1175. X  */
  1176. X  lsb_first=1;
  1177. X  if (*(char *) &lsb_first)
  1178. X    MSBFirstOrderLong((char *) &header,sizeof(header));
  1179. X  /*
  1180. X    Check to see if the dump file is in the proper format.
  1181. X  */
  1182. X  if (header.file_version != XWD_FILE_VERSION)
  1183. X    {
  1184. X      Warning("XWD file format version mismatch",image->filename);
  1185. X      DestroyImage(image);
  1186. X      return((Image *) NULL);
  1187. X    }
  1188. X  if (header.header_size < sizeof(header))
  1189. X    {
  1190. X      Warning("XWD header size is too small",image->filename);
  1191. X      DestroyImage(image);
  1192. X      return((Image *) NULL);
  1193. X    }
  1194. X  packets=(header.header_size-sizeof(header));
  1195. X  window_name=(char *) malloc((unsigned int) packets*sizeof(char));
  1196. X  if (window_name == (char *) NULL)
  1197. X    {
  1198. X      Warning("unable to allocate memory",(char *) NULL);
  1199. X      DestroyImage(image);
  1200. X      return((Image *) NULL);
  1201. X    }
  1202. X  status=ReadData((char *) window_name,1,(int) packets,image->file);
  1203. X  if (status == False)
  1204. X    {
  1205. X      Warning("unable to read window name from dump file",image->filename);
  1206. X      DestroyImage(image);
  1207. X      return((Image *) NULL);
  1208. X    }
  1209. X  /*
  1210. X    Initialize the X image.
  1211. X  */
  1212. X  display.byte_order=header.byte_order;
  1213. X  display.bitmap_unit=header.bitmap_unit;
  1214. X  display.bitmap_bit_order=header.bitmap_bit_order;
  1215. X  display.pixmap_format=(&screen_format);
  1216. X  display.nformats=1;
  1217. X  screen_format.depth=header.pixmap_depth;
  1218. X  screen_format.bits_per_pixel=(int) header.bits_per_pixel;
  1219. X  ximage=XCreateImage(&display,(Visual *) NULL,
  1220. X    (unsigned int) header.pixmap_depth,(int) header.pixmap_format,
  1221. X    (int) header.xoffset,(char *) NULL,(unsigned int) header.pixmap_width,
  1222. X    (unsigned int) header.pixmap_height,(int) header.bitmap_pad,
  1223. X    (int) header.bytes_per_line);
  1224. X  ximage->red_mask=header.red_mask;
  1225. X  ximage->green_mask=header.green_mask;
  1226. X  ximage->blue_mask=header.blue_mask;
  1227. X  /*
  1228. X    Read colormap.
  1229. X  */
  1230. X  if (header.ncolors > 0)
  1231. X    {
  1232. X      colors=(XColor *) malloc((unsigned int) header.ncolors*sizeof(XColor));
  1233. X      if (colors == (XColor *) NULL)
  1234. X        {
  1235. X          Warning("unable to allocate memory",(char *) NULL);
  1236. X          DestroyImage(image);
  1237. X          return((Image *) NULL);
  1238. X        }
  1239. X      status=ReadData((char *) colors,sizeof(XColor),(int) header.ncolors,
  1240. X        image->file);
  1241. X      if (status == False)
  1242. X        {
  1243. X          Warning("unable to read color map from dump file",image->filename);
  1244. X          DestroyImage(image);
  1245. X          return((Image *) NULL);
  1246. X        }
  1247. X      /*
  1248. X        Ensure the header byte-order is most-significant byte first.
  1249. X      */
  1250. X      lsb_first=1;
  1251. X      if (*(char *) &lsb_first)
  1252. X        for (i=0; i < header.ncolors; i++)
  1253. X        {
  1254. X          MSBFirstOrderLong((char *) &colors[i].pixel,sizeof(unsigned long));
  1255. X          MSBFirstOrderShort((char *) &colors[i].red,3*sizeof(unsigned short));
  1256. X        }
  1257. X    }
  1258. X  /*
  1259. X    Allocate the pixel buffer.
  1260. X  */
  1261. X  if (ximage->format == ZPixmap)
  1262. X    packets=ximage->bytes_per_line*ximage->height;
  1263. X  else
  1264. X    packets=ximage->bytes_per_line*ximage->height*ximage->depth;
  1265. X  ximage->data=(char *) malloc(packets*sizeof(unsigned char));
  1266. X  if (ximage->data == (char *) NULL)
  1267. X    {
  1268. X      Warning("unable to allocate memory",(char *) NULL);
  1269. X      DestroyImage(image);
  1270. X      return((Image *) NULL);
  1271. X    }
  1272. X  status=ReadData(ximage->data,1,(int) packets,image->file);
  1273. X  if (status == False)
  1274. X    {
  1275. X      Warning("unable to read dump pixmap",image->filename);
  1276. X      DestroyImage(image);
  1277. X      return((Image *) NULL);
  1278. X    }
  1279. X  /*
  1280. X    Convert image to MIFF format.
  1281. X  */
  1282. X  image->columns=ximage->width;
  1283. X  image->rows=ximage->height;
  1284. X  /*
  1285. X    Initial image comment.
  1286. X  */
  1287. X  if ((ximage->red_mask > 0) || (ximage->green_mask > 0) ||
  1288. X      (ximage->blue_mask > 0))
  1289. X    image->class=DirectClass;
  1290. X  else
  1291. X    image->class=PseudoClass;
  1292. X  image->colors=header.ncolors;
  1293. X  image->packets=image->columns*image->rows;
  1294. X  image->pixels=(RunlengthPacket *)
  1295. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1296. X  image->comments=(char *)
  1297. X    malloc((strlen(image->filename)+2048)*sizeof(char));
  1298. X  if ((image->pixels == (RunlengthPacket *) NULL) ||
  1299. X      (image->comments == (char *) NULL))
  1300. X    {
  1301. X      Warning("unable to allocate memory",(char *) NULL);
  1302. X      DestroyImage(image);
  1303. X      return((Image *) NULL);
  1304. X    }
  1305. X  (void) sprintf(image->comments,"\n  Imported from X11 dump file:  %s\n\0",
  1306. X    image->filename);
  1307. X  p=image->pixels;
  1308. X  switch (image->class)
  1309. X  {
  1310. X    case DirectClass:
  1311. X    {
  1312. X      register unsigned long
  1313. X        color;
  1314. X
  1315. X      unsigned long
  1316. X        blue_mask,
  1317. X        blue_shift,
  1318. X        green_mask,
  1319. X        green_shift,
  1320. X        red_mask,
  1321. X        red_shift;
  1322. X
  1323. X      /*
  1324. X        Determine shift and mask for red, green, and blue.
  1325. X      */
  1326. X      red_mask=ximage->red_mask;
  1327. X      red_shift=0;
  1328. X      while ((red_mask & 0x01) == 0)
  1329. X      {
  1330. X        red_mask>>=1;
  1331. X        red_shift++;
  1332. X      }
  1333. X      green_mask=ximage->green_mask;
  1334. X      green_shift=0;
  1335. X      while ((green_mask & 0x01) == 0)
  1336. X      {
  1337. X        green_mask>>=1;
  1338. X        green_shift++;
  1339. X      }
  1340. X      blue_mask=ximage->blue_mask;
  1341. X      blue_shift=0;
  1342. X      while ((blue_mask & 0x01) == 0)
  1343. X      {
  1344. X        blue_mask>>=1;
  1345. X        blue_shift++;
  1346. X      }
  1347. X      /*
  1348. X        Convert X image to DirectClass packets.
  1349. X      */
  1350. X      if (image->colors > 0)
  1351. X        for (y=0; y < image->rows; y++)
  1352. X        {
  1353. X          for (x=0; x < image->columns; x++)
  1354. X          {
  1355. X            pixel=XGetPixel(ximage,x,y);
  1356. X            p->index=(unsigned short) ((pixel >> red_shift) & red_mask);
  1357. X            p->red=(unsigned char) (colors[p->index].red >> 8);
  1358. X            p->index=(unsigned short) ((pixel >> green_shift) & green_mask);
  1359. X            p->green=(unsigned char) (colors[p->index].green >> 8);
  1360. X            p->index=(unsigned short) ((pixel >> blue_shift) & blue_mask);
  1361. X            p->blue=(unsigned char) (colors[p->index].blue >> 8);
  1362. X            p->index=0;
  1363. X            p->length=0;
  1364. X            p++;
  1365. X          }
  1366. X        }
  1367. X      else
  1368. X        for (y=0; y < image->rows; y++)
  1369. X          for (x=0; x < image->columns; x++)
  1370. X          {
  1371. X            pixel=XGetPixel(ximage,x,y);
  1372. X            color=(pixel >> red_shift) & red_mask;
  1373. X            p->red=(unsigned char)
  1374. X              ((((unsigned long) color*65535)/red_mask) >> 8);
  1375. X            color=(pixel >> green_shift) & green_mask;
  1376. X            p->green=(unsigned char)
  1377. X              ((((unsigned long) color*65535)/green_mask) >> 8);
  1378. X            color=(pixel >> blue_shift) & blue_mask;
  1379. X            p->blue=(unsigned char)
  1380. X              ((((unsigned long) color*65535)/blue_mask) >> 8);
  1381. X            p->index=0;
  1382. X            p->length=0;
  1383. X            p++;
  1384. X          }
  1385. X      break;
  1386. X    }
  1387. X    case PseudoClass:
  1388. X    {
  1389. X      /*
  1390. X        Convert X image to PseudoClass packets.
  1391. X      */
  1392. X      image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  1393. X      if (image->colormap == (ColorPacket *) NULL)
  1394. X        {
  1395. X          Warning("unable to allocate memory",(char *) NULL);
  1396. X          DestroyImage(image);
  1397. X          return((Image *) NULL);
  1398. X        }
  1399. X      for (i=0; i < image->colors; i++)
  1400. X      {
  1401. X        image->colormap[i].red=colors[i].red >> 8;
  1402. X        image->colormap[i].green=colors[i].green >> 8;
  1403. X        image->colormap[i].blue=colors[i].blue >> 8;
  1404. X      }
  1405. X      for (y=0; y < image->rows; y++)
  1406. X        for (x=0; x < image->columns; x++)
  1407. X        {
  1408. X          pixel=XGetPixel(ximage,x,y);
  1409. X          p->red=(unsigned char) (colors[pixel].red >> 8);
  1410. X          p->green=(unsigned char) (colors[pixel].green >> 8);
  1411. X          p->blue=(unsigned char) (colors[pixel].blue >> 8);
  1412. X          p->index=(unsigned short) pixel;
  1413. X          p->length=0;
  1414. X          p++;
  1415. X        }
  1416. X      CompressColormap(image);
  1417. X      break;
  1418. X    }
  1419. X  }
  1420. X  /*
  1421. X    Free image and colormap.
  1422. X  */
  1423. X  (void) free((char *) window_name);
  1424. X  if (header.ncolors > 0)
  1425. X    (void) free((char *) colors);
  1426. X  XDestroyImage(ximage);
  1427. X  CloseImage(image);
  1428. X  return(image);
  1429. }
  1430. X
  1431. /*
  1432. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1433. %                                                                             %
  1434. %                                                                             %
  1435. %                                                                             %
  1436. %   W r i t e A V S I m a g e                                                 %
  1437. %                                                                             %
  1438. %                                                                             %
  1439. %                                                                             %
  1440. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1441. %
  1442. %  Function WriteAVSImage writes an image to a file on disk in AVS rasterfile
  1443. %  format.
  1444. %
  1445. %  The format of the WriteAVSImage routine is:
  1446. %
  1447. %      status=WriteAVSImage(image)
  1448. %
  1449. %  A description of each parameter follows.
  1450. %
  1451. %    o status: Function WriteImage return True if the image is written.
  1452. %      False is returned is there is a memory shortage or if the image file
  1453. %      fails to write.
  1454. %
  1455. %    o image:  A pointer to a Image structure.
  1456. %
  1457. %
  1458. */
  1459. static unsigned int WriteAVSImage(image)
  1460. Image
  1461. X  *image;
  1462. {
  1463. X  typedef struct Raster
  1464. X  {
  1465. X    int
  1466. X      width,
  1467. X      height;
  1468. X  } Raster;
  1469. X
  1470. X  Raster
  1471. X    header;
  1472. X
  1473. X  register int
  1474. X    i,
  1475. X    j;
  1476. X
  1477. X  register RunlengthPacket
  1478. X    *p;
  1479. X
  1480. X  register unsigned char
  1481. X    *q;
  1482. X
  1483. X  unsigned char
  1484. X    *avs_pixels;
  1485. X
  1486. X  /*
  1487. X    Open output image file.
  1488. X  */
  1489. X  OpenImage(image,"w");
  1490. X  if (image->file == (FILE *) NULL)
  1491. X    {
  1492. X      Warning("unable to open file",image->filename);
  1493. X      return(False);
  1494. X    }
  1495. X  /*
  1496. X    Initialize raster file header.
  1497. X  */
  1498. X  header.width=image->columns;
  1499. X  header.height=image->rows;
  1500. X  avs_pixels=(unsigned char *)
  1501. X    malloc(4*image->columns*image->rows*sizeof(unsigned char));
  1502. X  if (avs_pixels == (unsigned char *) NULL)
  1503. X    {
  1504. X      Warning("unable to allocate memory",(char *) NULL);
  1505. X      return(False);
  1506. X    }
  1507. X  (void) fwrite((char *) &header,sizeof(header),1,image->file);
  1508. X  p=image->pixels;
  1509. X  q=avs_pixels;
  1510. X  for (i=0; i < image->packets; i++)
  1511. X  {
  1512. X    for (j=0; j <= (int) p->length; j++)
  1513. X    {
  1514. X      *q++=(unsigned char) (image->alpha ? p->index : 0);
  1515. X      *q++=p->red;
  1516. X      *q++=p->green;
  1517. X      *q++=p->blue;
  1518. X    }
  1519. X    p++;
  1520. X  }
  1521. X  (void) fwrite((char *) avs_pixels,sizeof(char),(int)
  1522. X    (image->columns*image->rows*4),image->file);
  1523. X  CloseImage(image);
  1524. X  return(True);
  1525. }
  1526. X
  1527. /*
  1528. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1529. %                                                                             %
  1530. %                                                                             %
  1531. %                                                                             %
  1532. %   W r i t e C M Y K I m a g e                                               %
  1533. %                                                                             %
  1534. %                                                                             %
  1535. %                                                                             %
  1536. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1537. %
  1538. %  Function WriteCMYKImage writes an image to a file on disk in red, green,
  1539. %  blue rasterfile format.
  1540. %
  1541. %  The format of the WriteCMYKImage routine is:
  1542. %
  1543. %      status=WriteCMYKImage(image)
  1544. %
  1545. %  A description of each parameter follows.
  1546. %
  1547. %    o status: Function WriteImage return True if the image is written.
  1548. %      False is returned is there is a memory shortage or if the image file
  1549. %      fails to write.
  1550. %
  1551. %    o image:  A pointer to a Image structure.
  1552. %
  1553. %
  1554. */
  1555. static unsigned int WriteCMYKImage(image)
  1556. Image
  1557. X  *image;
  1558. {
  1559. X  register int
  1560. X    i,
  1561. X    j;
  1562. X
  1563. X  register RunlengthPacket
  1564. X    *p;
  1565. X
  1566. X  register unsigned char
  1567. X    *q;
  1568. X
  1569. X  unsigned char
  1570. X    black,
  1571. X    cyan,
  1572. X    magenta,
  1573. X    *pixels,
  1574. X    yellow;
  1575. X
  1576. X  /*
  1577. X    Open output image file.
  1578. X  */
  1579. X  OpenImage(image,"w");
  1580. X  if (image->file == (FILE *) NULL)
  1581. X    {
  1582. X      Warning("unable to open file",image->filename);
  1583. X      return(False);
  1584. X    }
  1585. X  /*
  1586. X    Convert MIFF to CMYK raster pixels.
  1587. X  */
  1588. X  pixels=(unsigned char *)
  1589. X    malloc(4*image->columns*image->rows*sizeof(unsigned char));
  1590. X  if (pixels == (unsigned char *) NULL)
  1591. X    {
  1592. X      Warning("unable to allocate memory",(char *) NULL);
  1593. X      return(False);
  1594. X    }
  1595. X  p=image->pixels;
  1596. X  q=pixels;
  1597. X  for (i=0; i < image->packets; i++)
  1598. X  {
  1599. X    for (j=0; j <= ((int) p->length); j++)
  1600. X    {
  1601. X      cyan=MaxRGB-p->red;
  1602. X      magenta=MaxRGB-p->green;
  1603. X      yellow=MaxRGB-p->blue;
  1604. X      black=cyan;
  1605. X      if (magenta < black)
  1606. X        black=magenta;
  1607. X      if (yellow < black)
  1608. X        black=yellow;
  1609. X      *q++=cyan-black;
  1610. X      *q++=magenta-black;
  1611. X      *q++=yellow-black;
  1612. X      *q++=black;
  1613. X    }
  1614. X    p++;
  1615. X  }
  1616. X  (void) fwrite((char *) pixels,sizeof(unsigned char),
  1617. X    (int) (4*image->columns*image->rows),image->file);
  1618. X  (void) free((char *) pixels);
  1619. X  CloseImage(image);
  1620. X  return(True);
  1621. }
  1622. X
  1623. /*
  1624. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1625. %                                                                             %
  1626. %                                                                             %
  1627. %                                                                             %
  1628. %   W r i t e F A X I m a g e                                                 %
  1629. %                                                                             %
  1630. %                                                                             %
  1631. %                                                                             %
  1632. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1633. %
  1634. %  Procedure WriteFAXImage writes an image to a file on disk in 1 dimensional
  1635. %  Huffman encoded format.
  1636. %
  1637. %  The format of the WriteFAXImage routine is:
  1638. %
  1639. %      status=WriteFAXImage(image)
  1640. %
  1641. %  A description of each parameter follows.
  1642. %
  1643. %    o status: Function WriteFAXImage return True if the image is written.
  1644. %      False is returned is there is a memory shortage or if the image file
  1645. %      fails to write.
  1646. %
  1647. %    o image:  A pointer to a Image structure.
  1648. %
  1649. %
  1650. */
  1651. static unsigned int WriteFAXImage(image)
  1652. Image
  1653. X  *image;
  1654. {
  1655. X  /*
  1656. X    Open output image file.
  1657. X  */
  1658. X  OpenImage(image,"w");
  1659. X  if (image->file == (FILE *) NULL)
  1660. X    {
  1661. X      Warning("unable to open file",image->filename);
  1662. X      return(False);
  1663. X    }
  1664. X  /*
  1665. X    Convert MIFF to monochrome.
  1666. X  */
  1667. X  QuantizeImage(image,2,8,False,GRAYColorspace,True);
  1668. X  (void) HuffmanEncodeImage(image);
  1669. X  CloseImage(image);
  1670. X  return(True);
  1671. }
  1672. X
  1673. /*
  1674. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1675. %                                                                             %
  1676. %                                                                             %
  1677. %                                                                             %
  1678. %   W r i t e G I F I m a g e                                                 %
  1679. %                                                                             %
  1680. %                                                                             %
  1681. %                                                                             %
  1682. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1683. %
  1684. %  Function WriteGIFImage writes an image to a file on disk in the GIF image
  1685. %  format.
  1686. %
  1687. %  The format of the WriteGIFImage routine is:
  1688. %
  1689. %      status=WriteGIFImage(image)
  1690. %
  1691. %  A description of each parameter follows.
  1692. %
  1693. %    o status: Function WriteImage return True if the image is written.
  1694. %      False is returned is there is a memory shortage or if the image file
  1695. %      fails to write.
  1696. %
  1697. %    o image:  A pointer to a Image structure.
  1698. %
  1699. %
  1700. */
  1701. static unsigned int WriteGIFImage(image)
  1702. Image
  1703. X  *image;
  1704. {
  1705. X  int
  1706. X    status;
  1707. X
  1708. X  register int
  1709. X    i;
  1710. X
  1711. X  unsigned char
  1712. X    bits_per_pixel,
  1713. X    buffer[10];
  1714. X
  1715. X  /*
  1716. X    Open output image file.
  1717. X  */
  1718. X  OpenImage(image,"w");
  1719. X  if (image->file == (FILE *) NULL)
  1720. X    {
  1721. X      Warning("unable to open file",image->filename);
  1722. X      return(False);
  1723. X    }
  1724. X  /*
  1725. X    GIF colormap must be 256 entries or less.
  1726. X  */
  1727. X  if ((image->class == DirectClass) || (image->colors > 256))
  1728. X    QuantizeImage(image,256,8,False,RGBColorspace,True);
  1729. X  for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
  1730. X    if ((1 << bits_per_pixel) >= image->colors)
  1731. X      break;
  1732. X  /*
  1733. X    Write GIF header.
  1734. X  */
  1735. X  (void) fwrite("GIF87a",1,6,image->file);
  1736. X  buffer[0]=(unsigned char) (image->columns & 0xff);
  1737. X  buffer[1]=(unsigned char) ((image->columns >> 8) & 0xff);
  1738. X  buffer[2]=(unsigned char) (image->rows & 0xff);
  1739. X  buffer[3]=(unsigned char) ((image->rows >> 8) & 0xff);
  1740. X  buffer[4]=0x80;  /* global colormap */
  1741. X  buffer[4]|=(bits_per_pixel-1) << 4;  /* color resolution */
  1742. X  buffer[4]|=(bits_per_pixel-1);   /* size of global colormap */
  1743. X  buffer[5]=0;  /* background color */
  1744. X  buffer[6]=0;  /* aspect ratio */
  1745. X  (void) fwrite((char *) buffer,1,7,image->file);
  1746. X  /*
  1747. X    Write colormap.
  1748. X  */
  1749. X  for (i=0; i < image->colors; i++)
  1750. X  {
  1751. X    (void) fwrite((char *) &image->colormap[i].red,1,1,image->file);
  1752. X    (void) fwrite((char *) &image->colormap[i].green,1,1,image->file);
  1753. X    (void) fwrite((char *) &image->colormap[i].blue,1,1,image->file);
  1754. X  }
  1755. X  buffer[0]=0;
  1756. X  buffer[1]=0;
  1757. X  buffer[2]=0;
  1758. X  for ( ; i < (int) (1 << bits_per_pixel); i++)
  1759. X    (void) fwrite((char *) buffer,1,3,image->file);
  1760. X  (void) fwrite(",",1,1,image->file);  /* separator */
  1761. X  /*
  1762. X    Write the image header.
  1763. X  */
  1764. X  buffer[0]=0;  /* left/top */
  1765. X  buffer[1]=0;
  1766. X  buffer[2]=0;
  1767. X  buffer[3]=0;
  1768. X  buffer[4]=(unsigned char) (image->columns & 0xff);
  1769. X  buffer[5]=(unsigned char) ((image->columns >> 8) & 0xff);
  1770. X  buffer[6]=(unsigned char) (image->rows & 0xff);
  1771. X  buffer[7]=(unsigned char) ((image->rows >> 8) & 0xff);
  1772. X  buffer[8]=0;  /* no interlace */
  1773. X  buffer[9]=Max(bits_per_pixel,2);
  1774. X  (void) fwrite((char *) buffer,1,10,image->file);
  1775. X  status=LZWEncodeImage(image,Max(bits_per_pixel,2)+1);
  1776. X  if (status == False)
  1777. X    {
  1778. X      Warning("unable to write image","memory allocation failed");
  1779. X      DestroyImage(image);
  1780. X      return(False);
  1781. X    }
  1782. X  buffer[0]=0;
  1783. X  (void) fwrite((char *) buffer,1,1,image->file);  /* EOF */
  1784. X  (void) fwrite(";",1,1,image->file);  /* terminator */
  1785. X  CloseImage(image);
  1786. X  return(True);
  1787. }
  1788. X
  1789. /*
  1790. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1791. %                                                                             %
  1792. %                                                                             %
  1793. %                                                                             %
  1794. %   W r i t e G R A Y I m a g e                                               %
  1795. %                                                                             %
  1796. %                                                                             %
  1797. %                                                                             %
  1798. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1799. %
  1800. %  Function WriteGRAYImage writes an image to a file on disk in red, green,
  1801. %  blue rasterfile format.
  1802. %
  1803. %  The format of the WriteGRAYImage routine is:
  1804. %
  1805. %      status=WriteGRAYImage(image)
  1806. %
  1807. %  A description of each parameter follows.
  1808. %
  1809. %    o status: Function WriteImage return True if the image is written.
  1810. %      False is returned is there is a memory shortage or if the image file
  1811. %      fails to write.
  1812. %
  1813. %    o image:  A pointer to a Image structure.
  1814. %
  1815. %
  1816. */
  1817. static unsigned int WriteGRAYImage(image)
  1818. Image
  1819. X  *image;
  1820. {
  1821. X  register int
  1822. X    i,
  1823. X    j;
  1824. X
  1825. X  register RunlengthPacket
  1826. X    *p;
  1827. X
  1828. X  register unsigned char
  1829. X    *q;
  1830. X
  1831. X  unsigned char
  1832. X    *pixels;
  1833. X
  1834. X  /*
  1835. X    Open output image file.
  1836. X  */
  1837. X  OpenImage(image,"w");
  1838. X  if (image->file == (FILE *) NULL)
  1839. X    {
  1840. X      Warning("unable to open file",image->filename);
  1841. X      return(False);
  1842. X    }
  1843. X  /*
  1844. X    Convert image to gray scale PseudoColor class.
  1845. X  */
  1846. X  pixels=(unsigned char *)
  1847. X    malloc(image->columns*image->rows*sizeof(unsigned char));
  1848. X  if (pixels == (unsigned char *) NULL)
  1849. X    {
  1850. X      Warning("unable to allocate memory",(char *) NULL);
  1851. X      return(False);
  1852. X    }
  1853. X  p=image->pixels;
  1854. X  q=pixels;
  1855. X  for (i=0; i < image->packets; i++)
  1856. X  {
  1857. X    for (j=0; j <= ((int) p->length); j++)
  1858. X      *q++=Intensity(*p);
  1859. X    p++;
  1860. X  }
  1861. X  (void) fwrite((char *) pixels,sizeof(unsigned char),
  1862. X    (int) (image->columns*image->rows),image->file);
  1863. X  (void) free((char *) pixels);
  1864. X  CloseImage(image);
  1865. X  return(True);
  1866. }
  1867. X
  1868. #ifdef AlienJPEG
  1869. /*
  1870. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1871. %                                                                             %
  1872. %                                                                             %
  1873. %                                                                             %
  1874. %  W r i t e J P E G I m a g e                                                %
  1875. %                                                                             %
  1876. %                                                                             %
  1877. %                                                                             %
  1878. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1879. %
  1880. %  Function WriteJPEGImage writes an image file and returns it.  It allocates
  1881. %  the memory necessary for the new Image structure and returns a pointer to
  1882. %  the new jpeg_image.
  1883. %
  1884. %  The format of the WriteJPEGImage routine is:
  1885. %
  1886. %      status=WriteJPEGImage(image)
  1887. %
  1888. %  A description of each parameter follows:
  1889. %
  1890. %    o status:  Function WriteJPEGImage return True if the image is written.
  1891. %      False is returned is there is of a memory shortage or if the image
  1892. %      file cannot be opened for writing.
  1893. %
  1894. %    o jpeg_image:  A pointer to a Image structure.
  1895. %
  1896. %
  1897. */
  1898. X
  1899. METHODDEF void JPEGInitializeImage(jpeg_info)
  1900. compress_info_ptr
  1901. X  jpeg_info;
  1902. {
  1903. X  /*
  1904. X    Initialize JPEG image.
  1905. X  */
  1906. X  jpeg_info->image_width=jpeg_image->columns;
  1907. X  jpeg_info->image_height=jpeg_image->rows;
  1908. X  jpeg_info->data_precision=8;
  1909. X  jpeg_info->input_components=3;
  1910. X  jpeg_info->in_color_space=CS_RGB;
  1911. X  if ((jpeg_image->class == PseudoClass) && (jpeg_image->colors <= 256))
  1912. X    {
  1913. X      register int
  1914. X        i;
  1915. X
  1916. X      unsigned int
  1917. X        grayscale;
  1918. X
  1919. X      /*
  1920. X        Determine if jpeg_image is grayscale.
  1921. X      */
  1922. X      grayscale=True;
  1923. X      for (i=0; i < jpeg_image->colors; i++)
  1924. X        if ((jpeg_image->colormap[i].red != jpeg_image->colormap[i].green) ||
  1925. X            (jpeg_image->colormap[i].green != jpeg_image->colormap[i].blue))
  1926. X          {
  1927. X            grayscale=False;
  1928. X            break;
  1929. X          }
  1930. X      if (grayscale)
  1931. X        {
  1932. X          jpeg_info->input_components=1;
  1933. X          jpeg_info->in_color_space=CS_GRAYSCALE;
  1934. X        }
  1935. X    }
  1936. X  jpeg_image->packet=jpeg_image->pixels;
  1937. X  jpeg_image->runlength=jpeg_image->packet->length+1;
  1938. }
  1939. X
  1940. static void JPEGInputTermMethod(jpeg_info)
  1941. compress_info_ptr
  1942. X  jpeg_info;
  1943. {
  1944. }
  1945. X
  1946. static void JPEGWriteGRAY(jpeg_info,pixel_data)
  1947. compress_info_ptr
  1948. X  jpeg_info;
  1949. X
  1950. JSAMPARRAY
  1951. X  pixel_data;
  1952. {
  1953. X  register int
  1954. X    column;
  1955. X
  1956. X  register JSAMPROW
  1957. X    gray;
  1958. X
  1959. X  register RunlengthPacket
  1960. X    *p;
  1961. X
  1962. X  /*
  1963. X    Convert run-length encoded grayscale MIFF packets to JPEG pixels.
  1964. X  */
  1965. X  gray=pixel_data[0];
  1966. X  p=jpeg_image->packet;
  1967. X  for (column=jpeg_info->image_width; column > 0; column--)
  1968. X  {
  1969. X    if (jpeg_image->runlength > 0)
  1970. X      jpeg_image->runlength--;
  1971. X    else
  1972. X      {
  1973. X        p++;
  1974. X        jpeg_image->runlength=p->length;
  1975. SHAR_EOF
  1976. true || echo 'restore of ImageMagick/alien.c failed'
  1977. fi
  1978. echo 'End of  part 22'
  1979. echo 'File ImageMagick/alien.c is continued in part 23'
  1980. echo 23 > _shar_seq_.tmp
  1981. exit 0
  1982. exit 0 # Just in case...
  1983.