Cleanup code related to quality layer allocation, and add a few safety checks
[openjpeg.git] / src / bin / jp2 / opj_compress.c
index e488abcd46142d14e21b582a99bf37e642d4bdbc..8ba3b6d772e093d8c2e7618e4a6f8509fff98907 100644 (file)
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 #include <math.h>
 #include <assert.h>
+#include <limits.h>
 
 #ifdef _WIN32
 #include "windirent.h"
@@ -186,6 +187,11 @@ 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, "-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");
@@ -298,13 +304,16 @@ static void encode_help_display(void)
     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>> */
@@ -480,6 +489,11 @@ static unsigned int get_num_images(char *imgdirpath)
         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);
@@ -542,7 +556,8 @@ static char * get_file_name(char *name)
     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],
@@ -550,7 +565,7 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
     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;
@@ -600,7 +615,9 @@ static int parse_cmdline_encoder(int argc, char **argv,
                                  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;
@@ -620,10 +637,12 @@ 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'},
+        {"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 */
@@ -827,7 +846,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
 
         /* ----------------------------------------------------- */
 
-        case 'q': {         /* add fixed_quality */
+        case 'q': {         /* layer allocation by distortion ratio (PSNR) */
             char *s = opj_optarg;
             while (sscanf(s, "%f", &parameters->tcp_distoratio[parameters->tcp_numlayers])
                     == 1) {
@@ -847,7 +866,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
         /* 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;
 
@@ -908,6 +927,24 @@ static int parse_cmdline_encoder(int argc, char **argv,
         }
         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 */
@@ -1775,7 +1812,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
                   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 */
@@ -1907,6 +1944,10 @@ int main(int argc, char **argv)
     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(&parameters);
@@ -1928,7 +1969,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, &parameters, &img_fol, &raw_cp,
                               indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM,
-                              &num_threads) == 1) {
+                              &guard_bits, &num_threads, &target_bitdepth) == 1) {
         ret = 1;
         goto fin;
     }
@@ -1936,28 +1977,29 @@ int main(int argc, char **argv)
     /* 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;
     }
@@ -1967,7 +2009,7 @@ int main(int argc, char **argv)
         fprintf(stderr, "\n");
 
         if (img_fol.set_imgdir == 1) {
-            if (get_next_file((int)imageno, dirptr, &img_fol, &parameters)) {
+            if (get_next_file(imageno, dirptr, &img_fol, &parameters)) {
                 fprintf(stderr, "skipping file...\n");
                 continue;
             }
@@ -2021,7 +2063,7 @@ int main(int argc, char **argv)
 
 #ifdef OPJ_HAVE_LIBTIFF
         case TIF_DFMT:
-            image = tiftoimage(parameters.infile, &parameters);
+            image = tiftoimage(parameters.infile, &parameters, target_bitdepth);
             if (!image) {
                 fprintf(stderr, "Unable to load tif(f) file\n");
                 ret = 1;
@@ -2172,17 +2214,21 @@ int main(int argc, char **argv)
             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);
@@ -2222,7 +2268,7 @@ int main(int argc, char **argv)
             }
             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);