00001 /* 00002 pack_c.h: (de)compress diffraction image files 00003 Copyright (C) 1995 Jan P Abrahams 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later 00009 version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free 00018 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301 USA 00020 */ 00021 00022 00023 00024 /* Some general defines: */ 00025 00026 00027 #define PACKIDENTIFIER "\nCCP4 packed image, X: %04d, Y: %04d\n" 00028 /* This string defines the start of a packed image. An image file is scanned 00029 until this string is encountered, the size of the unpacked image is 00030 determined from the values of X and Y (which are written out as formatted 00031 ascii numbers), and the packed image is expected to start immediately after 00032 the null-character ending the string. */ 00033 00034 #define V2IDENTIFIER "\nCCP4 packed image V2, X: %04d, Y: %04d\n" 00035 /* This string defines the start of a packed image. An image file is scanned 00036 until this string is encountered, the size of the unpacked image is 00037 determined from the values of X and Y (which are written out as formatted 00038 ascii numbers), and the packed image is expected to start immediately after 00039 the null-character ending the string. */ 00040 00041 #define PACKBUFSIZ BUFSIZ 00042 /* Size of internal buffer in which the packed array is stored during transit 00043 form an unpacked image to a packed image on disk. It is set to the size 00044 used by the buffered io-routines given in <stdio.h>, but it could be 00045 anything. */ 00046 00047 #define DIFFBUFSIZ 16384L 00048 /* Size of the internal buffer in which the differences between neighbouring 00049 pixels are stored prior to compression. The image is therefore compressed 00050 in DIFFBUFSIZ chunks. Decompression does not need to know what DIFFBUFSIZ 00051 was when the image was compressed. By increasing this value, the image 00052 can be compressed into a packed image which is a few bytes smaller. Do 00053 not decrease the value of DIFFBUFSIZ below 128L. */ 00054 00055 #define BYTE char 00056 /* BYTE is a one byte integer. */ 00057 00058 #define WORD short int 00059 /* WORD is a two-byte integer. */ 00060 00061 #define LONG int 00062 /* LONG is a four byte integer. */ 00063 /* Dave Love 5/7/94: using `int' gets you 4 bytes on the 32-bit Unix 00064 (and VAX) systems I know of and also on (64-bit) OSF/1 Alphas which 00065 have 64-bit longs. (This definition previously used `long'.) */ 00066 00067 00068 00069 /******************************************************************************/ 00070 00071 /* Some usefull macros used in the code of this sourcefile: */ 00072 00073 00074 #define max(x, y) (((x) > (y)) ? (x) : (y)) 00075 /* Returns maximum of x and y. */ 00076 00077 #define min(x, y) (((x) < (y)) ? (x) : (y)) 00078 /* Returns minimum of x and y. */ 00079 00080 #undef abs /* avoid complaint from DEC C, at least */ 00081 #define abs(x) (((x) < 0) ? (-(x)) : (x)) 00082 /* Returns the absolute value of x. */ 00083 00084 /* Used to be 'static const LONG' but const declaration gives trouble on HPs */ 00085 #ifndef SKIP_SETBITS 00086 static LONG setbits[33] = 00087 {0x00000000L, 0x00000001L, 0x00000003L, 0x00000007L, 00088 0x0000000FL, 0x0000001FL, 0x0000003FL, 0x0000007FL, 00089 0x000000FFL, 0x000001FFL, 0x000003FFL, 0x000007FFL, 00090 0x00000FFFL, 0x00001FFFL, 0x00003FFFL, 0x00007FFFL, 00091 0x0000FFFFL, 0x0001FFFFL, 0x0003FFFFL, 0x0007FFFFL, 00092 0x000FFFFFL, 0x001FFFFFL, 0x003FFFFFL, 0x007FFFFFL, 00093 0x00FFFFFFL, 0x01FFFFFFL, 0x03FFFFFFL, 0x07FFFFFFL, 00094 0x0FFFFFFFL, 0x1FFFFFFFL, 0x3FFFFFFFL, 0x7FFFFFFFL, 00095 0xFFFFFFFFL}; 00096 /* This is not a macro really, but I've included it here anyway. Upon indexing, 00097 it returns a LONG with the lower (index) number of bits set. It is equivalent 00098 to the following macro: 00099 #define setbits(n) (((n) == 32) : ((1L << (n)) - 1) : (-1L)) 00100 Indexing the const array should usually be slightly faster. */ 00101 #endif 00102 00103 #define shift_left(x, n) (((x) & setbits[32 - (n)]) << (n)) 00104 /* This macro is included because the C standard does not properly define a 00105 left shift: on some machines the bits which are pushed out at the left are 00106 popped back in at the right. By masking, the macro prevents this behaviour. 00107 If you are sure that your machine does not pops bits back in, you can speed 00108 up the code insignificantly by taking out the masking. */ 00109 00110 #define shift_right(x, n) (((x) >> (n)) & setbits[32 - (n)]) 00111 /* See comment on left shift. */ 00112 00113 00114 00115 /******************************************************************************/ 00116 00117 00118 00119 00120 /* Functions required for packing: */ 00121 00122 #if defined (PROTOTYPE) 00123 void v2pack_wordimage_c(WORD *img, int x, int y, char *filename); 00124 /* Pack image 'img', containing 'x * y' WORD-sized pixels into 'filename'. 00125 This function generates Version 2 images! */ 00126 00127 void v2pack_longimage_c(LONG *img, int x, int y, char *filename); 00128 /* Pack image 'img', containing 'x * y' LONG-sized pixels into 'filename'. 00129 This function generates Version 2 images! */ 00130 00131 00132 /* Functions required for unpacking: */ 00133 00134 00135 void readpack_word_c(WORD *img, char *filename); 00136 /* Unpacks packed image from 'filename' into the WORD-array 'img'. Scans the 00137 file defined by 'filename' until the PACKIDENTIFIER is found, then unpacks 00138 starting from there. */ 00139 00140 void readpack_long_c(LONG *img, char *filename); 00141 /* Unpacks packed image from 'filename' into the LONG-array 'img'. Scans the 00142 file defined by 'filename' until the PACKIDENTIFIER is found, then unpacks 00143 starting from there. */ 00144 00145 void imsiz_c(char *filename, LONG *x, LONG *y); 00146 /* Determines the size of the the packed image "filename" after unpacking. The 00147 dimensions are returned in x and y. */ 00148 00149 #endif /* (PROTOTYPE) */ 00150