home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / imagemagic / part19 < prev    next >
Text File  |  1991-05-23  |  75KB  |  2,176 lines

  1. Path: uunet!uunet!cs.utexas.edu!sun-barr!newstop!exodus!dupont.com!cristy
  2. From: cristy@dupont.com
  3. Newsgroups: comp.sources.x
  4. Subject: v13i035: ImageMagick - Graphics display programs, Part19/21
  5. Message-ID: <13949@exodus.Eng.Sun.COM>
  6. Date: 24 May 91 03:20:39 GMT
  7. References: <csx-13i017:imagemagic@uunet.UU.NET>
  8. Sender: news@exodus.Eng.Sun.COM
  9. Lines: 2164
  10. Approved: argv@sun.com
  11.  
  12. Submitted-by: cristy@dupont.com
  13. Posting-number: Volume 13, Issue 35
  14. Archive-name: imagemagic/part19
  15.  
  16. #!/bin/sh
  17. # this is img.19 (part 19 of ImageMagick)
  18. # do not concatenate these parts, unpack them in order with /bin/sh
  19. # file ImageMagick/X.h 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" != 19; 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.h'
  35. else
  36. echo 'x - continuing file ImageMagick/X.h'
  37. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/X.h' &&
  38. X
  39. X  unsigned int
  40. X    monochrome;
  41. X
  42. X  char
  43. X    *name;
  44. X
  45. X  char
  46. X    *title,
  47. X    *visual_type;
  48. } XResourceInfo;
  49. X
  50. typedef struct _XWindowInfo
  51. {
  52. X  Window
  53. X    id;
  54. X
  55. X  int
  56. X    screen,
  57. X    depth;
  58. X
  59. X  XVisualInfo
  60. X    *visual_info;
  61. X
  62. X  XStandardColormap
  63. X    *map_info;
  64. X
  65. X  XPixelInfo
  66. X    *pixel_info;
  67. X
  68. X  XFontStruct
  69. X    *font_info;
  70. X
  71. X  GC
  72. X    graphic_context;
  73. X
  74. X  Cursor
  75. X    cursor,
  76. X    busy_cursor;
  77. X
  78. X  char
  79. X    *name,
  80. X    *geometry,
  81. X    *icon_geometry,
  82. X    *clip_geometry;
  83. X
  84. X  unsigned long
  85. X    flags;
  86. X
  87. X  int
  88. X    x,
  89. X    y;
  90. X
  91. X  unsigned int
  92. X    width,
  93. X    height,
  94. X    min_width,
  95. X    min_height,
  96. X    width_inc,
  97. X    height_inc,
  98. X    border_width,
  99. X    immutable;
  100. X
  101. X  XImage
  102. X    *ximage;
  103. X
  104. X  Pixmap
  105. X    *pixmaps;
  106. X
  107. X  XSetWindowAttributes
  108. X    attributes;
  109. } XWindowInfo;
  110. X
  111. /*
  112. X  X utilities routines.
  113. */
  114. extern char 
  115. X  *XVisualClassName();
  116. X
  117. extern int
  118. X  IsTrue();
  119. X
  120. extern Image 
  121. X  *XReadImage();
  122. X
  123. extern int 
  124. X  IsTrue(),
  125. X  XReadColormap();
  126. X
  127. extern XStandardColormap 
  128. X  *XMakeStandardColormap();
  129. X
  130. extern unsigned long 
  131. X  XBestPixel();
  132. X
  133. extern XVisualInfo 
  134. X  *XBestVisualInfo();
  135. X
  136. extern void 
  137. X  Latin1Upper(),
  138. X  XBestIconSize(),
  139. X  XMakeImageLSBFirst(),
  140. X  XMakeImageMSBFirst(),
  141. X  XMakeWindow();
  142. X
  143. extern Window 
  144. X  XClientWindow(),
  145. X  XSelectWindow(),
  146. X  XWindowByName(),
  147. X  XWindowByProperty();
  148. X
  149. extern XFontStruct 
  150. X  *XBestFont();
  151. X
  152. extern XImage 
  153. X  *XMakeImage();
  154. X
  155. /*
  156. X  Invoke pre-X11R4 ICCCM routines if PWinGravity is not defined.
  157. */
  158. #ifndef PWinGravity
  159. #define PRE_R4_ICCCM
  160. #include "PreR4Icccm.h"
  161. #else
  162. #define XInductColormap(display,colormap)
  163. #define XUninductColormap(display,colormap)
  164. #endif
  165. SHAR_EOF
  166. echo 'File ImageMagick/X.h is complete' &&
  167. chmod 0755 ImageMagick/X.h ||
  168. echo 'restore of ImageMagick/X.h failed'
  169. Wc_c="`wc -c < 'ImageMagick/X.h'`"
  170. test 2573 -eq "$Wc_c" ||
  171.     echo 'ImageMagick/X.h: original size 2573, current size' "$Wc_c"
  172. rm -f _shar_wnt_.tmp
  173. fi
  174. # ============= ImageMagick/animate.man ==============
  175. if test -f 'ImageMagick/animate.man' -a X"$1" != X"-c"; then
  176.     echo 'x - skipping ImageMagick/animate.man (File already exists)'
  177.     rm -f _shar_wnt_.tmp
  178. else
  179. > _shar_wnt_.tmp
  180. echo 'x - extracting ImageMagick/animate.man (Text)'
  181. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/animate.man' &&
  182. .ad l
  183. .nh
  184. .TH ANIMATE 1 "1 January 1991" "X Version 11"
  185. .SH NAME
  186. animate - display a sequence of images on any workstation running X
  187. .SH SYNOPSIS
  188. .B "animate" [ \fIoptions\fP ...] \fIfile\fP
  189. [ [ \fIoptions\fP ...] \fIfile\fP ...]
  190. .SH DESCRIPTION
  191. \fIAnimate\fP displays a sequence of images in the MIFF format on
  192. any workstation display running an X server.  \fIAnimate\fP first
  193. determines the hardware capabilities of the workstation.  If the number
  194. of unique colors in an image is less than or equal to the number the
  195. workstation can support, the image is displayed in an X window.
  196. Otherwise the number of colors in the image is first reduced to match
  197. the color resolution of the workstation before it is displayed.
  198. .PP
  199. This means that a continuous-tone 24 bits/pixel image can display on a
  200. 8 bit pseudo-color device or monochrome device.  In most instances the
  201. reduced color image closely resembles the original.  Alternatively, a
  202. monochrome or pseudo-color image can display on a continuous-tone 24
  203. bits/pixels device.
  204. .PP
  205. To prevent colormap flashing, the use of a Standard Colormap or static
  206. visual is recommended.  Refer to the \fB-map\fP and \fB-visual\fP in
  207. \fBOPTIONS\fP.
  208. .SH EXAMPLES
  209. To animate a cockatoo image sequence while using the Standard Colormap
  210. "best", use:
  211. .PP
  212. X     xstdcmap -best
  213. X     animate -map best cockatoo.*
  214. .PP
  215. .PP
  216. To scale each image of a cockatoo sequence to exactly 640 pixels in
  217. width and 480 pixels in height and position the window at location
  218. (200,200), use:
  219. .PP
  220. X     animate -geometry 640x480\+200\+200 cockatoo.*
  221. .PP
  222. To animate an image of a cockatoo without a border centered on a
  223. backdrop, use:
  224. .PP
  225. X     animate -backdrop -borderwidth 0 cockatoo.*
  226. .SH OPTIONS
  227. .TP 5
  228. .B "-backdrop"
  229. display the image centered on a backdrop.
  230. X
  231. This backdrop covers the entire workstation screen and is useful for
  232. hiding other X window activity while viewing the image.   The color of
  233. the backdrop is specified as the background color.  Refer to \fBX
  234. RESOURCES\fP for details.
  235. .TP 5
  236. .B "-clip \fI<width>x<height>{\+-}<x offset>{\+-}<y offset>\fP"
  237. preferred size and location of the clipped image.  See \fBX(1)\fP for details 
  238. about the geometry specification.
  239. X
  240. Use clipping to apply image processing options, or display, only a
  241. particular area of an image.  
  242. .TP 5
  243. .B "-colors \fIvalue\fP"
  244. preferred number of colors in the image.  
  245. X
  246. The actual number of colors in the image may be less than your request,
  247. but never more.  Note, this is a color reduction option.  Images with
  248. less unique colors than specified with this option will remain unchanged.
  249. Refer to \fBCOLOR REDUCTION ALGORITHM\fP for more details.
  250. X
  251. Note, options \fB-dither\fP and \fB-treedepth\fP affect the color reduction
  252. algorithm.
  253. .TP 5
  254. .B "-delay \fImilliseconds\fP"
  255. display the next image after pausing.
  256. X
  257. This option is useful for regulating the display of the sequence of
  258. images.  \fImilliseconds\fP milliseconds must expire before the display of 
  259. the next image.  The default is to display each image without delay.
  260. .TP 5
  261. .B "-display \fIhost:display[.screen]\fP"
  262. specifies the X server to contact; see \fBX(1)\fP.
  263. .TP 5
  264. .B "-dither"
  265. apply Floyd/Steinberg error diffusion to the image.  
  266. X
  267. The basic strategy of dithering is to trade intensity resolution for 
  268. spatial resolution by averaging the intensities of several neighboring 
  269. pixels.  Images which suffer from severe contouring when reducing colors 
  270. can be improved with this option.
  271. X
  272. The \fB-colors\fP, \fB-gray\fP, or \fB-monochrome\fP option is required 
  273. for this option to take effect.
  274. .TP 5
  275. .B "-gamma \fIvalue\fP"
  276. level of gamma correction.  
  277. X
  278. The same color image displayed on two different workstations may look
  279. different due to differences in the display monitor.  Use gamma
  280. correction to adjust for this color difference.  Reasonable values
  281. extend from 0.8 to 2.3.  
  282. .TP 5
  283. .B "-geometry \fI<width>x<height>{\+-}<x offset>{\+-}<y offset>\fP"
  284. preferred size and location of the image window.  See \fBX(1)\fP for details 
  285. about the geometry specification.  By default, the window size is the image 
  286. size and the location is choosen by you when it is mapped.
  287. X
  288. If the specified image size is smaller than the actual image size, the
  289. image is first reduced to an integral of the specified image size with
  290. an antialias digital filter.  The image is then scaled to the exact
  291. specified image size with pixel replication.  If the specified image
  292. size is greater than the actual image size, the image is first enlarged
  293. to an integral of the specified image size with bilinear
  294. interpolation.  The image is then scaled to the exact specified image
  295. size with pixel replication.
  296. X
  297. When displaying an image on an X server, the x and y offset in the
  298. geometry specification is relative to the root window.  When printing
  299. an image, the x and y offset in the geometry specification is relative
  300. to a Postscript page.  See \fB-print\fP for more details.
  301. .TP 5
  302. .B "-gray"
  303. transform the image to gray scale colors.  
  304. .TP 5
  305. .B "-map \fItype\fP"
  306. display image using this Standard Colormap type.
  307. X
  308. Choose from these Standard Colormap types:
  309. X
  310. X    default
  311. X    best
  312. X    red
  313. X    green
  314. X    blue
  315. X    gray
  316. X
  317. The X server must support the Standard Colormap you choose, otherwise an 
  318. error occurs.  See \fBxcmap(1)\fP for one way of creating Standard Colormaps.
  319. .TP 5
  320. .B "-monochrome"
  321. transform the image to black and white.
  322. X
  323. Monochrome images can benefit from error diffusion.  Use \fB-dither\fP with
  324. this option to diffuse the error.
  325. .TP 5
  326. .B "-reflect"
  327. create a "mirror image" by reflecting the image scanlines.
  328. .TP 5
  329. .B "-rotate \fIdegrees\fP"
  330. apply Paeth image rotation to the image.
  331. .TP 5
  332. .B "-scale \fI<width factor>x<height factor>\fP"
  333. preferred size factors of the image.
  334. X
  335. This option behaves like \fB-geometry\fP except the width and height values
  336. are relative instead of absolute.  The image size is multiplied by the
  337. width and height factors to obtain the final image dimensions.  If only
  338. one factor is specified, both the width and height factors assume the
  339. value.
  340. X
  341. Factors may be fractional.  For example, a factor of 1.5 will increase the
  342. image size by one and one-half.
  343. .TP 5
  344. .B "-treedepth \fIvalue\fP"
  345. Normally, this integer value is zero or one.  A zero or one tells
  346. \fIAnimate\fP to choose a optimal tree depth for the color reduction
  347. algorithm.  
  348. X
  349. An optimal depth generally allows the best representation of the source
  350. image with the fastest computational speed and the least amount of
  351. memory.  However, the default depth is inappropriate for some images.
  352. To assure the best representation, try values between 2 and 8 for this
  353. parameter.  Refer to \fBCOLOR REDUCTION ALGORITHM\fP for more details.
  354. X
  355. The \fB-colors\fP, \fB-gray\fP, or \fB-monochrome\fP option is required
  356. for this option to take effect.
  357. .TP 5
  358. .B "-visual \fItype\fP"
  359. display image using this visual type.
  360. X
  361. Choose from these visual classes:
  362. X
  363. X    StaticGray
  364. X    GrayScale
  365. X    StaticColor
  366. X    PseudoColor
  367. X    TrueColor
  368. X    DirectColor
  369. X    default
  370. X    \fIvisual id\fP
  371. X
  372. The X server must support the visual you choose, otherwise an error occurs.
  373. If a visual is not specified, the visual class that can display the most 
  374. simultaneous colors on the default screen is choosen.
  375. .PP
  376. In addition to those listed above, you can specify these standard X
  377. resources as command line options:  -background, -bordercolor,
  378. -borderwidth,  -font, -foreground, -iconGeometry, -iconic, -name, or -title.  
  379. See \fBX RESOURCES\fP for details.
  380. .PP
  381. Any option you specify on the command line remains in effect until it is
  382. explicitly changed by specifying the option again with a different effect.
  383. For example, to animate two images, the first with 32 colors and the
  384. second with only 16 colors, use:
  385. .PP
  386. X     animate -colors 32 cockatoo.1 -colors 16 cockatoo.2
  387. .PP
  388. Change \fI-\fP to \fI\+\fP in any option above to reverse its effect.
  389. For example, specify \fB\+display\fP to apply image transformations
  390. without viewing them on the X server.  Or, specify \fB\+dither\fP to not
  391. apply error diffusion to an image.
  392. .PP
  393. Specify \fIfile\fP as \fI-\fP for standard input or output.  If \fIfile\fP 
  394. has the extension \fB.Z\fP, the file is decoded with \fBuncompress\fP.  
  395. .SH BUTTONS
  396. .TP 5
  397. .B "1"
  398. Press and drag to select a command from a pop-up menu.  Choose from 
  399. these commands:
  400. X
  401. X    Play
  402. X    Step
  403. X    Repeat
  404. X    Auto Reverse
  405. X    Forward
  406. X    Reverse
  407. X    Image Info
  408. X    Quit
  409. .SH KEYS
  410. .TP 5
  411. .B "0-9"
  412. Press to change the level of delay.  Refer to \fB-delay\fP for more 
  413. information.
  414. .TP 5
  415. .B "p"
  416. Press to animate the sequence of images.
  417. .TP 5
  418. .B "s"
  419. Press to display the next image in the sequence.
  420. .TP 5
  421. .B "."
  422. Press to continually display the sequence of images.
  423. .TP 5
  424. .B "a"
  425. Press to automatically reverse the sequence of images.
  426. .TP 5
  427. .B "f"
  428. Press to animate in the forward direction.
  429. .TP 5
  430. .B "r"
  431. Press to animate in the reverse direction.
  432. .TP 5
  433. .B "i"
  434. Press to display information about the image.  Press any key or button
  435. to erase the information.
  436. X
  437. This information is printed: image name;  image size; and the total
  438. number of unique colors in the image.
  439. .TP 5
  440. .B "q"
  441. Press to discard all images and exit program.
  442. .SH "X RESOURCES"
  443. \fIDisplay\fP options can appear on the command line or in your X
  444. resource file.  Options on the command line supercede values specified
  445. in your X resource file.  See \fBX(1)\fP for more information on X
  446. resources.
  447. X
  448. All \fIdisplay\fP options have a corresponding X resource.  In addition,
  449. the \fIdisplay\fP program uses the following X resources:
  450. .TP 5
  451. .B background (\fPclass\fB Background)
  452. Specifies the preferred color to use for the image window background.  The
  453. default is black.
  454. .TP 5
  455. .B borderColor (\fPclass\fB BorderColor)
  456. Specifies the preferred color to use for the image window border.  The
  457. default is white.
  458. .TP 5
  459. .B borderWidth (\fPclass\fB BorderWidth)
  460. Specifies the width in pixels of the image window border.  The default is 2.
  461. .TP 5
  462. .B font (\fPclass\fB Font)
  463. Specifies the name of the preferred font to use when displaying text in the
  464. within the image window.  The default is 9x15, fixed, or 5x8 determined by
  465. the image window size.
  466. .TP 5
  467. .B foreground (\fPclass\fB Foreground)
  468. Specifies the preferred color to use for text within the image window.  The
  469. default is white.
  470. .TP 5
  471. .B iconGeometry (\fPclass\fB IconGeometry)
  472. Specifies the preferred size and position of the application when
  473. iconified.  It is not necessarily obeyed by all window managers.
  474. .TP 5
  475. .B iconic (\fPclass\fB Iconic)
  476. This resource indicates that you would prefer that the application's
  477. windows initially not be visible as if the windows had be immediately
  478. iconified by you.  Window managers may choose not to honor the
  479. application's request.
  480. .TP 5
  481. .B name (\fPclass\fB Name)
  482. This resource specifies the name under which resources for the
  483. application should be found.  This resource is useful in shell aliases to
  484. distinguish between invocations of an application, without resorting to
  485. creating links to alter the executable file name.  The default is the
  486. application name.
  487. .TP 5
  488. .B title (\fPclass\fB Title)
  489. This resource specifies the title to be used for the image window.  This
  490. information is sometimes used by a window manager to provide some sort
  491. of header identifying the window.  The default is the image file name.
  492. .SH "COLOR REDUCTION ALGORITHM"
  493. .PP
  494. This section describes how \fIDisplay\fP performs color reduction in an
  495. image.  To fully understand this section, you should have a knowledge
  496. of basic imaging techniques and the tree data structure and terminology.
  497. .PP
  498. For purposes of color allocation, an image is a set of \fIn\fP pixels,
  499. where each pixel is a point in RGB space.  RGB space is a 3-dimensional
  500. vector space, and each pixel, \fIp\d\s-3i\s0\u\fP,  is defined by an
  501. ordered triple of red, green, and blue coordinates, (\fIr\d\s-3i\s0\u,
  502. g\d\s-3i\s0\u, b\d\s-3i\s0\u\fP).
  503. .PP
  504. Each primary color component (red, green, or blue) represents an
  505. intensity which varies linearly from 0 to a maximum value,
  506. \fIc\d\s-3max\s0\u\fP, which corresponds to full saturation of that
  507. color.  Color allocation is defined over a domain consisting of the
  508. cube in RGB space with opposite vertices at (0,0,0) and
  509. (\fIc\d\s-3max\s0\u,c\d\s-3max\s0\u,c\d\s-3max\s0\u\fP).  \fIDisplay\fP
  510. requires \fIc\d\s-3max\s0\u = 255\fP.
  511. .PP
  512. The algorithm maps this domain onto a tree in which each node
  513. represents a cube within that domain.  In the following discussion,
  514. these cubes are defined by the coordinate of two opposite vertices: The
  515. vertex nearest the origin in RGB space and the vertex farthest from the
  516. origin.
  517. .PP
  518. The tree's root node represents the the entire domain, (0,0,0) through
  519. (\fIc\d\s-3max\s0\u,c\d\s-3max\s0\u,c\d\s-3max\s0\u\fP).  Each lower level in
  520. the tree is generated by subdividing one node's cube into eight smaller
  521. cubes of equal size.  This corresponds to bisecting the parent cube
  522. with planes passing through the midpoints of each edge.
  523. .PP
  524. The basic algorithm operates in three phases:  \fBClassification,
  525. Reduction\fP, and \fBAssignment\fP.  \fBClassification\fP builds a
  526. color description tree for the image.  \fBReduction\fP collapses the
  527. tree until the number it represents, at most, is the number of colors
  528. desired in the output image.  \fBAssignment\fP defines the output
  529. image's color map and sets each pixel's color by reclassification in
  530. the reduced tree.
  531. .PP
  532. \fBClassification\fP begins by initializing a color description tree of
  533. sufficient depth to represent each possible input color in a leaf.
  534. However, it is impractical to generate a fully-formed color description
  535. tree in the classification phase for realistic values of
  536. \fIc\d\s-3max\s0\u\fP.  If color components in the input image are
  537. quantized to \fIk\fP-bit precision, so that \fIc\d\s-3max\s0\u =
  538. 2\u\s-3k\s0\d-1\fP, the tree would need \fIk\fP levels below the root
  539. node to allow representing each possible input color in a leaf.  This
  540. becomes prohibitive because the tree's total number of nodes is
  541. .PP
  542. X        \fI\s+6\(*S\u\s-9 k\d\di=1\s0 8k\fP\s0\u
  543. .PP
  544. A complete tree would require 19,173,961 nodes for \fIk = 8,
  545. c\d\s-3max\s0\u = 255\fP.  Therefore, to avoid building a fully
  546. populated tree, \fIDisplay\fP: (1) Initializes data structures for
  547. nodes only as they are needed; (2) Chooses a maximum depth for the tree
  548. as a function of the desired number of colors in the output image
  549. (currently \fIlog\d\s-34\s0\u(colormap size)\+2\fP).  A tree of this
  550. depth generally allows the best representation of the source image with
  551. the fastest computational speed and the least amount of memory.
  552. However, the default depth is inappropriate for some images.
  553. Therefore, the caller can request a specific tree depth.
  554. .PP
  555. For each pixel in the input image, classification scans downward from
  556. the root of the color description tree.  At each level of the tree, it
  557. identifies the single node which represents a cube in RGB space
  558. containing the pixel's color.  It updates the following data for each
  559. such node:
  560. .TP 5
  561. .B n\d\s-31\s0\u:  
  562. Number of pixels whose color is contained in the RGB cube which this
  563. node represents;
  564. .TP 5
  565. .B n\d\s-32\s0\u:  
  566. Number of pixels whose color is not represented in a node at lower
  567. depth in the tree;  initially,  \fIn\d\s-32\s0\u = 0\fP for all nodes
  568. except leaves of the tree.
  569. .TP 5
  570. .B S\d\s-3r\s0\u, S\d\s-3g\s0\u, S\d\s-3b\s0\u:
  571. Sums of the red, green, and blue component values for all pixels not
  572. classified at a lower depth.  The combination of these sums and
  573. \fIn\d\s-32\s0\u\fP will ultimately characterize the mean color of a
  574. set of pixels represented by this node.
  575. .PP
  576. \fBReduction\fP repeatedly prunes the tree until the number of nodes with
  577. \fIn\d\s-32\s0\u  > 0\fP is less than or equal to the maximum number of colors
  578. allowed in the output image.  On any given iteration over the tree, it
  579. selects those nodes whose \fIn\d\s-31\s0\u\fP count is minimal for pruning and
  580. merges their color statistics upward.  It uses a pruning threshold,
  581. \fIn\d\s-3p\s0\u\fP, to govern node selection as follows:
  582. .PP
  583. X  n\d\s-3p\s0\u = 0
  584. X  while number of nodes with (n\d\s-32\s0\u > 0) > required maximum number of colors
  585. X      prune all nodes such that n\d\s-31\s0\u <= n\d\s-3p\s0\u
  586. X      Set n\d\s-3p\s0\u  to minimum n\d\s-31\s0\u  in remaining nodes
  587. .PP
  588. When a node to be pruned has offspring, the pruning procedure invokes
  589. itself recursively in order to prune the tree from the leaves upward.
  590. The values of \fIn\d\s-32\s0\u  S\d\s-3r\s0\u, S\d\s-3g\s0\u,\fP  and
  591. \fIS\d\s-3b\s0\u\fP in a node being pruned are always added to the
  592. corresponding data in that node's parent.  This retains the pruned
  593. node's color characteristics for later averaging.
  594. .PP
  595. For each node,  \fIn\d\s-32\s0\u\fP pixels exist for which that node
  596. represents the smallest volume in RGB space containing those pixel's
  597. colors.  When \fIn\d\s-32\s0\u  > 0\fP the node will uniquely define a
  598. color in the output image.  At the beginning of reduction,
  599. \fIn\d\s-32\s0\u = 0\fP  for all nodes except the leaves of the tree
  600. which represent colors present in the input image.
  601. .PP
  602. The other pixel count, \fIn\d\s-31\s0\u\fP,  indicates the total
  603. number of colors within the cubic volume which the node represents.
  604. This includes \fIn\d\s-31\s0\u - n\d\s-32\s0\u\fP pixels whose colors
  605. should be defined by nodes at a lower level in the tree.
  606. .PP
  607. \fBAssignment\fP generates the output image from the pruned tree.  The
  608. output image consists of two parts:  (1)  A color map, which is an
  609. array of color descriptions (RGB triples) for each color present in the
  610. output image; (2)  A pixel array, which represents each pixel as an
  611. index into the color map array.
  612. .PP
  613. First, the assignment phase makes one pass over the pruned color
  614. description tree to establish the image's color map.  For each node
  615. with \fIn\d\s-32\s0\u > 0\fP, it divides \fIS\d\s-3r\s0\u,
  616. S\d\s-3g\s0\u\fP, and \fPS\d\s-3b\s0\u\fP by \fIn\d\s-32\s0\u\fP.  This
  617. produces the mean color of all pixels that classify no lower than this
  618. node.  Each of these colors becomes an entry in the color map.
  619. .PP
  620. Finally, the assignment phase reclassifies each pixel in the pruned
  621. tree to identify the deepest node containing the pixel's color.  The
  622. pixel's value in the pixel array becomes the index of this node's mean
  623. color in the color map.
  624. .SH "MEASURING COLOR REDUCTION ERROR"
  625. .PP
  626. Depending on the image, the color reduction error may be obvious or
  627. invisible.  Images with high spatial frequencies (such as hair or
  628. grass) will show error much less than pictures with large smoothly
  629. shaded areas (such as faces).  This is because the high-frequency
  630. contour edges introduced by the color reduction process are masked by
  631. the high frequencies in the image.
  632. .PP
  633. To measure the difference between the original and color reduced images
  634. (the total color reduction error), \fIDisplay\fP sums over all pixels
  635. in an image the distance squared in RGB space between each original
  636. pixel value and its color reduced value. \fIDisplay\fP prints several error 
  637. measurements including the mean error per pixel, the normalized mean error,
  638. and the normalized maximum error.
  639. .PP
  640. The normalized error measurement can be used to compare images.  In
  641. general, the closer the mean error is to zero the more the quantized
  642. image resembles the source image.  Ideally, the error should be
  643. perceptually-based, since the human eye is the final judge of
  644. quantization quality.
  645. .PP
  646. These errors are measured and printed when \fB-verbose\fP and \fB-colors\fI 
  647. are specified on the command line:
  648. .TP 5
  649. .B mean error per pixel:  
  650. is the mean error for any single pixel in the image.
  651. .TP 5
  652. .B normalized mean square error:  
  653. is the normalized mean square quantization error for any single pixel in the
  654. image.  
  655. X
  656. This distance measure is normalized to a range between 0 and 1.  It is
  657. independent of the range of red, green, and blue values in the image.
  658. .TP 5
  659. .B normalized maximum square error:  
  660. is the largest normalized square quantization error for any single
  661. pixel in the image.
  662. X
  663. This distance measure is normalized to a range between 0 and 1.  It is
  664. independent of the range of red, green, and blue values in the image.
  665. .SH "MIFF FILE FORMAT"
  666. .PP
  667. The Machine Independent File Format is described in this section.
  668. .PP
  669. A MIFF image file consist of two sections.  The first section is
  670. composed of keywords describing the image in text form.  The next
  671. section is the binary image data.  The two sections are separated by a
  672. \fB:\fP character immediately followed by a \fInewline\fP.  Generally,
  673. the first section has a \fIform-feed\fP and \fInewline\fP proceeding
  674. the \fB:\fP character.   You can then list the image keywords with
  675. \fImore\fP, without printing the binary image that follows the \fB:\fP
  676. separator.
  677. .PP
  678. Each keyword must be separated by at least one space but can be
  679. separated with control characters such a \fIform-feed\fP or
  680. \fInewline\fP.
  681. .PP
  682. A list of valid keywords follows:
  683. .TP 5
  684. .B "class=\fIDirectClass | PseudoClass\fP"
  685. identifies the type of binary image stored within the file.  
  686. X
  687. This keyword is optional.  If it is not specified, a \fIDirectClass\fP
  688. image format is assumed.  An explanation of \fIDirectClass\fP and
  689. \fIPseudoClass\fP image data follows this list.
  690. .TP 5
  691. .B "colors=\fIvalue\fP"
  692. specifies the number of colors in the image, and for pseudo-color
  693. images the size of the colormap.  
  694. X
  695. This keyword is optional.  However, if a colormap size is not
  696. specified, a linear colormap is assumed for pseudo-color images.
  697. .TP 5
  698. .B "columns=\fIvalue\fP"
  699. is a required keyword and specifies the number of columns, or width in
  700. pixels, of the image.
  701. .TP 5
  702. .B "compression=\fIQEncoded | RunlengthEncoded\fP"
  703. identifies how the image stored within the file is compressed.
  704. X
  705. This keyword is optional.  If it is not specified, the image is assumed
  706. to be uncompressed.  A detailed explanation of runlength-encoded and
  707. Q-coder image compression follows this list.
  708. .TP 5
  709. .B "id=\fIImageMagick\fP"
  710. is a required keyword and identifies this file as a MIFF image.  
  711. .TP 5
  712. .B "packets=\fIvalue\fP"
  713. specifies the number of compressed color packets in the image data section.  
  714. X
  715. This keyword is optional, but recommended, for runlength-encoded image
  716. compression.  It is required for Q-encoded image compression.  A
  717. detailed explanation of image compression follows this list.
  718. .TP 5
  719. .B "rows=\fIvalue\fP"
  720. is a required keyword and specifies the number of rows, or height in pixels, 
  721. of the image.
  722. .TP 5
  723. .B "scene=\fIvalue\fP"
  724. is an optional keyword and is a reference number for sequencing of
  725. images.  
  726. X
  727. This keyword is typically useful for animating a sequence of images.
  728. .PP
  729. Comments can be included in the keyword section.  Comments must begin with
  730. a \fB{\fP character and end with a \fI}\fP character.  
  731. .PP
  732. An example keyword section follows:
  733. .PP
  734. X    {
  735. X      Rendered via Dore by Sandy Hause.
  736. X    }
  737. X    id=ImageMagick
  738. X    class=PseudoClass  colors=256
  739. X    compression=RunlengthEncoded  packets=27601
  740. X    columns=1280  rows=1024
  741. X    scene=1
  742. X    ^L
  743. X    :
  744. .PP
  745. The binary image data that follows the keyword text is stored in one of 
  746. two binary classes as specified by the \fBclass\fP keyword: 
  747. \fIDirectClass\fP or \fIPseudoClass\fP.
  748. .PP
  749. Use the \fIDirectClass\fP class to store continuous-tone images.
  750. \fIDirectClass\fP requires that the image pixels immediately follow the
  751. keyword text and be stored as binary red, green, and blue intensity
  752. values.  The total number of pixels expected is equal to the number of pixel 
  753. columns times the number of pixel rows as specified by the \fBcolumns\fP and 
  754. \fBrows\fP keywords.
  755. .PP
  756. If the \fBcompression\fP keyword is not specified, a red, green, and blue byte 
  757. in that order is expected for each pixel of the image.
  758. .PP
  759. If \fBcompression\fP is \fIQEncoded\fP, each red, green, and blue byte
  760. intensity value is encoded using the Q-coder compression algorithm.
  761. Use the \fBpackets\fP keyword to specify the total number of Q-encoded
  762. packets that comprise the image.  Refer to "Sofware implementations of
  763. the Q-Coder", by Mitchell, J. L. and Pennebaker, W.B. (IBM Journal Res.
  764. Development, Volume 32, Number 6, November 1988, pages 753 - 774) for
  765. implementation specific details.
  766. .PP
  767. If \fBcompression\fP is \fIRunlengthEncoded\fP, each red, green, and
  768. blue byte intensity value is followed by a count byte. This value
  769. specifies the number of horizonally contiguous pixels in the image of
  770. that color.  The count (0-255) is one less than the actual number of
  771. contiguous pixels; thus a single packet can represent from 1 up to 256
  772. identical pixels.  The total number of pixels specified by the
  773. individual count bytes must add up to the number of pixel columns times
  774. the number of pixel rows as specified by the \fBcolumns\fP and
  775. \fBrows\fP keywords.  Use \fBpackets\fP to specify the total number of
  776. runlength-encoded packets that comprise the image.
  777. .PP
  778. Use the \fIPseudoClass\fP class to store pseudo-color images.
  779. \fIPseudoClass\fP requires that the image colormap and
  780. pseudo-color pixels immediately follow the keyword text.  The colormap
  781. is stored as contiguous red, green, and blue intensity values.  The
  782. number of intensity values expected is determined by the \fBcolors\fP
  783. keyword.  Note, an image colormap is restricted to at most 65535
  784. entries.  The binary pseudo-color image is stored as indexes into the
  785. colormap.  If the colormap size exceeds 256 entries, then each colormap
  786. index is two bytes each with the most-significant-byte first.  The
  787. total number of pixels expected is equal to the number of pixel columns
  788. times the number of pixel rows as specified by the \fBcolumns\fP and
  789. \fBrows\fP keywords.
  790. .PP
  791. If the \fBcompression\fP keyword is not specified, a colormap index is 
  792. expected for each pixel of the image.
  793. .PP
  794. If \fBcompression\fP is \fIQEncoded\fP, each colormap index is
  795. encoded using the Q-coder compression algorithm.  Use the \fBpackets\fP
  796. keyword to specify the total number of Q-encoded packets comprise the
  797. image.  Refer to "Sofware implementations of the Q-Coder", by Mitchell,
  798. J. L. and Pennebaker, W.B. (IBM Journal Res. Development, Volume 32,
  799. Number 6, November 1988, pages 753 - 774) for implementation specific
  800. details.
  801. .PP
  802. If \fBcompression\fP is \fIRunlengthEncoded\fP, each colormap index
  803. is followed by a count byte. This value  specifies the number of
  804. horizonally contiguous pixels in the image of that color.  The count
  805. (0-255) is one less than the actual number of contiguous pixels; thus a
  806. single packet can represent from 1 up to 256 identical pixels.  The
  807. total number of pixels specified by the individual count bytes must add
  808. up to the number of pixels expected in the image as specified by the
  809. \fBcolumns\fP and \fBrows\fP keywords.  Use \fBpackets\fP to specify the 
  810. total number of runlength-encoded packets that comprise the image.
  811. .SH FEATURES
  812. Although \fIDisplay\fP will display an image on a server with an immutable
  813. colormap, the image quality may suffer as compared to a server with a 
  814. read/write colormap.
  815. .PP
  816. \fIDisplay\fP memory requirements is proportionate to the area of the image.
  817. .PP
  818. \fIDisplay\fP does not complain when it encounters a keyword in an image file
  819. it does not understand.
  820. .SH ENVIRONMENT
  821. .TP 5
  822. .B DISPLAY
  823. To get the default host, display number, and screen.
  824. .SH SEE ALSO
  825. XX(1), xcmap(1), import(1), XtoPS(1), more(1), compress(1)
  826. .SH COPYRIGHT
  827. Copyright 1991 E. I. Dupont de Nemours & Company                           
  828. .PP                                                                           
  829. Permission to use, copy, modify, distribute, and sell this software and    
  830. its documentation for any purpose is hereby granted without fee,           
  831. provided that the above copyright notice appear in all copies and that     
  832. both that copyright notice and this permission notice appear in            
  833. supporting documentation, and that the name of E. I. Dupont de Nemours     
  834. & Company not be used in advertising or publicity pertaining to            
  835. distribution of the software without specific, written prior               
  836. permission.  E. I. Dupont de Nemours & Company makes no representations    
  837. about the suitability of this software for any purpose.  It is provided    
  838. "as is" without express or implied warranty.                               
  839. .PP
  840. E. I. Dupont de Nemours & Company disclaims all warranties with regard
  841. to this software, including all implied warranties of merchantability
  842. and fitness, in no event shall E. I. Dupont de Nemours & Company be
  843. liable for any special, indirect or consequential damages or any
  844. damages whatsoever resulting from loss of use, data or profits, whether
  845. in an action of contract, negligence or other tortious action, arising
  846. out of or in connection with the use or performance of this software.      
  847. .SH ACKNOWLEDGEMENTS
  848. The MIT X Consortium for making network transparent graphics a reality.
  849. .PP
  850. Michael Halle, Spatial Imaging Group at MIT, for the initial
  851. implementation of Alan Paeth's image rotation algorithm.
  852. .PP
  853. David Pensak, E. I. Dupont de Nemours & Company, for providing a
  854. computing environment that made this program possible.
  855. .PP
  856. Paul Raveling, USC Information Sciences Institute, for the original
  857. idea of using space subdivision for the color reduction algorithm.
  858. With Paul's permission, the \fBCOLOR REDUCTION ALGORITHM\fP section is
  859. a adaptation from a document he wrote.
  860. .SH AUTHORS
  861. John Cristy, E.I. DuPont de Nemours & Company Incorporated
  862. SHAR_EOF
  863. chmod 0755 ImageMagick/animate.man ||
  864. echo 'restore of ImageMagick/animate.man failed'
  865. Wc_c="`wc -c < 'ImageMagick/animate.man'`"
  866. test 29239 -eq "$Wc_c" ||
  867.     echo 'ImageMagick/animate.man: original size 29239, current size' "$Wc_c"
  868. rm -f _shar_wnt_.tmp
  869. fi
  870. # ============= ImageMagick/Announce ==============
  871. if test -f 'ImageMagick/Announce' -a X"$1" != X"-c"; then
  872.     echo 'x - skipping ImageMagick/Announce (File already exists)'
  873.     rm -f _shar_wnt_.tmp
  874. else
  875. > _shar_wnt_.tmp
  876. echo 'x - extracting ImageMagick/Announce (Text)'
  877. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/Announce' &&
  878. ImageMagick, an X11 image processing and display utility, is now available
  879. on export.lcs.mit.edu as contrib/ImageMagick.tar.Z.  After any initial bug
  880. reports are fixed, I will submit the utilities to comp.sources.x.
  881. X
  882. cristy@dupont.com
  883. X
  884. ---
  885. X
  886. Display
  887. X
  888. X    Display is a machine architecture independent image processing and
  889. X    display program.  It can display any image in the MIFF format on
  890. X    any workstation display running an X server.  Display first
  891. X    determines the hardware capabilities of the workstation.  If the
  892. X    number of unique colors in the image is less than or equal to the
  893. X    number the workstation can support, the image is displayed in an X
  894. X    window.  Otherwise the number of colors in the image is first
  895. X    reduced to match the color resolution of the workstation before it
  896. X    is displayed.
  897. X
  898. X    This means that a continuous-tone 24 bits/pixel image can display
  899. X    on a 8 bit pseudo-color device or monochrome device.  In most
  900. X    instances the reduced color image closely resembles the original.
  901. X    Alternatively, a monochrome or pseudo-color image can display on a
  902. X    continuous-tone 24 bits/pixels device.
  903. X
  904. Import
  905. X
  906. X    Import is an X Window System window dumping utility.  Import allows
  907. X    X users to store window images in a specially formatted dump file.
  908. X    This file can then be read by the Display utility for redisplay,
  909. X    printing, editing, formatting, archiving, image processing, etc.
  910. X    The target window can be specified by id or name or be selected by
  911. X    clicking the mouse in the desired window.  The keyboard bell is
  912. X    rung once at the beginning of the dump and twice when the dump is
  913. X    completed.
  914. X
  915. XXtoPS
  916. X
  917. X    XtoPS is an X Window System window dumping utility.  XtoPS reads an
  918. X    image from any visible window on an X server and outputs it to
  919. X    encapsulated Postscript file.  You can view this file with any
  920. X    Postscript compatible viewer or printer.  The image is displayed in
  921. X    color on viewers or printers that support color Postscript,
  922. X    otherwise it is displayed as grayscale.  The target window can be
  923. X    specified by id or name or be selected by clicking the mouse in the
  924. X    desired window.  The keyboard bell is rung once at the beginning of
  925. X    the dump and twice when the dump is completed.
  926. X
  927. Animate
  928. X
  929. X     Animate displays a sequence of images in the MIFF format on
  930. X     any workstation display running an X server.  Animate first
  931. X     determines the hardware capabilities of the workstation.  If
  932. X     the number of unique colors in an image is less than or
  933. X     equal to the number the workstation can support, the image
  934. X     is displayed in an X window.  Otherwise the number of colors
  935. X     in the image is first reduced to match the color resolution
  936. X     of the workstation before it is displayed.
  937. SHAR_EOF
  938. chmod 0755 ImageMagick/Announce ||
  939. echo 'restore of ImageMagick/Announce failed'
  940. Wc_c="`wc -c < 'ImageMagick/Announce'`"
  941. test 2779 -eq "$Wc_c" ||
  942.     echo 'ImageMagick/Announce: original size 2779, current size' "$Wc_c"
  943. rm -f _shar_wnt_.tmp
  944. fi
  945. # ============= ImageMagick/X.c ==============
  946. if test -f 'ImageMagick/X.c' -a X"$1" != X"-c"; then
  947.     echo 'x - skipping ImageMagick/X.c (File already exists)'
  948.     rm -f _shar_wnt_.tmp
  949. else
  950. > _shar_wnt_.tmp
  951. echo 'x - extracting ImageMagick/X.c (Text)'
  952. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/X.c' &&
  953. /*
  954. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  955. %                                                                             %
  956. %                                                                             %
  957. %                                                                             %
  958. %                                                                             %
  959. %                                  X   X                                      %
  960. %                                   X X                                       %
  961. %                                    X                                        %
  962. %                                   X X                                       %
  963. %                                  X   X                                      %
  964. %                                                                             %
  965. %                       X11 Utility Routines for Display.                     %
  966. %                                                                             %
  967. %                                                                             %
  968. %                                                                             %
  969. %                           Software Design                                   %
  970. %                             John Cristy                                     %
  971. %                            January  1991                                    %
  972. %                                                                             %
  973. %                                                                             %
  974. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  975. %                                                                             %
  976. %  Permission to use, copy, modify, distribute, and sell this software and    %
  977. %  its documentation for any purpose is hereby granted without fee,           %
  978. %  provided that the above Copyright notice appear in all copies and that     %
  979. %  both that Copyright notice and this permission notice appear in            %
  980. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  981. %  & Company not be used in advertising or publicity pertaining to            %
  982. %  distribution of the software without specific, written prior               %
  983. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  984. %  about the suitability of this software for any purpose.  It is provided    %
  985. %  "as is" without express or implied warranty.                               %
  986. %                                                                             %
  987. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  988. %  to this software, including all implied warranties of merchantability      %
  989. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  990. %  liable for any special, indirect or consequential damages or any           %
  991. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  992. %  in an action of contract, negligence or other tortious action, arising     %
  993. %  out of or in connection with the use or performance of this software.      %
  994. %                                                                             %
  995. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  996. %
  997. %
  998. */
  999. X
  1000. /*
  1001. X  Include declarations.
  1002. */
  1003. #include "display.h"
  1004. #include "image.h"
  1005. #include "X.h"
  1006. /*
  1007. X  External declarations.
  1008. */
  1009. extern void
  1010. X  Error();
  1011. X
  1012. /*
  1013. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1014. %                                                                             %
  1015. %                                                                             %
  1016. %                                                                             %
  1017. %   I s T r u e                                                               %
  1018. %                                                                             %
  1019. %                                                                             %
  1020. %                                                                             %
  1021. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1022. %
  1023. %  Function IsTrue returns True if the boolean is "true", "on", "yes" or "1".
  1024. %
  1025. %  The format of the IsTrue routine is:
  1026. %
  1027. %      option=IsTrue(boolean)
  1028. %
  1029. %  A description of each parameter follows:
  1030. %
  1031. %    o option:either True or False depending on the boolean parameter.
  1032. %
  1033. %    o boolean:Specifies a pointer to a character array.
  1034. %
  1035. %
  1036. */
  1037. int IsTrue(boolean)
  1038. char
  1039. X  *boolean;
  1040. {
  1041. X  char
  1042. X    c,
  1043. X    *p;
  1044. X
  1045. X  if (boolean == (char *) NULL)
  1046. X    return(False);
  1047. X  for (p=boolean; *p != (char) NULL; p++)
  1048. X  {
  1049. X    /*
  1050. X      Convert to lower case.
  1051. X    */
  1052. X    c=(*p);
  1053. X    if (isascii(c) && isupper(c))
  1054. X      *p=tolower(c);
  1055. X  }
  1056. X  if (strcmp(boolean,"true") == 0)
  1057. X    return(True);
  1058. X  if (strcmp(boolean,"on") == 0)
  1059. X    return(True);
  1060. X  if (strcmp(boolean,"yes") == 0)
  1061. X    return(True);
  1062. X  if (strcmp(boolean,"1") == 0)
  1063. X    return(True);
  1064. X  return(False);
  1065. }
  1066. X
  1067. /*
  1068. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1069. %                                                                             %
  1070. %                                                                             %
  1071. %                                                                             %
  1072. %   L a t i n 1 U p p e r                                                     %
  1073. %                                                                             %
  1074. %                                                                             %
  1075. %                                                                             %
  1076. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1077. %
  1078. %  Function Latin1Upper converts a string to upper-case Latin1.
  1079. %
  1080. %  The format of the Latin1Upper routine is:
  1081. %
  1082. %      Latin1Upper(string)
  1083. %
  1084. %  A description of each parameter follows:
  1085. %
  1086. %    o string: A pointer to the string to convert to upper-case Latin1.
  1087. %
  1088. %
  1089. */
  1090. void Latin1Upper(string)
  1091. unsigned char
  1092. X  *string;
  1093. {
  1094. X  unsigned char
  1095. X    c;
  1096. X
  1097. X  c=(*string);
  1098. X  while (c != (char) NULL)
  1099. X  {
  1100. X    if ((c >= XK_a) && (c <= XK_z))
  1101. X      *string=c-(XK_a-XK_A);
  1102. X    else
  1103. X      if ((c >= XK_agrave) && (c <= XK_odiaeresis))
  1104. X        *string=c-(XK_agrave-XK_Agrave);
  1105. X      else
  1106. X        if ((c >= XK_oslash) && (c <= XK_thorn))
  1107. X          *string=c-(XK_oslash-XK_Ooblique);
  1108. X    string++;
  1109. X    c=(*string);
  1110. X  }
  1111. }
  1112. X
  1113. /*
  1114. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1115. %                                                                             %
  1116. %                                                                             %
  1117. %                                                                             %
  1118. %   X B e s t F o n t                                                         %
  1119. %                                                                             %
  1120. %                                                                             %
  1121. %                                                                             %
  1122. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1123. %
  1124. %  Function XBestFont returns the "best" font.  "Best" is defined as a font
  1125. %  specified in the X resource database or a font such that the text width
  1126. %  displayed with the font does not exceed the specified maximum width.
  1127. %
  1128. %  The format of the XBestFont routine is:
  1129. %
  1130. %      font=XBestFont(display,resource_info,text,maximum_width)
  1131. %
  1132. %  A description of each parameter follows:
  1133. %
  1134. %    o font: XBestFont returns a pointer to a XFontStruct structure.
  1135. %
  1136. %    o display: Specifies a connection to an X server;  returned from
  1137. %      XOpenDisplay.
  1138. %
  1139. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  1140. %
  1141. %    o text: Specifies the text whose width is compared to the maximum.
  1142. %
  1143. %    o maximum_width: Specifies the maximum width in pixels of the text.
  1144. %
  1145. %
  1146. */
  1147. XXFontStruct *XBestFont(display,resource_info,text,maximum_width)
  1148. Display
  1149. X  *display;
  1150. X
  1151. XXResourceInfo
  1152. X  *resource_info;
  1153. X
  1154. char
  1155. X  *text;
  1156. X
  1157. unsigned int
  1158. X  maximum_width;
  1159. {
  1160. X  static char
  1161. X    *fonts[]=
  1162. X    {
  1163. X      "fixed",
  1164. X      "6x10",
  1165. X      "5x8",
  1166. X      (char *) NULL
  1167. X    };
  1168. X
  1169. X  char
  1170. X    *font_name,
  1171. X    **p;
  1172. X
  1173. X  XFontStruct
  1174. X    *font_info;
  1175. X
  1176. X  font_info=(XFontStruct *) NULL;
  1177. X  font_name=resource_info->font_name;
  1178. X  if (font_name != (char *) NULL)
  1179. X    {
  1180. X      /*
  1181. X        Load preferred font specified in the X resource database.
  1182. X      */
  1183. X      font_info=XLoadQueryFont(display,font_name);
  1184. X      if (font_info == (XFontStruct *) NULL)
  1185. X        Warning("unable to load font",font_name);
  1186. X    }
  1187. X  /*
  1188. X    Load a font that does not exceed the text width.
  1189. X  */
  1190. X  for (p=fonts; *p != (char *) NULL; p++)
  1191. X  {
  1192. X    if (font_info != (XFontStruct *) NULL)
  1193. X      {
  1194. X        if (XTextWidth(font_info,text,strlen(text)) < maximum_width)
  1195. X          break;
  1196. X        font_name=(*p);
  1197. X        XFreeFont(display,font_info);
  1198. X      }
  1199. X    font_info=XLoadQueryFont(display,*p);
  1200. X  }
  1201. X  if (font_info == (XFontStruct *) NULL)
  1202. X    font_info=XLoadQueryFont(display,"fixed");  /* backup font */
  1203. X  if (font_info == (XFontStruct *) NULL)
  1204. X    font_info=XLoadQueryFont(display,"variable");  /* backup font */
  1205. X  return(font_info);
  1206. }
  1207. X
  1208. /*
  1209. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1210. %                                                                             %
  1211. %                                                                             %
  1212. %                                                                             %
  1213. %   X B e s t I c o n S i z e                                                 %
  1214. %                                                                             %
  1215. %                                                                             %
  1216. %                                                                             %
  1217. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1218. %
  1219. %  Function XBestIconSize returns the "best" icon size.  "Best" is defined as
  1220. %  an icon size that maintains the aspect ratio of the image.  If the window
  1221. %  manager has preferred icon sizes, one of the preferred sizes is used.
  1222. %
  1223. %  The format of the XBestIconSize routine is:
  1224. %
  1225. %      XBestIconSize(display,icon_window,image)
  1226. %
  1227. %  A description of each parameter follows:
  1228. %
  1229. %    o display: Specifies a connection to an X server;  returned from
  1230. %      XOpenDisplay.
  1231. %
  1232. %    o image: Specifies a pointer to a Image structure;  returned from
  1233. %      ReadImage.
  1234. %
  1235. %
  1236. */
  1237. void XBestIconSize(display,icon_window,image)
  1238. Display
  1239. X  *display;
  1240. X
  1241. XXWindowInfo
  1242. X  *icon_window;
  1243. X
  1244. Image
  1245. X  *image;
  1246. {
  1247. #define MaxIconSize  64
  1248. X
  1249. X  int
  1250. X    number_sizes;
  1251. X
  1252. X  unsigned long
  1253. X    icon_height,
  1254. X    icon_width,
  1255. X    scale_factor;
  1256. X
  1257. X  Window
  1258. X    root_window;
  1259. X
  1260. X  XIconSize
  1261. X    *icon_size,
  1262. X    *size_list;
  1263. X
  1264. X  /*
  1265. X    Determine if the window manager has specified preferred icon sizes.
  1266. X  */
  1267. X  icon_size=(XIconSize *) NULL;
  1268. X  number_sizes=0;
  1269. X  root_window=XRootWindow(display,icon_window->screen);
  1270. X  if (XGetIconSizes(display,root_window,&size_list,&number_sizes) != 0)
  1271. X    if ((number_sizes > 0) && (size_list != (XIconSize *) NULL))
  1272. X      icon_size=size_list;
  1273. X  if (icon_size == (XIconSize *) NULL)
  1274. X    {
  1275. X      /*
  1276. X        Window manager does not restrict icon size.
  1277. X      */
  1278. X      icon_size=XAllocIconSize();
  1279. X      if (icon_size == (XIconSize *) NULL)
  1280. X        Error("unable to create icon","memory allocation failed");
  1281. X      icon_size->min_width=1;
  1282. X      icon_size->max_width=MaxIconSize;
  1283. X      icon_size->min_height=1;
  1284. X      icon_size->max_height=MaxIconSize;
  1285. X      icon_size->width_inc=1;
  1286. X      icon_size->height_inc=1;
  1287. X    }
  1288. X  /*
  1289. X    Look for an icon size that maintains the aspect ratio of image.
  1290. X  */
  1291. X  if (image->columns >= image->rows)
  1292. X    {
  1293. X      icon_width=icon_size->max_width;
  1294. X      icon_height=icon_size->min_height;
  1295. X      scale_factor=(image->rows << 14)/image->columns;
  1296. X      while (icon_height < icon_size->max_height)
  1297. X      {
  1298. X        if (icon_height >= ((icon_size->max_width*scale_factor+8191) >> 14))
  1299. X          break;
  1300. X        icon_height+=icon_size->height_inc;
  1301. X      }
  1302. X    }
  1303. X  else
  1304. X    {
  1305. X      icon_width=icon_size->min_width;
  1306. X      icon_height=icon_size->max_height;
  1307. X      scale_factor=(image->columns << 14)/image->rows;
  1308. X      while (icon_width < icon_size->max_width)
  1309. X      {
  1310. X        if (icon_width >= ((icon_size->max_height*scale_factor+8191) >> 14))
  1311. X          break;
  1312. X        icon_width+=icon_size->width_inc;
  1313. X      }
  1314. X    }
  1315. X  XFree((char *) icon_size);
  1316. X  icon_window->width=icon_width;
  1317. X  icon_window->height=icon_height;
  1318. }
  1319. X
  1320. /*
  1321. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1322. %                                                                             %
  1323. %                                                                             %
  1324. %                                                                             %
  1325. %   X B e s t P i x e l                                                       %
  1326. %                                                                             %
  1327. %                                                                             %
  1328. %                                                                             %
  1329. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1330. %
  1331. %  Function XBestPixel returns a pixel from an array of pixels that is closest
  1332. %  to the requested color.
  1333. %
  1334. %  The format of the XBestPixel routine is:
  1335. %
  1336. %      pixel=XBestPixel(colors,number_colors,color)
  1337. %
  1338. %  A description of each parameter follows:
  1339. %
  1340. %    o pixel: XBestPixel returns the pixel value closest to the requested
  1341. %      color.
  1342. %
  1343. %    o colors: Specifies an array of XColor structures.
  1344. %
  1345. %    o number_colors: Specifies the number of XColor structures in the
  1346. %      color definition array.
  1347. %
  1348. %    o color: Specifies the desired RGB value to find in the colors array.
  1349. %
  1350. %
  1351. */
  1352. unsigned long XBestPixel(colors,number_colors,color)
  1353. XXColor
  1354. X  *colors;
  1355. X
  1356. unsigned int
  1357. X  number_colors;
  1358. X
  1359. XXColor
  1360. X  color;
  1361. {
  1362. X  register int
  1363. X    blue_distance,
  1364. X    green_distance,
  1365. X    i,
  1366. X    red_distance;
  1367. X
  1368. X  register unsigned long
  1369. X    distance,
  1370. X    min_distance,
  1371. X    pixel;
  1372. X
  1373. X  /*
  1374. X    Find closest representation for the requested RGB color.
  1375. X  */
  1376. X  pixel=0;
  1377. X  min_distance=(~0);
  1378. X  for (i=0; i < number_colors; i++)
  1379. X  {
  1380. X    red_distance=(colors[i].red >> 8)-(color.red >> 8);
  1381. X    green_distance=(colors[i].green >> 8)-(color.green >> 8);
  1382. X    blue_distance=(colors[i].blue >> 8)-(color.blue >> 8);
  1383. X    distance=red_distance*red_distance+green_distance*green_distance+
  1384. X      blue_distance*blue_distance;
  1385. X    if (distance < min_distance)
  1386. X      {
  1387. X        min_distance=distance;
  1388. X        pixel=colors[i].pixel;
  1389. X      }
  1390. X  }
  1391. X  return(pixel);
  1392. }
  1393. X
  1394. /*
  1395. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1396. %                                                                             %
  1397. %                                                                             %
  1398. %                                                                             %
  1399. %   X B e s t V i s u a l I n f o                                             %
  1400. %                                                                             %
  1401. %                                                                             %
  1402. %                                                                             %
  1403. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1404. %
  1405. %  Function XBestVisualInfo returns visual information for a visual that is
  1406. %  the "best" the server supports.  "Best" is defined as:
  1407. %
  1408. %    1. Restrict the visual list to those supported by the default screen.
  1409. %
  1410. %    2. If a visual type is specified, restrict the visual list to those of
  1411. %       that type.
  1412. %
  1413. %    3. If a map type is specified, choose the visual that matches the id
  1414. %       specified by the Standard Colormap.
  1415. %
  1416. %    4  From the list of visuals, choose one that can display the most
  1417. %       simultaneous colors.  If more than one visual can display the same
  1418. %       number of simultaneous colors, one is choosen based on a rank.
  1419. %
  1420. %  The format of the XBestVisualInfo routine is:
  1421. %
  1422. %      visual_info=XBestVisualInfo(display,visual_type,map_type,map_info)
  1423. %
  1424. %  A description of each parameter follows:
  1425. %
  1426. %    o visual_info: XBestVisualInfo returns a pointer to a X11 XVisualInfo
  1427. %      structure.
  1428. %
  1429. %    o display: Specifies a connection to an X server;  returned from
  1430. %      XOpenDisplay.
  1431. %
  1432. %    o visual_type: Specifies the desired visual type.
  1433. %
  1434. %    o map_type: Specifies the desired Standard Colormap type.
  1435. %
  1436. %    o map_info: If map_type is specified, this structure is initialized
  1437. %      with info from the Standard Colormap.
  1438. %
  1439. %
  1440. */
  1441. XXVisualInfo *XBestVisualInfo(display,visual_type,map_type,map_info)
  1442. Display
  1443. X  *display;
  1444. X
  1445. char
  1446. X  *visual_type,
  1447. X  *map_type;
  1448. X
  1449. XXStandardColormap
  1450. X  **map_info;
  1451. {
  1452. #define XVisualColormapSize(visual_info) \
  1453. X  ((visual_info->class == TrueColor) || (visual_info->class == DirectColor) ? \
  1454. X    visual_info->red_mask | visual_info->green_mask | visual_info->blue_mask : \
  1455. X    visual_info->colormap_size)
  1456. X
  1457. X  int
  1458. X    number_visuals;
  1459. X
  1460. X  register int
  1461. X    i;
  1462. X
  1463. X  unsigned int
  1464. X    visual_mask;
  1465. X
  1466. X  XVisualInfo
  1467. X    *visual_info,
  1468. X    *visual_list,
  1469. X    visual_template;
  1470. X
  1471. X  /*
  1472. X    Restrict visual search by screen number.
  1473. X  */
  1474. X  visual_mask=VisualScreenMask;
  1475. X  visual_template.screen=XDefaultScreen(display);
  1476. X  if (visual_type != (char *) NULL)
  1477. X    {
  1478. X      register char
  1479. X        *p;
  1480. X
  1481. X      register int
  1482. X        c;
  1483. X
  1484. X      /*
  1485. X        Restrict visual search by class or visual id.
  1486. X      */
  1487. X      p=visual_type;
  1488. X      for ( ; *p != (char) NULL; p++)
  1489. X      {
  1490. X        /*
  1491. X          Convert to lower case.
  1492. X        */
  1493. X        c=(*p);
  1494. X        if (isascii(c) && isupper(c))
  1495. X          *p=tolower(c);
  1496. X      }
  1497. X      if (strcmp("staticgray",visual_type) == 0)
  1498. X        {
  1499. X          visual_mask|=VisualClassMask;
  1500. X          visual_template.class=StaticGray;
  1501. X        }
  1502. X      else
  1503. X        if (strcmp("grayscale",visual_type) == 0)
  1504. X          {
  1505. X            visual_mask|=VisualClassMask;
  1506. X            visual_template.class=GrayScale;
  1507. X          }
  1508. X        else
  1509. X          if (strcmp("staticcolor",visual_type) == 0)
  1510. X            {
  1511. X              visual_mask|=VisualClassMask;
  1512. X              visual_template.class=StaticColor;
  1513. X            }
  1514. X          else
  1515. X            if (strcmp("pseudocolor",visual_type) == 0)
  1516. X              {
  1517. X                visual_mask|=VisualClassMask;
  1518. X                visual_template.class=PseudoColor;
  1519. X              }
  1520. X            else
  1521. X              if (strcmp("truecolor",visual_type) == 0)
  1522. X                {
  1523. X                  visual_mask|=VisualClassMask;
  1524. X                  visual_template.class=TrueColor;
  1525. X                }
  1526. X              else
  1527. X                if (strcmp("directcolor",visual_type) == 0)
  1528. X                  {
  1529. X                    visual_mask|=VisualClassMask;
  1530. X                    visual_template.class=DirectColor;
  1531. X                  }
  1532. X                else
  1533. X                  if (strcmp("default",visual_type) == 0)
  1534. X                    {
  1535. X                      visual_mask|=VisualIDMask;
  1536. X                      visual_template.visualid=XVisualIDFromVisual(
  1537. X                        XDefaultVisual(display,XDefaultScreen(display)));
  1538. X                    }
  1539. X                  else
  1540. X                    if (isdigit(*visual_type))
  1541. X                      {
  1542. X                        visual_mask|=VisualIDMask;
  1543. X                        visual_template.visualid=
  1544. X                          strtol(visual_type,(char **) NULL,0);
  1545. X                      }
  1546. X                    else
  1547. X                      Error("invalid visual specifier",visual_type);
  1548. X    }
  1549. X  /*
  1550. X    Get all visuals that meet our criteria so far.
  1551. X  */
  1552. X  number_visuals=0;
  1553. X  visual_list=XGetVisualInfo(display,visual_mask,&visual_template,
  1554. X    &number_visuals);
  1555. X  visual_mask=VisualScreenMask | VisualIDMask;
  1556. X  if ((number_visuals == 0) || (visual_list == (XVisualInfo *) NULL))
  1557. X    {
  1558. X      /*
  1559. X        Failed to get visual;  try using the default visual.
  1560. X      */
  1561. X      Warning("unable to get visual",visual_type);
  1562. X      visual_template.visualid=
  1563. X        XVisualIDFromVisual(XDefaultVisual(display,XDefaultScreen(display)));
  1564. X      visual_list=XGetVisualInfo(display,visual_mask,&visual_template,
  1565. X        &number_visuals);
  1566. X      if ((number_visuals == 0) || (visual_list == (XVisualInfo *) NULL))
  1567. X        return((XVisualInfo *) NULL);
  1568. X      Warning("using default visual",XVisualClassName(visual_list));
  1569. X    }
  1570. X  if (map_type != (char *) NULL)
  1571. X    {
  1572. X      Atom
  1573. X        map_property;
  1574. X
  1575. X      int
  1576. X        j,
  1577. X        number_maps,
  1578. X        status;
  1579. X
  1580. X      unsigned char
  1581. X        map_name[256];
  1582. X
  1583. X      Window
  1584. X        root_window;
  1585. X
  1586. X      XStandardColormap
  1587. X        *map_list;
  1588. X
  1589. X      /*
  1590. X        Restrict visual search by Standard Colormap visual id.
  1591. X      */
  1592. X      (void) sprintf((char *) map_name,"RGB_%s_MAP",map_type);
  1593. X      Latin1Upper(map_name);
  1594. X      map_property=XInternAtom(display,(char *) map_name,True);
  1595. X      if (map_property == (Atom) NULL)
  1596. X        Error("unable to get Standard Colormap",map_type);
  1597. X      root_window=XRootWindow(display,XDefaultScreen(display));
  1598. X      status=XGetRGBColormaps(display,root_window,&map_list,&number_maps,
  1599. X        map_property);
  1600. X      if (status == 0)
  1601. X        Error("unable to get Standard Colormap",map_type);
  1602. X      /*
  1603. X        Search all Standard Colormaps and visuals for ids that match.
  1604. X      */
  1605. X      *map_info=XAllocStandardColormap();
  1606. X      if (*map_info == (XStandardColormap *) NULL)
  1607. X        Error("unable to create Standard Colormap","memory allocation failed");
  1608. X      **map_info=map_list[0];
  1609. #ifndef PRE_R4_ICCCM
  1610. X      visual_template.visualid=XVisualIDFromVisual(visual_list[0].visual);
  1611. X      for (i=0; i < number_maps; i++)
  1612. X        for (j=0; j < number_visuals; j++)
  1613. X          if (map_list[i].visualid ==
  1614. X              XVisualIDFromVisual(visual_list[j].visual))
  1615. X            {
  1616. X              **map_info=map_list[i];
  1617. X              visual_template.visualid=
  1618. X                XVisualIDFromVisual(visual_list[j].visual);
  1619. X              break;
  1620. X            }
  1621. X      if ((*map_info)->visualid != visual_template.visualid)
  1622. X        Error("unable to match visual to Standard Colormap",map_type);
  1623. #endif
  1624. X      if ((*map_info)->colormap == (Colormap) NULL)
  1625. X        Error("Standard Colormap is not initialized",map_type);
  1626. X      XFree((char *) map_list);
  1627. X    }
  1628. X  else
  1629. X    {
  1630. X      static unsigned int
  1631. X        rank[]=
  1632. X          {
  1633. X            StaticGray,
  1634. X            GrayScale,
  1635. X            StaticColor,
  1636. X            DirectColor,
  1637. X            TrueColor,
  1638. X            PseudoColor
  1639. X          };
  1640. X
  1641. X      XVisualInfo
  1642. X        *p;
  1643. X
  1644. X      /*
  1645. X        Pick one visual that displays the most simultaneous colors.
  1646. X      */
  1647. X      visual_info=visual_list;
  1648. X      p=visual_list;
  1649. X      for (i=1; i < number_visuals; i++)
  1650. X      {
  1651. X        p++;
  1652. X        if (XVisualColormapSize(p) > XVisualColormapSize(visual_info))
  1653. X          visual_info=p;
  1654. X        else
  1655. X          if (XVisualColormapSize(p) == XVisualColormapSize(visual_info))
  1656. X            if (rank[p->class] > rank[visual_info->class])
  1657. X              visual_info=p;
  1658. X      }
  1659. X      visual_template.visualid=XVisualIDFromVisual(visual_info->visual);
  1660. X    }
  1661. X  XFree((char *) visual_list);
  1662. X  /*
  1663. X    Retrieve only one visual by its screen & id number.
  1664. X  */
  1665. X  visual_info=XGetVisualInfo(display,visual_mask,&visual_template,
  1666. X    &number_visuals);
  1667. X  if ((number_visuals == 0) || (visual_info == (XVisualInfo *) NULL))
  1668. X    return((XVisualInfo *) NULL);
  1669. X  return(visual_info);
  1670. }
  1671. X
  1672. /*
  1673. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1674. %                                                                             %
  1675. %                                                                             %
  1676. %                                                                             %
  1677. %   X C l i e n t W i n d o w                                                 %
  1678. %                                                                             %
  1679. %                                                                             %
  1680. %                                                                             %
  1681. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1682. %
  1683. %  Function XClientWindow finds a window, at or below the specified window,
  1684. %  which has a WM_STATE property.  If such a window is found, it is returned,
  1685. %  otherwise the argument window is returned.
  1686. %
  1687. %  The format of the XClientWindow function is:
  1688. %
  1689. %      client_window=XClientWindow(display,target_window)
  1690. %
  1691. %  A description of each parameter follows:
  1692. %
  1693. %    o client_window:XClientWindow returns a window, at or below the specified
  1694. %      window, which has a WM_STATE property otherwise the argument
  1695. %      target_window is returned.
  1696. %
  1697. %    o display:Specifies a pointer to the Display structure;  returned from
  1698. %      XOpenDisplay.
  1699. %
  1700. %    o target_window:Specifies the window to find a WM_STATE property.
  1701. %
  1702. %
  1703. */
  1704. Window XClientWindow(display,target_window)
  1705. Display
  1706. X  *display;
  1707. X
  1708. Window
  1709. X  target_window;
  1710. {
  1711. X  Atom
  1712. X    state,
  1713. X    type;
  1714. X
  1715. X  int
  1716. X    format;
  1717. X
  1718. X  unsigned char
  1719. X    *data;
  1720. X
  1721. X  unsigned long
  1722. X    after,
  1723. X    number_items;
  1724. X
  1725. X  Window
  1726. X    client_window;
  1727. X
  1728. X  state=XInternAtom(display,"WM_STATE",True);
  1729. X  if (state == (Atom) NULL)
  1730. X    return(target_window);
  1731. X  type=(Atom) NULL;
  1732. X  (void) XGetWindowProperty(display,target_window,state,0L,0L,False,
  1733. X    AnyPropertyType,&type,&format,&number_items,&after,&data);
  1734. X  if (type != (Atom) NULL)
  1735. X    return(target_window);
  1736. X  client_window=XWindowByProperty(display,target_window,state);
  1737. X  if (client_window == (Window) NULL)
  1738. X    return(target_window);
  1739. X  return(client_window);
  1740. }
  1741. X
  1742. /*
  1743. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1744. %                                                                             %
  1745. %                                                                             %
  1746. %                                                                             %
  1747. %   X M a k e I m a g e                                                       %
  1748. %                                                                             %
  1749. %                                                                             %
  1750. %                                                                             %
  1751. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1752. %
  1753. %  Function XMakeImage creates an X11 image.  If the image size differs from
  1754. %  the X11 image size, the image is first resized.
  1755. %
  1756. %  The format of the XMakeImage routine is:
  1757. %
  1758. %      ximage=XMakeImage(display,resource_info,image_window,image,columns,
  1759. %        height)
  1760. %
  1761. %  A description of each parameter follows:
  1762. %
  1763. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  1764. %      XMakeImage.
  1765. %
  1766. %    o display: Specifies a connection to an X server; returned from
  1767. %      XOpenDisplay.
  1768. %
  1769. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  1770. %
  1771. %    o image_window: Specifies a pointer to a XWindowInfo structure.
  1772. %
  1773. %    o image: Specifies a pointer to a Image structure;  returned from
  1774. %      ReadImage.
  1775. %
  1776. %    o width: Specifies the width in pixels of the rectangular area to
  1777. %      display.
  1778. %
  1779. %    o height: Specifies the height in pixels of the rectangular area to
  1780. %      display.
  1781. %
  1782. %
  1783. */
  1784. XXImage *XMakeImage(display,resource_info,image_window,image,width,height)
  1785. Display
  1786. X  *display;
  1787. X
  1788. XXResourceInfo
  1789. X  *resource_info;
  1790. X
  1791. XXWindowInfo
  1792. X  *image_window;
  1793. X
  1794. Image
  1795. X  *image;
  1796. X
  1797. unsigned int
  1798. X  width,
  1799. X  height;
  1800. {
  1801. X  Image
  1802. X    *transformed_image;
  1803. X
  1804. X  XImage
  1805. X    *ximage;
  1806. X
  1807. X  /*
  1808. X    Create X image.
  1809. X  */
  1810. X  ximage=XCreateImage(display,image_window->visual_info->visual,
  1811. X    (resource_info->monochrome ? 1 : image_window->depth),
  1812. X    (((image_window->depth == 1) || resource_info->monochrome) ? XYBitmap :
  1813. X    ZPixmap),0,(char *) NULL,width,height,XBitmapPad(display),0);
  1814. X  if (ximage == (XImage *) NULL)
  1815. X    return((XImage *) NULL);
  1816. X  /*
  1817. X    Allocate X image pixel data.
  1818. X  */
  1819. X  if (ximage->format == XYBitmap)
  1820. X    ximage->data=(char *) malloc((unsigned int)
  1821. X      ximage->bytes_per_line*ximage->height*ximage->depth);
  1822. X  else
  1823. X    ximage->data=(char *)
  1824. X      malloc((unsigned int) ximage->bytes_per_line*ximage->height);
  1825. X  if (ximage->data == (char *) NULL)
  1826. X    {
  1827. X      /*
  1828. X        Unable to allocate pixel data.
  1829. X      */
  1830. X      XDestroyImage(ximage);
  1831. X      return((XImage *) NULL);
  1832. X    }
  1833. X  if (image == (Image *) NULL)
  1834. X    return(ximage);
  1835. X  transformed_image=image;
  1836. X  if (image_window->clip_geometry)
  1837. X    {
  1838. X      Image
  1839. X        *clipped_image;
  1840. X
  1841. X      int
  1842. X        clip_x,
  1843. X        clip_y;
  1844. X
  1845. X      unsigned
  1846. X        clip_height,
  1847. X        clip_width;
  1848. X
  1849. X      /*
  1850. X        Clip image.
  1851. X      */
  1852. X      (void) XParseGeometry(image_window->clip_geometry,&clip_x,&clip_y,
  1853. X        &clip_width,&clip_height);
  1854. X      clipped_image=
  1855. X        ClipImage(transformed_image,clip_x,clip_y,clip_width,clip_height);
  1856. X      if (clipped_image == (Image *) NULL)
  1857. X        return((XImage *) NULL);
  1858. X      if (transformed_image != image)
  1859. X        DestroyImage(transformed_image);
  1860. X      transformed_image=clipped_image;
  1861. X    }
  1862. X  if ((ximage->width != transformed_image->columns) ||
  1863. X      (ximage->height != transformed_image->rows))
  1864. X    {
  1865. X      Image
  1866. X        *scaled_image;
  1867. X
  1868. X      /*
  1869. X        Scale image.
  1870. X      */
  1871. X      scaled_image=ScaleImage(transformed_image,(unsigned int) ximage->width,
  1872. X        (unsigned int) ximage->height);
  1873. X      if (scaled_image == (Image *) NULL)
  1874. X        return((XImage *) NULL);
  1875. X      if (transformed_image != image)
  1876. X        DestroyImage(transformed_image);
  1877. X      transformed_image=scaled_image;
  1878. X    }
  1879. X  /*
  1880. X    Convert runlength-encoded pixels to X image data.
  1881. X  */
  1882. X  if ((ximage->byte_order == LSBFirst) ||
  1883. X      ((ximage->format == XYBitmap) && (ximage->bitmap_bit_order == LSBFirst)))
  1884. X    XMakeImageLSBFirst(image_window->map_info,image_window->pixel_info,
  1885. X      transformed_image,ximage);
  1886. X  else
  1887. X    XMakeImageMSBFirst(image_window->map_info,image_window->pixel_info,
  1888. X      transformed_image,ximage);
  1889. X  if (transformed_image != image)
  1890. X    DestroyImage(transformed_image);
  1891. X  return(ximage);
  1892. }
  1893. X
  1894. /*
  1895. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1896. %                                                                             %
  1897. %                                                                             %
  1898. %                                                                             %
  1899. %   X M a k e I m a g e L S B F i r s t                                       %
  1900. %                                                                             %
  1901. %                                                                             %
  1902. %                                                                             %
  1903. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1904. %
  1905. %  Function XMakeImageLSBFirst initializes the pixel data of an X11 Image.
  1906. %  The X image pixels are copied in least-significant bit and byte first
  1907. %  order.  The server's scanline pad is respected.  Rather than using one or
  1908. %  two general cases, many special cases are found here to help speed up the
  1909. %  image conversion.
  1910. %
  1911. %  The format of the XMakeImageLSBFirst routine is:
  1912. %
  1913. %      XMakeImageLSBFirst(map_info,pixel_info,image,ximage)
  1914. %
  1915. %  A description of each parameter follows:
  1916. %
  1917. %    o map_info: Specifies a pointer to a XStandardColormap structure.
  1918. %
  1919. %    o pixel_info: Specifies a pointer to a XPixelInfo structure.
  1920. %
  1921. %    o image: Specifies a pointer to a Image structure;  returned from
  1922. %      ReadImage.
  1923. %
  1924. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  1925. %      XCreateImage.
  1926. %
  1927. %
  1928. */
  1929. void XMakeImageLSBFirst(map_info,pixel_info,image,ximage)
  1930. XXStandardColormap
  1931. X  *map_info;
  1932. X
  1933. XXPixelInfo
  1934. X  *pixel_info;
  1935. X
  1936. Image
  1937. X  *image;
  1938. X
  1939. XXImage
  1940. X  *ximage;
  1941. {
  1942. X  register int
  1943. X    i,
  1944. X    j,
  1945. X    x;
  1946. X
  1947. X  register RunlengthPacket
  1948. X    *p;
  1949. X
  1950. X  register unsigned char
  1951. X    *q;
  1952. X
  1953. X  register unsigned long
  1954. X    pixel;
  1955. X
  1956. X  unsigned int
  1957. X    scanline_pad;
  1958. X
  1959. X  unsigned long
  1960. X    *pixels;
  1961. X
  1962. X  pixels=pixel_info->pixels;
  1963. X  p=image->pixels;
  1964. X  q=(unsigned char *) ximage->data;
  1965. X  x=0;
  1966. X  if (ximage->format == XYBitmap)
  1967. X    {
  1968. X      register unsigned char
  1969. X        bit,
  1970. X        byte;
  1971. X
  1972. X      register unsigned char
  1973. X        foreground_pixel;
  1974. X
  1975. X      /*
  1976. X        Convert image to big-endian bitmap.
  1977. X      */
  1978. X      foreground_pixel=(Intensity(image->colormap[0]) >
  1979. X        Intensity(image->colormap[1]) ? 0 : 1);
  1980. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  1981. X      bit=0;
  1982. X      byte=0;
  1983. X      for (i=0; i < image->packets; i++)
  1984. X      {
  1985. X        for (j=0; j <= p->length; j++)
  1986. X        {
  1987. X          byte>>=1;
  1988. X          if (p->index == foreground_pixel)
  1989. X            byte|=0x80;
  1990. X          bit++;
  1991. X          if (bit == 8)
  1992. X            {
  1993. X              *q++=byte;
  1994. X              bit=0;
  1995. X              byte=0;
  1996. X            }
  1997. X          x++;
  1998. X          if (x == ximage->width)
  1999. X            {
  2000. X              /*
  2001. X                Advance to the next scanline.
  2002. X              */
  2003. X              if (bit > 0)
  2004. X                *q=byte >> (8-bit);
  2005. X              q+=scanline_pad;
  2006. X              bit=0;
  2007. X              byte=0;
  2008. X              x=0;
  2009. X            }
  2010. X        }
  2011. X        p++;
  2012. X      }
  2013. X    }
  2014. X  else
  2015. X    {
  2016. X      /*
  2017. X        Convert image to little-endian color-mapped X image.
  2018. X      */
  2019. X      scanline_pad=ximage->bytes_per_line-
  2020. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  2021. X      if (pixels != (unsigned long *) NULL)
  2022. X        switch (ximage->bits_per_pixel)
  2023. X        {
  2024. X          case 2:
  2025. X          {
  2026. X            register unsigned int
  2027. X              nibble;
  2028. X
  2029. X            /*
  2030. X              Convert to 2 bit color-mapped X image.
  2031. X            */
  2032. X            nibble=0;
  2033. X            for (i=0; i < image->packets; i++)
  2034. X            {
  2035. X              pixel=pixels[p->index] & 0xf;
  2036. X              for (j=0; j <= p->length; j++)
  2037. X              {
  2038. X                switch (nibble)
  2039. X                {
  2040. X                  case 0:
  2041. X                  {
  2042. X                    *q=(unsigned char) pixel;
  2043. X                    nibble++;
  2044. X                    break;
  2045. X                  }
  2046. X                  case 1:
  2047. X                  {
  2048. X                    *q|=(unsigned char) (pixel << 2);
  2049. X                    nibble++;
  2050. X                    break;
  2051. X                  }
  2052. X                  case 2:
  2053. X                  {
  2054. X                    *q|=(unsigned char) (pixel << 4);
  2055. X                    nibble++;
  2056. X                    break;
  2057. X                  }
  2058. X                  case 3:
  2059. X                  {
  2060. X                    *q|=(unsigned char) (pixel << 6);
  2061. X                    q++;
  2062. X                    nibble=0;
  2063. X                    break;
  2064. X                  }
  2065. X                }
  2066. X                x++;
  2067. X                if (x == ximage->width)
  2068. X                  {
  2069. X                    x=0;
  2070. X                    nibble=0;
  2071. X                    q+=scanline_pad;
  2072. X                  }
  2073. X              }
  2074. X              p++;
  2075. X            }
  2076. X            break;
  2077. X          }
  2078. X          case 4:
  2079. X          {
  2080. X            register unsigned int
  2081. X              nibble;
  2082. X
  2083. X            /*
  2084. X              Convert to 4 bit color-mapped X image.
  2085. X            */
  2086. X            nibble=0;
  2087. X            for (i=0; i < image->packets; i++)
  2088. X            {
  2089. X              pixel=pixels[p->index] & 0xf;
  2090. X              for (j=0; j <= p->length; j++)
  2091. X              {
  2092. X                switch (nibble)
  2093. X                {
  2094. X                  case 0:
  2095. X                  {
  2096. X                    *q=(unsigned char) pixel;
  2097. X                    nibble++;
  2098. X                    break;
  2099. X                  }
  2100. X                  case 1:
  2101. X                  {
  2102. X                    *q|=(unsigned char) (pixel << 4);
  2103. X                    q++;
  2104. X                    nibble=0;
  2105. X                    break;
  2106. X                  }
  2107. X                }
  2108. X                x++;
  2109. X                if (x == ximage->width)
  2110. X                  {
  2111. X                    x=0;
  2112. X                    nibble=0;
  2113. X                    q+=scanline_pad;
  2114. X                  }
  2115. X              }
  2116. X              p++;
  2117. X            }
  2118. X            break;
  2119. X          }
  2120. X          case 6:
  2121. X          case 8:
  2122. X          {
  2123. X            /*
  2124. X              Convert to 8 bit color-mapped X image.
  2125. X            */
  2126. X            for (i=0; i < image->packets; i++)
  2127. X            {
  2128. X              pixel=pixels[p->index];
  2129. X              for (j=0; j <= p->length; j++)
  2130. X              {
  2131. X                *q++=(unsigned char) pixel;
  2132. X                x++;
  2133. X                if (x == ximage->width)
  2134. X                  {
  2135. X                    x=0;
  2136. X                    q+=scanline_pad;
  2137. X                  }
  2138. X              }
  2139. X              p++;
  2140. X            }
  2141. X            break;
  2142. X          }
  2143. X          default:
  2144. X          {
  2145. X            register int
  2146. X              k;
  2147. X
  2148. X            register unsigned int
  2149. X              bytes_per_pixel;
  2150. X
  2151. X            unsigned char
  2152. X              channel[sizeof(unsigned long)];
  2153. X
  2154. X            /*
  2155. X              Convert to multi-byte color-mapped X image.
  2156. X            */
  2157. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  2158. X            for (i=0; i < image->packets; i++)
  2159. X            {
  2160. X              pixel=pixels[p->index];
  2161. X              for (k=0; k < bytes_per_pixel; k++)
  2162. X              {
  2163. X                channel[k]=(unsigned char) pixel;
  2164. SHAR_EOF
  2165. true || echo 'restore of ImageMagick/X.c failed'
  2166. fi
  2167. echo 'End of ImageMagick part 19'
  2168. echo 'File ImageMagick/X.c is continued in part 20'
  2169. echo 20 > _shar_seq_.tmp
  2170. exit 0
  2171. --
  2172. Dan Heller
  2173. O'Reilly && Associates       Z-Code Software    Comp-sources-x:
  2174. Senior Writer                President          comp-sources-x@uunet.uu.net
  2175. argv@ora.com                 argv@zipcode.com
  2176.