[trunk] Remove warning about sign conversion.
[openjpeg.git] / src / bin / jp2 / opj_compress.c
index e62a0fad4e0b3987db952cedc4880d0c42c24951..ea9415104f19c475bfe87faeabc352ce6187a47f 100644 (file)
 
 #include "format_defs.h"
 
-#define CINEMA_24_CS 1302083   /*Codestream length for 24fps*/
-#define CINEMA_48_CS 651041            /*Codestream length for 48fps*/
-#define COMP_24_CS 1041666             /*Maximum size per color component for 2K & 4K @ 24fps*/
-#define COMP_48_CS 520833              /*Maximum size per color component for 2K @ 48fps*/
-
 typedef struct dircnt{
     /** Buffer for holding images read from Directory*/
     char *filename_buf;
@@ -82,8 +77,6 @@ typedef struct img_folder{
     char set_imgdir;
     /** Enable Cod Format for output*/
     char set_out_format;
-    /** User specified rate stored in case of cinema option*/
-    float *rates;
 }img_fol_t;
 
 static void encode_help_display(void) {
@@ -140,7 +133,7 @@ static void encode_help_display(void) {
     fprintf(stdout,"-OutFor \n");
     fprintf(stdout,"    REQUIRED only if -ImgDir is used\n");
     fprintf(stdout,"     Need to specify only format without filename <BMP>  \n");
-    fprintf(stdout,"    Currently accepts PBM, PGM, PPM, PNM, PAM, PGX, PNG, BMP, TIF, RAW, RAWL and TGA formats\n");
+    fprintf(stdout,"    Currently accepts PBM, PGM, PPM, PNM, PAM, PGX, PNG, BMP, TIF, RAW (MSB), RAWL (LSB) and TGA formats\n");
     fprintf(stdout,"\n");
     fprintf(stdout,"-i           : source file  (-i source.pnm also *pbm, *.pgm, *.ppm, *.pam, *.pgx, *png, *.bmp, *.tif, *.raw, *.tga) \n");
     fprintf(stdout,"    When using this option -o must be used\n");
@@ -197,7 +190,7 @@ static void encode_help_display(void) {
     fprintf(stdout,"                 Indicate multiple modes by adding their values. \n");
     fprintf(stdout,"                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
     fprintf(stdout,"\n");
-    fprintf(stdout,"-TP          : devide packets of every tile into tile-parts (-TP R) [R, L, C]\n");
+    fprintf(stdout,"-TP          : divide packets of every tile into tile-parts (-TP R) [R, L, C]\n");
     fprintf(stdout,"\n");
     fprintf(stdout,"-x           : create an index file *.Idx (-x index_name.Idx) \n");
     fprintf(stdout,"\n");
@@ -446,163 +439,12 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c
     return 0;
 }
 
-static int initialise_4K_poc(opj_poc_t *POC, int numres){
-    POC[0].tile  = 1;
-    POC[0].resno0  = 0;
-    POC[0].compno0 = 0;
-    POC[0].layno1  = 1;
-    POC[0].resno1  = numres-1;
-    POC[0].compno1 = 3;
-    POC[0].prg1 = OPJ_CPRL;
-    POC[1].tile  = 1;
-    POC[1].resno0  = numres-1;
-    POC[1].compno0 = 0;
-    POC[1].layno1  = 1;
-    POC[1].resno1  = numres;
-    POC[1].compno1 = 3;
-    POC[1].prg1 = OPJ_CPRL;
-    return 2;
-}
-
-static void set_cinema_parameters(opj_cparameters_t *parameters){
-
-    /* No tiling */
-    parameters->tile_size_on = OPJ_FALSE;
-    parameters->cp_tdx=1;
-    parameters->cp_tdy=1;
-
-    /* One tile part for each component */
-    parameters->tp_flag = 'C';
-    parameters->tp_on = 1;
-
-    /* Tile and Image shall be at (0,0) */
-    parameters->cp_tx0 = 0;
-    parameters->cp_ty0 = 0;
-    parameters->image_offset_x0 = 0;
-    parameters->image_offset_y0 = 0;
-
-    /* Codeblock size= 32*32 */
-    parameters->cblockw_init = 32;
-    parameters->cblockh_init = 32;
-
-    /* Use of precincts */
-    parameters->csty |= 0x01;
-
-    /* The progression order shall be CPRL */
-    parameters->prog_order = OPJ_CPRL;
-
-    /* No ROI */
-    parameters->roi_compno = -1;
-
-    /* No subsampling */
-    parameters->subsampling_dx = 1;
-    parameters->subsampling_dy = 1;
-
-    /* 9-7 transform */
-    parameters->irreversible = 1;
-
-}
-
-static void setup_cinema_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){
-    int i;
-    float temp_rate;
-
-    /* Size and resolution levels */
-    switch (parameters->cp_cinema){
-    case OPJ_CINEMA2K_24:
-    case OPJ_CINEMA2K_48:
-        if(parameters->numresolution > 6){
-            fprintf(stdout,"JPEG 2000 Profile-3 (2k dc profile) requires:\n"
-                    "Number of decomposition levels <= 5\n"
-                    "-> Number of decomposition levels forced to 5");
-            parameters->numresolution = 6;
-        }
-        if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))){
-            fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\n"
-                    "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
-                    "at least one of coordinates match 2048 x 1080\n",
-                    image->comps[0].w,image->comps[0].h);
-            parameters->cp_rsiz = OPJ_STD_RSIZ;
-        }
-        break;
-    case OPJ_CINEMA4K_24:
-        if(parameters->numresolution < 1){
-            fprintf(stdout,"JPEG 2000 Profile-4 (4k dc profile) requires:\n"
-                    "Number of decomposition levels >= 1 && <= 6\n"
-                    "-> Number of decomposition levels forced to 1");
-            parameters->numresolution = 1;
-        }else if(parameters->numresolution > 7){
-            fprintf(stdout,"JPEG 2000 Profile-4 (4k dc profile) requires:\n"
-                    "Number of decomposition levels >= 1 && <= 6\n"
-                    "-> Number of decomposition levels forced to 6");
-            parameters->numresolution = 7;
-        }
-        if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))){
-            fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\n"
-                    "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
-                    "at least one of coordinates match 4096 x 2160\n",
-                    image->comps[0].w,image->comps[0].h);
-            parameters->cp_rsiz = OPJ_STD_RSIZ;
-        }
-        parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
-        break;
-    default :
-        break;
-    }
-
-    /* Limited bit-rate */
-    switch (parameters->cp_cinema){
-    case OPJ_CINEMA2K_24:
-    case OPJ_CINEMA4K_24:
-        for(i=0 ; i<parameters->tcp_numlayers ; i++){
-            temp_rate = 0 ;
-            if (img_fol->rates[i]== 0){
-                parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                        (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-            }else{
-                temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                        (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
-                if (temp_rate > CINEMA_24_CS ){
-                    parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                            (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                }else{
-                    parameters->tcp_rates[i]= img_fol->rates[i];
-                }
-            }
-        }
-        parameters->max_comp_size = COMP_24_CS;
-        break;
-
-    case OPJ_CINEMA2K_48:
-        for(i=0 ; i<parameters->tcp_numlayers ; i++){
-            temp_rate = 0 ;
-            if (img_fol->rates[i]== 0){
-                parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                        (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-            }else{
-                temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                        (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy);
-                if (temp_rate > CINEMA_48_CS ){
-                    parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
-                            (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
-                }else{
-                    parameters->tcp_rates[i]= img_fol->rates[i];
-                }
-            }
-        }
-        parameters->max_comp_size = COMP_48_CS;
-        break;
-    default:
-        break;
-    }
-    parameters->cp_disto_alloc = 1;
-}
-
 /* ------------------------------------------------------------------------------------ */
 
 static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
                                  img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename) {
-    int i, j, totlen, c;
+    OPJ_UINT32 i, j;
+    int totlen, c;
     opj_option_t long_option[]={
         {"cinema2K",REQ_ARG, NULL ,'w'},
         {"cinema4K",NO_ARG, NULL ,'y'},
@@ -775,7 +617,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
         case 'f':                      /* mod fixed_quality (before : -q) */
         {
             int *row = NULL, *col = NULL;
-            int numlayers = 0, numresolution = 0, matrix_width = 0;
+            OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
 
             char *s = opj_optarg;
             sscanf(s, "%d", &numlayers);
@@ -841,10 +683,17 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
             int res_spec = 0;
 
             char *s = opj_optarg;
+            int ret;
             do {
                 sep = 0;
-                sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec],
+                ret = sscanf(s, "[%d,%d]%c", &parameters->prcw_init[res_spec],
                        &parameters->prch_init[res_spec], &sep);
+                if( !(ret == 2 && sep == 0) && !(ret == 3 && sep == ',') )
+                  {
+                  fprintf(stderr,"\nError: could not parse precinct dimension: '%s' %x\n", s, sep);
+                  fprintf(stderr,"Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
+                  return 1;
+                  }
                 parameters->csty |= 0x01;
                 res_spec++;
                 s = strpbrk(s, "]") + 2;
@@ -1063,8 +912,8 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
                 fprintf(stderr,"Incorrect value!! must be 24 or 48\n");
                 return 1;
             }
-            fprintf(stdout,"CINEMA 2K compliant codestream\n");
-            parameters->cp_rsiz = OPJ_CINEMA2K;
+            fprintf(stdout,"CINEMA 2K profile activated\n"
+                    "Other options specified could be overriden\n");
 
         }
             break;
@@ -1074,8 +923,8 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
         case 'y':                      /* Digital Cinema 4K profile compliance*/
         {
             parameters->cp_cinema = OPJ_CINEMA4K_24;
-            fprintf(stdout,"CINEMA 4K compliant codestream\n");
-            parameters->cp_rsiz = OPJ_CINEMA4K;
+            fprintf(stdout,"CINEMA 4K profile activated\n"
+                    "Other options specified could be overriden\n");
         }
             break;
 
@@ -1088,7 +937,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
             float *lCurrentDoublePtr;
             float *lSpace;
             int *l_int_ptr;
-            int lNbComp = 0, lTotalComp, lMctComp, i, lStrLen;
+            int lNbComp = 0, lTotalComp, lMctComp, i; long int lStrLen, lStrFread;
 
             /* Open file */
             FILE * lFile = fopen(lFilename,"r");
@@ -1101,8 +950,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
             lStrLen = ftell(lFile);
             fseek(lFile,0,SEEK_SET);
             lMatrix = (char *) malloc(lStrLen + 1);
-            fread(lMatrix, lStrLen, 1, lFile);
+            lStrFread = fread(lMatrix, 1, lStrLen, lFile);
             fclose(lFile);
+            if( lStrLen != lStrFread ) return 1;
 
             lMatrix[lStrLen] = 0;
             lCurrentPtr = lMatrix;
@@ -1483,13 +1333,6 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
         }
     }while(c != -1);
 
-    /* check for possible errors */
-    if (parameters->cp_cinema){
-        if(parameters->tcp_numlayers > 1){
-            parameters->cp_rsiz = OPJ_STD_RSIZ;
-            fprintf(stdout,"Warning: DC profiles do not allow more than one quality layer. The codestream created will not be compliant with the DC profile\n");
-        }
-    }
     if(img_fol->set_imgdir == 1){
         if(!(parameters->infile[0] == 0)){
             fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
@@ -1556,28 +1399,6 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
 
 /* -------------------------------------------------------------------------- */
 
-/**
-sample error callback expecting a FILE* client object
-*/
-static void error_file_callback(const char *msg, void *client_data) {
-    FILE *stream = (FILE*)client_data;
-    fprintf(stream, "[ERROR] %s", msg);
-}
-/**
-sample warning callback expecting a FILE* client object
-*/
-static void warning_file_callback(const char *msg, void *client_data) {
-    FILE *stream = (FILE*)client_data;
-    fprintf(stream, "[WARNING] %s", msg);
-}
-/**
-sample debug callback expecting a FILE* client object
-*/
-static void info_file_callback(const char *msg, void *client_data) {
-    FILE *stream = (FILE*)client_data;
-    fprintf(stream, "[INFO] %s", msg);
-}
-
 /**
 sample error debug callback expecting no client object
 */
@@ -1636,14 +1457,6 @@ int main(int argc, char **argv) {
         return 1;
     }
 
-    if (parameters.cp_cinema){
-        img_fol.rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float));
-        for(i=0; i< parameters.tcp_numlayers; i++){
-            img_fol.rates[i] = parameters.tcp_rates[i];
-        }
-        set_cinema_parameters(&parameters);
-    }
-
     /* Create comment for codestream */
     if(parameters.cp_comment == NULL) {
         const char comment[] = "Created by OpenJPEG version ";
@@ -1801,10 +1614,6 @@ int main(int argc, char **argv) {
         /* Decide if MCT should be used */
         parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
 
-        if(parameters.cp_cinema){
-            setup_cinema_encoder(&parameters,image,&img_fol);
-        }
-
         /* encode the destination image */
         /* ---------------------------- */
 
@@ -1903,7 +1712,6 @@ int main(int argc, char **argv) {
     /* free user parameters structure */
     if(parameters.cp_comment)   free(parameters.cp_comment);
     if(parameters.cp_matrice)   free(parameters.cp_matrice);
-    if(parameters.cp_cinema)    free(img_fol.rates);
 
     return 0;
 }