| 1 |
|
| /* PDL: copied from CFITSIO 4.5.0, modified so rdecomp returns char *, and rcomp takes char ** first for error messages */ |
| 2 |
|
| /* replace EOF with -1 */ |
| 3 |
|
| /* remove fitsio2.h and string.h */ |
| 4 |
|
| /* replace ffpmsg with *ret = OR return */ |
| 5 |
|
| /* end of PDL bit */ |
| 6 |
|
| /* |
| 7 |
|
| The following code was written by Richard White at STScI and made |
| 8 |
|
| available for use in CFITSIO in July 1999. These routines were |
| 9 |
|
| originally contained in 2 source files: rcomp.c and rdecomp.c, |
| 10 |
|
| and the 'include' file now called ricecomp.h was originally called buffer.h. |
| 11 |
|
| |
| 12 |
|
| Note that beginning with CFITSIO v3.08, EOB checking was removed to improve |
| 13 |
|
| speed, and so now the input compressed bytes buffers must have been |
| 14 |
|
| allocated big enough so that they will never be overflowed. A simple |
| 15 |
|
| rule of thumb that guarantees the buffer will be large enough is to make |
| 16 |
|
| it 1% larger than the size of the input array of pixels that are being |
| 17 |
|
| compressed. |
| 18 |
|
| |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| /*----------------------------------------------------------*/ |
| 22 |
|
| /* */ |
| 23 |
|
| /* START OF SOURCE FILE ORIGINALLY CALLED rcomp.c */ |
| 24 |
|
| /* */ |
| 25 |
|
| /*----------------------------------------------------------*/ |
| 26 |
|
| /* @(#) rcomp.c 1.5 99/03/01 12:40:27 */ |
| 27 |
|
| /* rcomp.c Compress image line using |
| 28 |
|
| * (1) Difference of adjacent pixels |
| 29 |
|
| * (2) Rice algorithm coding |
| 30 |
|
| * |
| 31 |
|
| * Returns number of bytes written to code buffer or |
| 32 |
|
| * -1 on failure |
| 33 |
|
| */ |
| 34 |
|
| |
| 35 |
|
| #include <stdlib.h> |
| 36 |
|
| |
| 37 |
|
| /* |
| 38 |
|
| * nonzero_count is lookup table giving number of bits in 8-bit values not including |
| 39 |
|
| * leading zeros used in fits_rdecomp, fits_rdecomp_short and fits_rdecomp_byte |
| 40 |
|
| */ |
| 41 |
|
| static const int nonzero_count[256] = { |
| 42 |
|
| 0, |
| 43 |
|
| 1, |
| 44 |
|
| 2, 2, |
| 45 |
|
| 3, 3, 3, 3, |
| 46 |
|
| 4, 4, 4, 4, 4, 4, 4, 4, |
| 47 |
|
| 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 48 |
|
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 49 |
|
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 50 |
|
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 51 |
|
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 52 |
|
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 53 |
|
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 54 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 55 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 56 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 57 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 58 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 59 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 60 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 61 |
|
| 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}; |
| 62 |
|
| |
| 63 |
|
| typedef unsigned char Buffer_t; |
| 64 |
|
| |
| 65 |
|
| typedef struct { |
| 66 |
|
| int bitbuffer; /* bit buffer */ |
| 67 |
|
| int bits_to_go; /* bits to go in buffer */ |
| 68 |
|
| Buffer_t *start; /* start of buffer */ |
| 69 |
|
| Buffer_t *current; /* current position in buffer */ |
| 70 |
|
| Buffer_t *end; /* end of buffer */ |
| 71 |
|
| } Buffer; |
| 72 |
|
| |
| 73 |
|
| #define putcbuf(c,mf) ((*(mf->current)++ = c), 0) |
| 74 |
|
| |
| 75 |
|
| static void start_outputing_bits(Buffer *buffer); |
| 76 |
|
| static int done_outputing_bits(Buffer *buffer); |
| 77 |
|
| static int output_nbits(Buffer *buffer, int bits, int n); |
| 78 |
|
| |
| 79 |
|
| /* only used for diagnoistics |
| 80 |
|
| static int case1, case2, case3; |
| 81 |
|
| int fits_get_case(int *c1, int*c2, int*c3) { |
| 82 |
|
| |
| 83 |
|
| *c1 = case1; |
| 84 |
|
| *c2 = case2; |
| 85 |
|
| *c3 = case3; |
| 86 |
|
| return(0); |
| 87 |
|
| } |
| 88 |
|
| */ |
| 89 |
|
| |
| 90 |
|
| /* this routine used to be called 'rcomp' (WDP) */ |
| 91 |
|
| /*---------------------------------------------------------------------------*/ |
| 92 |
|
| |
| 93 |
1920 |
| int fits_rcomp(char **ret, int a[], /* input array */ |
| 94 |
|
| int nx, /* number of input pixels */ |
| 95 |
|
| unsigned char *c, /* output buffer */ |
| 96 |
|
| int clen, /* max length of output */ |
| 97 |
|
| int nblock) /* coding block size */ |
| 98 |
|
| { |
| 99 |
1920 |
| Buffer bufmem, *buffer = &bufmem; |
| 100 |
|
| /* int bsize; */ |
| 101 |
|
| int i, j, thisblock; |
| 102 |
|
| int lastpix, nextpix, pdiff; |
| 103 |
|
| int v, fs, fsmask, top, fsmax, fsbits, bbits; |
| 104 |
|
| int lbitbuffer, lbits_to_go; |
| 105 |
|
| unsigned int psum; |
| 106 |
|
| double pixelsum, dpsum; |
| 107 |
|
| unsigned int *diff; |
| 108 |
|
| |
| 109 |
|
| /* |
| 110 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 111 |
|
| * size (nblock, pixels) |
| 112 |
|
| * Could make bsize a parameter to allow more efficient |
| 113 |
|
| * compression of short & byte images. |
| 114 |
|
| */ |
| 115 |
|
| /* bsize = 4; */ |
| 116 |
|
| |
| 117 |
|
| /* nblock = 32; now an input parameter*/ |
| 118 |
|
| /* |
| 119 |
|
| * From bsize derive: |
| 120 |
|
| * FSBITS = # bits required to store FS |
| 121 |
|
| * FSMAX = maximum value for FS |
| 122 |
|
| * BBITS = bits/pixel for direct coding |
| 123 |
|
| */ |
| 124 |
|
| |
| 125 |
|
| /* |
| 126 |
|
| switch (bsize) { |
| 127 |
|
| case 1: |
| 128 |
|
| fsbits = 3; |
| 129 |
|
| fsmax = 6; |
| 130 |
|
| break; |
| 131 |
|
| case 2: |
| 132 |
|
| fsbits = 4; |
| 133 |
|
| fsmax = 14; |
| 134 |
|
| break; |
| 135 |
|
| case 4: |
| 136 |
|
| fsbits = 5; |
| 137 |
|
| fsmax = 25; |
| 138 |
|
| break; |
| 139 |
|
| default: |
| 140 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 141 |
|
| return(-1); |
| 142 |
|
| } |
| 143 |
|
| */ |
| 144 |
|
| |
| 145 |
|
| /* move out of switch block, to tweak performance */ |
| 146 |
1920 |
| fsbits = 5; |
| 147 |
1920 |
| fsmax = 25; |
| 148 |
|
| |
| 149 |
1920 |
| bbits = 1<<fsbits; |
| 150 |
|
| |
| 151 |
|
| /* |
| 152 |
|
| * Set up buffer pointers |
| 153 |
|
| */ |
| 154 |
1920 |
| buffer->start = c; |
| 155 |
1920 |
| buffer->current = c; |
| 156 |
1920 |
| buffer->end = c+clen; |
| 157 |
1920 |
| buffer->bits_to_go = 8; |
| 158 |
|
| /* |
| 159 |
|
| * array for differences mapped to non-negative values |
| 160 |
|
| */ |
| 161 |
1920 |
| diff = (unsigned int *) malloc(nblock*sizeof(unsigned int)); |
| 162 |
1920 |
▶ | if (diff == (unsigned int *) NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (diff == (unsigned int *) NULL) { |
0 |
1920 |
1920 |
|
| 163 |
0 |
| *ret = "fits_rcomp: insufficient memory"; |
| 164 |
0 |
| return(-1); |
| 165 |
|
| } |
| 166 |
|
| /* |
| 167 |
|
| * Code in blocks of nblock pixels |
| 168 |
|
| */ |
| 169 |
1920 |
| start_outputing_bits(buffer); |
| 170 |
|
| |
| 171 |
|
| /* write out first int value to the first 4 bytes of the buffer */ |
| 172 |
1920 |
▶ | if (output_nbits(buffer, a[0], 32) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, a[0], 32) == -1) { |
0 |
1920 |
1920 |
|
| 173 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 174 |
0 |
| free(diff); |
| 175 |
0 |
| return(-1); |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
1920 |
| lastpix = a[0]; /* the first difference will always be zero */ |
| 179 |
|
| |
| 180 |
1920 |
| thisblock = nblock; |
| 181 |
24960 |
▶ | for (i=0; i<nx; i += nblock) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<nx; i += nblock) { |
23040 |
1920 |
24960 |
|
| 182 |
|
| /* last block may be shorter */ |
| 183 |
23040 |
▶ | if (nx-i < nblock) thisblock = nx-i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nx-i < nblock) thisblock = nx-i; |
0 |
23040 |
23040 |
|
| 184 |
|
| /* |
| 185 |
|
| * Compute differences of adjacent pixels and map them to unsigned values. |
| 186 |
|
| * Note that this may overflow the integer variables -- that's |
| 187 |
|
| * OK, because we can recover when decompressing. If we were |
| 188 |
|
| * compressing shorts or bytes, would want to do this arithmetic |
| 189 |
|
| * with short/byte working variables (though diff will still be |
| 190 |
|
| * passed as an int.) |
| 191 |
|
| * |
| 192 |
|
| * compute sum of mapped pixel values at same time |
| 193 |
|
| * use double precision for sum to allow 32-bit integer inputs |
| 194 |
|
| */ |
| 195 |
23040 |
| pixelsum = 0.0; |
| 196 |
760320 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
737280 |
23040 |
760320 |
|
| 197 |
737280 |
| nextpix = a[i+j]; |
| 198 |
737280 |
| pdiff = nextpix - lastpix; |
| 199 |
737280 |
▶ | diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
Branch: 1 branch
| Branch | True | False | Total |
| diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
350215 |
387065 |
737280 |
|
| 200 |
737280 |
| pixelsum += diff[j]; |
| 201 |
737280 |
| lastpix = nextpix; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| /* |
| 205 |
|
| * compute number of bits to split from sum |
| 206 |
|
| */ |
| 207 |
23040 |
| dpsum = (pixelsum - (thisblock/2) - 1)/thisblock; |
| 208 |
23040 |
▶ | if (dpsum < 0) dpsum = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpsum < 0) dpsum = 0.0; |
0 |
23040 |
23040 |
|
| 209 |
23040 |
| psum = ((unsigned int) dpsum ) >> 1; |
| 210 |
134920 |
▶ | for (fs = 0; psum>0; fs++) psum >>= 1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (fs = 0; psum>0; fs++) psum >>= 1; |
111880 |
23040 |
134920 |
|
| 211 |
|
| |
| 212 |
|
| /* |
| 213 |
|
| * write the codes |
| 214 |
|
| * fsbits ID bits used to indicate split level |
| 215 |
|
| */ |
| 216 |
23040 |
▶ | if (fs >= fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs >= fsmax) { |
0 |
23040 |
23040 |
|
| 217 |
|
| /* Special high entropy case when FS >= fsmax |
| 218 |
|
| * Just write pixel difference values directly, no Rice coding at all. |
| 219 |
|
| */ |
| 220 |
0 |
▶ | if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
0 |
0 |
0 |
|
| 221 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 222 |
0 |
| free(diff); |
| 223 |
0 |
| return(-1); |
| 224 |
|
| } |
| 225 |
0 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
0 |
0 |
0 |
|
| 226 |
0 |
▶ | if (output_nbits(buffer, diff[j], bbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, diff[j], bbits) == -1) { |
0 |
0 |
0 |
|
| 227 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 228 |
0 |
| free(diff); |
| 229 |
0 |
| return(-1); |
| 230 |
|
| } |
| 231 |
|
| } |
| 232 |
23040 |
▶ | } else if (fs == 0 && pixelsum == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fs == 0 && pixelsum == 0) { |
0 |
23040 |
23040 |
| } else if (fs == 0 && pixelsum == 0) { |
0 |
0 |
0 |
|
| 233 |
|
| /* |
| 234 |
|
| * special low entropy case when FS = 0 and pixelsum=0 (all |
| 235 |
|
| * pixels in block are zero.) |
| 236 |
|
| * Output a 0 and return |
| 237 |
|
| */ |
| 238 |
0 |
▶ | if (output_nbits(buffer, 0, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, 0, fsbits) == -1) { |
0 |
0 |
0 |
|
| 239 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 240 |
0 |
| free(diff); |
| 241 |
0 |
| return(-1); |
| 242 |
|
| } |
| 243 |
|
| } else { |
| 244 |
|
| /* normal case: not either very high or very low entropy */ |
| 245 |
23040 |
▶ | if (output_nbits(buffer, fs+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fs+1, fsbits) == -1) { |
0 |
23040 |
23040 |
|
| 246 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 247 |
0 |
| free(diff); |
| 248 |
0 |
| return(-1); |
| 249 |
|
| } |
| 250 |
23040 |
| fsmask = (1<<fs) - 1; |
| 251 |
|
| /* |
| 252 |
|
| * local copies of bit buffer to improve optimization |
| 253 |
|
| */ |
| 254 |
23040 |
| lbitbuffer = buffer->bitbuffer; |
| 255 |
23040 |
| lbits_to_go = buffer->bits_to_go; |
| 256 |
760320 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
737280 |
23040 |
760320 |
|
| 257 |
737280 |
| v = diff[j]; |
| 258 |
737280 |
| top = v >> fs; |
| 259 |
|
| /* |
| 260 |
|
| * top is coded by top zeros + 1 |
| 261 |
|
| */ |
| 262 |
737280 |
▶ | if (lbits_to_go >= top+1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lbits_to_go >= top+1) { |
641925 |
95355 |
737280 |
|
| 263 |
641925 |
| lbitbuffer <<= top+1; |
| 264 |
641925 |
| lbitbuffer |= 1; |
| 265 |
641925 |
| lbits_to_go -= top+1; |
| 266 |
|
| } else { |
| 267 |
95355 |
| lbitbuffer <<= lbits_to_go; |
| 268 |
95355 |
| putcbuf(lbitbuffer & 0xff,buffer); |
| 269 |
|
| |
| 270 |
95880 |
▶ | for (top -= lbits_to_go; top>=8; top -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (top -= lbits_to_go; top>=8; top -= 8) { |
525 |
95355 |
95880 |
|
| 271 |
525 |
| putcbuf(0, buffer); |
| 272 |
|
| } |
| 273 |
95355 |
| lbitbuffer = 1; |
| 274 |
95355 |
| lbits_to_go = 7-top; |
| 275 |
|
| } |
| 276 |
|
| /* |
| 277 |
|
| * bottom FS bits are written without coding |
| 278 |
|
| * code is output_nbits, moved into this routine to reduce overheads |
| 279 |
|
| * This code potentially breaks if FS>24, so I am limiting |
| 280 |
|
| * FS to 24 by choice of FSMAX above. |
| 281 |
|
| */ |
| 282 |
737280 |
▶ | if (fs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs > 0) { |
737280 |
0 |
737280 |
|
| 283 |
737280 |
| lbitbuffer <<= fs; |
| 284 |
737280 |
| lbitbuffer |= v & fsmask; |
| 285 |
737280 |
| lbits_to_go -= fs; |
| 286 |
1276885 |
▶ | while (lbits_to_go <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lbits_to_go <= 0) { |
539605 |
737280 |
1276885 |
|
| 287 |
539605 |
| putcbuf((lbitbuffer>>(-lbits_to_go)) & 0xff,buffer); |
| 288 |
539605 |
| lbits_to_go += 8; |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* check if overflowed output buffer */ |
| 294 |
23040 |
▶ | if (buffer->current > buffer->end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer->current > buffer->end) { |
0 |
23040 |
23040 |
|
| 295 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 296 |
0 |
| free(diff); |
| 297 |
0 |
| return(-1); |
| 298 |
|
| } |
| 299 |
23040 |
| buffer->bitbuffer = lbitbuffer; |
| 300 |
23040 |
| buffer->bits_to_go = lbits_to_go; |
| 301 |
|
| } |
| 302 |
|
| } |
| 303 |
1920 |
| done_outputing_bits(buffer); |
| 304 |
1920 |
| free(diff); |
| 305 |
|
| /* |
| 306 |
|
| * return number of bytes used |
| 307 |
|
| */ |
| 308 |
1920 |
| *ret = NULL; |
| 309 |
1920 |
| return(buffer->current - buffer->start); |
| 310 |
|
| } |
| 311 |
|
| /*---------------------------------------------------------------------------*/ |
| 312 |
|
| |
| 313 |
0 |
| int fits_rcomp_short(char **ret, |
| 314 |
|
| short a[], /* input array */ |
| 315 |
|
| int nx, /* number of input pixels */ |
| 316 |
|
| unsigned char *c, /* output buffer */ |
| 317 |
|
| int clen, /* max length of output */ |
| 318 |
|
| int nblock) /* coding block size */ |
| 319 |
|
| { |
| 320 |
0 |
| Buffer bufmem, *buffer = &bufmem; |
| 321 |
|
| /* int bsize; */ |
| 322 |
|
| int i, j, thisblock; |
| 323 |
|
| |
| 324 |
|
| /* |
| 325 |
|
| NOTE: in principle, the following 2 variable could be declared as 'short' |
| 326 |
|
| but in fact the code runs faster (on 32-bit Linux at least) as 'int' |
| 327 |
|
| */ |
| 328 |
|
| int lastpix, nextpix; |
| 329 |
|
| /* int pdiff; */ |
| 330 |
|
| short pdiff; |
| 331 |
|
| int v, fs, fsmask, top, fsmax, fsbits, bbits; |
| 332 |
|
| int lbitbuffer, lbits_to_go; |
| 333 |
|
| /* unsigned int psum; */ |
| 334 |
|
| unsigned short psum; |
| 335 |
|
| double pixelsum, dpsum; |
| 336 |
|
| unsigned int *diff; |
| 337 |
|
| |
| 338 |
|
| /* |
| 339 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 340 |
|
| * size (nblock, pixels) |
| 341 |
|
| * Could make bsize a parameter to allow more efficient |
| 342 |
|
| * compression of short & byte images. |
| 343 |
|
| */ |
| 344 |
|
| /* bsize = 2; */ |
| 345 |
|
| |
| 346 |
|
| /* nblock = 32; now an input parameter */ |
| 347 |
|
| /* |
| 348 |
|
| * From bsize derive: |
| 349 |
|
| * FSBITS = # bits required to store FS |
| 350 |
|
| * FSMAX = maximum value for FS |
| 351 |
|
| * BBITS = bits/pixel for direct coding |
| 352 |
|
| */ |
| 353 |
|
| |
| 354 |
|
| /* |
| 355 |
|
| switch (bsize) { |
| 356 |
|
| case 1: |
| 357 |
|
| fsbits = 3; |
| 358 |
|
| fsmax = 6; |
| 359 |
|
| break; |
| 360 |
|
| case 2: |
| 361 |
|
| fsbits = 4; |
| 362 |
|
| fsmax = 14; |
| 363 |
|
| break; |
| 364 |
|
| case 4: |
| 365 |
|
| fsbits = 5; |
| 366 |
|
| fsmax = 25; |
| 367 |
|
| break; |
| 368 |
|
| default: |
| 369 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 370 |
|
| return(-1); |
| 371 |
|
| } |
| 372 |
|
| */ |
| 373 |
|
| |
| 374 |
|
| /* move these out of switch block to further tweak performance */ |
| 375 |
0 |
| fsbits = 4; |
| 376 |
0 |
| fsmax = 14; |
| 377 |
|
| |
| 378 |
0 |
| bbits = 1<<fsbits; |
| 379 |
|
| |
| 380 |
|
| /* |
| 381 |
|
| * Set up buffer pointers |
| 382 |
|
| */ |
| 383 |
0 |
| buffer->start = c; |
| 384 |
0 |
| buffer->current = c; |
| 385 |
0 |
| buffer->end = c+clen; |
| 386 |
0 |
| buffer->bits_to_go = 8; |
| 387 |
|
| /* |
| 388 |
|
| * array for differences mapped to non-negative values |
| 389 |
|
| */ |
| 390 |
0 |
| diff = (unsigned int *) malloc(nblock*sizeof(unsigned int)); |
| 391 |
0 |
▶ | if (diff == (unsigned int *) NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (diff == (unsigned int *) NULL) { |
0 |
0 |
0 |
|
| 392 |
0 |
| *ret = "fits_rcomp: insufficient memory"; |
| 393 |
0 |
| return(-1); |
| 394 |
|
| } |
| 395 |
|
| /* |
| 396 |
|
| * Code in blocks of nblock pixels |
| 397 |
|
| */ |
| 398 |
0 |
| start_outputing_bits(buffer); |
| 399 |
|
| |
| 400 |
|
| /* write out first short value to the first 2 bytes of the buffer */ |
| 401 |
0 |
▶ | if (output_nbits(buffer, a[0], 16) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, a[0], 16) == -1) { |
0 |
0 |
0 |
|
| 402 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 403 |
0 |
| free(diff); |
| 404 |
0 |
| return(-1); |
| 405 |
|
| } |
| 406 |
|
| |
| 407 |
0 |
| lastpix = a[0]; /* the first difference will always be zero */ |
| 408 |
|
| |
| 409 |
0 |
| thisblock = nblock; |
| 410 |
0 |
▶ | for (i=0; i<nx; i += nblock) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<nx; i += nblock) { |
0 |
0 |
0 |
|
| 411 |
|
| /* last block may be shorter */ |
| 412 |
0 |
▶ | if (nx-i < nblock) thisblock = nx-i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nx-i < nblock) thisblock = nx-i; |
0 |
0 |
0 |
|
| 413 |
|
| /* |
| 414 |
|
| * Compute differences of adjacent pixels and map them to unsigned values. |
| 415 |
|
| * Note that this may overflow the integer variables -- that's |
| 416 |
|
| * OK, because we can recover when decompressing. If we were |
| 417 |
|
| * compressing shorts or bytes, would want to do this arithmetic |
| 418 |
|
| * with short/byte working variables (though diff will still be |
| 419 |
|
| * passed as an int.) |
| 420 |
|
| * |
| 421 |
|
| * compute sum of mapped pixel values at same time |
| 422 |
|
| * use double precision for sum to allow 32-bit integer inputs |
| 423 |
|
| */ |
| 424 |
0 |
| pixelsum = 0.0; |
| 425 |
0 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
0 |
0 |
0 |
|
| 426 |
0 |
| nextpix = a[i+j]; |
| 427 |
0 |
| pdiff = nextpix - lastpix; |
| 428 |
0 |
▶ | diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
Branch: 1 branch
| Branch | True | False | Total |
| diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
0 |
0 |
0 |
|
| 429 |
0 |
| pixelsum += diff[j]; |
| 430 |
0 |
| lastpix = nextpix; |
| 431 |
|
| } |
| 432 |
|
| /* |
| 433 |
|
| * compute number of bits to split from sum |
| 434 |
|
| */ |
| 435 |
0 |
| dpsum = (pixelsum - (thisblock/2) - 1)/thisblock; |
| 436 |
0 |
▶ | if (dpsum < 0) dpsum = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpsum < 0) dpsum = 0.0; |
0 |
0 |
0 |
|
| 437 |
|
| /* psum = ((unsigned int) dpsum ) >> 1; */ |
| 438 |
0 |
| psum = ((unsigned short) dpsum ) >> 1; |
| 439 |
0 |
▶ | for (fs = 0; psum>0; fs++) psum >>= 1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (fs = 0; psum>0; fs++) psum >>= 1; |
0 |
0 |
0 |
|
| 440 |
|
| |
| 441 |
|
| /* |
| 442 |
|
| * write the codes |
| 443 |
|
| * fsbits ID bits used to indicate split level |
| 444 |
|
| */ |
| 445 |
0 |
▶ | if (fs >= fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs >= fsmax) { |
0 |
0 |
0 |
|
| 446 |
|
| /* case3++; */ |
| 447 |
|
| /* Special high entropy case when FS >= fsmax |
| 448 |
|
| * Just write pixel difference values directly, no Rice coding at all. |
| 449 |
|
| */ |
| 450 |
0 |
▶ | if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
0 |
0 |
0 |
|
| 451 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 452 |
0 |
| free(diff); |
| 453 |
0 |
| return(-1); |
| 454 |
|
| } |
| 455 |
0 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
0 |
0 |
0 |
|
| 456 |
0 |
▶ | if (output_nbits(buffer, diff[j], bbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, diff[j], bbits) == -1) { |
0 |
0 |
0 |
|
| 457 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 458 |
0 |
| free(diff); |
| 459 |
0 |
| return(-1); |
| 460 |
|
| } |
| 461 |
|
| } |
| 462 |
0 |
▶ | } else if (fs == 0 && pixelsum == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fs == 0 && pixelsum == 0) { |
0 |
0 |
0 |
| } else if (fs == 0 && pixelsum == 0) { |
0 |
0 |
0 |
|
| 463 |
|
| /* case1++; */ |
| 464 |
|
| /* |
| 465 |
|
| * special low entropy case when FS = 0 and pixelsum=0 (all |
| 466 |
|
| * pixels in block are zero.) |
| 467 |
|
| * Output a 0 and return |
| 468 |
|
| */ |
| 469 |
0 |
▶ | if (output_nbits(buffer, 0, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, 0, fsbits) == -1) { |
0 |
0 |
0 |
|
| 470 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 471 |
0 |
| free(diff); |
| 472 |
0 |
| return(-1); |
| 473 |
|
| } |
| 474 |
|
| } else { |
| 475 |
|
| /* case2++; */ |
| 476 |
|
| /* normal case: not either very high or very low entropy */ |
| 477 |
0 |
▶ | if (output_nbits(buffer, fs+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fs+1, fsbits) == -1) { |
0 |
0 |
0 |
|
| 478 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 479 |
0 |
| free(diff); |
| 480 |
0 |
| return(-1); |
| 481 |
|
| } |
| 482 |
0 |
| fsmask = (1<<fs) - 1; |
| 483 |
|
| /* |
| 484 |
|
| * local copies of bit buffer to improve optimization |
| 485 |
|
| */ |
| 486 |
0 |
| lbitbuffer = buffer->bitbuffer; |
| 487 |
0 |
| lbits_to_go = buffer->bits_to_go; |
| 488 |
0 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
0 |
0 |
0 |
|
| 489 |
0 |
| v = diff[j]; |
| 490 |
0 |
| top = v >> fs; |
| 491 |
|
| /* |
| 492 |
|
| * top is coded by top zeros + 1 |
| 493 |
|
| */ |
| 494 |
0 |
▶ | if (lbits_to_go >= top+1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lbits_to_go >= top+1) { |
0 |
0 |
0 |
|
| 495 |
0 |
| lbitbuffer <<= top+1; |
| 496 |
0 |
| lbitbuffer |= 1; |
| 497 |
0 |
| lbits_to_go -= top+1; |
| 498 |
|
| } else { |
| 499 |
0 |
| lbitbuffer <<= lbits_to_go; |
| 500 |
0 |
| putcbuf(lbitbuffer & 0xff,buffer); |
| 501 |
0 |
▶ | for (top -= lbits_to_go; top>=8; top -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (top -= lbits_to_go; top>=8; top -= 8) { |
0 |
0 |
0 |
|
| 502 |
0 |
| putcbuf(0, buffer); |
| 503 |
|
| } |
| 504 |
0 |
| lbitbuffer = 1; |
| 505 |
0 |
| lbits_to_go = 7-top; |
| 506 |
|
| } |
| 507 |
|
| /* |
| 508 |
|
| * bottom FS bits are written without coding |
| 509 |
|
| * code is output_nbits, moved into this routine to reduce overheads |
| 510 |
|
| * This code potentially breaks if FS>24, so I am limiting |
| 511 |
|
| * FS to 24 by choice of FSMAX above. |
| 512 |
|
| */ |
| 513 |
0 |
▶ | if (fs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs > 0) { |
0 |
0 |
0 |
|
| 514 |
0 |
| lbitbuffer <<= fs; |
| 515 |
0 |
| lbitbuffer |= v & fsmask; |
| 516 |
0 |
| lbits_to_go -= fs; |
| 517 |
0 |
▶ | while (lbits_to_go <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lbits_to_go <= 0) { |
0 |
0 |
0 |
|
| 518 |
0 |
| putcbuf((lbitbuffer>>(-lbits_to_go)) & 0xff,buffer); |
| 519 |
0 |
| lbits_to_go += 8; |
| 520 |
|
| } |
| 521 |
|
| } |
| 522 |
|
| } |
| 523 |
|
| /* check if overflowed output buffer */ |
| 524 |
0 |
▶ | if (buffer->current > buffer->end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer->current > buffer->end) { |
0 |
0 |
0 |
|
| 525 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 526 |
0 |
| free(diff); |
| 527 |
0 |
| return(-1); |
| 528 |
|
| } |
| 529 |
0 |
| buffer->bitbuffer = lbitbuffer; |
| 530 |
0 |
| buffer->bits_to_go = lbits_to_go; |
| 531 |
|
| } |
| 532 |
|
| } |
| 533 |
0 |
| done_outputing_bits(buffer); |
| 534 |
0 |
| free(diff); |
| 535 |
|
| /* |
| 536 |
|
| * return number of bytes used |
| 537 |
|
| */ |
| 538 |
0 |
| *ret = NULL; |
| 539 |
0 |
| return(buffer->current - buffer->start); |
| 540 |
|
| } |
| 541 |
|
| /*---------------------------------------------------------------------------*/ |
| 542 |
|
| |
| 543 |
1 |
| int fits_rcomp_byte(char **ret, |
| 544 |
|
| signed char a[], /* input array */ |
| 545 |
|
| int nx, /* number of input pixels */ |
| 546 |
|
| unsigned char *c, /* output buffer */ |
| 547 |
|
| int clen, /* max length of output */ |
| 548 |
|
| int nblock) /* coding block size */ |
| 549 |
|
| { |
| 550 |
1 |
| Buffer bufmem, *buffer = &bufmem; |
| 551 |
|
| /* int bsize; */ |
| 552 |
|
| int i, j, thisblock; |
| 553 |
|
| |
| 554 |
|
| /* |
| 555 |
|
| NOTE: in principle, the following 2 variable could be declared as 'short' |
| 556 |
|
| but in fact the code runs faster (on 32-bit Linux at least) as 'int' |
| 557 |
|
| */ |
| 558 |
|
| int lastpix, nextpix; |
| 559 |
|
| /* int pdiff; */ |
| 560 |
|
| signed char pdiff; |
| 561 |
|
| int v, fs, fsmask, top, fsmax, fsbits, bbits; |
| 562 |
|
| int lbitbuffer, lbits_to_go; |
| 563 |
|
| /* unsigned int psum; */ |
| 564 |
|
| unsigned char psum; |
| 565 |
|
| double pixelsum, dpsum; |
| 566 |
|
| unsigned int *diff; |
| 567 |
|
| |
| 568 |
|
| /* |
| 569 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 570 |
|
| * size (nblock, pixels) |
| 571 |
|
| * Could make bsize a parameter to allow more efficient |
| 572 |
|
| * compression of short & byte images. |
| 573 |
|
| */ |
| 574 |
|
| /* bsize = 1; */ |
| 575 |
|
| |
| 576 |
|
| /* nblock = 32; now an input parameter */ |
| 577 |
|
| /* |
| 578 |
|
| * From bsize derive: |
| 579 |
|
| * FSBITS = # bits required to store FS |
| 580 |
|
| * FSMAX = maximum value for FS |
| 581 |
|
| * BBITS = bits/pixel for direct coding |
| 582 |
|
| */ |
| 583 |
|
| |
| 584 |
|
| /* |
| 585 |
|
| switch (bsize) { |
| 586 |
|
| case 1: |
| 587 |
|
| fsbits = 3; |
| 588 |
|
| fsmax = 6; |
| 589 |
|
| break; |
| 590 |
|
| case 2: |
| 591 |
|
| fsbits = 4; |
| 592 |
|
| fsmax = 14; |
| 593 |
|
| break; |
| 594 |
|
| case 4: |
| 595 |
|
| fsbits = 5; |
| 596 |
|
| fsmax = 25; |
| 597 |
|
| break; |
| 598 |
|
| default: |
| 599 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 600 |
|
| return(-1); |
| 601 |
|
| } |
| 602 |
|
| */ |
| 603 |
|
| |
| 604 |
|
| /* move these out of switch block to further tweak performance */ |
| 605 |
1 |
| fsbits = 3; |
| 606 |
1 |
| fsmax = 6; |
| 607 |
1 |
| bbits = 1<<fsbits; |
| 608 |
|
| |
| 609 |
|
| /* |
| 610 |
|
| * Set up buffer pointers |
| 611 |
|
| */ |
| 612 |
1 |
| buffer->start = c; |
| 613 |
1 |
| buffer->current = c; |
| 614 |
1 |
| buffer->end = c+clen; |
| 615 |
1 |
| buffer->bits_to_go = 8; |
| 616 |
|
| /* |
| 617 |
|
| * array for differences mapped to non-negative values |
| 618 |
|
| */ |
| 619 |
1 |
| diff = (unsigned int *) malloc(nblock*sizeof(unsigned int)); |
| 620 |
1 |
▶ | if (diff == (unsigned int *) NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (diff == (unsigned int *) NULL) { |
0 |
1 |
1 |
|
| 621 |
0 |
| *ret = "fits_rcomp: insufficient memory"; |
| 622 |
0 |
| return(-1); |
| 623 |
|
| } |
| 624 |
|
| /* |
| 625 |
|
| * Code in blocks of nblock pixels |
| 626 |
|
| */ |
| 627 |
1 |
| start_outputing_bits(buffer); |
| 628 |
|
| |
| 629 |
|
| /* write out first byte value to the first byte of the buffer */ |
| 630 |
1 |
▶ | if (output_nbits(buffer, a[0], 8) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, a[0], 8) == -1) { |
0 |
1 |
1 |
|
| 631 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 632 |
0 |
| free(diff); |
| 633 |
0 |
| return(-1); |
| 634 |
|
| } |
| 635 |
|
| |
| 636 |
1 |
| lastpix = a[0]; /* the first difference will always be zero */ |
| 637 |
|
| |
| 638 |
1 |
| thisblock = nblock; |
| 639 |
3 |
▶ | for (i=0; i<nx; i += nblock) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<nx; i += nblock) { |
2 |
1 |
3 |
|
| 640 |
|
| /* last block may be shorter */ |
| 641 |
2 |
▶ | if (nx-i < nblock) thisblock = nx-i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nx-i < nblock) thisblock = nx-i; |
0 |
2 |
2 |
|
| 642 |
|
| /* |
| 643 |
|
| * Compute differences of adjacent pixels and map them to unsigned values. |
| 644 |
|
| * Note that this may overflow the integer variables -- that's |
| 645 |
|
| * OK, because we can recover when decompressing. If we were |
| 646 |
|
| * compressing shorts or bytes, would want to do this arithmetic |
| 647 |
|
| * with short/byte working variables (though diff will still be |
| 648 |
|
| * passed as an int.) |
| 649 |
|
| * |
| 650 |
|
| * compute sum of mapped pixel values at same time |
| 651 |
|
| * use double precision for sum to allow 32-bit integer inputs |
| 652 |
|
| */ |
| 653 |
2 |
| pixelsum = 0.0; |
| 654 |
66 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
64 |
2 |
66 |
|
| 655 |
64 |
| nextpix = a[i+j]; |
| 656 |
64 |
| pdiff = nextpix - lastpix; |
| 657 |
64 |
▶ | diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
Branch: 1 branch
| Branch | True | False | Total |
| diff[j] = (unsigned int) ((pdiff<0) ? ~(pdiff<<1) : (pdiff<<1)); |
24 |
40 |
64 |
|
| 658 |
64 |
| pixelsum += diff[j]; |
| 659 |
64 |
| lastpix = nextpix; |
| 660 |
|
| } |
| 661 |
|
| /* |
| 662 |
|
| * compute number of bits to split from sum |
| 663 |
|
| */ |
| 664 |
2 |
| dpsum = (pixelsum - (thisblock/2) - 1)/thisblock; |
| 665 |
2 |
▶ | if (dpsum < 0) dpsum = 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dpsum < 0) dpsum = 0.0; |
0 |
2 |
2 |
|
| 666 |
|
| /* psum = ((unsigned int) dpsum ) >> 1; */ |
| 667 |
2 |
| psum = ((unsigned char) dpsum ) >> 1; |
| 668 |
2 |
▶ | for (fs = 0; psum>0; fs++) psum >>= 1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (fs = 0; psum>0; fs++) psum >>= 1; |
0 |
2 |
2 |
|
| 669 |
|
| |
| 670 |
|
| /* |
| 671 |
|
| * write the codes |
| 672 |
|
| * fsbits ID bits used to indicate split level |
| 673 |
|
| */ |
| 674 |
2 |
▶ | if (fs >= fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs >= fsmax) { |
0 |
2 |
2 |
|
| 675 |
|
| /* Special high entropy case when FS >= fsmax |
| 676 |
|
| * Just write pixel difference values directly, no Rice coding at all. |
| 677 |
|
| */ |
| 678 |
0 |
▶ | if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fsmax+1, fsbits) == -1) { |
0 |
0 |
0 |
|
| 679 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 680 |
0 |
| free(diff); |
| 681 |
0 |
| return(-1); |
| 682 |
|
| } |
| 683 |
0 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
0 |
0 |
0 |
|
| 684 |
0 |
▶ | if (output_nbits(buffer, diff[j], bbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, diff[j], bbits) == -1) { |
0 |
0 |
0 |
|
| 685 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 686 |
0 |
| free(diff); |
| 687 |
0 |
| return(-1); |
| 688 |
|
| } |
| 689 |
|
| } |
| 690 |
2 |
▶ | } else if (fs == 0 && pixelsum == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (fs == 0 && pixelsum == 0) { |
2 |
0 |
2 |
| } else if (fs == 0 && pixelsum == 0) { |
0 |
2 |
2 |
|
| 691 |
|
| /* |
| 692 |
|
| * special low entropy case when FS = 0 and pixelsum=0 (all |
| 693 |
|
| * pixels in block are zero.) |
| 694 |
|
| * Output a 0 and return |
| 695 |
|
| */ |
| 696 |
0 |
▶ | if (output_nbits(buffer, 0, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, 0, fsbits) == -1) { |
0 |
0 |
0 |
|
| 697 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 698 |
0 |
| free(diff); |
| 699 |
0 |
| return(-1); |
| 700 |
|
| } |
| 701 |
|
| } else { |
| 702 |
|
| /* normal case: not either very high or very low entropy */ |
| 703 |
2 |
▶ | if (output_nbits(buffer, fs+1, fsbits) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (output_nbits(buffer, fs+1, fsbits) == -1) { |
0 |
2 |
2 |
|
| 704 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 705 |
0 |
| free(diff); |
| 706 |
0 |
| return(-1); |
| 707 |
|
| } |
| 708 |
2 |
| fsmask = (1<<fs) - 1; |
| 709 |
|
| /* |
| 710 |
|
| * local copies of bit buffer to improve optimization |
| 711 |
|
| */ |
| 712 |
2 |
| lbitbuffer = buffer->bitbuffer; |
| 713 |
2 |
| lbits_to_go = buffer->bits_to_go; |
| 714 |
66 |
▶ | for (j=0; j<thisblock; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j<thisblock; j++) { |
64 |
2 |
66 |
|
| 715 |
64 |
| v = diff[j]; |
| 716 |
64 |
| top = v >> fs; |
| 717 |
|
| /* |
| 718 |
|
| * top is coded by top zeros + 1 |
| 719 |
|
| */ |
| 720 |
64 |
▶ | if (lbits_to_go >= top+1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lbits_to_go >= top+1) { |
47 |
17 |
64 |
|
| 721 |
47 |
| lbitbuffer <<= top+1; |
| 722 |
47 |
| lbitbuffer |= 1; |
| 723 |
47 |
| lbits_to_go -= top+1; |
| 724 |
|
| } else { |
| 725 |
17 |
| lbitbuffer <<= lbits_to_go; |
| 726 |
17 |
| putcbuf(lbitbuffer & 0xff,buffer); |
| 727 |
18 |
▶ | for (top -= lbits_to_go; top>=8; top -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (top -= lbits_to_go; top>=8; top -= 8) { |
1 |
17 |
18 |
|
| 728 |
1 |
| putcbuf(0, buffer); |
| 729 |
|
| } |
| 730 |
17 |
| lbitbuffer = 1; |
| 731 |
17 |
| lbits_to_go = 7-top; |
| 732 |
|
| } |
| 733 |
|
| /* |
| 734 |
|
| * bottom FS bits are written without coding |
| 735 |
|
| * code is output_nbits, moved into this routine to reduce overheads |
| 736 |
|
| * This code potentially breaks if FS>24, so I am limiting |
| 737 |
|
| * FS to 24 by choice of FSMAX above. |
| 738 |
|
| */ |
| 739 |
64 |
▶ | if (fs > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs > 0) { |
0 |
64 |
64 |
|
| 740 |
0 |
| lbitbuffer <<= fs; |
| 741 |
0 |
| lbitbuffer |= v & fsmask; |
| 742 |
0 |
| lbits_to_go -= fs; |
| 743 |
0 |
▶ | while (lbits_to_go <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lbits_to_go <= 0) { |
0 |
0 |
0 |
|
| 744 |
0 |
| putcbuf((lbitbuffer>>(-lbits_to_go)) & 0xff,buffer); |
| 745 |
0 |
| lbits_to_go += 8; |
| 746 |
|
| } |
| 747 |
|
| } |
| 748 |
|
| } |
| 749 |
|
| /* check if overflowed output buffer */ |
| 750 |
2 |
▶ | if (buffer->current > buffer->end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer->current > buffer->end) { |
0 |
2 |
2 |
|
| 751 |
0 |
| *ret = "rice_encode: end of buffer"; |
| 752 |
0 |
| free(diff); |
| 753 |
0 |
| return(-1); |
| 754 |
|
| } |
| 755 |
2 |
| buffer->bitbuffer = lbitbuffer; |
| 756 |
2 |
| buffer->bits_to_go = lbits_to_go; |
| 757 |
|
| } |
| 758 |
|
| } |
| 759 |
1 |
| done_outputing_bits(buffer); |
| 760 |
1 |
| free(diff); |
| 761 |
|
| /* |
| 762 |
|
| * return number of bytes used |
| 763 |
|
| */ |
| 764 |
1 |
| *ret = NULL; |
| 765 |
1 |
| return(buffer->current - buffer->start); |
| 766 |
|
| } |
| 767 |
|
| /*---------------------------------------------------------------------------*/ |
| 768 |
|
| /* bit_output.c |
| 769 |
|
| * |
| 770 |
|
| * Bit output routines |
| 771 |
|
| * Procedures return zero on success, -1 on end-of-buffer |
| 772 |
|
| * |
| 773 |
|
| * Programmer: R. White Date: 20 July 1998 |
| 774 |
|
| */ |
| 775 |
|
| |
| 776 |
|
| /* Initialize for bit output */ |
| 777 |
|
| |
| 778 |
1921 |
| static void start_outputing_bits(Buffer *buffer) |
| 779 |
|
| { |
| 780 |
|
| /* |
| 781 |
|
| * Buffer is empty to start with |
| 782 |
|
| */ |
| 783 |
1921 |
| buffer->bitbuffer = 0; |
| 784 |
1921 |
| buffer->bits_to_go = 8; |
| 785 |
1921 |
| } |
| 786 |
|
| |
| 787 |
|
| /*---------------------------------------------------------------------------*/ |
| 788 |
|
| /* Output N bits (N must be <= 32) */ |
| 789 |
|
| |
| 790 |
24963 |
| static int output_nbits(Buffer *buffer, int bits, int n) |
| 791 |
|
| { |
| 792 |
|
| /* local copies */ |
| 793 |
|
| int lbitbuffer; |
| 794 |
|
| int lbits_to_go; |
| 795 |
|
| /* AND mask for the right-most n bits */ |
| 796 |
|
| static unsigned int mask[33] = |
| 797 |
|
| {0, |
| 798 |
|
| 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, |
| 799 |
|
| 0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff, |
| 800 |
|
| 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, |
| 801 |
|
| 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff}; |
| 802 |
|
| |
| 803 |
|
| /* |
| 804 |
|
| * insert bits at end of bitbuffer |
| 805 |
|
| */ |
| 806 |
24963 |
| lbitbuffer = buffer->bitbuffer; |
| 807 |
24963 |
| lbits_to_go = buffer->bits_to_go; |
| 808 |
24963 |
▶ | if (lbits_to_go+n > 32) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lbits_to_go+n > 32) { |
1920 |
23043 |
24963 |
|
| 809 |
|
| /* |
| 810 |
|
| * special case for large n: put out the top lbits_to_go bits first |
| 811 |
|
| * note that 0 < lbits_to_go <= 8 |
| 812 |
|
| */ |
| 813 |
1920 |
| lbitbuffer <<= lbits_to_go; |
| 814 |
|
| /* lbitbuffer |= (bits>>(n-lbits_to_go)) & ((1<<lbits_to_go)-1); */ |
| 815 |
1920 |
| lbitbuffer |= (bits>>(n-lbits_to_go)) & *(mask+lbits_to_go); |
| 816 |
1920 |
| putcbuf(lbitbuffer & 0xff,buffer); |
| 817 |
1920 |
| n -= lbits_to_go; |
| 818 |
1920 |
| lbits_to_go = 8; |
| 819 |
|
| } |
| 820 |
24963 |
| lbitbuffer <<= n; |
| 821 |
|
| /* lbitbuffer |= ( bits & ((1<<n)-1) ); */ |
| 822 |
24963 |
| lbitbuffer |= ( bits & *(mask+n) ); |
| 823 |
24963 |
| lbits_to_go -= n; |
| 824 |
43999 |
▶ | while (lbits_to_go <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lbits_to_go <= 0) { |
19036 |
24963 |
43999 |
|
| 825 |
|
| /* |
| 826 |
|
| * bitbuffer full, put out top 8 bits |
| 827 |
|
| */ |
| 828 |
19036 |
| putcbuf((lbitbuffer>>(-lbits_to_go)) & 0xff,buffer); |
| 829 |
19036 |
| lbits_to_go += 8; |
| 830 |
|
| } |
| 831 |
24963 |
| buffer->bitbuffer = lbitbuffer; |
| 832 |
24963 |
| buffer->bits_to_go = lbits_to_go; |
| 833 |
24963 |
| return(0); |
| 834 |
|
| } |
| 835 |
|
| /*---------------------------------------------------------------------------*/ |
| 836 |
|
| /* Flush out the last bits */ |
| 837 |
|
| |
| 838 |
1921 |
| static int done_outputing_bits(Buffer *buffer) |
| 839 |
|
| { |
| 840 |
1921 |
▶ | if(buffer->bits_to_go < 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(buffer->bits_to_go < 8) { |
1711 |
210 |
1921 |
|
| 841 |
1711 |
| putcbuf(buffer->bitbuffer<<buffer->bits_to_go,buffer); |
| 842 |
|
| |
| 843 |
|
| /* if (putcbuf(buffer->bitbuffer<<buffer->bits_to_go,buffer) == EOF) |
| 844 |
|
| return(EOF); |
| 845 |
|
| */ |
| 846 |
|
| } |
| 847 |
1921 |
| return(0); |
| 848 |
|
| } |
| 849 |
|
| /*---------------------------------------------------------------------------*/ |
| 850 |
|
| /*----------------------------------------------------------*/ |
| 851 |
|
| /* */ |
| 852 |
|
| /* START OF SOURCE FILE ORIGINALLY CALLED rdecomp.c */ |
| 853 |
|
| /* */ |
| 854 |
|
| /*----------------------------------------------------------*/ |
| 855 |
|
| |
| 856 |
|
| /* @(#) rdecomp.c 1.4 99/03/01 12:38:41 */ |
| 857 |
|
| /* rdecomp.c Decompress image line using |
| 858 |
|
| * (1) Difference of adjacent pixels |
| 859 |
|
| * (2) Rice algorithm coding |
| 860 |
|
| * |
| 861 |
|
| * Returns 0 on success or 1 on failure |
| 862 |
|
| */ |
| 863 |
|
| |
| 864 |
|
| /* moved these 'includes' to the beginning of the file (WDP) |
| 865 |
|
| #include <stdio.h> |
| 866 |
|
| #include <stdlib.h> |
| 867 |
|
| */ |
| 868 |
|
| |
| 869 |
|
| /*---------------------------------------------------------------------------*/ |
| 870 |
|
| /* this routine used to be called 'rdecomp' (WDP) */ |
| 871 |
|
| |
| 872 |
3840 |
| char *fits_rdecomp (unsigned char *c, /* input buffer */ |
| 873 |
|
| int clen, /* length of input */ |
| 874 |
|
| unsigned int array[], /* output array */ |
| 875 |
|
| int nx, /* number of output pixels */ |
| 876 |
|
| int nblock) /* coding block size */ |
| 877 |
|
| { |
| 878 |
|
| /* int bsize; */ |
| 879 |
|
| int i, k, imax; |
| 880 |
|
| int nbits, nzero, fs; |
| 881 |
|
| unsigned char *cend, bytevalue; |
| 882 |
|
| unsigned int b, diff, lastpix; |
| 883 |
|
| int fsmax, fsbits, bbits; |
| 884 |
|
| extern const int nonzero_count[]; |
| 885 |
|
| |
| 886 |
|
| /* |
| 887 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 888 |
|
| * size (nblock, pixels) |
| 889 |
|
| * Could make bsize a parameter to allow more efficient |
| 890 |
|
| * compression of short & byte images. |
| 891 |
|
| */ |
| 892 |
|
| /* bsize = 4; */ |
| 893 |
|
| |
| 894 |
|
| /* nblock = 32; now an input parameter */ |
| 895 |
|
| /* |
| 896 |
|
| * From bsize derive: |
| 897 |
|
| * FSBITS = # bits required to store FS |
| 898 |
|
| * FSMAX = maximum value for FS |
| 899 |
|
| * BBITS = bits/pixel for direct coding |
| 900 |
|
| */ |
| 901 |
|
| |
| 902 |
|
| /* |
| 903 |
|
| switch (bsize) { |
| 904 |
|
| case 1: |
| 905 |
|
| fsbits = 3; |
| 906 |
|
| fsmax = 6; |
| 907 |
|
| break; |
| 908 |
|
| case 2: |
| 909 |
|
| fsbits = 4; |
| 910 |
|
| fsmax = 14; |
| 911 |
|
| break; |
| 912 |
|
| case 4: |
| 913 |
|
| fsbits = 5; |
| 914 |
|
| fsmax = 25; |
| 915 |
|
| break; |
| 916 |
|
| default: |
| 917 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 918 |
|
| return 1; |
| 919 |
|
| } |
| 920 |
|
| */ |
| 921 |
|
| |
| 922 |
|
| /* move out of switch block, to tweak performance */ |
| 923 |
3840 |
| fsbits = 5; |
| 924 |
3840 |
| fsmax = 25; |
| 925 |
|
| |
| 926 |
3840 |
| bbits = 1<<fsbits; |
| 927 |
|
| |
| 928 |
|
| /* |
| 929 |
|
| * Decode in blocks of nblock pixels |
| 930 |
|
| */ |
| 931 |
|
| |
| 932 |
|
| /* first 4 bytes of input buffer contain the value of the first */ |
| 933 |
|
| /* 4 byte integer value, without any encoding */ |
| 934 |
|
| |
| 935 |
3840 |
| lastpix = 0; |
| 936 |
3840 |
| bytevalue = c[0]; |
| 937 |
3840 |
| lastpix = lastpix | (bytevalue<<24); |
| 938 |
3840 |
| bytevalue = c[1]; |
| 939 |
3840 |
| lastpix = lastpix | (bytevalue<<16); |
| 940 |
3840 |
| bytevalue = c[2]; |
| 941 |
3840 |
| lastpix = lastpix | (bytevalue<<8); |
| 942 |
3840 |
| bytevalue = c[3]; |
| 943 |
3840 |
| lastpix = lastpix | bytevalue; |
| 944 |
|
| |
| 945 |
3840 |
| c += 4; |
| 946 |
3840 |
| cend = c + clen - 4; |
| 947 |
|
| |
| 948 |
3840 |
| b = *c++; /* bit buffer */ |
| 949 |
3840 |
| nbits = 8; /* number of bits remaining in b */ |
| 950 |
49920 |
▶ | for (i = 0; i<nx; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i<nx; ) { |
46080 |
3840 |
49920 |
|
| 951 |
|
| /* get the FS value from first fsbits */ |
| 952 |
46080 |
| nbits -= fsbits; |
| 953 |
72260 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
26180 |
46080 |
72260 |
|
| 954 |
26180 |
| b = (b<<8) | (*c++); |
| 955 |
26180 |
| nbits += 8; |
| 956 |
|
| } |
| 957 |
46080 |
| fs = (b >> nbits) - 1; |
| 958 |
|
| |
| 959 |
46080 |
| b &= (1<<nbits)-1; |
| 960 |
|
| /* loop over the next block */ |
| 961 |
46080 |
| imax = i + nblock; |
| 962 |
46080 |
▶ | if (imax > nx) imax = nx; |
Branch: 1 branch
| Branch | True | False | Total |
| if (imax > nx) imax = nx; |
0 |
46080 |
46080 |
|
| 963 |
46080 |
▶ | if (fs<0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs<0) { |
0 |
46080 |
46080 |
|
| 964 |
|
| /* low-entropy case, all zero differences */ |
| 965 |
0 |
▶ | for ( ; i<imax; i++) array[i] = lastpix; |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) array[i] = lastpix; |
0 |
0 |
0 |
|
| 966 |
46080 |
▶ | } else if (fs==fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (fs==fsmax) { |
0 |
46080 |
46080 |
|
| 967 |
|
| /* high-entropy case, directly coded pixel values */ |
| 968 |
0 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
0 |
0 |
0 |
|
| 969 |
0 |
| k = bbits - nbits; |
| 970 |
0 |
| diff = b<<k; |
| 971 |
0 |
▶ | for (k -= 8; k >= 0; k -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k -= 8; k >= 0; k -= 8) { |
0 |
0 |
0 |
|
| 972 |
0 |
| b = *c++; |
| 973 |
0 |
| diff |= b<<k; |
| 974 |
|
| } |
| 975 |
0 |
▶ | if (nbits>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbits>0) { |
0 |
0 |
0 |
|
| 976 |
0 |
| b = *c++; |
| 977 |
0 |
| diff |= b>>(-k); |
| 978 |
0 |
| b &= (1<<nbits)-1; |
| 979 |
|
| } else { |
| 980 |
0 |
| b = 0; |
| 981 |
|
| } |
| 982 |
|
| /* |
| 983 |
|
| * undo mapping and differencing |
| 984 |
|
| * Note that some of these operations will overflow the |
| 985 |
|
| * unsigned int arithmetic -- that's OK, it all works |
| 986 |
|
| * out to give the right answers in the output file. |
| 987 |
|
| */ |
| 988 |
0 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
0 |
0 |
0 |
|
| 989 |
0 |
| diff = diff>>1; |
| 990 |
|
| } else { |
| 991 |
0 |
| diff = ~(diff>>1); |
| 992 |
|
| } |
| 993 |
0 |
| array[i] = diff+lastpix; |
| 994 |
0 |
| lastpix = array[i]; |
| 995 |
|
| } |
| 996 |
|
| } else { |
| 997 |
|
| /* normal case, Rice coding */ |
| 998 |
1520640 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
1474560 |
46080 |
1520640 |
|
| 999 |
|
| /* count number of leading zeros */ |
| 1000 |
1849850 |
▶ | while (b == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (b == 0) { |
375290 |
1474560 |
1849850 |
|
| 1001 |
375290 |
| nbits += 8; |
| 1002 |
375290 |
| b = *c++; |
| 1003 |
|
| } |
| 1004 |
1474560 |
| nzero = nbits - nonzero_count[b]; |
| 1005 |
1474560 |
| nbits -= nzero+1; |
| 1006 |
|
| /* flip the leading one-bit */ |
| 1007 |
1474560 |
| b ^= 1<<nbits; |
| 1008 |
|
| /* get the FS trailing bits */ |
| 1009 |
1474560 |
| nbits -= fs; |
| 1010 |
2370190 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
895630 |
1474560 |
2370190 |
|
| 1011 |
895630 |
| b = (b<<8) | (*c++); |
| 1012 |
895630 |
| nbits += 8; |
| 1013 |
|
| } |
| 1014 |
1474560 |
| diff = (nzero<<fs) | (b>>nbits); |
| 1015 |
1474560 |
| b &= (1<<nbits)-1; |
| 1016 |
|
| |
| 1017 |
|
| /* undo mapping and differencing */ |
| 1018 |
1474560 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
774130 |
700430 |
1474560 |
|
| 1019 |
774130 |
| diff = diff>>1; |
| 1020 |
|
| } else { |
| 1021 |
700430 |
| diff = ~(diff>>1); |
| 1022 |
|
| } |
| 1023 |
1474560 |
| array[i] = diff+lastpix; |
| 1024 |
1474560 |
| lastpix = array[i]; |
| 1025 |
|
| } |
| 1026 |
|
| } |
| 1027 |
46080 |
▶ | if (c > cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c > cend) { |
0 |
46080 |
46080 |
|
| 1028 |
0 |
| return "decompression error: hit end of compressed byte stream"; |
| 1029 |
|
| } |
| 1030 |
|
| } |
| 1031 |
3840 |
▶ | if (c < cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < cend) { |
0 |
3840 |
3840 |
|
| 1032 |
0 |
| return "decompression warning: unused bytes at end of compressed buffer"; |
| 1033 |
|
| } |
| 1034 |
3840 |
| return 0; |
| 1035 |
|
| } |
| 1036 |
|
| /*---------------------------------------------------------------------------*/ |
| 1037 |
|
| /* this routine used to be called 'rdecomp' (WDP) */ |
| 1038 |
|
| |
| 1039 |
0 |
| char *fits_rdecomp_short (unsigned char *c, /* input buffer */ |
| 1040 |
|
| int clen, /* length of input */ |
| 1041 |
|
| unsigned short array[], /* output array */ |
| 1042 |
|
| int nx, /* number of output pixels */ |
| 1043 |
|
| int nblock) /* coding block size */ |
| 1044 |
|
| { |
| 1045 |
|
| int i, imax; |
| 1046 |
|
| /* int bsize; */ |
| 1047 |
|
| int k; |
| 1048 |
|
| int nbits, nzero, fs; |
| 1049 |
|
| unsigned char *cend, bytevalue; |
| 1050 |
|
| unsigned int b, diff, lastpix; |
| 1051 |
|
| int fsmax, fsbits, bbits; |
| 1052 |
|
| extern const int nonzero_count[]; |
| 1053 |
|
| |
| 1054 |
|
| /* |
| 1055 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 1056 |
|
| * size (nblock, pixels) |
| 1057 |
|
| * Could make bsize a parameter to allow more efficient |
| 1058 |
|
| * compression of short & byte images. |
| 1059 |
|
| */ |
| 1060 |
|
| |
| 1061 |
|
| /* bsize = 2; */ |
| 1062 |
|
| |
| 1063 |
|
| /* nblock = 32; now an input parameter */ |
| 1064 |
|
| /* |
| 1065 |
|
| * From bsize derive: |
| 1066 |
|
| * FSBITS = # bits required to store FS |
| 1067 |
|
| * FSMAX = maximum value for FS |
| 1068 |
|
| * BBITS = bits/pixel for direct coding |
| 1069 |
|
| */ |
| 1070 |
|
| |
| 1071 |
|
| /* |
| 1072 |
|
| switch (bsize) { |
| 1073 |
|
| case 1: |
| 1074 |
|
| fsbits = 3; |
| 1075 |
|
| fsmax = 6; |
| 1076 |
|
| break; |
| 1077 |
|
| case 2: |
| 1078 |
|
| fsbits = 4; |
| 1079 |
|
| fsmax = 14; |
| 1080 |
|
| break; |
| 1081 |
|
| case 4: |
| 1082 |
|
| fsbits = 5; |
| 1083 |
|
| fsmax = 25; |
| 1084 |
|
| break; |
| 1085 |
|
| default: |
| 1086 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 1087 |
|
| return 1; |
| 1088 |
|
| } |
| 1089 |
|
| */ |
| 1090 |
|
| |
| 1091 |
|
| /* move out of switch block, to tweak performance */ |
| 1092 |
0 |
| fsbits = 4; |
| 1093 |
0 |
| fsmax = 14; |
| 1094 |
|
| |
| 1095 |
0 |
| bbits = 1<<fsbits; |
| 1096 |
|
| |
| 1097 |
|
| /* |
| 1098 |
|
| * Decode in blocks of nblock pixels |
| 1099 |
|
| */ |
| 1100 |
|
| |
| 1101 |
|
| /* first 2 bytes of input buffer contain the value of the first */ |
| 1102 |
|
| /* 2 byte integer value, without any encoding */ |
| 1103 |
|
| |
| 1104 |
0 |
| lastpix = 0; |
| 1105 |
0 |
| bytevalue = c[0]; |
| 1106 |
0 |
| lastpix = lastpix | (bytevalue<<8); |
| 1107 |
0 |
| bytevalue = c[1]; |
| 1108 |
0 |
| lastpix = lastpix | bytevalue; |
| 1109 |
|
| |
| 1110 |
0 |
| c += 2; |
| 1111 |
0 |
| cend = c + clen - 2; |
| 1112 |
|
| |
| 1113 |
0 |
| b = *c++; /* bit buffer */ |
| 1114 |
0 |
| nbits = 8; /* number of bits remaining in b */ |
| 1115 |
0 |
▶ | for (i = 0; i<nx; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i<nx; ) { |
0 |
0 |
0 |
|
| 1116 |
|
| /* get the FS value from first fsbits */ |
| 1117 |
0 |
| nbits -= fsbits; |
| 1118 |
0 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
0 |
0 |
0 |
|
| 1119 |
0 |
| b = (b<<8) | (*c++); |
| 1120 |
0 |
| nbits += 8; |
| 1121 |
|
| } |
| 1122 |
0 |
| fs = (b >> nbits) - 1; |
| 1123 |
|
| |
| 1124 |
0 |
| b &= (1<<nbits)-1; |
| 1125 |
|
| /* loop over the next block */ |
| 1126 |
0 |
| imax = i + nblock; |
| 1127 |
0 |
▶ | if (imax > nx) imax = nx; |
Branch: 1 branch
| Branch | True | False | Total |
| if (imax > nx) imax = nx; |
0 |
0 |
0 |
|
| 1128 |
0 |
▶ | if (fs<0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs<0) { |
0 |
0 |
0 |
|
| 1129 |
|
| /* low-entropy case, all zero differences */ |
| 1130 |
0 |
▶ | for ( ; i<imax; i++) array[i] = lastpix; |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) array[i] = lastpix; |
0 |
0 |
0 |
|
| 1131 |
0 |
▶ | } else if (fs==fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (fs==fsmax) { |
0 |
0 |
0 |
|
| 1132 |
|
| /* high-entropy case, directly coded pixel values */ |
| 1133 |
0 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
0 |
0 |
0 |
|
| 1134 |
0 |
| k = bbits - nbits; |
| 1135 |
0 |
| diff = b<<k; |
| 1136 |
0 |
▶ | for (k -= 8; k >= 0; k -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k -= 8; k >= 0; k -= 8) { |
0 |
0 |
0 |
|
| 1137 |
0 |
| b = *c++; |
| 1138 |
0 |
| diff |= b<<k; |
| 1139 |
|
| } |
| 1140 |
0 |
▶ | if (nbits>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbits>0) { |
0 |
0 |
0 |
|
| 1141 |
0 |
| b = *c++; |
| 1142 |
0 |
| diff |= b>>(-k); |
| 1143 |
0 |
| b &= (1<<nbits)-1; |
| 1144 |
|
| } else { |
| 1145 |
0 |
| b = 0; |
| 1146 |
|
| } |
| 1147 |
|
| |
| 1148 |
|
| /* |
| 1149 |
|
| * undo mapping and differencing |
| 1150 |
|
| * Note that some of these operations will overflow the |
| 1151 |
|
| * unsigned int arithmetic -- that's OK, it all works |
| 1152 |
|
| * out to give the right answers in the output file. |
| 1153 |
|
| */ |
| 1154 |
0 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
0 |
0 |
0 |
|
| 1155 |
0 |
| diff = diff>>1; |
| 1156 |
|
| } else { |
| 1157 |
0 |
| diff = ~(diff>>1); |
| 1158 |
|
| } |
| 1159 |
0 |
| array[i] = diff+lastpix; |
| 1160 |
0 |
| lastpix = array[i]; |
| 1161 |
|
| } |
| 1162 |
|
| } else { |
| 1163 |
|
| /* normal case, Rice coding */ |
| 1164 |
0 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
0 |
0 |
0 |
|
| 1165 |
|
| /* count number of leading zeros */ |
| 1166 |
0 |
▶ | while (b == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (b == 0) { |
0 |
0 |
0 |
|
| 1167 |
0 |
| nbits += 8; |
| 1168 |
0 |
| b = *c++; |
| 1169 |
|
| } |
| 1170 |
0 |
| nzero = nbits - nonzero_count[b]; |
| 1171 |
0 |
| nbits -= nzero+1; |
| 1172 |
|
| /* flip the leading one-bit */ |
| 1173 |
0 |
| b ^= 1<<nbits; |
| 1174 |
|
| /* get the FS trailing bits */ |
| 1175 |
0 |
| nbits -= fs; |
| 1176 |
0 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
0 |
0 |
0 |
|
| 1177 |
0 |
| b = (b<<8) | (*c++); |
| 1178 |
0 |
| nbits += 8; |
| 1179 |
|
| } |
| 1180 |
0 |
| diff = (nzero<<fs) | (b>>nbits); |
| 1181 |
0 |
| b &= (1<<nbits)-1; |
| 1182 |
|
| |
| 1183 |
|
| /* undo mapping and differencing */ |
| 1184 |
0 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
0 |
0 |
0 |
|
| 1185 |
0 |
| diff = diff>>1; |
| 1186 |
|
| } else { |
| 1187 |
0 |
| diff = ~(diff>>1); |
| 1188 |
|
| } |
| 1189 |
0 |
| array[i] = diff+lastpix; |
| 1190 |
0 |
| lastpix = array[i]; |
| 1191 |
|
| } |
| 1192 |
|
| } |
| 1193 |
0 |
▶ | if (c > cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c > cend) { |
0 |
0 |
0 |
|
| 1194 |
0 |
| return "decompression error: hit end of compressed byte stream"; |
| 1195 |
|
| } |
| 1196 |
|
| } |
| 1197 |
0 |
▶ | if (c < cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < cend) { |
0 |
0 |
0 |
|
| 1198 |
0 |
| return "decompression warning: unused bytes at end of compressed buffer"; |
| 1199 |
|
| } |
| 1200 |
0 |
| return 0; |
| 1201 |
|
| } |
| 1202 |
|
| /*---------------------------------------------------------------------------*/ |
| 1203 |
|
| /* this routine used to be called 'rdecomp' (WDP) */ |
| 1204 |
|
| |
| 1205 |
2 |
| char *fits_rdecomp_byte (unsigned char *c, /* input buffer */ |
| 1206 |
|
| int clen, /* length of input */ |
| 1207 |
|
| unsigned char array[], /* output array */ |
| 1208 |
|
| int nx, /* number of output pixels */ |
| 1209 |
|
| int nblock) /* coding block size */ |
| 1210 |
|
| { |
| 1211 |
|
| int i, imax; |
| 1212 |
|
| /* int bsize; */ |
| 1213 |
|
| int k; |
| 1214 |
|
| int nbits, nzero, fs; |
| 1215 |
|
| unsigned char *cend; |
| 1216 |
|
| unsigned int b, diff, lastpix; |
| 1217 |
|
| int fsmax, fsbits, bbits; |
| 1218 |
|
| extern const int nonzero_count[]; |
| 1219 |
|
| |
| 1220 |
|
| /* |
| 1221 |
|
| * Original size of each pixel (bsize, bytes) and coding block |
| 1222 |
|
| * size (nblock, pixels) |
| 1223 |
|
| * Could make bsize a parameter to allow more efficient |
| 1224 |
|
| * compression of short & byte images. |
| 1225 |
|
| */ |
| 1226 |
|
| |
| 1227 |
|
| /* bsize = 1; */ |
| 1228 |
|
| |
| 1229 |
|
| /* nblock = 32; now an input parameter */ |
| 1230 |
|
| /* |
| 1231 |
|
| * From bsize derive: |
| 1232 |
|
| * FSBITS = # bits required to store FS |
| 1233 |
|
| * FSMAX = maximum value for FS |
| 1234 |
|
| * BBITS = bits/pixel for direct coding |
| 1235 |
|
| */ |
| 1236 |
|
| |
| 1237 |
|
| /* |
| 1238 |
|
| switch (bsize) { |
| 1239 |
|
| case 1: |
| 1240 |
|
| fsbits = 3; |
| 1241 |
|
| fsmax = 6; |
| 1242 |
|
| break; |
| 1243 |
|
| case 2: |
| 1244 |
|
| fsbits = 4; |
| 1245 |
|
| fsmax = 14; |
| 1246 |
|
| break; |
| 1247 |
|
| case 4: |
| 1248 |
|
| fsbits = 5; |
| 1249 |
|
| fsmax = 25; |
| 1250 |
|
| break; |
| 1251 |
|
| default: |
| 1252 |
|
| ffpmsg("rdecomp: bsize must be 1, 2, or 4 bytes"); |
| 1253 |
|
| return 1; |
| 1254 |
|
| } |
| 1255 |
|
| */ |
| 1256 |
|
| |
| 1257 |
|
| /* move out of switch block, to tweak performance */ |
| 1258 |
2 |
| fsbits = 3; |
| 1259 |
2 |
| fsmax = 6; |
| 1260 |
|
| |
| 1261 |
2 |
| bbits = 1<<fsbits; |
| 1262 |
|
| |
| 1263 |
|
| /* |
| 1264 |
|
| * Decode in blocks of nblock pixels |
| 1265 |
|
| */ |
| 1266 |
|
| |
| 1267 |
|
| /* first byte of input buffer contain the value of the first */ |
| 1268 |
|
| /* byte integer value, without any encoding */ |
| 1269 |
|
| |
| 1270 |
2 |
| lastpix = c[0]; |
| 1271 |
2 |
| c += 1; |
| 1272 |
2 |
| cend = c + clen - 1; |
| 1273 |
|
| |
| 1274 |
2 |
| b = *c++; /* bit buffer */ |
| 1275 |
2 |
| nbits = 8; /* number of bits remaining in b */ |
| 1276 |
6 |
▶ | for (i = 0; i<nx; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i<nx; ) { |
4 |
2 |
6 |
|
| 1277 |
|
| /* get the FS value from first fsbits */ |
| 1278 |
4 |
| nbits -= fsbits; |
| 1279 |
4 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
0 |
4 |
4 |
|
| 1280 |
0 |
| b = (b<<8) | (*c++); |
| 1281 |
0 |
| nbits += 8; |
| 1282 |
|
| } |
| 1283 |
4 |
| fs = (b >> nbits) - 1; |
| 1284 |
|
| |
| 1285 |
4 |
| b &= (1<<nbits)-1; |
| 1286 |
|
| /* loop over the next block */ |
| 1287 |
4 |
| imax = i + nblock; |
| 1288 |
4 |
▶ | if (imax > nx) imax = nx; |
Branch: 1 branch
| Branch | True | False | Total |
| if (imax > nx) imax = nx; |
0 |
4 |
4 |
|
| 1289 |
4 |
▶ | if (fs<0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fs<0) { |
0 |
4 |
4 |
|
| 1290 |
|
| /* low-entropy case, all zero differences */ |
| 1291 |
0 |
▶ | for ( ; i<imax; i++) array[i] = lastpix; |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) array[i] = lastpix; |
0 |
0 |
0 |
|
| 1292 |
4 |
▶ | } else if (fs==fsmax) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (fs==fsmax) { |
0 |
4 |
4 |
|
| 1293 |
|
| /* high-entropy case, directly coded pixel values */ |
| 1294 |
0 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
0 |
0 |
0 |
|
| 1295 |
0 |
| k = bbits - nbits; |
| 1296 |
0 |
| diff = b<<k; |
| 1297 |
0 |
▶ | for (k -= 8; k >= 0; k -= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k -= 8; k >= 0; k -= 8) { |
0 |
0 |
0 |
|
| 1298 |
0 |
| b = *c++; |
| 1299 |
0 |
| diff |= b<<k; |
| 1300 |
|
| } |
| 1301 |
0 |
▶ | if (nbits>0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbits>0) { |
0 |
0 |
0 |
|
| 1302 |
0 |
| b = *c++; |
| 1303 |
0 |
| diff |= b>>(-k); |
| 1304 |
0 |
| b &= (1<<nbits)-1; |
| 1305 |
|
| } else { |
| 1306 |
0 |
| b = 0; |
| 1307 |
|
| } |
| 1308 |
|
| |
| 1309 |
|
| /* |
| 1310 |
|
| * undo mapping and differencing |
| 1311 |
|
| * Note that some of these operations will overflow the |
| 1312 |
|
| * unsigned int arithmetic -- that's OK, it all works |
| 1313 |
|
| * out to give the right answers in the output file. |
| 1314 |
|
| */ |
| 1315 |
0 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
0 |
0 |
0 |
|
| 1316 |
0 |
| diff = diff>>1; |
| 1317 |
|
| } else { |
| 1318 |
0 |
| diff = ~(diff>>1); |
| 1319 |
|
| } |
| 1320 |
0 |
| array[i] = diff+lastpix; |
| 1321 |
0 |
| lastpix = array[i]; |
| 1322 |
|
| } |
| 1323 |
|
| } else { |
| 1324 |
|
| /* normal case, Rice coding */ |
| 1325 |
132 |
▶ | for ( ; i<imax; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; i<imax; i++) { |
128 |
4 |
132 |
|
| 1326 |
|
| /* count number of leading zeros */ |
| 1327 |
164 |
▶ | while (b == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (b == 0) { |
36 |
128 |
164 |
|
| 1328 |
36 |
| nbits += 8; |
| 1329 |
36 |
| b = *c++; |
| 1330 |
|
| } |
| 1331 |
128 |
| nzero = nbits - nonzero_count[b]; |
| 1332 |
128 |
| nbits -= nzero+1; |
| 1333 |
|
| /* flip the leading one-bit */ |
| 1334 |
128 |
| b ^= 1<<nbits; |
| 1335 |
|
| /* get the FS trailing bits */ |
| 1336 |
128 |
| nbits -= fs; |
| 1337 |
128 |
▶ | while (nbits < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nbits < 0) { |
0 |
128 |
128 |
|
| 1338 |
0 |
| b = (b<<8) | (*c++); |
| 1339 |
0 |
| nbits += 8; |
| 1340 |
|
| } |
| 1341 |
128 |
| diff = (nzero<<fs) | (b>>nbits); |
| 1342 |
128 |
| b &= (1<<nbits)-1; |
| 1343 |
|
| |
| 1344 |
|
| /* undo mapping and differencing */ |
| 1345 |
128 |
▶ | if ((diff & 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((diff & 1) == 0) { |
80 |
48 |
128 |
|
| 1346 |
80 |
| diff = diff>>1; |
| 1347 |
|
| } else { |
| 1348 |
48 |
| diff = ~(diff>>1); |
| 1349 |
|
| } |
| 1350 |
128 |
| array[i] = diff+lastpix; |
| 1351 |
128 |
| lastpix = array[i]; |
| 1352 |
|
| } |
| 1353 |
|
| } |
| 1354 |
4 |
▶ | if (c > cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c > cend) { |
0 |
4 |
4 |
|
| 1355 |
0 |
| return "decompression error: hit end of compressed byte stream"; |
| 1356 |
|
| } |
| 1357 |
|
| } |
| 1358 |
2 |
▶ | if (c < cend) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < cend) { |
0 |
2 |
2 |
|
| 1359 |
0 |
| return "decompression warning: unused bytes at end of compressed buffer"; |
| 1360 |
|
| } |
| 1361 |
2 |
| return 0; |
| 1362 |
|
| } |