/src/gdal/frmts/grib/degrib/g2clib/jpcunpack.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | #include <float.h>  | 
2  |  | #include <stdio.h>  | 
3  |  | #include <stdlib.h>  | 
4  |  | #include "grib2.h"  | 
5  |  |  | 
6  | 40  | static float DoubleToFloatClamp(double val) { | 
7  | 40  |    if (val >= FLT_MAX) return FLT_MAX;  | 
8  | 40  |    if (val <= -FLT_MAX) return -FLT_MAX;  | 
9  | 40  |    return (float)val;  | 
10  | 40  | }  | 
11  |  |  | 
12  |  | g2int jpcunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,  | 
13  |  |                 g2float **fld)  | 
14  |  | //$$$  SUBPROGRAM DOCUMENTATION BLOCK  | 
15  |  | //                .      .    .                                       .  | 
16  |  | // SUBPROGRAM:    jpcunpack  | 
17  |  | //   PRGMMR: Gilbert          ORG: W/NP11    DATE: 2003-08-27  | 
18  |  | //  | 
19  |  | // ABSTRACT: This subroutine unpacks a data field that was packed into a  | 
20  |  | //   JPEG2000 code stream  | 
21  |  | //   using info from the GRIB2 Data Representation Template 5.40 or 5.40000.  | 
22  |  | //  | 
23  |  | // PROGRAM HISTORY LOG:  | 
24  |  | // 2003-08-27  Gilbert  | 
25  |  | //  | 
26  |  | // USAGE:    jpcunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,  | 
27  |  | //                     g2float *fld)  | 
28  |  | //   INPUT ARGUMENT LIST:  | 
29  |  | //     cpack    - The packed data field (character*1 array)  | 
30  |  | //     len      - length of packed field cpack().  | 
31  |  | //     idrstmpl - Pointer to array of values for Data Representation  | 
32  |  | //                Template 5.40 or 5.40000  | 
33  |  | //     ndpts    - The number of data values to unpack  | 
34  |  | //  | 
35  |  | //   OUTPUT ARGUMENT LIST:  | 
36  |  | //     fld[]    - Contains the unpacked data values  | 
37  |  | //  | 
38  |  | // REMARKS: None  | 
39  |  | //  | 
40  |  | // ATTRIBUTES:  | 
41  |  | //   LANGUAGE: C  | 
42  |  | //   MACHINE:  IBM SP  | 
43  |  | //  | 
44  |  | //$$$  | 
45  | 20  | { | 
46  |  |  | 
47  | 20  |       g2int  *ifld;  | 
48  | 20  |       g2int  j,nbits, iret;  | 
49  | 20  |       g2float  ref,bscale,dscale;  | 
50  |  |  | 
51  | 20  |       rdieee(idrstmpl+0,&ref,1);  | 
52  | 20  |       bscale = DoubleToFloatClamp(int_power(2.0,idrstmpl[1]));  | 
53  | 20  |       dscale = DoubleToFloatClamp(int_power(10.0,-idrstmpl[2]));  | 
54  | 20  |       nbits = idrstmpl[3];  | 
55  |  | //  | 
56  |  | //  if nbits equals 0, we have a constant field where the reference value  | 
57  |  | //  is the data value at each gridpoint  | 
58  |  | //  | 
59  | 20  |       *fld = 0;  | 
60  | 20  |       if (nbits != 0) { | 
61  |  |  | 
62  | 14  |          ifld = NULL;  | 
63  | 14  |          iret= (g2int) dec_jpeg2000(cpack,len,&ifld,ndpts);  | 
64  | 14  |          if( iret != 0 )  | 
65  | 14  |          { | 
66  | 14  |              free(ifld);  | 
67  | 14  |              return -1;  | 
68  | 14  |          }  | 
69  | 0  |          *fld =(g2float *)calloc(ndpts,sizeof(g2float));  | 
70  | 0  |          if( *fld == 0 )  | 
71  | 0  |          { | 
72  | 0  |              free(ifld);  | 
73  | 0  |              return -1;  | 
74  | 0  |          }  | 
75  | 0  |          for (j=0;j<ndpts;j++) { | 
76  | 0  |            (*fld)[j]=(((g2float)ifld[j]*bscale)+ref)*dscale;  | 
77  | 0  |          }  | 
78  | 0  |          free(ifld);  | 
79  | 0  |       }  | 
80  | 6  |       else { | 
81  |  |          // Limit to 2 GB  | 
82  | 6  |          if( ndpts > 500 * 1024 * 1024 )  | 
83  | 0  |          { | 
84  | 0  |              fprintf(stderr, "jpcunpack: ndpts = %d > 500 * 1024 * 1024", ndpts );  | 
85  | 0  |              return -1;  | 
86  | 0  |          }  | 
87  | 6  |          *fld =(g2float *)calloc(ndpts,sizeof(g2float));  | 
88  | 6  |          if( *fld == 0 )  | 
89  | 0  |          { | 
90  | 0  |              return -1;  | 
91  | 0  |          }  | 
92  | 12  |          for (j=0;j<ndpts;j++) (*fld)[j]=ref * dscale;  | 
93  | 6  |       }  | 
94  |  |  | 
95  | 6  |       return(0);  | 
96  | 20  | }  |