[trunk] WIP: enhance j2k_to_image with new get_decoded_tile functionality
authorMickael Savinaud <savmickael@users.noreply.github.com>
Thu, 17 Nov 2011 14:24:51 +0000 (14:24 +0000)
committerMickael Savinaud <savmickael@users.noreply.github.com>
Thu, 17 Nov 2011 14:24:51 +0000 (14:24 +0000)
CHANGES
applications/codec/j2k_to_image.c
libopenjpeg/j2k.c
libopenjpeg/openjpeg.h
tests/nonregression/test_suite.ctest.in

diff --git a/CHANGES b/CHANGES
index 9eed0d55d7652276434817b5ce648f6cb0f006bc..b9c829aeaa1655153bad3bba7f083c6cab5e6e91 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,7 @@ What's New for OpenJPEG
 + : added
 
 November 17, 2011
++ [mickael] WIP: enhance j2k_to_image with new get_decoded_tile functionality
 + [mickael] WIP: clean j2k_dump and enhance j2k_dump with commit 1052.
 + [mickael] WIP: add a set decoded resolution factor function and update j2k_to_image help about decoded region.
 
index cae7c1634130df9f2bc18c2d56f5715f6cbfbef3..449a71e93febc390592f6ab7390c5942f8adbecb 100644 (file)
@@ -143,7 +143,11 @@ void decode_help_display(void) {
        fprintf(stdout,"  -d <x0,y0,x1,y1>\n");
        fprintf(stdout,"    OPTIONAL\n");
        fprintf(stdout,"    Decoding area\n");
-       fprintf(stdout,"    By default all tiles header are read.\n");
+       fprintf(stdout,"    By default all the image is decoded.\n");
+       fprintf(stdout,"  -t <tile_number>\n");
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n");
+       fprintf(stdout,"    By default all tiles are decoded.\n");
        fprintf(stdout,"\n");
 /* UniPG>> */
 #ifdef USE_JPWL
@@ -271,8 +275,12 @@ static int infile_format(const char *fname)
                return -1;
 
        memset(buf, 0, 12);
-       fread(buf, 1, 12, reader);
+       unsigned int l_nb_read = fread(buf, 1, 12, reader);
        fclose(reader);
+       if (l_nb_read != 12)
+               return -1;
+
+
 
        ext_format = get_file_format(fname);
 
@@ -316,7 +324,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                {"OutFor",REQ_ARG, NULL ,'O'},
        };
 
-       const char optlist[] = "i:o:r:l:x:d:"
+       const char optlist[] = "i:o:r:l:x:d:t:"
 
 /* UniPG>> */
 #ifdef USE_JPWL
@@ -468,6 +476,16 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                                free(ROI_values);
                        }
+                       break;
+
+                       /* ----------------------------------------------------- */
+
+                       case 't':               /* Input tile index */
+                       {
+                               sscanf(opj_optarg, "%d", &parameters->tile_index);
+                               parameters->nb_tile_to_decode = 1;
+                       }
+                       break;
 
                                /* ----------------------------------------------------- */                                                             
 
@@ -785,37 +803,47 @@ int main(int argc, char **argv)
                        return EXIT_FAILURE;
                }
 
