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

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i072:  imagemagic - X11 image processing and display, Part16/38
  4. Message-ID: <1993Jul14.175657.1661@sparky.sterling.com>
  5. X-Md4-Signature: 80afac055652aaa0e77951b19450970a
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 17:56:57 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 72
  13. Archive-name: imagemagic/part16
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.16 (part 16 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/decode.c continued
  21. #
  22. if test ! -r _shar_seq_.tmp; then
  23.     echo 'Please unpack part 1 first!'
  24.     exit 1
  25. fi
  26. (read Scheck
  27.  if test "$Scheck" != 16; then
  28.     echo Please unpack part "$Scheck" next!
  29.     exit 1
  30.  else
  31.     exit 0
  32.  fi
  33. ) < _shar_seq_.tmp || exit 1
  34. if test ! -f _shar_wnt_.tmp; then
  35.     echo 'x - still skipping ImageMagick/decode.c'
  36. else
  37. echo 'x - continuing file ImageMagick/decode.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/decode.c' &&
  39. %                                                                             %
  40. %                                                                             %
  41. %                                                                             %
  42. %  R e a d R L E I m a g e                                                    %
  43. %                                                                             %
  44. %                                                                             %
  45. %                                                                             %
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. %
  48. %  Function ReadRLEImage reads a run-length encoded Utah Raster Toolkit
  49. %  image file and returns it.  It allocates the memory necessary for the new
  50. %  Image structure and returns a pointer to the new image.
  51. %
  52. %  The format of the ReadRLEImage routine is:
  53. %
  54. %      image=ReadRLEImage(image_info)
  55. %
  56. %  A description of each parameter follows:
  57. %
  58. %    o image:  Function ReadRLEImage returns a pointer to the image after
  59. %      reading.  A null image is returned if there is a a memory shortage or
  60. %      if the image cannot be read.
  61. %
  62. %    o image_info: Specifies a pointer to an ImageInfo structure.
  63. %
  64. %
  65. */
  66. static Image *ReadRLEImage(image_info)
  67. ImageInfo
  68. X  *image_info;
  69. {
  70. #define SkipLinesOp  0x01
  71. #define SetColorOp  0x02
  72. #define SkipPixelsOp  0x03
  73. #define ByteDataOp  0x05
  74. #define RunDataOp  0x06
  75. #define EOFOp  0x07
  76. X
  77. X  char
  78. X    magick[12];
  79. X
  80. X  Image
  81. X    *image;
  82. X
  83. X  int
  84. X    opcode,
  85. X    operand,
  86. X    status,
  87. X    x,
  88. X    y;
  89. X
  90. X  register int
  91. X    i,
  92. X    j;
  93. X
  94. X  register RunlengthPacket
  95. X    *q;
  96. X
  97. X  register unsigned char
  98. X    *p;
  99. X
  100. X  unsigned char
  101. X    background_color[256],
  102. X    *colormap,
  103. X    header[2048],
  104. X    integer[2],
  105. X    pixel,
  106. X    plane,
  107. X    *rle_pixels;
  108. X
  109. X  unsigned int
  110. X    bits_per_pixel,
  111. X    flags,
  112. X    map_length,
  113. X    number_colormaps,
  114. X    number_planes;
  115. X
  116. X  /*
  117. X    Allocate image structure.
  118. X  */
  119. X  image=AllocateImage("RLE");
  120. X  if (image == (Image *) NULL)
  121. X    return((Image *) NULL);
  122. X  /*
  123. X    Open image file.
  124. X  */
  125. X  (void) strcpy(image->filename,image_info->filename);
  126. X  OpenImage(image,"r");
  127. X  if (image->file == (FILE *) NULL)
  128. X    {
  129. X      Warning("unable to open file",image->filename);
  130. X      DestroyImage(image);
  131. X      return((Image *) NULL);
  132. X    }
  133. X  /*
  134. X    Determine if this is a RLE file.
  135. X  */
  136. X  status=ReadData((char *) magick,1,2,image->file);
  137. X  if ((status == False) || (strncmp(magick,"\122\314",2) != 0))
  138. X    {
  139. X      Warning("not a RLE image file",(char *) NULL);
  140. X      DestroyImage(image);
  141. X      return((Image *) NULL);
  142. X    }
  143. X  do
  144. X  {
  145. X    /*
  146. X      Read image header.
  147. X    */
  148. X    status=ReadData((char *) header,1,13,image->file);
  149. X    if (status == False)
  150. X      {
  151. X        Warning("unable to read RLE image header",(char *) NULL);
  152. X        DestroyImage(image);
  153. X        return((Image *) NULL);
  154. X      }
  155. X    image->columns=header[4]+(header[5] << 8);
  156. X    image->rows=header[6]+(header[7] << 8);
  157. X    image->packets=image->columns*image->rows;
  158. X    flags=header[8];
  159. X    image->alpha=flags & 0x04;
  160. X    number_planes=header[9];
  161. X    bits_per_pixel=header[10];
  162. X    number_colormaps=header[11];
  163. X    map_length=1 << header[12];
  164. X    if ((number_planes == 0) || (number_planes == 2) || (bits_per_pixel != 8) ||
  165. X        ((image->columns*image->rows) == 0))
  166. X      {
  167. X        Warning("unsupported RLE image file",(char *) NULL);
  168. X        DestroyImage(image);
  169. X        return((Image *) NULL);
  170. X      }
  171. X    if (flags & 0x02)
  172. X      {
  173. X        /*
  174. X          No background color-- initialize to black.
  175. X        */
  176. X        for (i=0; i < number_planes; i++)
  177. X          background_color[i]=(unsigned char) 0;
  178. X        (void) fgetc(image->file);
  179. X      }
  180. X    else
  181. X      {
  182. X        /*
  183. X          Initialize background color.
  184. X        */
  185. X        p=background_color;
  186. X        for (i=0; i < number_planes; i++)
  187. X          *p++=(unsigned char) fgetc(image->file);
  188. X        if ((number_planes % 2) == 0)
  189. X          (void) fgetc(image->file);
  190. X      }
  191. X    colormap=(unsigned char *) NULL;
  192. X    if (number_colormaps != 0)
  193. X      {
  194. X        /*
  195. X          Read image colormaps.
  196. X        */
  197. X        colormap=(unsigned char *)
  198. X          malloc(number_colormaps*map_length*sizeof(unsigned char));
  199. X        if (colormap == (unsigned char *) NULL)
  200. X          {
  201. X            Warning("memory allocation error",(char *) NULL);
  202. X            DestroyImage(image);
  203. X            return((Image *) NULL);
  204. X          }
  205. X        p=colormap;
  206. X        for (i=0; i < number_colormaps; i++)
  207. X          for (j=0; j < map_length; j++)
  208. X          {
  209. X            (void) fgetc(image->file);
  210. X            *p++=(unsigned char) fgetc(image->file);
  211. X          }
  212. X      }
  213. X    /*
  214. X      Allocate image comment.
  215. X    */
  216. X    image->comments=(char *)
  217. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  218. X    if (image->comments == (char *) NULL)
  219. X      {
  220. X        Warning("memory allocation error",(char *) NULL);
  221. X        DestroyImage(image);
  222. X        return((Image *) NULL);
  223. X      }
  224. X    (void) sprintf(image->comments,"\n  Imported from Utah raster image:  %s\n",
  225. X      image->filename);
  226. X    if (flags & 0x08)
  227. X      {
  228. X        unsigned int
  229. X          comment_length;
  230. X
  231. X        /*
  232. X          Read image comment.
  233. X        */
  234. X        (void) ReadData((char *) integer,1,2,image->file);
  235. X        comment_length=integer[0]+(integer[1] << 8);
  236. X        image->comments=(char *) realloc((char *) image->comments,
  237. X          (unsigned int) (strlen(image->comments)+comment_length+2048));
  238. X        if (image->comments == (char *) NULL)
  239. X          {
  240. X            Warning("memory allocation error",(char *) NULL);
  241. X            DestroyImage(image);
  242. X            return((Image *) NULL);
  243. X          }
  244. X        (void) strcat(image->comments,"\n  ");
  245. X        status=ReadData((char *) image->comments+strlen(image->comments),1,
  246. X          (int) comment_length+(comment_length % 2 ? 0 : 1),image->file);
  247. X        if (status == False)
  248. X          {
  249. X            Warning("unable to read RLE comment",(char *) NULL);
  250. X            DestroyImage(image);
  251. X            return((Image *) NULL);
  252. X          }
  253. X        (void) strcat(image->comments,"\n");
  254. X      }
  255. X    /*
  256. X      Allocate RLE pixels.
  257. X    */
  258. X    if (image->alpha)
  259. X      number_planes++;
  260. X    rle_pixels=(unsigned char *)
  261. X      malloc((unsigned int) image->packets*number_planes*sizeof(unsigned char));
  262. X    if (rle_pixels == (unsigned char *) NULL)
  263. X      {
  264. X        Warning("memory allocation error",(char *) NULL);
  265. X        DestroyImage(image);
  266. X        return((Image *) NULL);
  267. X      }
  268. X    if ((flags & 0x01) && ((~flags) & 0x02))
  269. X      {
  270. X        /*
  271. X          Set background color.
  272. X        */
  273. X        p=rle_pixels;
  274. X        for (i=0; i < image->packets; i++)
  275. X        {
  276. X          if (!image->alpha)
  277. X            for (j=0; j < number_planes; j++)
  278. X              *p++=background_color[j];
  279. X          else
  280. X            {
  281. X              for (j=0; j < (number_planes-1); j++)
  282. X                *p++=background_color[j];
  283. X              *p++=0;  /* initialize alpha channel */
  284. X            }
  285. X        }
  286. X      }
  287. X    /*
  288. X      Read runlength-encoded image.
  289. X    */
  290. X    plane=0;
  291. X    x=0;
  292. X    y=0;
  293. X    (void) fgetc(image->file);
  294. X    opcode=fgetc(image->file);
  295. X    while (((opcode & 0x3f) != EOFOp) && (opcode != EOF))
  296. X    {
  297. X      switch (opcode & 0x3f)
  298. X      {
  299. X        case SkipLinesOp:
  300. X        {
  301. X          operand=fgetc(image->file);
  302. X          if (opcode & 0x40)
  303. X            {
  304. X              (void) ReadData((char *) integer,1,2,image->file);
  305. X              operand=integer[0]+(integer[1] << 8);
  306. X            }
  307. X          x=0;
  308. X          y+=operand;
  309. X          break;
  310. X        }
  311. X        case SetColorOp:
  312. X        {
  313. X          operand=fgetc(image->file);
  314. X          plane=operand;
  315. X          if (plane == 255)
  316. X            plane=number_planes-1;
  317. X          x=0;
  318. X          break;
  319. X        }
  320. X        case SkipPixelsOp:
  321. X        {
  322. X          operand=fgetc(image->file);
  323. X          if (opcode & 0x40)
  324. X            {
  325. X              (void) ReadData((char *) integer,1,2,image->file);
  326. X              operand=integer[0]+(integer[1] << 8);
  327. X            }
  328. X          x+=operand;
  329. X          break;
  330. X        }
  331. X        case ByteDataOp:
  332. X        {
  333. X          operand=fgetc(image->file);
  334. X          if (opcode & 0x40)
  335. X            {
  336. X              (void) ReadData((char *) integer,1,2,image->file);
  337. X              operand=integer[0]+(integer[1] << 8);
  338. X            }
  339. X          p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
  340. X            x*number_planes+plane;
  341. X          operand++;
  342. X          for (i=0; i < operand; i++)
  343. X          {
  344. X            pixel=fgetc(image->file);
  345. X            if ((y < image->rows) && ((x+i) < image->columns))
  346. X              *p=pixel;
  347. X            p+=number_planes;
  348. X          }
  349. X          if (operand & 0x01)
  350. X            (void) fgetc(image->file);
  351. X          x+=operand;
  352. X          break;
  353. X        }
  354. X        case RunDataOp:
  355. X        {
  356. X          operand=fgetc(image->file);
  357. X          if (opcode & 0x40)
  358. X            {
  359. X              (void) ReadData((char *) integer,1,2,image->file);
  360. X              operand=integer[0]+(integer[1] << 8);
  361. X            }
  362. X          pixel=fgetc(image->file);
  363. X          (void) fgetc(image->file);
  364. X          operand++;
  365. X          p=rle_pixels+((image->rows-y-1)*image->columns*number_planes)+
  366. X            x*number_planes+plane;
  367. X          for (i=0; i < operand; i++)
  368. X          {
  369. X            if ((y < image->rows) && ((x+i) < image->columns))
  370. X              *p=pixel;
  371. X            p+=number_planes;
  372. X          }
  373. X          x+=operand;
  374. X          break;
  375. X        }
  376. X        default:
  377. X          break;
  378. X      }
  379. X      opcode=fgetc(image->file);
  380. X    }
  381. X    if (number_colormaps != 0)
  382. X      {
  383. X        unsigned char
  384. X          pixel;
  385. X
  386. X        unsigned int
  387. X          mask;
  388. X
  389. X        /*
  390. X          Apply colormap transformation to image.
  391. X        */
  392. X        mask=(map_length-1);
  393. X        p=rle_pixels;
  394. X        for (i=0; i < image->packets; i++)
  395. X          for (j=0; j < number_planes; j++)
  396. X          {
  397. X            pixel=colormap[j*map_length+(*p & mask)];
  398. X            *p++=pixel;
  399. X          }
  400. X        (void) free((char *) colormap);
  401. X      }
  402. X    /*
  403. X      Create image.
  404. X    */
  405. X    image->pixels=(RunlengthPacket *)
  406. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  407. X    if (image->pixels == (RunlengthPacket *) NULL)
  408. X      {
  409. X        Warning("memory allocation error",(char *) NULL);
  410. X        DestroyImage(image);
  411. X        return((Image *) NULL);
  412. X      }
  413. X    p=rle_pixels;
  414. X    q=image->pixels;
  415. X    if (number_planes >= 3)
  416. X      {
  417. X        /*
  418. X          Convert raster image to DirectClass runlength-encoded packets.
  419. X        */
  420. X        for (i=0; i < (image->columns*image->rows); i++)
  421. X        {
  422. X          q->red=(*p++);
  423. X          q->green=(*p++);
  424. X          q->blue=(*p++);
  425. X          q->index=(unsigned short) (image->alpha ? (*p++) : 0);
  426. X          q->length=0;
  427. X          q++;
  428. X        }
  429. X      }
  430. X    else
  431. X      {
  432. X        unsigned short
  433. X          index;
  434. X
  435. X        /*
  436. X          Create grayscale map.
  437. X        */
  438. X        image->class=PseudoClass;
  439. X        image->colors=256;
  440. X        image->colormap=(ColorPacket *)
  441. X          malloc(image->colors*sizeof(ColorPacket));
  442. X        if (image->colormap == (ColorPacket *) NULL)
  443. X          {
  444. X            Warning("unable to read image","memory allocation failed");
  445. X            DestroyImage(image);
  446. X            return((Image *) NULL);
  447. X          }
  448. X        for (i=0; i < image->colors; i++)
  449. X        {
  450. X          image->colormap[i].red=(unsigned char) i;
  451. X          image->colormap[i].green=(unsigned char) i;
  452. X          image->colormap[i].blue=(unsigned char) i;
  453. X        }
  454. X        /*
  455. X          Convert raster image to PseudoClass runlength-encoded packets.
  456. X        */
  457. X        for (i=0; i < (image->columns*image->rows); i++)
  458. X        {
  459. X          index=(unsigned short) (*p++);
  460. X          q->red=image->colormap[index].red;
  461. X          q->green=image->colormap[index].green;
  462. X          q->blue=image->colormap[index].blue;
  463. X          q->index=index;
  464. X          q->length=0;
  465. X          q++;
  466. X        }
  467. X      }
  468. X    (void) free((char *) rle_pixels);
  469. X    /*
  470. X      Proceed to next image.
  471. X    */
  472. X    (void) fgetc(image->file);
  473. X    status=ReadData((char *) magick,1,2,image->file);
  474. X    if ((status == True) && (strncmp(magick,"\122\314",2) == 0))
  475. X      {
  476. X        /*
  477. X          Allocate next image structure.
  478. X        */
  479. X        image->next=AllocateImage("RLE");
  480. X        if (image->next == (Image *) NULL)
  481. X          {
  482. X            DestroyImages(image);
  483. X            return((Image *) NULL);
  484. X          }
  485. X        image->next->file=image->file;
  486. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  487. X          image->scene+1);
  488. X        image->next->scene=image->scene+1;
  489. X        image->next->previous=image;
  490. X        image=image->next;
  491. X      }
  492. X  } while ((status == True) && (strncmp(magick,"\122\314",2) == 0));
  493. X  while (image->previous != (Image *) NULL)
  494. X    image=image->previous;
  495. X  CloseImage(image);
  496. X  return(image);
  497. }
  498. X
  499. /*
  500. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  501. %                                                                             %
  502. %                                                                             %
  503. %                                                                             %
  504. %  R e a d S U N I m a g e                                                    %
  505. %                                                                             %
  506. %                                                                             %
  507. %                                                                             %
  508. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  509. %
  510. %  Function ReadSUNImage reads a SUN image file and returns it.  It allocates
  511. %  the memory necessary for the new Image structure and returns a pointer to
  512. %  the new image.
  513. %
  514. %  The format of the ReadSUNImage routine is:
  515. %
  516. %      image=ReadSUNImage(image_info)
  517. %
  518. %  A description of each parameter follows:
  519. %
  520. %    o image:  Function ReadSUNImage returns a pointer to the image after
  521. %      reading.  A null image is returned if there is a a memory shortage or
  522. %      if the image cannot be read.
  523. %
  524. %    o image_info: Specifies a pointer to an ImageInfo structure.
  525. %
  526. %
  527. */
  528. static Image *ReadSUNImage(image_info)
  529. ImageInfo
  530. X  *image_info;
  531. {
  532. #define RMT_EQUAL_RGB  1
  533. #define RMT_NONE  0
  534. #define RMT_RAW  2
  535. #define RT_STANDARD  1
  536. #define RT_ENCODED  2
  537. #define RT_FORMAT_RGB  3
  538. X
  539. X  typedef struct _SUNHeader
  540. X  {
  541. X    unsigned long
  542. X      magic,
  543. X      width,
  544. X      height,
  545. X      depth,
  546. X      length,
  547. X      type,
  548. X      maptype,
  549. X      maplength;
  550. X  } SUNHeader;
  551. X
  552. X  Image
  553. X    *image;
  554. X
  555. X  register int
  556. X    bit,
  557. X    i,
  558. X    x,
  559. X    y;
  560. X
  561. X  register RunlengthPacket
  562. X    *q;
  563. X
  564. X  register unsigned char
  565. X    *p;
  566. X
  567. X  SUNHeader
  568. X    sun_header;
  569. X
  570. X  unsigned char
  571. X    *sun_data,
  572. X    *sun_pixels;
  573. X
  574. X  unsigned int
  575. X    status;
  576. X
  577. X  /*
  578. X    Allocate image structure.
  579. X  */
  580. X  image=AllocateImage("SUN");
  581. X  if (image == (Image *) NULL)
  582. X    return((Image *) NULL);
  583. X  /*
  584. X    Open image file.
  585. X  */
  586. X  (void) strcpy(image->filename,image_info->filename);
  587. X  OpenImage(image,"r");
  588. X  if (image->file == (FILE *) NULL)
  589. X    {
  590. X      Warning("unable to open file",image->filename);
  591. X      DestroyImage(image);
  592. X      return((Image *) NULL);
  593. X    }
  594. X  /*
  595. X    Read SUN raster header.
  596. X  */
  597. X  sun_header.magic=MSBFirstReadLong(image->file);
  598. X  do
  599. X  {
  600. X    /*
  601. X      Verify SUN identifier.
  602. X    */
  603. X    if (sun_header.magic != 0x59a66a95)
  604. X      {
  605. X        Warning("not a SUN raster,",image->filename);
  606. X        DestroyImages(image);
  607. X        return((Image *) NULL);
  608. X      }
  609. X    sun_header.width=MSBFirstReadLong(image->file);
  610. X    sun_header.height=MSBFirstReadLong(image->file);
  611. X    sun_header.depth=MSBFirstReadLong(image->file);
  612. X    sun_header.length=MSBFirstReadLong(image->file);
  613. X    sun_header.type=MSBFirstReadLong(image->file);
  614. X    sun_header.maptype=MSBFirstReadLong(image->file);
  615. X    sun_header.maplength=MSBFirstReadLong(image->file);
  616. X    switch (sun_header.maptype)
  617. X    {
  618. X      case RMT_NONE:
  619. X      {
  620. X        if (sun_header.depth < 24)
  621. X          {
  622. X            /*
  623. X              Create linear color ramp.
  624. X            */
  625. X            image->colors=1 << sun_header.depth;
  626. X            image->colormap=(ColorPacket *)
  627. X              malloc(image->colors*sizeof(ColorPacket));
  628. X            if (image->colormap == (ColorPacket *) NULL)
  629. X              {
  630. X                Warning("memory allocation error",(char *) NULL);
  631. X                return((Image *) NULL);
  632. X              }
  633. X            for (i=0; i < image->colors; i++)
  634. X            {
  635. X              image->colormap[i].red=(255*i)/(image->colors-1);
  636. X              image->colormap[i].green=(255*i)/(image->colors-1);
  637. X              image->colormap[i].blue=(255*i)/(image->colors-1);
  638. X            }
  639. X          }
  640. X        break;
  641. X      }
  642. X      case RMT_EQUAL_RGB:
  643. X      {
  644. X        unsigned char
  645. X          *sun_colormap;
  646. X
  647. X        /*
  648. X          Read SUN raster colormap.
  649. X        */
  650. X        image->colors=sun_header.maplength/3;
  651. X        image->colormap=(ColorPacket *)
  652. X          malloc(image->colors*sizeof(ColorPacket));
  653. X        sun_colormap=(unsigned char *)
  654. X          malloc(image->colors*sizeof(unsigned char));
  655. X        if ((image->colormap == (ColorPacket *) NULL) ||
  656. X            (sun_colormap == (unsigned char *) NULL))
  657. X          {
  658. X            Warning("memory allocation error",(char *) NULL);
  659. X            DestroyImages(image);
  660. X            return((Image *) NULL);
  661. X          }
  662. X        (void) ReadData((char *) sun_colormap,1,(int) image->colors,
  663. X          image->file);
  664. X        for (i=0; i < image->colors; i++)
  665. X          image->colormap[i].red=sun_colormap[i];
  666. X        (void) ReadData((char *) sun_colormap,1,(int) image->colors,
  667. X          image->file);
  668. X        for (i=0; i < image->colors; i++)
  669. X          image->colormap[i].green=sun_colormap[i];
  670. X        (void) ReadData((char *) sun_colormap,1,(int) image->colors,
  671. X          image->file);
  672. X        for (i=0; i < image->colors; i++)
  673. X          image->colormap[i].blue=sun_colormap[i];
  674. X        (void) free((char *) sun_colormap);
  675. X        break;
  676. X      }
  677. X      case RMT_RAW:
  678. X      {
  679. X        unsigned char
  680. X          *sun_colormap;
  681. X
  682. X        /*
  683. X          Read SUN raster colormap.
  684. X        */
  685. X        sun_colormap=(unsigned char *)
  686. X          malloc(sun_header.maplength*sizeof(unsigned char));
  687. X        if (sun_colormap == (unsigned char *) NULL)
  688. X          {
  689. X            Warning("memory allocation error",(char *) NULL);
  690. X            DestroyImages(image);
  691. X            return((Image *) NULL);
  692. X          }
  693. X        (void) ReadData((char *) sun_colormap,1,(int) sun_header.maplength,
  694. X          image->file);
  695. X        (void) free((char *) sun_colormap);
  696. X        break;
  697. X      }
  698. X      default:
  699. X      {
  700. X        Warning("colormap type is not supported",image->filename);
  701. X        DestroyImages(image);
  702. X        return((Image *) NULL);
  703. X      }
  704. X    }
  705. X    sun_data=(unsigned char *) malloc(sun_header.length*sizeof(unsigned char));
  706. X    if (sun_data == (unsigned char *) NULL)
  707. X      {
  708. X        Warning("memory allocation error",(char *) NULL);
  709. X        DestroyImages(image);
  710. X        return((Image *) NULL);
  711. X      }
  712. X    status=ReadData((char *) sun_data,1,(int) sun_header.length,image->file);
  713. X    if (status == False)
  714. X      {
  715. X        Warning("unable to read image data",image_info->filename);
  716. X        DestroyImages(image);
  717. X        return((Image *) NULL);
  718. X      }
  719. X    sun_pixels=sun_data;
  720. X    if (sun_header.type == RT_ENCODED)
  721. X      {
  722. X        unsigned int
  723. X          number_pixels;
  724. X
  725. X        /*
  726. X          Read run-length encoded raster pixels.
  727. X        */
  728. X        number_pixels=(sun_header.width+(sun_header.width % 2))*
  729. X          sun_header.height*(((sun_header.depth-1) >> 3)+1);
  730. X        sun_pixels=(unsigned char *)
  731. X          malloc(number_pixels*sizeof(unsigned char));
  732. X        if (sun_pixels == (unsigned char *) NULL)
  733. X          {
  734. X            Warning("memory allocation error",(char *) NULL);
  735. X            DestroyImages(image);
  736. X            return((Image *) NULL);
  737. X          }
  738. X        (void) SUNDecodeImage(sun_data,sun_pixels,
  739. X          (unsigned int) sun_header.width,(unsigned int) sun_header.height);
  740. X        (void) free((char *) sun_data);
  741. X      }
  742. X    /*
  743. X      Create image.
  744. X    */
  745. X    image->alpha=(sun_header.depth == 32);
  746. X    image->class=(sun_header.depth < 24 ? PseudoClass : DirectClass);
  747. X    image->columns=sun_header.width;
  748. X    image->rows=sun_header.height;
  749. X    image->packets=image->columns*image->rows;
  750. X    image->pixels=(RunlengthPacket *)
  751. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  752. X    image->comments=(char *)
  753. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  754. X    (void) sprintf(image->comments,
  755. X      "\n  Imported from SUN raster image:  %s\n",image->filename);
  756. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  757. X        (image->comments == (char *) NULL))
  758. X      {
  759. X        Warning("memory allocation error",(char *) NULL);
  760. X        DestroyImages(image);
  761. X        return((Image *) NULL);
  762. X      }
  763. X    /*
  764. X      Convert SUN raster image to runlength-encoded packets.
  765. X    */
  766. X    p=sun_pixels;
  767. X    q=image->pixels;
  768. X    if (sun_header.depth == 1)
  769. X      for (y=0; y < image->rows; y++)
  770. X      {
  771. X        /*
  772. X          Convert bitmap scanline to runlength-encoded color packets.
  773. X        */
  774. X        for (x=0; x < (image->columns >> 3); x++)
  775. X        {
  776. X          for (bit=7; bit >= 0; bit--)
  777. X          {
  778. X            q->index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
  779. X            q->length=0;
  780. X            q++;
  781. X          }
  782. X          p++;
  783. X        }
  784. X        if ((image->columns % 8) != 0)
  785. X          {
  786. X            for (bit=7; bit >= (8-(image->columns % 8)); bit--)
  787. X            {
  788. X              q->index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
  789. X              q->length=0;
  790. X              q++;
  791. X            }
  792. X            p++;
  793. X          }
  794. X        if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
  795. X          p++;
  796. X      }
  797. X    else
  798. X      if (image->class == PseudoClass)
  799. X        for (y=0; y < image->rows; y++)
  800. X        {
  801. X          /*
  802. X            Convert PseudoColor scanline to runlength-encoded color packets.
  803. X          */
  804. X          for (x=0; x < image->columns; x++)
  805. X          {
  806. X            q->index=(*p++);
  807. X            q->length=0;
  808. X            q++;
  809. X          }
  810. X          if ((image->columns % 2) != 0)
  811. X            p++;
  812. X        }
  813. X      else
  814. X        for (y=0; y < image->rows; y++)
  815. X        {
  816. X          /*
  817. X            Convert DirectColor scanline to runlength-encoded color packets.
  818. X          */
  819. X          for (x=0; x < image->columns; x++)
  820. X          {
  821. X            q->index=(unsigned short) (image->alpha ? (*p++) : 0);
  822. X            if (sun_header.type == RT_STANDARD)
  823. X              {
  824. X                q->blue=(*p++);
  825. X                q->green=(*p++);
  826. X                q->red=(*p++);
  827. X              }
  828. X            else
  829. X              {
  830. X                q->red=(*p++);
  831. X                q->green=(*p++);
  832. X                q->blue=(*p++);
  833. X              }
  834. X            if (image->colors != 0)
  835. X              {
  836. X                q->red=image->colormap[q->red].red;
  837. X                q->green=image->colormap[q->green].green;
  838. X                q->blue=image->colormap[q->blue].blue;
  839. X              }
  840. X            q->length=0;
  841. X            q++;
  842. X          }
  843. X          if (((image->columns % 2) != 0) && (image->alpha == False))
  844. X            p++;
  845. X        }
  846. X    (void) free((char *) sun_pixels);
  847. X    if (image->class == PseudoClass)
  848. X      {
  849. X        SyncImage(image);
  850. X        CompressColormap(image);
  851. X      }
  852. X    /*
  853. X      Proceed to next image.
  854. X    */
  855. X    sun_header.magic=MSBFirstReadLong(image->file);
  856. X    if (sun_header.magic == 0x59a66a95)
  857. X      {
  858. X        /*
  859. X          Allocate image structure.
  860. X        */
  861. X        image->next=AllocateImage("SUN");
  862. X        if (image->next == (Image *) NULL)
  863. X          {
  864. X            DestroyImages(image);
  865. X            return((Image *) NULL);
  866. X          }
  867. X        image->next->file=image->file;
  868. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  869. X          image->scene+1);
  870. X        image->next->scene=image->scene+1;
  871. X        image->next->previous=image;
  872. X        image=image->next;
  873. X      }
  874. X  } while (sun_header.magic == 0x59a66a95);
  875. X  while (image->previous != (Image *) NULL)
  876. X    image=image->previous;
  877. X  CloseImage(image);
  878. X  return(image);
  879. }
  880. X
  881. /*
  882. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  883. %                                                                             %
  884. %                                                                             %
  885. %                                                                             %
  886. %  R e a d T A R G A I m a g e                                                %
  887. %                                                                             %
  888. %                                                                             %
  889. %                                                                             %
  890. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  891. %
  892. %  Function ReadTARGAImage reads a Truevision Targa image file and returns
  893. %  it.  It allocates the memory necessary for the new Image structure and
  894. %  returns a pointer to the new image.
  895. %
  896. %  The format of the ReadTARGAImage routine is:
  897. %
  898. %      image=ReadTARGAImage(image_info)
  899. %
  900. %  A description of each parameter follows:
  901. %
  902. %    o image:  Function ReadTARGAImage returns a pointer to the image after
  903. %      reading.  A null image is returned if there is a a memory shortage or
  904. %      if the image cannot be read.
  905. %
  906. %    o image_info: Specifies a pointer to an ImageInfo structure.
  907. %
  908. %
  909. */
  910. static Image *ReadTARGAImage(image_info)
  911. ImageInfo
  912. X  *image_info;
  913. {
  914. #define TargaColormap 1
  915. #define TargaRGB 2
  916. #define TargaMonochrome 3
  917. #define TargaRLEColormap  9
  918. #define TargaRLERGB  10
  919. #define TargaRLEMonochrome  11
  920. X
  921. X  typedef struct _TargaHeader
  922. X  {
  923. X    unsigned char
  924. X      id_length,
  925. X      colormap_type,
  926. X      image_type;
  927. X
  928. X    unsigned short
  929. X      colormap_index,
  930. X      colormap_length;
  931. X
  932. X    unsigned char
  933. X      colormap_size;
  934. X
  935. X    unsigned short
  936. X      x_origin,
  937. X      y_origin,
  938. X      width,
  939. X      height;
  940. X
  941. X    unsigned char
  942. X      pixel_size,
  943. X      attributes;
  944. X  } TargaHeader;
  945. X
  946. X  Image
  947. X    *image;
  948. X
  949. X  register int
  950. X    i,
  951. X    x,
  952. X    y;
  953. X
  954. X  register RunlengthPacket
  955. X    *q;
  956. X
  957. X  TargaHeader
  958. X    targa_header;
  959. X
  960. X  unsigned char
  961. X    blue,
  962. X    green,
  963. X    j,
  964. X    k,
  965. X    red,
  966. X    runlength;
  967. X
  968. X  unsigned int
  969. X    base,
  970. X    flag,
  971. X    real,
  972. X    skip,
  973. X    status,
  974. X    true;
  975. X
  976. X  unsigned short
  977. X    index;
  978. X
  979. X  /*
  980. X    Allocate image structure.
  981. X  */
  982. X  image=AllocateImage("TGA");
  983. X  if (image == (Image *) NULL)
  984. X    return((Image *) NULL);
  985. X  /*
  986. X    Open image file.
  987. X  */
  988. X  (void) strcpy(image->filename,image_info->filename);
  989. X  OpenImage(image,"r");
  990. X  if (image->file == (FILE *) NULL)
  991. X    {
  992. X      Warning("unable to open file",image->filename);
  993. X      DestroyImage(image);
  994. X      return((Image *) NULL);
  995. X    }
  996. X  /*
  997. X    Determine if this is a TARGA file.
  998. X  */
  999. X  status=ReadData((char *) &targa_header.id_length,1,1,image->file);
  1000. X  do
  1001. X  {
  1002. X    /*
  1003. X      Read TARGA header information.
  1004. X    */
  1005. X    if (status == False)
  1006. X      {
  1007. X        Warning("not a TARGA image file",(char *) NULL);
  1008. X        DestroyImages(image);
  1009. X        return((Image *) NULL);
  1010. X      }
  1011. X    targa_header.colormap_type=fgetc(image->file);
  1012. X    targa_header.image_type=fgetc(image->file);
  1013. X    targa_header.colormap_index=LSBFirstReadShort(image->file);
  1014. X    targa_header.colormap_length=LSBFirstReadShort(image->file);
  1015. X    targa_header.colormap_size=fgetc(image->file);
  1016. X    targa_header.x_origin=LSBFirstReadShort(image->file);
  1017. X    targa_header.y_origin=LSBFirstReadShort(image->file);
  1018. X    targa_header.width=LSBFirstReadShort(image->file);
  1019. X    targa_header.height=LSBFirstReadShort(image->file);
  1020. X    targa_header.pixel_size=fgetc(image->file);
  1021. X    targa_header.attributes=fgetc(image->file);
  1022. X    /*
  1023. X      Create image.
  1024. X    */
  1025. X    image->alpha=targa_header.pixel_size == 32;
  1026. X    image->columns=targa_header.width;
  1027. X    image->rows=targa_header.height;
  1028. X    image->packets=image->columns*image->rows;
  1029. X    image->pixels=(RunlengthPacket *)
  1030. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1031. X    if (targa_header.id_length != 0)
  1032. X      image->comments=(char *)
  1033. X        malloc((targa_header.id_length+1)*sizeof(char));
  1034. X    else
  1035. X      image->comments=(char *)
  1036. X        malloc((strlen(image->filename)+2048)*sizeof(char));
  1037. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  1038. X        (image->comments == (char *) NULL))
  1039. X      {
  1040. X        Warning("memory allocation error",(char *) NULL);
  1041. X        DestroyImages(image);
  1042. X        return((Image *) NULL);
  1043. X      }
  1044. X    if (targa_header.id_length == 0)
  1045. X      (void) sprintf(image->comments,
  1046. X        "\n  Imported from TARGA raster image:  %s\n",image->filename);
  1047. X    else
  1048. X      {
  1049. X        (void) ReadData(image->comments,1,targa_header.id_length,image->file);
  1050. X        image->comments[targa_header.id_length]='\0';
  1051. X      }
  1052. X    red=0;
  1053. X    green=0;
  1054. X    blue=0;
  1055. X    if (targa_header.colormap_type != 0)
  1056. X      {
  1057. X        /*
  1058. X          Read TARGA raster colormap.
  1059. X        */
  1060. X        image->class=PseudoClass;
  1061. X        image->colors=targa_header.colormap_length;
  1062. X        image->colormap=(ColorPacket *)
  1063. X          malloc(image->colors*sizeof(ColorPacket));
  1064. X        if (image->colormap == (ColorPacket *) NULL)
  1065. X          {
  1066. X            Warning("memory allocation error",(char *) NULL);
  1067. X            DestroyImages(image);
  1068. X            return((Image *) NULL);
  1069. X          }
  1070. X        for (i=0; i < image->colors; i++)
  1071. X        {
  1072. X          switch (targa_header.colormap_size)
  1073. X          {
  1074. X            case 8:
  1075. X            default:
  1076. X            {
  1077. X              /*
  1078. X                Gray scale.
  1079. X              */
  1080. X              red=fgetc(image->file);
  1081. X              green=red;
  1082. X              blue=red;
  1083. X              break;
  1084. X            }
  1085. X            case 15:
  1086. X            case 16:
  1087. X            {
  1088. X              /*
  1089. X                5 bits each of red green and blue.
  1090. X              */
  1091. X              j=fgetc(image->file);
  1092. X              k=fgetc(image->file);
  1093. X              red=(unsigned char) (k & 0x7C) >> 2;
  1094. X              green=((unsigned char) (k & 0x03) << 3)+
  1095. X                ((unsigned char) (j & 0xE0) >> 5);
  1096. X              blue=j & 0x1F;
  1097. X              break;
  1098. X            }
  1099. X            case 32:
  1100. X            case 24:
  1101. X            {
  1102. X              /*
  1103. X                8 bits each of blue green and red.
  1104. X              */
  1105. X              blue=fgetc(image->file);
  1106. X              green=fgetc(image->file);
  1107. X              red=fgetc(image->file);
  1108. X              break;
  1109. X            }
  1110. X          }
  1111. X          image->colormap[i].red=red;
  1112. X          image->colormap[i].green=green;
  1113. X          image->colormap[i].blue=blue;
  1114. X        }
  1115. X      }
  1116. X    /*
  1117. X      Convert TARGA pixels to runlength-encoded packets.
  1118. X    */
  1119. X    base=0;
  1120. X    flag=0;
  1121. X    index=0;
  1122. X    skip=False;
  1123. X    real=0;
  1124. X    runlength=0;
  1125. X    true=0;
  1126. X    for (y=0; y < image->rows; y++)
  1127. X    {
  1128. X      real=true;
  1129. X      if (((unsigned char) (targa_header.attributes & 0x20) >> 5) == 0)
  1130. X        real=image->rows-real-1;
  1131. X      q=image->pixels+(real*image->columns)-1;
  1132. X      for (x=0; x < image->columns; x++)
  1133. X      {
  1134. X        if ((targa_header.image_type == TargaRLEColormap) ||
  1135. X            (targa_header.image_type == TargaRLERGB) ||
  1136. X            (targa_header.image_type == TargaRLEMonochrome))
  1137. X          if (runlength != 0)
  1138. X            {
  1139. X              runlength--;
  1140. X              skip=flag != 0;
  1141. X            }
  1142. X          else
  1143. X            {
  1144. X              status=ReadData((char *) &runlength,1,1,image->file);
  1145. X              if (status == False)
  1146. X                {
  1147. X                  Warning("unable to read image data",image_info->filename);
  1148. X                  DestroyImages(image);
  1149. X                  return((Image *) NULL);
  1150. X                }
  1151. X              flag=runlength & 0x80;
  1152. X              if (flag != 0)
  1153. X                runlength-=128;
  1154. X              skip=False;
  1155. X            }
  1156. X        if (!skip)
  1157. X          switch (targa_header.pixel_size)
  1158. X          {
  1159. X            case 8:
  1160. X            default:
  1161. X            {
  1162. X              /*
  1163. X                Gray scale.
  1164. X              */
  1165. X              index=fgetc(image->file);
  1166. X              if (targa_header.colormap_type == 0)
  1167. X                {
  1168. X                  red=index;
  1169. X                  green=index;
  1170. X                  blue=index;
  1171. X                }
  1172. X              else
  1173. X                {
  1174. X                  red=image->colormap[index].red;
  1175. X                  green=image->colormap[index].green;
  1176. X                  blue=image->colormap[index].blue;
  1177. X                }
  1178. X              break;
  1179. X            }
  1180. X            case 15:
  1181. X            case 16:
  1182. X            {
  1183. X              /*
  1184. X                5 bits each of red green and blue.
  1185. X              */
  1186. X              j=fgetc(image->file);
  1187. X              k=fgetc(image->file);
  1188. X              red=(unsigned char) (k & 0x7C) >> 2;
  1189. X              green=((unsigned char) (k & 0x03) << 3)+
  1190. X                ((unsigned char) (j & 0xE0) >> 5);
  1191. X              blue=j & 0x1F;
  1192. X              break;
  1193. X            }
  1194. X            case 32:
  1195. X            case 24:
  1196. X            {
  1197. X              /*
  1198. X                8 bits each of blue green and red.
  1199. X              */
  1200. X              blue=fgetc(image->file);
  1201. X              green=fgetc(image->file);
  1202. X              red=fgetc(image->file);
  1203. X              if (targa_header.pixel_size == 32)
  1204. X                index=fgetc(image->file);
  1205. X              break;
  1206. X            }
  1207. X          }
  1208. X        if (status == False)
  1209. X          {
  1210. X            Warning("unable to read image data",image_info->filename);
  1211. X            DestroyImages(image);
  1212. X            return((Image *) NULL);
  1213. X          }
  1214. X        q->red=red;
  1215. X        q->green=green;
  1216. X        q->blue=blue;
  1217. X        q->index=index;
  1218. X        q->length=0;
  1219. X        q++;
  1220. X      }
  1221. X      if (((unsigned char) (targa_header.attributes & 0xc0) >> 6) == 4)
  1222. X        true+=4;
  1223. X      else
  1224. X        if (((unsigned char) (targa_header.attributes & 0xc0) >> 6) == 2)
  1225. X          true+=2;
  1226. X        else
  1227. X          true++;
  1228. X      if (true >= image->rows)
  1229. X        {
  1230. X          base++;
  1231. X          true=base;
  1232. X        }
  1233. X    }
  1234. X    if ((targa_header.image_type == TargaMonochrome) ||
  1235. X        (targa_header.image_type == TargaRLEMonochrome))
  1236. X      QuantizeImage(image,2,8,False,GRAYColorspace,True);
  1237. X    /*
  1238. X      Proceed to next image.
  1239. X    */
  1240. X    status=ReadData((char *) &targa_header.id_length,1,1,image->file);
  1241. X    if (status == True)
  1242. X      {
  1243. X        /*
  1244. X          Allocate image structure.
  1245. X        */
  1246. X        image->next=AllocateImage("TGA");
  1247. X        if (image->next == (Image *) NULL)
  1248. X          {
  1249. X            DestroyImages(image);
  1250. X            return((Image *) NULL);
  1251. X          }
  1252. X        image->next->file=image->file;
  1253. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  1254. X          image->scene+1);
  1255. X        image->next->scene=image->scene+1;
  1256. X        image->next->previous=image;
  1257. X        image=image->next;
  1258. X      }
  1259. X  } while (status == True);
  1260. X  while (image->previous != (Image *) NULL)
  1261. X    image=image->previous;
  1262. X  CloseImage(image);
  1263. X  return(image);
  1264. }
  1265. X
  1266. /*
  1267. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1268. %                                                                             %
  1269. %                                                                             %
  1270. %                                                                             %
  1271. %  R e a d T E X T I m a g e                                                  %
  1272. %                                                                             %
  1273. %                                                                             %
  1274. %                                                                             %
  1275. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1276. %
  1277. %  Function ReadTEXTImage reads a text file and returns it as an image.  It
  1278. %  allocates the memory necessary for the new Image structure and returns a
  1279. %  pointer to the new image.
  1280. %
  1281. %  The format of the ReadTEXTImage routine is:
  1282. %
  1283. %      image=ReadTEXTImage(image_info)
  1284. %
  1285. %  A description of each parameter follows:
  1286. %
  1287. %    o image:  Function ReadTEXTImage returns a pointer to the image after
  1288. %      reading. A null image is returned if there is a a memory shortage or if
  1289. %      the image cannot be read.
  1290. %
  1291. %    o image_info: Specifies a pointer to an ImageInfo structure.
  1292. %
  1293. %
  1294. */
  1295. static Image *ReadTEXTImage(image_info)
  1296. ImageInfo
  1297. X  *image_info;
  1298. {
  1299. #define PageHeight  60
  1300. #define PageWidth  80
  1301. X
  1302. X  char
  1303. X    *resource_value,
  1304. X    *text_status,
  1305. X    text[2048];
  1306. X
  1307. X  Display
  1308. X    *display;
  1309. X
  1310. X  Image
  1311. X    *image;
  1312. X
  1313. X  int
  1314. X    status,
  1315. X    x,
  1316. X    y;
  1317. X
  1318. X  register int
  1319. X    i;
  1320. X
  1321. X  register RunlengthPacket
  1322. X    *p;
  1323. X
  1324. X  RunlengthPacket
  1325. X    background_color;
  1326. X
  1327. X  unsigned int
  1328. X    height,
  1329. X    width;
  1330. X
  1331. X  XAnnotateInfo
  1332. X    annotate_info;
  1333. X
  1334. X  XFontStruct
  1335. X    *font_info;
  1336. X
  1337. X  XPixelInfo
  1338. X    pixel_info;
  1339. X
  1340. X  XResourceInfo
  1341. X    resource_info;
  1342. X
  1343. X  XrmDatabase
  1344. X    resource_database,
  1345. X    server_database;
  1346. X
  1347. X  XStandardColormap
  1348. X    map_info;
  1349. X
  1350. X  XVisualInfo
  1351. X    *visual_info;
  1352. X
  1353. X  XWindowInfo
  1354. X    image_window;
  1355. X
  1356. X  /*
  1357. X    Allocate image structure.
  1358. X  */
  1359. X  image=AllocateImage("TEXT");
  1360. X  if (image == (Image *) NULL)
  1361. X    return((Image *) NULL);
  1362. X  /*
  1363. X    Open image file.
  1364. X  */
  1365. X  (void) strcpy(image->filename,image_info->filename);
  1366. X  OpenImage(image,"r");
  1367. X  if (image->file == (FILE *) NULL)
  1368. X    {
  1369. X      Warning("unable to open file",image->filename);
  1370. X      DestroyImage(image);
  1371. X      return((Image *) NULL);
  1372. X    }
  1373. X  /*
  1374. X    Open X server connection.
  1375. X  */
  1376. X  display=XOpenDisplay(image_info->server_name);
  1377. X  if (display == (Display *) NULL)
  1378. X    {
  1379. X      Warning("unable to connect to X server",
  1380. X        XDisplayName(image_info->server_name));
  1381. X      return((Image *) NULL);
  1382. X    }
  1383. X  /*
  1384. X    Set our forgiving error handler.
  1385. X  */
  1386. X  XSetErrorHandler(XError);
  1387. X  /*
  1388. X    Initialize resource database.
  1389. X  */
  1390. X  XrmInitialize();
  1391. X  resource_database=XrmGetDatabase(display);
  1392. X  resource_value=XResourceManagerString(display);
  1393. X  if (resource_value == (char *) NULL)
  1394. X    resource_value="";
  1395. X  server_database=XrmGetStringDatabase(resource_value);
  1396. X  XrmMergeDatabases(server_database,&resource_database);
  1397. X  /*
  1398. X    Get user defaults from X resource database.
  1399. X  */
  1400. X  XGetResourceInfo(resource_database,client_name,&resource_info);
  1401. X  /*
  1402. X    Initialize visual info.
  1403. X  */
  1404. X  visual_info=XBestVisualInfo(display,"default",(char *) NULL,
  1405. X    (XStandardColormap *) NULL);
  1406. X  if (visual_info == (XVisualInfo *) NULL)
  1407. X    {
  1408. X      Warning("unable to get visual",resource_info.visual_type);
  1409. X      return((Image *) NULL);
  1410. X    }
  1411. X  /*
  1412. X    Determine background and foreground colors.
  1413. X  */
  1414. X  map_info.colormap=XDefaultColormap(display,visual_info->screen);
  1415. X  XGetPixelInfo(display,visual_info,&map_info,&resource_info,(Image *) NULL,
  1416. X    &pixel_info);
  1417. X  pixel_info.annotate_color=pixel_info.foreground_color;
  1418. X  pixel_info.annotate_index=1;
  1419. X  /*
  1420. X    Initialize font info.
  1421. X  */
  1422. X  if (image_info->font != (char *) NULL)
  1423. X    resource_info.font=image_info->font;
  1424. X  font_info=XBestFont(display,&resource_info,(char *) NULL,~0);
  1425. X  if (font_info == (XFontStruct *) NULL)
  1426. X    {
  1427. X      Warning("unable to load font",resource_info.font);
  1428. X      return((Image *) NULL);
  1429. X    }
  1430. X  /*
  1431. X    Window superclass.
  1432. X  */
  1433. X  image_window.id=XRootWindow(display,visual_info->screen);
  1434. X  image_window.screen=visual_info->screen;
  1435. X  image_window.depth=visual_info->depth;
  1436. X  image_window.visual_info=visual_info;
  1437. X  image_window.pixel_info=(&pixel_info);
  1438. X  image_window.font_info=font_info;
  1439. X  /*
  1440. X    Initialize Image structure.
  1441. X  */
  1442. X  width=PageWidth;
  1443. X  height=PageHeight;
  1444. X  if (image_info->density != (char *) NULL)
  1445. X    {
  1446. X      int
  1447. X        flags;
  1448. X
  1449. X      /*
  1450. X        User specified density.
  1451. X      */
  1452. X      flags=XParseGeometry(image_info->density,&x,&y,&width,&height);
  1453. X      if ((flags & WidthValue) == 0)
  1454. X        width=PageWidth;
  1455. X      if ((flags & HeightValue) == 0)
  1456. X        height=width;
  1457. X    }
  1458. X  image->columns=width*font_info->max_bounds.width+4;
  1459. X  image->rows=height*
  1460. X    (font_info->max_bounds.ascent+font_info->max_bounds.descent)+4;
  1461. X  image->packets=image->columns*image->rows;
  1462. X  image->pixels=(RunlengthPacket *)
  1463. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1464. X  image->comments=(char *) malloc((strlen(image->filename)+2048)*sizeof(char));
  1465. X  if ((image->pixels == (RunlengthPacket *) NULL) ||
  1466. X      (image->comments == (char *) NULL))
  1467. X    {
  1468. X      Warning("unable to allocate image","memory allocation error");
  1469. X      DestroyImage(image);
  1470. X      return((Image *) NULL);
  1471. X    }
  1472. X  (void) sprintf(image->comments,"\n  Imported from text file:  %s\n",
  1473. X    image->filename);
  1474. X  /*
  1475. X    Create colormap.
  1476. X  */
  1477. X  image->colors=2;
  1478. X  image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  1479. X  if (image->colormap == (ColorPacket *) NULL)
  1480. X    {
  1481. X      Warning("unable to read image","memory allocation failed");
  1482. X      DestroyImage(image);
  1483. X      return((Image *) NULL);
  1484. X    }
  1485. X  image->colormap[0].red=pixel_info.background_color.red >> 8;
  1486. X  image->colormap[0].green=pixel_info.background_color.green >> 8;
  1487. X  image->colormap[0].blue=pixel_info.background_color.blue >> 8;
  1488. X  image->colormap[1].red=pixel_info.foreground_color.red >> 8;
  1489. X  image->colormap[1].green=pixel_info.foreground_color.green >> 8;
  1490. X  image->colormap[1].blue=pixel_info.foreground_color.blue >> 8;
  1491. X  /*
  1492. X    Initialize text image to background color.
  1493. X  */
  1494. X  background_color.red=image->colormap[0].red;
  1495. X  background_color.green=image->colormap[0].green;
  1496. X  background_color.blue=image->colormap[0].blue;
  1497. X  background_color.index=0;
  1498. X  background_color.length=0;
  1499. X  p=image->pixels;
  1500. X  for (i=0; i < image->packets; i++)
  1501. X    *p++=background_color;
  1502. X  /*
  1503. X    Annotate the text image.
  1504. X  */
  1505. X  XGetAnnotateInfo(&annotate_info);
  1506. X  annotate_info.font_info=font_info;
  1507. X  annotate_info.text=(char *)
  1508. X    malloc((image->columns/Max(font_info->min_bounds.width,1)+2)*sizeof(char));
  1509. X  if (annotate_info.text == (char *) NULL)
  1510. X    {
  1511. X      Warning("unable to read image","memory allocation failed");
  1512. X      DestroyImage(image);
  1513. X      return((Image *) NULL);
  1514. X    }
  1515. X  image->colormap[0].red=pixel_info.background_color.red >> 8;
  1516. X  annotate_info.height=font_info->ascent+font_info->descent;
  1517. X  x=0;
  1518. X  y=0;
  1519. X  text[sizeof(text)-1]='\0';
  1520. X  text_status=fgets(text,sizeof(text)-1,image->file);
  1521. X  if ((int) strlen(text) > 0)
  1522. X    text[strlen(text)-1]='\0';
  1523. X  while (text_status != (char *) NULL)
  1524. X  {
  1525. X    *annotate_info.text='\0';
  1526. X    if (*text != '\0')
  1527. X      {
  1528. X        /*
  1529. X          Compute width of text.
  1530. X        */
  1531. X        (void) strcpy(annotate_info.text,text);
  1532. X        annotate_info.width=
  1533. X          XTextWidth(font_info,annotate_info.text,strlen(annotate_info.text));
  1534. X        if ((annotate_info.width+4) >= image->columns)
  1535. X          {
  1536. X            /*
  1537. X              Reduce text until width is within bounds.
  1538. X            */
  1539. X            i=strlen(annotate_info.text);
  1540. X            for (; (annotate_info.width+4) >= image->columns; i--)
  1541. X              annotate_info.width=XTextWidth(font_info,annotate_info.text,
  1542. X                (unsigned int) i);
  1543. X            annotate_info.text[i]='\0';
  1544. X            while ((i > 0) && !isspace(annotate_info.text[i]))
  1545. X              i--;
  1546. X            if (i > 0)
  1547. X              annotate_info.text[i]='\0';
  1548. X            annotate_info.width=XTextWidth(font_info,annotate_info.text,
  1549. X              strlen(annotate_info.text));
  1550. X          }
  1551. X        /*
  1552. X          Annotate image with text.
  1553. X        */
  1554. X        (void) sprintf(annotate_info.geometry,"%ux%u%+d%+d",
  1555. X          annotate_info.width,annotate_info.height,x+2,y+2);
  1556. X        status=XAnnotateImage(display,&image_window,&annotate_info,False,image);
  1557. X        if (status == 0)
  1558. X          {
  1559. X            Warning("unable to annotate image","memory allocation error");
  1560. X            DestroyImage(image);
  1561. X            return((Image *) NULL);
  1562. X          }
  1563. X      }
  1564. X    /*
  1565. X      Get next string.
  1566. X    */
  1567. X    if (strlen(text) != strlen(annotate_info.text))
  1568. X      (void) strcpy(text,text+strlen(annotate_info.text)+1);
  1569. X    else
  1570. X      {
  1571. X        text_status=fgets(text,sizeof(text)-1,image->file);
  1572. X        if ((int) strlen(text) > 0)
  1573. X          text[strlen(text)-1]='\0';
  1574. X      }
  1575. X    y+=annotate_info.height;
  1576. X    if ((text_status != (char *) NULL) &&
  1577. X        ((y+font_info->ascent+4) > image->rows))
  1578. X      {
  1579. X        /*
  1580. X          Page is full-- allocate next image structure.
  1581. X        */
  1582. X        image->orphan=True;
  1583. X        image->next=CopyImage(image,image->columns,image->rows,False);
  1584. X        image->orphan=False;
  1585. X        if (image->next == (Image *) NULL)
  1586. X          {
  1587. X            DestroyImages(image);
  1588. X            return((Image *) NULL);
  1589. X          }
  1590. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  1591. X          image->scene+1);
  1592. X        image->next->scene=image->scene+1;
  1593. X        image->next->previous=image;
  1594. X        image=image->next;
  1595. X        /*
  1596. X          Initialize text image to background color.
  1597. X        */
  1598. X        p=image->pixels;
  1599. X        for (i=0; i < image->packets; i++)
  1600. X          *p++=background_color;
  1601. X        y=0;
  1602. X      }
  1603. X  }
  1604. X  /*
  1605. X    Free resources.
  1606. X  */
  1607. X  (void) free((char *) annotate_info.text);
  1608. X  XFreeFont(display,font_info);
  1609. X  XFree((void *) visual_info);
  1610. X  /*
  1611. X    Force to runlength-encoded PseudoClass image.
  1612. X  */
  1613. X  while (image->previous != (Image *) NULL)
  1614. X  {
  1615. X    image->class=PseudoClass;
  1616. X    image=image->previous;
  1617. X  }
  1618. X  image->class=PseudoClass;
  1619. X  CloseImage(image);
  1620. X  XCloseDisplay(display);
  1621. X  return(image);
  1622. }
  1623. X
  1624. #ifdef HasTIFF
  1625. #include "tiff.h"
  1626. #include "tiffio.h"
  1627. X
  1628. /*
  1629. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1630. %                                                                             %
  1631. %                                                                             %
  1632. %                                                                             %
  1633. %  R e a d T I F F I m a g e                                                  %
  1634. %                                                                             %
  1635. %                                                                             %
  1636. %                                                                             %
  1637. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1638. %
  1639. %  Function ReadTIFFImage reads a Tagged image file and returns it.  It
  1640. %  allocates the memory necessary for the new Image structure and returns a
  1641. %  pointer to the new image.
  1642. %
  1643. %  The format of the ReadTIFFImage routine is:
  1644. %
  1645. %      image=ReadTIFFImage(image_info)
  1646. %
  1647. %  A description of each parameter follows:
  1648. %
  1649. %    o image:  Function ReadTIFFImage returns a pointer to the image after
  1650. %      reading.  A null image is returned if there is a a memory shortage or
  1651. %      if the image cannot be read.
  1652. %
  1653. %    o image_info: Specifies a pointer to an ImageInfo structure.
  1654. %
  1655. %
  1656. */
  1657. static Image *ReadTIFFImage(image_info)
  1658. ImageInfo
  1659. X  *image_info;
  1660. {
  1661. X  Image
  1662. X    *image;
  1663. X
  1664. X  int
  1665. X    range;
  1666. X
  1667. X  register int
  1668. X    i,
  1669. X    quantum,
  1670. X    x,
  1671. X    y;
  1672. X
  1673. X  register RunlengthPacket
  1674. X    *q;
  1675. X
  1676. X  TIFF
  1677. X    *tiff;
  1678. X
  1679. X  unsigned int
  1680. X    status;
  1681. X
  1682. X  unsigned long
  1683. X    height,
  1684. X    width;
  1685. X
  1686. X  unsigned short
  1687. X    bits_per_sample,
  1688. X    max_sample_value,
  1689. X    min_sample_value,
  1690. X    photometric,
  1691. X    samples_per_pixel;
  1692. X
  1693. X  /*
  1694. X    Allocate image structure.
  1695. X  */
  1696. X  image=AllocateImage("TIFF");
  1697. X  if (image == (Image *) NULL)
  1698. X    return((Image *) NULL);
  1699. X  /*
  1700. X    Open TIFF image tiff.
  1701. X  */
  1702. X  (void) strcpy(image->filename,image_info->filename);
  1703. X  tiff=TIFFOpen(image->filename,"r");
  1704. X  if (tiff == (TIFF *) NULL)
  1705. X    {
  1706. X      Warning("unable to open tiff image",image->filename);
  1707. X      DestroyImage(image);
  1708. X      return((Image *) NULL);
  1709. X    }
  1710. X  do
  1711. X  {
  1712. X    if (image_info->verbose)
  1713. X      TIFFPrintDirectory(tiff,stderr,False);
  1714. X    /*
  1715. X      Allocate memory for the image and pixel buffer.
  1716. X    */
  1717. X    TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width);
  1718. X    TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height);
  1719. X    for (quantum=1; quantum <= 16; quantum<<=1)
  1720. X    {
  1721. X      image->columns=width/quantum;
  1722. X      image->rows=height/quantum;
  1723. X      image->packets=image->columns*image->rows;
  1724. X      image->pixels=(RunlengthPacket *)
  1725. X        malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1726. X      if ((image->pixels != (RunlengthPacket *) NULL))
  1727. X        break;
  1728. X    }
  1729. X    image->comments=(char *)
  1730. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  1731. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  1732. X        (image->comments == (char *) NULL))
  1733. X      {
  1734. X        Warning("unable to allocate memory",(char *) NULL);
  1735. X        DestroyImages(image);
  1736. X        TIFFClose(tiff);
  1737. X        return((Image *) NULL);
  1738. X      }
  1739. X    (void) sprintf(image->comments,"\n  Imported from TIFF image %s.\n",
  1740. X      image->filename);
  1741. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
  1742. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
  1743. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
  1744. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
  1745. X    TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel);
  1746. X    range=max_sample_value-min_sample_value;
  1747. X    if ((bits_per_sample > 8) || (samples_per_pixel > 1) || TIFFIsTiled(tiff))
  1748. X      {
  1749. X        register unsigned long
  1750. SHAR_EOF
  1751. true || echo 'restore of ImageMagick/decode.c failed'
  1752. fi
  1753. echo 'End of ImageMagick part 16'
  1754. echo 'File ImageMagick/decode.c is continued in part 17'
  1755. echo 17 > _shar_seq_.tmp
  1756. exit 0
  1757.  
  1758. exit 0 # Just in case...
  1759. -- 
  1760.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1761. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1762.  "It's intuitively obvious to the |
  1763.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1764.