home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sources / misc / 4190 < prev    next >
Encoding:
Text File  |  1992-12-14  |  57.5 KB  |  1,989 lines

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