From 4735d941d9b11bcca7cd55fec5519f440b2c7e64 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Wed, 20 Oct 2021 15:29:04 -0700 Subject: [PATCH] enable decimation of input TIF --- src/bin/jp2/converttif.c | 4 ++++ src/bin/jp2/opj_compress.c | 19 ++++++++++++++++++- src/lib/openjp2/openjpeg.c | 1 + src/lib/openjp2/openjpeg.h | 2 ++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c index efd5a4e7..c349f520 100644 --- a/src/bin/jp2/converttif.c +++ b/src/bin/jp2/converttif.c @@ -1507,6 +1507,10 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) scale_component(&(image->comps[j]), 12); } + } else if ((parameters->target_bitdepth > 0) && (parameters->target_bitdepth != tiBps)) { + for (j = 0; j < numcomps; ++j) { + scale_component(&(image->comps[j]), parameters->target_bitdepth); + } } return image; diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index e488abcd..2472f6d8 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -186,6 +186,10 @@ static void encode_help_display(void) fprintf(stdout, " It corresponds to the number of DWT decompositions +1. \n"); fprintf(stdout, " Default: 6.\n"); + fprintf(stdout, "-X \n"); + fprintf(stdout, " Target bitdepth.\n"); + fprintf(stdout, " Number of bits per component to use from input image\n"); + fprintf(stdout, " if all bits are unwanted. \n"); fprintf(stdout, "-b ,\n"); fprintf(stdout, " Code-block size. The dimension must respect the constraint \n"); @@ -623,7 +627,7 @@ static int parse_cmdline_encoder(int argc, char **argv, }; /* parse the command line */ - const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:" + const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:" #ifdef USE_JPWL "W:" #endif /* USE_JPWL */ @@ -908,6 +912,19 @@ static int parse_cmdline_encoder(int argc, char **argv, } break; + /* ----------------------------------------------------- */ + case 'X': { /* target bitdepth */ + int target_bitdepth = 0; + char *s = opj_optarg; + sscanf(s, "%d", &target_bitdepth); + if (target_bitdepth <= 0) { + fprintf(stderr, "Target bitdepth must be at least 1 bit.\n"); + return 1; + } + parameters->target_bitdepth = target_bitdepth; + } + break; + /* ----------------------------------------------------- */ case 'n': { /* resolution */ diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c index 0c5f2d5f..f0db4670 100644 --- a/src/lib/openjp2/openjpeg.c +++ b/src/lib/openjp2/openjpeg.c @@ -756,6 +756,7 @@ void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t parameters->tp_on = 0; parameters->decod_format = -1; parameters->cod_format = -1; + parameters->target_bitdepth = -1; parameters->tcp_rates[0] = 0; parameters->tcp_numlayers = 0; parameters->cp_disto_alloc = 0; diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 8829963f..21bc48e7 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -465,6 +465,8 @@ typedef struct opj_cparameters { int decod_format; /** output file format 0: J2K, 1: JP2, 2: JPT */ int cod_format; + /** desired bitdepth from input file */ + int target_bitdepth; /*@}*/ /* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */