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 1/7] 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 efd5a4e79..c349f5202 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 e488abcd4..2472f6d81 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 0c5f2d5f6..f0db46703 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 8829963f5..21bc48e75 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 */ From 28b9522f6e0e9bfda9bae335f3412528bcfea832 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:15:10 -0700 Subject: [PATCH 2/7] switch field from signed int to unsigned int --- src/bin/jp2/opj_compress.c | 6 +++--- src/lib/openjp2/openjpeg.c | 2 +- src/lib/openjp2/openjpeg.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 2472f6d81..4448f7405 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -914,10 +914,10 @@ static int parse_cmdline_encoder(int argc, char **argv, /* ----------------------------------------------------- */ case 'X': { /* target bitdepth */ - int target_bitdepth = 0; + unsigned int target_bitdepth = 0; char *s = opj_optarg; - sscanf(s, "%d", &target_bitdepth); - if (target_bitdepth <= 0) { + sscanf(s, "%u", &target_bitdepth); + if (target_bitdepth == 0) { fprintf(stderr, "Target bitdepth must be at least 1 bit.\n"); return 1; } diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c index f0db46703..477d3310a 100644 --- a/src/lib/openjp2/openjpeg.c +++ b/src/lib/openjp2/openjpeg.c @@ -756,7 +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->target_bitdepth = 0; 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 21bc48e75..68c15b7fe 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -466,7 +466,7 @@ typedef struct opj_cparameters { /** output file format 0: J2K, 1: JP2, 2: JPT */ int cod_format; /** desired bitdepth from input file */ - int target_bitdepth; + unsigned int target_bitdepth; /*@}*/ /* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */ From 4a1e327415affbf29253fdc2465ae78771b82761 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:44:34 -0700 Subject: [PATCH 3/7] update spacing --- src/bin/jp2/converttif.c | 3 ++- src/bin/jp2/opj_compress.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c index 3744fafb4..f657d2d4c 100644 --- a/src/bin/jp2/converttif.c +++ b/src/bin/jp2/converttif.c @@ -1506,7 +1506,8 @@ 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)) { + } else if ((parameters->target_bitdepth > 0) && + (parameters->target_bitdepth != tiBps)) { for (j = 0; j < numcomps; ++j) { scale_component(&(image->comps[j]), parameters->target_bitdepth); } diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 4448f7405..61fd9ed71 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -918,8 +918,8 @@ static int parse_cmdline_encoder(int argc, char **argv, char *s = opj_optarg; sscanf(s, "%u", &target_bitdepth); if (target_bitdepth == 0) { - fprintf(stderr, "Target bitdepth must be at least 1 bit.\n"); - return 1; + fprintf(stderr, "Target bitdepth must be at least 1 bit.\n"); + return 1; } parameters->target_bitdepth = target_bitdepth; } From ae566ad713a298eac539b5a143ae56cf1f33f491 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Thu, 21 Oct 2021 13:31:38 -0700 Subject: [PATCH 4/7] refactor to not use opj_cparameters_t --- src/bin/jp2/convert.h | 3 ++- src/bin/jp2/converttif.c | 8 ++++---- src/bin/jp2/opj_compress.c | 19 +++++++++++-------- src/lib/openjp2/openjpeg.c | 1 - src/lib/openjp2/openjpeg.h | 2 -- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/bin/jp2/convert.h b/src/bin/jp2/convert.h index 61439d49a..b9b009ce1 100644 --- a/src/bin/jp2/convert.h +++ b/src/bin/jp2/convert.h @@ -95,7 +95,8 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters); int imagetobmp(opj_image_t *image, const char *outfile); /* TIFF conversion*/ -opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters); +opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters, + const unsigned int target_bitdepth); int imagetotif(opj_image_t *image, const char *outfile); /** Load a single image component encoded in PGX file format diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c index f657d2d4c..5c6295cec 100644 --- a/src/bin/jp2/converttif.c +++ b/src/bin/jp2/converttif.c @@ -1247,7 +1247,8 @@ static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, * libtiff/tif_getimage.c : 1,2,4,8,16 bitspersample accepted * CINEMA : 12 bit precision */ -opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) +opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters, + const unsigned int target_bitdepth) { int subsampling_dx = parameters->subsampling_dx; int subsampling_dy = parameters->subsampling_dy; @@ -1506,10 +1507,9 @@ 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)) { + } else if ((target_bitdepth > 0) && (target_bitdepth != tiBps)) { for (j = 0; j < numcomps; ++j) { - scale_component(&(image->comps[j]), parameters->target_bitdepth); + scale_component(&(image->comps[j]), target_bitdepth); } } return image; diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 61fd9ed71..aa1300240 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -189,7 +189,8 @@ static void encode_help_display(void) 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, " if all bits are unwanted.\n"); + fprintf(stdout, " (Currently only implemented for TIF.)\n"); fprintf(stdout, "-b ,\n"); fprintf(stdout, " Code-block size. The dimension must respect the constraint \n"); @@ -604,7 +605,8 @@ static int parse_cmdline_encoder(int argc, char **argv, int* pOutFramerate, OPJ_BOOL* pOutPLT, OPJ_BOOL* pOutTLM, - int* pOutNumThreads) + int* pOutNumThreads, + unsigned int* pTarget_bitdepth) { OPJ_UINT32 i, j; int totlen, c; @@ -914,14 +916,12 @@ static int parse_cmdline_encoder(int argc, char **argv, /* ----------------------------------------------------- */ case 'X': { /* target bitdepth */ - unsigned int target_bitdepth = 0; char *s = opj_optarg; - sscanf(s, "%u", &target_bitdepth); - if (target_bitdepth == 0) { + sscanf(s, "%u", pTarget_bitdepth); + if (*pTarget_bitdepth == 0) { fprintf(stderr, "Target bitdepth must be at least 1 bit.\n"); return 1; } - parameters->target_bitdepth = target_bitdepth; } break; @@ -1925,6 +1925,9 @@ int main(int argc, char **argv) OPJ_BOOL TLM = OPJ_FALSE; int num_threads = 0; + /** desired bitdepth from input file */ + unsigned int target_bitdepth = 0; + /* set encoding parameters to default values */ opj_set_default_encoder_parameters(¶meters); @@ -1945,7 +1948,7 @@ int main(int argc, char **argv) 255; /* This will be set later according to the input image or the provided option */ if (parse_cmdline_encoder(argc, argv, ¶meters, &img_fol, &raw_cp, indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM, - &num_threads) == 1) { + &num_threads, &target_bitdepth) == 1) { ret = 1; goto fin; } @@ -2038,7 +2041,7 @@ int main(int argc, char **argv) #ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: - image = tiftoimage(parameters.infile, ¶meters); + image = tiftoimage(parameters.infile, ¶meters, target_bitdepth); if (!image) { fprintf(stderr, "Unable to load tif(f) file\n"); ret = 1; diff --git a/src/lib/openjp2/openjpeg.c b/src/lib/openjp2/openjpeg.c index 477d3310a..0c5f2d5f6 100644 --- a/src/lib/openjp2/openjpeg.c +++ b/src/lib/openjp2/openjpeg.c @@ -756,7 +756,6 @@ 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 = 0; 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 67819c709..68e4e7e10 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -471,8 +471,6 @@ typedef struct opj_cparameters { int decod_format; /** output file format 0: J2K, 1: JP2, 2: JPT */ int cod_format; - /** desired bitdepth from input file */ - unsigned int target_bitdepth; /*@}*/ /* UniPG>> */ /* NOT YET USED IN THE V2 VERSION OF OPENJPEG */ From 018b7e9b6916e4e2bef5124e307136ed861ceacc Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Thu, 21 Oct 2021 13:48:20 -0700 Subject: [PATCH 5/7] adjust spacing --- src/bin/jp2/opj_compress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index aa1300240..d924c2a89 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -2041,7 +2041,7 @@ int main(int argc, char **argv) #ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: - image = tiftoimage(parameters.infile, ¶meters, target_bitdepth); + image = tiftoimage(parameters.infile, ¶meters, target_bitdepth); if (!image) { fprintf(stderr, "Unable to load tif(f) file\n"); ret = 1; From c96c009acab51f268b971e04d2a83f8c206771b6 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Fri, 22 Oct 2021 11:18:32 -0700 Subject: [PATCH 6/7] make sure tests build --- tests/compare_images.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/compare_images.c b/tests/compare_images.c index 5e644f5a4..f3f876397 100644 --- a/tests/compare_images.c +++ b/tests/compare_images.c @@ -260,6 +260,9 @@ static opj_image_t* readImageFromFileTIF(const char* filename, { opj_image_t* image_read = NULL; opj_cparameters_t parameters; +#ifdef OPJ_HAVE_LIBTIFF + const unsigned int target_bitdepth = 0; +#endif (void)nbFilenamePGX; (void)separator; @@ -284,7 +287,7 @@ static opj_image_t* readImageFromFileTIF(const char* filename, /* Read the tif file corresponding to the component */ #ifdef OPJ_HAVE_LIBTIFF - image_read = tiftoimage(filename, ¶meters); + image_read = tiftoimage(filename, ¶meters, target_bitdepth); #endif if (!image_read) { fprintf(stderr, "Unable to load TIF file\n"); From 7c3ba2dae61dbfc99fbd47121da77d76b42a3e14 Mon Sep 17 00:00:00 2001 From: Matthew Sheby <44249925+msheby@users.noreply.github.com> Date: Tue, 26 Oct 2021 20:17:17 -0700 Subject: [PATCH 7/7] add 'TargetBitDepth' alias --- src/bin/jp2/opj_compress.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index d924c2a89..8c71d4536 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -186,8 +186,8 @@ 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, "-TargetBitDepth \n"); + fprintf(stdout, " Target bit depth.\n"); fprintf(stdout, " Number of bits per component to use from input image\n"); fprintf(stdout, " if all bits are unwanted.\n"); fprintf(stdout, " (Currently only implemented for TIF.)\n"); @@ -626,6 +626,7 @@ static int parse_cmdline_encoder(int argc, char **argv, {"PLT", NO_ARG, NULL, 'A'}, {"threads", REQ_ARG, NULL, 'B'}, {"TLM", NO_ARG, NULL, 'D'}, + {"TargetBitDepth", REQ_ARG, NULL, 'X'}, }; /* parse the command line */