Change CMakeLists.txt to build examples by default (image_to_j2k and j2k_to_image).
[openjpeg.git] / codec / j2k_to_image.c
index 182420f5e133ecff137d767f774de239c4a63b45..3b1915c74547afce9c7ed4f3235eb53d1ec56724 100644 (file)
 #include "compat/getopt.h"
 #include "convert.h"
 #include "dirent.h"
+#include "index.h"
 
 #ifndef WIN32
-#define stricmp strcasecmp
-#define strnicmp strncasecmp
+#include <strings.h>
+#define _stricmp strcasecmp
+#define _strnicmp strncasecmp
 #endif
 
 /* ----------------------------------------------------------------------- */
@@ -57,7 +59,7 @@
 #define TIF_DFMT 14
 #define RAW_DFMT 15
 #define TGA_DFMT 16
-
+#define PNG_DFMT 17
 /* ----------------------------------------------------------------------- */
 
 typedef struct dircnt{
@@ -72,7 +74,7 @@ typedef struct img_folder{
        /** The directory path of the folder containing input images*/
        char *imgdirpath;
        /** Output format*/
-       char *out_format;
+       const char *out_format;
        /** Enable option*/
        char set_imgdir;
        /** Enable Cod Format for output*/
@@ -187,15 +189,15 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
 
 int get_file_format(char *filename) {
        unsigned int i;
-       static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c" };
-       static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT };
+       static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
+       static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
        char * ext = strrchr(filename, '.');
        if (ext == NULL)
                return -1;
        ext++;
        if(ext) {
                for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
-                       if(strnicmp(ext, extension[i], 3) == 0) {
+                       if(_strnicmp(ext, extension[i], 3) == 0) {
                                return format[i];
                        }
                }
@@ -229,235 +231,6 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparamet
        return 0;
 }
 
-/* ------------------------------------------------------------------------------------ */
-
-/**
-Create an index and write it to a file
-@param cstr_info Codestream information 
-@param index Index filename
-@return Returns 0 if successful, returns 1 otherwise
-*/
-int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
-       int tileno, compno, layno, resno, precno, pack_nb, x, y;
-       FILE *stream = NULL;
-       int tilepartno;
-
-       if (!cstr_info)         
-               return 1;
-
-       stream = fopen(index, "w");
-       if (!stream) {
-               fprintf(stderr, "failed to open index file [%s] for writing\n", index);
-               return 1;
-       }
-       
-       fprintf(stream, "%d %d\n", cstr_info->image_w, cstr_info->image_h);
-       fprintf(stream, "%d\n", cstr_info->prog);
-       fprintf(stream, "%d %d\n", cstr_info->tile_x, cstr_info->tile_y);
-       fprintf(stream, "%d %d\n", cstr_info->tw, cstr_info->th);
-       fprintf(stream, "%d\n", cstr_info->numcomps);
-       fprintf(stream, "%d\n", cstr_info->numlayers);
-       fprintf(stream, "%d\n", cstr_info->numdecompos);
-       
-       for (resno = cstr_info->numdecompos; resno >= 0; resno--) {
-               fprintf(stream, "[%d,%d] ", 
-                       (1 << cstr_info->tile[0].pdx[resno]), (1 << cstr_info->tile[0].pdx[resno]));    /* based on tile 0 */
-       }
-       fprintf(stream, "\n");
-       fprintf(stream, "%d\n", cstr_info->main_head_start);
-       fprintf(stream, "%d\n", cstr_info->main_head_end);
-       fprintf(stream, "%d\n", cstr_info->codestream_size);
-       
-       fprintf(stream, "\nINFO ON TILES\n");
-       fprintf(stream, "tileno start_pos  end_hd  end_tile   nbparts\n");
-       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
-               fprintf(stream, "%4d %9d %9d %9d %9d\n",
-                       cstr_info->tile[tileno].tileno,
-                       cstr_info->tile[tileno].start_pos,
-                       cstr_info->tile[tileno].end_header,
-                       cstr_info->tile[tileno].end_pos,
-                       cstr_info->tile[tileno].num_tps);
-       }
-               
-       for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
-               int start_pos, end_ph_pos, end_pos;
-               pack_nb = 0;
-
-               fprintf(stream, "\nTILE %d DETAILS\n", tileno); 
-               fprintf(stream, "part_nb tileno  num_packs  start_pos end_tph_pos   end_pos\n");
-               for (tilepartno = 0; tilepartno < cstr_info->tile[tileno].num_tps; tilepartno++)
-                       fprintf(stream, "%4d %9d  %9d  %9d %11d %9d\n",
-                               tilepartno, tileno,
-                               cstr_info->tile[tileno].tp[tilepartno].tp_numpacks,
-                               cstr_info->tile[tileno].tp[tilepartno].tp_start_pos,
-                               cstr_info->tile[tileno].tp[tilepartno].tp_end_header,
-                               cstr_info->tile[tileno].tp[tilepartno].tp_end_pos
-                               );
-               if (cstr_info->prog == LRCP) {  /* LRCP */
-                       fprintf(stream, "LRCP\npack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos\n");
-
-                       for (layno = 0; layno < cstr_info->numlayers; layno++) {
-                               for (resno = 0; resno < cstr_info->numdecompos + 1; resno++) {
-                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
-                                               int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
-                                               for (precno = 0; precno < prec_max; precno++) {
-                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
-                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
-                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
-                                                       fprintf(stream, "%4d %6d %7d %5d %6d  %6d    %6d     %6d %7d\n",
-                                                               pack_nb, tileno, layno, resno, compno, precno, start_pos, end_ph_pos, end_pos);
-                                                       pack_nb++;
-                                               }
-                                       }
-                               }
-                       }
-               } /* LRCP */
-               else if (cstr_info->prog == RLCP) {     /* RLCP */
-
-                       fprintf(stream, "RLCP\npack_nb tileno resno layno compno precno start_pos end_ph_pos end_pos\n");
-
-                       for (resno = 0; resno < cstr_info->numdecompos + 1; resno++) {
-                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
-                                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
-                                               int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
-                                               for (precno = 0; precno < prec_max; precno++) {
-                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
-                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
-                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
-                                                       fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d   %9d %7d\n",
-                                                               pack_nb, tileno, resno, layno, compno, precno, start_pos, end_ph_pos, end_pos);
-                                                       pack_nb++;
-                                               }
-                                       }
-                               }
-                       }
-               } /* RLCP */
-               else if (cstr_info->prog == RPCL) {     /* RPCL */
-
-                       fprintf(stream, "RPCL\npack_nb tileno resno precno compno layno start_pos end_ph_pos end_pos\n"); 
-
-                       for (resno = 0; resno < cstr_info->numdecompos + 1; resno++) {
-                               /* I suppose components have same XRsiz, YRsiz */
-                               int x0 = cstr_info->tile_Ox + tileno - (int)floor((float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
-                               int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
-                               int x1 = x0 + cstr_info->tile_x;
-                               int y1 = y0 + cstr_info->tile_y;
-                               for (compno = 0; compno < cstr_info->numcomps; compno++) {
-                                       int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
-                                       for (precno = 0; precno < prec_max; precno++) {
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos - resno );
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos - resno );
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                               for(y = y0; y < y1; y++) {                                                      
-                                                       if (precno_y*pcy == y ) {
-                                                               for (x = x0; x < x1; x++) {                                                                     
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d   %9d %7d\n",
-                                                                                               pack_nb, tileno, resno, precno, compno, layno, start_pos, end_ph_pos, end_pos); 
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }/* x = x0..x1 */
-                                                       } 
-                                               }  /* y = y0..y1 */
-                                       } /* precno */
-                               } /* compno */
-                       } /* resno */
-               } /* RPCL */
-               else if (cstr_info->prog == PCRL) {     /* PCRL */
-                       /* I suppose components have same XRsiz, YRsiz */
-                       int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
-                       int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
-                       int x1 = x0 + cstr_info->tile_x;
-                       int y1 = y0 + cstr_info->tile_y;
-
-                       fprintf(stream, "PCRL\npack_nb tileno precno compno resno layno start_pos end_ph_pos end_pos\n"); 
-
-                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
-                               for (resno = 0; resno < cstr_info->numdecompos + 1; resno++) {
-                                       int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
-                                       for (precno = 0; precno < prec_max; precno++) {
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos - resno );
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos - resno );
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                               for(y = y0; y < y1; y++) {                                                      
-                                                       if (precno_y*pcy == y ) {
-                                                               for (x = x0; x < x1; x++) {                                                                     
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d\n",
-                                                                                               pack_nb, tileno, precno, compno, resno, layno, start_pos, end_ph_pos, end_pos); 
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }/* x = x0..x1 */
-                                                       } 
-                                               }  /* y = y0..y1 */
-                                       } /* precno */
-                               } /* resno */
-                       } /* compno */
-               } /* PCRL */
-               else {  /* CPRL */
-
-                       fprintf(stream, "CPRL\npack_nb tileno compno precno resno layno start_pos end_ph_pos end_pos\n"); 
-
-                       for (compno = 0; compno < cstr_info->numcomps; compno++) {
-                               /* I suppose components have same XRsiz, YRsiz */
-                               int x0 = cstr_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tw * cstr_info->tile_x;
-                               int y0 = cstr_info->tile_Ox + (int)floor( (float)tileno/(float)cstr_info->tw ) * cstr_info->tile_y;
-                               int x1 = x0 + cstr_info->tile_x;
-                               int y1 = y0 + cstr_info->tile_y;
-                               
-                               for (resno = 0; resno < cstr_info->numdecompos + 1; resno++) {
-                                       int prec_max = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
-                                       for (precno = 0; precno < prec_max; precno++) {
-                                               int pcnx = cstr_info->tile[tileno].pw[resno];
-                                               int pcx = (int) pow( 2, cstr_info->tile[tileno].pdx[resno] + cstr_info->numdecompos - resno );
-                                               int pcy = (int) pow( 2, cstr_info->tile[tileno].pdy[resno] + cstr_info->numdecompos - resno );
-                                               int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
-                                               int precno_y = (int) floor( (float)precno/(float)pcnx );
-                                               for(y = y0; y < y1; y++) {
-                                                       if (precno_y*pcy == y ) {
-                                                               for (x = x0; x < x1; x++) {
-                                                                       if (precno_x*pcx == x ) {
-                                                                               for (layno = 0; layno < cstr_info->numlayers; layno++) {
-                                                                                       start_pos = cstr_info->tile[tileno].packet[pack_nb].start_pos;
-                                                                                       end_ph_pos = cstr_info->tile[tileno].packet[pack_nb].end_ph_pos;
-                                                                                       end_pos = cstr_info->tile[tileno].packet[pack_nb].end_pos;
-                                                                                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d   %9d %7d\n",
-                                                                                               pack_nb, tileno, compno, precno, resno, layno, start_pos, end_ph_pos, end_pos); 
-                                                                                       pack_nb++; 
-                                                                               }
-                                                                       }
-                                                               }/* x = x0..x1 */
-                                                       }
-                                               } /* y = y0..y1 */
-                                       } /* precno */
-                               } /* resno */
-                       } /* compno */
-               } /* CPRL */   
-       } /* tileno */
-       
-       fclose(stream);
-
-       fprintf(stderr,"Generated index file %s\n", index);
-
-       return 0;
-}
-
-/* ------------------------------------------------------------------------------------ */
-
 /* -------------------------------------------------------------------------- */
 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
        /* parse the command line */
