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

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i076:  imagemagic - X11 image processing and display, Part20/38
  4. Message-ID: <1993Jul14.175802.1951@sparky.sterling.com>
  5. X-Md4-Signature: ba04536a88600fc878061d0b4fb6cfad
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 17:58:02 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 76
  13. Archive-name: imagemagic/part20
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.20 (part 20 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/animate.man continued
  21. #
  22. if test ! -r _shar_seq_.tmp; then
  23.     echo 'Please unpack part 1 first!'
  24.     exit 1
  25. fi
  26. (read Scheck
  27.  if test "$Scheck" != 20; then
  28.     echo Please unpack part "$Scheck" next!
  29.     exit 1
  30.  else
  31.     exit 0
  32.  fi
  33. ) < _shar_seq_.tmp || exit 1
  34. if test ! -f _shar_wnt_.tmp; then
  35.     echo 'x - still skipping ImageMagick/animate.man'
  36. else
  37. echo 'x - continuing file ImageMagick/animate.man'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/animate.man' &&
  39. the next image.  The default is to display each image without delay.
  40. .TP 5
  41. .B "-density \fI<width>x<height>
  42. vertical and horizonal density of the image.
  43. X
  44. This option specifies an image density whose interpretation changes
  45. with the type of image.  The default is 72 dots per inch in the
  46. horizonal and vertical direction for Postscript.  Text files default to
  47. 80 characters in width and 60 lines in height.  Use this option to
  48. alter the default density.
  49. .TP 5
  50. .B "-display \fIhost:display[.screen]\fP"
  51. specifies the X server to contact; see \fBX(1)\fP.
  52. .TP 5
  53. .B "-dither"
  54. apply Floyd/Steinberg error diffusion to the image.
  55. X
  56. The basic strategy of dithering is to trade intensity resolution for
  57. spatial resolution by averaging the intensities of several neighboring
  58. pixels.  Images which suffer from severe contouring when reducing colors
  59. can be improved with this option.
  60. X
  61. The \fB-colors\fP or \fB-monochrome\fP option is required for this option
  62. to take effect.
  63. .TP 5
  64. .B "-gamma \fIvalue\fP"
  65. level of gamma correction.
  66. X
  67. The same color image displayed on two different workstations may look
  68. different due to differences in the display monitor.  Use gamma
  69. correction to adjust for this color difference.  Reasonable values
  70. extend from 0.8 to 2.3.
  71. .TP 5
  72. .B "-geometry \fI<width>x<height>{\+-}<x offset>{\+-}<y offset>\fP"
  73. preferred size and location of the image window.  See \fBX(1)\fP for details
  74. about the geometry specification.  By default, the window size is the image
  75. size and the location is choosen by you when it is mapped.
  76. X
  77. If the specified image size is smaller than the actual image size, the
  78. image is first reduced to an integral of the specified image size with
  79. an antialias digital filter.  The image is then scaled to the exact
  80. specified image size with pixel replication.  If the specified image
  81. size is greater than the actual image size, the image is first enlarged
  82. to an integral of the specified image size with bilinear
  83. interpolation.  The image is then scaled to the exact specified image
  84. size with pixel replication.
  85. X
  86. When displaying an image on an X server, \fI<x offset>\fP and
  87. \fI<y offset>\fP is relative to the root window.
  88. X
  89. The equivalent X resource for this option is \fBimageGeometry\fP
  90. (class \fBImageGeometry\fP).  See \fBX RESOURCES\fP for details.
  91. .TP 5
  92. .B "-interlace \fItype\fP"
  93. the type of interlacing scheme: \fBNONE\fP, \fBLINE\fP, or \fBPLANE\fP.
  94. X
  95. This option is used to specify the type of interlacing scheme for raw
  96. image formats such as \fBRGB\fP or \fBYUV\fP.  \fBNONE\fP means do not
  97. interlace (RGBRGBRGBRGBRGBRGB...), \fBLINE\fP uses scanline
  98. interlacing (RRR...GGG...BBB...RRR...GGG...BBB...), and \fBPLANE\fP uses
  99. plane interlacing (RRRRRR...GGGGGG...BBBBBB...).
  100. .TP 5
  101. .B "-map \fItype\fP"
  102. animate images using this Standard Colormap type.
  103. X
  104. Choose from these Standard Colormap types:
  105. X
  106. X    default
  107. X    best
  108. X    red
  109. X    green
  110. X    blue
  111. X    gray
  112. X
  113. The X server must support the Standard Colormap you choose, otherwise an
  114. error occurs.  See \fBXSTDCMAP(1)\fP for one way of creating Standard Colormaps.
  115. .TP 5
  116. .B "-monochrome"
  117. transform the image to black and white.
  118. X
  119. Monochrome images can benefit from error diffusion.  Use \fB-dither\fP with
  120. this option to diffuse the error.
  121. .TP 5
  122. .B "-reflect"
  123. create a "mirror image" by reflecting the image scanlines.
  124. .TP 5
  125. .B "-rotate \fIdegrees\fP"
  126. apply Paeth image rotation to the image.
  127. X
  128. Empty triangles left over from rotating the image are filled with
  129. the color defined by the pixel at location (0,0).
  130. .TP 5
  131. .B "-scale \fI<width factor>x<height factor>\fP"
  132. preferred size factors of the image.
  133. X
  134. This option behaves like \fB-geometry\fP except the width and height values
  135. are relative instead of absolute.  The image size is multiplied by the
  136. width and height factors to obtain the final image dimensions.  If only
  137. one factor is specified, both the width and height factors assume the
  138. value.
  139. X
  140. Factors may be fractional.  To increase the size of an image, use a
  141. scale factor greater than 1.0.  To decrease an image's size, use a
  142. scale factor less than 1.0.  Default is 1.0.
  143. X
  144. The equivalent X resource for this option is \fBscaleGeometry\fP
  145. (class \fBScaleGeometry\fP).  See \fBX RESOURCES\fP for details.
  146. .TP 5
  147. .B "-treedepth \fIvalue\fP"
  148. Normally, this integer value is zero or one.  A zero or one tells
  149. \fBanimate\fP to choose a optimal tree depth for the color reduction
  150. algorithm.
  151. X
  152. An optimal depth generally allows the best representation of the source
  153. image with the fastest computational speed and the least amount of
  154. memory.  However, the default depth is inappropriate for some images.
  155. To assure the best representation, try values between 2 and 8 for this
  156. parameter.  Refer to \fBquantize(9)\fP for more details.
  157. X
  158. The \fB-colors\fP or \fB-monochrome\fP option is required for this option
  159. to take effect.
  160. .TP 5
  161. .B -verbose
  162. print detailed information about the image.
  163. X
  164. This information is printed: image scene number;  image name;  image
  165. size; the image class (\fIDirectClass\fP or \fIPseudoClass\fP);  the total
  166. number of unique colors;  and the number of seconds to read and
  167. transform the image.  Refer to \fBmiff(5)\fP for a description of
  168. the image class.
  169. X
  170. If \fB-colors\fP is also specified, the total unique colors in the image
  171. and color reduction error values are printed.  Refer to \fBquantize(9)\fP
  172. for a description of these values.
  173. .TP 5
  174. .B "-visual \fItype\fP"
  175. animate images using this visual type.
  176. X
  177. Choose from these visual classes:
  178. X
  179. X    StaticGray
  180. X    GrayScale
  181. X    StaticColor
  182. X    PseudoColor
  183. X    TrueColor
  184. X    DirectColor
  185. X    default
  186. X    \fIvisual id\fP
  187. X
  188. The X server must support the visual you choose, otherwise an error occurs.
  189. If a visual is not specified, the visual class that can display the most
  190. simultaneous colors on the default screen is choosen.
  191. .PP
  192. In addition to those listed above, you can specify these standard X
  193. resources as command line options:  \fB-background\fP,
  194. \fB-bordercolor\fP, \fB-borderwidth\fP,  \fB-font\fP,
  195. \fB-foreground\fP, \fB-iconGeometry\fP, \fB-iconic\fP, \fB-name\fP, or
  196. \fB-title\fP.  See \fBX RESOURCES\fP for details.
  197. .PP
  198. Any option you specify on the command line remains in effect until it is
  199. explicitly changed by specifying the option again with a different effect.
  200. For example, to animate two images, the first with 32 colors and the
  201. second with only 16 colors, use:
  202. .PP
  203. X     animate -colors 32 cockatoo.1 -colors 16 cockatoo.2
  204. .PP
  205. Change \fI-\fP to \fI\+\fP in any option above to reverse its effect.
  206. For example, specify \fB\+dither\fP to not apply error diffusion to an image.
  207. .PP
  208. By default, the image format is determined by its magic number. To
  209. specify a particular image format, precede the filename with an image
  210. format name and a colon (i.e. ps:image) or specify the image type as
  211. the filename suffix (i.e. image.ps).  See \fBconvert(1)\fP for a list
  212. of valid image formats.
  213. .PP
  214. When you specify \fBX\fP as your image type, the filename has special
  215. meaning.  It specifies an X window by id, name, or \fBroot\fP.  If no
  216. filename is specified, the window is selected by clicking the mouse in
  217. the desired window.
  218. .PP
  219. Specify \fIfile\fP as \fI-\fP for standard input, If \fIfile\fP has the
  220. extension \fB.Z\fP or \fB.gz\fP, the file is uncompressed with
  221. \fBuncompress\fP or \fBgunzip\fP respectively.
  222. .PP
  223. Image filenames may appear in any order on the command line if the image
  224. format is \fIMIFF\fP (refer to \fBmiff(5)\fP and the \fBscene\fP keyword is
  225. specified in the image.  Otherwise the images will display in the order
  226. they appear on the command line.
  227. .SH BUTTONS
  228. .TP 5
  229. .B "1"
  230. Press and drag to select a command from a pop-up menu.  Choose from
  231. these commands:
  232. X
  233. X    Play
  234. X    Step
  235. X    Repeat
  236. X    Auto Reverse
  237. X    Slower
  238. X    Faster
  239. X    Forward
  240. X    Reverse
  241. X    Image Info
  242. X    Quit
  243. .SH KEYBOARD ACCELERATORS
  244. .TP 5
  245. .B "p"
  246. Press to animate the sequence of images.
  247. .TP 5
  248. .B "s"
  249. Press to display the next image in the sequence.
  250. .TP 5
  251. .B "."
  252. Press to continually display the sequence of images.
  253. .TP 5
  254. .B "a"
  255. Press to automatically reverse the sequence of images.
  256. .TP 5
  257. .B "<"
  258. Press to slow the display of the images.  Refer to \fB-delay\fP for more
  259. information.
  260. .TP 5
  261. .B ">"
  262. Press to speed-up the display of the images.  Refer to \fB-delay\fP for more
  263. information.
  264. .TP 5
  265. .B "f"
  266. Press to animate in the forward direction.
  267. .TP 5
  268. .B "r"
  269. Press to animate in the reverse direction.
  270. .TP 5
  271. .B "i"
  272. Press to display information about the image.  Press any key or button
  273. to erase the information.
  274. X
  275. This information is printed: image name;  image size; and the total
  276. number of unique colors in the image.
  277. .TP 5
  278. .B "q"
  279. Press to discard all images and exit program.
  280. .SH "X RESOURCES"
  281. \fBanimate\fP options can appear on the command line or in your X
  282. resource file.  Options on the command line supersede values specified
  283. in your X resource file.  See \fBX(1)\fP for more information on X
  284. resources.
  285. X
  286. All \fBanimate\fP options have a corresponding X resource.  In addition,
  287. the \fBanimate\fP program uses the following X resources:
  288. .TP 5
  289. .B background (\fPclass\fB Background)
  290. Specifies the preferred color to use for the image window background.  The
  291. default is black.
  292. .TP 5
  293. .B borderColor (\fPclass\fB BorderColor)
  294. Specifies the preferred color to use for the image window border.  The
  295. default is white.
  296. .TP 5
  297. .B borderWidth (\fPclass\fB BorderWidth)
  298. Specifies the width in pixels of the image window border.  The default is 2.
  299. .TP 5
  300. .B font (\fPclass\fB Font)
  301. Specifies the name of the preferred font to use when displaying text
  302. within the image window.  The default is \fI/g9x15\fP, \fIfixed\fP, or
  303. \fI/g6x13\fP determined by the image window size.
  304. .TP 5
  305. .B foreground (\fPclass\fB Foreground)
  306. Specifies the preferred color to use for text within the image window.  The
  307. default is white.
  308. .TP 5
  309. .B iconGeometry (\fPclass\fB IconGeometry)
  310. Specifies the preferred size and position of the application when
  311. iconified.  It is not necessarily obeyed by all window managers.
  312. .TP 5
  313. .B iconic (\fPclass\fB Iconic)
  314. This resource indicates that you would prefer that the application's
  315. windows initially not be visible as if the windows had be immediately
  316. iconified by you.  Window managers may choose not to honor the
  317. application's request.
  318. .TP 5
  319. .B name (\fPclass\fB Name)
  320. This resource specifies the name under which resources for the
  321. application should be found.  This resource is useful in shell aliases to
  322. distinguish between invocations of an application, without resorting to
  323. creating links to alter the executable file name.  The default is the
  324. application name.
  325. .TP 5
  326. .B title (\fPclass\fB Title)
  327. This resource specifies the title to be used for the image window.  This
  328. information is sometimes used by a window manager to provide some sort
  329. of header identifying the window.  The default is the image file name.
  330. .SH ENVIRONMENT
  331. .TP 5
  332. .B display
  333. To get the default host, display number, and screen.
  334. .SH SEE ALSO
  335. .B
  336. display(1), import(1), mogrify(1), convert(1), quantize(9), miff(5), X(1), xstdcmap(1), compress(1), miff(5)
  337. .SH COPYRIGHT
  338. Copyright 1993 E. I. du Pont de Nemours & Company
  339. .PP
  340. Permission to use, copy, modify, distribute, and sell this software and
  341. its documentation for any purpose is hereby granted without fee,
  342. provided that the above copyright notice appear in all copies and that
  343. both that copyright notice and this permission notice appear in
  344. supporting documentation, and that the name of E. I. du Pont de Nemours
  345. & Company not be used in advertising or publicity pertaining to
  346. distribution of the software without specific, written prior
  347. permission.  E. I. du Pont de Nemours & Company makes no representations
  348. about the suitability of this software for any purpose.  It is provided
  349. "as is" without express or implied warranty.
  350. .PP
  351. E. I. du Pont de Nemours & Company disclaims all warranties with regard
  352. to this software, including all implied warranties of merchantability
  353. and fitness, in no event shall E. I. du Pont de Nemours & Company be
  354. liable for any special, indirect or consequential damages or any
  355. damages whatsoever resulting from loss of use, data or profits, whether
  356. in an action of contract, negligence or other tortious action, arising
  357. out of or in connection with the use or performance of this software.
  358. .SH ACKNOWLEDGEMENTS
  359. The MIT X Consortium for making network transparent graphics a reality.
  360. .PP
  361. Michael Halle, Spatial Imaging Group at MIT, for the initial
  362. implementation of Alan Paeth's image rotation algorithm.
  363. .PP
  364. David Pensak, E. I. du Pont de Nemours & Company, for providing a
  365. computing environment that made this program possible.
  366. .PP
  367. Paul Raveling, USC Information Sciences Institute, for the original
  368. idea of using space subdivision for the color reduction algorithm.
  369. .SH AUTHORS
  370. John Cristy, E.I. du Pont de Nemours & Company Incorporated
  371. SHAR_EOF
  372. echo 'File ImageMagick/animate.man is complete' &&
  373. chmod 0644 ImageMagick/animate.man ||
  374. echo 'restore of ImageMagick/animate.man failed'
  375. Wc_c="`wc -c < 'ImageMagick/animate.man'`"
  376. test 17246 -eq "$Wc_c" ||
  377.     echo 'ImageMagick/animate.man: original size 17246, current size' "$Wc_c"
  378. rm -f _shar_wnt_.tmp
  379. fi
  380. # ============= ImageMagick/display.h ==============
  381. if test -f 'ImageMagick/display.h' -a X"$1" != X"-c"; then
  382.     echo 'x - skipping ImageMagick/display.h (File already exists)'
  383.     rm -f _shar_wnt_.tmp
  384. else
  385. > _shar_wnt_.tmp
  386. echo 'x - extracting ImageMagick/display.h (Text)'
  387. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/display.h' &&
  388. /*
  389. X  Include declarations
  390. */
  391. #include <stdio.h>
  392. #if defined(__STDC__) || defined(sgi) || defined(_AIX)
  393. #include <stdlib.h>
  394. #include <unistd.h>
  395. #else
  396. #ifndef vms
  397. #include <malloc.h>
  398. #include <memory.h>
  399. #endif
  400. #endif
  401. #include <ctype.h>
  402. #include <string.h>
  403. #include <time.h>
  404. #include <math.h>
  405. #include <sys/types.h>
  406. #include <sys/stat.h>
  407. #undef index
  408. X
  409. /*
  410. X  Define declarations for the Display program.
  411. */
  412. #if __STDC__ || defined(sgi) || defined(_AIX)
  413. #define _Declare(formal_parameters) formal_parameters
  414. #else
  415. #define const 
  416. #define _Declare(formal_parameters) ()
  417. #endif
  418. #define DownShift(x) ((int) ((x)+(1L << 15)) >> 16)
  419. #define False  0
  420. #define Max(x,y)  (((x) > (y)) ? (x) : (y))
  421. #define Min(x,y)  (((x) < (y)) ? (x) : (y))
  422. #define MinInfoSize (1 << 18)
  423. #define True  1
  424. #define UpShift(x) ((x) << 16)
  425. #define UpShifted(x) ((int) ((x)*(1L << 16)+0.5))
  426. #define Warning(message,qualifier)  \
  427. {  \
  428. X  (void) fprintf(stderr,"%s: %s",client_name,message);  \
  429. X  if (qualifier != (char *) NULL)  \
  430. X    (void) fprintf(stderr," (%s)",qualifier);  \
  431. X  (void) fprintf(stderr,".\n");  \
  432. }
  433. #ifdef vms
  434. #define pclose(file)  exit(1)
  435. #define popen(command,mode)  exit(1)
  436. #define unlink(file)  remove(file)
  437. #endif
  438. X
  439. /*
  440. X  Variable declarations.
  441. */
  442. #ifndef lint
  443. static char 
  444. X  Version[]="@(#)ImageMagick 2.3.3 93/07/01 cristy@dupont.com";
  445. #endif
  446. SHAR_EOF
  447. chmod 0644 ImageMagick/display.h ||
  448. echo 'restore of ImageMagick/display.h failed'
  449. Wc_c="`wc -c < 'ImageMagick/display.h'`"
  450. test 1328 -eq "$Wc_c" ||
  451.     echo 'ImageMagick/display.h: original size 1328, current size' "$Wc_c"
  452. rm -f _shar_wnt_.tmp
  453. fi
  454. # ============= ImageMagick/compress.c ==============
  455. if test -f 'ImageMagick/compress.c' -a X"$1" != X"-c"; then
  456.     echo 'x - skipping ImageMagick/compress.c (File already exists)'
  457.     rm -f _shar_wnt_.tmp
  458. else
  459. > _shar_wnt_.tmp
  460. echo 'x - extracting ImageMagick/compress.c (Text)'
  461. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/compress.c' &&
  462. /*
  463. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  464. %                                                                             %
  465. %                                                                             %
  466. %                                                                             %
  467. %           CCCC   OOO   M   M  PPPP   RRRR   EEEEE   SSSSS  SSSSS            %
  468. %          C      O   O  MM MM  P   P  R   R  E       SS     SS               %
  469. %          C      O   O  M M M  PPPP   RRRR   EEE      SSS    SSS             %
  470. %          C      O   O  M   M  P      R R    E          SS     SS            %
  471. %           CCCC   OOO   M   M  P      R  R   EEEEE   SSSSS  SSSSS            %
  472. %                                                                             %
  473. %                                                                             %
  474. %                  Image Compression/Decompression Coders                     %
  475. %                                                                             %
  476. %                                                                             %
  477. %                                                                             %
  478. %                           Software Design                                   %
  479. %                             John Cristy                                     %
  480. %                              May  1993                                      %
  481. %                                                                             %
  482. %                                                                             %
  483. %  Copyright 1993 E. I. du Pont de Nemours & Company                          %
  484. %                                                                             %
  485. %  Permission to use, copy, modify, distribute, and sell this software and    %
  486. %  its documentation for any purpose is hereby granted without fee,           %
  487. %  provided that the above Copyright notice appear in all copies and that     %
  488. %  both that Copyright notice and this permission notice appear in            %
  489. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  490. %  & Company not be used in advertising or publicity pertaining to            %
  491. %  distribution of the software without specific, written prior               %
  492. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  493. %  about the suitability of this software for any purpose.  It is provided    %
  494. %  "as is" without express or implied warranty.                               %
  495. %                                                                             %
  496. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  497. %  to this software, including all implied warranties of merchantability      %
  498. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  499. %  liable for any special, indirect or consequential damages or any           %
  500. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  501. %  in an action of contract, negligence or other tortious action, arising     %
  502. %  out of or in connection with the use or performance of this software.      %
  503. %                                                                             %
  504. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  505. %
  506. %
  507. %
  508. */
  509. X
  510. /*
  511. X  Include declarations.
  512. */
  513. #include "display.h"
  514. #include "image.h"
  515. #include "utility.h"
  516. /*
  517. X  Define declarations.
  518. */
  519. #define LowerBound  0
  520. #define MaxContextStates  121
  521. #define MinimumIntervalD  (unsigned short) 0xf000  /* ~-0.75 */
  522. #define MinimumIntervalE  (unsigned short) 0x1000  /* ~0.75 */
  523. #define No  0
  524. #define UpperBound  2
  525. #define Yes  1
  526. /*
  527. X  State classification.
  528. */
  529. #define ZeroState  0
  530. #define SmallPostitiveState  1
  531. #define SmallNegativeState  2
  532. #define LargePostitiveState  3
  533. #define LargeNegativeState  4
  534. /*
  535. X  Typedef declarations.
  536. */
  537. typedef struct _ScanlinePacket
  538. {
  539. X  unsigned char
  540. X    pixel;
  541. X
  542. X  int
  543. X    state;
  544. } ScanlinePacket;
  545. /*
  546. X  Initialized declarations.
  547. */
  548. static int decrement_less_probable[]=
  549. {
  550. X  0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
  551. X  2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2
  552. };
  553. X
  554. static int increment_more_probable[]=
  555. {
  556. X  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  557. X  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
  558. };
  559. X
  560. static int more_probable_exchange[]=
  561. {
  562. X  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  563. X  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  564. };
  565. X
  566. static int statistics[][5]=
  567. {
  568. X   0,  4,  8, 12, 16,
  569. X  20, 24, 28, 32, 36,
  570. X  40, 44, 48, 52, 56,
  571. X  60, 64, 68, 72, 76,
  572. X  80, 84, 88, 92, 96
  573. };
  574. X
  575. static unsigned short probability[]=
  576. {
  577. X  0x0ac1, 0x0a81, 0x0a01, 0x0901, 0x0701, 0x0681,
  578. X  0x0601, 0x0501, 0x0481, 0x0441, 0x0381, 0x0301,
  579. X  0x02c1, 0x0281, 0x0241, 0x0181, 0x0121, 0x00e1,
  580. X  0x00a1, 0x0071, 0x0059, 0x0053, 0x0027, 0x0017,
  581. X  0x0013, 0x000b, 0x0007, 0x0005, 0x0003, 0x0001
  582. };
  583. /*
  584. X  Declarations and initializations for predictive arithimetic coder.
  585. */
  586. static int
  587. X  code,
  588. X  less_probable[MaxContextStates],
  589. X  more_probable[MaxContextStates],
  590. X  probability_estimate[MaxContextStates];
  591. X
  592. static unsigned char
  593. X  *q;
  594. X
  595. static unsigned short
  596. X  interval;
  597. X
  598. /*
  599. X  External declarations.
  600. */
  601. extern char
  602. X  *client_name;
  603. X
  604. /*
  605. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  606. %                                                                             %
  607. %                                                                             %
  608. %                                                                             %
  609. %   B M P D e c o d e I m a g e                                               %
  610. %                                                                             %
  611. %                                                                             %
  612. %                                                                             %
  613. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  614. %
  615. %  Function BMPDecodeImage unpacks the packed image pixels into
  616. %  runlength-encoded pixel packets.
  617. %
  618. %  The format of the BMPDecodeImage routine is:
  619. %
  620. %      status=BMPDecodeImage(compressed_pixels,pixels,number_columns,
  621. %        number_rows)
  622. %
  623. %  A description of each parameter follows:
  624. %
  625. %    o status:  Function BMPDecodeImage returns True if all the pixels are
  626. %      uncompressed without error, otherwise False.
  627. %
  628. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  629. %      pixel data.
  630. %
  631. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  632. %      the uncompression process.  The number of bytes in this array
  633. %      must be at least equal to the number columns times the number of rows
  634. %      of the source pixels.
  635. %
  636. %    o number_columns:  An integer value that is the number of columns or
  637. %      width in pixels of your source image.
  638. %
  639. %    o number_rows:  An integer value that is the number of rows or
  640. %      heigth in pixels of your source image.
  641. %
  642. %
  643. */
  644. unsigned int BMPDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  645. unsigned char
  646. X  *compressed_pixels,
  647. X  *pixels;
  648. X
  649. unsigned int
  650. X  number_columns,
  651. X  number_rows;
  652. {
  653. X  register int
  654. X    i,
  655. X    x,
  656. X    y;
  657. X
  658. X  register unsigned char
  659. X    *p,
  660. X    *q;
  661. X
  662. X  unsigned char
  663. X    count,
  664. X    byte;
  665. X
  666. X  p=compressed_pixels;
  667. X  q=pixels;
  668. X  x=0;
  669. X  for (y=0; y < number_rows; )
  670. X  {
  671. X    count=(*p++);
  672. X    if (count != 0)
  673. X      {
  674. X        /*
  675. X          Encoded mode.
  676. X        */
  677. X        byte=(*p++);
  678. X        for (i=0; i < (int) count; i++)
  679. X        {
  680. X          *q++=byte;
  681. X          x++;
  682. X        }
  683. X      }
  684. X    else
  685. X      {
  686. X        /*
  687. X          Escape mode.
  688. X        */
  689. X        count=(*p++);
  690. X        if (count == 0x01)
  691. X          return(True);
  692. X        switch (count)
  693. X        {
  694. X          case 0x00:
  695. X          {
  696. X            /*
  697. X              End of line.
  698. X            */
  699. X            x=0;
  700. X            y++;
  701. X            q=pixels+y*number_columns;
  702. X            break;
  703. X          }
  704. X          case 0x02:
  705. X          {
  706. X            /*
  707. X              Delta mode.
  708. X            */
  709. X            x+=(*p++);
  710. X            y+=(*p++);
  711. X            q=pixels+y*number_columns+x;
  712. X            break;
  713. X          }
  714. X          default:
  715. X          {
  716. X            /*
  717. X              Absolute mode.
  718. X            */
  719. X            for (i=0; i < (int) count; i++)
  720. X            {
  721. X              byte=(*p++);
  722. X              *q++=byte;
  723. X              x++;
  724. X            }
  725. X            if (count & 0x01)
  726. X              p++;  /* Read pad byte */
  727. X            break;
  728. X          }
  729. X        }
  730. X     }
  731. X  }
  732. X  return(False);
  733. }
  734. X
  735. /*
  736. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  737. %                                                                             %
  738. %                                                                             %
  739. %                                                                             %
  740. %   D e c o d e                                                               %
  741. %                                                                             %
  742. %                                                                             %
  743. %                                                                             %
  744. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  745. %
  746. %  Function Decode uncompresses a string.
  747. %
  748. %  The format of the Decode routine is:
  749. %
  750. %      Decode(state,decision)
  751. %
  752. %  A description of each parameter follows:
  753. %
  754. %    o state:  An integer value representing the current state.
  755. %
  756. %    o decision:  A pointer to an integer.  The output of the binary
  757. %      decision (Yes/No) is returned in this value.
  758. %
  759. %
  760. */
  761. static void Decode(state,decision)
  762. register int
  763. X  state,
  764. X  *decision;
  765. {
  766. X  interval+=probability[probability_estimate[state]];
  767. X  if (((code >> 16) & 0xffff) < ((int) interval))
  768. X    {
  769. X      code-=(interval << 16);
  770. X      interval=(-probability[probability_estimate[state]]);
  771. X      *decision=less_probable[state];
  772. X    }
  773. X  else
  774. X    {
  775. X      *decision=more_probable[state];
  776. X      if (interval <= MinimumIntervalD)
  777. X        return;
  778. X    }
  779. X  do
  780. X  {
  781. X    if ((code & 0xff) == 0)
  782. X      {
  783. X        code&=0xffff0000;
  784. X        if ((*q++) == 0xff)
  785. X          code+=((int) (*q) << 9)+0x02;
  786. X        else
  787. X          code+=((int) (*q) << 8)+0x01;
  788. X      }
  789. X    interval<<=1;
  790. X    code<<=1;
  791. X  } while (interval > MinimumIntervalD);
  792. X  /*
  793. X    Update probability estimates.
  794. X  */
  795. X  if (*decision == more_probable[state])
  796. X    probability_estimate[state]+=
  797. X      increment_more_probable[probability_estimate[state]];
  798. X  else
  799. X    probability_estimate[state]-=
  800. X      decrement_less_probable[probability_estimate[state]];
  801. X  if (more_probable_exchange[probability_estimate[state]] != 0)
  802. X    {
  803. X      /*
  804. X        Exchange sense of most probable and least probable.
  805. X      */
  806. X      less_probable[state]=more_probable[state];
  807. X      more_probable[state]=1-more_probable[state];
  808. X    }
  809. }
  810. X
  811. /*
  812. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  813. %                                                                             %
  814. %                                                                             %
  815. %                                                                             %
  816. %   E n c o d e                                                               %
  817. %                                                                             %
  818. %                                                                             %
  819. %                                                                             %
  820. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  821. %
  822. %  Function Encode generate compressed data string by encoding yes-no decision
  823. %  given state s.
  824. %
  825. %  The format of the Encode routine is:
  826. %
  827. %      Encode(state,decision)
  828. %
  829. %  A description of each parameter follows:
  830. %
  831. %    o state:  An integer value representing the current state.
  832. %
  833. %    o decision:  An integer value representing a binary decision.
  834. %
  835. %
  836. */
  837. static void Encode(state,decision)
  838. register int
  839. X  state,
  840. X  decision;
  841. {
  842. X  /*
  843. X    Test on "most-probable-symbol" for state s(more_probable[state])
  844. X  */
  845. X  interval-=probability[probability_estimate[state]];
  846. X  if (more_probable[state] != decision)
  847. X    {
  848. X      code-=interval;
  849. X      interval=probability[probability_estimate[state]];
  850. X    }
  851. X  else
  852. X    if (interval >= MinimumIntervalE)
  853. X      return;
  854. X  /*
  855. X    Encoder renormalization.
  856. X  */
  857. X  do
  858. X  {
  859. X    interval<<=1;
  860. X    if (code >= 0)
  861. X      code<<=1;
  862. X    else
  863. X      {
  864. X        /*
  865. X          Shift unsigned char of data from Code register to compressed string.
  866. X        */
  867. X        code<<=1;
  868. X        if (code > 0)
  869. X          {
  870. X            /*
  871. X              Add eight bits from Code register to compressed data string.
  872. X            */
  873. X            (*q++)--;
  874. X            *q=(unsigned char) (code >> 16);
  875. X            code&=0x0000ffff;
  876. X            code|=0x01800000;
  877. X          }
  878. X        else
  879. X          {
  880. X            code&=0x01ffffff;
  881. X            if ((int) interval > code)
  882. X              {
  883. X                /*
  884. X                  Add eight bits from Code register to compressed data string.
  885. X                */
  886. X                (*q++)--;
  887. X                *q=0xff;
  888. X                code|=0x01810000;
  889. X              }
  890. X            else
  891. X              if ((*q++) == 0xff)
  892. X                {
  893. X                  /*
  894. X                    Add seven bits from Code register plus one stuffed bit to
  895. X                    compressed data string.
  896. X                  */
  897. X                  *q=(unsigned char) (code >> 17);
  898. X                  code&=0x0001ffff;
  899. X                  code|=0x03000000;
  900. X                }
  901. X              else
  902. X                {
  903. X                  /*
  904. X                    Add eight bits from Code register to compressed data string.
  905. X                  */
  906. X                  *q=(unsigned char) (code >> 16);
  907. X                  code&=0x0000ffff;
  908. X                  code|=0x01800000;
  909. X                }
  910. X          }
  911. X      }
  912. X  } while (interval < MinimumIntervalE);
  913. X  /*
  914. X    Update probability estimates
  915. X  */
  916. X  if (decision == more_probable[state])
  917. X    probability_estimate[state]+=
  918. X      increment_more_probable[probability_estimate[state]];
  919. X  else
  920. X    probability_estimate[state]-=
  921. X      decrement_less_probable[probability_estimate[state]];
  922. X  if (more_probable_exchange[probability_estimate[state]] != 0)
  923. X    more_probable[state]=1-more_probable[state];
  924. }
  925. X
  926. /*
  927. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  928. %                                                                             %
  929. %                                                                             %
  930. %                                                                             %
  931. %   F l u s h                                                                 %
  932. %                                                                             %
  933. %                                                                             %
  934. %                                                                             %
  935. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  936. %
  937. %  Function Flush flushes the final bits of data from the Code register to the
  938. %  compressed data string.
  939. %
  940. %  The format of the Flush routine is:
  941. %
  942. %      Flush()
  943. %
  944. %
  945. */
  946. static void Flush()
  947. {
  948. X  register int
  949. X    extra_bits;
  950. X
  951. X  code-=interval;
  952. X  extra_bits=24;
  953. X  extra_bits--;
  954. X  while (code >= 0)
  955. X  {
  956. X    code<<=1;
  957. X    extra_bits--;
  958. X  }
  959. X  code<<=1;
  960. X  if (code > 0)
  961. X    (*q)--;
  962. X  /*
  963. X    Add the final compressed data unsigned chars to the compressed data string.
  964. X  */
  965. X  do
  966. X  {
  967. X    if ((*q++) == 0xff)
  968. X      {
  969. X        /*
  970. X          Add seven bits of data plus one stuffed bit to the compressed data
  971. X          string during final Flush of Code register.
  972. X        */
  973. X        *q=(unsigned char) (code >> 17);
  974. X        code&=0x0001ffff;
  975. X        code<<=7;
  976. X        extra_bits-=7;
  977. X      }
  978. X    else
  979. X      {
  980. X        /*
  981. X           Add eight bits of data to the compressed data string during final
  982. X           flush of Code register.
  983. X        */
  984. X        *q=(unsigned char) (code >> 16);
  985. X        code&=0x0000ffff;
  986. X        code<<=8;
  987. X        extra_bits-=8;
  988. X      }
  989. X  } while (extra_bits > 0);
  990. X  q++;
  991. }
  992. X
  993. /*
  994. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  995. %                                                                             %
  996. %                                                                             %
  997. %                                                                             %
  998. %   H u f f m a n E n c o d e I m a g e                                       %
  999. %                                                                             %
  1000. %                                                                             %
  1001. %                                                                             %
  1002. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1003. %
  1004. %  Function HuffmanEncodeImage compresses an image via Huffman-coding.
  1005. %
  1006. %  The format of the HuffmanEncodeImage routine is:
  1007. %
  1008. %      status=HuffmanEncodeImage(image)
  1009. %
  1010. %  A description of each parameter follows:
  1011. %
  1012. %    o status:  Function HuffmanEncodeImage returns True if all the pixels are
  1013. %      compressed without error, otherwise False.
  1014. %
  1015. %    o image: The address of a structure of type Image.
  1016. %
  1017. %
  1018. */
  1019. unsigned int HuffmanEncodeImage(image)
  1020. Image
  1021. X  *image;
  1022. {
  1023. #define HuffmanOutputCode(te)  \
  1024. {  \
  1025. X  mask=1 << (entry->length-1);  \
  1026. X  while (mask)   \
  1027. X  {  \
  1028. X    if (entry->code & mask)  \
  1029. X      OutputBit(1)  \
  1030. X    else  \
  1031. X      OutputBit(0);  \
  1032. X    mask>>=1;  \
  1033. X  }  \
  1034. }
  1035. X
  1036. #define OutputBit(count)  \
  1037. {  \
  1038. X  if(count > 0)  \
  1039. X    byte=byte | bit;  \
  1040. X  bit>>=1;  \
  1041. X  if ((bit & 0xff) == 0)   \
  1042. X    {  \
  1043. X      (void) fputc((char) byte,image->file);  \
  1044. X      byte=0;  \
  1045. X      bit=0x80;  \
  1046. X    }  \
  1047. }
  1048. X
  1049. X  typedef struct HuffmanTable
  1050. X  {
  1051. X    int
  1052. X      id,
  1053. X      code,
  1054. X      length,
  1055. X      count;
  1056. X  } HuffmanTable;
  1057. X
  1058. X  static HuffmanTable MBTable[]=
  1059. X  {
  1060. X    { 26, 0x0f, 10, 64 }, { 26, 0xc8, 12, 128 }, { 26, 0xc9, 12, 192 },
  1061. X    { 26, 0x5b, 12, 256 }, { 26, 0x33, 12, 320 }, { 26, 0x34, 12, 384 },
  1062. X    { 26, 0x35, 12, 448 }, { 26, 0x6c, 13, 512 }, { 26, 0x6d, 13, 576 },
  1063. X    { 26, 0x4a, 13, 640 }, { 26, 0x4b, 13, 704 }, { 26, 0x4c, 13, 768 },
  1064. X    { 26, 0x4d, 13, 832 }, { 26, 0x72, 13, 896 }, { 26, 0x73, 13, 960 },
  1065. X    { 26, 0x74, 13, 1024 }, { 26, 0x75, 13, 1088 }, { 26, 0x76, 13, 1152 },
  1066. X    { 26, 0x77, 13, 1216 }, { 26, 0x52, 13, 1280 }, { 26, 0x53, 13, 1344 },
  1067. X    { 26, 0x54, 13, 1408 }, { 26, 0x55, 13, 1472 }, { 26, 0x5a, 13, 1536 },
  1068. X    { 26, 0x5b, 13, 1600 }, { 26, 0x64, 13, 1664 }, { 26, 0x65, 13, 1728 },
  1069. X  };
  1070. X
  1071. X  static HuffmanTable MWTable[]=
  1072. X  {
  1073. X    { 24, 0x1b, 5, 64 }, { 24, 0x12, 5, 128 }, { 24, 0x17, 6, 192 },
  1074. X    { 24, 0x37, 7, 256 }, { 24, 0x36, 8, 320 }, { 24, 0x37, 8, 384 },
  1075. X    { 24, 0x64, 8, 448 }, { 24, 0x65, 8, 512 }, { 24, 0x68, 8, 576 },
  1076. X    { 24, 0x67, 8, 640 }, { 24, 0xcc, 9, 704 }, { 24, 0xcd, 9, 768 },
  1077. X    { 24, 0xd2, 9, 832 }, { 24, 0xd3, 9, 896 }, { 24, 0xd4, 9, 960 },
  1078. X    { 24, 0xd5, 9, 1024 }, { 24, 0xd6, 9, 1088 }, { 24, 0xd7, 9, 1152 },
  1079. X    { 24, 0xd8, 9, 1216 }, { 24, 0xd9, 9, 1280 }, { 24, 0xda, 9, 1344 },
  1080. X    { 24, 0xdb, 9, 1408 }, { 24, 0x98, 9, 1472 }, { 24, 0x99, 9, 1536 },
  1081. X    { 24, 0x9a, 9, 1600 }, { 24, 0x18, 6, 1664 }, { 24, 0x9b, 9, 1728 },
  1082. X  };
  1083. X
  1084. X  static HuffmanTable TBTable[]=
  1085. X  {
  1086. X    { 25, 0x37, 10, 0 }, { 25, 0x02, 3, 1 }, { 25, 0x03, 2, 2 },
  1087. X    { 25, 0x02, 2, 3 }, { 25, 0x03, 3, 4 }, { 25, 0x03, 4, 5 },
  1088. X    { 25, 0x02, 4, 6 }, { 25, 0x03, 5, 7 }, { 25, 0x05, 6, 8 },
  1089. X    { 25, 0x04, 6, 9 }, { 25, 0x04, 7, 10 }, { 25, 0x05, 7, 11 },
  1090. X    { 25, 0x07, 7, 12 }, { 25, 0x04, 8, 13 }, { 25, 0x07, 8, 14 },
  1091. X    { 25, 0x18, 9, 15 }, { 25, 0x17, 10, 16 }, { 25, 0x18, 10, 17 },
  1092. X    { 25, 0x8, 10, 18 }, { 25, 0x67, 11, 19 }, { 25, 0x68, 11, 20 },
  1093. X    { 25, 0x6c, 11, 21 }, { 25, 0x37, 11, 22 }, { 25, 0x28, 11, 23 },
  1094. X    { 25, 0x17, 11, 24 }, { 25, 0x18, 11, 25 }, { 25, 0xca, 12, 26 },
  1095. X    { 25, 0xcb, 12, 27 }, { 25, 0xcc, 12, 28 }, { 25, 0xcd, 12, 29 },
  1096. X    { 25, 0x68, 12, 30 }, { 25, 0x69, 12, 31 }, { 25, 0x6a, 12, 32 },
  1097. X    { 25, 0x6b, 12, 33 }, { 25, 0xd2, 12, 34 }, { 25, 0xd3, 12, 35 },
  1098. X    { 25, 0xd4, 12, 36 }, { 25, 0xd5, 12, 37 }, { 25, 0xd6, 12, 38 },
  1099. X    { 25, 0xd7, 12, 39 }, { 25, 0x6c, 12, 40 }, { 25, 0x6d, 12, 41 },
  1100. X    { 25, 0xda, 12, 42 }, { 25, 0xdb, 12, 43 }, { 25, 0x54, 12, 44 },
  1101. X    { 25, 0x55, 12, 45 }, { 25, 0x56, 12, 46 }, { 25, 0x57, 12, 47 },
  1102. X    { 25, 0x64, 12, 48 }, { 25, 0x65, 12, 49 }, { 25, 0x52, 12, 50 },
  1103. X    { 25, 0x53, 12, 51 }, { 25, 0x24, 12, 52 }, { 25, 0x37, 12, 53 },
  1104. X    { 25, 0x38, 12, 54 }, { 25, 0x27, 12, 55 }, { 25, 0x28, 12, 56 },
  1105. X    { 25, 0x58, 12, 57 }, { 25, 0x59, 12, 58 }, { 25, 0x2b, 12, 59 },
  1106. X    { 25, 0x2c, 12, 60 }, { 25, 0x5a, 12, 61 }, { 25, 0x66, 12, 62 },
  1107. X    { 25, 0x67, 12, 63 },
  1108. X  };
  1109. X
  1110. X  static HuffmanTable TWTable[]=
  1111. X  {
  1112. X    { 23, 0x35, 8, 0 }, { 23, 0x07, 6, 1 }, { 23, 0x07, 4, 2 },
  1113. X    { 23, 0x08, 4, 3 }, { 23, 0x0b, 4, 4 }, { 23, 0x0c, 4, 5 },
  1114. X    { 23, 0x0e, 4, 6 }, { 23, 0x0f, 4, 7 }, { 23, 0x13, 5, 8 },
  1115. X    { 23, 0x14, 5, 9 }, { 23, 0x07, 5, 10 }, { 23, 0x08, 5, 11 },
  1116. X    { 23, 0x08, 6, 12 }, { 23, 0x03, 6, 13 }, { 23, 0x34, 6, 14 },
  1117. X    { 23, 0x35, 6, 15 }, { 23, 0x2a, 6, 16 }, { 23, 0x2b, 6, 17 },
  1118. X    { 23, 0x27, 7, 18 }, { 23, 0x0c, 7, 19 }, { 23, 0x08, 7, 20 },
  1119. X    { 23, 0x17, 7, 21 }, { 23, 0x03, 7, 22 }, { 23, 0x04, 7, 23 },
  1120. X    { 23, 0x28, 7, 24 }, { 23, 0x2b, 7, 25 }, { 23, 0x13, 7, 26 },
  1121. X    { 23, 0x24, 7, 27 }, { 23, 0x18, 7, 28 }, { 23, 0x02, 8, 29 },
  1122. X    { 23, 0x03, 8, 30 }, { 23, 0x1a, 8, 31 }, { 23, 0x1b, 8, 32 },
  1123. X    { 23, 0x12, 8, 33 }, { 23, 0x13, 8, 34 }, { 23, 0x14, 8, 35 },
  1124. X    { 23, 0x15, 8, 36 }, { 23, 0x16, 8, 37 }, { 23, 0x17, 8, 38 },
  1125. X    { 23, 0x28, 8, 39 }, { 23, 0x29, 8, 40 }, { 23, 0x2a, 8, 41 },
  1126. X    { 23, 0x2b, 8, 42 }, { 23, 0x2c, 8, 43 }, { 23, 0x2d, 8, 44 },
  1127. X    { 23, 0x04, 8, 45 }, { 23, 0x05, 8, 46 }, { 23, 0x0a, 8, 47 },
  1128. X    { 23, 0x0b, 8, 48 }, { 23, 0x52, 8, 49 }, { 23, 0x53, 8, 50 },
  1129. X    { 23, 0x54, 8, 51 }, { 23, 0x55, 8, 52 }, { 23, 0x24, 8, 53 },
  1130. X    { 23, 0x25, 8, 54 }, { 23, 0x58, 8, 55 }, { 23, 0x59, 8, 56 },
  1131. X    { 23, 0x5a, 8, 57 }, { 23, 0x5b, 8, 58 }, { 23, 0x4a, 8, 59 },
  1132. X    { 23, 0x4b, 8, 60 }, { 23, 0x32, 8, 61 }, { 23, 0x33, 8, 62 },
  1133. X    { 23, 0x34, 8, 63 },
  1134. X  };
  1135. X
  1136. X  HuffmanTable*
  1137. X    entry;
  1138. X
  1139. X  int
  1140. X    c,
  1141. X    k,
  1142. X    n,
  1143. X    x;
  1144. X
  1145. X  register int
  1146. X    i,
  1147. X    j;
  1148. X
  1149. X  register RunlengthPacket
  1150. X    *p;
  1151. X
  1152. X  register unsigned char
  1153. X    *q;
  1154. X
  1155. X  register unsigned short
  1156. X    polarity;
  1157. X
  1158. X  unsigned int
  1159. X    mask;
  1160. X
  1161. X  unsigned char
  1162. X    bit,
  1163. X    byte,
  1164. X    *scanline;
  1165. X
  1166. X  /*
  1167. X    Allocate scanline buffer.
  1168. X  */
  1169. X  scanline=(unsigned char *)
  1170. X    malloc(Max(image->columns,1728)*sizeof(unsigned char));
  1171. X  if (scanline == (unsigned char *) NULL)
  1172. X    {
  1173. X      Warning("unable to allocate memory",(char *) NULL);
  1174. X      return(False);
  1175. X    }
  1176. X  /*
  1177. X    Compress MIFF to 1D Huffman encoded pixels.
  1178. X  */
  1179. X  q=scanline;
  1180. X  polarity=(Intensity(image->colormap[0]) >
  1181. X    Intensity(image->colormap[1]) ? 0 : 1);
  1182. X  for (i=0; i < Max(image->columns,1728); i++)
  1183. X    *q++=polarity;
  1184. X  byte=0;
  1185. X  bit=0x80;
  1186. X  p=image->pixels;
  1187. X  q=scanline;
  1188. X  x=0;
  1189. X  for (i=0; i < image->packets; i++)
  1190. X  {
  1191. X    for (j=0; j <= ((int) p->length); j++)
  1192. X    {
  1193. X      if (p->index == polarity)
  1194. X        *q++=(unsigned char) polarity;
  1195. X       else
  1196. X        *q++=(unsigned char) !polarity;
  1197. X      x++;
  1198. X      if (x == image->columns)
  1199. X        {
  1200. X          /*
  1201. X            Huffman encode scanline.
  1202. X          */
  1203. X          q=scanline;
  1204. X          n=Max(image->columns,1728);
  1205. X          while (n > 0)
  1206. X          {
  1207. X            /*
  1208. X              Find white run.
  1209. X            */
  1210. X            c=0;
  1211. X            while((*q == polarity) && (n > 0))
  1212. X            {
  1213. X              q++;
  1214. X              c++;
  1215. X              n--;
  1216. X            }
  1217. X            /*
  1218. X              Output white run.
  1219. X            */
  1220. X            if (c >= 64)
  1221. X              {
  1222. X                entry=MWTable+((c/64)-1);
  1223. X                c-=entry->count;
  1224. X                HuffmanOutputCode(entry);
  1225. X              }
  1226. X            entry=TWTable+c;
  1227. X            HuffmanOutputCode(entry);
  1228. X            c=0;
  1229. X            if (n == 0)
  1230. X              break;
  1231. X            /*
  1232. X              Find black run.
  1233. X            */
  1234. X            while ((*q != polarity) && (n > 0))
  1235. X            {
  1236. X              q++;
  1237. X              c++;
  1238. X              n--;
  1239. X            }
  1240. X            /*
  1241. X              Output black run.
  1242. X            */
  1243. X            if (c >= 64)
  1244. X              {
  1245. X                entry=MBTable+((c/64)-1);
  1246. X                c-=entry->count;
  1247. X                HuffmanOutputCode(entry);
  1248. X              }
  1249. X            entry=TBTable+c;
  1250. X            HuffmanOutputCode(entry);
  1251. X            if (n == 0)
  1252. X              break;
  1253. X          }
  1254. X          /*
  1255. X            End of line.
  1256. X          */
  1257. X          for (k=0; k < 11; k++)
  1258. X            OutputBit(0);
  1259. X          OutputBit(1);
  1260. X          x=0;
  1261. X          q=scanline;
  1262. X        }
  1263. X    }
  1264. X    p++;
  1265. X  }
  1266. X  /*
  1267. X    End of page.
  1268. X  */
  1269. X  for (i=0; i < 6; i++)
  1270. X  {
  1271. X    /*
  1272. X      End of line.
  1273. X    */
  1274. X    for (k=0; k < 11; k++)
  1275. X      OutputBit(0);
  1276. X    OutputBit(1);
  1277. X  }
  1278. X  /*
  1279. X    Flush bits.
  1280. X  */
  1281. X  if (bit != 0x80)
  1282. X    (void) fputc((char) byte,image->file);
  1283. X  (void) free((char *) scanline);
  1284. X  return(True);
  1285. }
  1286. X
  1287. /*
  1288. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1289. %                                                                             %
  1290. %                                                                             %
  1291. %                                                                             %
  1292. %   L Z W D e c o d e I m a g e                                               %
  1293. %                                                                             %
  1294. %                                                                             %
  1295. %                                                                             %
  1296. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1297. %
  1298. %  Function LZWDecodeImage uncompresses an image via LZW-coding.
  1299. %
  1300. %  The format of the LZWDecodeImage routine is:
  1301. %
  1302. %      status=LZWDecodeImage(image)
  1303. %
  1304. %  A description of each parameter follows:
  1305. %
  1306. %    o status:  Function LZWDecodeImage returns True if all the pixels are
  1307. %      uncompressed without error, otherwise False.
  1308. %
  1309. %    o image: The address of a structure of type Image.
  1310. %
  1311. %
  1312. */
  1313. unsigned int LZWDecodeImage(image)
  1314. Image
  1315. X  *image;
  1316. {
  1317. #define MaxStackSize  4096
  1318. #define NullCode  (-1)
  1319. X
  1320. X  int
  1321. X    available,
  1322. X    clear,
  1323. X    code_mask,
  1324. X    code_size,
  1325. X    end_of_information,
  1326. X    in_code,
  1327. X    old_code;
  1328. X
  1329. X  register int
  1330. X    bits,
  1331. X    code,
  1332. X    count,
  1333. X    i;
  1334. X
  1335. X  register RunlengthPacket
  1336. X    *p;
  1337. X
  1338. X  register unsigned char
  1339. X    *c;
  1340. X
  1341. X  register unsigned int
  1342. X    datum;
  1343. X
  1344. X  short
  1345. X    *prefix;
  1346. X
  1347. X  unsigned char
  1348. X    data_size,
  1349. X    first,
  1350. X    *packet,
  1351. X    *pixel_stack,
  1352. X    *suffix,
  1353. X    *top_stack;
  1354. X
  1355. X  unsigned short
  1356. X    index;
  1357. X
  1358. X  /*
  1359. X    Allocate decoder tables.
  1360. X  */
  1361. X  packet=(unsigned char *) malloc(256*sizeof(unsigned char));
  1362. X  prefix=(short *) malloc(MaxStackSize*sizeof(short));
  1363. X  suffix=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
  1364. X  pixel_stack=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
  1365. X  if ((packet == (unsigned char *) NULL) ||
  1366. X      (prefix == (short *) NULL) ||
  1367. X      (suffix == (unsigned char *) NULL) ||
  1368. X      (pixel_stack == (unsigned char *) NULL))
  1369. X    return(False);
  1370. X  /*
  1371. X    Initialize LZW data stream decoder.
  1372. X  */
  1373. X  data_size=fgetc(image->file);
  1374. X  clear=1 << data_size;
  1375. X  end_of_information=clear+1;
  1376. X  available=clear+2;
  1377. X  old_code=NullCode;
  1378. X  code_size=data_size+1;
  1379. X  code_mask=(1 << code_size)-1;
  1380. X  for (code=0; code < clear; code++)
  1381. X  {
  1382. X    prefix[code]=0;
  1383. X    suffix[code]=code;
  1384. X  }
  1385. X  /*
  1386. X    Decode LZW pixel stream.
  1387. X  */
  1388. X  datum=0;
  1389. X  bits=0;
  1390. X  c=0;
  1391. X  count=0;
  1392. X  first=0;
  1393. X  top_stack=pixel_stack;
  1394. X  p=image->pixels;
  1395. X  for (i=0; i < image->packets; )
  1396. X  {
  1397. X    if (top_stack == pixel_stack)
  1398. X      {
  1399. X        if (bits < code_size)
  1400. X          {
  1401. X            /*
  1402. X              Load bytes until there is enough bits for a code.
  1403. X            */
  1404. X            if (count == 0)
  1405. X              {
  1406. X                /*
  1407. X                  Read a new data block.
  1408. X                */
  1409. X                count=ReadDataBlock((char *) packet,image->file);
  1410. X                if (count <= 0)
  1411. X                  break;
  1412. X                c=packet;
  1413. X              }
  1414. X            datum+=(*c) << bits;
  1415. X            bits+=8;
  1416. X            c++;
  1417. X            count--;
  1418. X            continue;
  1419. X          }
  1420. X        /*
  1421. X          Get the next code.
  1422. X        */
  1423. X        code=datum & code_mask;
  1424. X        datum>>=code_size;
  1425. X        bits-=code_size;
  1426. X        /*
  1427. X          Interpret the code
  1428. X        */
  1429. X        if ((code > available) || (code == end_of_information))
  1430. X          break;
  1431. X        if (code == clear)
  1432. X          {
  1433. X            /*
  1434. X              Reset decoder.
  1435. X            */
  1436. X            code_size=data_size+1;
  1437. X            code_mask=(1 << code_size)-1;
  1438. X            available=clear+2;
  1439. X            old_code=NullCode;
  1440. X            continue;
  1441. X          }
  1442. X        if (old_code == NullCode)
  1443. X          {
  1444. X            *top_stack++=suffix[code];
  1445. X            old_code=code;
  1446. X            first=code;
  1447. X            continue;
  1448. X          }
  1449. X        in_code=code;
  1450. X        if (code == available)
  1451. X          {
  1452. X            *top_stack++=first;
  1453. X            code=old_code;
  1454. X          }
  1455. X        while (code > clear)
  1456. X        {
  1457. X          *top_stack++=suffix[code];
  1458. X          code=prefix[code];
  1459. X        }
  1460. X        first=suffix[code];
  1461. X        /*
  1462. X          Add a new string to the string table,
  1463. X        */
  1464. X        *top_stack++=first;
  1465. X        prefix[available]=old_code;
  1466. X        suffix[available]=first;
  1467. X        available++;
  1468. X        if (((available & code_mask) == 0) && (available < MaxStackSize))
  1469. X          {
  1470. X            code_size++;
  1471. X            code_mask+=available;
  1472. X          }
  1473. X        old_code=in_code;
  1474. X      }
  1475. X    /*
  1476. X      Pop a pixel off the pixel stack.
  1477. X    */
  1478. X    top_stack--;
  1479. X    index=(unsigned short) *top_stack;
  1480. X    p->red=image->colormap[index].red;
  1481. X    p->green=image->colormap[index].green;
  1482. X    p->blue=image->colormap[index].blue;
  1483. X    p->index=index;
  1484. X    p->length=0;
  1485. X    p++;
  1486. X    i++;
  1487. X  }
  1488. X  /*
  1489. X    Initialize any remaining color packets to a known color.
  1490. X  */
  1491. X  for ( ; i < image->packets; i++)
  1492. X  {
  1493. X    p->red=image->colormap[0].red;
  1494. X    p->green=image->colormap[0].green;
  1495. X    p->blue=image->colormap[0].blue;
  1496. X    p->index=0;
  1497. X    p->length=0;
  1498. X    p++;
  1499. X  }
  1500. X  /*
  1501. X    Free decoder memory.
  1502. X  */
  1503. X  (void) free((char *) pixel_stack);
  1504. X  (void) free((char *) suffix);
  1505. X  (void) free((char *) prefix);
  1506. X  (void) free((char *) packet);
  1507. X  return(True);
  1508. }
  1509. X
  1510. /*
  1511. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1512. %                                                                             %
  1513. %                                                                             %
  1514. %                                                                             %
  1515. %   L Z W E n c o d e F i l t e r                                             %
  1516. %                                                                             %
  1517. %                                                                             %
  1518. %                                                                             %
  1519. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1520. %
  1521. %  Function LZWEncodeFilter compresses an image via LZW-coding specific to
  1522. %  Postscript Level II.
  1523. %
  1524. %  The format of the LZWEncodeFilter routine is:
  1525. %
  1526. %      status=LZWEncodeFilter(file,pixels,number_pixels)
  1527. %
  1528. %  A description of each parameter follows:
  1529. %
  1530. %    o status:  Function LZWEncodeFilter returns True if all the pixels are
  1531. %      compressed without error, otherwise False.
  1532. %
  1533. %    o file: The address of a structure of type FILE.  LZW encoded pixels
  1534. %      are written to this file.
  1535. %
  1536. %    o pixels: The address of an unsigned array of characters containing the
  1537. %      pixels to compress.
  1538. %
  1539. %    o number_pixels:  An unsigned interger that specifies the number of
  1540. %      pixels to compress.
  1541. %
  1542. %
  1543. */
  1544. unsigned int LZWEncodeFilter(file,pixels,number_pixels)
  1545. FILE
  1546. X  *file;
  1547. X
  1548. unsigned char
  1549. X  *pixels;
  1550. X
  1551. unsigned int
  1552. X  number_pixels;
  1553. {
  1554. #define LZWClr  256  /* Clear Table Marker */
  1555. #define LZWEod  257  /* End of Data marker */
  1556. #define OutputCode(code) \
  1557. { \
  1558. X    accumulator+=((long) code) << (32-code_width-number_bits); \
  1559. X    number_bits+=code_width; \
  1560. X    while (number_bits >= 8) \
  1561. X    { \
  1562. X        PrintByte(accumulator >> 24); \
  1563. X        accumulator=accumulator << 8; \
  1564. X        number_bits-=8; \
  1565. X    } \
  1566. }
  1567. #define PrintByte(value)  \
  1568. { \
  1569. X  (void) fprintf(file,"%02x",(int) (value & 0x0ff));  \
  1570. X  count++;  \
  1571. X  if (count >= 36)  \
  1572. X    {  \
  1573. X      (void) fputc('\n',file);  \
  1574. X      count=0; \
  1575. X    } \
  1576. }
  1577. X
  1578. X  typedef struct _TableType
  1579. X  {
  1580. X    short
  1581. X      prefix,
  1582. X      suffix,
  1583. X      next;
  1584. X  } TableType;
  1585. X
  1586. X  int
  1587. X    index;
  1588. X
  1589. X  register int
  1590. X    i;
  1591. X
  1592. X  short
  1593. X    number_bits,
  1594. X    code_width,
  1595. X    count,
  1596. X    last_code,
  1597. X    next_index;
  1598. X
  1599. X  TableType
  1600. X    *table;
  1601. X
  1602. X  unsigned long
  1603. X    accumulator;
  1604. X
  1605. X  /*
  1606. X    Allocate string table.
  1607. X  */
  1608. X  table=(TableType *) malloc((1 << 12)*sizeof(TableType));
  1609. X  if (table == (TableType *) NULL)
  1610. X    return(False);
  1611. X  /*
  1612. X    Initialize variables.
  1613. X  */
  1614. X  accumulator=0;
  1615. X  code_width=9;
  1616. X  count=0;
  1617. X  number_bits=0;
  1618. X  last_code=0;
  1619. X  OutputCode(LZWClr);
  1620. X  for (index=0; index < 256; index++)
  1621. X  {
  1622. X    table[index].prefix=(-1);
  1623. X    table[index].suffix=index;
  1624. X    table[index].next=(-1);
  1625. X  }
  1626. X  next_index=LZWEod+1;
  1627. X  code_width=9;
  1628. X  last_code=pixels[0];
  1629. X  for (i=1; i < number_pixels; i++)
  1630. X  {
  1631. X    /*
  1632. X      Find string.
  1633. X    */
  1634. X    index=last_code;
  1635. X    while (index != -1)
  1636. X      if ((table[index].prefix != last_code) ||
  1637. X          (table[index].suffix != pixels[i]))
  1638. X        index=table[index].next;
  1639. X      else
  1640. X        {
  1641. X          last_code=index;
  1642. X          break;
  1643. X        }
  1644. X    if (last_code != index)
  1645. SHAR_EOF
  1646. true || echo 'restore of ImageMagick/compress.c failed'
  1647. fi
  1648. echo 'End of ImageMagick part 20'
  1649. echo 'File ImageMagick/compress.c is continued in part 21'
  1650. echo 21 > _shar_seq_.tmp
  1651. exit 0
  1652.  
  1653. exit 0 # Just in case...
  1654. -- 
  1655.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1656. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1657.  "It's intuitively obvious to the |
  1658.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1659.