Skip to content

Commit

Permalink
require g2c for openjpeg pack/unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
AlysonStahl-NOAA committed Nov 1, 2024
1 parent cfc151d commit 1fed595
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ endif()
# Find required packages to use OpenJPEG
message(STATUS "Checking if the user wants to use OpenJPEG...")
if(USE_OPENJPEG)
find_package(OpenJPEG REQUIRED)
find_package(g2c 1.9.0 CONFIG REQUIRED)
endif()

message(STATUS "Checking if the user want to use OpenMP...")
Expand Down
9 changes: 2 additions & 7 deletions wgrib2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Alias.c Ave_test.c Ave_var.c Bbox.c bitstream.c Box_ave.c
Check_pdt_size.c Checksum.c Cluster.c cname.c codetable_4_230.c
CodeTable.c Code_Values.c Code_Values_JMA.c complex_pk.c Config.c
copy.c crc32.c Cress_lola.c Csv.c Csv_long.c cubed_sphere2ll.c
CubeFace2global.c Cyclic.c Data.c decenc_openjpeg.c dec_png_clone.c
CubeFace2global.c Cyclic.c Data.c dec_png_clone.c
Dump.c Earth.c Else.c End.c Endif.c Ensemble.c
Ens_processing.c Ens_qc.c EOF.c Export_lonlat.c ExtName.c
fatal_error.c Fcst_ave.c ffopen.c Fi.c File.c Fix_CFSv2_fcst.c
Expand Down Expand Up @@ -143,16 +143,11 @@ if(USE_AEC)
target_link_libraries(wgrib2_exe PRIVATE ${LIBAEC_LIBRARIES})
endif()

if(USE_JASPER)
if(USE_JASPER OR USE_OPENJPEG)
target_link_libraries(obj_lib PUBLIC g2c::g2c)
target_link_libraries(wgrib2_exe PRIVATE g2c::g2c)
endif()

if(USE_OPENJPEG)
include_directories(${OPENJPEG_INCLUDE_DIRS})
target_link_libraries(wgrib2_exe PRIVATE ${OPENJPEG_LIBRARIES})
endif()

target_link_libraries(wgrib2_exe PRIVATE obj_lib)
# target_link_libraries(wgrib2_exe PRIVATE wgrib2_lib)

Expand Down
16 changes: 1 addition & 15 deletions wgrib2/jpeg_pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
#include "wgrib2.h"
#include "fnlist.h"

#ifdef USE_JASPER
#include "grib2.h"
#endif

/* 10/2024 Public Domain Wesley Ebisuzaki */

#if defined USE_JASPER || defined USE_OPENJPEG

#include "grib2.h"
/*
* writes out jpeg2000 compressed grib message
*/
Expand All @@ -31,9 +28,7 @@ int jpeg2000_grib_out(unsigned char **sec, float *data, unsigned int ndata,
int i, k, nbits, nbytes;
int ltype, ratio, retry;
char *outjpc;
#ifdef USE_JASPER
unsigned char *cdata, *p;
#endif

/* required passed sections */
sec0 = sec[0];
Expand Down Expand Up @@ -124,7 +119,6 @@ int jpeg2000_grib_out(unsigned char **sec, float *data, unsigned int ndata,
nbytes = (nbits + 7) / 8;
if (nbytes > 4) fatal_error_i("jpeg2000_grib_out number of bytes is %d > 4", nbytes);

#ifdef USE_JASPER
/* floats -> integers -> bytes */

cdata = (unsigned char *) malloc(nbytes * (size_t) n_defined);
Expand Down Expand Up @@ -156,28 +150,20 @@ int jpeg2000_grib_out(unsigned char **sec, float *data, unsigned int ndata,
}
}

// jas_init();
#endif

ltype = 0;
ratio = 1;
retry = 0;

jpclen = 4*n_defined+200;
outjpc = (char *) malloc(jpclen);

#ifdef USE_JASPER
i = g2c_enc_jpeg2000(cdata,ix,iy,nbits,ltype,ratio,retry,outjpc,jpclen);
// we try to catch following error: "error: too few guard bits (need at least x)"
if (i == -3) {
retry = 1;
i = g2c_enc_jpeg2000(cdata,ix,iy,nbits,ltype,ratio,retry,outjpc,jpclen);
}
free(cdata);
#endif
#ifdef USE_OPENJPEG
i = enc_jpeg2000_clone_float(data,ix,iy,nbits,ltype,ratio,retry,outjpc,jpclen);
#endif

if (i <= 0) fatal_error_i("enc_jpeg error %d", i);
}
Expand Down
29 changes: 1 addition & 28 deletions wgrib2/unpk.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "wgrib2.h"
#include "grb2.h"

#ifdef USE_JASPER
#if defined USE_JASPER || defined USE_OPENJPEG
#include "grib2.h"
#endif

Expand Down Expand Up @@ -193,7 +193,6 @@ int unpk_grib(unsigned char **sec, float *data) {

// decode jpeg2000

#ifdef USE_JASPER
ifld = (int *) malloc(ndata * sizeof(int));
if (ifld == 0) fatal_error("unpk: memory allocation error","");
err = g2c_dec_jpeg2000((char *) sec[7]+5, (size_t) GB2_Sec7_size(sec)-5, ifld);
Expand All @@ -214,32 +213,6 @@ int unpk_grib(unsigned char **sec, float *data) {
}
free(ifld);
return 0;
#endif
#ifdef USE_OPENJPEG
ifld = (int *) malloc(ndata * sizeof(int));
if (ifld == 0) fatal_error("unpk: memory allocation error","");
err = dec_jpeg2000_clone((char *) sec[7]+5, (int) GB2_Sec7_size(sec)-5, ifld);
if (err != 0) fatal_error_i("dec_jpeg2000, error %d",err);

if (bitmap_flag == 255) {
#pragma omp parallel for private(ii)
for (ii = 0; ii < ndata; ii++) {
data[ii] = ((ifld[ii]*bin_scale)+reference)*dec_scale;
}
}
else if (bitmap_flag == 0 || bitmap_flag == 254) {
mask_pointer = sec[6] + 6;
mask = 0;
kk = 0;
for (ii = 0; ii < ndata; ii++) {
if ((ii & 7) == 0) mask = *mask_pointer++;
data[ii] = (mask & 128) ? ((ifld[kk++]*bin_scale)+reference)*dec_scale : UNDEFINED;
mask <<= 1;
}
}
free(ifld);
return 0;
#endif
}
#endif

Expand Down
5 changes: 0 additions & 5 deletions wgrib2/wgrib2.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,6 @@ int scaling(unsigned char **sec, double *base, int *decimal, int *binary, int *n
unsigned char *mk_bms(float *data, unsigned int *ndata);

int dec_png_clone(unsigned char *pngbuf,int *width,int *height, unsigned char *cout, int *grib2_bit_depth, unsigned int ndata);
#ifdef USE_OPENJPEG
int dec_jpeg2000_clone(char *injpc, int bufsize, int *outfld);
int enc_jpeg2000_clone_float(float *data, int width, int height, int nbits,
int ltype, int ratio, int retry, char *outjpc, int jpclen);
#endif
int ieee_grib_out(unsigned char **sec, float *data, unsigned int ndata, struct seq_file *out);
int jpeg_grib_out(unsigned char **sec, float *data, unsigned int ndata,
int nx, int ny, int use_scale, int dec_scale, int bin_scale, FILE *out);
Expand Down

0 comments on commit 1fed595

Please sign in to comment.