#include <stdlib.h>
#include <math.h>
#include <assert.h>
+#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
fprintf(stdout,
" It corresponds to the number of DWT decompositions +1. \n");
fprintf(stdout, " Default: 6.\n");
+ fprintf(stdout, "-TargetBitDepth <target bit depth>\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");
fprintf(stdout, "-b <cblk width>,<cblk height>\n");
fprintf(stdout,
" Code-block size. The dimension must respect the constraint \n");
fprintf(stdout, " Y >= 0 and Y <= 9.\n");
fprintf(stdout,
" framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n");
+ fprintf(stdout, "-GuardBits value\n");
+ fprintf(stdout,
+ " Number of guard bits in [0,7] range. Usually 1 or 2 (default value).\n");
fprintf(stdout, "-jpip\n");
fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
fprintf(stdout, " Currently supports only RPCL order.\n");
fprintf(stdout, "-C <comment>\n");
fprintf(stdout, " Add <comment> in the comment marker segment.\n");
if (opj_has_thread_support()) {
- fprintf(stdout, " -threads <num_threads|ALL_CPUS>\n"
+ fprintf(stdout, "-threads <num_threads|ALL_CPUS>\n"
" Number of threads to use for encoding or ALL_CPUS for all available cores.\n");
}
/* UniPG>> */
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
}
+ if (num_images == UINT_MAX) {
+ fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
+ num_images = 0;
+ break;
+ }
num_images++;
}
closedir(dir);
return fname;
}
-static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+static char get_next_file(unsigned int imageno, dircnt_t *dirptr,
+ img_fol_t *img_fol,
opj_cparameters_t *parameters)
{
char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
char *temp_p, temp1[OPJ_PATH_LEN] = "";
strcpy(image_filename, dirptr->filename[imageno]);
- fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
+ fprintf(stderr, "File Number %u \"%s\"\n", imageno, image_filename);
parameters->decod_format = get_file_format(image_filename);
if (parameters->decod_format == -1) {
return 1;
int* pOutFramerate,
OPJ_BOOL* pOutPLT,
OPJ_BOOL* pOutTLM,
- int* pOutNumThreads)
+ int* pOutGuardBits,
+ int* pOutNumThreads,
+ unsigned int* pTarget_bitdepth)
{
OPJ_UINT32 i, j;
int totlen, c;
{"PLT", NO_ARG, NULL, 'A'},
{"threads", REQ_ARG, NULL, 'B'},
{"TLM", NO_ARG, NULL, 'D'},
+ {"TargetBitDepth", REQ_ARG, NULL, 'X'},
+ {"GuardBits", REQ_ARG, NULL, 'G'}
};
/* 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:G:"
#ifdef USE_JPWL
"W:"
#endif /* USE_JPWL */
/* ----------------------------------------------------- */
- case 'q': { /* add fixed_quality */
+ case 'q': { /* layer allocation by distortion ratio (PSNR) */
char *s = opj_optarg;
while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers])
== 1) {
/* dda */
/* ----------------------------------------------------- */
- case 'f': { /* mod fixed_quality (before : -q) */
+ case 'f': { /* layer allocation by fixed layer */
int *row = NULL, *col = NULL;
OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
}
break;
+ /* ----------------------------------------------------- */
+ case 'X': { /* target bitdepth */
+ char *s = opj_optarg;
+ sscanf(s, "%u", pTarget_bitdepth);
+ if (*pTarget_bitdepth == 0) {
+ fprintf(stderr, "Target bitdepth must be at least 1 bit.\n");
+ return 1;
+ }
+ }
+ break;
+
+ /* ----------------------------------------------------- */
+ case 'G': { /* guard bits */
+ char *s = opj_optarg;
+ sscanf(s, "%d", pOutGuardBits);
+ }
+ break;
+
/* ----------------------------------------------------- */
case 'n': { /* resolution */
parameters->cp_fixed_quality))) {
fprintf(stderr, "[ERROR] options -r -q and -f cannot be used together !!\n");
return 1;
- } /* mod fixed_quality */
+ }
/* if no rate entered, lossless by default */
OPJ_BOOL PLT = OPJ_FALSE;
OPJ_BOOL TLM = OPJ_FALSE;
int num_threads = 0;
+ int guard_bits = -1;
+
+ /** desired bitdepth from input file */
+ unsigned int target_bitdepth = 0;
/* set encoding parameters to default values */
opj_set_default_encoder_parameters(¶meters);
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) {
+ &guard_bits, &num_threads, &target_bitdepth) == 1) {
ret = 1;
goto fin;
}
/* Read directory if necessary */
if (img_fol.set_imgdir == 1) {
num_images = get_num_images(img_fol.imgdirpath);
+ if (num_images == 0) {
+ fprintf(stdout, "Folder is empty\n");
+ ret = 0;
+ goto fin;
+ }
dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
if (dirptr) {
- dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
+ dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
char)); /* Stores at max 10 image file names*/
- dirptr->filename = (char**) malloc(num_images * sizeof(char*));
+ dirptr->filename = (char**) calloc(num_images, sizeof(char*));
if (!dirptr->filename_buf) {
ret = 0;
goto fin;
}
for (i = 0; i < num_images; i++) {
- dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
+ dirptr->filename[i] = dirptr->filename_buf + (size_t)i * OPJ_PATH_LEN;
}
}
if (load_images(dirptr, img_fol.imgdirpath) == 1) {
ret = 0;
goto fin;
}
- if (num_images == 0) {
- fprintf(stdout, "Folder is empty\n");
- ret = 0;
- goto fin;
- }
+
} else {
num_images = 1;
}
fprintf(stderr, "\n");
if (img_fol.set_imgdir == 1) {
- if (get_next_file((int)imageno, dirptr, &img_fol, ¶meters)) {
+ if (get_next_file(imageno, dirptr, &img_fol, ¶meters)) {
fprintf(stderr, "skipping file...\n");
continue;
}
#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;
goto fin;
}
- if (PLT || TLM) {
- const char* options[3] = { NULL, NULL, NULL };
+ {
+ const char* options[4] = { NULL, NULL, NULL, NULL };
int iOpt = 0;
+ char szGuardBits[32];
if (PLT) {
options[iOpt++] = "PLT=YES";
}
if (TLM) {
options[iOpt++] = "TLM=YES";
}
- (void)iOpt;
- if (!opj_encoder_set_extra_options(l_codec, options)) {
+ if (guard_bits >= 0) {
+ sprintf(szGuardBits, "GUARD_BITS=%d", guard_bits);
+ options[iOpt++] = szGuardBits;
+ }
+ if (iOpt > 0 && !opj_encoder_set_extra_options(l_codec, options)) {
fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n");
opj_destroy_codec(l_codec);
opj_image_destroy(image);
}
for (i = 0; i < l_nb_tiles; ++i) {
if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
- fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",
+ fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %u!\n",
i);
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);