Line data Source code
1 : #include <stdio.h>
2 : #include <stdlib.h>
3 : #include <limits.h>
4 : #include "grib2.h"
5 :
6 16 : g2int pngunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,
7 : g2float *fld)
8 : //$$$ SUBPROGRAM DOCUMENTATION BLOCK
9 : // . . . .
10 : // SUBPROGRAM: pngunpack
11 : // PRGMMR: Gilbert ORG: W/NP11 DATE: 2003-08-27
12 : //
13 : // ABSTRACT: This subroutine unpacks a data field that was packed into a
14 : // PNG image format
15 : // using info from the GRIB2 Data Representation Template 5.41 or 5.40010.
16 : //
17 : // PROGRAM HISTORY LOG:
18 : // 2003-08-27 Gilbert
19 : //
20 : // USAGE: pngunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,
21 : // g2float *fld)
22 : // INPUT ARGUMENT LIST:
23 : // cpack - The packed data field (character*1 array)
24 : // len - length of packed field cpack().
25 : // idrstmpl - Pointer to array of values for Data Representation
26 : // Template 5.41 or 5.40010
27 : // ndpts - The number of data values to unpack
28 : //
29 : // OUTPUT ARGUMENT LIST:
30 : // fld[] - Contains the unpacked data values
31 : //
32 : // REMARKS: None
33 : //
34 : // ATTRIBUTES:
35 : // LANGUAGE: C
36 : // MACHINE: IBM SP
37 : //
38 : //$$$
39 : {
40 :
41 : g2int *ifld;
42 16 : g2int j,nbits,iret = 0,width,height;
43 : g2float refD, refV,bscale,dscale, bdscale;
44 : unsigned char *ctemp;
45 :
46 16 : rdieee(idrstmpl+0,&refV,1);
47 16 : bscale = (g2float)int_power(2.0,idrstmpl[1]);
48 16 : dscale = (g2float)int_power(10.0,-idrstmpl[2]);
49 16 : bdscale = bscale * dscale;
50 16 : refD = refV * dscale;
51 :
52 16 : nbits = idrstmpl[3];
53 : //
54 : // if nbits equals 0, we have a constant field where the reference value
55 : // is the data value at each gridpoint
56 : //
57 16 : if (nbits != 0) {
58 14 : int nbytes = nbits/8;
59 14 : if( ndpts != 0 && nbytes > INT_MAX / ndpts )
60 : {
61 0 : return 1;
62 : }
63 14 : ifld=(g2int *)calloc(ndpts,sizeof(g2int));
64 : // Was checked just before
65 : // coverity[integer_overflow,overflow_sink]
66 14 : ctemp=(unsigned char *)calloc((size_t)(ndpts)*nbytes,1);
67 14 : if ( ifld == NULL || ctemp == NULL) {
68 0 : fprintf(stderr, "Could not allocate space in jpcunpack.\n"
69 : "Data field NOT unpacked.\n");
70 0 : free(ifld);
71 0 : free(ctemp);
72 0 : return(1);
73 : }
74 14 : iret=(g2int)dec_png(cpack,len,&width,&height,ctemp, ndpts, nbits);
75 14 : gbits(ctemp,ndpts*nbytes,ifld,0,nbits,0,ndpts);
76 4504420 : for (j=0;j<ndpts;j++) {
77 4504410 : fld[j] = refD + bdscale*(g2float)(ifld[j]);
78 : }
79 14 : free(ctemp);
80 14 : free(ifld);
81 : }
82 : else {
83 4 : for (j=0;j<ndpts;j++) fld[j]=refD;
84 : }
85 :
86 16 : return(iret);
87 : }
|