1 /* gribgetbms.c June 17, 1996 by Alice Nakajima, SAIC */
5 #include "dprints.h" /* for dprints */
6 #include "gribfuncs.h" /* prototypes */
10 ************************************************************************
11 * A. FUNCTION: gribgetbms
12 * decode the Bitmap Section from the provided pointer location
13 * and store its info in the internal BMS structure.
14 * Pre-defined Bitmap case is not supported.
17 * int gribgetbms ( curr_ptr, bms, gds_flag, ulGrid_size, errmsg)
19 * ARGUMENTS (I=input, O=output, I&O=input and output):
21 * pointer to location where Bitmap Section to decode is expected;
23 * pointer to empty BMS structure; will hold decoded BMS info;
25 * flag set if GDS is present
26 * (I) unsigned long ulGrid_size;
27 * size of grid as in Binary Data Section struct
29 * returned filled if error occurred;
32 * 0> BMS info stored in BMS structure if not using pre-defined bitmap;
33 * 1> error, corrupted bms; msg in errmsg;
34 ************************************************************************
38 int gribgetbms ( char *curr_ptr
, BMS_INPUT
*bms
, int gds_flag
,
39 unsigned long ulGrid_size
, char *errmsg
)
41 int gribgetbms (curr_ptr
, bms
, gds_flag
, ulGrid_size
, errmsg
)
45 unsigned long ulGrid_size
;
49 char *func
= "gribgetbms";
51 int totbits
,val
, bitpos
,stopbit
; /* tmp working vars */
52 unsigned long SectLength
; /* message and section size */
53 unsigned long ulvar
; /* tmp var */
58 * A.0 INIT Status to no error
62 DPRINT0 ("Entering gribgetbms():\n");
65 * A.1 FUNCTION gbyte !get bitmap length
68 gbyte(curr_ptr
,(unsigned long *)&SectLength
,&skip
,24);
69 DPRINT0 ("SectLength\n");
70 bms
->uslength
= (unsigned long) SectLength
;
74 * A.2 FUNCTION gbyte !get number of unused bits
76 gbyte(curr_ptr
,&ulvar
,&skip
,8);
77 DPRINT0 ("bms->usUnused_bits\n");
78 bms
->usUnused_bits
= (unsigned short) ulvar
;
82 * A.3 FUNCTION gbyte !get bitmap id (non-zero for a pre-defined bitmap)
84 gbyte(curr_ptr
,&ulvar
,&skip
,16);
85 DPRINT0 ("bms->usBMS_id\n");
86 bms
->usBMS_id
= (unsigned short) ulvar
;
90 * A.4 IF (Bitmap follows) !not a predefined bitmap
92 if ( bms
->uslength
> 6) /* Bitmap follows */
96 * A.4.1 CALCULATE Num of bits in bitmap
98 /* = (BMS length)*8 bits - 48 header bits - # of unsused bits */
99 totbits
=SectLength
*8 - 48 - bms
->usUnused_bits
;
102 * A.4.2 IF (GDS is present AND
103 * #bits differs from Grid Size) !Corrupted BMS
107 if (gds_flag
&& totbits
!= ulGrid_size
) {
108 DPRINT3( "%s: corrupted BMS, gds_flag set but "\
109 "totbits %d != ulgrid_sz %d\n"
110 , func
, totbits
, ulGrid_size
);
112 sprintf(errmsg
, "%s: corrupted BMS, gds_flag set but "\
113 "totbits %d != ulgrid_sz %d\n" , func
, totbits
, ulGrid_size
);
114 status
= (1); /* Corrupted BMS */
119 * A.4.3 ASSIGN bitmap pointer to 6th byte of BMS
121 bms
->bit_map
= curr_ptr
+ 6;
126 * A.4.4 !SUM up total number of bits set
127 * FOR (Each 8-bit block of Total Bits Present in BMS)
129 for ( ; totbits
> 0 ; totbits
-=8)
132 * A.4.4.1 IF (any of the 8 bits are set)
134 if ((val
=(int)*pp
++) != 0)
138 * A.4.4.1.1 IF (not within 8 bits of end of bitmap)
141 * SET stopbit to end of bitmap
144 if (totbits
> 8) stopbit
=0; /* check all 8 bits */
145 else stopbit
= 7-totbits
+1; /* stop at end of bitmap */
148 * A.4.4.1.2 SUM up number of bits set in this BMS byte
150 for (bitpos
= 7; bitpos
>= stopbit
; bitpos
--)
151 if (val
>> bitpos
& 0x0001) bms
->ulbits_set
+= 1;
153 * A.4.4.1 ENDIF ! any of 8 exists
157 * A.4.4 ENDFOR !each 8-bit loop
161 * A.4 ENDIF !Bitmap follows
166 / * Predefined Bitmap - not supported!! Could add function here
167 to load a bitmap from local storage * /
169 bms->bit_map= Load_predefined_bms (bms->usBMS_id);
181 DPRINT2 ("Exiting %s, Status=%d;\n", func
, status
);