LCOV - code coverage report
Current view: top level - frmts/grib/degrib/g2clib - jpcunpack.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 24 31 77.4 %
Date: 2024-11-21 22:18:42 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include <float.h>
       2             : #include <stdio.h>
       3             : #include <stdlib.h>
       4             : #include "grib2.h"
       5             : 
       6          36 : static float DoubleToFloatClamp(double val) {
       7          36 :    if (val >= FLT_MAX) return FLT_MAX;
       8          36 :    if (val <= -FLT_MAX) return -FLT_MAX;
       9          36 :    return (float)val;
      10             : }
      11             : 
      12          18 : 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             : {
      46             : 
      47             :       g2int  *ifld;
      48             :       g2int  j,nbits, iret;
      49             :       g2float  ref,bscale,dscale;
      50             : 
      51          18 :       rdieee(idrstmpl+0,&ref,1);
      52          18 :       bscale = DoubleToFloatClamp(int_power(2.0,idrstmpl[1]));
      53          18 :       dscale = DoubleToFloatClamp(int_power(10.0,-idrstmpl[2]));
      54          18 :       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          18 :       *fld = 0;
      60          18 :       if (nbits != 0) {
      61             : 
      62          15 :          ifld = NULL;
      63          15 :          iret= (g2int) dec_jpeg2000(cpack,len,&ifld,ndpts);
      64          15 :          if( iret != 0 )
      65             :          {
      66           0 :              free(ifld);
      67           0 :              return -1;
      68             :          }
      69          15 :          *fld =(g2float *)calloc(ndpts,sizeof(g2float));
      70          15 :          if( *fld == 0 )
      71             :          {
      72           0 :              free(ifld);
      73           0 :              return -1;
      74             :          }
      75      570071 :          for (j=0;j<ndpts;j++) {
      76      570056 :            (*fld)[j]=(((g2float)ifld[j]*bscale)+ref)*dscale;
      77             :          }
      78          15 :          free(ifld);
      79             :       }
      80             :       else {
      81             :          // Limit to 2 GB
      82           3 :          if( ndpts > 500 * 1024 * 1024 )
      83             :          {
      84           0 :              fprintf(stderr, "jpcunpack: ndpts = %d > 500 * 1024 * 1024", ndpts );
      85           0 :              return -1;
      86             :          }
      87           3 :          *fld =(g2float *)calloc(ndpts,sizeof(g2float));
      88           3 :          if( *fld == 0 )
      89             :          {
      90           0 :              return -1;
      91             :          }
      92        3435 :          for (j=0;j<ndpts;j++) (*fld)[j]=ref * dscale;
      93             :       }
      94             : 
      95          18 :       return(0);
      96             : }

Generated by: LCOV version 1.14