10 /* 1996 wesley ebisuzaki
14 * input: *bits, pointer to packed integer data
15 * *bitmap, pointer to bitmap (undefined data), NULL if none
16 * n_bits, number of bits per packed integer
17 * n, number of data points (includes undefined data)
18 * ref, scale: flt[] = ref + scale*packed_int
19 * output: *flt, pointer to output array
20 * undefined values filled with UNDEFINED
22 * note: code assumes an integer > 32 bits
24 * 7/98 v1.2.1 fix bug for bitmaps and nbit >= 25 found by Larry Brasfield
25 * 2/01 v1.2.2 changed jj from long int to double
26 * 3/02 v1.2.3 added unpacking extensions for spectral data
27 * Luis Kornblueh, MPIfM
30 static unsigned int mask
[] = {0,1,3,7,15,31,63,127,255};
31 static unsigned int map_masks
[8] = {128, 64, 32, 16, 8, 4, 2, 1};
32 static double shift
[9] = {1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0};
34 void BDS_unpack(float *flt
, unsigned char *bds
, unsigned char *bitmap
,
35 int n_bits
, int n
, double ref
, double scale
) {
39 int i
, mask_idx
, t_bits
, c_bits
, j_bits
;
40 unsigned int j
, map_mask
, tbits
, jmask
, bbits
;
43 if (BDS_Harmonic(bds
)) {
45 /* fill in global mean */
46 *flt
++ = BDS_Harmonic_RefValue(bds
);
55 /* assume integer has 32+ bits */
57 jmask
= (1 << n_bits
) - 1;
61 for (i
= 0; i
< n
; i
++) {
64 if (mask_idx
== 0) bbits
= *bitmap
++;
65 if ((bbits
& map_masks
[mask_idx
]) == 0) {
70 while (t_bits
< n_bits
) {
71 tbits
= (tbits
* 256) + *bits
++;
75 j
= (tbits
>> t_bits
) & jmask
;
76 *flt
++ = ref
+ scale
*j
;
80 for (i
= 0; i
< n
; i
++) {
81 while (t_bits
< n_bits
) {
82 tbits
= (tbits
* 256) + *bits
++;
86 flt
[i
] = (tbits
>> t_bits
) & jmask
;
88 /* at least this vectorizes :) */
89 for (i
= 0; i
< n
; i
++) {
90 flt
[i
] = ref
+ scale
*flt
[i
];
95 /* older unoptimized code, not often used */
100 j
= (*bitmap
& map_mask
);
101 if ((map_mask
>>= 1) == 0) {
113 while (c_bits
<= j_bits
) {
115 jj
= jj
* 256.0 + (double) (*bits
++);
119 jj
= (jj
* shift
[c_bits
]) + (double) (*bits
& mask
[c_bits
]);
127 jj
= (jj
* shift
[j_bits
]) + (double) ((*bits
>> c_bits
) & mask
[j_bits
]);
129 *flt
++ = ref
+ scale
*jj
;