[trunk] move to the new API for function opj_stream_create_default_file_stream. Use...
[openjpeg.git] / src / bin / jp2 / opj_compress.c
index d5736c1ed764657ada4a9e5145ad17a44a1ce8f4..24217aa3de7af8d4d1a96947697c4ab88dbac98c 100644 (file)
@@ -7,6 +7,8 @@
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2006-2007, Parvatha Elangovan
  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
+ * Copyright (c) 2012, CS Systemes d'Information, France
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -149,7 +151,7 @@ static void encode_help_display(void) {
        fprintf(stdout,"\n");
        fprintf(stdout,"-h           : display the help information \n ");
        fprintf(stdout,"\n");
-       fprintf(stdout,"-cinema2K    : Digital Cinema 2K profile compliant codestream for 2K resolution.(-cinema2k 24 or 48) \n");
+       fprintf(stdout,"-cinema2K    : Digital Cinema 2K profile compliant codestream for 2K resolution.(-CINEMA2K 24 or 48) \n");
   fprintf(stdout,"       Need to specify the frames per second for a 2K resolution. Only 24 or 48 fps is allowed\n");
        fprintf(stdout,"\n");
        fprintf(stdout,"-cinema4K    : Digital Cinema 4K profile compliant codestream for 4K resolution \n");
@@ -327,22 +329,22 @@ static void encode_help_display(void) {
 
 static OPJ_PROG_ORDER give_progression(const char progression[4]) {
        if(strncmp(progression, "LRCP", 4) == 0) {
-               return LRCP;
+               return OPJ_LRCP;
        }
        if(strncmp(progression, "RLCP", 4) == 0) {
-               return RLCP;
+               return OPJ_RLCP;
        }
        if(strncmp(progression, "RPCL", 4) == 0) {
-               return RPCL;
+               return OPJ_RPCL;
        }
        if(strncmp(progression, "PCRL", 4) == 0) {
-               return PCRL;
+               return OPJ_PCRL;
        }
        if(strncmp(progression, "CPRL", 4) == 0) {
-               return CPRL;
+               return OPJ_CPRL;
        }
 
-       return PROG_UNKNOWN;
+       return OPJ_PROG_UNKNOWN;
 }
 
 static unsigned int get_num_images(char *imgdirpath){
@@ -451,14 +453,14 @@ static int initialise_4K_poc(opj_poc_t *POC, int numres){
        POC[0].layno1  = 1;
        POC[0].resno1  = numres-1;
        POC[0].compno1 = 3;
-       POC[0].prg1 = CPRL;
+       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 = CPRL;
+       POC[1].prg1 = OPJ_CPRL;
        return 2;
 }
 
@@ -483,7 +485,7 @@ static void cinema_parameters(opj_cparameters_t *parameters){
        parameters->csty |= 0x01;
 
        /*The progression order shall be CPRL*/
-       parameters->prog_order = CPRL;
+       parameters->prog_order = OPJ_CPRL;
 
        /* No ROI */
        parameters->roi_compno = -1;
@@ -500,8 +502,8 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *imag
        float temp_rate;
 
        switch (parameters->cp_cinema){
-       case CINEMA2K_24:
-       case CINEMA2K_48:
+       case OPJ_CINEMA2K_24:
+       case OPJ_CINEMA2K_48:
                if(parameters->numresolution > 6){
                        parameters->numresolution = 6;
                }
@@ -509,11 +511,11 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *imag
                        fprintf(stdout,"Image coordinates %d x %d is not 2K compliant.\nJPEG Digital Cinema Profile-3 "
                                "(2K profile) compliance requires that at least one of coordinates match 2048 x 1080\n",
                                image->comps[0].w,image->comps[0].h);
-                       parameters->cp_rsiz = STD_RSIZ;
+                       parameters->cp_rsiz = OPJ_STD_RSIZ;
                }
        break;
 
-       case CINEMA4K_24:
+       case OPJ_CINEMA4K_24:
                if(parameters->numresolution < 1){
                                parameters->numresolution = 1;
                        }else if(parameters->numresolution > 7){
@@ -523,7 +525,7 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *imag
                        fprintf(stdout,"Image coordinates %d x %d is not 4K compliant.\nJPEG Digital Cinema Profile-4"
                                "(4K profile) compliance requires that at least one of coordinates match 4096 x 2160\n",
                                image->comps[0].w,image->comps[0].h);
-                       parameters->cp_rsiz = STD_RSIZ;
+                       parameters->cp_rsiz = OPJ_STD_RSIZ;
                }
                parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution);
                break;
@@ -532,8 +534,8 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *imag
        }
 
        switch (parameters->cp_cinema){
-               case CINEMA2K_24:
-               case CINEMA4K_24:
+               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){
@@ -553,7 +555,7 @@ static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *imag
                        parameters->max_comp_size = COMP_24_CS;
                        break;
 
-               case CINEMA2K_48:
+               case OPJ_CINEMA2K_48:
                        for(i=0 ; i<parameters->tcp_numlayers ; i++){
                                temp_rate = 0 ;
                                if (img_fol->rates[i]== 0){
@@ -1036,15 +1038,15 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
                                int fps=0;
                                sscanf(opj_optarg,"%d",&fps);
                                if(fps == 24){
-                                       parameters->cp_cinema = CINEMA2K_24;
+                                       parameters->cp_cinema = OPJ_CINEMA2K_24;
                                }else if(fps == 48 ){
-                                       parameters->cp_cinema = CINEMA2K_48;
+                                       parameters->cp_cinema = OPJ_CINEMA2K_48;
                                }else {
                                        fprintf(stderr,"Incorrect value!! must be 24 or 48\n");
                                        return 1;
                                }
                                fprintf(stdout,"CINEMA 2K compliant codestream\n");
-                               parameters->cp_rsiz = CINEMA2K;
+                               parameters->cp_rsiz = OPJ_CINEMA2K;
 
                        }
                        break;
@@ -1053,9 +1055,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
 
                        case 'y':                       /* Digital Cinema 4K profile compliance*/
                        {
-                               parameters->cp_cinema = CINEMA4K_24;
+                               parameters->cp_cinema = OPJ_CINEMA4K_24;
                                fprintf(stdout,"CINEMA 4K compliant codestream\n");
-                               parameters->cp_rsiz = CINEMA4K;
+                               parameters->cp_rsiz = OPJ_CINEMA4K;
                        }
                        break;
 
@@ -1466,7 +1468,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
        /* check for possible errors */
        if (parameters->cp_cinema){
                if(parameters->tcp_numlayers > 1){
-                       parameters->cp_rsiz = STD_RSIZ;
+                       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");
                }
        }
@@ -1586,7 +1588,6 @@ static void info_callback(const char *msg, void *client_data) {
  */
 /* -------------------------------------------------------------------------- */
 int main(int argc, char **argv) {
-       FILE *fout = NULL;
 
        opj_cparameters_t parameters;   /* compression parameters */
 
@@ -1601,8 +1602,8 @@ int main(int argc, char **argv) {
        img_fol_t img_fol;
        dircnt_t *dirptr = NULL;
 
-       opj_bool bSuccess;
-  opj_bool bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
+       OPJ_BOOL bSuccess;
+  OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
        OPJ_UINT32 l_nb_tiles = 4;
 
        /* set encoding parameters to default values */
@@ -1793,18 +1794,18 @@ int main(int argc, char **argv) {
                        case J2K_CFMT:  /* JPEG-2000 codestream */
                        {
                                /* Get a decoder handle */
-                               l_codec = opj_create_compress(CODEC_J2K);
+                               l_codec = opj_create_compress(OPJ_CODEC_J2K);
                                break;
                        }
                        case JP2_CFMT:  /* JPEG 2000 compressed image data */
                        {
                                /* Get a decoder handle */
-                               l_codec = opj_create_compress(CODEC_JP2);
+                               l_codec = opj_create_compress(OPJ_CODEC_JP2);
                                break;
                        }
                        default:
                                fprintf(stderr, "skipping file..\n");
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                continue;
                }
                
@@ -1822,16 +1823,8 @@ int main(int argc, char **argv) {
     }
                opj_setup_encoder(l_codec, &parameters, image);
 
-               /* Open the output file*/
-               fout = fopen(parameters.outfile, "wb");
-               if (! fout) {
-                       fprintf(stderr, "Not enable to create output file!\n");
-                       opj_stream_destroy(l_stream);
-                       return 1;
-               }
-
                /* open a byte stream for writing and allocate memory for all tiles */
-               l_stream = opj_stream_create_default_file_stream(fout,OPJ_FALSE);
+               l_stream = opj_stream_create_default_file_stream_v3(parameters.outfile,OPJ_FALSE);
                if (! l_stream){
                        return 1;
                }
@@ -1850,8 +1843,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",i);
-          opj_stream_destroy(l_stream);
-          fclose(fout);
+          opj_stream_destroy_v3(l_stream);
           opj_destroy_codec(l_codec);
           opj_image_destroy(image);
           return 1;
@@ -1871,8 +1863,7 @@ int main(int argc, char **argv) {
                }
 
                if (!bSuccess)  {
-                       opj_stream_destroy(l_stream);
-                       fclose(fout);
+                       opj_stream_destroy_v3(l_stream);
       opj_destroy_codec(l_codec);
       opj_image_destroy(image);
                        fprintf(stderr, "failed to encode image\n");
@@ -1881,8 +1872,7 @@ int main(int argc, char **argv) {
 
                fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
                /* close and free the byte stream */
-               opj_stream_destroy(l_stream);
-               fclose(fout);
+               opj_stream_destroy_v3(l_stream);
 
                /* free remaining compression structures */
                opj_destroy_codec(l_codec);