remove deprecated v1 style function j2k_read_sot
[openjpeg.git] / tests / test_tile_decoder.c
index c2102f27404b1a11f664659e4ec1cf898174e192..84293586a1c21930372bf9a28e5f484a6f6a306f 100644 (file)
 #endif
 
 #include "opj_config.h"
+#include <stdlib.h>
+
+#ifdef _WIN32\r
+#include <windows.h>\r
+#define strcasecmp _stricmp\r
+#define strncasecmp _strnicmp\r
+#else\r
+#include <strings.h>\r
+#endif /* _WIN32 */
+
 #include "openjpeg.h"
-#include "stdlib.h"
+#include "format_defs.h"
+
+
+/* -------------------------------------------------------------------------- */\r
+/* Declarations                                                               */ 
+int get_file_format(const char *filename);\r
+static int infile_format(const char *fname);
+
+/* -------------------------------------------------------------------------- */\r
+int get_file_format(const char *filename) {\r
+       unsigned int i;\r
+       static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };\r
+       static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };\r
+       char * ext = strrchr(filename, '.');\r
+       if (ext == NULL)\r
+               return -1;\r
+       ext++;\r
+       if(ext) {\r
+               for(i = 0; i < sizeof(format)/sizeof(*format); i++) {\r
+                       if(strcasecmp(ext, extension[i]) == 0) {\r
+                               return format[i];\r
+                       }\r
+               }\r
+       }\r
+\r
+       return -1;\r
+}
+
+/* -------------------------------------------------------------------------- */\r
+#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"\r
+#define JP2_MAGIC "\x0d\x0a\x87\x0a"\r
+/* position 45: "\xff\x52" */\r
+#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"\r
+\r
+static int infile_format(const char *fname)\r
+{\r
+       FILE *reader;\r
+       const char *s, *magic_s;\r
+       int ext_format, magic_format;\r
+       unsigned char buf[12];\r
+       unsigned int l_nb_read;\r
+\r
+       reader = fopen(fname, "rb");\r
+\r
+       if (reader == NULL)\r
+               return -1;\r
+\r
+       memset(buf, 0, 12);\r
+       l_nb_read = fread(buf, 1, 12, reader);\r
+       fclose(reader);\r
+       if (l_nb_read != 12)\r
+               return -1;\r
+\r
+       ext_format = get_file_format(fname);\r
+\r
+       if (ext_format == JPT_CFMT)\r
+               return JPT_CFMT;\r
+\r
+       if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {\r
+               magic_format = JP2_CFMT;\r
+               magic_s = ".jp2";\r
+       }\r
+       else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {\r
+               magic_format = J2K_CFMT;\r
+               magic_s = ".j2k or .jpc or .j2c";\r
+       }\r
+       else\r
+               return -1;\r
+\r
+       if (magic_format == ext_format)\r
+               return ext_format;\r
+\r
+       s = fname + strlen(fname) - 4;\r
+\r
+       fputs("\n===========================================\n", stderr);\r
+       fprintf(stderr, "The extension of this file is incorrect.\n"\r
+                                       "FOUND %s. SHOULD BE %s\n", s, magic_s);\r
+       fputs("===========================================\n", stderr);\r
+\r
+       return magic_format;\r
+}
 
-#define DA_X0 0
-#define DA_Y0 0
-#define DA_X1 1000
-#define DA_Y1 1000
-#define INPUT_FILE                     "test.j2k"
 
 /* -------------------------------------------------------------------------- */
 
