home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-07-13 | 50.3 KB | 1,659 lines |
- Newsgroups: comp.sources.x
- From: cristy@eplrx7.es.duPont.com (Cristy)
- Subject: v20i076: imagemagic - X11 image processing and display, Part20/38
- Message-ID: <1993Jul14.175802.1951@sparky.sterling.com>
- X-Md4-Signature: ba04536a88600fc878061d0b4fb6cfad
- Sender: chris@sparky.sterling.com (Chris Olson)
- Organization: Sterling Software
- Date: Wed, 14 Jul 1993 17:58:02 GMT
- Approved: chris@sterling.com
-
- Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
- Posting-number: Volume 20, Issue 76
- Archive-name: imagemagic/part20
- Environment: X11
- Supersedes: imagemagic: Volume 13, Issue 17-37
-
- #!/bin/sh
- # this is magick.20 (part 20 of ImageMagick)
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file ImageMagick/animate.man continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 20; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping ImageMagick/animate.man'
- else
- echo 'x - continuing file ImageMagick/animate.man'
- sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/animate.man' &&
- the next image. The default is to display each image without delay.
- .TP 5
- .B "-density \fI<width>x<height>
- vertical and horizonal density of the image.
- X
- This option specifies an image density whose interpretation changes
- with the type of image. The default is 72 dots per inch in the
- horizonal and vertical direction for Postscript. Text files default to
- 80 characters in width and 60 lines in height. Use this option to
- alter the default density.
- .TP 5
- .B "-display \fIhost:display[.screen]\fP"
- specifies the X server to contact; see \fBX(1)\fP.
- .TP 5
- .B "-dither"
- apply Floyd/Steinberg error diffusion to the image.
- X
- The basic strategy of dithering is to trade intensity resolution for
- spatial resolution by averaging the intensities of several neighboring
- pixels. Images which suffer from severe contouring when reducing colors
- can be improved with this option.
- X
- The \fB-colors\fP or \fB-monochrome\fP option is required for this option
- to take effect.
- .TP 5
- .B "-gamma \fIvalue\fP"
- level of gamma correction.
- X
- The same color image displayed on two different workstations may look
- different due to differences in the display monitor. Use gamma
- correction to adjust for this color difference. Reasonable values
- extend from 0.8 to 2.3.
- .TP 5
- .B "-geometry \fI<width>x<height>{\+-}<x offset>{\+-}<y offset>\fP"
- preferred size and location of the image window. See \fBX(1)\fP for details
- about the geometry specification. By default, the window size is the image
- size and the location is choosen by you when it is mapped.
- X
- If the specified image size is smaller than the actual image size, the
- image is first reduced to an integral of the specified image size with
- an antialias digital filter. The image is then scaled to the exact
- specified image size with pixel replication. If the specified image
- size is greater than the actual image size, the image is first enlarged
- to an integral of the specified image size with bilinear
- interpolation. The image is then scaled to the exact specified image
- size with pixel replication.
- X
- When displaying an image on an X server, \fI<x offset>\fP and
- \fI<y offset>\fP is relative to the root window.
- X
- The equivalent X resource for this option is \fBimageGeometry\fP
- (class \fBImageGeometry\fP). See \fBX RESOURCES\fP for details.
- .TP 5
- .B "-interlace \fItype\fP"
- the type of interlacing scheme: \fBNONE\fP, \fBLINE\fP, or \fBPLANE\fP.
- X
- This option is used to specify the type of interlacing scheme for raw
- image formats such as \fBRGB\fP or \fBYUV\fP. \fBNONE\fP means do not
- interlace (RGBRGBRGBRGBRGBRGB...), \fBLINE\fP uses scanline
- interlacing (RRR...GGG...BBB...RRR...GGG...BBB...), and \fBPLANE\fP uses
- plane interlacing (RRRRRR...GGGGGG...BBBBBB...).
- .TP 5
- .B "-map \fItype\fP"
- animate images using this Standard Colormap type.
- X
- Choose from these Standard Colormap types:
- X
- X default
- X best
- X red
- X green
- X blue
- X gray
- X
- The X server must support the Standard Colormap you choose, otherwise an
- error occurs. See \fBXSTDCMAP(1)\fP for one way of creating Standard Colormaps.
- .TP 5
- .B "-monochrome"
- transform the image to black and white.
- X
- Monochrome images can benefit from error diffusion. Use \fB-dither\fP with
- this option to diffuse the error.
- .TP 5
- .B "-reflect"
- create a "mirror image" by reflecting the image scanlines.
- .TP 5
- .B "-rotate \fIdegrees\fP"
- apply Paeth image rotation to the image.
- X
- Empty triangles left over from rotating the image are filled with
- the color defined by the pixel at location (0,0).
- .TP 5
- .B "-scale \fI<width factor>x<height factor>\fP"
- preferred size factors of the image.
- X
- This option behaves like \fB-geometry\fP except the width and height values
- are relative instead of absolute. The image size is multiplied by the
- width and height factors to obtain the final image dimensions. If only
- one factor is specified, both the width and height factors assume the
- value.
- X
- Factors may be fractional. To increase the size of an image, use a
- scale factor greater than 1.0. To decrease an image's size, use a
- scale factor less than 1.0. Default is 1.0.
- X
- The equivalent X resource for this option is \fBscaleGeometry\fP
- (class \fBScaleGeometry\fP). See \fBX RESOURCES\fP for details.
- .TP 5
- .B "-treedepth \fIvalue\fP"
- Normally, this integer value is zero or one. A zero or one tells
- \fBanimate\fP to choose a optimal tree depth for the color reduction
- algorithm.
- X
- An optimal depth generally allows the best representation of the source
- image with the fastest computational speed and the least amount of
- memory. However, the default depth is inappropriate for some images.
- To assure the best representation, try values between 2 and 8 for this
- parameter. Refer to \fBquantize(9)\fP for more details.
- X
- The \fB-colors\fP or \fB-monochrome\fP option is required for this option
- to take effect.
- .TP 5
- .B -verbose
- print detailed information about the image.
- X
- This information is printed: image scene number; image name; image
- size; the image class (\fIDirectClass\fP or \fIPseudoClass\fP); the total
- number of unique colors; and the number of seconds to read and
- transform the image. Refer to \fBmiff(5)\fP for a description of
- the image class.
- X
- If \fB-colors\fP is also specified, the total unique colors in the image
- and color reduction error values are printed. Refer to \fBquantize(9)\fP
- for a description of these values.
- .TP 5
- .B "-visual \fItype\fP"
- animate images using this visual type.
- X
- Choose from these visual classes:
- X
- X StaticGray
- X GrayScale
- X StaticColor
- X PseudoColor
- X TrueColor
- X DirectColor
- X default
- X \fIvisual id\fP
- X
- The X server must support the visual you choose, otherwise an error occurs.
- If a visual is not specified, the visual class that can display the most
- simultaneous colors on the default screen is choosen.
- .PP
- In addition to those listed above, you can specify these standard X
- resources as command line options: \fB-background\fP,
- \fB-bordercolor\fP, \fB-borderwidth\fP, \fB-font\fP,
- \fB-foreground\fP, \fB-iconGeometry\fP, \fB-iconic\fP, \fB-name\fP, or
- \fB-title\fP. See \fBX RESOURCES\fP for details.
- .PP
- Any option you specify on the command line remains in effect until it is
- explicitly changed by specifying the option again with a different effect.
- For example, to animate two images, the first with 32 colors and the
- second with only 16 colors, use:
- .PP
- X animate -colors 32 cockatoo.1 -colors 16 cockatoo.2
- .PP
- Change \fI-\fP to \fI\+\fP in any option above to reverse its effect.
- For example, specify \fB\+dither\fP to not apply error diffusion to an image.
- .PP
- By default, the image format is determined by its magic number. To
- specify a particular image format, precede the filename with an image
- format name and a colon (i.e. ps:image) or specify the image type as
- the filename suffix (i.e. image.ps). See \fBconvert(1)\fP for a list
- of valid image formats.
- .PP
- When you specify \fBX\fP as your image type, the filename has special
- meaning. It specifies an X window by id, name, or \fBroot\fP. If no
- filename is specified, the window is selected by clicking the mouse in
- the desired window.
- .PP
- Specify \fIfile\fP as \fI-\fP for standard input, If \fIfile\fP has the
- extension \fB.Z\fP or \fB.gz\fP, the file is uncompressed with
- \fBuncompress\fP or \fBgunzip\fP respectively.
- .PP
- Image filenames may appear in any order on the command line if the image
- format is \fIMIFF\fP (refer to \fBmiff(5)\fP and the \fBscene\fP keyword is
- specified in the image. Otherwise the images will display in the order
- they appear on the command line.
- .SH BUTTONS
- .TP 5
- .B "1"
- Press and drag to select a command from a pop-up menu. Choose from
- these commands:
- X
- X Play
- X Step
- X Repeat
- X Auto Reverse
- X Slower
- X Faster
- X Forward
- X Reverse
- X Image Info
- X Quit
- .SH KEYBOARD ACCELERATORS
- .TP 5
- .B "p"
- Press to animate the sequence of images.
- .TP 5
- .B "s"
- Press to display the next image in the sequence.
- .TP 5
- .B "."
- Press to continually display the sequence of images.
- .TP 5
- .B "a"
- Press to automatically reverse the sequence of images.
- .TP 5
- .B "<"
- Press to slow the display of the images. Refer to \fB-delay\fP for more
- information.
- .TP 5
- .B ">"
- Press to speed-up the display of the images. Refer to \fB-delay\fP for more
- information.
- .TP 5
- .B "f"
- Press to animate in the forward direction.
- .TP 5
- .B "r"
- Press to animate in the reverse direction.
- .TP 5
- .B "i"
- Press to display information about the image. Press any key or button
- to erase the information.
- X
- This information is printed: image name; image size; and the total
- number of unique colors in the image.
- .TP 5
- .B "q"
- Press to discard all images and exit program.
- .SH "X RESOURCES"
- \fBanimate\fP options can appear on the command line or in your X
- resource file. Options on the command line supersede values specified
- in your X resource file. See \fBX(1)\fP for more information on X
- resources.
- X
- All \fBanimate\fP options have a corresponding X resource. In addition,
- the \fBanimate\fP program uses the following X resources:
- .TP 5
- .B background (\fPclass\fB Background)
- Specifies the preferred color to use for the image window background. The
- default is black.
- .TP 5
- .B borderColor (\fPclass\fB BorderColor)
- Specifies the preferred color to use for the image window border. The
- default is white.
- .TP 5
- .B borderWidth (\fPclass\fB BorderWidth)
- Specifies the width in pixels of the image window border. The default is 2.
- .TP 5
- .B font (\fPclass\fB Font)
- Specifies the name of the preferred font to use when displaying text
- within the image window. The default is \fI/g9x15\fP, \fIfixed\fP, or
- \fI/g6x13\fP determined by the image window size.
- .TP 5
- .B foreground (\fPclass\fB Foreground)
- Specifies the preferred color to use for text within the image window. The
- default is white.
- .TP 5
- .B iconGeometry (\fPclass\fB IconGeometry)
- Specifies the preferred size and position of the application when
- iconified. It is not necessarily obeyed by all window managers.
- .TP 5
- .B iconic (\fPclass\fB Iconic)
- This resource indicates that you would prefer that the application's
- windows initially not be visible as if the windows had be immediately
- iconified by you. Window managers may choose not to honor the
- application's request.
- .TP 5
- .B name (\fPclass\fB Name)
- This resource specifies the name under which resources for the
- application should be found. This resource is useful in shell aliases to
- distinguish between invocations of an application, without resorting to
- creating links to alter the executable file name. The default is the
- application name.
- .TP 5
- .B title (\fPclass\fB Title)
- This resource specifies the title to be used for the image window. This
- information is sometimes used by a window manager to provide some sort
- of header identifying the window. The default is the image file name.
- .SH ENVIRONMENT
- .TP 5
- .B display
- To get the default host, display number, and screen.
- .SH SEE ALSO
- .B
- display(1), import(1), mogrify(1), convert(1), quantize(9), miff(5), X(1), xstdcmap(1), compress(1), miff(5)
- .SH COPYRIGHT
- Copyright 1993 E. I. du Pont de Nemours & Company
- .PP
- Permission to use, copy, modify, distribute, and sell this software and
- its documentation for any purpose is hereby granted without fee,
- provided that the above copyright notice appear in all copies and that
- both that copyright notice and this permission notice appear in
- supporting documentation, and that the name of E. I. du Pont de Nemours
- & Company not be used in advertising or publicity pertaining to
- distribution of the software without specific, written prior
- permission. E. I. du Pont de Nemours & Company makes no representations
- about the suitability of this software for any purpose. It is provided
- "as is" without express or implied warranty.
- .PP
- E. I. du Pont de Nemours & Company disclaims all warranties with regard
- to this software, including all implied warranties of merchantability
- and fitness, in no event shall E. I. du Pont de Nemours & Company be
- liable for any special, indirect or consequential damages or any
- damages whatsoever resulting from loss of use, data or profits, whether
- in an action of contract, negligence or other tortious action, arising
- out of or in connection with the use or performance of this software.
- .SH ACKNOWLEDGEMENTS
- The MIT X Consortium for making network transparent graphics a reality.
- .PP
- Michael Halle, Spatial Imaging Group at MIT, for the initial
- implementation of Alan Paeth's image rotation algorithm.
- .PP
- David Pensak, E. I. du Pont de Nemours & Company, for providing a
- computing environment that made this program possible.
- .PP
- Paul Raveling, USC Information Sciences Institute, for the original
- idea of using space subdivision for the color reduction algorithm.
- .SH AUTHORS
- John Cristy, E.I. du Pont de Nemours & Company Incorporated
- SHAR_EOF
- echo 'File ImageMagick/animate.man is complete' &&
- chmod 0644 ImageMagick/animate.man ||
- echo 'restore of ImageMagick/animate.man failed'
- Wc_c="`wc -c < 'ImageMagick/animate.man'`"
- test 17246 -eq "$Wc_c" ||
- echo 'ImageMagick/animate.man: original size 17246, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= ImageMagick/display.h ==============
- if test -f 'ImageMagick/display.h' -a X"$1" != X"-c"; then
- echo 'x - skipping ImageMagick/display.h (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- echo 'x - extracting ImageMagick/display.h (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/display.h' &&
- /*
- X Include declarations
- */
- #include <stdio.h>
- #if defined(__STDC__) || defined(sgi) || defined(_AIX)
- #include <stdlib.h>
- #include <unistd.h>
- #else
- #ifndef vms
- #include <malloc.h>
- #include <memory.h>
- #endif
- #endif
- #include <ctype.h>
- #include <string.h>
- #include <time.h>
- #include <math.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #undef index
- X
- /*
- X Define declarations for the Display program.
- */
- #if __STDC__ || defined(sgi) || defined(_AIX)
- #define _Declare(formal_parameters) formal_parameters
- #else
- #define const
- #define _Declare(formal_parameters) ()
- #endif
- #define DownShift(x) ((int) ((x)+(1L << 15)) >> 16)
- #define False 0
- #define Max(x,y) (((x) > (y)) ? (x) : (y))
- #define Min(x,y) (((x) < (y)) ? (x) : (y))
- #define MinInfoSize (1 << 18)
- #define True 1
- #define UpShift(x) ((x) << 16)
- #define UpShifted(x) ((int) ((x)*(1L << 16)+0.5))
- #define Warning(message,qualifier) \
- { \
- X (void) fprintf(stderr,"%s: %s",client_name,message); \
- X if (qualifier != (char *) NULL) \
- X (void) fprintf(stderr," (%s)",qualifier); \
- X (void) fprintf(stderr,".\n"); \
- }
- #ifdef vms
- #define pclose(file) exit(1)
- #define popen(command,mode) exit(1)
- #define unlink(file) remove(file)
- #endif
- X
- /*
- X Variable declarations.
- */
- #ifndef lint
- static char
- X Version[]="@(#)ImageMagick 2.3.3 93/07/01 cristy@dupont.com";
- #endif
- SHAR_EOF
- chmod 0644 ImageMagick/display.h ||
- echo 'restore of ImageMagick/display.h failed'
- Wc_c="`wc -c < 'ImageMagick/display.h'`"
- test 1328 -eq "$Wc_c" ||
- echo 'ImageMagick/display.h: original size 1328, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= ImageMagick/compress.c ==============
- if test -f 'ImageMagick/compress.c' -a X"$1" != X"-c"; then
- echo 'x - skipping ImageMagick/compress.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- echo 'x - extracting ImageMagick/compress.c (Text)'
- sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/compress.c' &&
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % CCCC OOO M M PPPP RRRR EEEEE SSSSS SSSSS %
- % C O O MM MM P P R R E SS SS %
- % C O O M M M PPPP RRRR EEE SSS SSS %
- % C O O M M P R R E SS SS %
- % CCCC OOO M M P R R EEEEE SSSSS SSSSS %
- % %
- % %
- % Image Compression/Decompression Coders %
- % %
- % %
- % %
- % Software Design %
- % John Cristy %
- % May 1993 %
- % %
- % %
- % Copyright 1993 E. I. du Pont de Nemours & Company %
- % %
- % Permission to use, copy, modify, distribute, and sell this software and %
- % its documentation for any purpose is hereby granted without fee, %
- % provided that the above Copyright notice appear in all copies and that %
- % both that Copyright notice and this permission notice appear in %
- % supporting documentation, and that the name of E. I. du Pont de Nemours %
- % & Company not be used in advertising or publicity pertaining to %
- % distribution of the software without specific, written prior %
- % permission. E. I. du Pont de Nemours & Company makes no representations %
- % about the suitability of this software for any purpose. It is provided %
- % "as is" without express or implied warranty. %
- % %
- % E. I. du Pont de Nemours & Company disclaims all warranties with regard %
- % to this software, including all implied warranties of merchantability %
- % and fitness, in no event shall E. I. du Pont de Nemours & Company be %
- % liable for any special, indirect or consequential damages or any %
- % damages whatsoever resulting from loss of use, data or profits, whether %
- % in an action of contract, negligence or other tortious action, arising %
- % out of or in connection with the use or performance of this software. %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- %
- */
- X
- /*
- X Include declarations.
- */
- #include "display.h"
- #include "image.h"
- #include "utility.h"
- /*
- X Define declarations.
- */
- #define LowerBound 0
- #define MaxContextStates 121
- #define MinimumIntervalD (unsigned short) 0xf000 /* ~-0.75 */
- #define MinimumIntervalE (unsigned short) 0x1000 /* ~0.75 */
- #define No 0
- #define UpperBound 2
- #define Yes 1
- /*
- X State classification.
- */
- #define ZeroState 0
- #define SmallPostitiveState 1
- #define SmallNegativeState 2
- #define LargePostitiveState 3
- #define LargeNegativeState 4
- /*
- X Typedef declarations.
- */
- typedef struct _ScanlinePacket
- {
- X unsigned char
- X pixel;
- X
- X int
- X state;
- } ScanlinePacket;
- /*
- X Initialized declarations.
- */
- static int decrement_less_probable[]=
- {
- X 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
- X 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2
- };
- X
- static int increment_more_probable[]=
- {
- X 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- X 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
- };
- X
- static int more_probable_exchange[]=
- {
- X 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- X 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- };
- X
- static int statistics[][5]=
- {
- X 0, 4, 8, 12, 16,
- X 20, 24, 28, 32, 36,
- X 40, 44, 48, 52, 56,
- X 60, 64, 68, 72, 76,
- X 80, 84, 88, 92, 96
- };
- X
- static unsigned short probability[]=
- {
- X 0x0ac1, 0x0a81, 0x0a01, 0x0901, 0x0701, 0x0681,
- X 0x0601, 0x0501, 0x0481, 0x0441, 0x0381, 0x0301,
- X 0x02c1, 0x0281, 0x0241, 0x0181, 0x0121, 0x00e1,
- X 0x00a1, 0x0071, 0x0059, 0x0053, 0x0027, 0x0017,
- X 0x0013, 0x000b, 0x0007, 0x0005, 0x0003, 0x0001
- };
- /*
- X Declarations and initializations for predictive arithimetic coder.
- */
- static int
- X code,
- X less_probable[MaxContextStates],
- X more_probable[MaxContextStates],
- X probability_estimate[MaxContextStates];
- X
- static unsigned char
- X *q;
- X
- static unsigned short
- X interval;
- X
- /*
- X External declarations.
- */
- extern char
- X *client_name;
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % B M P D e c o d e I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function BMPDecodeImage unpacks the packed image pixels into
- % runlength-encoded pixel packets.
- %
- % The format of the BMPDecodeImage routine is:
- %
- % status=BMPDecodeImage(compressed_pixels,pixels,number_columns,
- % number_rows)
- %
- % A description of each parameter follows:
- %
- % o status: Function BMPDecodeImage returns True if all the pixels are
- % uncompressed without error, otherwise False.
- %
- % o compressed_pixels: The address of a byte (8 bits) array of compressed
- % pixel data.
- %
- % o pixels: The address of a byte (8 bits) array of pixel data created by
- % the uncompression process. The number of bytes in this array
- % must be at least equal to the number columns times the number of rows
- % of the source pixels.
- %
- % o number_columns: An integer value that is the number of columns or
- % width in pixels of your source image.
- %
- % o number_rows: An integer value that is the number of rows or
- % heigth in pixels of your source image.
- %
- %
- */
- unsigned int BMPDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
- unsigned char
- X *compressed_pixels,
- X *pixels;
- X
- unsigned int
- X number_columns,
- X number_rows;
- {
- X register int
- X i,
- X x,
- X y;
- X
- X register unsigned char
- X *p,
- X *q;
- X
- X unsigned char
- X count,
- X byte;
- X
- X p=compressed_pixels;
- X q=pixels;
- X x=0;
- X for (y=0; y < number_rows; )
- X {
- X count=(*p++);
- X if (count != 0)
- X {
- X /*
- X Encoded mode.
- X */
- X byte=(*p++);
- X for (i=0; i < (int) count; i++)
- X {
- X *q++=byte;
- X x++;
- X }
- X }
- X else
- X {
- X /*
- X Escape mode.
- X */
- X count=(*p++);
- X if (count == 0x01)
- X return(True);
- X switch (count)
- X {
- X case 0x00:
- X {
- X /*
- X End of line.
- X */
- X x=0;
- X y++;
- X q=pixels+y*number_columns;
- X break;
- X }
- X case 0x02:
- X {
- X /*
- X Delta mode.
- X */
- X x+=(*p++);
- X y+=(*p++);
- X q=pixels+y*number_columns+x;
- X break;
- X }
- X default:
- X {
- X /*
- X Absolute mode.
- X */
- X for (i=0; i < (int) count; i++)
- X {
- X byte=(*p++);
- X *q++=byte;
- X x++;
- X }
- X if (count & 0x01)
- X p++; /* Read pad byte */
- X break;
- X }
- X }
- X }
- X }
- X return(False);
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % D e c o d e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function Decode uncompresses a string.
- %
- % The format of the Decode routine is:
- %
- % Decode(state,decision)
- %
- % A description of each parameter follows:
- %
- % o state: An integer value representing the current state.
- %
- % o decision: A pointer to an integer. The output of the binary
- % decision (Yes/No) is returned in this value.
- %
- %
- */
- static void Decode(state,decision)
- register int
- X state,
- X *decision;
- {
- X interval+=probability[probability_estimate[state]];
- X if (((code >> 16) & 0xffff) < ((int) interval))
- X {
- X code-=(interval << 16);
- X interval=(-probability[probability_estimate[state]]);
- X *decision=less_probable[state];
- X }
- X else
- X {
- X *decision=more_probable[state];
- X if (interval <= MinimumIntervalD)
- X return;
- X }
- X do
- X {
- X if ((code & 0xff) == 0)
- X {
- X code&=0xffff0000;
- X if ((*q++) == 0xff)
- X code+=((int) (*q) << 9)+0x02;
- X else
- X code+=((int) (*q) << 8)+0x01;
- X }
- X interval<<=1;
- X code<<=1;
- X } while (interval > MinimumIntervalD);
- X /*
- X Update probability estimates.
- X */
- X if (*decision == more_probable[state])
- X probability_estimate[state]+=
- X increment_more_probable[probability_estimate[state]];
- X else
- X probability_estimate[state]-=
- X decrement_less_probable[probability_estimate[state]];
- X if (more_probable_exchange[probability_estimate[state]] != 0)
- X {
- X /*
- X Exchange sense of most probable and least probable.
- X */
- X less_probable[state]=more_probable[state];
- X more_probable[state]=1-more_probable[state];
- X }
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % E n c o d e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function Encode generate compressed data string by encoding yes-no decision
- % given state s.
- %
- % The format of the Encode routine is:
- %
- % Encode(state,decision)
- %
- % A description of each parameter follows:
- %
- % o state: An integer value representing the current state.
- %
- % o decision: An integer value representing a binary decision.
- %
- %
- */
- static void Encode(state,decision)
- register int
- X state,
- X decision;
- {
- X /*
- X Test on "most-probable-symbol" for state s(more_probable[state])
- X */
- X interval-=probability[probability_estimate[state]];
- X if (more_probable[state] != decision)
- X {
- X code-=interval;
- X interval=probability[probability_estimate[state]];
- X }
- X else
- X if (interval >= MinimumIntervalE)
- X return;
- X /*
- X Encoder renormalization.
- X */
- X do
- X {
- X interval<<=1;
- X if (code >= 0)
- X code<<=1;
- X else
- X {
- X /*
- X Shift unsigned char of data from Code register to compressed string.
- X */
- X code<<=1;
- X if (code > 0)
- X {
- X /*
- X Add eight bits from Code register to compressed data string.
- X */
- X (*q++)--;
- X *q=(unsigned char) (code >> 16);
- X code&=0x0000ffff;
- X code|=0x01800000;
- X }
- X else
- X {
- X code&=0x01ffffff;
- X if ((int) interval > code)
- X {
- X /*
- X Add eight bits from Code register to compressed data string.
- X */
- X (*q++)--;
- X *q=0xff;
- X code|=0x01810000;
- X }
- X else
- X if ((*q++) == 0xff)
- X {
- X /*
- X Add seven bits from Code register plus one stuffed bit to
- X compressed data string.
- X */
- X *q=(unsigned char) (code >> 17);
- X code&=0x0001ffff;
- X code|=0x03000000;
- X }
- X else
- X {
- X /*
- X Add eight bits from Code register to compressed data string.
- X */
- X *q=(unsigned char) (code >> 16);
- X code&=0x0000ffff;
- X code|=0x01800000;
- X }
- X }
- X }
- X } while (interval < MinimumIntervalE);
- X /*
- X Update probability estimates
- X */
- X if (decision == more_probable[state])
- X probability_estimate[state]+=
- X increment_more_probable[probability_estimate[state]];
- X else
- X probability_estimate[state]-=
- X decrement_less_probable[probability_estimate[state]];
- X if (more_probable_exchange[probability_estimate[state]] != 0)
- X more_probable[state]=1-more_probable[state];
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % F l u s h %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function Flush flushes the final bits of data from the Code register to the
- % compressed data string.
- %
- % The format of the Flush routine is:
- %
- % Flush()
- %
- %
- */
- static void Flush()
- {
- X register int
- X extra_bits;
- X
- X code-=interval;
- X extra_bits=24;
- X extra_bits--;
- X while (code >= 0)
- X {
- X code<<=1;
- X extra_bits--;
- X }
- X code<<=1;
- X if (code > 0)
- X (*q)--;
- X /*
- X Add the final compressed data unsigned chars to the compressed data string.
- X */
- X do
- X {
- X if ((*q++) == 0xff)
- X {
- X /*
- X Add seven bits of data plus one stuffed bit to the compressed data
- X string during final Flush of Code register.
- X */
- X *q=(unsigned char) (code >> 17);
- X code&=0x0001ffff;
- X code<<=7;
- X extra_bits-=7;
- X }
- X else
- X {
- X /*
- X Add eight bits of data to the compressed data string during final
- X flush of Code register.
- X */
- X *q=(unsigned char) (code >> 16);
- X code&=0x0000ffff;
- X code<<=8;
- X extra_bits-=8;
- X }
- X } while (extra_bits > 0);
- X q++;
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % H u f f m a n E n c o d e I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function HuffmanEncodeImage compresses an image via Huffman-coding.
- %
- % The format of the HuffmanEncodeImage routine is:
- %
- % status=HuffmanEncodeImage(image)
- %
- % A description of each parameter follows:
- %
- % o status: Function HuffmanEncodeImage returns True if all the pixels are
- % compressed without error, otherwise False.
- %
- % o image: The address of a structure of type Image.
- %
- %
- */
- unsigned int HuffmanEncodeImage(image)
- Image
- X *image;
- {
- #define HuffmanOutputCode(te) \
- { \
- X mask=1 << (entry->length-1); \
- X while (mask) \
- X { \
- X if (entry->code & mask) \
- X OutputBit(1) \
- X else \
- X OutputBit(0); \
- X mask>>=1; \
- X } \
- }
- X
- #define OutputBit(count) \
- { \
- X if(count > 0) \
- X byte=byte | bit; \
- X bit>>=1; \
- X if ((bit & 0xff) == 0) \
- X { \
- X (void) fputc((char) byte,image->file); \
- X byte=0; \
- X bit=0x80; \
- X } \
- }
- X
- X typedef struct HuffmanTable
- X {
- X int
- X id,
- X code,
- X length,
- X count;
- X } HuffmanTable;
- X
- X static HuffmanTable MBTable[]=
- X {
- X { 26, 0x0f, 10, 64 }, { 26, 0xc8, 12, 128 }, { 26, 0xc9, 12, 192 },
- X { 26, 0x5b, 12, 256 }, { 26, 0x33, 12, 320 }, { 26, 0x34, 12, 384 },
- X { 26, 0x35, 12, 448 }, { 26, 0x6c, 13, 512 }, { 26, 0x6d, 13, 576 },
- X { 26, 0x4a, 13, 640 }, { 26, 0x4b, 13, 704 }, { 26, 0x4c, 13, 768 },
- X { 26, 0x4d, 13, 832 }, { 26, 0x72, 13, 896 }, { 26, 0x73, 13, 960 },
- X { 26, 0x74, 13, 1024 }, { 26, 0x75, 13, 1088 }, { 26, 0x76, 13, 1152 },
- X { 26, 0x77, 13, 1216 }, { 26, 0x52, 13, 1280 }, { 26, 0x53, 13, 1344 },
- X { 26, 0x54, 13, 1408 }, { 26, 0x55, 13, 1472 }, { 26, 0x5a, 13, 1536 },
- X { 26, 0x5b, 13, 1600 }, { 26, 0x64, 13, 1664 }, { 26, 0x65, 13, 1728 },
- X };
- X
- X static HuffmanTable MWTable[]=
- X {
- X { 24, 0x1b, 5, 64 }, { 24, 0x12, 5, 128 }, { 24, 0x17, 6, 192 },
- X { 24, 0x37, 7, 256 }, { 24, 0x36, 8, 320 }, { 24, 0x37, 8, 384 },
- X { 24, 0x64, 8, 448 }, { 24, 0x65, 8, 512 }, { 24, 0x68, 8, 576 },
- X { 24, 0x67, 8, 640 }, { 24, 0xcc, 9, 704 }, { 24, 0xcd, 9, 768 },
- X { 24, 0xd2, 9, 832 }, { 24, 0xd3, 9, 896 }, { 24, 0xd4, 9, 960 },
- X { 24, 0xd5, 9, 1024 }, { 24, 0xd6, 9, 1088 }, { 24, 0xd7, 9, 1152 },
- X { 24, 0xd8, 9, 1216 }, { 24, 0xd9, 9, 1280 }, { 24, 0xda, 9, 1344 },
- X { 24, 0xdb, 9, 1408 }, { 24, 0x98, 9, 1472 }, { 24, 0x99, 9, 1536 },
- X { 24, 0x9a, 9, 1600 }, { 24, 0x18, 6, 1664 }, { 24, 0x9b, 9, 1728 },
- X };
- X
- X static HuffmanTable TBTable[]=
- X {
- X { 25, 0x37, 10, 0 }, { 25, 0x02, 3, 1 }, { 25, 0x03, 2, 2 },
- X { 25, 0x02, 2, 3 }, { 25, 0x03, 3, 4 }, { 25, 0x03, 4, 5 },
- X { 25, 0x02, 4, 6 }, { 25, 0x03, 5, 7 }, { 25, 0x05, 6, 8 },
- X { 25, 0x04, 6, 9 }, { 25, 0x04, 7, 10 }, { 25, 0x05, 7, 11 },
- X { 25, 0x07, 7, 12 }, { 25, 0x04, 8, 13 }, { 25, 0x07, 8, 14 },
- X { 25, 0x18, 9, 15 }, { 25, 0x17, 10, 16 }, { 25, 0x18, 10, 17 },
- X { 25, 0x8, 10, 18 }, { 25, 0x67, 11, 19 }, { 25, 0x68, 11, 20 },
- X { 25, 0x6c, 11, 21 }, { 25, 0x37, 11, 22 }, { 25, 0x28, 11, 23 },
- X { 25, 0x17, 11, 24 }, { 25, 0x18, 11, 25 }, { 25, 0xca, 12, 26 },
- X { 25, 0xcb, 12, 27 }, { 25, 0xcc, 12, 28 }, { 25, 0xcd, 12, 29 },
- X { 25, 0x68, 12, 30 }, { 25, 0x69, 12, 31 }, { 25, 0x6a, 12, 32 },
- X { 25, 0x6b, 12, 33 }, { 25, 0xd2, 12, 34 }, { 25, 0xd3, 12, 35 },
- X { 25, 0xd4, 12, 36 }, { 25, 0xd5, 12, 37 }, { 25, 0xd6, 12, 38 },
- X { 25, 0xd7, 12, 39 }, { 25, 0x6c, 12, 40 }, { 25, 0x6d, 12, 41 },
- X { 25, 0xda, 12, 42 }, { 25, 0xdb, 12, 43 }, { 25, 0x54, 12, 44 },
- X { 25, 0x55, 12, 45 }, { 25, 0x56, 12, 46 }, { 25, 0x57, 12, 47 },
- X { 25, 0x64, 12, 48 }, { 25, 0x65, 12, 49 }, { 25, 0x52, 12, 50 },
- X { 25, 0x53, 12, 51 }, { 25, 0x24, 12, 52 }, { 25, 0x37, 12, 53 },
- X { 25, 0x38, 12, 54 }, { 25, 0x27, 12, 55 }, { 25, 0x28, 12, 56 },
- X { 25, 0x58, 12, 57 }, { 25, 0x59, 12, 58 }, { 25, 0x2b, 12, 59 },
- X { 25, 0x2c, 12, 60 }, { 25, 0x5a, 12, 61 }, { 25, 0x66, 12, 62 },
- X { 25, 0x67, 12, 63 },
- X };
- X
- X static HuffmanTable TWTable[]=
- X {
- X { 23, 0x35, 8, 0 }, { 23, 0x07, 6, 1 }, { 23, 0x07, 4, 2 },
- X { 23, 0x08, 4, 3 }, { 23, 0x0b, 4, 4 }, { 23, 0x0c, 4, 5 },
- X { 23, 0x0e, 4, 6 }, { 23, 0x0f, 4, 7 }, { 23, 0x13, 5, 8 },
- X { 23, 0x14, 5, 9 }, { 23, 0x07, 5, 10 }, { 23, 0x08, 5, 11 },
- X { 23, 0x08, 6, 12 }, { 23, 0x03, 6, 13 }, { 23, 0x34, 6, 14 },
- X { 23, 0x35, 6, 15 }, { 23, 0x2a, 6, 16 }, { 23, 0x2b, 6, 17 },
- X { 23, 0x27, 7, 18 }, { 23, 0x0c, 7, 19 }, { 23, 0x08, 7, 20 },
- X { 23, 0x17, 7, 21 }, { 23, 0x03, 7, 22 }, { 23, 0x04, 7, 23 },
- X { 23, 0x28, 7, 24 }, { 23, 0x2b, 7, 25 }, { 23, 0x13, 7, 26 },
- X { 23, 0x24, 7, 27 }, { 23, 0x18, 7, 28 }, { 23, 0x02, 8, 29 },
- X { 23, 0x03, 8, 30 }, { 23, 0x1a, 8, 31 }, { 23, 0x1b, 8, 32 },
- X { 23, 0x12, 8, 33 }, { 23, 0x13, 8, 34 }, { 23, 0x14, 8, 35 },
- X { 23, 0x15, 8, 36 }, { 23, 0x16, 8, 37 }, { 23, 0x17, 8, 38 },
- X { 23, 0x28, 8, 39 }, { 23, 0x29, 8, 40 }, { 23, 0x2a, 8, 41 },
- X { 23, 0x2b, 8, 42 }, { 23, 0x2c, 8, 43 }, { 23, 0x2d, 8, 44 },
- X { 23, 0x04, 8, 45 }, { 23, 0x05, 8, 46 }, { 23, 0x0a, 8, 47 },
- X { 23, 0x0b, 8, 48 }, { 23, 0x52, 8, 49 }, { 23, 0x53, 8, 50 },
- X { 23, 0x54, 8, 51 }, { 23, 0x55, 8, 52 }, { 23, 0x24, 8, 53 },
- X { 23, 0x25, 8, 54 }, { 23, 0x58, 8, 55 }, { 23, 0x59, 8, 56 },
- X { 23, 0x5a, 8, 57 }, { 23, 0x5b, 8, 58 }, { 23, 0x4a, 8, 59 },
- X { 23, 0x4b, 8, 60 }, { 23, 0x32, 8, 61 }, { 23, 0x33, 8, 62 },
- X { 23, 0x34, 8, 63 },
- X };
- X
- X HuffmanTable*
- X entry;
- X
- X int
- X c,
- X k,
- X n,
- X x;
- X
- X register int
- X i,
- X j;
- X
- X register RunlengthPacket
- X *p;
- X
- X register unsigned char
- X *q;
- X
- X register unsigned short
- X polarity;
- X
- X unsigned int
- X mask;
- X
- X unsigned char
- X bit,
- X byte,
- X *scanline;
- X
- X /*
- X Allocate scanline buffer.
- X */
- X scanline=(unsigned char *)
- X malloc(Max(image->columns,1728)*sizeof(unsigned char));
- X if (scanline == (unsigned char *) NULL)
- X {
- X Warning("unable to allocate memory",(char *) NULL);
- X return(False);
- X }
- X /*
- X Compress MIFF to 1D Huffman encoded pixels.
- X */
- X q=scanline;
- X polarity=(Intensity(image->colormap[0]) >
- X Intensity(image->colormap[1]) ? 0 : 1);
- X for (i=0; i < Max(image->columns,1728); i++)
- X *q++=polarity;
- X byte=0;
- X bit=0x80;
- X p=image->pixels;
- X q=scanline;
- X x=0;
- X for (i=0; i < image->packets; i++)
- X {
- X for (j=0; j <= ((int) p->length); j++)
- X {
- X if (p->index == polarity)
- X *q++=(unsigned char) polarity;
- X else
- X *q++=(unsigned char) !polarity;
- X x++;
- X if (x == image->columns)
- X {
- X /*
- X Huffman encode scanline.
- X */
- X q=scanline;
- X n=Max(image->columns,1728);
- X while (n > 0)
- X {
- X /*
- X Find white run.
- X */
- X c=0;
- X while((*q == polarity) && (n > 0))
- X {
- X q++;
- X c++;
- X n--;
- X }
- X /*
- X Output white run.
- X */
- X if (c >= 64)
- X {
- X entry=MWTable+((c/64)-1);
- X c-=entry->count;
- X HuffmanOutputCode(entry);
- X }
- X entry=TWTable+c;
- X HuffmanOutputCode(entry);
- X c=0;
- X if (n == 0)
- X break;
- X /*
- X Find black run.
- X */
- X while ((*q != polarity) && (n > 0))
- X {
- X q++;
- X c++;
- X n--;
- X }
- X /*
- X Output black run.
- X */
- X if (c >= 64)
- X {
- X entry=MBTable+((c/64)-1);
- X c-=entry->count;
- X HuffmanOutputCode(entry);
- X }
- X entry=TBTable+c;
- X HuffmanOutputCode(entry);
- X if (n == 0)
- X break;
- X }
- X /*
- X End of line.
- X */
- X for (k=0; k < 11; k++)
- X OutputBit(0);
- X OutputBit(1);
- X x=0;
- X q=scanline;
- X }
- X }
- X p++;
- X }
- X /*
- X End of page.
- X */
- X for (i=0; i < 6; i++)
- X {
- X /*
- X End of line.
- X */
- X for (k=0; k < 11; k++)
- X OutputBit(0);
- X OutputBit(1);
- X }
- X /*
- X Flush bits.
- X */
- X if (bit != 0x80)
- X (void) fputc((char) byte,image->file);
- X (void) free((char *) scanline);
- X return(True);
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L Z W D e c o d e I m a g e %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LZWDecodeImage uncompresses an image via LZW-coding.
- %
- % The format of the LZWDecodeImage routine is:
- %
- % status=LZWDecodeImage(image)
- %
- % A description of each parameter follows:
- %
- % o status: Function LZWDecodeImage returns True if all the pixels are
- % uncompressed without error, otherwise False.
- %
- % o image: The address of a structure of type Image.
- %
- %
- */
- unsigned int LZWDecodeImage(image)
- Image
- X *image;
- {
- #define MaxStackSize 4096
- #define NullCode (-1)
- X
- X int
- X available,
- X clear,
- X code_mask,
- X code_size,
- X end_of_information,
- X in_code,
- X old_code;
- X
- X register int
- X bits,
- X code,
- X count,
- X i;
- X
- X register RunlengthPacket
- X *p;
- X
- X register unsigned char
- X *c;
- X
- X register unsigned int
- X datum;
- X
- X short
- X *prefix;
- X
- X unsigned char
- X data_size,
- X first,
- X *packet,
- X *pixel_stack,
- X *suffix,
- X *top_stack;
- X
- X unsigned short
- X index;
- X
- X /*
- X Allocate decoder tables.
- X */
- X packet=(unsigned char *) malloc(256*sizeof(unsigned char));
- X prefix=(short *) malloc(MaxStackSize*sizeof(short));
- X suffix=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
- X pixel_stack=(unsigned char *) malloc(MaxStackSize*sizeof(unsigned char));
- X if ((packet == (unsigned char *) NULL) ||
- X (prefix == (short *) NULL) ||
- X (suffix == (unsigned char *) NULL) ||
- X (pixel_stack == (unsigned char *) NULL))
- X return(False);
- X /*
- X Initialize LZW data stream decoder.
- X */
- X data_size=fgetc(image->file);
- X clear=1 << data_size;
- X end_of_information=clear+1;
- X available=clear+2;
- X old_code=NullCode;
- X code_size=data_size+1;
- X code_mask=(1 << code_size)-1;
- X for (code=0; code < clear; code++)
- X {
- X prefix[code]=0;
- X suffix[code]=code;
- X }
- X /*
- X Decode LZW pixel stream.
- X */
- X datum=0;
- X bits=0;
- X c=0;
- X count=0;
- X first=0;
- X top_stack=pixel_stack;
- X p=image->pixels;
- X for (i=0; i < image->packets; )
- X {
- X if (top_stack == pixel_stack)
- X {
- X if (bits < code_size)
- X {
- X /*
- X Load bytes until there is enough bits for a code.
- X */
- X if (count == 0)
- X {
- X /*
- X Read a new data block.
- X */
- X count=ReadDataBlock((char *) packet,image->file);
- X if (count <= 0)
- X break;
- X c=packet;
- X }
- X datum+=(*c) << bits;
- X bits+=8;
- X c++;
- X count--;
- X continue;
- X }
- X /*
- X Get the next code.
- X */
- X code=datum & code_mask;
- X datum>>=code_size;
- X bits-=code_size;
- X /*
- X Interpret the code
- X */
- X if ((code > available) || (code == end_of_information))
- X break;
- X if (code == clear)
- X {
- X /*
- X Reset decoder.
- X */
- X code_size=data_size+1;
- X code_mask=(1 << code_size)-1;
- X available=clear+2;
- X old_code=NullCode;
- X continue;
- X }
- X if (old_code == NullCode)
- X {
- X *top_stack++=suffix[code];
- X old_code=code;
- X first=code;
- X continue;
- X }
- X in_code=code;
- X if (code == available)
- X {
- X *top_stack++=first;
- X code=old_code;
- X }
- X while (code > clear)
- X {
- X *top_stack++=suffix[code];
- X code=prefix[code];
- X }
- X first=suffix[code];
- X /*
- X Add a new string to the string table,
- X */
- X *top_stack++=first;
- X prefix[available]=old_code;
- X suffix[available]=first;
- X available++;
- X if (((available & code_mask) == 0) && (available < MaxStackSize))
- X {
- X code_size++;
- X code_mask+=available;
- X }
- X old_code=in_code;
- X }
- X /*
- X Pop a pixel off the pixel stack.
- X */
- X top_stack--;
- X index=(unsigned short) *top_stack;
- X p->red=image->colormap[index].red;
- X p->green=image->colormap[index].green;
- X p->blue=image->colormap[index].blue;
- X p->index=index;
- X p->length=0;
- X p++;
- X i++;
- X }
- X /*
- X Initialize any remaining color packets to a known color.
- X */
- X for ( ; i < image->packets; i++)
- X {
- X p->red=image->colormap[0].red;
- X p->green=image->colormap[0].green;
- X p->blue=image->colormap[0].blue;
- X p->index=0;
- X p->length=0;
- X p++;
- X }
- X /*
- X Free decoder memory.
- X */
- X (void) free((char *) pixel_stack);
- X (void) free((char *) suffix);
- X (void) free((char *) prefix);
- X (void) free((char *) packet);
- X return(True);
- }
- X
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L Z W E n c o d e F i l t e r %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LZWEncodeFilter compresses an image via LZW-coding specific to
- % Postscript Level II.
- %
- % The format of the LZWEncodeFilter routine is:
- %
- % status=LZWEncodeFilter(file,pixels,number_pixels)
- %
- % A description of each parameter follows:
- %
- % o status: Function LZWEncodeFilter returns True if all the pixels are
- % compressed without error, otherwise False.
- %
- % o file: The address of a structure of type FILE. LZW encoded pixels
- % are written to this file.
- %
- % o pixels: The address of an unsigned array of characters containing the
- % pixels to compress.
- %
- % o number_pixels: An unsigned interger that specifies the number of
- % pixels to compress.
- %
- %
- */
- unsigned int LZWEncodeFilter(file,pixels,number_pixels)
- FILE
- X *file;
- X
- unsigned char
- X *pixels;
- X
- unsigned int
- X number_pixels;
- {
- #define LZWClr 256 /* Clear Table Marker */
- #define LZWEod 257 /* End of Data marker */
- #define OutputCode(code) \
- { \
- X accumulator+=((long) code) << (32-code_width-number_bits); \
- X number_bits+=code_width; \
- X while (number_bits >= 8) \
- X { \
- X PrintByte(accumulator >> 24); \
- X accumulator=accumulator << 8; \
- X number_bits-=8; \
- X } \
- }
- #define PrintByte(value) \
- { \
- X (void) fprintf(file,"%02x",(int) (value & 0x0ff)); \
- X count++; \
- X if (count >= 36) \
- X { \
- X (void) fputc('\n',file); \
- X count=0; \
- X } \
- }
- X
- X typedef struct _TableType
- X {
- X short
- X prefix,
- X suffix,
- X next;
- X } TableType;
- X
- X int
- X index;
- X
- X register int
- X i;
- X
- X short
- X number_bits,
- X code_width,
- X count,
- X last_code,
- X next_index;
- X
- X TableType
- X *table;
- X
- X unsigned long
- X accumulator;
- X
- X /*
- X Allocate string table.
- X */
- X table=(TableType *) malloc((1 << 12)*sizeof(TableType));
- X if (table == (TableType *) NULL)
- X return(False);
- X /*
- X Initialize variables.
- X */
- X accumulator=0;
- X code_width=9;
- X count=0;
- X number_bits=0;
- X last_code=0;
- X OutputCode(LZWClr);
- X for (index=0; index < 256; index++)
- X {
- X table[index].prefix=(-1);
- X table[index].suffix=index;
- X table[index].next=(-1);
- X }
- X next_index=LZWEod+1;
- X code_width=9;
- X last_code=pixels[0];
- X for (i=1; i < number_pixels; i++)
- X {
- X /*
- X Find string.
- X */
- X index=last_code;
- X while (index != -1)
- X if ((table[index].prefix != last_code) ||
- X (table[index].suffix != pixels[i]))
- X index=table[index].next;
- X else
- X {
- X last_code=index;
- X break;
- X }
- X if (last_code != index)
- SHAR_EOF
- true || echo 'restore of ImageMagick/compress.c failed'
- fi
- echo 'End of ImageMagick part 20'
- echo 'File ImageMagick/compress.c is continued in part 21'
- echo 21 > _shar_seq_.tmp
- exit 0
-
- exit 0 # Just in case...
- --
- // chris@Sterling.COM | Send comp.sources.x submissions to:
- \X/ Amiga - The only way to fly! | sources-x@sterling.com
- "It's intuitively obvious to the |
- most casual observer..." | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
-