Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require g2c for openjpeg decoding #287

Merged
merged 13 commits into from
Nov 9, 2024
19 changes: 18 additions & 1 deletion .github/workflows/Linux_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ jobs:
make -j2
make install

- name: checkout-g2c
uses: actions/checkout@v4
with:
repository: NOAA-EMC/NCEPLIBS-g2c
path: g2c
ref: develop

- name: build-g2c
run: |
cd g2c
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=~/g2c -DUSE_OpenJPEG=ON -DUSE_Jasper=OFF
make -j2
make install

- name: checkout
uses: actions/checkout@v2
with:
Expand All @@ -115,6 +131,7 @@ jobs:
cd wgrib2
mkdir b
cd b
cmake ${{ matrix.config.options }} -DCMAKE_PREFIX_PATH="~/ip" ..
export CFLAGS='-I/home/runner/g2c/include'
cmake ${{ matrix.config.options }} -DCMAKE_PREFIX_PATH="~/ip;~/g2c" ..
make VERBOSE=1
ctest --verbose --output-on-failure --rerun-failed
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
4 changes: 2 additions & 2 deletions spack/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def url_for_version(self, version):
depends_on("g2c", when="@develop +jasper")
depends_on("zlib-api", when="@3.2: +png")
depends_on("libpng", when="@3.2: +png")
depends_on("openjpeg", when="@3.2: +openjpeg")

depends_on("openjpeg", when="@3.2:3.4 +openjpeg")
depends_on("g2c +openjpeg", when="@develop +openjpeg")

@when("@:2 ^[email protected]:")

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
Loading