-               if (! opj_set_decode_area(      dinfo, image,
-                                                                       parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){
-                       fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n");
-                       opj_stream_destroy(cio);
-                       opj_destroy_codec(dinfo);
-                       opj_image_destroy(image);
-                       fclose(fsrc);
-                       return EXIT_FAILURE;
+               if (!parameters.nb_tile_to_decode) {
+                       // Optional if you want decode the entire image
+                       if (!opj_set_decode_area(dinfo, image, parameters.DA_x0,
+                                       parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){
+                               fprintf(stderr,
+                                               "ERROR -> j2k_to_image: failed to set the decoded area\n");
+                               opj_stream_destroy(cio);
+                               opj_destroy_codec(dinfo);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
                        }
 
-               /* Get the decoded image */
-               if ( !( opj_decode_v2(dinfo, cio, image) && opj_end_decompress(dinfo,cio) ) ) {
-                       fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
-                       opj_destroy_codec(dinfo);
-                       opj_stream_destroy(cio);
-                       opj_image_destroy(image);
-                       fclose(fsrc);
-                       return EXIT_FAILURE;
+                       /* Get the decoded image */
+                       if (!(opj_decode_v2(dinfo, cio, image) && opj_end_decompress(dinfo,
+                                       cio))) {
+                               fprintf(stderr,
+                                               "ERROR -> j2k_to_image: failed to decode image!\n");
+                               opj_destroy_codec(dinfo);
+                               opj_stream_destroy(cio);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
+                       }
+               }
+               else {
+                       if (!opj_get_decoded_tile(dinfo, cio, image, parameters.tile_index)) {
+                               fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile!\n");
+                               opj_destroy_codec(dinfo);
+                               opj_stream_destroy(cio);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
+                       }
+                       fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
                }
-
-               /*opj_dump_codec(dinfo, OPJ_J2K_MH_IND, stdout );
-
-               cstr_index = opj_get_cstr_index(dinfo);*/
-
-               fprintf(stderr, "image is decoded!\n");
 
                /* Close the byte stream */
                opj_stream_destroy(cio);
                fclose(fsrc);
 
-
                if(image->color_space == CLRSPC_SYCC){
                        color_sycc_to_rgb(image); /* FIXME */
                }
index c41ada7f1b2d66c73470e0a5bee015a558135bdb..0de530f96f947875164736b0cc0481212dfc1962 100644 (file)
@@ -8371,6 +8371,12 @@ opj_bool j2k_get_tile(   opj_j2k_v2_t *p_j2k,
                return OPJ_FALSE;
        }
 
+       if (tile_index >= p_j2k->m_cp.th * p_j2k->m_cp.tw) {
+               opj_event_msg_v2(p_manager, EVT_ERROR, "Decoded tile index is "
+                               "inconsistent with the number of tiles in the codestream (%d vs %d).\n", tile_index, p_j2k->m_cp.th * p_j2k->m_cp.tw );
+               return OPJ_FALSE;
+       }
+
        /* Compute the dimension of the desired tile*/
        l_tile_x = tile_index % p_j2k->m_cp.tw;
        l_tile_y = tile_index / p_j2k->m_cp.tw;
index a1f3b83a5219ce306068a81796d43aa70c0a41cd..d71b42868e35cf37cf74b2f6943ce0bd9525ac62 100644 (file)
@@ -439,6 +439,11 @@ typedef struct opj_dparameters {
        /** Verbose mode */
        opj_bool m_verbose;
 
+       /** tile number ot the decoded tile*/
+       OPJ_UINT32 tile_index;
+       /** Nb of tile to decode */
+       OPJ_UINT32 nb_tile_to_decode;
+
        /*@}*/
 
 /* UniPG>> */
index d969ef5c31e1dca24de6800d62af08a6a6ecd57d..2fd4daf4171976633fff9642fea55ce5900eaecc 100644 (file)
@@ -96,6 +96,11 @@ j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_4.j2k.png -d 3,
 j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_5.j2k.png -d 4,4,7,7 -r 1\r
 j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_6.j2k.png -d 4,4,5,5 -r 1\r
 \r
+j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06.j2k_0.png -t 0\r
+j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_1.j2k_5.png -t 5\r
+j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_2.j2k_9.png -t 9\r
+j2k_to_image -i @INPUT_CONF_PATH@/p1_06.j2k -o @TEMP_PATH@/p1_06_3.j2k_15.png -t 15\r
+\r
 # prec=4; nb_c=3 ; signd=yes\r
 j2k_to_image -i @INPUT_CONF_PATH@/p0_04.j2k -o @TEMP_PATH@/p0_04.j2k.png -d 0,0,256,256\r
 j2k_to_image -i @INPUT_CONF_PATH@/p0_04.j2k -o @TEMP_PATH@/p0_04_1.j2k.png -d 128,0,256,128\r