gpp4  1.3.1
pack_c.h
1 /*
2  pack_c.h: (de)compress diffraction image files
3  Copyright (C) 1995 Jan P Abrahams
4 
5  This library is free software: you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation, either
8  version 3 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with This library. If not, see
17  <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 
22 /* Some general defines: */
23 
24 
25 #define PACKIDENTIFIER "\nCCP4 packed image, X: %04d, Y: %04d\n"
26 /* This string defines the start of a packed image. An image file is scanned
27  until this string is encountered, the size of the unpacked image is
28  determined from the values of X and Y (which are written out as formatted
29  ascii numbers), and the packed image is expected to start immediately after
30  the null-character ending the string. */
31 
32 #define V2IDENTIFIER "\nCCP4 packed image V2, X: %04d, Y: %04d\n"
33 /* This string defines the start of a packed image. An image file is scanned
34  until this string is encountered, the size of the unpacked image is
35  determined from the values of X and Y (which are written out as formatted
36  ascii numbers), and the packed image is expected to start immediately after
37  the null-character ending the string. */
38 
39 #define PACKBUFSIZ BUFSIZ
40 /* Size of internal buffer in which the packed array is stored during transit
41  form an unpacked image to a packed image on disk. It is set to the size
42  used by the buffered io-routines given in <stdio.h>, but it could be
43  anything. */
44 
45 #define DIFFBUFSIZ 16384L
46 /* Size of the internal buffer in which the differences between neighbouring
47  pixels are stored prior to compression. The image is therefore compressed
48  in DIFFBUFSIZ chunks. Decompression does not need to know what DIFFBUFSIZ
49  was when the image was compressed. By increasing this value, the image
50  can be compressed into a packed image which is a few bytes smaller. Do
51  not decrease the value of DIFFBUFSIZ below 128L. */
52 
53 #define BYTE char
54 /* BYTE is a one byte integer. */
55 
56 #define WORD short int
57 /* WORD is a two-byte integer. */
58 
59 #define LONG int
60 /* LONG is a four byte integer. */
61 /* Dave Love 5/7/94: using `int' gets you 4 bytes on the 32-bit Unix
62  (and VAX) systems I know of and also on (64-bit) OSF/1 Alphas which
63  have 64-bit longs. (This definition previously used `long'.) */
64 
65 
66 
67 /******************************************************************************/
68 
69 /* Some usefull macros used in the code of this sourcefile: */
70 
71 
72 #define max(x, y) (((x) > (y)) ? (x) : (y))
73 /* Returns maximum of x and y. */
74 
75 #define min(x, y) (((x) < (y)) ? (x) : (y))
76 /* Returns minimum of x and y. */
77 
78 #undef abs /* avoid complaint from DEC C, at least */
79 #define abs(x) (((x) < 0) ? (-(x)) : (x))
80 /* Returns the absolute value of x. */
81 
82 /* Used to be 'static const LONG' but const declaration gives trouble on HPs */
83 #ifndef SKIP_SETBITS
84 static LONG setbits[33] =
85  {0x00000000L, 0x00000001L, 0x00000003L, 0x00000007L,
86  0x0000000FL, 0x0000001FL, 0x0000003FL, 0x0000007FL,
87  0x000000FFL, 0x000001FFL, 0x000003FFL, 0x000007FFL,
88  0x00000FFFL, 0x00001FFFL, 0x00003FFFL, 0x00007FFFL,
89  0x0000FFFFL, 0x0001FFFFL, 0x0003FFFFL, 0x0007FFFFL,
90  0x000FFFFFL, 0x001FFFFFL, 0x003FFFFFL, 0x007FFFFFL,
91  0x00FFFFFFL, 0x01FFFFFFL, 0x03FFFFFFL, 0x07FFFFFFL,
92  0x0FFFFFFFL, 0x1FFFFFFFL, 0x3FFFFFFFL, 0x7FFFFFFFL,
93  0xFFFFFFFFL};
94 /* This is not a macro really, but I've included it here anyway. Upon indexing,
95  it returns a LONG with the lower (index) number of bits set. It is equivalent
96  to the following macro:
97  #define setbits(n) (((n) == 32) : ((1L << (n)) - 1) : (-1L))
98  Indexing the const array should usually be slightly faster. */
99 #endif
100 
101 #define shift_left(x, n) (((x) & setbits[32 - (n)]) << (n))
102 /* This macro is included because the C standard does not properly define a
103  left shift: on some machines the bits which are pushed out at the left are
104  popped back in at the right. By masking, the macro prevents this behaviour.
105  If you are sure that your machine does not pops bits back in, you can speed
106  up the code insignificantly by taking out the masking. */
107 
108 #define shift_right(x, n) (((x) >> (n)) & setbits[32 - (n)])
109 /* See comment on left shift. */
110 
111 
112 
113 /******************************************************************************/
114 
115 
116 
117 
118 /* Functions required for packing: */
119 
120 #if defined (PROTOTYPE)
121 void v2pack_wordimage_c(WORD *img, int x, int y, char *filename);
122 /* Pack image 'img', containing 'x * y' WORD-sized pixels into 'filename'.
123  This function generates Version 2 images! */
124 
125 void v2pack_longimage_c(LONG *img, int x, int y, char *filename);
126 /* Pack image 'img', containing 'x * y' LONG-sized pixels into 'filename'.
127  This function generates Version 2 images! */
128 
129 
130 /* Functions required for unpacking: */
131 
132 
133 void readpack_word_c(WORD *img, char *filename);
134 /* Unpacks packed image from 'filename' into the WORD-array 'img'. Scans the
135  file defined by 'filename' until the PACKIDENTIFIER is found, then unpacks
136  starting from there. */
137 
138 void readpack_long_c(LONG *img, char *filename);
139 /* Unpacks packed image from 'filename' into the LONG-array 'img'. Scans the
140  file defined by 'filename' until the PACKIDENTIFIER is found, then unpacks
141  starting from there. */
142 
143 void imsiz_c(char *filename, LONG *x, LONG *y);
144 /* Determines the size of the the packed image "filename" after unpacking. The
145  dimensions are returned in x and y. */
146 
147 #endif /* (PROTOTYPE) */
148