@@ -514,6 +287,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                        case TIF_DFMT:
                                        case RAW_DFMT:
                                        case TGA_DFMT:
+                                       case PNG_DFMT:
                                                break;
                                        default:
                                                fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
@@ -551,6 +325,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                        case TGA_DFMT:
                                                img_fol->out_format = "raw";
                                                break;
+                                       case PNG_DFMT:
+                                               img_fol->out_format = "png";
+                                               break;
                                        default:
                                                fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
                                                return 1;
@@ -776,18 +553,18 @@ int main(int argc, char **argv) {
                        dirptr->filename = (char**) malloc(num_images*sizeof(char*));
 
                        if(!dirptr->filename_buf){
-                               return 0;
+                               return 1;
                        }
                        for(i=0;i<num_images;i++){
                                dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
                        }
                }
                if(load_images(dirptr,img_fol.imgdirpath)==1){
-                       return 0;
+                       return 1;
                }
                if (num_images==0){
                        fprintf(stdout,"Folder is empty\n");
-                       return 0;
+                       return 1;
                }
        }else{
                num_images=1;
@@ -819,8 +596,6 @@ int main(int argc, char **argv) {
                fread(src, 1, file_length, fsrc);
                fclose(fsrc);
 
-
-
                /* decode the code-stream */
                /* ---------------------- */
 
@@ -842,7 +617,10 @@ int main(int argc, char **argv) {
                        cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
 
                        /* decode the stream and fill the image structure */
-                       image = opj_decode(dinfo, cio, &cstr_info);
+                       if (*indexfilename)                             // If need to extract codestream information
+                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
+                       else
+                               image = opj_decode(dinfo, cio);
                        if(!image) {
                                fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
                                opj_destroy_decompress(dinfo);
@@ -881,7 +659,10 @@ int main(int argc, char **argv) {
                        cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
 
                        /* decode the stream and fill the image structure */
-                       image = opj_decode(dinfo, cio, &cstr_info);
+                       if (*indexfilename)                             // If need to extract codestream information
+                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
+                       else
+                               image = opj_decode(dinfo, cio);                 
                        if(!image) {
                                fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
                                opj_destroy_decompress(dinfo);
@@ -920,7 +701,10 @@ int main(int argc, char **argv) {
                        cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
 
                        /* decode the stream and fill the image structure */
-                       image = opj_decode(dinfo, cio, &cstr_info);
+                       if (*indexfilename)                             // If need to extract codestream information
+                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
+                       else
+                               image = opj_decode(dinfo, cio);
                        if(!image) {
                                fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
                                opj_destroy_decompress(dinfo);
@@ -1007,6 +791,15 @@ int main(int argc, char **argv) {
                                fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
                        }
                        break;
+
+               case PNG_DFMT:                  /* PNG */
+                       if(imagetopng(image, parameters.outfile)){
+                               fprintf(stdout,"Error generating png file. Outfile %s not generated\n",parameters.outfile);
+                       }
+                       else {
+                               fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
+                       }
+                       break;
                }
 
                /* free remaining structures */
@@ -1014,7 +807,8 @@ int main(int argc, char **argv) {
                        opj_destroy_decompress(dinfo);
                }
                /* free codestream information structure */
-               opj_destroy_cstr_info(&cstr_info);
+               if (*indexfilename)     
+                       opj_destroy_cstr_info(&cstr_info);
                /* free image data structure */
                opj_image_destroy(image);
 
@@ -1024,3 +818,5 @@ int main(int argc, char **argv) {
 //end main
 
 
+
+