Coverage Report

Created: 2026-02-14 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/grib/degrib/g2clib/jpcunpack.c
Line
Count
Source
1
#include <float.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include "grib2.h"
5
6
10
static float DoubleToFloatClamp(double val) {
7
10
   if (val >= FLT_MAX) return FLT_MAX;
8
10
   if (val <= -FLT_MAX) return -FLT_MAX;
9
10
   return (float)val;
10
10
}
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
5
{
46
47
5
      g2int  *ifld;
48
5
      g2int  j,nbits, iret;
49
5
      g2float  ref,bscale,dscale;
50
51
5
      rdieee(idrstmpl+0,&ref,1);
52
5
      bscale = DoubleToFloatClamp(int_power(2.0,idrstmpl[1]));
53
5
      dscale = DoubleToFloatClamp(int_power(10.0,-idrstmpl[2]));
54
5
      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
5
      *fld = 0;
60
5
      if (nbits != 0) {
61
62
5
         ifld = NULL;
63
5
         iret= (g2int) dec_jpeg2000(cpack,len,&ifld,ndpts);
64
5
         if( iret != 0 )
65
5
         {
66
5
             free(ifld);
67
5
             return -1;
68
5
         }
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
0
      else {
81
         // Limit to 2 GB
82
0
         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
0
         *fld =(g2float *)calloc(ndpts,sizeof(g2float));
88
0
         if( *fld == 0 )
89
0
         {
90
0
             return -1;
91
0
         }
92
0
         for (j=0;j<ndpts;j++) (*fld)[j]=ref * dscale;
93
0
      }
94
95
0
      return(0);
96
5
}