home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / imagemagic / part20 < prev    next >
Internet Message Format  |  1991-05-23  |  75KB

  1. Path: uunet!uunet!elroy.jpl.nasa.gov!lll-winken!sun-barr!newstop!exodus!dupont.com!cristy
  2. From: cristy@dupont.com
  3. Newsgroups: comp.sources.x
  4. Subject: v13i036: ImageMagick - Graphics display programs, Part20/21
  5. Message-ID: <13950@exodus.Eng.Sun.COM>
  6. Date: 24 May 91 03:22:04 GMT
  7. References: <csx-13i017:imagemagic@uunet.UU.NET>
  8. Sender: news@exodus.Eng.Sun.COM
  9. Lines: 2337
  10. Approved: argv@sun.com
  11.  
  12. Submitted-by: cristy@dupont.com
  13. Posting-number: Volume 13, Issue 36
  14. Archive-name: imagemagic/part20
  15.  
  16. #!/bin/sh
  17. # this is img.20 (part 20 of ImageMagick)
  18. # do not concatenate these parts, unpack them in order with /bin/sh
  19. # file ImageMagick/X.c continued
  20. #
  21. if test ! -r _shar_seq_.tmp; then
  22.     echo 'Please unpack part 1 first!'
  23.     exit 1
  24. fi
  25. (read Scheck
  26.  if test "$Scheck" != 20; then
  27.     echo Please unpack part "$Scheck" next!
  28.     exit 1
  29.  else
  30.     exit 0
  31.  fi
  32. ) < _shar_seq_.tmp || exit 1
  33. if test ! -f _shar_wnt_.tmp; then
  34.     echo 'x - still skipping ImageMagick/X.c'
  35. else
  36. echo 'x - continuing file ImageMagick/X.c'
  37. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/X.c' &&
  38. X                pixel>>=8;
  39. X              }
  40. X              for (j=0; j <= p->length; j++)
  41. X              {
  42. X                for (k=0; k < bytes_per_pixel; k++)
  43. X                  *q++=channel[k];
  44. X                x++;
  45. X                if (x == ximage->width)
  46. X                  {
  47. X                    x=0;
  48. X                    q+=scanline_pad;
  49. X                  }
  50. X              }
  51. X              p++;
  52. X            }
  53. X            break;
  54. X          }
  55. X        }
  56. X      else
  57. X        {
  58. X          /*
  59. X            Convert image to little-endian continuous-tone X image.
  60. X          */
  61. X          switch (ximage->bits_per_pixel)
  62. X          {
  63. X            case 2:
  64. X            {
  65. X              register unsigned int
  66. X                nibble;
  67. X
  68. X              /*
  69. X                Convert to contiguous 2 bit continuous-tone X image.
  70. X              */
  71. X              nibble=0;
  72. X              for (i=0; i < image->packets; i++)
  73. X              {
  74. X                pixel=XStandardPixel(map_info,(*p),8);
  75. X                pixel&=0xf;
  76. X                for (j=0; j <= p->length; j++)
  77. X                {
  78. X                  switch (nibble)
  79. X                  {
  80. X                    case 0:
  81. X                    {
  82. X                      *q=(unsigned char) pixel;
  83. X                      nibble++;
  84. X                      break;
  85. X                    }
  86. X                    case 1:
  87. X                    {
  88. X                      *q|=(unsigned char) (pixel << 2);
  89. X                      nibble++;
  90. X                      break;
  91. X                    }
  92. X                    case 2:
  93. X                    {
  94. X                      *q|=(unsigned char) (pixel << 4);
  95. X                      nibble++;
  96. X                      break;
  97. X                    }
  98. X                    case 3:
  99. X                    {
  100. X                      *q|=(unsigned char) (pixel << 6);
  101. X                      q++;
  102. X                      nibble=0;
  103. X                      break;
  104. X                    }
  105. X                  }
  106. X                  x++;
  107. X                  if (x == ximage->width)
  108. X                    {
  109. X                      x=0;
  110. X                      nibble=0;
  111. X                      q+=scanline_pad;
  112. X                    }
  113. X                }
  114. X                p++;
  115. X              }
  116. X              break;
  117. X            }
  118. X            case 4:
  119. X            {
  120. X              register unsigned int
  121. X                nibble;
  122. X
  123. X              /*
  124. X                Convert to contiguous 4 bit continuous-tone X image.
  125. X              */
  126. X              nibble=0;
  127. X              for (i=0; i < image->packets; i++)
  128. X              {
  129. X                pixel=XStandardPixel(map_info,(*p),8);
  130. X                pixel&=0xf;
  131. X                for (j=0; j <= p->length; j++)
  132. X                {
  133. X                  switch (nibble)
  134. X                  {
  135. X                    case 0:
  136. X                    {
  137. X                      *q=(unsigned char) pixel;
  138. X                      nibble++;
  139. X                      break;
  140. X                    }
  141. X                    case 1:
  142. X                    {
  143. X                      *q|=(unsigned char) (pixel << 4);
  144. X                      q++;
  145. X                      nibble=0;
  146. X                      break;
  147. X                    }
  148. X                  }
  149. X                  x++;
  150. X                  if (x == ximage->width)
  151. X                    {
  152. X                      x=0;
  153. X                      nibble=0;
  154. X                      q+=scanline_pad;
  155. X                    }
  156. X                }
  157. X                p++;
  158. X              }
  159. X              break;
  160. X            }
  161. X            case 6:
  162. X            case 8:
  163. X            {
  164. X              /*
  165. X                Convert to contiguous 8 bit continuous-tone X image.
  166. X              */
  167. X              for (i=0; i < image->packets; i++)
  168. X              {
  169. X                pixel=XStandardPixel(map_info,(*p),8);
  170. X                for (j=0; j <= p->length; j++)
  171. X                {
  172. X                  *q++=(unsigned char) pixel;
  173. X                  x++;
  174. X                  if (x == ximage->width)
  175. X                    {
  176. X                      x=0;
  177. X                      q+=scanline_pad;
  178. X                    }
  179. X                }
  180. X                p++;
  181. X              }
  182. X              break;
  183. X            }
  184. X            default:
  185. X            {
  186. X              if ((ximage->bits_per_pixel == 32) &&
  187. X                  (map_info->red_max == 255) &&
  188. X                  (map_info->green_max == 255) &&
  189. X                  (map_info->blue_max == 255) &&
  190. X                  (map_info->red_mult == 65536) &&
  191. X                  (map_info->green_mult == 256) &&
  192. X                  (map_info->blue_mult == 1))
  193. X                {
  194. X                  /*
  195. X                    Convert to 32 bit continuous-tone X image.
  196. X                  */
  197. X                  for (i=0; i < image->packets; i++)
  198. X                  {
  199. X                    for (j=0; j <= p->length; j++)
  200. X                    {
  201. X                      *q++=p->blue;
  202. X                      *q++=p->green;
  203. X                      *q++=p->red;
  204. X                      *q++=0;
  205. X                    }
  206. X                    p++;
  207. X                  }
  208. X                }
  209. X              else
  210. X                {
  211. X                  register int
  212. X                    k;
  213. X
  214. X                  register unsigned int
  215. X                    bytes_per_pixel;
  216. X
  217. X                  unsigned char
  218. X                    channel[sizeof(unsigned long)];
  219. X
  220. X                  /*
  221. X                    Convert to multi-byte continuous-tone X image.
  222. X                  */
  223. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  224. X                  for (i=0; i < image->packets; i++)
  225. X                  {
  226. X                    pixel=XStandardPixel(map_info,(*p),8);
  227. X                    for (k=0; k < bytes_per_pixel; k++)
  228. X                    {
  229. X                      channel[k]=(unsigned char) pixel;
  230. X                      pixel>>=8;
  231. X                    }
  232. X                    for (j=0; j <= p->length; j++)
  233. X                    {
  234. X                      for (k=0; k < bytes_per_pixel; k++)
  235. X                        *q++=channel[k];
  236. X                      x++;
  237. X                      if (x == ximage->width)
  238. X                        {
  239. X                          x=0;
  240. X                          q+=scanline_pad;
  241. X                        }
  242. X                    }
  243. X                    p++;
  244. X                  }
  245. X                }
  246. X              break;
  247. X            }
  248. X          }
  249. X        }
  250. X    }
  251. }
  252. X
  253. /*
  254. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  255. %                                                                             %
  256. %                                                                             %
  257. %                                                                             %
  258. %   X M a k e I m a g e M S B F i r s t                                       %
  259. %                                                                             %
  260. %                                                                             %
  261. %                                                                             %
  262. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  263. %
  264. %  Function XMakeImageMSBFirst initializes the pixel data of an X11 Image.
  265. %  The X image pixels are copied in most-significant bit and byte first order.
  266. %  The server's scanline pad is also resprected. Rather than using one or two
  267. %  general cases, many special cases are found here to help speed up the image
  268. %  conversion.
  269. %
  270. %  The format of the XMakeImageMSBFirst routine is:
  271. %
  272. %      XMakeImageMSBFirst(map_info,pixel_info,image,ximage)
  273. %
  274. %  A description of each parameter follows:
  275. %
  276. %    o map_info: Specifies a pointer to a XStandardColormap structure.
  277. %
  278. %    o pixel_info: Specifies a pointer to a XPixelInfo structure.
  279. %
  280. %    o image: Specifies a pointer to a Image structure;  returned from
  281. %      ReadImage.
  282. %
  283. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  284. %      XCreateImage.
  285. %
  286. %
  287. */
  288. void XMakeImageMSBFirst(map_info,pixel_info,image,ximage)
  289. XXStandardColormap
  290. X  *map_info;
  291. X
  292. XXPixelInfo
  293. X  *pixel_info;
  294. X
  295. Image
  296. X  *image;
  297. X
  298. XXImage
  299. X  *ximage;
  300. {
  301. X  register int
  302. X    i,
  303. X    j,
  304. X    x;
  305. X
  306. X  register RunlengthPacket
  307. X    *p;
  308. X
  309. X  register unsigned char
  310. X    *q;
  311. X
  312. X  register unsigned long
  313. X    pixel;
  314. X
  315. X  unsigned int
  316. X    scanline_pad;
  317. X
  318. X  unsigned long
  319. X    *pixels;
  320. X
  321. X  pixels=pixel_info->pixels;
  322. X  p=image->pixels;
  323. X  q=(unsigned char *) ximage->data;
  324. X  x=0;
  325. X  if (ximage->format == XYBitmap)
  326. X    {
  327. X      register unsigned char
  328. X        bit,
  329. X        byte;
  330. X
  331. X      register unsigned char
  332. X        foreground_pixel;
  333. X
  334. X      /*
  335. X        Convert image to big-endian bitmap.
  336. X      */
  337. X      foreground_pixel=(Intensity(image->colormap[0]) >
  338. X        Intensity(image->colormap[1]) ? 0 : 1);
  339. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  340. X      bit=0;
  341. X      byte=0;
  342. X      for (i=0; i < image->packets; i++)
  343. X      {
  344. X        for (j=0; j <= p->length; j++)
  345. X        {
  346. X          byte<<=1;
  347. X          if (p->index == foreground_pixel)
  348. X            byte|=0x01;
  349. X          bit++;
  350. X          if (bit == 8)
  351. X            {
  352. X              *q++=byte;
  353. X              bit=0;
  354. X              byte=0;
  355. X            }
  356. X          x++;
  357. X          if (x == ximage->width)
  358. X            {
  359. X              /*
  360. X                Advance to the next scanline.
  361. X              */
  362. X              if (bit > 0)
  363. X                *q=byte << (8-bit);
  364. X              q+=scanline_pad;
  365. X              bit=0;
  366. X              byte=0;
  367. X              x=0;
  368. X            }
  369. X        }
  370. X        p++;
  371. X      }
  372. X    }
  373. X  else
  374. X    {
  375. X      /*
  376. X        Convert image to big-endian X image.
  377. X      */
  378. X      scanline_pad=ximage->bytes_per_line-
  379. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  380. X      if (pixels != (unsigned long *) NULL)
  381. X        switch (ximage->bits_per_pixel)
  382. X        {
  383. X          case 2:
  384. X          {
  385. X            register unsigned int
  386. X              nibble;
  387. X
  388. X            /*
  389. X              Convert to 2 bit color-mapped X image.
  390. X            */
  391. X            nibble=0;
  392. X            for (i=0; i < image->packets; i++)
  393. X            {
  394. X              pixel=pixels[p->index] & 0xf;
  395. X              for (j=0; j <= p->length; j++)
  396. X              {
  397. X                switch (nibble)
  398. X                {
  399. X                  case 0:
  400. X                  {
  401. X                    *q=(unsigned char) (pixel << 6);
  402. X                    nibble++;
  403. X                    break;
  404. X                  }
  405. X                  case 1:
  406. X                  {
  407. X                    *q|=(unsigned char) (pixel << 4);
  408. X                    nibble++;
  409. X                    break;
  410. X                  }
  411. X                  case 2:
  412. X                  {
  413. X                    *q|=(unsigned char) (pixel << 2);
  414. X                    nibble++;
  415. X                    break;
  416. X                  }
  417. X                  case 3:
  418. X                  {
  419. X                    *q|=(unsigned char) pixel;
  420. X                    q++;
  421. X                    nibble=0;
  422. X                    break;
  423. X                  }
  424. X                }
  425. X                x++;
  426. X                if (x == ximage->width)
  427. X                  {
  428. X                    x=0;
  429. X                    nibble=0;
  430. X                    q+=scanline_pad;
  431. X                  }
  432. X              }
  433. X              p++;
  434. X            }
  435. X            break;
  436. X          }
  437. X          case 4:
  438. X          {
  439. X            register unsigned int
  440. X              nibble;
  441. X
  442. X            /*
  443. X              Convert to 4 bit color-mapped X image.
  444. X            */
  445. X            nibble=0;
  446. X            for (i=0; i < image->packets; i++)
  447. X            {
  448. X              pixel=pixels[p->index] & 0xf;
  449. X              for (j=0; j <= p->length; j++)
  450. X              {
  451. X                switch (nibble)
  452. X                {
  453. X                  case 0:
  454. X                  {
  455. X                    *q=(unsigned char) (pixel << 4);
  456. X                    nibble++;
  457. X                    break;
  458. X                  }
  459. X                  case 1:
  460. X                  {
  461. X                    *q|=(unsigned char) pixel;
  462. X                    q++;
  463. X                    nibble=0;
  464. X                    break;
  465. X                  }
  466. X                }
  467. X                x++;
  468. X                if (x == ximage->width)
  469. X                  {
  470. X                    x=0;
  471. X                    nibble=0;
  472. X                    q+=scanline_pad;
  473. X                  }
  474. X              }
  475. X              p++;
  476. X            }
  477. X            break;
  478. X          }
  479. X          case 8:
  480. X          {
  481. X            /*
  482. X              Convert to 8 bit color-mapped X image.
  483. X            */
  484. X            for (i=0; i < image->packets; i++)
  485. X            {
  486. X              pixel=pixels[p->index];
  487. X              for (j=0; j <= p->length; j++)
  488. X              {
  489. X                *q++=(unsigned char) pixel;
  490. X                x++;
  491. X                if (x == ximage->width)
  492. X                  {
  493. X                    x=0;
  494. X                    q+=scanline_pad;
  495. X                  }
  496. X              }
  497. X              p++;
  498. X            }
  499. X            break;
  500. X          }
  501. X          default:
  502. X          {
  503. X            register int
  504. X              k;
  505. X
  506. X            register unsigned int
  507. X              bytes_per_pixel;
  508. X
  509. X            unsigned char
  510. X              channel[sizeof(unsigned long)];
  511. X
  512. X            /*
  513. X              Convert to 8 bit color-mapped X image.
  514. X            */
  515. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  516. X            for (i=0; i < image->packets; i++)
  517. X            {
  518. X              pixel=pixels[p->index];
  519. X              for (k=bytes_per_pixel-1; k >= 0; k--)
  520. X              {
  521. X                channel[k]=(unsigned char) pixel;
  522. X                pixel>>=8;
  523. X              }
  524. X              for (j=0; j <= p->length; j++)
  525. X              {
  526. X                for (k=0; k < bytes_per_pixel; k++)
  527. X                  *q++=channel[k];
  528. X                x++;
  529. X                if (x == ximage->width)
  530. X                  {
  531. X                    x=0;
  532. X                    q+=scanline_pad;
  533. X                  }
  534. X              }
  535. X              p++;
  536. X            }
  537. X            break;
  538. X          }
  539. X        }
  540. X      else
  541. X        {
  542. X          /*
  543. X            Convert to big-endian continuous-tone X image.
  544. X          */
  545. X          switch (ximage->bits_per_pixel)
  546. X          {
  547. X            case 2:
  548. X            {
  549. X              register unsigned int
  550. X                nibble;
  551. X
  552. X              /*
  553. X                Convert to 4 bit continuous-tone X image.
  554. X              */
  555. X              nibble=0;
  556. X              for (i=0; i < image->packets; i++)
  557. X              {
  558. X                pixel=XStandardPixel(map_info,(*p),8);
  559. X                pixel&=0xf;
  560. X                for (j=0; j <= p->length; j++)
  561. X                {
  562. X                  switch (nibble)
  563. X                  {
  564. X                    case 0:
  565. X                    {
  566. X                      *q=(unsigned char) (pixel << 6);
  567. X                      nibble++;
  568. X                      break;
  569. X                    }
  570. X                    case 1:
  571. X                    {
  572. X                      *q|=(unsigned char) (pixel << 4);
  573. X                      nibble++;
  574. X                      break;
  575. X                    }
  576. X                    case 2:
  577. X                    {
  578. X                      *q|=(unsigned char) (pixel << 2);
  579. X                      nibble++;
  580. X                      break;
  581. X                    }
  582. X                    case 3:
  583. X                    {
  584. X                      *q|=(unsigned char) pixel;
  585. X                      q++;
  586. X                      nibble=0;
  587. X                      break;
  588. X                    }
  589. X                  }
  590. X                  x++;
  591. X                  if (x == ximage->width)
  592. X                    {
  593. X                      x=0;
  594. X                      nibble=0;
  595. X                      q+=scanline_pad;
  596. X                    }
  597. X                }
  598. X                p++;
  599. X              }
  600. X              break;
  601. X            }
  602. X            case 4:
  603. X            {
  604. X              register unsigned int
  605. X                nibble;
  606. X
  607. X              /*
  608. X                Convert to 4 bit continuous-tone X image.
  609. X              */
  610. X              nibble=0;
  611. X              for (i=0; i < image->packets; i++)
  612. X              {
  613. X                pixel=XStandardPixel(map_info,(*p),8);
  614. X                pixel&=0xf;
  615. X                for (j=0; j <= p->length; j++)
  616. X                {
  617. X                  switch (nibble)
  618. X                  {
  619. X                    case 0:
  620. X                    {
  621. X                      *q=(unsigned char) (pixel << 4);
  622. X                      nibble++;
  623. X                      break;
  624. X                    }
  625. X                    case 1:
  626. X                    {
  627. X                      *q|=(unsigned char) pixel;
  628. X                      q++;
  629. X                      nibble=0;
  630. X                      break;
  631. X                    }
  632. X                  }
  633. X                  x++;
  634. X                  if (x == ximage->width)
  635. X                    {
  636. X                      x=0;
  637. X                      nibble=0;
  638. X                      q+=scanline_pad;
  639. X                    }
  640. X                }
  641. X                p++;
  642. X              }
  643. X              break;
  644. X            }
  645. X            case 8:
  646. X            {
  647. X              /*
  648. X                Convert to 8 bit continuous-tone X image.
  649. X              */
  650. X              for (i=0; i < image->packets; i++)
  651. X              {
  652. X                pixel=XStandardPixel(map_info,(*p),8);
  653. X                for (j=0; j <= p->length; j++)
  654. X                {
  655. X                  *q++=(unsigned char) pixel;
  656. X                  x++;
  657. X                  if (x == ximage->width)
  658. X                    {
  659. X                      x=0;
  660. X                      q+=scanline_pad;
  661. X                    }
  662. X                }
  663. X                p++;
  664. X              }
  665. X              break;
  666. X            }
  667. X            default:
  668. X            {
  669. X              if ((ximage->bits_per_pixel == 32) &&
  670. X                  (map_info->red_max == 255) &&
  671. X                  (map_info->green_max == 255) &&
  672. X                  (map_info->blue_max == 255) &&
  673. X                  (map_info->red_mult == 65536) &&
  674. X                  (map_info->green_mult == 256) &&
  675. X                  (map_info->blue_mult == 1))
  676. X                {
  677. X                  /*
  678. X                    Convert to 32 bit continuous-tone X image.
  679. X                  */
  680. X                  for (i=0; i < image->packets; i++)
  681. X                  {
  682. X                    for (j=0; j <= p->length; j++)
  683. X                    {
  684. X                      *q++=0;
  685. X                      *q++=p->red;
  686. X                      *q++=p->green;
  687. X                      *q++=p->blue;
  688. X                    }
  689. X                    p++;
  690. X                  }
  691. X                }
  692. X              else
  693. X                {
  694. X                  register int
  695. X                    k;
  696. X
  697. X                  register unsigned int
  698. X                    bytes_per_pixel;
  699. X
  700. X                  unsigned char
  701. X                    channel[sizeof(unsigned long)];
  702. X
  703. X                  /*
  704. X                    Convert to multi-byte continuous-tone X image.
  705. X                  */
  706. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  707. X                  for (i=0; i < image->packets; i++)
  708. X                  {
  709. X                    pixel=XStandardPixel(map_info,(*p),8);
  710. X                    for (k=bytes_per_pixel-1; k >= 0; k--)
  711. X                    {
  712. X                      channel[k]=(unsigned char) pixel;
  713. X                      pixel>>=8;
  714. X                    }
  715. X                    for (j=0; j <= p->length; j++)
  716. X                    {
  717. X                      for (k=0; k < bytes_per_pixel; k++)
  718. X                        *q++=channel[k];
  719. X                      x++;
  720. X                      if (x == ximage->width)
  721. X                        {
  722. X                          x=0;
  723. X                          q+=scanline_pad;
  724. X                        }
  725. X                    }
  726. X                    p++;
  727. X                  }
  728. X                }
  729. X              break;
  730. X            }
  731. X          }
  732. X        }
  733. X    }
  734. }
  735. X
  736. /*
  737. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  738. %                                                                             %
  739. %                                                                             %
  740. %                                                                             %
  741. %   X M a k e S t a n d a r d C o l o r m a p                                 %
  742. %                                                                             %
  743. %                                                                             %
  744. %                                                                             %
  745. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  746. %
  747. %  Function XMakeStandardColormap creates an X11 colormap.  If the visual
  748. %  class is TrueColor or DirectColor a Standard Colormap is also created.
  749. %
  750. %  The format of the XMakeStandardColormap routine is:
  751. %
  752. %      XMakeStandardColormap(display,visual_info,resource_info,pixel_info,
  753. %        image)
  754. %
  755. %  A description of each parameter follows:
  756. %
  757. %    o display: Specifies a connection to an X server; returned from
  758. %      XOpenDisplay.
  759. %
  760. %    o visual_info: Specifies a pointer to a X11 XVisualInfo structure;
  761. %      returned from XGetVisualInfo.
  762. %
  763. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  764. %
  765. %    o pixel_info: Specifies a pointer to a XPixelInfo structure.
  766. %
  767. %    o image: Specifies a pointer to a Image structure;  returned from
  768. %      ReadImage.
  769. %
  770. %
  771. */
  772. XXStandardColormap *XMakeStandardColormap(display,visual_info,resource_info,
  773. X  pixel_info,image)
  774. Display
  775. X  *display;
  776. X
  777. XXVisualInfo
  778. X  *visual_info;
  779. X
  780. XXResourceInfo
  781. X  *resource_info;
  782. X
  783. XXPixelInfo
  784. X  *pixel_info;
  785. X
  786. Image
  787. X  *image;
  788. {
  789. X  char
  790. X    *background_color,
  791. X    *foreground_color;
  792. X
  793. X  Colormap
  794. X    colormap;
  795. X
  796. X  int
  797. X    status;
  798. X
  799. X  register int
  800. X    i;
  801. X
  802. X  XColor
  803. X    color;
  804. X
  805. X  XStandardColormap
  806. X    *map_info;
  807. X
  808. X  Window
  809. X    root_window;
  810. X
  811. X  /*
  812. X    Initialize function return parameters.
  813. X  */
  814. X  root_window=XRootWindow(display,visual_info->screen);
  815. X  map_info=XAllocStandardColormap();
  816. X  if (map_info == (XStandardColormap *) NULL)
  817. X    Error("unable to create colormap","memory allocation failed");
  818. X  if ((visual_info->visual == XDefaultVisual(display,visual_info->screen)) &&
  819. X      (visual_info->class != DirectColor))
  820. X    map_info->colormap=XDefaultColormap(display,visual_info->screen);
  821. X  else
  822. X    if ((visual_info->class == StaticGray) ||
  823. X        (visual_info->class == StaticColor) ||
  824. X        (visual_info->class == TrueColor))
  825. X      map_info->colormap=
  826. X        XCreateColormap(display,root_window,visual_info->visual,AllocNone);
  827. X    else
  828. X      map_info->colormap=
  829. X        XCreateColormap(display,root_window,visual_info->visual,AllocAll);
  830. X  if (map_info->colormap == (Colormap) NULL)
  831. X    Error("unable to create colormap",(char *) NULL);
  832. X  map_info->red_max=0;
  833. X  map_info->green_max=0;
  834. X  map_info->blue_max=0;
  835. X  map_info->red_mult=0;
  836. X  map_info->green_mult=0;
  837. X  map_info->blue_mult=0;
  838. X  map_info->base_pixel=0;
  839. X  pixel_info->pixels=(unsigned long *) NULL;
  840. X  pixel_info->background_pixel=XBlackPixel(display,visual_info->screen);
  841. X  pixel_info->border_pixel=XWhitePixel(display,visual_info->screen);
  842. X  pixel_info->foreground_pixel=XWhitePixel(display,visual_info->screen);
  843. X  if (resource_info->monochrome)
  844. X    {
  845. X      background_color="black";
  846. X      foreground_color="white";
  847. X    }
  848. X  else
  849. X    {
  850. X      background_color=resource_info->background_color;
  851. X      foreground_color=resource_info->foreground_color;
  852. X    }
  853. X  colormap=map_info->colormap;
  854. X  if ((visual_info->class != TrueColor) && (visual_info->class != DirectColor))
  855. X    {
  856. X      if ((image->class == DirectClass) ||
  857. X          (image->colors > visual_info->colormap_size))
  858. X        {
  859. X          unsigned int
  860. X            number_colors;
  861. X
  862. X          /*
  863. X            Prevent colormap "flashing"; reserve some colormap entries.
  864. X          */
  865. X          number_colors=visual_info->colormap_size;
  866. X          if ((visual_info->class == GrayScale) ||
  867. X              (visual_info->class == PseudoColor))
  868. X            if (visual_info->visual ==
  869. X                XDefaultVisual(display,visual_info->screen))
  870. X              if ((visual_info->depth > 1) && !resource_info->backdrop)
  871. X                number_colors=(number_colors*61) >> 6;
  872. X          QuantizeImage(image,number_colors,0,False,False);
  873. X          image->class=DirectClass;
  874. X        }
  875. X      /*
  876. X        Allocate pixels array.
  877. X      */
  878. X      pixel_info->pixels=(unsigned long *)
  879. X        malloc((unsigned int) image->colors*sizeof(unsigned long));
  880. X      if (pixel_info->pixels == (unsigned long *) NULL)
  881. X        Error("unable to create colormap","memory allocation failed");
  882. X      /*
  883. X        Colormap creation is dictated by the server visual class.
  884. X      */
  885. X      switch (visual_info->class)
  886. X      {
  887. X        case StaticGray:
  888. X        case StaticColor:
  889. X        {
  890. X          /*
  891. X            Immutable colormap;  allocate shared colors.
  892. X          */
  893. X          color.flags=DoRed | DoGreen | DoBlue;
  894. X          for (i=0; i < image->colors; i++)
  895. X          {
  896. X            color.red=(unsigned short) (image->colormap[i].red << 8);
  897. X            color.green=(unsigned short) (image->colormap[i].green << 8);
  898. X            color.blue=(unsigned short) (image->colormap[i].blue << 8);
  899. X            status=XAllocColor(display,colormap,&color);
  900. X            if (status == 0)
  901. X              {
  902. X                colormap=XCopyColormapAndFree(display,colormap);
  903. X                XAllocColor(display,colormap,&color);
  904. X              }
  905. X            pixel_info->pixels[i]=color.pixel;
  906. X          }
  907. X          break;
  908. X        }
  909. X        case GrayScale:
  910. X        case PseudoColor:
  911. X        {
  912. X          XColor
  913. X            *colors;
  914. X
  915. X          /*
  916. X            Read/write colormap;  allocate private colors.
  917. X          */
  918. X          colors=(XColor *)
  919. X            malloc((unsigned int) visual_info->colormap_size*sizeof(XColor));
  920. X          if (colors == (XColor *) NULL)
  921. X            Error("unable to create colormap","memory allocation failed");
  922. X          status=XAllocColorCells(display,colormap,False,
  923. X            (unsigned long *) NULL,0,pixel_info->pixels,image->colors);
  924. X          if (status == 0)
  925. X            {
  926. X              unsigned int
  927. X                retain_colors;
  928. X
  929. X              /*
  930. X                Create a new colormap.
  931. X              */
  932. X              colormap=XCreateColormap(display,root_window,visual_info->visual,
  933. X                AllocNone);
  934. X              retain_colors=visual_info->colormap_size-image->colors;
  935. X              if (retain_colors > 0)
  936. X                {
  937. X                  /*
  938. X                    Not enough colormap entries in the default colormap;
  939. X                    create a new colormap and retain as many colors from the
  940. X                    default colormap as possible.
  941. X                  */
  942. X                  if (retain_colors > 144)
  943. X                    retain_colors=144;
  944. X                  for (i=0; i < retain_colors; i++)
  945. X                    colors[i].pixel=i;
  946. X                  XQueryColors(display,XDefaultColormap(display,
  947. X                    visual_info->screen),colors,retain_colors);
  948. X                  XAllocColorCells(display,colormap,False,
  949. X                    (unsigned long *) NULL,0,pixel_info->pixels,retain_colors);
  950. X                  XStoreColors(display,colormap,colors,retain_colors);
  951. X                }
  952. X              XAllocColorCells(display,colormap,False,(unsigned long *) NULL,0,
  953. X                pixel_info->pixels,image->colors);
  954. X            }
  955. X          /*
  956. X            Store the image colormap.
  957. X          */
  958. X          if (visual_info->class != GrayScale)
  959. X            for (i=0; i < image->colors; i++)
  960. X            {
  961. X              colors[i].flags=DoRed | DoGreen | DoBlue;
  962. X              colors[i].red=(unsigned short) (image->colormap[i].red << 8);
  963. X              colors[i].green=(unsigned short) (image->colormap[i].green << 8);
  964. X              colors[i].blue=(unsigned short) (image->colormap[i].blue << 8);
  965. X              colors[i].pixel=pixel_info->pixels[i];
  966. X            }
  967. X          else
  968. X            {
  969. X              register unsigned char
  970. X                gray_value;
  971. X
  972. X              /*
  973. X                Convert PseudoClass packets to grayscale.
  974. X              */
  975. X              for (i=0; i < image->colors; i++)
  976. X              {
  977. X                colors[i].flags=DoRed | DoGreen | DoBlue;
  978. X                gray_value=Intensity(image->colormap[i]);
  979. X                colors[i].red=(unsigned short) (gray_value << 8);
  980. X                colors[i].green=(unsigned short) (gray_value << 8);
  981. X                colors[i].blue=(unsigned short) (gray_value << 8);
  982. X                colors[i].pixel=pixel_info->pixels[i];
  983. X              }
  984. X            }
  985. X          XStoreColors(display,colormap,colors,image->colors);
  986. X          if (image->colors == visual_info->colormap_size)
  987. X            {
  988. X              /*
  989. X                Find closest color for background/border/foreground pixels.
  990. X              */
  991. X              XParseColor(display,colormap,background_color,&color);
  992. X              pixel_info->background_pixel=
  993. X                XBestPixel(colors,image->colors,color);
  994. X              XParseColor(display,colormap,resource_info->border_color,&color);
  995. X              pixel_info->border_pixel=XBestPixel(colors,image->colors,color);
  996. X              XParseColor(display,colormap,foreground_color,&color);
  997. X              pixel_info->foreground_pixel=
  998. X                XBestPixel(colors,image->colors,color);
  999. X            }
  1000. X          (void) free((char *) colors);
  1001. X          break;
  1002. X        }
  1003. X      }
  1004. X      /*
  1005. X        Final check for background/border/foreground pixels.
  1006. X      */
  1007. X      XParseColor(display,colormap,background_color,&color);
  1008. X      if (XAllocColor(display,colormap,&color) != 0)
  1009. X        pixel_info->background_pixel=color.pixel;
  1010. X      XParseColor(display,colormap,resource_info->border_color,&color);
  1011. X      if (XAllocColor(display,colormap,&color) != 0)
  1012. X        pixel_info->border_pixel=color.pixel;
  1013. X      XParseColor(display,colormap,foreground_color,&color);
  1014. X      if (XAllocColor(display,colormap,&color) != 0)
  1015. X        pixel_info->foreground_pixel=color.pixel;
  1016. X    }
  1017. X  else
  1018. X    {
  1019. X      unsigned long
  1020. X        shift;
  1021. X
  1022. X      /*
  1023. X        Initialize the Standard Colormap attributes.
  1024. X      */
  1025. X      shift=0;
  1026. X      map_info->red_max=visual_info->red_mask;
  1027. X      if (map_info->red_max > 0)
  1028. X        while ((map_info->red_max & 0x01) == 0)
  1029. X        {
  1030. X          shift++;
  1031. X          map_info->red_max>>=1;
  1032. X        }
  1033. X      map_info->red_mult=1 << shift;
  1034. X      shift=0;
  1035. X      map_info->green_max=visual_info->green_mask;
  1036. X      if (map_info->green_max > 0)
  1037. X        while ((map_info->green_max & 0x01) == 0)
  1038. X        {
  1039. X          shift++;
  1040. X          map_info->green_max>>=1;
  1041. X        }
  1042. X      map_info->green_mult=1 << shift;
  1043. X      shift=0;
  1044. X      map_info->blue_max=visual_info->blue_mask;
  1045. X      if (map_info->blue_max > 0)
  1046. X        while ((map_info->blue_max & 0x01) == 0)
  1047. X        {
  1048. X          shift++;
  1049. X          map_info->blue_max>>=1;
  1050. X        }
  1051. X      map_info->blue_mult=1 << shift;
  1052. X      switch (visual_info->class)
  1053. X      {
  1054. X        case TrueColor:
  1055. X        {
  1056. X          /*
  1057. X            TrueColor.
  1058. X          */
  1059. X          break;
  1060. X        }
  1061. X        case DirectColor:
  1062. X        {
  1063. X          unsigned int
  1064. X            linear_colormap,
  1065. X            number_colors;
  1066. X
  1067. X          unsigned long
  1068. X            blue_max,
  1069. X            green_max,
  1070. X            red_max;
  1071. X
  1072. X          XColor
  1073. X            *colors;
  1074. X
  1075. X          /*
  1076. X            Determine the number of colors this server supports.
  1077. X          */
  1078. X          linear_colormap=
  1079. X            ((map_info->red_max+1) == visual_info->colormap_size) &&
  1080. X            ((map_info->green_max+1) == visual_info->colormap_size) &&
  1081. X            ((map_info->blue_max+1) == visual_info->colormap_size);
  1082. X          if (linear_colormap)
  1083. X            number_colors=visual_info->colormap_size;
  1084. X          else
  1085. X            number_colors=(map_info->red_max*map_info->red_mult)+
  1086. X              (map_info->green_max*map_info->green_mult)+
  1087. X              (map_info->blue_max*map_info->blue_mult)+1;
  1088. X          /*
  1089. X            Allocate color array.
  1090. X          */
  1091. X          colors=(XColor *) malloc((unsigned int) number_colors*sizeof(XColor));
  1092. X          if (colors == (XColor *) NULL)
  1093. X            Error("unable to create colormap","memory allocation failed");
  1094. X          /*
  1095. X            Initialize linear color ramp.
  1096. X          */
  1097. X          red_max=map_info->red_max;
  1098. X          if (red_max == 0)
  1099. X            red_max=1;
  1100. X          green_max=map_info->green_max;
  1101. X          if (green_max == 0)
  1102. X            green_max=1;
  1103. X          blue_max=map_info->blue_max;
  1104. X          if (blue_max == 0)
  1105. X            blue_max=1;
  1106. X          if (linear_colormap)
  1107. X            for (i=0; i < number_colors; i++)
  1108. X            {
  1109. X              colors[i].flags=DoRed | DoGreen | DoBlue;
  1110. X              colors[i].blue=(unsigned short)
  1111. X                (((i % map_info->green_mult)*65535)/blue_max);
  1112. X              colors[i].red=colors[i].blue;
  1113. X              colors[i].green=colors[i].blue;
  1114. X              colors[i].pixel=XStandardPixel(map_info,colors[i],16);
  1115. X            }
  1116. X          else
  1117. X            for (i=0; i < number_colors; i++)
  1118. X            {
  1119. X              colors[i].flags=DoRed | DoGreen | DoBlue;
  1120. X              colors[i].red=(unsigned short)
  1121. X                (((i/map_info->red_mult)*65535)/red_max);
  1122. X              colors[i].green=(unsigned short) ((((i/map_info->green_mult) %
  1123. X                (map_info->green_max+1))*65535)/green_max);
  1124. X              colors[i].blue=(unsigned short)
  1125. X                (((i % map_info->green_mult)*65535)/blue_max);
  1126. X              colors[i].pixel=XStandardPixel(map_info,colors[i],16);
  1127. X            }
  1128. X          XStoreColors(display,colormap,colors,number_colors);
  1129. X          (void) free((char *) colors);
  1130. X          break;
  1131. X        }
  1132. X        default:
  1133. X          break;
  1134. X      }
  1135. X      if (image->class == PseudoClass)
  1136. X        {
  1137. X          /*
  1138. X            Initialize pixel array for images of type PseudoClass.
  1139. X          */
  1140. X          pixel_info->pixels=(unsigned long *)
  1141. X            malloc((unsigned int) image->colors*sizeof(unsigned long));
  1142. X          if (pixel_info->pixels == (unsigned long *) NULL)
  1143. X            Error("unable to create colormap","memory allocation failed");
  1144. X          for (i=0; i < image->colors; i++)
  1145. X            pixel_info->pixels[i]=
  1146. X              XStandardPixel(map_info,image->colormap[i],8);
  1147. X        }
  1148. X      /*
  1149. X        Define background/border/foreground pixels.
  1150. X      */
  1151. X      XParseColor(display,colormap,background_color,&color);
  1152. X      pixel_info->background_pixel=XStandardPixel(map_info,color,16);
  1153. X      XParseColor(display,colormap,resource_info->border_color,&color);
  1154. X      pixel_info->border_pixel=XStandardPixel(map_info,color,16);
  1155. X      XParseColor(display,colormap,foreground_color,&color);
  1156. X      pixel_info->foreground_pixel=XStandardPixel(map_info,color,16);
  1157. X    }
  1158. X  map_info->colormap=colormap;
  1159. X  return(map_info);
  1160. }
  1161. X
  1162. /*
  1163. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1164. %                                                                             %
  1165. %                                                                             %
  1166. %                                                                             %
  1167. %   X M a k e W i n d o w                                                     %
  1168. %                                                                             %
  1169. %                                                                             %
  1170. %                                                                             %
  1171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1172. %
  1173. %  Function XMakeWindow creates an X11 window.
  1174. %
  1175. %  The format of the XMakeWindow routine is:
  1176. %
  1177. %      XMakeWindow(display,parent,argv,argc,class_hint,manager_hints,property,
  1178. %        window_info)
  1179. %
  1180. %  A description of each parameter follows:
  1181. %
  1182. %    o display: Specifies a connection to an X server; returned from
  1183. %      XOpenDisplay.
  1184. %
  1185. %    o parent: Specifies the parent window.
  1186. %
  1187. %    o argv: Specifies the application's argument list.
  1188. %
  1189. %    o argc: Specifies the number of arguments.
  1190. %
  1191. %    o class_hint: Specifies a pointer to a X11 XClassHint structure.
  1192. %
  1193. %    o manager_hints: Specifies a pointer to a X11 XWMHints structure.
  1194. %
  1195. %    o property: A property to define on the window.
  1196. %
  1197. %    o window_info: Specifies a pointer to a X11 XWindowInfo structure.
  1198. %
  1199. %
  1200. */
  1201. void XMakeWindow(display,parent,argv,argc,class_hint,manager_hints,property,
  1202. X  window_info)
  1203. Display
  1204. X  *display;
  1205. X
  1206. Window
  1207. X  parent;
  1208. X
  1209. char
  1210. X  **argv;
  1211. X
  1212. int
  1213. X  argc;
  1214. X
  1215. XXClassHint
  1216. X  *class_hint;
  1217. X
  1218. XXWMHints
  1219. X  *manager_hints;
  1220. X
  1221. Atom
  1222. X  property;
  1223. X
  1224. XXWindowInfo
  1225. X  *window_info;
  1226. {
  1227. #define MinWindowSize  64
  1228. X
  1229. X  XSizeHints
  1230. X    *size_hints;
  1231. X
  1232. X  XTextProperty
  1233. X    name;
  1234. X
  1235. X  /*
  1236. X    Set window hints.
  1237. X  */
  1238. X  size_hints=XAllocSizeHints();
  1239. X  if (size_hints == (XSizeHints *) NULL)
  1240. X    Error("unable to make window","memory allocation failed");
  1241. X  size_hints->flags=window_info->flags;
  1242. X  size_hints->x=window_info->x;
  1243. X  size_hints->y=window_info->y;
  1244. X  size_hints->width=window_info->width;
  1245. X  size_hints->height=window_info->height;
  1246. X  size_hints->flags|=PMinSize | PMaxSize;
  1247. X  if (!window_info->immutable)
  1248. X    {
  1249. X      /*
  1250. X        Window size can be changed.
  1251. X      */
  1252. X      size_hints->min_width=window_info->min_width;
  1253. X      size_hints->min_height=window_info->min_height;
  1254. X      size_hints->max_width=XDisplayWidth(display,window_info->screen);
  1255. X      size_hints->max_height=XDisplayHeight(display,window_info->screen);
  1256. X    }
  1257. X  else
  1258. X    {
  1259. X      /*
  1260. X        Window size cannot be changed.
  1261. X      */
  1262. X      size_hints->min_width=window_info->width;
  1263. X      size_hints->min_height=window_info->height;
  1264. X      size_hints->max_width=window_info->width;
  1265. X      size_hints->max_height=window_info->height;
  1266. X    }
  1267. X  size_hints->flags|=PResizeInc;
  1268. X  size_hints->width_inc=window_info->width_inc;
  1269. X  size_hints->height_inc=window_info->height_inc;
  1270. #ifndef PRE_R4_ICCCM
  1271. X  size_hints->flags|=PBaseSize;
  1272. X  size_hints->base_width=size_hints->min_width;
  1273. X  size_hints->base_height=size_hints->min_height;
  1274. #endif
  1275. X  if (window_info->geometry != (char *) NULL)
  1276. X    {
  1277. X      char
  1278. X        default_geometry[256];
  1279. X
  1280. X      int
  1281. X        flags,
  1282. X        gravity;
  1283. X
  1284. X      /*
  1285. X        User specified geometry.
  1286. X      */
  1287. X      (void) sprintf(default_geometry,"%dx%d\0",size_hints->width,
  1288. X        size_hints->height);
  1289. X      flags=XWMGeometry(display,window_info->screen,window_info->geometry,
  1290. X        default_geometry,window_info->border_width,size_hints,&size_hints->x,
  1291. X        &size_hints->y,&size_hints->width,&size_hints->height,&gravity);
  1292. X      window_info->x=size_hints->x;
  1293. X      window_info->y=size_hints->y;
  1294. X      if ((flags & WidthValue) && (flags & HeightValue))
  1295. X        size_hints->flags|=USSize;
  1296. X      if ((flags & XValue) && (flags & YValue))
  1297. X        size_hints->flags|=USPosition;
  1298. #ifndef PRE_R4_ICCCM
  1299. X      size_hints->win_gravity=gravity;
  1300. X      size_hints->flags|=PWinGravity;
  1301. #endif
  1302. X    }
  1303. X  window_info->id=XCreateWindow(display,parent,window_info->x,window_info->y,
  1304. X    window_info->width,window_info->height,window_info->border_width,
  1305. X    window_info->depth,InputOutput,window_info->visual_info->visual,
  1306. X    CWBackingStore | CWBackPixel | CWBackPixmap | CWBitGravity |
  1307. X    CWBorderPixel | CWColormap | CWCursor | CWDontPropagate | CWEventMask |
  1308. X    CWOverrideRedirect | CWSaveUnder | CWWinGravity,&window_info->attributes);
  1309. X  if (window_info->id == (Window) NULL)
  1310. X    Error("unable to create window",window_info->name);
  1311. X  name.value=(unsigned char *) window_info->name;
  1312. X  name.encoding=XA_STRING;
  1313. X  name.format=8;
  1314. X  name.nitems=strlen(window_info->name);
  1315. X  if (window_info->icon_geometry != (char *) NULL)
  1316. X    {
  1317. X      int
  1318. X        flags,
  1319. X        gravity,
  1320. X        height,
  1321. X        width;
  1322. X
  1323. X      /*
  1324. X        User specified icon geometry.
  1325. X      */
  1326. X      size_hints->flags|=USPosition;
  1327. X      flags=XWMGeometry(display,window_info->screen,window_info->icon_geometry,
  1328. X        (char *) NULL,0,size_hints,&manager_hints->icon_x,
  1329. X        &manager_hints->icon_y,&width,&height,&gravity);
  1330. X      if ((flags & XValue) && (flags & YValue))
  1331. X        manager_hints->flags|=IconPositionHint;
  1332. X    }
  1333. X  XSetWMProperties(display,window_info->id,&name,&name,argv,argc,size_hints,
  1334. X    manager_hints,class_hint);
  1335. X  XSetWMProtocols(display,window_info->id,&property,1);
  1336. X  XFree((char *) size_hints);
  1337. }
  1338. X
  1339. /*
  1340. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1341. %                                                                             %
  1342. %                                                                             %
  1343. %                                                                             %
  1344. %   X R e a d C o l o r m a p                                                 %
  1345. %                                                                             %
  1346. %                                                                             %
  1347. %                                                                             %
  1348. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1349. %
  1350. %  Function XReadColormap returns the red, green, and blue colormap of a window.
  1351. %  Additionally, the number of colors in the colormap is returned.
  1352. %
  1353. %  The format of the XReadColormap function is:
  1354. %
  1355. %      number_colors=XReadColormap(display,window_attributes,colors)
  1356. %
  1357. %  A description of each parameter follows:
  1358. %
  1359. %    o number_colors:XReadColormap returns the number of colors in the
  1360. %      colormap.
  1361. %
  1362. %    o display:Specifies a pointer to the Display structure;  returned from
  1363. %      XOpenDisplay.
  1364. %
  1365. %    o window_attributes:Specifies a pointer to the window attributes
  1366. %      structure;  returned from XGetWindowAttributes.
  1367. %
  1368. %    o colors:Specifies a an array of XColor structures.  The colormap
  1369. %      red, green, and blue are returned.
  1370. %
  1371. %
  1372. */
  1373. int XReadColormap(display,window_attributes,colors)
  1374. Display
  1375. X  *display;
  1376. X
  1377. XXWindowAttributes
  1378. X  *window_attributes;
  1379. X
  1380. XXColor
  1381. X  **colors;
  1382. {
  1383. X  int
  1384. X    number_colors;
  1385. X
  1386. X  register int
  1387. X    i;
  1388. X
  1389. X  if (window_attributes->colormap == (Colormap) NULL)
  1390. X    return(0);
  1391. X  number_colors=window_attributes->visual->map_entries;
  1392. X  *colors=(XColor *) malloc((unsigned) (number_colors*sizeof(XColor)));
  1393. X  if (*colors == (XColor *) NULL)
  1394. X    {
  1395. X      Warning("unable to read colormap","memory allocation failed");
  1396. X      return(0);
  1397. X    }
  1398. X  for (i=0; i < number_colors; i++)
  1399. X    if ((window_attributes->visual->class != DirectColor) &&
  1400. X        (window_attributes->visual->class != TrueColor))
  1401. X      for (i=0; i < number_colors; i++)
  1402. X      {
  1403. X        (*colors)[i].pixel=i;
  1404. X        (*colors)[i].pad=0;
  1405. X      }
  1406. X    else
  1407. X      {
  1408. X        unsigned long
  1409. X          blue,
  1410. X          blue_bit,
  1411. X          green,
  1412. X          green_bit,
  1413. X          red,
  1414. X          red_bit;
  1415. X
  1416. X        /*
  1417. X          DirectColor or TrueColor visual.
  1418. X        */
  1419. X        red=0;
  1420. X        green=0;
  1421. X        blue=0;
  1422. X        red_bit=window_attributes->visual->red_mask &
  1423. X          (~(window_attributes->visual->red_mask)+1);
  1424. X        green_bit=window_attributes->visual->green_mask &
  1425. X          (~(window_attributes->visual->green_mask)+1);
  1426. X        blue_bit=window_attributes->visual->blue_mask &
  1427. X          (~(window_attributes->visual->blue_mask)+1);
  1428. X        for (i=0; i < number_colors; i++)
  1429. X        {
  1430. X          (*colors)[i].pixel=red | green | blue;
  1431. X          (*colors)[i].pad=0;
  1432. X          red+=red_bit;
  1433. X          if (red > window_attributes->visual->red_mask)
  1434. X            red=0;
  1435. X          green+=green_bit;
  1436. X          if (green > window_attributes->visual->green_mask)
  1437. X            green=0;
  1438. X          blue+=blue_bit;
  1439. X          if (blue > window_attributes->visual->blue_mask)
  1440. X            blue=0;
  1441. X        }
  1442. X      }
  1443. X  XQueryColors(display,window_attributes->colormap,*colors,number_colors);
  1444. X  return(number_colors);
  1445. }
  1446. X
  1447. /*
  1448. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1449. %                                                                             %
  1450. %                                                                             %
  1451. %                                                                             %
  1452. %   X R e a d I m a g e                                                       %
  1453. %                                                                             %
  1454. %                                                                             %
  1455. %                                                                             %
  1456. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1457. %
  1458. %  Procedure XReadImage reads an image from an X window.
  1459. %
  1460. %  The format of the XReadImage routine is:
  1461. %
  1462. %      image=XReadImage(display,root_window,target_window,client_window,
  1463. %        screen,borders)
  1464. %
  1465. %  A description of each parameter follows:
  1466. %
  1467. %    o image:specifies a pointer to the Image structure.
  1468. %
  1469. %    o display:Specifies a pointer to the Display structure;  returned from
  1470. %      XOpenDisplay.
  1471. %
  1472. %    o root_window:Specifies the id of the root window.
  1473. %
  1474. %    o target_window:Specifies the id of the target window.
  1475. %
  1476. %    o client_window:Specifies the id of the client of the target window.
  1477. %
  1478. %    o borders:Specifies whether borders pixels are to be saved with
  1479. %      the image.
  1480. %
  1481. %
  1482. */
  1483. Image *XReadImage(display,root_window,target_window,client_window,screen,
  1484. X  borders)
  1485. Display
  1486. X  *display;
  1487. X
  1488. Window
  1489. X  root_window,
  1490. X  target_window,
  1491. X  client_window;
  1492. X
  1493. unsigned int
  1494. X  screen,
  1495. X  borders;
  1496. {
  1497. X  Image
  1498. X    *image;
  1499. X
  1500. X  int
  1501. X    display_width,
  1502. X    display_height,
  1503. X    x,
  1504. X    y;
  1505. X
  1506. X  register int
  1507. X    i;
  1508. X
  1509. X  register RunlengthPacket
  1510. X    *p;
  1511. X
  1512. X  register unsigned long
  1513. X    pixel;
  1514. X
  1515. X  unsigned char
  1516. X    blue,
  1517. X    green,
  1518. X    red;
  1519. X
  1520. X  Window
  1521. X    child;
  1522. X
  1523. X  XColor
  1524. X    *colors;
  1525. X
  1526. X  XImage
  1527. X    *ximage;
  1528. X
  1529. X  XTextProperty
  1530. X    window_name;
  1531. X
  1532. X  XWindowAttributes
  1533. X    client_attributes,
  1534. X    target_attributes;
  1535. X
  1536. X  /*
  1537. X    Inform the user not to alter the screen.
  1538. X  */
  1539. X  XBell(display,0);
  1540. X  /*
  1541. X    Get the attributes of the window being dumped.
  1542. X  */
  1543. X  if ((XGetWindowAttributes(display,target_window,&target_attributes) == 0) ||
  1544. X      (XGetWindowAttributes(display,client_window,&client_attributes) == 0))
  1545. X    {
  1546. X      Warning("unable to get target window attributes",(char *) NULL);
  1547. X      return((Image *) NULL);
  1548. X    }
  1549. X  XTranslateCoordinates(display,target_window,root_window,0,0,&x,&y,&child);
  1550. X  target_attributes.x=x;
  1551. X  target_attributes.y=y;
  1552. X  /*
  1553. X    Allocate image structure.
  1554. X  */
  1555. X  image=(Image *) malloc(sizeof(Image));
  1556. X  if (image == (Image *) NULL)
  1557. X    {
  1558. X      Warning("unable to read image","memory allocation failed");
  1559. X      return((Image *) NULL);
  1560. X    }
  1561. X  /*
  1562. X    Initialize Image structure.
  1563. X  */
  1564. X  image->id=UnknownId;
  1565. X  image->class=DirectClass;
  1566. X  image->compression=NoCompression;
  1567. X  image->columns=target_attributes.width;
  1568. X  image->rows=target_attributes.height;
  1569. X  image->packets=0;
  1570. X  image->colors=0;
  1571. X  image->scene=0;
  1572. X  image->colormap=(ColorPacket *) NULL;
  1573. X  image->pixels=(RunlengthPacket *) NULL;
  1574. X  image->comments=(char *) NULL;
  1575. X  if (borders)
  1576. X    {
  1577. X      /*
  1578. X        Do not include border in image.
  1579. X      */
  1580. X      x-=target_attributes.border_width;
  1581. X      y-=target_attributes.border_width;
  1582. X      image->columns+=2*target_attributes.border_width;
  1583. X      image->rows+=2*target_attributes.border_width;
  1584. X    }
  1585. X  if (screen)
  1586. X  /*
  1587. X    clip to window
  1588. X  */
  1589. X  if (x < 0)
  1590. X    {
  1591. X      image->columns+=x;
  1592. X      x=0;
  1593. X    }
  1594. X  if (y < 0)
  1595. X    {
  1596. X      image->rows+=y;
  1597. X      y=0;
  1598. X    }
  1599. X  display_width=DisplayWidth(display,XDefaultScreen(display));
  1600. X  display_height=DisplayHeight(display,XDefaultScreen(display));
  1601. X  if ((x+(int) image->columns) > display_width)
  1602. X    image->columns=display_width-x;
  1603. X  if ((y+(int) image->rows) > display_height)
  1604. X    image->rows=display_height-y;
  1605. X  /*
  1606. X    Get image from window with XGetImage.
  1607. X  */
  1608. X  if (screen)
  1609. X    ximage=XGetImage(display,root_window,x,y,image->columns,image->rows,
  1610. X      AllPlanes,ZPixmap);
  1611. X  else
  1612. X    {
  1613. X      x-=target_attributes.x;
  1614. X      y-=target_attributes.y;
  1615. X      ximage=XGetImage(display,target_window,x,y,image->columns,image->rows,
  1616. X        AllPlanes,ZPixmap);
  1617. X    }
  1618. X  if (ximage == (XImage *) NULL)
  1619. X    {
  1620. X      Warning("unable to read image",(char *) NULL);
  1621. X      DestroyImage(image);
  1622. X      return((Image *) NULL);
  1623. X    }
  1624. X  /*
  1625. X    Obtain the window colormap from the client of the target window.
  1626. X  */
  1627. X  image->colors=XReadColormap(display,&client_attributes,&colors);
  1628. X  XBell(display,0);
  1629. X  XBell(display,0);
  1630. X  XFlush(display);
  1631. X  /*
  1632. X    Convert image to MIFF format.
  1633. X  */
  1634. X  image->comments=(char *) NULL;
  1635. X  if (XGetWMName(display,target_window,&window_name) != 0)
  1636. X    {
  1637. X      /*
  1638. X        Initial image comment.
  1639. X      */
  1640. X      image->comments=(char *)
  1641. X        malloc((unsigned int) (strlen((char *) window_name.value)+256));
  1642. X      if (image->comments == (char *) NULL)
  1643. X        {
  1644. X          Warning("unable to read image","memory allocation failed");
  1645. X          DestroyImage(image);
  1646. X          return((Image *) NULL);
  1647. X        }
  1648. X      (void) sprintf(image->comments,"\n  Imported from X11 window:  %s\n\0",
  1649. X        window_name.value);
  1650. X    }
  1651. X  if ((target_attributes.visual->class == TrueColor) ||
  1652. X      (target_attributes.visual->class == DirectColor))
  1653. X    image->class=DirectClass;
  1654. X  else
  1655. X    image->class=PseudoClass;
  1656. X  image->compression=RunlengthEncodedCompression;
  1657. X  image->scene=0;
  1658. X  image->pixels=(RunlengthPacket *)
  1659. X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
  1660. X  if (image->pixels == (RunlengthPacket *) NULL)
  1661. X    {
  1662. X      Warning("unable to read image","memory allocation failed");
  1663. X      DestroyImage(image);
  1664. X      return((Image *) NULL);
  1665. X    }
  1666. X  image->packets=0;
  1667. X  p=image->pixels;
  1668. X  p->length=MaxRunlength;
  1669. X  switch (image->class)
  1670. X  {
  1671. X    case DirectClass:
  1672. X    {
  1673. X      register unsigned long
  1674. X        color,
  1675. X        index;
  1676. X
  1677. X      unsigned long
  1678. X        blue_mask,
  1679. X        blue_shift,
  1680. X        green_mask,
  1681. X        green_shift,
  1682. X        red_mask,
  1683. X        red_shift;
  1684. X
  1685. X      /*
  1686. X        Determine shift and mask for red, green, and blue.
  1687. X      */
  1688. X      red_mask=target_attributes.visual->red_mask;
  1689. X      red_shift=0;
  1690. X      while ((red_mask & 0x01) == 0)
  1691. X      {
  1692. X        red_mask>>=1;
  1693. X        red_shift++;
  1694. X      }
  1695. X      green_mask=target_attributes.visual->green_mask;
  1696. X      green_shift=0;
  1697. X      while ((green_mask & 0x01) == 0)
  1698. X      {
  1699. X        green_mask>>=1;
  1700. X        green_shift++;
  1701. X      }
  1702. X      blue_mask=target_attributes.visual->blue_mask;
  1703. X      blue_shift=0;
  1704. X      while ((blue_mask & 0x01) == 0)
  1705. X      {
  1706. X        blue_mask>>=1;
  1707. X        blue_shift++;
  1708. X      }
  1709. X      /*
  1710. X        Convert X image to DirectClass packets.
  1711. X      */
  1712. X      if ((image->colors > 0) &&
  1713. X          (target_attributes.visual->class == DirectColor))
  1714. X        for (y=0; y < image->rows; y++)
  1715. X        {
  1716. X          for (x=0; x < image->columns; x++)
  1717. X          {
  1718. X            pixel=XGetPixel(ximage,x,y);
  1719. X            index=(pixel >> red_shift) & red_mask;
  1720. X            red=(unsigned char) (colors[index].red >> 8);
  1721. X            index=(pixel >> green_shift) & green_mask;
  1722. X            green=(unsigned char) (colors[index].green >> 8);
  1723. X            index=(pixel >> blue_shift) & blue_mask;
  1724. X            blue=(unsigned char) (colors[index].blue >> 8);
  1725. X            if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
  1726. X                (p->length < MaxRunlength))
  1727. X                p->length++;
  1728. X              else
  1729. X                {
  1730. X                  if (image->packets > 0)
  1731. X                    p++;
  1732. X                  image->packets++;
  1733. X                  p->red=red;
  1734. X                  p->green=green;
  1735. X                  p->blue=blue;
  1736. X                  p->index=0;
  1737. X                  p->length=0;
  1738. X                }
  1739. X          }
  1740. X        }
  1741. X      else
  1742. X        for (y=0; y < image->rows; y++)
  1743. X          for (x=0; x < image->columns; x++)
  1744. X          {
  1745. X            pixel=XGetPixel(ximage,x,y);
  1746. X            color=(pixel >> red_shift) & red_mask;
  1747. X            red=(unsigned char)
  1748. X              ((((unsigned long) color*65535)/red_mask) >> 8);
  1749. X            color=(pixel >> green_shift) & green_mask;
  1750. X            green=(unsigned char)
  1751. X              ((((unsigned long) color*65535)/green_mask) >> 8);
  1752. X            color=(pixel >> blue_shift) & blue_mask;
  1753. X            blue=(unsigned char)
  1754. X              ((((unsigned long) color*65535)/blue_mask) >> 8);
  1755. X            if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
  1756. X                (p->length < MaxRunlength))
  1757. X                p->length++;
  1758. X              else
  1759. X                {
  1760. X                  if (image->packets > 0)
  1761. X                    p++;
  1762. X                  image->packets++;
  1763. X                  p->red=red;
  1764. X                  p->green=green;
  1765. X                  p->blue=blue;
  1766. X                  p->index=0;
  1767. X                  p->length=0;
  1768. X                }
  1769. X          }
  1770. X      break;
  1771. X    }
  1772. X    case PseudoClass:
  1773. X    {
  1774. X      /*
  1775. X        Convert X image to PseudoClass packets.
  1776. X      */
  1777. X      image->colormap=(ColorPacket *)
  1778. X        malloc((unsigned) (image->colors*sizeof(ColorPacket)));
  1779. X      if (image->colormap == (ColorPacket *) NULL)
  1780. X        {
  1781. X          Warning("unable to read image","memory allocation failed");
  1782. X          DestroyImage(image);
  1783. X          return((Image *) NULL);
  1784. X        }
  1785. X      for (i=0; i < image->colors; i++)
  1786. X      {
  1787. X        image->colormap[i].red=colors[i].red >> 8;
  1788. X        image->colormap[i].green=colors[i].green >> 8;
  1789. X        image->colormap[i].blue=colors[i].blue >> 8;
  1790. X      }
  1791. X      for (y=0; y < image->rows; y++)
  1792. X        for (x=0; x < image->columns; x++)
  1793. X        {
  1794. X          pixel=XGetPixel(ximage,x,y);
  1795. X          red=(unsigned char) (colors[pixel].red >> 8);
  1796. X          green=(unsigned char) (colors[pixel].green >> 8);
  1797. X          blue=(unsigned char) (colors[pixel].blue >> 8);
  1798. X          if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
  1799. X              (p->length < MaxRunlength))
  1800. X              p->length++;
  1801. X            else
  1802. X              {
  1803. X                if (image->packets > 0)
  1804. X                  p++;
  1805. X                image->packets++;
  1806. X                p->red=red;
  1807. X                p->green=green;
  1808. X                p->blue=blue;
  1809. X                p->index=(unsigned short) pixel;
  1810. X                p->length=0;
  1811. X              }
  1812. X        }
  1813. X      break;
  1814. X    }
  1815. X  }
  1816. X  /*
  1817. X    Free image and colormap.
  1818. X  */
  1819. X  if (image->colors > 0)
  1820. X    (void) free((char *) colors);
  1821. X  XDestroyImage(ximage);
  1822. X  if (image->packets > ((image->columns*image->rows*3) >> 2))
  1823. X    image->compression=NoCompression;
  1824. X  image->pixels=(RunlengthPacket *)
  1825. X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
  1826. X  return(image);
  1827. }
  1828. X
  1829. /*
  1830. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1831. %                                                                             %
  1832. %                                                                             %
  1833. %                                                                             %
  1834. %   X S e l e c t W i n d o w                                                 %
  1835. %                                                                             %
  1836. %                                                                             %
  1837. %                                                                             %
  1838. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1839. %
  1840. %  Function XSelectWindow allows a user to select a window using the mouse.
  1841. %
  1842. %  The format of the XSelectWindow function is:
  1843. %
  1844. %      target_window=XSelectWindow(display,root_window)
  1845. %
  1846. %  A description of each parameter follows:
  1847. %
  1848. %    o window:XSelectWindow returns the window id.
  1849. %
  1850. %    o display:Specifies a pointer to the Display structure;  returned from
  1851. %      XOpenDisplay.
  1852. %
  1853. %    o root_window:Specifies the window id of the root window.
  1854. %
  1855. %
  1856. */
  1857. Window XSelectWindow(display,root_window)
  1858. Display
  1859. X  *display;
  1860. X
  1861. Window
  1862. X  root_window;
  1863. {
  1864. X  Cursor
  1865. X    crosshair_cursor;
  1866. X
  1867. X  int
  1868. X    presses,
  1869. X    status;
  1870. X
  1871. X  Window
  1872. X    target_window;
  1873. X
  1874. X  XEvent
  1875. X    event;
  1876. X
  1877. X  /*
  1878. X    Make the target cursor.
  1879. X  */
  1880. X  crosshair_cursor=XCreateFontCursor(display,XC_crosshair);
  1881. X  /*
  1882. X    Grab the pointer using target cursor.
  1883. X  */
  1884. X  status=XGrabPointer(display,root_window,False,ButtonPressMask |
  1885. X    ButtonReleaseMask,GrabModeSync,GrabModeAsync,root_window,crosshair_cursor,
  1886. X    CurrentTime);
  1887. X  if (status != GrabSuccess)
  1888. X    Error("unable to grab the mouse",(char *) NULL);
  1889. X  /*
  1890. X    Select a window.
  1891. X  */
  1892. X  target_window=(Window) NULL;
  1893. X  presses=0;
  1894. X  do
  1895. X  {
  1896. X    /*
  1897. X      Allow another event.
  1898. X    */
  1899. X    XAllowEvents(display,SyncPointer,CurrentTime);
  1900. X    XWindowEvent(display,root_window,ButtonPressMask | ButtonReleaseMask,
  1901. X      &event);
  1902. X    switch (event.type)
  1903. X    {
  1904. X      case ButtonPress:
  1905. X      {
  1906. X        if (target_window == (Window) NULL)
  1907. X          {
  1908. X            target_window=event.xbutton.subwindow;
  1909. X            if (target_window == (Window) NULL)
  1910. X              target_window=root_window;
  1911. X          }
  1912. X        presses++;
  1913. X        break;
  1914. X      }
  1915. X      case ButtonRelease:
  1916. X      {
  1917. X        presses--;
  1918. X        break;
  1919. X      }
  1920. X      default:
  1921. X        break;
  1922. X    }
  1923. X  }
  1924. X  while ((target_window == (Window) NULL) || (presses > 0));
  1925. X  XUngrabPointer(display,CurrentTime);
  1926. X  XFreeCursor(display,crosshair_cursor);
  1927. X  return(target_window);
  1928. }
  1929. X
  1930. /*
  1931. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1932. %                                                                             %
  1933. %                                                                             %
  1934. %                                                                             %
  1935. %   X V i s u a l C l a s s N a m e                                           %
  1936. %                                                                             %
  1937. %                                                                             %
  1938. %                                                                             %
  1939. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1940. %
  1941. %  Function XVisualClassName returns the visual class name as a character
  1942. %  string.
  1943. %
  1944. %  The format of the XVisualClassName routine is:
  1945. %
  1946. %      visual_type=XVisualClassName(visual_info)
  1947. %
  1948. %  A description of each parameter follows:
  1949. %
  1950. %    o visual_type: XVisualClassName returns the visual class as a character
  1951. %      string.
  1952. %
  1953. %    o visual_info: Specifies a pointer to a X11 XVisualInfo structure;
  1954. %      returned from XGetVisualInfo.
  1955. %
  1956. %
  1957. */
  1958. char *XVisualClassName(visual_info)
  1959. XXVisualInfo
  1960. X  *visual_info;
  1961. {
  1962. X  if (visual_info == (XVisualInfo *) NULL)
  1963. X    return((char *) NULL);
  1964. X  switch (visual_info->class)
  1965. X  {
  1966. X    case StaticGray: return("StaticGray");
  1967. X    case GrayScale: return("GrayScale");
  1968. X    case StaticColor: return("StaticColor");
  1969. X    case PseudoColor: return("PseudoColor");
  1970. X    case TrueColor: return("TrueColor");
  1971. X    case DirectColor: return("DirectColor");
  1972. X  }
  1973. X  return("unknown visual class");
  1974. }
  1975. X
  1976. /*
  1977. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1978. %                                                                             %
  1979. %                                                                             %
  1980. %                                                                             %
  1981. %   X W i n d o w B y N a m e                                                 %
  1982. %                                                                             %
  1983. %                                                                             %
  1984. %                                                                             %
  1985. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1986. %
  1987. %  Function XWindowByName locates a window with a given name on a display.
  1988. %  If no window with the given name is found, 0 is returned. If more than
  1989. %  one window has the given name, the first one is returned.  Only root and
  1990. %  its children are searched.
  1991. %
  1992. %  The format of the XWindowByName function is:
  1993. %
  1994. %      window=XWindowByName(display,root_window,name)
  1995. %
  1996. %  A description of each parameter follows:
  1997. %
  1998. %    o window:XWindowByName returns the window id.
  1999. %
  2000. %    o display:Specifies a pointer to the Display structure;  returned from
  2001. %      XOpenDisplay.
  2002. %
  2003. %    o root_window:Specifies the id of the root window.
  2004. %
  2005. %    o name:Specifies the name of the window to locate.
  2006. %
  2007. %
  2008. */
  2009. Window XWindowByName(display,root_window,name)
  2010. Display
  2011. X  *display;
  2012. X
  2013. Window
  2014. X  root_window;
  2015. X
  2016. char
  2017. X  *name;
  2018. {
  2019. X  register int
  2020. X    i;
  2021. X
  2022. X  unsigned int
  2023. X    number_children;
  2024. X
  2025. X  Window
  2026. X    *children,
  2027. X    child,
  2028. X    window;
  2029. X
  2030. X  XTextProperty
  2031. X    window_name;
  2032. X
  2033. X  if (XGetWMName(display,root_window,&window_name) != 0)
  2034. X    if (strcmp((char *) window_name.value,name) == 0)
  2035. X      return(root_window);
  2036. X  if (!XQueryTree(display,root_window,&child,&child,&children,&number_children))
  2037. X    return(0);
  2038. X  window=0;
  2039. X  for (i=0; i < number_children; i++)
  2040. X  {
  2041. X    /*
  2042. X      Search each child and their children.
  2043. X    */
  2044. X    window=XWindowByName(display,children[i],name);
  2045. X    if (window != (Window) NULL)
  2046. X      break;
  2047. X  }
  2048. X  if (children != (Window *) NULL)
  2049. X    XFree((char *) children);
  2050. X  return(window);
  2051. }
  2052. X
  2053. /*
  2054. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2055. %                                                                             %
  2056. %                                                                             %
  2057. %                                                                             %
  2058. %   X W i n d o w B y P r o p e r y                                           %
  2059. %                                                                             %
  2060. %                                                                             %
  2061. %                                                                             %
  2062. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2063. %
  2064. %  Function XWindowByProperty locates a child window with a given property.
  2065. %  If no window with the given name is found, 0 is returned.  If more than
  2066. %  one window has the given property, the first one is returned.  Only the
  2067. %  window specified and its subwindows are searched.
  2068. %
  2069. %  The format of the XWindowByProperty function is:
  2070. %
  2071. %      child=XWindowByProperty(display,window,property)
  2072. %
  2073. %  A description of each parameter follows:
  2074. %
  2075. %    o child:XWindowByProperty returns the window id with the specified
  2076. %      property.  If no windows are found, XWindowByProperty returns 0.
  2077. %
  2078. %    o display:Specifies a pointer to the Display structure;  returned from
  2079. %      XOpenDisplay.
  2080. %
  2081. %    o property:Specifies the property of the window to locate.
  2082. %
  2083. %
  2084. */
  2085. Window XWindowByProperty(display,window,property)
  2086. Display
  2087. X  *display;
  2088. X
  2089. Window
  2090. X  window;
  2091. X
  2092. Atom
  2093. X  property;
  2094. {
  2095. X  Atom
  2096. X    type;
  2097. X
  2098. X  int
  2099. X    format;
  2100. X
  2101. X  unsigned char
  2102. X    *data;
  2103. X
  2104. X  unsigned int
  2105. X    i,
  2106. X    number_children;
  2107. X
  2108. X  unsigned long
  2109. X    after,
  2110. X    number_items;
  2111. X
  2112. X  Window
  2113. X    *children,
  2114. X    child,
  2115. X    parent,
  2116. X    root;
  2117. X
  2118. X  if (XQueryTree(display,window,&root,&parent,&children,&number_children) == 0)
  2119. X    return((Window) NULL);
  2120. X  type=(Atom) NULL;
  2121. X  child=(Window) NULL;
  2122. X  for (i=0; (i < number_children) && (child == (Window) NULL); i++)
  2123. X  {
  2124. X    (void) XGetWindowProperty(display,children[i],property,0L,0L,False,
  2125. X      AnyPropertyType,&type,&format,&number_items,&after,&data);
  2126. X    if (type != (Atom) NULL)
  2127. X      child=children[i];
  2128. X  }
  2129. X  for (i = 0; (i < number_children) && (child == (Window) NULL); i++)
  2130. X    child=XWindowByProperty(display,children[i],property);
  2131. X  if (children != (Window *) NULL)
  2132. X    XFree((char *) children);
  2133. X  return(child);
  2134. }
  2135. SHAR_EOF
  2136. echo 'File ImageMagick/X.c is complete' &&
  2137. chmod 0755 ImageMagick/X.c ||
  2138. echo 'restore of ImageMagick/X.c failed'
  2139. Wc_c="`wc -c < 'ImageMagick/X.c'`"
  2140. test 99539 -eq "$Wc_c" ||
  2141.     echo 'ImageMagick/X.c: original size 99539, current size' "$Wc_c"
  2142. rm -f _shar_wnt_.tmp
  2143. fi
  2144. # ============= ImageMagick/animate.h ==============
  2145. if test -f 'ImageMagick/animate.h' -a X"$1" != X"-c"; then
  2146.     echo 'x - skipping ImageMagick/animate.h (File already exists)'
  2147.     rm -f _shar_wnt_.tmp
  2148. else
  2149. > _shar_wnt_.tmp
  2150. echo 'x - extracting ImageMagick/animate.h (Text)'
  2151. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/animate.h' &&
  2152. X
  2153. /*
  2154. X  Define declarations.
  2155. */
  2156. #ifdef SYSV
  2157. #include <poll.h>
  2158. poll((struct poll *) 0, (size_t) 0, usec / 1000);
  2159. #define Delay(milliseconds)  \
  2160. X  poll((struct poll *) 0,(size_t) 0,milliseconds/1000);
  2161. #else
  2162. #ifdef vms
  2163. #define Delay(milliseconds)
  2164. #else
  2165. #define Delay(milliseconds)  \
  2166. {  \
  2167. X  struct timeval  \
  2168. X    timeout;  \
  2169. X  \
  2170. X  timeout.tv_usec=(milliseconds % 1000)*1000;  \
  2171. X  timeout.tv_sec=milliseconds/1000;  \
  2172. X  select(0,(void *) 0,(void *) 0,(void *) 0,&timeout);  \
  2173. }
  2174. #endif
  2175. #endif
  2176. SHAR_EOF
  2177. chmod 0755 ImageMagick/animate.h ||
  2178. echo 'restore of ImageMagick/animate.h failed'
  2179. Wc_c="`wc -c < 'ImageMagick/animate.h'`"
  2180. test 487 -eq "$Wc_c" ||
  2181.     echo 'ImageMagick/animate.h: original size 487, current size' "$Wc_c"
  2182. rm -f _shar_wnt_.tmp
  2183. fi
  2184. # ============= ImageMagick/animate.c ==============
  2185. if test -f 'ImageMagick/animate.c' -a X"$1" != X"-c"; then
  2186.     echo 'x - skipping ImageMagick/animate.c (File already exists)'
  2187.     rm -f _shar_wnt_.tmp
  2188. else
  2189. > _shar_wnt_.tmp
  2190. echo 'x - extracting ImageMagick/animate.c (Text)'
  2191. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/animate.c' &&
  2192. /*
  2193. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2194. %                                                                             %
  2195. %                                                                             %
  2196. %                                                                             %
  2197. %              AAA   N   N  IIIII  M   M   AAA   TTTTT  EEEEE                 %
  2198. %             A   A  NN  N    I    MM MM  A   A    T    E                     %
  2199. %             AAAAA  N N N    I    M M M  AAAAA    T    EEE                   %
  2200. %             A   A  N  NN    I    M   M  A   A    T    E                     %
  2201. %             A   A  N   N  IIIII  M   M  A   A    T    EEEEE                 %
  2202. %                                                                             %
  2203. %                                                                             %
  2204. %          Animate Machine Independent File Format Image via X11.             %
  2205. %                                                                             %
  2206. %                                                                             %
  2207. %                                                                             %
  2208. %                           Software Design                                   %
  2209. %                             John Cristy                                     %
  2210. %                            January  1991                                    %
  2211. %                                                                             %
  2212. %                                                                             %
  2213. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  2214. %                                                                             %
  2215. %  Permission to use, copy, modify, distribute, and sell this software and    %
  2216. %  its documentation for any purpose is hereby granted without fee,           %
  2217. %  provided that the above Copyright notice appear in all copies and that     %
  2218. %  both that Copyright notice and this permission notice appear in            %
  2219. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  2220. %  & Company not be used in advertising or publicity pertaining to            %
  2221. %  distribution of the software without specific, written prior               %
  2222. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  2223. %  about the suitability of this software for any purpose.  It is provided    %
  2224. %  "as is" without express or implied warranty.                               %
  2225. %                                                                             %
  2226. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  2227. %  to this software, including all implied warranties of merchantability      %
  2228. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  2229. %  liable for any special, indirect or consequential damages or any           %
  2230. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  2231. %  in an action of contract, negligence or other tortious action, arising     %
  2232. %  out of or in connection with the use or performance of this software.      %
  2233. %                                                                             %
  2234. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2235. %
  2236. %  Display is a machine architecture independent image processing
  2237. %  and display program.  It can display any image in the MIFF format on
  2238. %  any workstation display running X.  Display first determines the
  2239. %  hardware capabilities of the workstation.  If the number of unique
  2240. %  colors in the image is less than or equal to the number the workstation
  2241. %  can support, the image is displayed in an X window.  Otherwise the
  2242. %  number of colors in the image is first reduced to match the color
  2243. %  resolution of the workstation before it is displayed.
  2244. %
  2245. %  This means that a continuous-tone 24 bits/pixel image can display on a
  2246. %  8 bit pseudo-color device or monochrome device.  In most instances the
  2247. %  reduced color image closely resembles the original.  Alternatively, a
  2248. %  monochrome or pseudo-color image can display on a continuous-tone 24
  2249. %  bits/pixels device.
  2250. %
  2251. %  The Display program command syntax is:
  2252. %
  2253. %  Usage: display [options ...] file [ [options ...] file ...]
  2254. %
  2255. %  Where options include:
  2256. %    -backdrop            display image centered on a backdrop
  2257. %    -clip geometry       preferred size and location of the clipped image
  2258. %    -colors value        preferred number of colors in the image
  2259. %    -delay milliseconds  display the next image after pausing
  2260. %    -display server      display image to this X server
  2261. %    -dither              apply Floyd/Steinberg error diffusion to image
  2262. %    -gamma value         level of gamma correction
  2263. %    -geometry geometry   preferred size and location of the image window
  2264. %    -gray                transform image to gray scale colors
  2265. %    -map type            display image using this Standard Colormap
  2266. %    -monochrome          transform image to black and white
  2267. %    -reflect             reverse image scanlines
  2268. %    -rotate degrees      apply Paeth rotation to the image
  2269. %    -scale geometry      preferred size factors of the image
  2270. %    -treedepth value     depth of the color classification tree
  2271. %    -verbose             print detailed information about the image
  2272. %    -visual type         display image using this visual type
  2273. %
  2274. %  In addition to those listed above, you can specify these standard X
  2275. %  resources as command line options:  -background, -bordercolor,
  2276. %  -borderwidth, -font, -foreground, -iconGeometry, -iconic, -name, or
  2277. %  -title.
  2278. %
  2279. %  Change '-' to '+' in any option above to reverse its effect.  For
  2280. %  example, specify +compress to store the image as uncompressed.
  2281. %
  2282. %  Specify 'file' as '-' for standard input or output.
  2283. %
  2284. %  Buttons
  2285. %    1    press and drag to select a command from a pop-up menu
  2286. %
  2287. %  Keys
  2288. %    0-9  press to change the level of delay
  2289. %    p    press to animate the sequence of images
  2290. %    s    press to display the next image in the sequence
  2291. %    .    press to continually display the sequence of images
  2292. %    a    press to automatically reverse the sequence of images
  2293. %    f    press to animate in the forward direction
  2294. %    r    press to animate in the reverse direction
  2295. %    i    press to display information about the image
  2296. %    q    press to discard all images and exit program
  2297. %
  2298. %
  2299. */
  2300. X
  2301. /*
  2302. X  Include declarations.
  2303. */
  2304. #include "display.h"
  2305. #include "image.h"
  2306. #include "X.h"
  2307. X
  2308. /*
  2309. X  Define declarations.
  2310. */
  2311. #define AutoReverseAnimationState 0x0001
  2312. #define ConfigureWindowState  0x0002
  2313. #define DefaultState  0x0004
  2314. #define ExitState  0x0008
  2315. #define ForwardAnimationState 0x0010
  2316. #define HighlightState  0x0020
  2317. #define InfoMappedState  0x0040
  2318. #define PlayAnimationState 0x0080
  2319. #define RepeatAnimationState 0x0100
  2320. #define StepAnimationState 0x0200
  2321. X
  2322. /*
  2323. X  Forward declarations.
  2324. */
  2325. static void
  2326. X  XMenuWindow();
  2327. X
  2328. /*
  2329. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2330. %                                                                             %
  2331. %                                                                             %
  2332. %                                                                             %
  2333. %   D e l a y                                                                 %
  2334. %                                                                             %
  2335. %                                                                             %
  2336. %                                                                             %
  2337. SHAR_EOF
  2338. true || echo 'restore of ImageMagick/animate.c failed'
  2339. fi
  2340. echo 'End of ImageMagick part 20'
  2341. echo 'File ImageMagick/animate.c is continued in part 21'
  2342. echo 21 > _shar_seq_.tmp
  2343. exit 0
  2344. --
  2345. Dan Heller
  2346. O'Reilly && Associates       Z-Code Software    Comp-sources-x:
  2347. Senior Writer                President          comp-sources-x@uunet.uu.net
  2348. argv@ora.com                 argv@zipcode.com
  2349.