/* Declarations */
int get_num_images(char *imgdirpath);
int load_images(dircnt_t *dirptr, char *imgdirpath);
-int get_file_format(char *filename);
+int get_file_format(const char *filename);
char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
+static int infile_format(const char *fname);
int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename);
int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
fprintf(stdout," are decoded.\n");
fprintf(stdout," -x \n");
fprintf(stdout," Create an index file *.Idx (-x index_name.Idx) \n");
- fprintf(stdout," -d <x0,x1,y0,y1>\n");
+ 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
}
/* -------------------------------------------------------------------------- */
-int get_file_format(char *filename) {
+int get_file_format(const char *filename) {
unsigned int i;
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 };
strcpy(image_filename,dirptr->filename[imageno]);
fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
- parameters->decod_format = get_file_format(image_filename);
+ parameters->decod_format = infile_format(image_filename);
if (parameters->decod_format == -1)
return 1;
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename));
- //Set output file
+ /*Set output file*/
strcpy(temp_ofname,strtok(image_filename,"."));
while((temp_p = strtok(NULL,".")) != NULL){
strcat(temp_ofname,temp1);
return 0;
}
+/* -------------------------------------------------------------------------- */
+#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
+#define JP2_MAGIC "\x0d\x0a\x87\x0a"
+/* position 45: "\xff\x52" */
+#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
+
+static int infile_format(const char *fname)
+{
+ FILE *reader;
+ const char *s, *magic_s;
+ int ext_format, magic_format;
+ unsigned char buf[12];
+ unsigned int l_nb_read;
+
+ reader = fopen(fname, "rb");
+
+ if (reader == NULL)
+ return -1;
+
+ memset(buf, 0, 12);
+ l_nb_read = fread(buf, 1, 12, reader);
+ fclose(reader);
+ if (l_nb_read != 12)
+ return -1;
+
+
+
+ ext_format = get_file_format(fname);
+
+ if (ext_format == JPT_CFMT)
+ return JPT_CFMT;
+
+ if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
+ magic_format = JP2_CFMT;
+ magic_s = ".jp2";
+ }
+ else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
+ magic_format = J2K_CFMT;
+ magic_s = ".j2k or .jpc or .j2c";
+ }
+ else
+ return -1;
+
+ if (magic_format == ext_format)
+ return ext_format;
+
+ s = fname + strlen(fname) - 4;
+
+ fputs("\n===========================================\n", stderr);
+ fprintf(stderr, "The extension of this file is incorrect.\n"
+ "FOUND %s. SHOULD BE %s\n", s, magic_s);
+ fputs("===========================================\n", stderr);
+
+ return magic_format;
+}
+
/* -------------------------------------------------------------------------- */
/**
* Parse the command line
{"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
case 'i': /* input file */
{
char *infile = opj_optarg;
- parameters->decod_format = get_file_format(infile);
+ parameters->decod_format = infile_format(infile);
switch(parameters->decod_format) {
case J2K_CFMT:
break;
free(ROI_values);
}
+ break;
+
+ /* ----------------------------------------------------- */
+
+ case 't': /* Input tile index */
+ {
+ sscanf(opj_optarg, "%d", ¶meters->tile_index);
+ parameters->nb_tile_to_decode = 1;
+ }
+ break;
/* ----------------------------------------------------- */
dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
if(dirptr){
- dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names */
+ dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images*sizeof(char*));
if(!dirptr->filename_buf){
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 {
- /*opj_dump_codec(dinfo, OPJ_J2K_MH_IND, stdout );
-
- cstr_index = opj_get_cstr_index(dinfo);*/
+ // It is just here to illustrate how to use the resolution after set parameters
+ /*if (!opj_set_decoded_resolution_factor(dinfo, 5)) {
+ fprintf(stderr, "ERROR -> j2k_to_image: failed to set the resolution factor tile!\n");
+ opj_destroy_codec(dinfo);
+ opj_stream_destroy(cio);
+ opj_image_destroy(image);
+ fclose(fsrc);
+ return EXIT_FAILURE;
+ }*/
- fprintf(stderr, "image is decoded!\n");
+ 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);
+ }
/* Close the byte stream */
opj_stream_destroy(cio);
fclose(fsrc);
-
if(image->color_space == CLRSPC_SYCC){
color_sycc_to_rgb(image); /* FIXME */
}
}
return EXIT_SUCCESS;
}
-//end main
+/*end main*/