@@ -89,7 +174,7 @@ void info_callback(const char *msg, void *client_data) {
 
 /* -------------------------------------------------------------------------- */
 
-int main ()
+int main (int argc, char *argv[])
 {
        opj_dparameters_t l_param;
        opj_codec_t * l_codec;
@@ -101,20 +186,61 @@ int main ()
        OPJ_UINT32 l_tile_index;
        OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
        opj_bool l_go_on = OPJ_TRUE;
-       OPJ_INT32 l_tile_x0,l_tile_y0;
-       OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
+       OPJ_INT32 l_tile_x0=0, l_tile_y0=0 ;
+       OPJ_UINT32 l_tile_width=0, l_tile_height=0, l_nb_tiles_x=0, l_nb_tiles_y=0, l_nb_comps=0 ;
        OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
+
+    int da_x0=0;
+    int da_y0=0;
+    int da_x1=1000;
+    int da_y1=1000;
+    char input_file[64];
        
-       //PROFINIT();
+    /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */
+       if( argc == 6 )
+    {
+           da_x0=atoi(argv[1]);
+           da_y0=atoi(argv[2]);
+        da_x1=atoi(argv[3]);
+        da_y1=atoi(argv[4]);
+        strcpy(input_file,argv[5]);
 
+    }
+    else
+    {
+        da_x0=0;
+        da_y0=0;
+        da_x1=1000;
+        da_y1=1000;
+        strcpy(input_file,"test.j2k");
+    }
 
-       if
-               (! l_data)
+       if (! l_data) {
+        return EXIT_FAILURE;
+       }
+
+       l_file = fopen(input_file,"rb");
+       if (! l_file)
        {
-               return 1;
+           fprintf(stdout, "ERROR while opening input file\n");
+           free(l_data);
+               return EXIT_FAILURE;
        }
+
+    l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);
+    if (!l_stream){\r
+           fclose(l_file);\r
+        free(l_data);\r
+           fprintf(stderr, "ERROR -> failed to create the stream from the file\n");\r
+               return EXIT_FAILURE;\r
+       }
+
+    /* Set the default decoding parameters */
        opj_set_default_decoder_parameters(&l_param);
 
+    /* */
+    l_param.decod_format = infile_format(input_file);
+
        /** you may here add custom decoding parameters */
        /* do not use layer decoding limitations */
        l_param.cp_layer = 0;
@@ -125,123 +251,137 @@ int main ()
        /* to decode only a part of the image data */
        //opj_restrict_decoding(&l_param,0,0,1000,1000);
        
-       l_codec = opj_create_decompress_v2(CODEC_J2K);
-       if
-               (! l_codec)
-       {
-               free(l_data);
-               return 1;
-       }
+
+    switch(l_param.decod_format) {\r
+        case J2K_CFMT: /* JPEG-2000 codestream */\r
+        {\r
+            /* Get a decoder handle */\r
+            l_codec = opj_create_decompress(CODEC_J2K);\r
+            break;\r
+        }\r
+        case JP2_CFMT: /* JPEG 2000 compressed image data */\r
+        {\r
+            /* Get a decoder handle */\r
+            l_codec = opj_create_decompress(CODEC_JP2);\r
+            break;\r
+        }\r
+        default:\r
+        {    \r
+            fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n");\r
+            fclose(l_file);\r
+            free(l_data);\r
+            opj_stream_destroy(l_stream);\r
+            return EXIT_FAILURE;\r
+        }\r
+    }
 
        /* catch events using our callbacks and give a local context */         
        opj_set_info_handler(l_codec, info_callback,00);
        opj_set_warning_handler(l_codec, warning_callback,00);
        opj_set_error_handler(l_codec, error_callback,00);
        
-       if
-               (! opj_setup_decoder_v2(l_codec,&l_param))
+    /* Setup the decoder decoding parameters using user parameters */
+       if (! opj_setup_decoder(l_codec, &l_param))
        {
-               free(l_data);
-               opj_destroy_codec(l_codec);
-               return 1;
+        fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
+        fclose(l_file);
+        free(l_data);
+        opj_stream_destroy(l_stream);
+        opj_destroy_codec(l_codec);
+        return EXIT_FAILURE;
        }
        
-       l_file = fopen(INPUT_FILE,"rb");
-       if
-               (! l_file)
-       {
-               fprintf(stdout, "Error opening input file\n");
-               free(l_data);
+    /* Read the main header of the codestream and if necessary the JP2 boxes*/
+       if (! opj_read_header(l_stream, l_codec, &l_image))
+    {
+        fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
+               fclose(l_file);
+        free(l_data);
+               opj_stream_destroy(l_stream);
                opj_destroy_codec(l_codec);
-               return 1;
+               return EXIT_FAILURE;
        }
 
-       l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);
+    if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){\r
+        fprintf(stderr,        "ERROR -> j2k_to_image: failed to set the decoded area\n");\r
+        fclose(l_file);
+        free(l_data);\r
+        opj_stream_destroy(l_stream);\r
+        opj_destroy_codec(l_codec);\r
+        opj_image_destroy(l_image);\r
+        return EXIT_FAILURE;\r
+    }\r
 
