Skip to content

Commit

Permalink
New argument: nthreads
Browse files Browse the repository at this point in the history
Commit new image and update README
  • Loading branch information
jdavid committed Aug 29, 2023
1 parent 4e89cd6 commit 8735fc0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ supported.

# Usage from Python

The examples are not distributed with the wheel, but you can just clone the project:

git clone https://github.com/Blosc/blosc2_openhtj2k.git
cd blosc2_openhtj2k

In the examples folder there are the compress and decompress scripts, both take two
required arguments, for the input and output file:

Expand All @@ -33,10 +38,14 @@ To try out these scripts first install the required software:
pip install blosc2-openhtj2k
pip install Pillow

Then you can run the scripts, for example:
Then you can run the scripts, from the examples folder, for example:

cd examples
python compress.py kodim23.png /tmp/kodim23.b2nd
python decompress.py /tmp/kodim23.b2nd /tmp/kodim23.png

python examples/compress.py examples/teapot.ppm /tmp/teapot.b2nd
python examples/decompress.py /tmp/teapot.b2nd /tmp/teapot.png
Note that the examples cannot be run from the project's root, because it will fail to
import `blosc2_openhtj2k`, since there's a directory with that name.

For details on the arguments these commands accept call them with the `--help` option.

Expand Down
3 changes: 2 additions & 1 deletion blosc2_openhtj2k/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def print_libpath():
'qfactor' : 255,
'isJPH' : False,
'color_space' : 0,
'nthreads' : 1,
# COD
'blkwidth' : 4,
'blkheight' : 4,
Expand Down Expand Up @@ -60,7 +61,7 @@ def set_params_defaults(**kwargs):
params.update(kwargs)
args = params.values()
args = list(args)
args[16] = ctypes.c_double(args[16])
args[17] = ctypes.c_double(args[17])

libpath = get_libpath()
lib = ctypes.cdll.LoadLibrary(libpath)
Expand Down
1 change: 1 addition & 0 deletions examples/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def compress(im, urlpath=None, **kwargs):
add_argument('--qfactor', type=int, help='Quality factor: 0-100')
#add_argument('--isJPH', action='store_true', help='')
add_argument('--color-space', type=int, help='0:RGB 1:YCC')
add_argument('--nthreads', type=int, help='Number of threads')
add_argument('--blkwidth', type=int, help='Precinct width (default: 4)')
add_argument('--blkheight', type=int, help='Precinct height (default: 4)')
args = parser.parse_args()
Expand Down
Binary file added examples/kodim23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed output/.keep
Empty file.
17 changes: 11 additions & 6 deletions src/blosc2_htj2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ static blosc2_openhtj2k_qcd_params params_qcd_default = {
};

static blosc2_openhtj2k_params params_defaults = {
.qfactor = NO_QFACTOR, // Quality factor
.qfactor = NO_QFACTOR, // Quality factor
.isJPH = false,
.color_space = 0, // 0:RGB 1:YCC (or YCbCr)
.color_space = 0, // 0:RGB 1:YCC (or YCbCr)
.nthreads = 1, // Number of threads
};

int blosc2_openhtj2k_encoder(
Expand Down Expand Up @@ -82,13 +83,14 @@ int blosc2_openhtj2k_encoder(
uint8_t qfactor = params_defaults.qfactor;
bool isJPH = params_defaults.isJPH;
uint8_t color_space = params_defaults.color_space;
uint32_t num_threads = 1;
uint32_t nthreads = params_defaults.nthreads;

blosc2_openhtj2k_params* plugin_params = (blosc2_openhtj2k_params*) cparams->codec_params;
if (plugin_params != NULL) {
qfactor = plugin_params->qfactor;
isJPH = plugin_params->isJPH;
color_space = plugin_params->color_space;
nthreads = plugin_params->nthreads;
}

// Input buffer
Expand Down Expand Up @@ -179,7 +181,7 @@ int blosc2_openhtj2k_encoder(
qfactor, // quality factor (0-100 or 255)
isJPH,
color_space, // 0: RGB or 1: YCC
num_threads // num_threads
nthreads // Number of threads (default: 1)
);

encoder.set_output_buffer(outbuf);
Expand Down Expand Up @@ -212,9 +214,10 @@ int blosc2_openhtj2k_decoder(
{
// Input variables
uint8_t reduce_NL = 0; // Number of DWT resolution reduction (0-32)
uint32_t num_threads = 1;
int32_t num_iterations = 1; // Number of iterations (1-INT32_MAX)

uint32_t nthreads = params_defaults.nthreads;

// Decode
std::vector<int32_t *> buf;
std::vector<uint32_t> img_width;
Expand All @@ -223,7 +226,7 @@ int blosc2_openhtj2k_decoder(
std::vector<bool> img_signed;
for (int i = 0; i < num_iterations; ++i) {
// Create decoder
open_htj2k::openhtj2k_decoder decoder(input, input_len, reduce_NL, num_threads);
open_htj2k::openhtj2k_decoder decoder(input, input_len, reduce_NL, nthreads);

// Clear vectors
for (auto &j : buf) {
Expand Down Expand Up @@ -282,6 +285,7 @@ int set_params_defaults(
uint8_t qfactor,
bool isJPH,
uint8_t color_space,
uint32_t nthreads,
uint16_t cod_blkwidth,
uint16_t cod_blkheight,
bool cod_is_max_precincts,
Expand All @@ -301,6 +305,7 @@ int set_params_defaults(
params_defaults.qfactor = qfactor;
params_defaults.isJPH = isJPH;
params_defaults.color_space = color_space;
params_defaults.nthreads = nthreads;
params_cod_default.blkwidth = cod_blkwidth;
params_cod_default.blkheight = cod_blkheight;
params_cod_default.is_max_precincts = cod_is_max_precincts;
Expand Down
2 changes: 2 additions & 0 deletions src/blosc2_htj2k.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {
uint32_t YTOsiz;
blosc2_openhtj2k_cod_params *cod;
blosc2_openhtj2k_qcd_params *qcd;
uint32_t nthreads;
} blosc2_openhtj2k_params;

/* Extra functions */
Expand All @@ -70,6 +71,7 @@ int set_params_defaults(
uint8_t qfactor,
bool isJPH,
uint8_t color_space,
uint32_t nthreads,
uint16_t cod_blkwidth,
uint16_t cod_blkheight,
bool cod_is_max_precincts,
Expand Down

0 comments on commit 8735fc0

Please sign in to comment.