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

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