-       if
-               (! opj_read_header(l_stream, l_codec, &l_image))
-       {
-               free(l_data);
-               opj_stream_destroy(l_stream);
-               fclose(l_file);
-               opj_destroy_codec(l_codec);
-               return 1;
-       }
-       printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
-       opj_set_decode_area(l_codec, l_image, DA_X0, DA_Y0, DA_X1, DA_Y1);
-       while
-               (l_go_on)
+
+       while (l_go_on)
        {
-               if
-                       (! opj_read_tile_header(
-                                               l_codec,
-                                               &l_tile_index,
-                                               &l_data_size,
-                                               &l_current_tile_x0,
-                                               &l_current_tile_y0,
-                                               &l_current_tile_x1,
-                                               &l_current_tile_y1,
-                                               &l_nb_comps,
-                                               &l_go_on,
-                                               l_stream))
-               {
-                       free(l_data);
-                       opj_stream_destroy(l_stream);
-                       fclose(l_file);
-                       opj_destroy_codec(l_codec);
-                       opj_image_destroy(l_image);
-                       return 1;
+               if (! opj_read_tile_header( l_codec,
+                                    l_stream,
+                                    &l_tile_index,
+                                    &l_data_size,
+                                    &l_current_tile_x0,
+                                    &l_current_tile_y0,
+                                    &l_current_tile_x1,
+                                    &l_current_tile_y1,
+                                    &l_nb_comps,
+                                    &l_go_on))
+        {
+            fclose(l_file);
+            free(l_data);
+            opj_stream_destroy(l_stream);
+            opj_destroy_codec(l_codec);
+            opj_image_destroy(l_image);
+            return EXIT_FAILURE;
                }
-               if
-                       (l_go_on)
-               {
-                       if
-                               (l_data_size > l_max_data_size)
-                       {
+
+               if (l_go_on)
+        {
+                       if (l_data_size > l_max_data_size)
+            {
                                l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
-                               if
-                                       (! l_data)
-                               {
-                                       opj_stream_destroy(l_stream);
-                                       fclose(l_file);
+                               if (! l_data)
+                {
+                    fclose(l_file);
+                                   opj_stream_destroy(l_stream);
                                        opj_destroy_codec(l_codec);
                                        opj_image_destroy(l_image);
-                                       return 1;
+                                       return EXIT_FAILURE;
                                }
                                l_max_data_size = l_data_size;
                        }
 
-                       if
-                               (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
+                       if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
                        {
+                fclose(l_file);
                                free(l_data);
                                opj_stream_destroy(l_stream);
-                               fclose(l_file);
                                opj_destroy_codec(l_codec);
                                opj_image_destroy(l_image);
-                               return 1;
+                               return EXIT_FAILURE;
                        }
                        /** now should inspect image to know the reduction factor and then how to behave with data */
                }
        }
-       if
-               (! opj_end_decompress(l_codec,l_stream))
-       {
+
+       if (! opj_end_decompress(l_codec,l_stream))
+    {
+        fclose(l_file);
                free(l_data);
                opj_stream_destroy(l_stream);
-               fclose(l_file);
                opj_destroy_codec(l_codec);
                opj_image_destroy(l_image);
-               return 1;
+               return EXIT_FAILURE;
        }
+
+    /* Free memory */
+    fclose(l_file);
        free(l_data);
        opj_stream_destroy(l_stream);
-       fclose(l_file);
        opj_destroy_codec(l_codec);
        opj_image_destroy(l_image);
 
        // Print profiling
        //PROFPRINT();
 
-       return 0;
+       return EXIT_SUCCESS;
 }