diff options
| author | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2014-03-25 15:16:07 +0000 |
|---|---|---|
| committer | Mathieu Malaterre <mathieu.malaterre@gmail.com> | 2014-03-25 15:16:07 +0000 |
| commit | f776cffbc68c543e89366d001c9255824d7b3e51 (patch) | |
| tree | 20fd03225ac84baf4f6000afffed6c2475c4c50c /src/bin/jp2 | |
| parent | f52bb0e7a675d84b168c5f6ae8120063753cb30d (diff) | |
| parent | 666f71b2a171f0d5f94dc49ae60f439b0f517c6b (diff) | |
[2.0] Backport all changes from trunk
We only backport change that do not affect API or ABI
Update issue 313
Diffstat (limited to 'src/bin/jp2')
| -rw-r--r-- | src/bin/jp2/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/bin/jp2/convert.c | 6054 | ||||
| -rw-r--r-- | src/bin/jp2/opj_compress.c | 3301 | ||||
| -rw-r--r-- | src/bin/jp2/opj_decompress.c | 77 | ||||
| -rw-r--r-- | src/bin/jp2/opj_dump.c | 37 |
5 files changed, 4742 insertions, 4728 deletions
diff --git a/src/bin/jp2/CMakeLists.txt b/src/bin/jp2/CMakeLists.txt index aee330a5..43746317 100644 --- a/src/bin/jp2/CMakeLists.txt +++ b/src/bin/jp2/CMakeLists.txt @@ -11,6 +11,7 @@ set(common_SRCS # Headers file are located here: include_directories( ${OPENJPEG_BINARY_DIR}/src/lib/openjp2 # opj_config.h + ${OPENJPEG_BINARY_DIR}/src/bin/common # opj_apps_config.h ${OPENJPEG_SOURCE_DIR}/src/lib/openjp2 ${OPENJPEG_SOURCE_DIR}/src/bin/common ${LCMS_INCLUDE_DIRNAME} diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 98d900ad..d3e9773c 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -29,21 +29,21 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_config.h" +#include "opj_apps_config.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> -#ifdef HAVE_LIBTIFF +#ifdef OPJ_HAVE_LIBTIFF #include <tiffio.h> -#endif /* HAVE_LIBTIFF */ +#endif /* OPJ_HAVE_LIBTIFF */ -#ifdef HAVE_LIBPNG +#ifdef OPJ_HAVE_LIBPNG #include <zlib.h> #include <png.h> -#endif /* HAVE_LIBPNG */ +#endif /* OPJ_HAVE_LIBPNG */ #include "openjpeg.h" #include "convert.h" @@ -54,11 +54,11 @@ * log2(a) */ static int int_floorlog2(int a) { - int l; - for (l = 0; a > 1; l++) { - a >>= 1; - } - return l; + int l; + for (l = 0; a > 1; l++) { + a >>= 1; + } + return l; } /* -->> -->> -->> -->> @@ -94,8 +94,8 @@ struct tga_header static unsigned short get_ushort(unsigned short val) { -#ifdef WORDS_BIGENDIAN - return( ((val & 0xff) << 8) + (val >> 8) ); +#ifdef OPJ_BIG_ENDIAN + return( ((val & 0xff) << 8) + (val >> 8) ); #else return( val ); #endif @@ -105,399 +105,422 @@ static unsigned short get_ushort(unsigned short val) { #define TGA_HEADER_SIZE 18 static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, - unsigned int *width, unsigned int *height, int *flip_image) + unsigned int *width, unsigned int *height, int *flip_image) { - int palette_size; - unsigned char *tga ; - unsigned char id_len, cmap_type, image_type; - unsigned char pixel_depth, image_desc; - unsigned short cmap_index, cmap_len, cmap_entry_size; - unsigned short x_origin, y_origin, image_w, image_h; - - if (!bits_per_pixel || !width || !height || !flip_image) - return 0; - tga = (unsigned char*)malloc(18); - - if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0 ; - } - id_len = (unsigned char)tga[0]; - cmap_type = (unsigned char)tga[1]; - image_type = (unsigned char)tga[2]; - cmap_index = get_ushort(*(unsigned short*)(&tga[3])); - cmap_len = get_ushort(*(unsigned short*)(&tga[5])); - cmap_entry_size = (unsigned char)tga[7]; - - - x_origin = get_ushort(*(unsigned short*)(&tga[8])); - y_origin = get_ushort(*(unsigned short*)(&tga[10])); - image_w = get_ushort(*(unsigned short*)(&tga[12])); - image_h = get_ushort(*(unsigned short*)(&tga[14])); - pixel_depth = (unsigned char)tga[16]; - image_desc = (unsigned char)tga[17]; - - free(tga); - - *bits_per_pixel = (unsigned int)pixel_depth; - *width = (unsigned int)image_w; - *height = (unsigned int)image_h; - - /* Ignore tga identifier, if present ... */ - if (id_len) - { - unsigned char *id = (unsigned char *) malloc(id_len); - if ( !fread(id, id_len, 1, fp) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - free(id); - return 0 ; - } - free(id); - } - - /* Test for compressed formats ... not yet supported ... - // Note :- 9 - RLE encoded palettized. - // 10 - RLE encoded RGB. */ - if (image_type > 8) - { - fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); - return 0 ; - } - - *flip_image = !(image_desc & 32); - - /* Palettized formats are not yet supported, skip over the palette, if present ... */ - palette_size = cmap_len * (cmap_entry_size/8); - - if (palette_size>0) - { - fprintf(stderr, "File contains a palette - not yet supported."); - fseek(fp, palette_size, SEEK_CUR); - } - return 1; + int palette_size; + unsigned char *tga ; + unsigned char id_len, /*cmap_type,*/ image_type; + unsigned char pixel_depth, image_desc; + unsigned short /*cmap_index,*/ cmap_len, cmap_entry_size; + unsigned short /*x_origin, y_origin,*/ image_w, image_h; + + if (!bits_per_pixel || !width || !height || !flip_image) + return 0; + tga = (unsigned char*)malloc(18); + + if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0 ; + } + id_len = (unsigned char)tga[0]; + /*cmap_type = (unsigned char)tga[1];*/ + image_type = (unsigned char)tga[2]; + /*cmap_index = get_ushort(*(unsigned short*)(&tga[3]));*/ + cmap_len = get_ushort(*(unsigned short*)(&tga[5])); + cmap_entry_size = (unsigned char)tga[7]; + + +#if 0 + x_origin = get_ushort(*(unsigned short*)(&tga[8])); + y_origin = get_ushort(*(unsigned short*)(&tga[10])); +#endif + image_w = get_ushort(*(unsigned short*)(&tga[12])); + image_h = get_ushort(*(unsigned short*)(&tga[14])); + pixel_depth = (unsigned char)tga[16]; + image_desc = (unsigned char)tga[17]; + + free(tga); + + *bits_per_pixel = (unsigned int)pixel_depth; + *width = (unsigned int)image_w; + *height = (unsigned int)image_h; + + /* Ignore tga identifier, if present ... */ + if (id_len) + { + unsigned char *id = (unsigned char *) malloc(id_len); + if ( !fread(id, id_len, 1, fp) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + free(id); + return 0 ; + } + free(id); + } + + /* Test for compressed formats ... not yet supported ... + // Note :- 9 - RLE encoded palettized. + // 10 - RLE encoded RGB. */ + if (image_type > 8) + { + fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); + return 0 ; + } + + *flip_image = !(image_desc & 32); + + /* Palettized formats are not yet supported, skip over the palette, if present ... */ + palette_size = cmap_len * (cmap_entry_size/8); + + if (palette_size>0) + { + fprintf(stderr, "File contains a palette - not yet supported."); + fseek(fp, palette_size, SEEK_CUR); + } + return 1; } -#if WORDS_BIGENDIAN == 1 +#ifdef OPJ_BIG_ENDIAN -static inline int16_t swap16(int16_t x) +static INLINE int16_t swap16(int16_t x) { - return((((u_int16_t)x & 0x00ffU) << 8) | - (((u_int16_t)x & 0xff00U) >> 8)); + return((((u_int16_t)x & 0x00ffU) << 8) | + (((u_int16_t)x & 0xff00U) >> 8)); } #endif static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, - OPJ_BOOL flip_image) + OPJ_BOOL flip_image) { - unsigned short image_w, image_h, us0; - unsigned char uc0, image_type; - unsigned char pixel_depth, image_desc; + unsigned short image_w, image_h, us0; + unsigned char uc0, image_type; + unsigned char pixel_depth, image_desc; - if (!bits_per_pixel || !width || !height) - return 0; + if (!bits_per_pixel || !width || !height) + return 0; - pixel_depth = 0; + pixel_depth = 0; - if ( bits_per_pixel < 256 ) - pixel_depth = (unsigned char)bits_per_pixel; - else{ - fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header"); - return 0; - } - uc0 = 0; + if ( bits_per_pixel < 256 ) + pixel_depth = (unsigned char)bits_per_pixel; + else{ + fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header"); + return 0; + } + uc0 = 0; - if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */ - if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */ + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */ + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */ - image_type = 2; /* Uncompressed. */ - if(fwrite(&image_type, 1, 1, fp) != 1) goto fails; + image_type = 2; /* Uncompressed. */ + if(fwrite(&image_type, 1, 1, fp) != 1) goto fails; - us0 = 0; - if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */ - if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */ - if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */ + us0 = 0; + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */ + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */ + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */ - if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */ - if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */ + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */ + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */ - image_w = (unsigned short)width; - image_h = (unsigned short) height; + image_w = (unsigned short)width; + image_h = (unsigned short) height; -#if WORDS_BIGENDIAN == 0 - if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; - if(fwrite(&image_h, 2, 1, fp) != 1) goto fails; +#ifndef OPJ_BIG_ENDIAN + if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; + if(fwrite(&image_h, 2, 1, fp) != 1) goto fails; #else - image_w = swap16(image_w); - image_h = swap16(image_h); - if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; - if(fwrite(&image_h, 2, 1, fp) != 1) goto fails; + image_w = swap16(image_w); + image_h = swap16(image_h); + if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; + if(fwrite(&image_h, 2, 1, fp) != 1) goto fails; #endif - if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails; + if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails; - image_desc = 8; /* 8 bits per component. */ + image_desc = 8; /* 8 bits per component. */ - if (flip_image) - image_desc |= 32; - if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails; + if (flip_image) + image_desc |= 32; + if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails; - return 1; + return 1; fails: - fputs("\nwrite_tgaheader: write ERROR\n", stderr); - return 0; + fputs("\nwrite_tgaheader: write ERROR\n", stderr); + return 0; } opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { - FILE *f; - opj_image_t *image; - unsigned int image_width, image_height, pixel_bit_depth; - unsigned int x, y; - int flip_image=0; - opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */ - int numcomps; - OPJ_COLOR_SPACE color_space; - OPJ_BOOL mono ; - OPJ_BOOL save_alpha; - int subsampling_dx, subsampling_dy; - int i; - - f = fopen(filename, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !!\n", filename); - return 0; - } - - if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) - return NULL; - - /* We currently only support 24 & 32 bit tga's ... */ - if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) - return NULL; - - /* initialize image components */ - memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); - - mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */ - save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */ - - if (mono) { - color_space = OPJ_CLRSPC_GRAY; - numcomps = save_alpha ? 2 : 1; - } - else { - numcomps = save_alpha ? 4 : 3; - color_space = OPJ_CLRSPC_SRGB; - } - - subsampling_dx = parameters->subsampling_dx; - subsampling_dy = parameters->subsampling_dy; - - for (i = 0; i < numcomps; i++) { - cmptparm[i].prec = 8; - cmptparm[i].bpp = 8; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = image_width; - cmptparm[i].h = image_height; - } - - /* create the image */ - image = opj_image_create(numcomps, &cmptparm[0], color_space); - - if (!image) - return NULL; - - /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (image_width - 1) * subsampling_dx + 1 : image->x0 + (image_width - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (image_height - 1) * subsampling_dy + 1 : image->y0 + (image_height - 1) * subsampling_dy + 1; - - /* set image data */ - for (y=0; y < image_height; y++) - { - int index; - - if (flip_image) - index = (image_height-y-1)*image_width; - else - index = y*image_width; - - if (numcomps==3) - { - for (x=0;x<image_width;x++) - { - unsigned char r,g,b; - - if( !fread(&b, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - if ( !fread(&g, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - if ( !fread(&r, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - - image->comps[0].data[index]=r; - image->comps[1].data[index]=g; - image->comps[2].data[index]=b; - index++; - } - } - else if (numcomps==4) - { - for (x=0;x<image_width;x++) - { - unsigned char r,g,b,a; - if ( !fread(&b, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - if ( !fread(&g, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - if ( !fread(&r, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - if ( !fread(&a, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - opj_image_destroy(image); - return NULL; - } - - image->comps[0].data[index]=r; - image->comps[1].data[index]=g; - image->comps[2].data[index]=b; - image->comps[3].data[index]=a; - index++; - } - } - else { - fprintf(stderr, "Currently unsupported bit depth : %s\n", filename); - } - } - return image; -} + FILE *f; + opj_image_t *image; + unsigned int image_width, image_height, pixel_bit_depth; + unsigned int x, y; + int flip_image=0; + opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */ + int numcomps; + OPJ_COLOR_SPACE color_space; + OPJ_BOOL mono ; + OPJ_BOOL save_alpha; + int subsampling_dx, subsampling_dy; + int i; + + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "Failed to open %s for reading !!\n", filename); + return 0; + } -int imagetotga(opj_image_t * image, const char *outfile) { - int width, height, bpp, x, y; - OPJ_BOOL write_alpha; - int i, adjustR, adjustG, adjustB; - unsigned int alpha_channel; - float r,g,b,a; - unsigned char value; - float scale; - FILE *fdest; - size_t res; - - fdest = fopen(outfile, "wb"); - if (!fdest) { - fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); - return 1; - } - - for (i = 0; i < image->numcomps-1; i++) { - if ((image->comps[0].dx != image->comps[i+1].dx) - ||(image->comps[0].dy != image->comps[i+1].dy) - ||(image->comps[0].prec != image->comps[i+1].prec)) { - fprintf(stderr, "Unable to create a tga file with such J2K image charateristics."); - return 1; - } - } + if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) + return NULL; - width = image->comps[0].w; - height = image->comps[0].h; + /* We currently only support 24 & 32 bit tga's ... */ + if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) + return NULL; - /* Mono with alpha, or RGB with alpha. */ - write_alpha = (image->numcomps==2) || (image->numcomps==4); + /* initialize image components */ + memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); - /* Write TGA header */ - bpp = write_alpha ? 32 : 24; - if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE)) - return 1; + mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */ + save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */ - alpha_channel = image->numcomps-1; + if (mono) { + color_space = OPJ_CLRSPC_GRAY; + numcomps = save_alpha ? 2 : 1; + } + else { + numcomps = save_alpha ? 4 : 3; + color_space = OPJ_CLRSPC_SRGB; + } - scale = 255.0f / (float)((1<<image->comps[0].prec)-1); + subsampling_dx = parameters->subsampling_dx; + subsampling_dy = parameters->subsampling_dy; - adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + for (i = 0; i < numcomps; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = image_width; + cmptparm[i].h = image_height; + } - for (y=0; y < height; y++) { - unsigned int index=y*width; + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); - for (x=0; x < width; x++, index++) { - r = (float)(image->comps[0].data[index] + adjustR); + if (!image) + return NULL; - if (image->numcomps>2) { - g = (float)(image->comps[1].data[index] + adjustG); - b = (float)(image->comps[2].data[index] + adjustB); - } - else {/* Greyscale ... */ - g = r; - b = r; - } + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(image_width - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(image_height - 1) * (OPJ_UINT32)subsampling_dy + 1; - /* TGA format writes BGR ... */ - value = (unsigned char)(b*scale); - res = fwrite(&value,1,1,fdest); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; - } + /* set image data */ + for (y=0; y < image_height; y++) + { + int index; - value = (unsigned char)(g*scale); - res = fwrite(&value,1,1,fdest); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; - } + if (flip_image) + index = (int)((image_height-y-1)*image_width); + else + index = (int)(y*image_width); + + if (numcomps==3) + { + for (x=0;x<image_width;x++) + { + unsigned char r,g,b; + + if( !fread(&b, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if ( !fread(&g, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if ( !fread(&r, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + + image->comps[0].data[index]=r; + image->comps[1].data[index]=g; + image->comps[2].data[index]=b; + index++; + } + } + else if (numcomps==4) + { + for (x=0;x<image_width;x++) + { + unsigned char r,g,b,a; + if ( !fread(&b, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if ( !fread(&g, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if ( !fread(&r, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if ( !fread(&a, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + + image->comps[0].data[index]=r; + image->comps[1].data[index]=g; + image->comps[2].data[index]=b; + image->comps[3].data[index]=a; + index++; + } + } + else { + fprintf(stderr, "Currently unsupported bit depth : %s\n", filename); + } + } + return image; +} + +int imagetotga(opj_image_t * image, const char *outfile) { + int width, height, bpp, x, y; + OPJ_BOOL write_alpha; + unsigned int i; + int adjustR, adjustG, adjustB, fails; + unsigned int alpha_channel; + float r,g,b,a; + unsigned char value; + float scale; + FILE *fdest; + size_t res; + fails = 1; - value = (unsigned char)(r*scale); - res = fwrite(&value,1,1,fdest); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + fdest = fopen(outfile, "wb"); + if (!fdest) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); return 1; - } + } - if (write_alpha) { - a = (float)(image->comps[alpha_channel].data[index]); - value = (unsigned char)(a*scale); - res = fwrite(&value,1,1,fdest); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; + for (i = 0; i < image->numcomps-1; i++) { + if ((image->comps[0].dx != image->comps[i+1].dx) + ||(image->comps[0].dy != image->comps[i+1].dy) + ||(image->comps[0].prec != image->comps[i+1].prec)) { + fprintf(stderr, "Unable to create a tga file with such J2K image charateristics."); + return 1; } - } - } - } + } + + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; + + /* Mono with alpha, or RGB with alpha. */ + write_alpha = (image->numcomps==2) || (image->numcomps==4); + + /* Write TGA header */ + bpp = write_alpha ? 32 : 24; + + if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE)) + goto fin; + + alpha_channel = image->numcomps-1; + + scale = 255.0f / (float)((1<<image->comps[0].prec)-1); + + adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + + for (y=0; y < height; y++) + { + unsigned int index= (unsigned int)(y*width); + + for (x=0; x < width; x++, index++) + { + r = (float)(image->comps[0].data[index] + adjustR); + + if (image->numcomps > 2) + { + g = (float)(image->comps[1].data[index] + adjustG); + b = (float)(image->comps[2].data[index] + adjustB); + } + else + {/* Greyscale ... */ + g = r; + b = r; + } + +/* TGA format writes BGR ... */ + if(b > 255.) b = 255.; else if(b < 0.) b = 0.; + value = (unsigned char)(b*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) + { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + if(g > 255.) g = 255.; else if(g < 0.) g = 0.; + value = (unsigned char)(g*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) + { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + if(r > 255.) r = 255.; else if(r < 0.) r = 0.; + value = (unsigned char)(r*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) + { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } - return 0; + if (write_alpha) + { + a = (float)(image->comps[alpha_channel].data[index]); + if(a > 255.) a = 255.; else if(a < 0.) a = 0.; + value = (unsigned char)(a*scale); + res = fwrite(&value,1,1,fdest); + + if( res < 1 ) + { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + } + } + } + fails = 0; +fin: + fclose(fdest); + + return fails; } /* -->> -->> -->> -->> @@ -513,686 +536,685 @@ typedef unsigned short int WORD; typedef unsigned int DWORD; typedef struct { - WORD bfType; /* 'BM' for Bitmap (19776) */ - DWORD bfSize; /* Size of the file */ - WORD bfReserved1; /* Reserved : 0 */ - WORD bfReserved2; /* Reserved : 0 */ - DWORD bfOffBits; /* Offset */ + WORD bfType; /* 'BM' for Bitmap (19776) */ + DWORD bfSize; /* Size of the file */ + WORD bfReserved1; /* Reserved : 0 */ + WORD bfReserved2; /* Reserved : 0 */ + DWORD bfOffBits; /* Offset */ } BITMAPFILEHEADER_t; typedef struct { - DWORD biSize; /* Size of the structure in bytes */ - DWORD biWidth; /* Width of the image in pixels */ - DWORD biHeight; /* Heigth of the image in pixels */ - WORD biPlanes; /* 1 */ - WORD biBitCount; /* Number of color bits by pixels */ - DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ - DWORD biSizeImage; /* Size of the image in bytes */ - DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ - DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ - DWORD biClrUsed; /* Number of color used in the image (0: ALL) */ - DWORD biClrImportant; /* Number of important color (0: ALL) */ + DWORD biSize; /* Size of the structure in bytes */ + DWORD biWidth; /* Width of the image in pixels */ + DWORD biHeight; /* Heigth of the image in pixels */ + WORD biPlanes; /* 1 */ + WORD biBitCount; /* Number of color bits by pixels */ + DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ + DWORD biSizeImage; /* Size of the image in bytes */ + DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ + DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ + DWORD biClrUsed; /* Number of color used in the image (0: ALL) */ + DWORD biClrImportant; /* Number of important color (0: ALL) */ } BITMAPINFOHEADER_t; opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - - int i, numcomps, w, h; - OPJ_COLOR_SPACE color_space; - opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */ - opj_image_t * image = NULL; - - FILE *IN; - BITMAPFILEHEADER_t File_h; - BITMAPINFOHEADER_t Info_h; - unsigned char *RGB; - unsigned char *table_R, *table_G, *table_B; - unsigned int j, PAD = 0; - - int x, y, index; - int gray_scale = 1; - int has_color; - DWORD W, H; - - IN = fopen(filename, "rb"); - if (!IN) - { - fprintf(stderr, "Failed to open %s for reading !!\n", filename); - return NULL; - } - - File_h.bfType = getc(IN); - File_h.bfType = (getc(IN) << 8) + File_h.bfType; - - if (File_h.bfType != 19778) - { - fprintf(stderr,"Error, not a BMP file!\n"); - fclose(IN); - return NULL; - } - /* FILE HEADER */ - /* ------------- */ - File_h.bfSize = getc(IN); - File_h.bfSize = (getc(IN) << 8) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 16) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 24) + File_h.bfSize; + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + + int i, numcomps, w, h; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */ + opj_image_t * image = NULL; + + FILE *IN; + BITMAPFILEHEADER_t File_h; + BITMAPINFOHEADER_t Info_h; + unsigned char *RGB; + unsigned char *table_R, *table_G, *table_B; + unsigned int j, PAD = 0; + + unsigned int x, y; + int index; + int gray_scale = 1; + int has_color; + DWORD W, H; + + IN = fopen(filename, "rb"); + if (!IN) + { + fprintf(stderr, "Failed to open %s for reading !!\n", filename); + return NULL; + } - File_h.bfReserved1 = getc(IN); - File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1; + File_h.bfType = (WORD)getc(IN); + File_h.bfType = (WORD)((getc(IN) << 8) + File_h.bfType); - File_h.bfReserved2 = getc(IN); - File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2; + if (File_h.bfType != 19778) + { + fprintf(stderr,"Error, not a BMP file!\n"); + fclose(IN); + return NULL; + } + /* FILE HEADER */ + /* ------------- */ + File_h.bfSize = (DWORD)getc(IN); + File_h.bfSize = (DWORD)(getc(IN) << 8) + File_h.bfSize; + File_h.bfSize = (DWORD)(getc(IN) << 16) + File_h.bfSize; + File_h.bfSize = (DWORD)(getc(IN) << 24) + File_h.bfSize; - File_h.bfOffBits = getc(IN); - File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits; + File_h.bfReserved1 = (WORD)getc(IN); + File_h.bfReserved1 = (WORD)((getc(IN) << 8) + File_h.bfReserved1); - /* INFO HEADER */ - /* ------------- */ + File_h.bfReserved2 = (WORD)getc(IN); + File_h.bfReserved2 = (WORD)((getc(IN) << 8) + File_h.bfReserved2); - Info_h.biSize = getc(IN); - Info_h.biSize = (getc(IN) << 8) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 16) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 24) + Info_h.biSize; + File_h.bfOffBits = (DWORD)getc(IN); + File_h.bfOffBits = (DWORD)(getc(IN) << 8) + File_h.bfOffBits; + File_h.bfOffBits = (DWORD)(getc(IN) << 16) + File_h.bfOffBits; + File_h.bfOffBits = (DWORD)(getc(IN) << 24) + File_h.bfOffBits; - if(Info_h.biSize != 40) - { - fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize); - fclose(IN); - return NULL; - } - Info_h.biWidth = getc(IN); - Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth; - w = Info_h.biWidth; - - Info_h.biHeight = getc(IN); - Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight; - h = Info_h.biHeight; - - Info_h.biPlanes = getc(IN); - Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes; - - Info_h.biBitCount = getc(IN); - Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount; - - Info_h.biCompression = getc(IN); - Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression; - - Info_h.biSizeImage = getc(IN); - Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage; - - Info_h.biXpelsPerMeter = getc(IN); - Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter; - - Info_h.biYpelsPerMeter = getc(IN); - Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter; - - Info_h.biClrUsed = getc(IN); - Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed; - - Info_h.biClrImportant = getc(IN); - Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; - - /* Read the data and store them in the OUT file */ - - if (Info_h.biBitCount == 24) - { - numcomps = 3; - color_space = OPJ_CLRSPC_SRGB; - /* initialize image components */ - memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); - for(i = 0; i < numcomps; i++) - { - cmptparm[i].prec = 8; - cmptparm[i].bpp = 8; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = w; - cmptparm[i].h = h; - } - /* create the image */ - image = opj_image_create(numcomps, &cmptparm[0], color_space); - if(!image) - { - fclose(IN); - return NULL; - } + /* INFO HEADER */ + /* ------------- */ - /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1; - - /* set image data */ - - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - W = Info_h.biWidth; - H = Info_h.biHeight; - - /* PAD = 4 - (3 * W) % 4; */ - /* PAD = (PAD == 4) ? 0 : PAD; */ - PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; - - RGB = (unsigned char *) - malloc((3 * W + PAD) * H * sizeof(unsigned char)); - - if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H ) - { - free(RGB); - opj_image_destroy(image); - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return NULL; - } - - index = 0; - - for(y = 0; y < (int)H; y++) - { - unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y); - for(x = 0; x < (int)W; x++) - { - unsigned char *pixel = &scanline[3 * x]; - image->comps[0].data[index] = pixel[2]; /* R */ - image->comps[1].data[index] = pixel[1]; /* G */ - image->comps[2].data[index] = pixel[0]; /* B */ - index++; - } - } - free(RGB); - }/* if (Info_h.biBitCount == 24) */ - else - if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */ - { - if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256; - else - if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256; - - table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - - has_color = 0; - for (j = 0; j < Info_h.biClrUsed; j++) - { - table_B[j] = (unsigned char)getc(IN); - table_G[j] = (unsigned char)getc(IN); - table_R[j] = (unsigned char)getc(IN); - getc(IN); - has_color += - !(table_R[j] == table_G[j] && table_R[j] == table_B[j]); - } - if(has_color) gray_scale = 0; - - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - W = Info_h.biWidth; - H = Info_h.biHeight; - if (Info_h.biWidth % 2) - W++; - - numcomps = gray_scale ? 1 : 3; - color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB; - /* initialize image components */ - memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); - for(i = 0; i < numcomps; i++) - { - cmptparm[i].prec = 8; - cmptparm[i].bpp = 8; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = w; - cmptparm[i].h = h; - } - /* create the image */ - image = opj_image_create(numcomps, &cmptparm[0], color_space); - if(!image) - { - fclose(IN); - free(table_R); free(table_G); free(table_B); - return NULL; - } + Info_h.biSize = (DWORD)getc(IN); + Info_h.biSize = (DWORD)(getc(IN) << 8) + Info_h.biSize; + Info_h.biSize = (DWORD)(getc(IN) << 16) + Info_h.biSize; + Info_h.biSize = (DWORD)(getc(IN) << 24) + Info_h.biSize; - /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1; - - /* set image data */ - - RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); - - if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H ) - { - free(table_R); - free(table_G); - free(table_B); - free(RGB); - opj_image_destroy(image); - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return NULL; - } - if (gray_scale) - { - index = 0; - for (j = 0; j < W * H; j++) - { - if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) - { - image->comps[0].data[index] = - table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]; - index++; - } - } + if(Info_h.biSize != 40) + { + fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize); + fclose(IN); + return NULL; + } + Info_h.biWidth = (DWORD)getc(IN); + Info_h.biWidth = (DWORD)(getc(IN) << 8) + Info_h.biWidth; + Info_h.biWidth = (DWORD)(getc(IN) << 16) + Info_h.biWidth; + Info_h.biWidth = (DWORD)(getc(IN) << 24) + Info_h.biWidth; + w = (int)Info_h.biWidth; + + Info_h.biHeight = (DWORD)getc(IN); + Info_h.biHeight = (DWORD)(getc(IN) << 8) + Info_h.biHeight; + Info_h.biHeight = (DWORD)(getc(IN) << 16) + Info_h.biHeight; + Info_h.biHeight = (DWORD)(getc(IN) << 24) + Info_h.biHeight; + h = (int)Info_h.biHeight; + + Info_h.biPlanes = (WORD)getc(IN); + Info_h.biPlanes = (WORD)((getc(IN) << 8) + Info_h.biPlanes); + + Info_h.biBitCount = (WORD)getc(IN); + Info_h.biBitCount = (WORD)((getc(IN) << 8) + Info_h.biBitCount); + + Info_h.biCompression = (DWORD)getc(IN); + Info_h.biCompression = (DWORD)(getc(IN) << 8) + Info_h.biCompression; + Info_h.biCompression = (DWORD)(getc(IN) << 16) + Info_h.biCompression; + Info_h.biCompression = (DWORD)(getc(IN) << 24) + Info_h.biCompression; + + Info_h.biSizeImage = (DWORD)getc(IN); + Info_h.biSizeImage = (DWORD)(getc(IN) << 8) + Info_h.biSizeImage; + Info_h.biSizeImage = (DWORD)(getc(IN) << 16) + Info_h.biSizeImage; + Info_h.biSizeImage = (DWORD)(getc(IN) << 24) + Info_h.biSizeImage; + + Info_h.biXpelsPerMeter = (DWORD)getc(IN); + Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (DWORD)(getc(IN) << 24) + Info_h.biXpelsPerMeter; + + Info_h.biYpelsPerMeter = (DWORD)getc(IN); + Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 8) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 16) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (DWORD)(getc(IN) << 24) + Info_h.biYpelsPerMeter; + + Info_h.biClrUsed = (DWORD)getc(IN); + Info_h.biClrUsed = (DWORD)(getc(IN) << 8) + Info_h.biClrUsed; + Info_h.biClrUsed = (DWORD)(getc(IN) << 16) + Info_h.biClrUsed; + Info_h.biClrUsed = (DWORD)(getc(IN) << 24) + Info_h.biClrUsed; + + Info_h.biClrImportant = (DWORD)getc(IN); + Info_h.biClrImportant = (DWORD)(getc(IN) << 8) + Info_h.biClrImportant; + Info_h.biClrImportant = (DWORD)(getc(IN) << 16) + Info_h.biClrImportant; + Info_h.biClrImportant = (DWORD)(getc(IN) << 24) + Info_h.biClrImportant; + + /* Read the data and store them in the OUT file */ + + if (Info_h.biBitCount == 24) + { + numcomps = 3; + color_space = OPJ_CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) + { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)w; + cmptparm[i].h = (OPJ_UINT32)h; + } + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + if(!image) + { + fclose(IN); + return NULL; + } - } - else - { - index = 0; - for (j = 0; j < W * H; j++) - { - if ((j % W < W - 1 && Info_h.biWidth % 2) - || !(Info_h.biWidth % 2)) - { - unsigned char pixel_index = - RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]; - image->comps[0].data[index] = table_R[pixel_index]; - image->comps[1].data[index] = table_G[pixel_index]; - image->comps[2].data[index] = table_B[pixel_index]; - index++; - } - } - } - free(RGB); - free(table_R); - free(table_G); - free(table_B); - }/* RGB8 */ - else - if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/ - { - unsigned char *pix, *beyond; - int *gray, *red, *green, *blue; - unsigned int x, y, max; - int i, c, c1; - unsigned char uc; - - if (Info_h.biClrUsed == 0) - Info_h.biClrUsed = 256; - else if (Info_h.biClrUsed > 256) - Info_h.biClrUsed = 256; - - table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); - table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - - has_color = 0; - for (j = 0; j < Info_h.biClrUsed; j++) - { - table_B[j] = (unsigned char)getc(IN); - table_G[j] = (unsigned char)getc(IN); - table_R[j] = (unsigned char)getc(IN); - getc(IN); - has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]); - } - - if (has_color) - gray_scale = 0; - - numcomps = gray_scale ? 1 : 3; - color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB; - /* initialize image components */ - memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); - for (i = 0; i < numcomps; i++) - { - cmptparm[i].prec = 8; - cmptparm[i].bpp = 8; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = w; - cmptparm[i].h = h; - } - /* create the image */ - image = opj_image_create(numcomps, &cmptparm[0], color_space); - if (!image) - { - fclose(IN); - free(table_R); - free(table_G); - free(table_B); - return NULL; - } - - /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - - 1) * subsampling_dy + 1; - - /* set image data */ - - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - W = Info_h.biWidth; - H = Info_h.biHeight; - RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char)); - beyond = RGB + W * H; - pix = beyond - W; - x = y = 0; - - while (y < H) - { - c = getc(IN); - - if (c) - { - c1 = getc(IN); - - for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) - *pix = (unsigned char)c1; - } - else - { - c = getc(IN); - - if (c == 0x00) /* EOL */ - { - x = 0; - ++y; - pix = RGB + x + (H - y - 1) * W; - } - else if (c == 0x01) /* EOP */ - break; - else if (c == 0x02) /* MOVE by dxdy */ - { - c = getc(IN); - x += c; - c = getc(IN); - y += c; - pix = RGB + (H - y - 1) * W + x; - } - else /* 03 .. 255 */ - { - i = 0; - for (; i < c && x < W && pix < beyond; i++, x++, pix++) - { - c1 = getc(IN); - *pix = (unsigned char)c1; - } - if (c & 1) /* skip padding byte */ - getc(IN); - } - } - }/* while() */ - - if (gray_scale) - { - gray = image->comps[0].data; - pix = RGB; - max = W * H; - - while (max--) - { - uc = *pix++; - - *gray++ = table_R[uc]; - } - } - else - { - /*int *red, *green, *blue;*/ - - red = image->comps[0].data; - green = image->comps[1].data; - blue = image->comps[2].data; - pix = RGB; - max = W * H; - - while (max--) - { - uc = *pix++; - - *red++ = table_R[uc]; - *green++ = table_G[uc]; - *blue++ = table_B[uc]; - } - } - free(RGB); - free(table_R); - free(table_G); - free(table_B); - }/* RLE8 */ - else - { - fprintf(stderr, - "Other system than 24 bits/pixels or 8 bits (no RLE coding) " - "is not yet implemented [%d]\n", Info_h.biBitCount); - } - fclose(IN); - return image; + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + /* set image data */ + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, (long)File_h.bfOffBits, SEEK_SET); + + W = Info_h.biWidth; + H = Info_h.biHeight; + + /* PAD = 4 - (3 * W) % 4; */ + /* PAD = (PAD == 4) ? 0 : PAD; */ + PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; + + RGB = (unsigned char *) + malloc((3 * W + PAD) * H * sizeof(unsigned char)); + + if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H ) + { + free(RGB); + opj_image_destroy(image); + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return NULL; + } + + index = 0; + + for(y = 0; y < H; y++) + { + unsigned char *scanline = RGB + (3 * (unsigned int)W + PAD) * ((unsigned int)H - 1 - (unsigned int)y); + for(x = 0; x < W; x++) + { + unsigned char *pixel = &scanline[3 * x]; + image->comps[0].data[index] = pixel[2]; /* R */ + image->comps[1].data[index] = pixel[1]; /* G */ + image->comps[2].data[index] = pixel[0]; /* B */ + index++; + } + } + free(RGB); + }/* if (Info_h.biBitCount == 24) */ + else + if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */ + { + if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256; + else + if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256; + + table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); + + has_color = 0; + for (j = 0; j < Info_h.biClrUsed; j++) + { + table_B[j] = (unsigned char)getc(IN); + table_G[j] = (unsigned char)getc(IN); + table_R[j] = (unsigned char)getc(IN); + getc(IN); + has_color += + !(table_R[j] == table_G[j] && table_R[j] == table_B[j]); + } + if(has_color) gray_scale = 0; + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, (long)File_h.bfOffBits, SEEK_SET); + + W = Info_h.biWidth; + H = Info_h.biHeight; + if (Info_h.biWidth % 2) + W++; + + numcomps = gray_scale ? 1 : 3; + color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) + { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)w; + cmptparm[i].h = (OPJ_UINT32)h; + } + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + if(!image) + { + fclose(IN); + free(table_R); free(table_G); free(table_B); + return NULL; + } + + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + /* set image data */ + + RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); + + if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H ) + { + free(table_R); + free(table_G); + free(table_B); + free(RGB); + opj_image_destroy(image); + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return NULL; + } + if (gray_scale) + { + index = 0; + for (j = 0; j < W * H; j++) + { + if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) + { + image->comps[0].data[index] = + table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]; + index++; + } + } + + } + else + { + index = 0; + for (j = 0; j < W * H; j++) + { + if ((j % W < W - 1 && Info_h.biWidth % 2) + || !(Info_h.biWidth % 2)) + { + unsigned char pixel_index = + RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]; + image->comps[0].data[index] = table_R[pixel_index]; + image->comps[1].data[index] = table_G[pixel_index]; + image->comps[2].data[index] = table_B[pixel_index]; + index++; + } + } + } + free(RGB); + free(table_R); + free(table_G); + free(table_B); + }/* RGB8 */ + else + if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/ + { + unsigned char *pix, *beyond; + int *gray, *red, *green, *blue; + unsigned int max; + int c, c1; + unsigned char uc; + + if (Info_h.biClrUsed == 0) + Info_h.biClrUsed = 256; + else if (Info_h.biClrUsed > 256) + Info_h.biClrUsed = 256; + + table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); + table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); + + has_color = 0; + for (j = 0; j < Info_h.biClrUsed; j++) + { + table_B[j] = (unsigned char)getc(IN); + table_G[j] = (unsigned char)getc(IN); + table_R[j] = (unsigned char)getc(IN); + getc(IN); + has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]); + } + + if (has_color) + gray_scale = 0; + + numcomps = gray_scale ? 1 : 3; + color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for (i = 0; i < numcomps; i++) + { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)w; + cmptparm[i].h = (OPJ_UINT32)h; + } + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + if (!image) + { + fclose(IN); + free(table_R); + free(table_G); + free(table_B); + return NULL; + } + + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + /* set image data */ + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, (long)File_h.bfOffBits, SEEK_SET); + + W = Info_h.biWidth; + H = Info_h.biHeight; + RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char)); + beyond = RGB + W * H; + pix = beyond - W; + x = y = 0; + + while (y < H) + { + c = getc(IN); + + if (c) + { + c1 = getc(IN); + + for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++) + *pix = (unsigned char)c1; + } + else + { + c = getc(IN); + + if (c == 0x00) /* EOL */ + { + x = 0; + ++y; + pix = RGB + x + (H - y - 1) * W; + } + else if (c == 0x01) /* EOP */ + break; + else if (c == 0x02) /* MOVE by dxdy */ + { + c = getc(IN); + x += (unsigned int)c; + c = getc(IN); + y += (unsigned int)c; + pix = RGB + (H - y - 1) * W + x; + } + else /* 03 .. 255 */ + { + i = 0; + for (; i < c && x < W && pix < beyond; i++, x++, pix++) + { + c1 = getc(IN); + *pix = (unsigned char)c1; + } + if (c & 1) /* skip padding byte */ + getc(IN); + } + } + }/* while() */ + + if (gray_scale) + { + gray = image->comps[0].data; + pix = RGB; + max = W * H; + + while (max--) + { + uc = *pix++; + + *gray++ = table_R[uc]; + } + } + else + { + /*int *red, *green, *blue;*/ + + red = image->comps[0].data; + green = image->comps[1].data; + blue = image->comps[2].data; + pix = RGB; + max = W * H; + + while (max--) + { + uc = *pix++; + + *red++ = table_R[uc]; + *green++ = table_G[uc]; + *blue++ = table_B[uc]; + } + } + free(RGB); + free(table_R); + free(table_G); + free(table_B); + }/* RLE8 */ + else + { + fprintf(stderr, + "Other system than 24 bits/pixels or 8 bits (no RLE coding) " + "is not yet implemented [%d]\n", Info_h.biBitCount); + } + fclose(IN); + return image; } int imagetobmp(opj_image_t * image, const char *outfile) { - int w, h; - int i, pad; - FILE *fdest = NULL; - int adjustR, adjustG, adjustB; + int w, h; + int i, pad; + FILE *fdest = NULL; + int adjustR, adjustG, adjustB; - if (image->comps[0].prec < 8) { - fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec); - return 1; - } - if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx - && image->comps[1].dx == image->comps[2].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[1].dy == image->comps[2].dy - && image->comps[0].prec == image->comps[1].prec - && image->comps[1].prec == image->comps[2].prec) { - - /* -->> -->> -->> -->> - 24 bits color - <<-- <<-- <<-- <<-- */ - - fdest = fopen(outfile, "wb"); - if (!fdest) { - fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); - return 1; - } - - w = image->comps[0].w; - h = image->comps[0].h; - - fprintf(fdest, "BM"); - - /* FILE HEADER */ - /* ------------- */ - fprintf(fdest, "%c%c%c%c", - (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff); - - /* INFO HEADER */ - /* ------------- */ - fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff), - (unsigned char) ((w) >> 8) & 0xff, - (unsigned char) ((w) >> 16) & 0xff, - (unsigned char) ((w) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff), - (unsigned char) ((h) >> 8) & 0xff, - (unsigned char) ((h) >> 16) & 0xff, - (unsigned char) ((h) >> 24) & 0xff); - fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); - fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, - (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - - if (image->comps[0].prec > 8) { - adjustR = image->comps[0].prec - 8; - printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); - } - else - adjustR = 0; - if (image->comps[1].prec > 8) { - adjustG = image->comps[1].prec - 8; - printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec); - } - else - adjustG = 0; - if (image->comps[2].prec > 8) { - adjustB = image->comps[2].prec - 8; - printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec); - } - else - adjustB = 0; - - for (i = 0; i < w * h; i++) { - unsigned char rc, gc, bc; - int r, g, b; - - r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; - r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - r = ((r >> adjustR)+((r >> (adjustR-1))%2)); - if(r > 255) r = 255; else if(r < 0) r = 0; - rc = (unsigned char)r; - - g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; - g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - g = ((g >> adjustG)+((g >> (adjustG-1))%2)); - if(g > 255) g = 255; else if(g < 0) g = 0; - gc = (unsigned char)g; - - b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; - b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); - b = ((b >> adjustB)+((b >> (adjustB-1))%2)); - if(b > 255) b = 255; else if(b < 0) b = 0; - bc = (unsigned char)b; - - fprintf(fdest, "%c%c%c", bc, gc, rc); - - if ((i + 1) % w == 0) { - for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */ - fprintf(fdest, "%c", 0); - } - } - fclose(fdest); - } else { /* Gray-scale */ - - /* -->> -->> -->> -->> - 8 bits non code (Gray scale) - <<-- <<-- <<-- <<-- */ - - fdest = fopen(outfile, "wb"); - w = image->comps[0].w; - h = image->comps[0].h; - - fprintf(fdest, "BM"); - - /* FILE HEADER */ - /* ------------- */ - fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff, - (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, - (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, - (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, - ((54 + 1024) >> 16) & 0xff, - ((54 + 1024) >> 24) & 0xff); - - /* INFO HEADER */ - /* ------------- */ - fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff), - (unsigned char) ((w) >> 8) & 0xff, - (unsigned char) ((w) >> 16) & 0xff, - (unsigned char) ((w) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff), - (unsigned char) ((h) >> 8) & 0xff, - (unsigned char) ((h) >> 16) & 0xff, - (unsigned char) ((h) >> 24) & 0xff); - fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); - fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff); - fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff, - (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff, - (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff, - (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); - fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); - - if (image->comps[0].prec > 8) { - adjustR = image->comps[0].prec - 8; - printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); - }else - adjustR = 0; - - for (i = 0; i < 256; i++) { - fprintf(fdest, "%c%c%c%c", i, i, i, 0); - } - - for (i = 0; i < w * h; i++) { - int r; - - r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; - r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - r = ((r >> adjustR)+((r >> (adjustR-1))%2)); - if(r > 255) r = 255; else if(r < 0) r = 0; - - fprintf(fdest, "%c", (unsigned char)r); - - if ((i + 1) % w == 0) { - for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */ - fprintf(fdest, "%c", 0); - } - } - fclose(fdest); - } - - return 0; + if (image->comps[0].prec < 8) { + fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec); + return 1; + } + if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx + && image->comps[1].dx == image->comps[2].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[1].dy == image->comps[2].dy + && image->comps[0].prec == image->comps[1].prec + && image->comps[1].prec == image->comps[2].prec) { + + /* -->> -->> -->> -->> + 24 bits color + <<-- <<-- <<-- <<-- */ + + fdest = fopen(outfile, "wb"); + if (!fdest) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); + return 1; + } + + w = (int)image->comps[0].w; + h = (int)image->comps[0].h; + + fprintf(fdest, "BM"); + + /* FILE HEADER */ + /* ------------- */ + fprintf(fdest, "%c%c%c%c", + (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff); + + /* INFO HEADER */ + /* ------------- */ + fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff), + (unsigned char) ((w) >> 8) & 0xff, + (unsigned char) ((w) >> 16) & 0xff, + (unsigned char) ((w) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff), + (unsigned char) ((h) >> 8) & 0xff, + (unsigned char) ((h) >> 16) & 0xff, + (unsigned char) ((h) >> 24) & 0xff); + fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); + fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff, + (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + + if (image->comps[0].prec > 8) { + adjustR = (int)image->comps[0].prec - 8; + printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); + } + else + adjustR = 0; + if (image->comps[1].prec > 8) { + adjustG = (int)image->comps[1].prec - 8; + printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec); + } + else + adjustG = 0; + if (image->comps[2].prec > 8) { + adjustB = (int)image->comps[2].prec - 8; + printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec); + } + else + adjustB = 0; + + for (i = 0; i < w * h; i++) { + unsigned char rc, gc, bc; + int r, g, b; + + r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + r = ((r >> adjustR)+((r >> (adjustR-1))%2)); + if(r > 255) r = 255; else if(r < 0) r = 0; + rc = (unsigned char)r; + + g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + g = ((g >> adjustG)+((g >> (adjustG-1))%2)); + if(g > 255) g = 255; else if(g < 0) g = 0; + gc = (unsigned char)g; + + b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + b = ((b >> adjustB)+((b >> (adjustB-1))%2)); + if(b > 255) b = 255; else if(b < 0) b = 0; + bc = (unsigned char)b; + + fprintf(fdest, "%c%c%c", bc, gc, rc); + + if ((i + 1) % w == 0) { + for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */ + fprintf(fdest, "%c", 0); + } + } + fclose(fdest); + } else { /* Gray-scale */ + + /* -->> -->> -->> -->> + 8 bits non code (Gray scale) + <<-- <<-- <<-- <<-- */ + + fdest = fopen(outfile, "wb"); + w = (int)image->comps[0].w; + h = (int)image->comps[0].h; + + fprintf(fdest, "BM"); + + /* FILE HEADER */ + /* ------------- */ + fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff, + (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff, + (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff, + (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff, + ((54 + 1024) >> 16) & 0xff, + ((54 + 1024) >> 24) & 0xff); + + /* INFO HEADER */ + /* ------------- */ + fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff), + (unsigned char) ((w) >> 8) & 0xff, + (unsigned char) ((w) >> 16) & 0xff, + (unsigned char) ((w) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff), + (unsigned char) ((h) >> 8) & 0xff, + (unsigned char) ((h) >> 16) & 0xff, + (unsigned char) ((h) >> 24) & 0xff); + fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); + fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff); + fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff, + (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff, + (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff, + (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); + fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff); + + if (image->comps[0].prec > 8) { + adjustR = (int)image->comps[0].prec - 8; + printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); + }else + adjustR = 0; + + for (i = 0; i < 256; i++) { + fprintf(fdest, "%c%c%c%c", i, i, i, 0); + } + + for (i = 0; i < w * h; i++) { + int r; + + r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + r = ((r >> adjustR)+((r >> (adjustR-1))%2)); + if(r > 255) r = 255; else if(r < 0) r = 0; + + fprintf(fdest, "%c", (unsigned char)r); + + if ((i + 1) % w == 0) { + for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */ + fprintf(fdest, "%c", 0); + } + } + fclose(fdest); + } + + return 0; } /* -->> -->> -->> -->> @@ -1204,263 +1226,295 @@ PGX IMAGE FORMAT static unsigned char readuchar(FILE * f) { - unsigned char c1; - if ( !fread(&c1, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - return c1; + unsigned char c1; + if ( !fread(&c1, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + return c1; } static unsigned short readushort(FILE * f, int bigendian) { - unsigned char c1, c2; - if ( !fread(&c1, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if ( !fread(&c2, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if (bigendian) - return (c1 << 8) + c2; - else - return (c2 << 8) + c1; + unsigned char c1, c2; + if ( !fread(&c1, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if ( !fread(&c2, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if (bigendian) + return (unsigned short)((c1 << 8) + c2); + else + return (unsigned short)((c2 << 8) + c1); } static unsigned int readuint(FILE * f, int bigendian) { - unsigned char c1, c2, c3, c4; - if ( !fread(&c1, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if ( !fread(&c2, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if ( !fread(&c3, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if ( !fread(&c4, 1, 1, f) ) - { - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - return 0; - } - if (bigendian) - return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4; - else - return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1; + unsigned char c1, c2, c3, c4; + if ( !fread(&c1, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if ( !fread(&c2, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if ( !fread(&c3, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if ( !fread(&c4, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } + if (bigendian) + return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(c3 << 8) + c4; + else + return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1; } opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { - FILE *f = NULL; - int w, h, prec; - int i, numcomps, max; - OPJ_COLOR_SPACE color_space; - opj_image_cmptparm_t cmptparm; /* maximum of 1 component */ - opj_image_t * image = NULL; - int adjustS, ushift, dshift, force8; - - char endian1,endian2,sign; - char signtmp[32]; - - char temp[32]; - int bigendian; - opj_image_comp_t *comp = NULL; - - numcomps = 1; - color_space = OPJ_CLRSPC_GRAY; - - memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t)); - - max = 0; - - f = fopen(filename, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !\n", filename); - return NULL; - } - - fseek(f, 0, SEEK_SET); - if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){ - fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n"); - return NULL; - } - - i=0; - sign='+'; - while (signtmp[i]!='\0') { - if (signtmp[i]=='-') sign='-'; - i++; - } - - fgetc(f); - if (endian1=='M' && endian2=='L') { - bigendian = 1; - } else if (endian2=='M' && endian1=='L') { - bigendian = 0; - } else { - fprintf(stderr, "Bad pgx header, please check input file\n"); - return NULL; - } - - /* initialize image component */ - - cmptparm.x0 = parameters->image_offset_x0; - cmptparm.y0 = parameters->image_offset_y0; - cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1; - cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1; - - if (sign == '-') { - cmptparm.sgnd = 1; - } else { - cmptparm.sgnd = 0; - } - if(prec < 8) - { - force8 = 1; - ushift = 8 - prec; dshift = prec - ushift; - if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0; - cmptparm.sgnd = 0; - prec = 8; - } - else ushift = dshift = force8 = adjustS = 0; - - cmptparm.prec = prec; - cmptparm.bpp = prec; - cmptparm.dx = parameters->subsampling_dx; - cmptparm.dy = parameters->subsampling_dy; - - /* create the image */ - image = opj_image_create(numcomps, &cmptparm, color_space); - if(!image) { - fclose(f); - return NULL; - } - /* set image offset and reference grid */ - image->x0 = cmptparm.x0; - image->y0 = cmptparm.x0; - image->x1 = cmptparm.w; - image->y1 = cmptparm.h; - - /* set image data */ - - comp = &image->comps[0]; - - for (i = 0; i < w * h; i++) { - int v; - if(force8) - { - v = readuchar(f) + adjustS; - v = (v<<ushift) + (v>>dshift); - comp->data[i] = (unsigned char)v; + FILE *f = NULL; + int w, h, prec; + int i, numcomps, max; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm; /* maximum of 1 component */ + opj_image_t * image = NULL; + int adjustS, ushift, dshift, force8; - if(v > max) max = v; + char endian1,endian2,sign; + char signtmp[32]; - continue; - } - if (comp->prec == 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, bigendian); - } else { - v = (short) readushort(f, bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, bigendian); - } else { - v = (int) readuint(f, bigendian); - } - } - if (v > max) - max = v; - comp->data[i] = v; - } - fclose(f); - comp->bpp = int_floorlog2(max) + 1; - - return image; + char temp[32]; + int bigendian; + opj_image_comp_t *comp = NULL; + + numcomps = 1; + color_space = OPJ_CLRSPC_GRAY; + + memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t)); + + max = 0; + + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "Failed to open %s for reading !\n", filename); + return NULL; + } + + fseek(f, 0, SEEK_SET); + if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){ + fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n"); + return NULL; + } + + i=0; + sign='+'; + while (signtmp[i]!='\0') { + if (signtmp[i]=='-') sign='-'; + i++; + } + + fgetc(f); + if (endian1=='M' && endian2=='L') { + bigendian = 1; + } else if (endian2=='M' && endian1=='L') { + bigendian = 0; + } else { + fprintf(stderr, "Bad pgx header, please check input file\n"); + return NULL; + } + + /* initialize image component */ + + cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0; + cmptparm.y0 = (OPJ_UINT32)parameters->image_offset_y0; + cmptparm.w = !cmptparm.x0 ? (OPJ_UINT32)((w - 1) * parameters->subsampling_dx + 1) : cmptparm.x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)parameters->subsampling_dx + 1; + cmptparm.h = !cmptparm.y0 ? (OPJ_UINT32)((h - 1) * parameters->subsampling_dy + 1) : cmptparm.y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)parameters->subsampling_dy + 1; + + if (sign == '-') { + cmptparm.sgnd = 1; + } else { + cmptparm.sgnd = 0; + } + if(prec < 8) + { + force8 = 1; + ushift = 8 - prec; dshift = prec - ushift; + if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0; + cmptparm.sgnd = 0; + prec = 8; + } + else ushift = dshift = force8 = adjustS = 0; + + cmptparm.prec = (OPJ_UINT32)prec; + cmptparm.bpp = (OPJ_UINT32)prec; + cmptparm.dx = (OPJ_UINT32)parameters->subsampling_dx; + cmptparm.dy = (OPJ_UINT32)parameters->subsampling_dy; + + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm, color_space); + if(!image) { + fclose(f); + return NULL; + } + /* set image offset and reference grid */ + image->x0 = cmptparm.x0; + image->y0 = cmptparm.x0; + image->x1 = cmptparm.w; + image->y1 = cmptparm.h; + + /* set image data */ + + comp = &image->comps[0]; + + for (i = 0; i < w * h; i++) { + int v; + if(force8) + { + v = readuchar(f) + adjustS; + v = (v<<ushift) + (v>>dshift); + comp->data[i] = (unsigned char)v; + + if(v > max) max = v; + + continue; + } + if (comp->prec == 8) { + if (!comp->sgnd) { + v = readuchar(f); + } else { + v = (char) readuchar(f); + } + } else if (comp->prec <= 16) { + if (!comp->sgnd) { + v = readushort(f, bigendian); + } else { + v = (short) readushort(f, bigendian); + } + } else { + if (!comp->sgnd) { + v = (int)readuint(f, bigendian); + } else { + v = (int) readuint(f, bigendian); + } + } + if (v > max) + max = v; + comp->data[i] = v; + } + fclose(f); + comp->bpp = (OPJ_UINT32)int_floorlog2(max) + 1; + + return image; } -int imagetopgx(opj_image_t * image, const char *outfile) { - int w, h; - int i, j, compno; - FILE *fdest = NULL; +#define CLAMP(x,a,b) x < a ? a : (x > b ? b : x) + +static INLINE int clamp( const int value, const int prec, const int sgnd ) +{ + if( sgnd ) + { + if (prec <= 8) return CLAMP(value,-128,127); + else if (prec <= 16) return CLAMP(value,-32768,32767); + else return CLAMP(value,-2147483647-1,2147483647); + } + else + { + if (prec <= 8) return CLAMP(value,0,255); + else if (prec <= 16) return CLAMP(value,0,65535); + else return value; /*CLAMP(value,0,4294967295);*/ + } +} - for (compno = 0; compno < image->numcomps; compno++) { - opj_image_comp_t *comp = &image->comps[compno]; - char bname[256]; /* buffer for name */ +int imagetopgx(opj_image_t * image, const char *outfile) +{ + int w, h; + int i, j, fails = 1; + unsigned int compno; + FILE *fdest = NULL; + + for (compno = 0; compno < image->numcomps; compno++) + { + opj_image_comp_t *comp = &image->comps[compno]; + char bname[256]; /* buffer for name */ char *name = bname; /* pointer */ int nbytes = 0; size_t res; const size_t olen = strlen(outfile); const size_t dotpos = olen - 4; const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ - if( outfile[dotpos] != '.' ) { + + if( outfile[dotpos] != '.' ) + { /* `pgx` was recognized but there is no dot at expected position */ fprintf(stderr, "ERROR -> Impossible happen." ); - return 1; + goto fin; } - if( total > 256 ) { + if( total > 256 ) + { name = (char*)malloc(total+1); } strncpy(name, outfile, dotpos); - /*if (image->numcomps > 1) {*/ - sprintf(name+dotpos, "_%d.pgx", compno); - /*} else { - strcpy(name+dotpos, ".pgx"); - }*/ - fdest = fopen(name, "wb"); - if (!fdest) { - fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); - return 1; - } + sprintf(name+dotpos, "_%d.pgx", compno); + fdest = fopen(name, "wb"); /* dont need name anymore */ - if( total > 256 ) { - free(name); + if( total > 256 ) free(name); + if (!fdest) + { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); + goto fin; } - w = image->comps[compno].w; - h = image->comps[compno].h; - - fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h); - if (comp->prec <= 8) { - nbytes = 1; - } else if (comp->prec <= 16) { - nbytes = 2; - } else { - nbytes = 4; - } - for (i = 0; i < w * h; i++) { - int v = image->comps[compno].data[i]; - for (j = nbytes - 1; j >= 0; j--) { - char byte = (char) (v >> (j * 8)); - res = fwrite(&byte, 1, 1, fdest); - if( res < 1 ) { + w = (int)image->comps[compno].w; + h = (int)image->comps[compno].h; + + fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, + w, h); + + if (comp->prec <= 8) + nbytes = 1; + else if (comp->prec <= 16) + nbytes = 2; + else + nbytes = 4; + + for (i = 0; i < w * h; i++) + { + /* FIXME: clamp func is being called within a loop */ + const int val = clamp(image->comps[compno].data[i], + (int)comp->prec, (int)comp->sgnd); + + for (j = nbytes - 1; j >= 0; j--) + { + int v = (int)(val >> (j * 8)); + unsigned char byte = (unsigned char)v; + res = fwrite(&byte, 1, 1, fdest); + + if( res < 1 ) + { fprintf(stderr, "failed to write 1 byte for %s\n", name); - return 1; + goto fin; + } } - } - } - fclose(fdest); - } + } + fclose(fdest); fdest = NULL; + } + fails = 0; +fin: + if(fdest) fclose(fdest); - return 0; + return fails; } /* -->> -->> -->> -->> @@ -1479,11 +1533,11 @@ struct pnm_header static char *skip_white(char *s) { while(*s) - { - if(*s == '\n' || *s == '\r') return NULL; - if(isspace(*s)) { ++s; continue; } - return s; - } + { + if(*s == '\n' || *s == '\r') return NULL; + if(isspace(*s)) { ++s; continue; } + return s; + } return NULL; } @@ -1499,10 +1553,10 @@ static char *skip_int(char *start, int *out_n) start = s; while(*s) - { - if( !isdigit(*s)) break; - ++s; - } + { + if( !isdigit(*s)) break; + ++s; + } c = *s; *s = 0; *out_n = atoi(start); *s = c; return s; } @@ -1517,10 +1571,10 @@ static char *skip_idf(char *start, char out_idf[256]) start = s; while(*s) - { - if(isalpha(*s) || *s == '_') { ++s; continue; } - break; - } + { + if(isalpha(*s) || *s == '_') { ++s; continue; } + break; + } c = *s; *s = 0; strncpy(out_idf, start, 255); *s = c; return s; } @@ -1534,149 +1588,149 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) if (fgets(line, 250, reader) == NULL) { - fprintf(stderr,"\nWARNING: fgets return a NULL value"); - return; + fprintf(stderr,"\nWARNING: fgets return a NULL value"); + return; } if(line[0] != 'P') - { - fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return; - } + { + fprintf(stderr,"read_pnm_header:PNM:magic P missing\n"); return; + } format = atoi(line + 1); if(format < 1 || format > 7) - { - fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format); - return; - } + { + fprintf(stderr,"read_pnm_header:magic format %d invalid\n", format); + return; + } ph->format = format; ttype = end = have_wh = 0; while(fgets(line, 250, reader)) - { - if(*line == '#') continue; + { + if(*line == '#') continue; - s = line; + s = line; - if(format == 7) - { - s = skip_idf(s, idf); + if(format == 7) + { + s = skip_idf(s, idf); - if(s == NULL || *s == 0) return; + if(s == NULL || *s == 0) return; - if(strcmp(idf, "ENDHDR") == 0) - { - end = 1; break; - } - if(strcmp(idf, "WIDTH") == 0) - { - s = skip_int(s, &ph->width); - if(s == NULL || *s == 0) return; + if(strcmp(idf, "ENDHDR") == 0) + { + end = 1; break; + } + if(strcmp(idf, "WIDTH") == 0) + { + s = skip_int(s, &ph->width); + if(s == NULL || *s == 0) return; - continue; - } - if(strcmp(idf, "HEIGHT") == 0) - { - s = skip_int(s, &ph->height); - if(s == NULL || *s == 0) return; + continue; + } + if(strcmp(idf, "HEIGHT") == 0) + { + s = skip_int(s, &ph->height); + if(s == NULL || *s == 0) return; - continue; - } - if(strcmp(idf, "DEPTH") == 0) - { - s = skip_int(s, &ph->depth); - if(s == NULL || *s == 0) return; + continue; + } + if(strcmp(idf, "DEPTH") == 0) + { + s = skip_int(s, &ph->depth); + if(s == NULL || *s == 0) return; - continue; - } - if(strcmp(idf, "MAXVAL") == 0) - { - s = skip_int(s, &ph->maxval); - if(s == NULL || *s == 0) return; + continue; + } + if(strcmp(idf, "MAXVAL") == 0) + { + s = skip_int(s, &ph->maxval); + if(s == NULL || *s == 0) return; - continue; - } - if(strcmp(idf, "TUPLTYPE") == 0) - { - s = skip_idf(s, type); - if(s == NULL || *s == 0) return; - - if(strcmp(type, "BLACKANDWHITE") == 0) - { - ph->bw = 1; ttype = 1; continue; - } - if(strcmp(type, "GRAYSCALE") == 0) - { - ph->gray = 1; ttype = 1; continue; - } - if(strcmp(type, "GRAYSCALE_ALPHA") == 0) - { - ph->graya = 1; ttype = 1; continue; - } - if(strcmp(type, "RGB") == 0) - { - ph->rgb = 1; ttype = 1; continue; - } - if(strcmp(type, "RGB_ALPHA") == 0) - { - ph->rgba = 1; ttype = 1; continue; - } - fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type); - return; - } - fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf); - return; - } /* if(format == 7) */ + continue; + } + if(strcmp(idf, "TUPLTYPE") == 0) + { + s = skip_idf(s, type); + if(s == NULL || *s == 0) return; + + if(strcmp(type, "BLACKANDWHITE") == 0) + { + ph->bw = 1; ttype = 1; continue; + } + if(strcmp(type, "GRAYSCALE") == 0) + { + ph->gray = 1; ttype = 1; continue; + } + if(strcmp(type, "GRAYSCALE_ALPHA") == 0) + { + ph->graya = 1; ttype = 1; continue; + } + if(strcmp(type, "RGB") == 0) + { + ph->rgb = 1; ttype = 1; continue; + } + if(strcmp(type, "RGB_ALPHA") == 0) + { + ph->rgba = 1; ttype = 1; continue; + } + fprintf(stderr,"read_pnm_header:unknown P7 TUPLTYPE %s\n",type); + return; + } + fprintf(stderr,"read_pnm_header:unknown P7 idf %s\n",idf); + return; + } /* if(format == 7) */ - if( !have_wh) - { - s = skip_int(s, &ph->width); + if( !have_wh) + { + s = skip_int(s, &ph->width); - s = skip_int(s, &ph->height); + s = skip_int(s, &ph->height); - have_wh = 1; + have_wh = 1; - if(format == 1 || format == 4) break; + if(format == 1 || format == 4) break; - continue; - } - if(format == 2 || format == 3 || format == 5 || format == 6) - { -/* P2, P3, P5, P6: */ - s = skip_int(s, &ph->maxval); + continue; + } + if(format == 2 || format == 3 || format == 5 || format == 6) + { + /* P2, P3, P5, P6: */ + s = skip_int(s, &ph->maxval); - if(ph->maxval > 65535) return; - } - break; - }/* while(fgets( ) */ + if(ph->maxval > 65535) return; + } + break; + }/* while(fgets( ) */ if(format == 2 || format == 3 || format > 4) - { - if(ph->maxval < 1 || ph->maxval > 65535) return; - } + { + if(ph->maxval < 1 || ph->maxval > 65535) return; + } if(ph->width < 1 || ph->height < 1) return; if(format == 7) - { - if(!end) - { - fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return; - } - if(ph->depth < 1 || ph->depth > 4) return; + { + if(!end) + { + fprintf(stderr,"read_pnm_header:P7 without ENDHDR\n"); return; + } + if(ph->depth < 1 || ph->depth > 4) return; - if(ph->width && ph->height && ph->depth & ph->maxval && ttype) - ph->ok = 1; - } - else - { - if(format != 1 && format != 4) - { - if(ph->width && ph->height && ph->maxval) ph->ok = 1; - } + if(ph->width && ph->height && ph->depth && ph->maxval && ttype) + ph->ok = 1; + } else - { - if(ph->width && ph->height) ph->ok = 1; - ph->maxval = 255; - } - } + { + if(format != 1 && format != 4) + { + if(ph->width && ph->height && ph->maxval) ph->ok = 1; + } + else + { + if(ph->width && ph->height) ph->ok = 1; + ph->maxval = 255; + } + } } static int has_prec(int val) @@ -1700,31 +1754,31 @@ static int has_prec(int val) } opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - - FILE *fp = NULL; - int i, compno, numcomps, w, h, prec, format; - OPJ_COLOR_SPACE color_space; - opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */ - opj_image_t * image = NULL; - struct pnm_header header_info; - - if((fp = fopen(filename, "rb")) == NULL) - { - fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename); - return NULL; - } - memset(&header_info, 0, sizeof(struct pnm_header)); + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + + FILE *fp = NULL; + int i, compno, numcomps, w, h, prec, format; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm[4]; /* RGBA: max. 4 components */ + opj_image_t * image = NULL; + struct pnm_header header_info; + + if((fp = fopen(filename, "rb")) == NULL) + { + fprintf(stderr, "pnmtoimage:Failed to open %s for reading!\n",filename); + return NULL; + } + memset(&header_info, 0, sizeof(struct pnm_header)); - read_pnm_header(fp, &header_info); + read_pnm_header(fp, &header_info); - if(!header_info.ok) { fclose(fp); return NULL; } + if(!header_info.ok) { fclose(fp); return NULL; } - format = header_info.format; + format = header_info.format; switch(format) - { + { case 1: /* ascii bitmap */ case 4: /* raw bitmap */ numcomps = 1; @@ -1742,141 +1796,145 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { case 7: /* arbitrary map */ numcomps = header_info.depth; - break; + break; default: fclose(fp); return NULL; - } + } if(numcomps < 3) - color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */ + color_space = OPJ_CLRSPC_GRAY;/* GRAY, GRAYA */ else - color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */ + color_space = OPJ_CLRSPC_SRGB;/* RGB, RGBA */ prec = has_prec(header_info.maxval); - if(prec < 8) prec = 8; + if(prec < 8) prec = 8; w = header_info.width; h = header_info.height; subsampling_dx = parameters->subsampling_dx; subsampling_dy = parameters->subsampling_dy; - memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t)); + memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t)); for(i = 0; i < numcomps; i++) - { - cmptparm[i].prec = prec; - cmptparm[i].bpp = prec; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = w; - cmptparm[i].h = h; - } - image = opj_image_create(numcomps, &cmptparm[0], color_space); + { + cmptparm[i].prec = (OPJ_UINT32)prec; + cmptparm[i].bpp = (OPJ_UINT32)prec; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)w; + cmptparm[i].h = (OPJ_UINT32)h; + } + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); if(!image) { fclose(fp); return NULL; } -/* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1; - image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1; + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = (OPJ_UINT32)(parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1); + image->y1 = (OPJ_UINT32)(parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1); if((format == 2) || (format == 3)) /* ascii pixmap */ - { - unsigned int index; + { + unsigned int index; - for (i = 0; i < w * h; i++) - { - for(compno = 0; compno < numcomps; compno++) - { - index = 0; - if (fscanf(fp, "%u", &index) != 1) - fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); + for (i = 0; i < w * h; i++) + { + for(compno = 0; compno < numcomps; compno++) + { + index = 0; + if (fscanf(fp, "%u", &index) != 1) + fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); - image->comps[compno].data[i] = (index * 255)/header_info.maxval; - } - } - } + image->comps[compno].data[i] = (OPJ_INT32)(index * 255)/header_info.maxval; + } + } + } else - if((format == 5) - || (format == 6) - ||((format == 7) - && ( header_info.gray || header_info.graya - || header_info.rgb || header_info.rgba)))/* binary pixmap */ - { - unsigned char c0, c1, one; - - one = (prec < 9); - - for (i = 0; i < w * h; i++) - { - for(compno = 0; compno < numcomps; compno++) - { - if ( !fread(&c0, 1, 1, fp) ) - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - if(one) - { - image->comps[compno].data[i] = c0; - } + if((format == 5) + || (format == 6) + ||((format == 7) + && ( header_info.gray || header_info.graya + || header_info.rgb || header_info.rgba)))/* binary pixmap */ + { + unsigned char c0, c1, one; + + one = (prec < 9); + + for (i = 0; i < w * h; i++) + { + for(compno = 0; compno < numcomps; compno++) + { + if ( !fread(&c0, 1, 1, fp) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + opj_image_destroy(image); + return NULL; + } + if(one) + { + image->comps[compno].data[i] = c0; + } + else + { + if ( !fread(&c1, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + /* netpbm: */ + image->comps[compno].data[i] = ((c0<<8) | c1); + } + } + } + } else - { - if ( !fread(&c1, 1, 1, fp) ) - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); -/* netpbm: */ - image->comps[compno].data[i] = ((c0<<8) | c1); - } - } - } - } - else - if(format == 1) /* ascii bitmap */ - { - for (i = 0; i < w * h; i++) - { - unsigned int index; - - if ( fscanf(fp, "%u", &index) != 1) - fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); - - image->comps[0].data[i] = (index?0:255); - } - } - else - if(format == 4) - { - int x, y, bit; - unsigned char uc; + if(format == 1) /* ascii bitmap */ + { + for (i = 0; i < w * h; i++) + { + unsigned int index; - i = 0; - for(y = 0; y < h; ++y) - { - bit = -1; uc = 0; + if ( fscanf(fp, "%u", &index) != 1) + fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); - for(x = 0; x < w; ++x) - { - if(bit == -1) - { - bit = 7; - uc = (unsigned char)getc(fp); - } - image->comps[0].data[i] = (((uc>>bit) & 1)?0:255); - --bit; ++i; - } - } - } - else - if((format == 7 && header_info.bw)) /*MONO*/ - { - unsigned char uc; - - for(i = 0; i < w * h; ++i) - { - if ( !fread(&uc, 1, 1, fp) ) - fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); - image->comps[0].data[i] = (uc & 1)?0:255; - } - } + image->comps[0].data[i] = (index?0:255); + } + } + else + if(format == 4) + { + int x, y, bit; + unsigned char uc; + + i = 0; + for(y = 0; y < h; ++y) + { + bit = -1; uc = 0; + + for(x = 0; x < w; ++x) + { + if(bit == -1) + { + bit = 7; + uc = (unsigned char)getc(fp); + } + image->comps[0].data[i] = (((uc>>bit) & 1)?0:255); + --bit; ++i; + } + } + } + else + if((format == 7 && header_info.bw)) /*MONO*/ + { + unsigned char uc; + + for(i = 0; i < w * h; ++i) + { + if ( !fread(&uc, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + image->comps[0].data[i] = (uc & 1)?0:255; + } + } fclose(fp); return image; @@ -1884,563 +1942,633 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { int imagetopnm(opj_image_t * image, const char *outfile) { - int *red, *green, *blue, *alpha; - int wr, hr, max; - int i, compno, ncomp; - int adjustR, adjustG, adjustB, adjustA; - int fails, two, want_gray, has_alpha, triple; - int prec, v; - FILE *fdest = NULL; - const char *tmp = outfile; - char *destname; - alpha = NULL; - if((prec = image->comps[0].prec) > 16) - { - fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16" - "\n\t: refused.\n",__FILE__,__LINE__,prec); - return 1; - } + int *red, *green, *blue, *alpha; + int wr, hr, max; + int i; + unsigned int compno, ncomp; + int adjustR, adjustG, adjustB, adjustA; + int fails, two, want_gray, has_alpha, triple; + int prec, v; + FILE *fdest = NULL; + const char *tmp = outfile; + char *destname; + + alpha = NULL; + + if((prec = (int)image->comps[0].prec) > 16) + { + fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16" + "\n\t: refused.\n",__FILE__,__LINE__,prec); + return 1; + } two = has_alpha = 0; fails = 1; - ncomp = image->numcomps; - - while (*tmp) ++tmp; tmp -= 2; - want_gray = (*tmp == 'g' || *tmp == 'G'); - ncomp = image->numcomps; - - if(want_gray) ncomp = 1; - - if (ncomp == 2 /* GRAYA */ - || (ncomp > 2 /* RGB, RGBA */ - && image->comps[0].dx == image->comps[1].dx - && image->comps[1].dx == image->comps[2].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[1].dy == image->comps[2].dy - && image->comps[0].prec == image->comps[1].prec - && image->comps[1].prec == image->comps[2].prec - )) - { - fdest = fopen(outfile, "wb"); + ncomp = image->numcomps; + + while (*tmp) ++tmp; tmp -= 2; + want_gray = (*tmp == 'g' || *tmp == 'G'); + ncomp = image->numcomps; + + if(want_gray) ncomp = 1; + + if (ncomp == 2 /* GRAYA */ + || (ncomp > 2 /* RGB, RGBA */ + && image->comps[0].dx == image->comps[1].dx + && image->comps[1].dx == image->comps[2].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[1].dy == image->comps[2].dy + && image->comps[0].prec == image->comps[1].prec + && image->comps[1].prec == image->comps[2].prec + )) + { + fdest = fopen(outfile, "wb"); - if (!fdest) - { - fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); - return fails; - } - two = (prec > 8); - triple = (ncomp > 2); - wr = image->comps[0].w; hr = image->comps[0].h; - max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2); + if (!fdest) + { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); + return fails; + } + two = (prec > 8); + triple = (ncomp > 2); + wr = (int)image->comps[0].w; hr = (int)image->comps[0].h; + max = (1<<prec) - 1; has_alpha = (ncomp == 4 || ncomp == 2); - red = image->comps[0].data; + red = image->comps[0].data; - if(triple) - { - green = image->comps[1].data; - blue = image->comps[2].data; - } - else green = blue = NULL; - - if(has_alpha) - { - const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA"); - - fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n" - "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(), - wr, hr, ncomp, max, tt); - alpha = image->comps[ncomp - 1].data; - adjustA = (image->comps[ncomp - 1].sgnd ? - 1 << (image->comps[ncomp - 1].prec - 1) : 0); - } - else - { - fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", - opj_version(), wr, hr, max); - adjustA = 0; - } - adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + if(triple) + { + green = image->comps[1].data; + blue = image->comps[2].data; + } + else green = blue = NULL; - if(triple) - { - adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); - } - else adjustG = adjustB = 0; + if(has_alpha) + { + const char *tt = (triple?"RGB_ALPHA":"GRAYSCALE_ALPHA"); + + fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH %d\n" + "MAXVAL %d\nTUPLTYPE %s\nENDHDR\n", opj_version(), + wr, hr, ncomp, max, tt); + alpha = image->comps[ncomp - 1].data; + adjustA = (image->comps[ncomp - 1].sgnd ? + 1 << (image->comps[ncomp - 1].prec - 1) : 0); + } + else + { + fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", + opj_version(), wr, hr, max); + adjustA = 0; + } + adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - for(i = 0; i < wr * hr; ++i) - { - if(two) - { - v = *red + adjustR; ++red; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + if(triple) + { + adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + } + else adjustG = adjustB = 0; - if(triple) - { - v = *green + adjustG; ++green; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + for(i = 0; i < wr * hr; ++i) + { + if(two) + { + v = *red + adjustR; ++red; +if(v > 65535) v = 65535; else if(v < 0) v = 0; - v = *blue + adjustB; ++blue; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - }/* if(triple) */ + if(triple) + { + v = *green + adjustG; ++green; +if(v > 65535) v = 65535; else if(v < 0) v = 0; - if(has_alpha) - { - v = *alpha + adjustA; ++alpha; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - } - continue; + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - } /* if(two) */ + v = *blue + adjustB; ++blue; +if(v > 65535) v = 65535; else if(v < 0) v = 0; -/* prec <= 8: */ + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); - fprintf(fdest, "%c", (unsigned char)*red++); - if(triple) - fprintf(fdest, "%c%c",(unsigned char)*green++, (unsigned char)*blue++); + }/* if(triple) */ - if(has_alpha) - fprintf(fdest, "%c", (unsigned char)*alpha++); + if(has_alpha) + { + v = *alpha + adjustA; ++alpha; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - } /* for(i */ + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + } + continue; - fclose(fdest); return 0; - } + } /* if(two) */ -/* YUV or MONO: */ + /* prec <= 8: */ + v = *red++; + if(v > 255) v = 255; else if(v < 0) v = 0; - if (image->numcomps > ncomp) - { - fprintf(stderr,"WARNING -> [PGM file] Only the first component\n"); - fprintf(stderr," is written to the file\n"); - } - destname = (char*)malloc(strlen(outfile) + 8); + fprintf(fdest, "%c", (unsigned char)v); + if(triple) + { + v = *green++; + if(v > 255) v = 255; else if(v < 0) v = 0; - for (compno = 0; compno < ncomp; compno++) - { - if (ncomp > 1) - sprintf(destname, "%d.%s", compno, outfile); - else - sprintf(destname, "%s", outfile); + fprintf(fdest, "%c", (unsigned char)v); + v = *blue++; + if(v > 255) v = 255; else if(v < 0) v = 0; - fdest = fopen(destname, "wb"); - if (!fdest) - { - fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname); - free(destname); - return 1; - } - wr = image->comps[compno].w; hr = image->comps[compno].h; - prec = image->comps[compno].prec; - max = (1<<prec) - 1; + fprintf(fdest, "%c", (unsigned char)v); + } + if(has_alpha) + { + v = *alpha++; + if(v > 255) v = 255; else if(v < 0) v = 0; - fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n", - opj_version(), wr, hr, max); + fprintf(fdest, "%c", (unsigned char)v); + } + } /* for(i */ - red = image->comps[compno].data; - adjustR = - (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); + fclose(fdest); return 0; + } - if(prec > 8) - { - for (i = 0; i < wr * hr; i++) - { - v = *red + adjustR; ++red; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + /* YUV or MONO: */ - if(has_alpha) + if (image->numcomps > ncomp) + { + fprintf(stderr,"WARNING -> [PGM file] Only the first component\n"); + fprintf(stderr," is written to the file\n"); + } + destname = (char*)malloc(strlen(outfile) + 8); + + for (compno = 0; compno < ncomp; compno++) + { + if (ncomp > 1) { - v = *alpha++; -/* netpbm: */ - fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + /*sprintf(destname, "%d.%s", compno, outfile);*/ + const size_t olen = strlen(outfile); + const size_t dotpos = olen - 4; + + strncpy(destname, outfile, dotpos); + sprintf(destname+dotpos, "_%d.pgm", compno); } - }/* for(i */ - } - else /* prec <= 8 */ - { - for(i = 0; i < wr * hr; ++i) - { - fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red; - } - } - fclose(fdest); - } /* for (compno */ - free(destname); + else + sprintf(destname, "%s", outfile); + + fdest = fopen(destname, "wb"); + if (!fdest) + { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname); + free(destname); + return 1; + } + wr = (int)image->comps[compno].w; hr = (int)image->comps[compno].h; + prec = (int)image->comps[compno].prec; + max = (1<<prec) - 1; + + fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n", + opj_version(), wr, hr, max); + + red = image->comps[compno].data; + adjustR = + (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); + + if(prec > 8) + { + for (i = 0; i < wr * hr; i++) + { + v = *red + adjustR; ++red; +if(v > 65535) v = 65535; else if(v < 0) v = 0; + + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + + if(has_alpha) + { + v = *alpha++; +if(v > 65535) v = 65535; else if(v < 0) v = 0; + + /* netpbm: */ + fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); + } + }/* for(i */ + } + else /* prec <= 8 */ + { + for(i = 0; i < wr * hr; ++i) + { + v = *red + adjustR; ++red; + if(v > 255) v = 255; else if(v < 0) v = 0; - return 0; + fprintf(fdest, "%c", (unsigned char)v); + } + } + fclose(fdest); + } /* for (compno */ + free(destname); + + return 0; }/* imagetopnm() */ -#ifdef HAVE_LIBTIFF +#ifdef OPJ_HAVE_LIBTIFF /* -->> -->> -->> -->> - TIFF IMAGE FORMAT + TIFF IMAGE FORMAT <<-- <<-- <<-- <<-- */ int imagetotif(opj_image_t * image, const char *outfile) { - int width, height, imgsize; - int bps,index,adjust, sgnd; - int ushift, dshift, has_alpha, force16; - TIFF *tif; - tdata_t buf; - tstrip_t strip; - tsize_t strip_size; - - ushift = dshift = force16 = has_alpha = 0; - bps = image->comps[0].prec; - - if(bps > 8 && bps < 16) - { - ushift = 16 - bps; dshift = bps - ushift; - bps = 16; force16 = 1; - } - - if(bps != 8 && bps != 16) - { - fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n", - bps); - fprintf(stderr,"\tAborting\n"); - return 1; - } - tif = TIFFOpen(outfile, "wb"); - - if (!tif) - { - fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile); - return 1; - } - sgnd = image->comps[0].sgnd; - adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; - - if(image->numcomps >= 3 - && image->comps[0].dx == image->comps[1].dx - && image->comps[1].dx == image->comps[2].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[1].dy == image->comps[2].dy - && image->comps[0].prec == image->comps[1].prec - && image->comps[1].prec == image->comps[2].prec) - { - has_alpha = (image->numcomps == 4); - - width = image->comps[0].w; - height = image->comps[0].h; - imgsize = width * height ; - - TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); - TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); - TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha); - TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); - TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); - TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); - TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); - TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); - strip_size = TIFFStripSize(tif); - buf = _TIFFmalloc(strip_size); - index=0; - - for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) - { - unsigned char *dat8; - tsize_t i, ssize, last_i = 0; - int step, restx; - ssize = TIFFStripSize(tif); - dat8 = (unsigned char*)buf; - - if(bps == 8) - { - step = 3 + has_alpha; - restx = step - 1; - - for(i=0; i < ssize - restx; i += step) - { - int r, g, b, a = 0; - - if(index < imgsize) - { - r = image->comps[0].data[index]; - g = image->comps[1].data[index]; - b = image->comps[2].data[index]; - if(has_alpha) a = image->comps[3].data[index]; - - if(sgnd) - { - r += adjust; - g += adjust; - b += adjust; - if(has_alpha) a += adjust; - } - dat8[i+0] = r ; - dat8[i+1] = g ; - dat8[i+2] = b ; - if(has_alpha) dat8[i+3] = a; - - index++; - last_i = i + step; - } - else - break; - }/*for(i = 0;)*/ - - if(last_i < ssize) - { - for(i = last_i; i < ssize; i += step) - { - int r, g, b, a = 0; - - if(index < imgsize) - { - r = image->comps[0].data[index]; - g = image->comps[1].data[index]; - b = image->comps[2].data[index]; - if(has_alpha) a = image->comps[3].data[index]; - - if(sgnd) - { - r += adjust; - g += adjust; - b += adjust; - if(has_alpha) a += adjust; - } - dat8[i+0] = r ; - if(i+1 < ssize) dat8[i+1] = g ; else break; - if(i+2 < ssize) dat8[i+2] = b ; else break; - if(has_alpha) - { - if(i+3 < ssize) dat8[i+3] = a ; else break; - } - index++; - } - else - break; - }/*for(i)*/ - }/*if(last_i < ssize)*/ - - } /*if(bps == 8)*/ - else - if(bps == 16) - { - step = 6 + has_alpha + has_alpha; - restx = step - 1; - - for(i = 0; i < ssize - restx ; i += step) - { - int r, g, b, a = 0; + int width, height, imgsize; + int bps,index,adjust, sgnd; + int ushift, dshift, has_alpha, force16; + TIFF *tif; + tdata_t buf; + tstrip_t strip; + tsize_t strip_size; + + ushift = dshift = force16 = has_alpha = 0; + bps = (int)image->comps[0].prec; + + if(bps > 8 && bps < 16) + { + ushift = 16 - bps; dshift = bps - ushift; + bps = 16; force16 = 1; + } - if(index < imgsize) - { - r = image->comps[0].data[index]; - g = image->comps[1].data[index]; - b = image->comps[2].data[index]; - if(has_alpha) a = image->comps[3].data[index]; + if(bps != 8 && bps != 16) + { + fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits implemented\n", + bps); + fprintf(stderr,"\tAborting\n"); + return 1; + } + tif = TIFFOpen(outfile, "wb"); - if(sgnd) - { - r += adjust; - g += adjust; - b += adjust; - if(has_alpha) a += adjust; - } - if(force16) - { - r = (r<<ushift) + (r>>dshift); - g = (g<<ushift) + (g>>dshift); - b = (b<<ushift) + (b>>dshift); - if(has_alpha) a = (a<<ushift) + (a>>dshift); - } - dat8[i+0] = r;/*LSB*/ - dat8[i+1] = (r >> 8);/*MSB*/ - dat8[i+2] = g; - dat8[i+3] = (g >> 8); - dat8[i+4] = b; - dat8[i+5] = (b >> 8); + if (!tif) + { + fprintf(stderr, "imagetotif:failed to open %s for writing\n", outfile); + return 1; + } + sgnd = (int)image->comps[0].sgnd; + adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; + + if(image->numcomps >= 3 + && image->comps[0].dx == image->comps[1].dx + && image->comps[1].dx == image->comps[2].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[1].dy == image->comps[2].dy + && image->comps[0].prec == image->comps[1].prec + && image->comps[1].prec == image->comps[2].prec) + { + has_alpha = (image->numcomps == 4); + + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; + imgsize = width * height ; + + TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); + TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3 + has_alpha); + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); + TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); + TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); + TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); + strip_size = TIFFStripSize(tif); + buf = _TIFFmalloc(strip_size); + index=0; + + for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) + { + unsigned char *dat8; + tsize_t i, ssize, last_i = 0; + int step, restx; + ssize = TIFFStripSize(tif); + dat8 = (unsigned char*)buf; + + if(bps == 8) + { + step = 3 + has_alpha; + restx = step - 1; + + for(i=0; i < ssize - restx; i += step) + { + int r, g, b, a = 0; + + if(index < imgsize) + { + r = image->comps[0].data[index]; + g = image->comps[1].data[index]; + b = image->comps[2].data[index]; + if(has_alpha) a = image->comps[3].data[index]; + + if(sgnd) + { + r += adjust; + g += adjust; + b += adjust; + if(has_alpha) a += adjust; + } + if(r > 255) r = 255; else if(r < 0) r = 0; + dat8[i+0] = (unsigned char)r ; + if(g > 255) g = 255; else if(g < 0) g = 0; + dat8[i+1] = (unsigned char)g ; + if(b > 255) b = 255; else if(b < 0) b = 0; + dat8[i+2] = (unsigned char)b ; if(has_alpha) - { - dat8[i+6] = a; - dat8[i+7] = (a >> 8); - } - index++; - last_i = i + step; - } - else - break; - }/*for(i = 0;)*/ - - if(last_i < ssize) - { - for(i = last_i ; i < ssize ; i += step) - { - int r, g, b, a = 0; - - if(index < imgsize) - { - r = image->comps[0].data[index]; - g = image->comps[1].data[index]; - b = image->comps[2].data[index]; - if(has_alpha) a = image->comps[3].data[index]; - - if(sgnd) - { - r += adjust; - g += adjust; - b += adjust; - if(has_alpha) a += adjust; - } - if(force16) - { - r = (r<<ushift) + (r>>dshift); - g = (g<<ushift) + (g>>dshift); - b = (b<<ushift) + (b>>dshift); - if(has_alpha) a = (a<<ushift) + (a>>dshift); - } - dat8[i+0] = r;/*LSB*/ - if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/ - if(i+2 < ssize) dat8[i+2] = g; else break; - if(i+3 < ssize) dat8[i+3] = (g >> 8);else break; - if(i+4 < ssize) dat8[i+4] = b; else break; - if(i+5 < ssize) dat8[i+5] = (b >> 8);else break; - - if(has_alpha) - { - if(i+6 < ssize) dat8[i+6] = a; else break; - if(i+7 < ssize) dat8[i+7] = (a >> 8); else break; - } - index++; - } - else - break; - }/*for(i)*/ - }/*if(last_i < ssize)*/ - - }/*if(bps == 16)*/ - (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); - }/*for(strip = 0; )*/ - - _TIFFfree((void*)buf); - TIFFClose(tif); - - return 0; - }/*RGB(A)*/ - - if(image->numcomps == 1 /* GRAY */ - || ( image->numcomps == 2 /* GRAY_ALPHA */ - && image->comps[0].dx == image->comps[1].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[0].prec == image->comps[1].prec)) - { - int step; - - has_alpha = (image->numcomps == 2); - - width = image->comps[0].w; - height = image->comps[0].h; - imgsize = width * height; - -/* Set tags */ - TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); - TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); - TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha); - TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); - TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); - TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); - TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); - TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); - -/* Get a buffer for the data */ - strip_size = TIFFStripSize(tif); - buf = _TIFFmalloc(strip_size); - index = 0; - - for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) - { - unsigned char *dat8; - tsize_t i, ssize = TIFFStripSize(tif); - dat8 = (unsigned char*)buf; - - if(bps == 8) - { - step = 1 + has_alpha; - - for(i=0; i < ssize; i += step) - { - if(index < imgsize) - { - int r, a = 0; - - r = image->comps[0].data[index]; - if(has_alpha) a = image->comps[1].data[index]; - - if(sgnd) { - r += adjust; - if(has_alpha) a += adjust; - } - dat8[i+0] = r; - if(has_alpha) dat8[i+1] = a; - index++; + if(a > 255) a = 255; else if(a < 0) a = 0; + dat8[i+3] = (unsigned char)a; } - else - break; - }/*for(i )*/ - }/*if(bps == 8*/ - else - if(bps == 16) - { - step = 2 + has_alpha + has_alpha; - for(i=0; i < ssize; i += step) - { - if(index < imgsize) - { - int r, a = 0; - - r = image->comps[0].data[index]; - if(has_alpha) a = image->comps[1].data[index]; + index++; + last_i = i + step; + } + else + break; + }/*for(i = 0;)*/ + + if(last_i < ssize) + { + for(i = last_i; i < ssize; i += step) + { + int r, g, b, a = 0; + + if(index < imgsize) + { + r = image->comps[0].data[index]; + g = image->comps[1].data[index]; + b = image->comps[2].data[index]; + if(has_alpha) a = image->comps[3].data[index]; + + if(sgnd) + { + r += adjust; + g += adjust; + b += adjust; + if(has_alpha) a += adjust; + } + if(r > 255) r = 255; else if(r < 0) r = 0; + if(g > 255) g = 255; else if(g < 0) g = 0; + if(b > 255) b = 255; else if(b < 0) b = 0; + + dat8[i+0] = (unsigned char)r ; + if(i+1 < ssize) dat8[i+1] = (unsigned char)g ; else break; + if(i+2 < ssize) dat8[i+2] = (unsigned char)b ; else break; + if(has_alpha) + { + if(a > 255) a = 255; else if(a < 0) a = 0; + + if(i+3 < ssize) dat8[i+3] = (unsigned char)a ; else break; + } + index++; + } + else + break; + }/*for(i)*/ + }/*if(last_i < ssize)*/ + + } /*if(bps == 8)*/ + else + if(bps == 16) + { + step = 6 + has_alpha + has_alpha; + restx = step - 1; + + for(i = 0; i < ssize - restx ; i += step) + { + int r, g, b, a = 0; + + if(index < imgsize) + { + r = image->comps[0].data[index]; + g = image->comps[1].data[index]; + b = image->comps[2].data[index]; + if(has_alpha) a = image->comps[3].data[index]; + + if(sgnd) + { + r += adjust; + g += adjust; + b += adjust; + if(has_alpha) a += adjust; + } + if(force16) + { + r = (r<<ushift) + (r>>dshift); + g = (g<<ushift) + (g>>dshift); + b = (b<<ushift) + (b>>dshift); + if(has_alpha) a = (a<<ushift) + (a>>dshift); + } + if(r > 65535) r = 65535; else if(r < 0) r = 0; + if(g > 65535) g = 65535; else if(g < 0) g = 0; + if(b > 65535) b = 65535; else if(b < 0) b = 0; + + dat8[i+0] = (unsigned char)r;/*LSB*/ + dat8[i+1] = (unsigned char)(r >> 8);/*MSB*/ + dat8[i+2] = (unsigned char)g; + dat8[i+3] = (unsigned char)(g >> 8); + dat8[i+4] = (unsigned char)b; + dat8[i+5] = (unsigned char)(b >> 8); + if(has_alpha) + { + if(a > 65535) a = 65535; else if(a < 0) a = 0; + dat8[i+6] = (unsigned char)a; + dat8[i+7] = (unsigned char)(a >> 8); + } + index++; + last_i = i + step; + } + else + break; + }/*for(i = 0;)*/ + + if(last_i < ssize) + { + for(i = last_i ; i < ssize ; i += step) + { + int r, g, b, a = 0; + + if(index < imgsize) + { + r = image->comps[0].data[index]; + g = image->comps[1].data[index]; + b = image->comps[2].data[index]; + if(has_alpha) a = image->comps[3].data[index]; + + if(sgnd) + { + r += adjust; + g += adjust; + b += adjust; + if(has_alpha) a += adjust; + } + if(force16) + { + r = (r<<ushift) + (r>>dshift); + g = (g<<ushift) + (g>>dshift); + b = (b<<ushift) + (b>>dshift); + if(has_alpha) a = (a<<ushift) + (a>>dshift); + } + if(r > 65535) r = 65535; else if(r < 0) r = 0; + if(g > 65535) g = 65535; else if(g < 0) g = 0; + if(b > 65535) b = 65535; else if(b < 0) b = 0; + + dat8[i+0] = (unsigned char) r;/*LSB*/ + if(i+1 < ssize) dat8[i+1] = (unsigned char)(r >> 8);else break;/*MSB*/ + if(i+2 < ssize) dat8[i+2] = (unsigned char) g; else break; + if(i+3 < ssize) dat8[i+3] = (unsigned char)(g >> 8);else break; + if(i+4 < ssize) dat8[i+4] = (unsigned char) b; else break; + if(i+5 < ssize) dat8[i+5] = (unsigned char)(b >> 8);else break; + + if(has_alpha) + { + if(a > 65535) a = 65535; else if(a < 0) a = 0; + if(i+6 < ssize) dat8[i+6] = (unsigned char)a; else break; + if(i+7 < ssize) dat8[i+7] = (unsigned char)(a >> 8); else break; + } + index++; + } + else + break; + }/*for(i)*/ + }/*if(last_i < ssize)*/ + + }/*if(bps == 16)*/ + (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); + }/*for(strip = 0; )*/ + + _TIFFfree((void*)buf); + TIFFClose(tif); + + return 0; + }/*RGB(A)*/ + + if(image->numcomps == 1 /* GRAY */ + || ( image->numcomps == 2 /* GRAY_ALPHA */ + && image->comps[0].dx == image->comps[1].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[0].prec == image->comps[1].prec)) + { + int step; + + has_alpha = (image->numcomps == 2); + + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; + imgsize = width * height; + + /* Set tags */ + TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); + TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1 + has_alpha); + TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); + TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); + TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); + TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); + + /* Get a buffer for the data */ + strip_size = TIFFStripSize(tif); + buf = _TIFFmalloc(strip_size); + index = 0; + + for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) + { + unsigned char *dat8; + tsize_t i, ssize = TIFFStripSize(tif); + dat8 = (unsigned char*)buf; + + if(bps == 8) + { + step = 1 + has_alpha; + + for(i=0; i < ssize; i += step) + { + if(index < imgsize) + { + int r, a = 0; + + r = image->comps[0].data[index]; + if(has_alpha) a = image->comps[1].data[index]; + + if(sgnd) + { + r += adjust; + if(has_alpha) a += adjust; + } + if(r > 255) r = 255; else if(r < 0) r = 0; + dat8[i+0] = (unsigned char)r; - if(sgnd) - { - r += adjust; - if(has_alpha) a += adjust; - } - if(force16) - { - r = (r<<ushift) + (r>>dshift); - if(has_alpha) a = (a<<ushift) + (a>>dshift); - } - dat8[i+0] = r;/*LSB*/ - dat8[i+1] = r >> 8;/*MSB*/ - if(has_alpha) + if(has_alpha) { - dat8[i+2] = a; - dat8[i+3] = a >> 8; - } + if(a > 255) a = 255; else if(a < 0) a = 0; + dat8[i+1] = (unsigned char)a; + } index++; - }/*if(index < imgsize)*/ - else - break; - }/*for(i )*/ - } - (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); - }/*for(strip*/ - - _TIFFfree(buf); - TIFFClose(tif); - - return 0; - } + } + else + break; + }/*for(i )*/ + }/*if(bps == 8*/ + else + if(bps == 16) + { + step = 2 + has_alpha + has_alpha; + + for(i=0; i < ssize; i += step) + { + if(index < imgsize) + { + int r, a = 0; + + r = image->comps[0].data[index]; + if(has_alpha) a = image->comps[1].data[index]; + + if(sgnd) + { + r += adjust; + if(has_alpha) a += adjust; + } + if(force16) + { + r = (r<<ushift) + (r>>dshift); + if(has_alpha) a = (a<<ushift) + (a>>dshift); + } + if(r > 65535) r = 65535; else if(r < 0) r = 0; + dat8[i+0] = (unsigned char)r;/*LSB*/ + dat8[i+1] = (unsigned char)(r >> 8);/*MSB*/ + if(has_alpha) + { + if(a > 65535) a = 65535; else if(a < 0) a = 0; + dat8[i+2] = (unsigned char)a; + dat8[i+3] = (unsigned char)(a >> 8); + } + index++; + }/*if(index < imgsize)*/ + else + break; + }/*for(i )*/ + } + (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); + }/*for(strip*/ + + _TIFFfree(buf); + TIFFClose(tif); + + return 0; + } - TIFFClose(tif); + TIFFClose(tif); - fprintf(stderr,"imagetotif: Bad color format.\n" - "\tOnly RGB(A) and GRAY(A) has been implemented\n"); - fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n", - image->numcomps); + fprintf(stderr,"imagetotif: Bad color format.\n" + "\tOnly RGB(A) and GRAY(A) has been implemented\n"); + fprintf(stderr,"\tFOUND: numcomps(%d)\n\tAborting\n", + image->numcomps); - return 1; + return 1; }/* imagetotif() */ /* @@ -2449,643 +2577,627 @@ int imagetotif(opj_image_t * image, const char *outfile) */ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - TIFF *tif; - tdata_t buf; - tstrip_t strip; - tsize_t strip_size; - int j, numcomps, w, h,index; - OPJ_COLOR_SPACE color_space; - opj_image_cmptparm_t cmptparm[4]; /* RGBA */ - opj_image_t *image = NULL; - int imgsize = 0; - int has_alpha = 0; - unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; - unsigned int tiWidth, tiHeight; - - tif = TIFFOpen(filename, "r"); - - if(!tif) - { - fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename); - return 0; - } - tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0; - tiWidth = tiHeight = 0; - - TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth); - TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight); - TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps); - TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf); - TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); - TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); - TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); - w= tiWidth; - h= tiHeight; - - if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0; - if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0; + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + TIFF *tif; + tdata_t buf; + tstrip_t strip; + tsize_t strip_size; + int j, numcomps, w, h,index; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm[4]; /* RGBA */ + opj_image_t *image = NULL; + int imgsize = 0; + int has_alpha = 0; + unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; + unsigned int tiWidth, tiHeight; + + tif = TIFFOpen(filename, "r"); + + if(!tif) + { + fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename); + return 0; + } + tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0; + tiWidth = tiHeight = 0; + + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight); + TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps); + TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf); + TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); + TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); + TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); + w= (int)tiWidth; + h= (int)tiHeight; + + if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0; + if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0; if( !tiBps || !tiPhoto) - { - if( !tiBps) - fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits" - " implemented\n",tiBps); - else - if( !tiPhoto) - fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)" - " and GRAY(A) has been implemented\n",(int) tiPhoto); + { + if( !tiBps) + fprintf(stderr,"tiftoimage: Bits=%d, Only 8 and 16 bits" + " implemented\n",tiBps); + else + if( !tiPhoto) + fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)" + " and GRAY(A) has been implemented\n",(int) tiPhoto); - fprintf(stderr,"\tAborting\n"); - TIFFClose(tif); + fprintf(stderr,"\tAborting\n"); + TIFFClose(tif); - return NULL; - } + return NULL; + } - {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */ - uint16* sampleinfo; - uint16 extrasamples; + {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */ + uint16* sampleinfo; + uint16 extrasamples; - TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, - &extrasamples, &sampleinfo); + TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, + &extrasamples, &sampleinfo); - if(extrasamples >= 1) - { - switch(sampleinfo[0]) - { - case EXTRASAMPLE_UNSPECIFIED: -/* Workaround for some images without correct info about alpha channel + if(extrasamples >= 1) + { + switch(sampleinfo[0]) + { + case EXTRASAMPLE_UNSPECIFIED: + /* Workaround for some images without correct info about alpha channel */ - if(tiSpp > 3) - has_alpha = 1; - break; - - case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */ - case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */ - has_alpha = 1; - break; - } - } - else /* extrasamples == 0 */ - if(tiSpp == 4 || tiSpp == 2) has_alpha = 1; - } + if(tiSpp > 3) + has_alpha = 1; + break; + + case EXTRASAMPLE_ASSOCALPHA: /* data pre-multiplied */ + case EXTRASAMPLE_UNASSALPHA: /* data not pre-multiplied */ + has_alpha = 1; + break; + } + } + else /* extrasamples == 0 */ + if(tiSpp == 4 || tiSpp == 2) has_alpha = 1; + } -/* initialize image components + /* initialize image components */ - memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); - - if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */ - { - numcomps = 3 + has_alpha; - color_space = OPJ_CLRSPC_SRGB; + memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); + + if ((tiPhoto == PHOTOMETRIC_RGB) && (parameters->cp_cinema)) { + fprintf(stdout,"WARNING:\n" + "Input image bitdepth is %d bits\n" + "TIF conversion has automatically rescaled to 12-bits\n" + "to comply with cinema profiles.\n", + tiBps); + } -/*#define USETILEMODE*/ - for(j = 0; j < numcomps; j++) - { - if(parameters->cp_cinema) - { - cmptparm[j].prec = 12; - cmptparm[j].bpp = 12; - } - else - { - cmptparm[j].prec = tiBps; - cmptparm[j].bpp = tiBps; - } - cmptparm[j].dx = subsampling_dx; - cmptparm[j].dy = subsampling_dy; - cmptparm[j].w = w; - cmptparm[j].h = h; + if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */ + { + numcomps = 3 + has_alpha; + color_space = OPJ_CLRSPC_SRGB; + + /*#define USETILEMODE*/ + for(j = 0; j < numcomps; j++) + { + if(parameters->cp_cinema) + { + cmptparm[j].prec = 12; + cmptparm[j].bpp = 12; + } + else + { + cmptparm[j].prec = tiBps; + cmptparm[j].bpp = tiBps; + } + cmptparm[j].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[j].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[j].w = (OPJ_UINT32)w; + cmptparm[j].h = (OPJ_UINT32)h; #ifdef USETILEMODE - cmptparm[j].x0 = 0; - cmptparm[j].y0 = 0; + cmptparm[j].x0 = 0; + cmptparm[j].y0 = 0; #endif - } + } #ifdef USETILEMODE - image = opj_image_tile_create(numcomps,&cmptparm[0],color_space); + image = opj_image_tile_create(numcomps,&cmptparm[0],color_space); #else - image = opj_image_create(numcomps, &cmptparm[0], color_space); + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); #endif - if(!image) - { - TIFFClose(tif); - return NULL; - } -/* set image offset and reference grid + if(!image) + { + TIFFClose(tif); + return NULL; + } + /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : - image->x0 + (w - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : - image->y0 + (h - 1) * subsampling_dy + 1; - - buf = _TIFFmalloc(TIFFStripSize(tif)); - - strip_size=TIFFStripSize(tif); - index = 0; - imgsize = image->comps[0].w * image->comps[0].h ; -/* Read the Image components + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : + image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : + image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + buf = _TIFFmalloc(TIFFStripSize(tif)); + + strip_size=TIFFStripSize(tif); + index = 0; + imgsize = (int)(image->comps[0].w * image->comps[0].h); + /* Read the Image components */ - for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) - { - unsigned char *dat8; - int step; - tsize_t i, ssize; - ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); - dat8 = (unsigned char*)buf; - - if(tiBps == 16) - { - step = 6 + has_alpha + has_alpha; - - for(i = 0; i < ssize; i += step) - { - if(index < imgsize) - { - image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */ - image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */ - image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */ - if(has_alpha) - image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6]; - - if(parameters->cp_cinema) - { -/* Rounding 16 to 12 bits + for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) + { + unsigned char *dat8; + int step; + tsize_t i, ssize; + ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); + dat8 = (unsigned char*)buf; + + if(tiBps == 16) + { + step = 6 + has_alpha + has_alpha; + + for(i = 0; i < ssize; i += step) + { + if(index < imgsize) + { + image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */ + image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */ + image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */ + if(has_alpha) + image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6]; + + if(parameters->cp_cinema) + { + /* Rounding 16 to 12 bits */ - image->comps[0].data[index] = - (image->comps[0].data[index] + 0x08) >> 4 ; - image->comps[1].data[index] = - (image->comps[1].data[index] + 0x08) >> 4 ; - image->comps[2].data[index] = - (image->comps[2].data[index] + 0x08) >> 4 ; - if(has_alpha) - image->comps[3].data[index] = - (image->comps[3].data[index] + 0x08) >> 4 ; - } - index++; - } - else - break; - }/*for(i = 0)*/ - }/*if(tiBps == 16)*/ - else - if(tiBps == 8) - { - step = 3 + has_alpha; - - for(i = 0; i < ssize; i += step) - { - if(index < imgsize) - { + image->comps[0].data[index] = + (image->comps[0].data[index] + 0x08) >> 4 ; + image->comps[1].data[index] = + (image->comps[1].data[index] + 0x08) >> 4 ; + image->comps[2].data[index] = + (image->comps[2].data[index] + 0x08) >> 4 ; + if(has_alpha) + image->comps[3].data[index] = + (image->comps[3].data[index] + 0x08) >> 4 ; + } + index++; + } + else + break; + }/*for(i = 0)*/ + }/*if(tiBps == 16)*/ + else + if(tiBps == 8) + { + step = 3 + has_alpha; + + for(i = 0; i < ssize; i += step) + { + if(index < imgsize) + { #ifndef USETILEMODE - image->comps[0].data[index] = dat8[i+0];/* R */ - image->comps[1].data[index] = dat8[i+1];/* G */ - image->comps[2].data[index] = dat8[i+2];/* B */ - if(has_alpha) - image->comps[3].data[index] = dat8[i+3]; + image->comps[0].data[index] = dat8[i+0];/* R */ + image->comps[1].data[index] = dat8[i+1];/* G */ + image->comps[2].data[index] = dat8[i+2];/* B */ + if(has_alpha) + image->comps[3].data[index] = dat8[i+3]; #endif - if(parameters->cp_cinema) - { -/* Rounding 8 to 12 bits + if(parameters->cp_cinema) + { + /* Rounding 8 to 12 bits */ #ifndef USETILEMODE - image->comps[0].data[index] = image->comps[0].data[index] << 4 ; - image->comps[1].data[index] = image->comps[1].data[index] << 4 ; - image->comps[2].data[index] = image->comps[2].data[index] << 4 ; - if(has_alpha) - image->comps[3].data[index] = image->comps[3].data[index] << 4 ; + image->comps[0].data[index] = image->comps[0].data[index] << 4 ; + image->comps[1].data[index] = image->comps[1].data[index] << 4 ; + image->comps[2].data[index] = image->comps[2].data[index] << 4 ; + if(has_alpha) + image->comps[3].data[index] = image->comps[3].data[index] << 4 ; #endif - } - index++; - }/*if(index*/ - else - break; - }/*for(i )*/ - }/*if( tiBps == 8)*/ - else - if(tiBps == 12)/* CINEMA file */ - { - step = 9; - - for(i = 0; i < ssize; i += step) - { - if((index < imgsize)&(index+1 < imgsize)) - { - image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4); - image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2]; - - image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4); - image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5]; - - image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4); - image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8]; - - index += 2; - } - else - break; - }/*for(i )*/ - } - }/*for(strip = 0; )*/ - - _TIFFfree(buf); - TIFFClose(tif); - - return image; - }/*RGB(A)*/ - - if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */ - { - numcomps = 1 + has_alpha; - color_space = OPJ_CLRSPC_GRAY; - - for(j = 0; j < numcomps; ++j) - { - cmptparm[j].prec = tiBps; - cmptparm[j].bpp = tiBps; - cmptparm[j].dx = subsampling_dx; - cmptparm[j].dy = subsampling_dy; - cmptparm[j].w = w; - cmptparm[j].h = h; - } + } + index++; + }/*if(index*/ + else + break; + }/*for(i )*/ + }/*if( tiBps == 8)*/ + else + if(tiBps == 12)/* CINEMA file */ + { + step = 9; + + for(i = 0; i < ssize; i += step) + { + if((index < imgsize)&(index+1 < imgsize)) + { + image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4); + image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2]; + + image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4); + image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5]; + + image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4); + image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8]; + + index += 2; + } + else + break; + }/*for(i )*/ + } + }/*for(strip = 0; )*/ + + _TIFFfree(buf); + TIFFClose(tif); + + return image; + }/*RGB(A)*/ + + if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */ + { + numcomps = 1 + has_alpha; + color_space = OPJ_CLRSPC_GRAY; + + for(j = 0; j < numcomps; ++j) + { + cmptparm[j].prec = tiBps; + cmptparm[j].bpp = tiBps; + cmptparm[j].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[j].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[j].w = (OPJ_UINT32)w; + cmptparm[j].h = (OPJ_UINT32)h; + } #ifdef USETILEMODE - image = opj_image_tile_create(numcomps,&cmptparm[0],color_space); + image = opj_image_tile_create(numcomps,&cmptparm[0],color_space); #else - image = opj_image_create(numcomps, &cmptparm[0], color_space); + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); #endif - if(!image) - { - TIFFClose(tif); - return NULL; - } -/* set image offset and reference grid + if(!image) + { + TIFFClose(tif); + return NULL; + } + /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : - image->x0 + (w - 1) * subsampling_dx + 1; - image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : - image->y0 + (h - 1) * subsampling_dy + 1; - - buf = _TIFFmalloc(TIFFStripSize(tif)); - - strip_size = TIFFStripSize(tif); - index = 0; - imgsize = image->comps[0].w * image->comps[0].h ; -/* Read the Image components + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : + image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : + image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; + + buf = _TIFFmalloc(TIFFStripSize(tif)); + + strip_size = TIFFStripSize(tif); + index = 0; + imgsize = (int)(image->comps[0].w * image->comps[0].h); + /* Read the Image components */ - for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) - { - unsigned char *dat8; - tsize_t i, ssize; - int step; + for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) + { + unsigned char *dat8; + tsize_t i, ssize; + int step; + + ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); + dat8 = (unsigned char*)buf; + + if(tiBps == 16) + { + step = 2 + has_alpha + has_alpha; + + for(i = 0; i < ssize; i += step) + { + if(index < imgsize) + { + image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; + if(has_alpha) + image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; + index++; + } + else + break; + }/*for(i )*/ + } + else + if(tiBps == 8) + { + step = 1 + has_alpha; + + for(i = 0; i < ssize; i += step) + { + if(index < imgsize) + { + image->comps[0].data[index] = dat8[i+0]; + if(has_alpha) + image->comps[1].data[index] = dat8[i+1]; + index++; + } + else + break; + }/*for(i )*/ + } + }/*for(strip = 0;*/ + + _TIFFfree(buf); + TIFFClose(tif); + + }/*GRAY(A)*/ - ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); - dat8 = (unsigned char*)buf; + return image; - if(tiBps == 16) - { - step = 2 + has_alpha + has_alpha; +}/* tiftoimage() */ - for(i = 0; i < ssize; i += step) - { - if(index < imgsize) - { - image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; - if(has_alpha) - image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; - index++; - } - else - break; - }/*for(i )*/ - } - else - if(tiBps == 8) - { - step = 1 + has_alpha; +#endif /* OPJ_HAVE_LIBTIFF */ - for(i = 0; i < ssize; i += step) - { - if(index < imgsize) - { - image->comps[0].data[index] = dat8[i+0]; - if(has_alpha) - image->comps[1].data[index] = dat8[i+1]; - index++; - } - else - break; - }/*for(i )*/ - } - }/*for(strip = 0;*/ +/* -->> -->> -->> -->> - _TIFFfree(buf); - TIFFClose(tif); + RAW IMAGE FORMAT - }/*GRAY(A)*/ + <<-- <<-- <<-- <<-- */ +static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) { + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; - return image; + FILE *f = NULL; + int i, compno, numcomps, w, h; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t *cmptparm; + opj_image_t * image = NULL; + unsigned short ch; -}/* tiftoimage() */ + if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0) + { + fprintf(stderr,"\nError: invalid raw image parameters\n"); + fprintf(stderr,"Please use the Format option -F:\n"); + fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stderr,"Aborting\n"); + return NULL; + } -#endif /* HAVE_LIBTIFF */ + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "Failed to open %s for reading !!\n", filename); + fprintf(stderr,"Aborting\n"); + return NULL; + } + numcomps = raw_cp->rawComp; + color_space = OPJ_CLRSPC_SRGB; + w = raw_cp->rawWidth; + h = raw_cp->rawHeight; + cmptparm = (opj_image_cmptparm_t*) malloc((size_t)numcomps * sizeof(opj_image_cmptparm_t)); + + /* initialize image components */ + memset(&cmptparm[0], 0, (size_t)numcomps * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) { + cmptparm[i].prec = (OPJ_UINT32)raw_cp->rawBitDepth; + cmptparm[i].bpp = (OPJ_UINT32)raw_cp->rawBitDepth; + cmptparm[i].sgnd = (OPJ_UINT32)raw_cp->rawSigned; + cmptparm[i].dx = (OPJ_UINT32)subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)w; + cmptparm[i].h = (OPJ_UINT32)h; + } + /* create the image */ + image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space); + free(cmptparm); + if(!image) { + fclose(f); + return NULL; + } + /* set image offset and reference grid */ + image->x0 = (OPJ_UINT32)parameters->image_offset_x0; + image->y0 = (OPJ_UINT32)parameters->image_offset_y0; + image->x1 = (OPJ_UINT32)parameters->image_offset_x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; + image->y1 = (OPJ_UINT32)parameters->image_offset_y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; -/* -->> -->> -->> -->> + if(raw_cp->rawBitDepth <= 8) + { + unsigned char value = 0; + for(compno = 0; compno < numcomps; compno++) { + for (i = 0; i < w * h; i++) { + if (!fread(&value, 1, 1, f)) { + fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); + return NULL; + } + image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value; + } + } + } + else if(raw_cp->rawBitDepth <= 16) + { + unsigned short value; + for(compno = 0; compno < numcomps; compno++) { + for (i = 0; i < w * h; i++) { + unsigned char temp1; + unsigned char temp2; + if (!fread(&temp1, 1, 1, f)) { + fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); + return NULL; + } + if (!fread(&temp2, 1, 1, f)) { + fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); + return NULL; + } + if( big_endian ) + { + value = (unsigned short)((temp1 << 8) + temp2); + } + else + { + value = (unsigned short)((temp2 << 8) + temp1); + } + image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value; + } + } + } + else { + fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n"); + return NULL; + } - RAW IMAGE FORMAT + if (fread(&ch, 1, 1, f)) { + fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n"); + } + fclose(f); - <<-- <<-- <<-- <<-- */ -static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp, OPJ_BOOL big_endian) { - int subsampling_dx = parameters->subsampling_dx; - int subsampling_dy = parameters->subsampling_dy; - - FILE *f = NULL; - int i, compno, numcomps, w, h; - OPJ_COLOR_SPACE color_space; - opj_image_cmptparm_t *cmptparm; - opj_image_t * image = NULL; - unsigned short ch; - - if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0) - { - fprintf(stderr,"\nError: invalid raw image parameters\n"); - fprintf(stderr,"Please use the Format option -F:\n"); - fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); - fprintf(stderr,"Aborting\n"); - return NULL; - } - - f = fopen(filename, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !!\n", filename); - fprintf(stderr,"Aborting\n"); - return NULL; - } - numcomps = raw_cp->rawComp; - color_space = OPJ_CLRSPC_SRGB; - w = raw_cp->rawWidth; - h = raw_cp->rawHeight; - cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t)); - - /* initialize image components */ - memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t)); - for(i = 0; i < numcomps; i++) { - cmptparm[i].prec = raw_cp->rawBitDepth; - cmptparm[i].bpp = raw_cp->rawBitDepth; - cmptparm[i].sgnd = raw_cp->rawSigned; - cmptparm[i].dx = subsampling_dx; - cmptparm[i].dy = subsampling_dy; - cmptparm[i].w = w; - cmptparm[i].h = h; - } - /* create the image */ - image = opj_image_create(numcomps, &cmptparm[0], color_space); - if(!image) { - fclose(f); - return NULL; - } - /* set image offset and reference grid */ - image->x0 = parameters->image_offset_x0; - image->y0 = parameters->image_offset_y0; - image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1; - image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1; - - if(raw_cp->rawBitDepth <= 8) - { - unsigned char value = 0; - for(compno = 0; compno < numcomps; compno++) { - for (i = 0; i < w * h; i++) { - if (!fread(&value, 1, 1, f)) { - fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); - return NULL; - } - image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value; - } - } - } - else if(raw_cp->rawBitDepth <= 16) - { - unsigned short value; - for(compno = 0; compno < numcomps; compno++) { - for (i = 0; i < w * h; i++) { - unsigned char temp1; - unsigned char temp2; - if (!fread(&temp1, 1, 1, f)) { - fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); - return NULL; - } - if (!fread(&temp2, 1, 1, f)) { - fprintf(stderr,"Error reading raw file. End of file probably reached.\n"); - return NULL; - } - if( big_endian ) - { - value = temp1 << 8; - value += temp2; - } - else - { - value = temp2 << 8; - value += temp1; - } - image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value; - } - } - } - else { - fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n"); - return NULL; - } - - if (fread(&ch, 1, 1, f)) { - fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n"); - } - fclose(f); - - return image; + return image; } opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) { - return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE); + return rawtoimage_common(filename, parameters, raw_cp, OPJ_FALSE); } opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) { - return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE); + return rawtoimage_common(filename, parameters, raw_cp, OPJ_TRUE); } static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL big_endian) { - FILE *rawFile = NULL; - size_t res; - int compno; - int w, h; - int line, row; - int *ptr; - - if((image->numcomps * image->x1 * image->y1) == 0) - { - fprintf(stderr,"\nError: invalid raw image parameters\n"); - return 1; - } - - rawFile = fopen(outfile, "wb"); - if (!rawFile) { - fprintf(stderr, "Failed to open %s for writing !!\n", outfile); - return 1; - } - - fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps); - - for(compno = 0; compno < image->numcomps; compno++) - { - fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, - image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned"); - - w = image->comps[compno].w; - h = image->comps[compno].h; - - if(image->comps[compno].prec <= 8) - { - if(image->comps[compno].sgnd == 1) - { - signed char curr; - int mask = (1 << image->comps[compno].prec) - 1; - ptr = image->comps[compno].data; - for (line = 0; line < h; line++) { - for(row = 0; row < w; row++) { - curr = (signed char) (*ptr & mask); - res = fwrite(&curr, sizeof(signed char), 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; - } - ptr++; - } - } - } - else if(image->comps[compno].sgnd == 0) - { - unsigned char curr; - int mask = (1 << image->comps[compno].prec) - 1; - ptr = image->comps[compno].data; - for (line = 0; line < h; line++) { - for(row = 0; row < w; row++) { - curr = (unsigned char) (*ptr & mask); - res = fwrite(&curr, sizeof(unsigned char), 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; - } - ptr++; - } - } - } - } - else if(image->comps[compno].prec <= 16) - { - if(image->comps[compno].sgnd == 1) - { - signed short int curr; - int mask = (1 << image->comps[compno].prec) - 1; - ptr = image->comps[compno].data; - for (line = 0; line < h; line++) { - for(row = 0; row < w; row++) { - unsigned char temp1; - unsigned char temp2; - curr = (signed short int) (*ptr & mask); - if( big_endian ) - { - temp1 = (unsigned char) (curr >> 8); - temp2 = (unsigned char) curr; - } - else - { - temp2 = (unsigned char) (curr >> 8); - temp1 = (unsigned char) curr; - } - res = fwrite(&temp1, 1, 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; + FILE *rawFile = NULL; + size_t res; + unsigned int compno; + int w, h, fails; + int line, row, curr, mask; + int *ptr; + unsigned char uc; + (void)big_endian; + + if((image->numcomps * image->x1 * image->y1) == 0) + { + fprintf(stderr,"\nError: invalid raw image parameters\n"); + return 1; + } + + rawFile = fopen(outfile, "wb"); + if (!rawFile) { + fprintf(stderr, "Failed to open %s for writing !!\n", outfile); + return 1; + } + + fails = 1; + fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps); + + for(compno = 0; compno < image->numcomps; compno++) + { + fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, + image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned"); + + w = (int)image->comps[compno].w; + h = (int)image->comps[compno].h; + + if(image->comps[compno].prec <= 8) + { + if(image->comps[compno].sgnd == 1) + { + mask = (1 << image->comps[compno].prec) - 1; + ptr = image->comps[compno].data; + for (line = 0; line < h; line++) { + for(row = 0; row < w; row++) { + curr = *ptr; + if(curr > 127) curr = 127; else if(curr < -128) curr = -128; + uc = (unsigned char) (curr & mask); + res = fwrite(&uc, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + ptr++; + } + } } - res = fwrite(&temp2, 1, 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; + else if(image->comps[compno].sgnd == 0) + { + mask = (1 << image->comps[compno].prec) - 1; + ptr = image->comps[compno].data; + for (line = 0; line < h; line++) { + for(row = 0; row < w; row++) { + curr = *ptr; + if(curr > 255) curr = 255; else if(curr < 0) curr = 0; + uc = (unsigned char) (curr & mask); + res = fwrite(&uc, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + goto fin; + } + ptr++; + } + } } - ptr++; - } - } - } - else if(image->comps[compno].sgnd == 0) - { - unsigned short int curr; - int mask = (1 << image->comps[compno].prec) - 1; - ptr = image->comps[compno].data; - for (line = 0; line < h; line++) { - for(row = 0; row < w; row++) { - unsigned char temp1; - unsigned char temp2; - curr = (unsigned short int) (*ptr & mask); - if( big_endian ) - { - temp1 = (unsigned char) (curr >> 8); - temp2 = (unsigned char) curr; - } - else - { - temp2 = (unsigned char) (curr >> 8); - temp1 = (unsigned char) curr; - } - res = fwrite(&temp1, 1, 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; + } + else if(image->comps[compno].prec <= 16) + { + if(image->comps[compno].sgnd == 1) + { + union { signed short val; signed char vals[2]; } uc16; + mask = (1 << image->comps[compno].prec) - 1; + ptr = image->comps[compno].data; + for (line = 0; line < h; line++) { + for(row = 0; row < w; row++) { + curr = *ptr; + if(curr > 32767 ) curr = 32767; else if( curr < -32768) curr = -32768; + uc16.val = (signed short)(curr & mask); + res = fwrite(uc16.vals, 1, 2, rawFile); + if( res < 2 ) { + fprintf(stderr, "failed to write 2 byte for %s\n", outfile); + goto fin; + } + ptr++; + } + } } - res = fwrite(&temp2, 1, 1, rawFile); - if( res < 1 ) { - fprintf(stderr, "failed to write 1 byte for %s\n", outfile); - return 1; + else if(image->comps[compno].sgnd == 0) + { + union { unsigned short val; unsigned char vals[2]; } uc16; + mask = (1 << image->comps[compno].prec) - 1; + ptr = image->comps[compno].data; + for (line = 0; line < h; line++) { + for(row = 0; row < w; row++) { + curr = *ptr; + if(curr > 65536 ) curr = 65536; else if( curr < 0) curr = 0; + uc16.val = (unsigned short)(curr & mask); + res = fwrite(uc16.vals, 1, 2, rawFile); + if( res < 2 ) { + fprintf(stderr, "failed to write 2 byte for %s\n", outfile); + goto fin; + } + ptr++; + } + } } - ptr++; - } - } - } - } - else if (image->comps[compno].prec <= 32) - { - fprintf(stderr,"More than 16 bits per component no handled yet\n"); - return 1; - } - else - { - fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec); - return 1; - } - } - fclose(rawFile); - return 0; + } + else if (image->comps[compno].prec <= 32) + { + fprintf(stderr,"More than 16 bits per component no handled yet\n"); + goto fin; + } + else + { + fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec); + goto fin; + } + } + fails = 0; +fin: + fclose(rawFile); + return fails; } int imagetoraw(opj_image_t * image, const char *outfile) { - return imagetoraw_common(image, outfile, OPJ_TRUE); + return imagetoraw_common(image, outfile, OPJ_TRUE); } int imagetorawl(opj_image_t * image, const char *outfile) { - return imagetoraw_common(image, outfile, OPJ_FALSE); + return imagetoraw_common(image, outfile, OPJ_FALSE); } -#ifdef HAVE_LIBPNG +#ifdef OPJ_HAVE_LIBPNG #define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a" #define MAGIC_SIZE 8 @@ -3093,257 +3205,257 @@ int imagetorawl(opj_image_t * image, const char *outfile) opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params) { - png_structp png; - png_infop info; - double gamma, display_exponent; - int bit_depth, interlace_type,compression_type, filter_type; - int unit; - png_uint_32 resx, resy; - unsigned int i, j; - png_uint_32 width, height; - int color_type, has_alpha, is16; - unsigned char *s; - FILE *reader; - unsigned char **rows; -/* j2k: */ - opj_image_t *image; - opj_image_cmptparm_t cmptparm[4]; - int sub_dx, sub_dy; - unsigned int nr_comp; - int *r, *g, *b, *a; - unsigned char sigbuf[8]; - - if((reader = fopen(read_idf, "rb")) == NULL) - { - fprintf(stderr,"pngtoimage: can not open %s\n",read_idf); - return NULL; - } - image = NULL; png = NULL; rows = NULL; + png_structp png; + png_infop info; + double gamma, display_exponent; + int bit_depth, interlace_type,compression_type, filter_type; + int unit; + png_uint_32 resx, resy; + unsigned int i, j; + png_uint_32 width, height; + int color_type, has_alpha, is16; + unsigned char *s; + FILE *reader; + unsigned char **rows; + /* j2k: */ + opj_image_t *image; + opj_image_cmptparm_t cmptparm[4]; + int sub_dx, sub_dy; + unsigned int nr_comp; + int *r, *g, *b, *a = NULL; + unsigned char sigbuf[8]; + + if((reader = fopen(read_idf, "rb")) == NULL) + { + fprintf(stderr,"pngtoimage: can not open %s\n",read_idf); + return NULL; + } + image = NULL; png = NULL; rows = NULL; - if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE - || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) - { - fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf); - goto fin; - } -/* libpng-VERSION/example.c: + if(fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE + || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) + { + fprintf(stderr,"pngtoimage: %s is no valid PNG file\n",read_idf); + goto fin; + } + /* libpng-VERSION/example.c: * PC : screen_gamma = 2.2; * Mac: screen_gamma = 1.7 or 1.0; */ - display_exponent = 2.2; + display_exponent = 2.2; - if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, - NULL, NULL, NULL)) == NULL) - goto fin; - if((info = png_create_info_struct(png)) == NULL) - goto fin; + if((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL)) == NULL) + goto fin; + if((info = png_create_info_struct(png)) == NULL) + goto fin; - if(setjmp(png_jmpbuf(png))) - goto fin; + if(setjmp(png_jmpbuf(png))) + goto fin; - png_init_io(png, reader); - png_set_sig_bytes(png, MAGIC_SIZE); + png_init_io(png, reader); + png_set_sig_bytes(png, MAGIC_SIZE); - png_read_info(png, info); + png_read_info(png, info); - if(png_get_IHDR(png, info, &width, &height, - &bit_depth, &color_type, &interlace_type, - &compression_type, &filter_type) == 0) - goto fin; + if(png_get_IHDR(png, info, &width, &height, + &bit_depth, &color_type, &interlace_type, + &compression_type, &filter_type) == 0) + goto fin; -/* png_set_expand(): + /* png_set_expand(): * expand paletted images to RGB, expand grayscale images of * less than 8-bit depth to 8-bit depth, and expand tRNS chunks * to alpha channels. */ - if(color_type == PNG_COLOR_TYPE_PALETTE) - png_set_expand(png); - else - if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) - png_set_expand(png); + if(color_type == PNG_COLOR_TYPE_PALETTE) + png_set_expand(png); + else + if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) + png_set_expand(png); - if(png_get_valid(png, info, PNG_INFO_tRNS)) - png_set_expand(png); + if(png_get_valid(png, info, PNG_INFO_tRNS)) + png_set_expand(png); - is16 = (bit_depth == 16); + is16 = (bit_depth == 16); -/* GRAY => RGB; GRAY_ALPHA => RGBA + /* GRAY => RGB; GRAY_ALPHA => RGBA */ - if(color_type == PNG_COLOR_TYPE_GRAY - || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) - { - png_set_gray_to_rgb(png); - color_type = - (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB: - PNG_COLOR_TYPE_RGB_ALPHA); - } - if( !png_get_gAMA(png, info, &gamma)) - gamma = 0.45455; + if(color_type == PNG_COLOR_TYPE_GRAY + || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + { + png_set_gray_to_rgb(png); + color_type = + (color_type == PNG_COLOR_TYPE_GRAY? PNG_COLOR_TYPE_RGB: + PNG_COLOR_TYPE_RGB_ALPHA); + } + if( !png_get_gAMA(png, info, &gamma)) + gamma = 0.45455; - png_set_gamma(png, display_exponent, gamma); + png_set_gamma(png, display_exponent, gamma); - png_read_update_info(png, info); + png_read_update_info(png, info); - png_get_pHYs(png, info, &resx, &resy, &unit); + png_get_pHYs(png, info, &resx, &resy, &unit); - color_type = png_get_color_type(png, info); + color_type = png_get_color_type(png, info); - has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA); + has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA); - nr_comp = 3 + has_alpha; + nr_comp = 3 + (unsigned int)has_alpha; - bit_depth = png_get_bit_depth(png, info); + bit_depth = png_get_bit_depth(png, info); - rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*)); - for(i = 0; i < height; ++i) - rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info)); + rows = (unsigned char**)calloc(height+1, sizeof(unsigned char*)); + for(i = 0; i < height; ++i) + rows[i] = (unsigned char*)malloc(png_get_rowbytes(png,info)); - png_read_image(png, rows); + png_read_image(png, rows); - memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t)); + memset(cmptparm, 0, sizeof(cmptparm)); - sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy; + sub_dx = params->subsampling_dx; sub_dy = params->subsampling_dy; - for(i = 0; i < nr_comp; ++i) - { - cmptparm[i].prec = bit_depth; -/* bits_per_pixel: 8 or 16 */ - cmptparm[i].bpp = bit_depth; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = sub_dx; - cmptparm[i].dy = sub_dy; - cmptparm[i].w = width; - cmptparm[i].h = height; - } + for(i = 0; i < nr_comp; ++i) + { + cmptparm[i].prec = (OPJ_UINT32)bit_depth; + /* bits_per_pixel: 8 or 16 */ + cmptparm[i].bpp = (OPJ_UINT32)bit_depth; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)sub_dx; + cmptparm[i].dy = (OPJ_UINT32)sub_dy; + cmptparm[i].w = (OPJ_UINT32)width; + cmptparm[i].h = (OPJ_UINT32)height; + } - image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB); + image = opj_image_create(nr_comp, &cmptparm[0], OPJ_CLRSPC_SRGB); - if(image == NULL) goto fin; + if(image == NULL) goto fin; - image->x0 = params->image_offset_x0; - image->y0 = params->image_offset_y0; - image->x1 = image->x0 + (width - 1) * sub_dx + 1 + image->x0; - image->y1 = image->y0 + (height - 1) * sub_dy + 1 + image->y0; + image->x0 = (OPJ_UINT32)params->image_offset_x0; + image->y0 = (OPJ_UINT32)params->image_offset_y0; + image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32)sub_dx + 1 + image->x0); + image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32)sub_dy + 1 + image->y0); - r = image->comps[0].data; - g = image->comps[1].data; - b = image->comps[2].data; - a = image->comps[3].data; + r = image->comps[0].data; + g = image->comps[1].data; + b = image->comps[2].data; + if(has_alpha) a = image->comps[3].data; - for(i = 0; i < height; ++i) - { - s = rows[i]; + for(i = 0; i < height; ++i) + { + s = rows[i]; - for(j = 0; j < width; ++j) - { - if(is16) - { - *r++ = s[0]<<8|s[1]; s += 2; + for(j = 0; j < width; ++j) + { + if(is16) + { + *r++ = s[0]<<8|s[1]; s += 2; - *g++ = s[0]<<8|s[1]; s += 2; - - *b++ = s[0]<<8|s[1]; s += 2; - - if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; } + *g++ = s[0]<<8|s[1]; s += 2; - continue; - } - *r++ = *s++; *g++ = *s++; *b++ = *s++; + *b++ = s[0]<<8|s[1]; s += 2; - if(has_alpha) *a++ = *s++; - } - } + if(has_alpha) { *a++ = s[0]<<8|s[1]; s += 2; } + + continue; + } + *r++ = *s++; *g++ = *s++; *b++ = *s++; + + if(has_alpha) *a++ = *s++; + } + } fin: - if(rows) - { - for(i = 0; i < height; ++i) - free(rows[i]); - free(rows); - } - if(png) - png_destroy_read_struct(&png, &info, NULL); + if(rows) + { + for(i = 0; i < height; ++i) + free(rows[i]); + free(rows); + } + if(png) + png_destroy_read_struct(&png, &info, NULL); - fclose(reader); + fclose(reader); - return image; + return image; }/* pngtoimage() */ int imagetopng(opj_image_t * image, const char *write_idf) { - FILE *writer; - png_structp png; - png_infop info; - int *red, *green, *blue, *alpha; - unsigned char *row_buf, *d; - int has_alpha, width, height, nr_comp, color_type; - int adjustR, adjustG, adjustB, adjustA, x, y, fails; - int prec, ushift, dshift, is16, force16, force8; - unsigned short mask = 0xffff; - png_color_8 sig_bit; - - is16 = force16 = force8 = ushift = dshift = 0; fails = 1; - prec = image->comps[0].prec; - nr_comp = image->numcomps; - - if(prec > 8 && prec < 16) - { - ushift = 16 - prec; dshift = prec - ushift; - prec = 16; force16 = 1; - } - else - if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */ - { - ushift = 8 - prec; dshift = 8 - ushift; - prec = 8; force8 = 1; - } + FILE *writer; + png_structp png; + png_infop info; + int *red, *green, *blue, *alpha; + unsigned char *row_buf, *d; + int has_alpha, width, height, nr_comp, color_type; + int adjustR, adjustG, adjustB, adjustA, x, y, fails; + int prec, ushift, dshift, is16, force16, force8; + unsigned short mask = 0xffff; + png_color_8 sig_bit; + + is16 = force16 = force8 = ushift = dshift = 0; fails = 1; + prec = (int)image->comps[0].prec; + nr_comp = (int)image->numcomps; + + if(prec > 8 && prec < 16) + { + ushift = 16 - prec; dshift = prec - ushift; + prec = 16; force16 = 1; + } + else + if(prec < 8 && nr_comp > 1)/* GRAY_ALPHA, RGB, RGB_ALPHA */ + { + ushift = 8 - prec; dshift = 8 - ushift; + prec = 8; force8 = 1; + } - if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) - { - fprintf(stderr,"imagetopng: can not create %s" - "\n\twrong bit_depth %d\n", write_idf, prec); - return fails; - } - writer = fopen(write_idf, "wb"); + if(prec != 1 && prec != 2 && prec != 4 && prec != 8 && prec != 16) + { + fprintf(stderr,"imagetopng: can not create %s" + "\n\twrong bit_depth %d\n", write_idf, prec); + return fails; + } + writer = fopen(write_idf, "wb"); - if(writer == NULL) return fails; + if(writer == NULL) return fails; - info = NULL; has_alpha = 0; + info = NULL; has_alpha = 0; -/* Create and initialize the png_struct with the desired error handler + /* Create and initialize the png_struct with the desired error handler * functions. If you want to use the default stderr and longjump method, * you can supply NULL for the last three parameters. We also check that * the library version is compatible with the one used at compile time, * in case we are using dynamically linked libraries. REQUIRED. */ - png = png_create_write_struct(PNG_LIBPNG_VER_STRING, - NULL, NULL, NULL); -/*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */ + png = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, NULL, NULL); + /*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */ - if(png == NULL) goto fin; + if(png == NULL) goto fin; -/* Allocate/initialize the image information data. REQUIRED + /* Allocate/initialize the image information data. REQUIRED */ - info = png_create_info_struct(png); + info = png_create_info_struct(png); - if(info == NULL) goto fin; + if(info == NULL) goto fin; -/* Set error handling. REQUIRED if you are not supplying your own + /* Set error handling. REQUIRED if you are not supplying your own * error handling functions in the png_create_write_struct() call. */ - if(setjmp(png_jmpbuf(png))) goto fin; + if(setjmp(png_jmpbuf(png))) goto fin; -/* I/O initialization functions is REQUIRED + /* I/O initialization functions is REQUIRED */ - png_init_io(png, writer); + png_init_io(png, writer); -/* Set the image information here. Width and height are up to 2^31, + /* Set the image information here. Width and height are up to 2^31, * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST - * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. + * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. * REQUIRED * * ERRORS: @@ -3352,276 +3464,292 @@ int imagetopng(opj_image_t * image, const char *write_idf) * color_type == PNG_COLOR_TYPE_RGB && bit_depth < 8 * color_type == PNG_COLOR_TYPE_GRAY_ALPHA && bit_depth < 8 * color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8 - * + * */ - png_set_compression_level(png, Z_BEST_COMPRESSION); - - if(prec == 16) mask = 0xffff; - else - if(prec == 8) mask = 0x00ff; - else - if(prec == 4) mask = 0x000f; - else - if(prec == 2) mask = 0x0003; - else - if(prec == 1) mask = 0x0001; - - if(nr_comp >= 3 - && image->comps[0].dx == image->comps[1].dx - && image->comps[1].dx == image->comps[2].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[1].dy == image->comps[2].dy - && image->comps[0].prec == image->comps[1].prec - && image->comps[1].prec == image->comps[2].prec) - { - int v; + png_set_compression_level(png, Z_BEST_COMPRESSION); - has_alpha = (nr_comp > 3); + if(prec == 16) mask = 0xffff; + else + if(prec == 8) mask = 0x00ff; + else + if(prec == 4) mask = 0x000f; + else + if(prec == 2) mask = 0x0003; + else + if(prec == 1) mask = 0x0001; + + if(nr_comp >= 3 + && image->comps[0].dx == image->comps[1].dx + && image->comps[1].dx == image->comps[2].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[1].dy == image->comps[2].dy + && image->comps[0].prec == image->comps[1].prec + && image->comps[1].prec == image->comps[2].prec) + { + int v; - is16 = (prec == 16); - - width = image->comps[0].w; - height = image->comps[0].h; + has_alpha = (nr_comp > 3); - red = image->comps[0].data; - green = image->comps[1].data; - blue = image->comps[2].data; + is16 = (prec == 16); - sig_bit.red = sig_bit.green = sig_bit.blue = prec; + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; - if(has_alpha) - { - sig_bit.alpha = prec; - alpha = image->comps[3].data; - color_type = PNG_COLOR_TYPE_RGB_ALPHA; - adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0); - } - else - { - sig_bit.alpha = 0; alpha = NULL; - color_type = PNG_COLOR_TYPE_RGB; - adjustA = 0; - } - png_set_sBIT(png, info, &sig_bit); - - png_set_IHDR(png, info, width, height, prec, - color_type, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - - png_set_gamma(png, 2.2, 1./2.2); - png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); -/*=============================*/ - png_write_info(png, info); -/*=============================*/ - if(prec < 8) - { - png_set_packing(png); - } - adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + red = image->comps[0].data; + green = image->comps[1].data; + blue = image->comps[2].data; - row_buf = (unsigned char*)malloc(width * nr_comp * 2); + sig_bit.red = sig_bit.green = sig_bit.blue = (png_byte)prec; - for(y = 0; y < height; ++y) - { - d = row_buf; + if(has_alpha) + { + sig_bit.alpha = (png_byte)prec; + alpha = image->comps[3].data; + color_type = PNG_COLOR_TYPE_RGB_ALPHA; + adjustA = (image->comps[3].sgnd ? 1 << (image->comps[3].prec - 1) : 0); + } + else + { + sig_bit.alpha = 0; alpha = NULL; + color_type = PNG_COLOR_TYPE_RGB; + adjustA = 0; + } + png_set_sBIT(png, info, &sig_bit); + + png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, prec, + color_type, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_set_gamma(png, 2.2, 1./2.2); + png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); + /*=============================*/ + png_write_info(png, info); + /*=============================*/ + if(prec < 8) + { + png_set_packing(png); + } +printf("%s:%d:sgnd(%d,%d,%d) w(%d) h(%d) alpha(%d)\n",__FILE__,__LINE__, +image->comps[0].sgnd, +image->comps[1].sgnd,image->comps[2].sgnd,width,height,has_alpha); - for(x = 0; x < width; ++x) - { - if(is16) - { - v = *red + adjustR; ++red; - - if(force16) { v = (v<<ushift) + (v>>dshift); } + adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + row_buf = (unsigned char*)malloc((size_t)width * (size_t)nr_comp * 2); - v = *green + adjustG; ++green; - - if(force16) { v = (v<<ushift) + (v>>dshift); } + for(y = 0; y < height; ++y) + { + d = row_buf; - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + for(x = 0; x < width; ++x) + { + if(is16) + { + v = *red + adjustR; ++red; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - v = *blue + adjustB; ++blue; - - if(force16) { v = (v<<ushift) + (v>>dshift); } + if(force16) { v = (v<<ushift) + (v>>dshift); } - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - if(has_alpha) - { - v = *alpha + adjustA; ++alpha; - - if(force16) { v = (v<<ushift) + (v>>dshift); } + v = *green + adjustG; ++green; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - } - continue; - }/* if(is16) */ + if(force16) { v = (v<<ushift) + (v>>dshift); } - v = *red + adjustR; ++red; + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - if(force8) { v = (v<<ushift) + (v>>dshift); } + v = *blue + adjustB; ++blue; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - *d++ = (unsigned char)(v & mask); + if(force16) { v = (v<<ushift) + (v>>dshift); } - v = *green + adjustG; ++green; + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - if(force8) { v = (v<<ushift) + (v>>dshift); } + if(has_alpha) + { + v = *alpha + adjustA; ++alpha; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - *d++ = (unsigned char)(v & mask); + if(force16) { v = (v<<ushift) + (v>>dshift); } - v = *blue + adjustB; ++blue; + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + } + continue; + }/* if(is16) */ - if(force8) { v = (v<<ushift) + (v>>dshift); } + v = *red + adjustR; ++red; + if(v > 255) v = 255; else if(v < 0) v = 0; - *d++ = (unsigned char)(v & mask); + if(force8) { v = (v<<ushift) + (v>>dshift); } - if(has_alpha) - { - v = *alpha + adjustA; ++alpha; + *d++ = (unsigned char)(v & mask); - if(force8) { v = (v<<ushift) + (v>>dshift); } + v = *green + adjustG; ++green; + if(v > 255) v = 255; else if(v < 0) v = 0; - *d++ = (unsigned char)(v & mask); - } - } /* for(x) */ + if(force8) { v = (v<<ushift) + (v>>dshift); } - png_write_row(png, row_buf); + *d++ = (unsigned char)(v & mask); - } /* for(y) */ - free(row_buf); + v = *blue + adjustB; ++blue; + if(v > 255) v = 255; else if(v < 0) v = 0; - }/* nr_comp >= 3 */ - else - if(nr_comp == 1 /* GRAY */ - || ( nr_comp == 2 /* GRAY_ALPHA */ - && image->comps[0].dx == image->comps[1].dx - && image->comps[0].dy == image->comps[1].dy - && image->comps[0].prec == image->comps[1].prec)) - { - int v; + if(force8) { v = (v<<ushift) + (v>>dshift); } - red = image->comps[0].data; + *d++ = (unsigned char)(v & mask); - sig_bit.gray = prec; - sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0; - alpha = NULL; adjustA = 0; - color_type = PNG_COLOR_TYPE_GRAY; + if(has_alpha) + { + v = *alpha + adjustA; ++alpha; + if(v > 255) v = 255; else if(v < 0) v = 0; - if(nr_comp == 2) - { - has_alpha = 1; sig_bit.alpha = prec; - alpha = image->comps[1].data; - color_type = PNG_COLOR_TYPE_GRAY_ALPHA; - adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - } - width = image->comps[0].w; - height = image->comps[0].h; + if(force8) { v = (v<<ushift) + (v>>dshift); } - png_set_IHDR(png, info, width, height, sig_bit.gray, - color_type, - PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + *d++ = (unsigned char)(v & mask); + } + } /* for(x) */ - png_set_sBIT(png, info, &sig_bit); + png_write_row(png, row_buf); - png_set_gamma(png, 2.2, 1./2.2); - png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); -/*=============================*/ - png_write_info(png, info); -/*=============================*/ - adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + } /* for(y) */ + free(row_buf); - if(prec < 8) - { - png_set_packing(png); - } + }/* nr_comp >= 3 */ + else + if(nr_comp == 1 /* GRAY */ + || ( nr_comp == 2 /* GRAY_ALPHA */ + && image->comps[0].dx == image->comps[1].dx + && image->comps[0].dy == image->comps[1].dy + && image->comps[0].prec == image->comps[1].prec)) + { + int v; + + red = image->comps[0].data; + + sig_bit.gray = (png_byte)prec; + sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0; + alpha = NULL; adjustA = 0; + color_type = PNG_COLOR_TYPE_GRAY; + + if(nr_comp == 2) + { + has_alpha = 1; sig_bit.alpha = (png_byte)prec; + alpha = image->comps[1].data; + color_type = PNG_COLOR_TYPE_GRAY_ALPHA; + adjustA = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + } + width = (int)image->comps[0].w; + height = (int)image->comps[0].h; + + png_set_IHDR(png, info, (png_uint_32)width, (png_uint_32)height, sig_bit.gray, + color_type, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_set_sBIT(png, info, &sig_bit); + + png_set_gamma(png, 2.2, 1./2.2); + png_set_sRGB(png, info, PNG_sRGB_INTENT_PERCEPTUAL); + /*=============================*/ + png_write_info(png, info); + /*=============================*/ + adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + + if(prec < 8) + { + png_set_packing(png); + } - if(prec > 8) - { - row_buf = (unsigned char*) - malloc(width * nr_comp * sizeof(unsigned short)); + if(prec > 8) + { + row_buf = (unsigned char*) + malloc((size_t)width * (size_t)nr_comp * sizeof(unsigned short)); - for(y = 0; y < height; ++y) - { - d = row_buf; + for(y = 0; y < height; ++y) + { + d = row_buf; - for(x = 0; x < width; ++x) - { - v = *red + adjustR; ++red; + for(x = 0; x < width; ++x) + { + v = *red + adjustR; ++red; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - if(force16) { v = (v<<ushift) + (v>>dshift); } + if(force16) { v = (v<<ushift) + (v>>dshift); } - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - if(has_alpha) - { - v = *alpha++; + if(has_alpha) + { + v = *alpha++; + if(v > 65535) v = 65535; else if(v < 0) v = 0; - if(force16) { v = (v<<ushift) + (v>>dshift); } + if(force16) { v = (v<<ushift) + (v>>dshift); } - *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; - } - }/* for(x) */ - png_write_row(png, row_buf); + *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; + } + }/* for(x) */ + png_write_row(png, row_buf); - } /* for(y) */ - free(row_buf); - } - else /* prec <= 8 */ - { - row_buf = (unsigned char*)calloc(width, nr_comp * 2); + } /* for(y) */ + free(row_buf); + } + else /* prec <= 8 */ + { + row_buf = (unsigned char*)calloc((size_t)width, (size_t)nr_comp * 2); - for(y = 0; y < height; ++y) - { - d = row_buf; + for(y = 0; y < height; ++y) + { + d = row_buf; - for(x = 0; x < width; ++x) - { - v = *red + adjustR; ++red; + for(x = 0; x < width; ++x) + { + v = *red + adjustR; ++red; + if(v > 255) v = 255; else if(v < 0) v = 0; - if(force8) { v = (v<<ushift) + (v>>dshift); } + if(force8) { v = (v<<ushift) + (v>>dshift); } - *d++ = (unsigned char)(v & mask); + *d++ = (unsigned char)(v & mask); - if(has_alpha) - { - v = *alpha + adjustA; ++alpha; + if(has_alpha) + { + v = *alpha + adjustA; ++alpha; + if(v > 255) v = 255; else if(v < 0) v = 0; - if(force8) { v = (v<<ushift) + (v>>dshift); } + if(force8) { v = (v<<ushift) + (v>>dshift); } - *d++ = (unsigned char)(v & mask); - } - }/* for(x) */ + *d++ = (unsigned char)(v & mask); + } + }/* for(x) */ - png_write_row(png, row_buf); + png_write_row(png, row_buf); - } /* for(y) */ - free(row_buf); - } - } - else - { - fprintf(stderr,"imagetopng: can not create %s\n",write_idf); - goto fin; - } - png_write_end(png, info); + } /* for(y) */ + free(row_buf); + } + } + else + { + fprintf(stderr,"imagetopng: can not create %s\n",write_idf); + goto fin; + } + png_write_end(png, info); - fails = 0; + fails = 0; fin: - if(png) - { - png_destroy_write_struct(&png, &info); - } - fclose(writer); + if(png) + { + png_destroy_write_struct(&png, &info); + } + fclose(writer); - if(fails) remove(write_idf); + if(fails) remove(write_idf); - return fails; + return fails; }/* imagetopng() */ -#endif /* HAVE_LIBPNG */ +#endif /* OPJ_HAVE_LIBPNG */ diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 9c957502..ba1d8374 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -7,7 +7,7 @@ * 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) 2011-2012, Centre National d'Etudes Spatiales (CNES), France * Copyright (c) 2012, CS Systemes d'Information, France * All rights reserved. * @@ -53,7 +53,7 @@ #include <strings.h> #endif /* _WIN32 */ -#include "opj_config.h" +#include "opj_apps_config.h" #include "openjpeg.h" #include "opj_getopt.h" #include "convert.h" @@ -61,1525 +61,1387 @@ #include "format_defs.h" -#define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/ -#define CINEMA_48_CS 651041 /*Codestream length for 48fps*/ -#define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/ -#define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/ - typedef struct dircnt{ - /** Buffer for holding images read from Directory*/ - char *filename_buf; - /** Pointer to the buffer*/ - char **filename; + /** Buffer for holding images read from Directory*/ + char *filename_buf; + /** Pointer to the buffer*/ + char **filename; }dircnt_t; typedef struct img_folder{ - /** The directory path of the folder containing input images*/ - char *imgdirpath; - /** Output format*/ - char *out_format; - /** Enable option*/ - char set_imgdir; - /** Enable Cod Format for output*/ - char set_out_format; - /** User specified rate stored in case of cinema option*/ - float *rates; + /** The directory path of the folder containing input images*/ + char *imgdirpath; + /** Output format*/ + char *out_format; + /** Enable option*/ + char set_imgdir; + /** Enable Cod Format for output*/ + char set_out_format; }img_fol_t; static void encode_help_display(void) { - fprintf(stdout,"HELP for opj_compress\n----\n\n"); - fprintf(stdout,"- the -h option displays this help information on screen\n\n"); - -/* UniPG>> */ - fprintf(stdout,"List of parameters for the JPEG 2000 " -#ifdef USE_JPWL - "+ JPWL " -#endif /* USE_JPWL */ - "encoder:\n"); -/* <<UniPG */ - fprintf(stdout,"\n"); - fprintf(stdout,"REMARKS:\n"); - fprintf(stdout,"---------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n"); - fprintf(stdout,"COD and QCD never appear in the tile_header.\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"By default:\n"); - fprintf(stdout,"------------\n"); - fprintf(stdout,"\n"); - fprintf(stdout," * Lossless\n"); - fprintf(stdout," * 1 tile\n"); - fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n"); - fprintf(stdout," * Size of code-block : 64 x 64\n"); - fprintf(stdout," * Number of resolutions: 6\n"); - fprintf(stdout," * No SOP marker in the codestream\n"); - fprintf(stdout," * No EPH marker in the codestream\n"); - fprintf(stdout," * No sub-sampling in x or y direction\n"); - fprintf(stdout," * No mode switch activated\n"); - fprintf(stdout," * Progression order: LRCP\n"); - fprintf(stdout," * No index file\n"); - fprintf(stdout," * No ROI upshifted\n"); - fprintf(stdout," * No offset of the origin of the image\n"); - fprintf(stdout," * No offset of the origin of the tiles\n"); - fprintf(stdout," * Reversible DWT 5-3\n"); -/* UniPG>> */ + fprintf(stdout,"HELP for opj_compress\n----\n\n"); + fprintf(stdout,"- the -h option displays this help information on screen\n\n"); + + /* UniPG>> */ + fprintf(stdout,"List of parameters for the JPEG 2000 " + #ifdef USE_JPWL + "+ JPWL " + #endif /* USE_JPWL */ + "encoder:\n"); + /* <<UniPG */ + fprintf(stdout,"\n"); + fprintf(stdout,"REMARKS:\n"); + fprintf(stdout,"---------\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n"); + fprintf(stdout,"COD and QCD never appear in the tile_header.\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"By default:\n"); + fprintf(stdout,"------------\n"); + fprintf(stdout,"\n"); + fprintf(stdout," * Lossless\n"); + fprintf(stdout," * 1 tile\n"); + fprintf(stdout," * RGB->YCC conversion if at least 3 components\n"); + fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n"); + fprintf(stdout," * Size of code-block : 64 x 64\n"); + fprintf(stdout," * Number of resolutions: 6\n"); + fprintf(stdout," * No SOP marker in the codestream\n"); + fprintf(stdout," * No EPH marker in the codestream\n"); + fprintf(stdout," * No sub-sampling in x or y direction\n"); + fprintf(stdout," * No mode switch activated\n"); + fprintf(stdout," * Progression order: LRCP\n"); + fprintf(stdout," * No index file\n"); + fprintf(stdout," * No ROI upshifted\n"); + fprintf(stdout," * No offset of the origin of the image\n"); + fprintf(stdout," * No offset of the origin of the tiles\n"); + fprintf(stdout," * Reversible DWT 5-3\n"); + /* UniPG>> */ #ifdef USE_JPWL - fprintf(stdout," * No JPWL protection\n"); + fprintf(stdout," * No JPWL protection\n"); #endif /* USE_JPWL */ -/* <<UniPG */ - fprintf(stdout,"\n"); - fprintf(stdout,"Parameters:\n"); - fprintf(stdout,"------------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"Required Parameters (except with -h):\n"); - fprintf(stdout,"One of the two options -ImgDir or -i must be used\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-ImgDir : Image file Directory path (example ../Images) \n"); - fprintf(stdout," When using this option -OutFor must be used\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-OutFor \n"); - fprintf(stdout," REQUIRED only if -ImgDir is used\n"); - fprintf(stdout," Need to specify only format without filename <BMP> \n"); - fprintf(stdout," Currently accepts PBM, PGM, PPM, PNM, PAM, PGX, PNG, BMP, TIF, RAW, RAWL and TGA formats\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-i : source file (-i source.pnm also *pbm, *.pgm, *.ppm, *.pam, *.pgx, *png, *.bmp, *.tif, *.raw, *.tga) \n"); - fprintf(stdout," When using this option -o must be used\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-o : destination file (-o dest.j2k or .jp2) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"Optional Parameters:\n"); - 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," 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"); - fprintf(stdout," Frames per second not required. Default value is 24fps\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n "); - fprintf(stdout," - The rate specified for each quality level is the desired \n"); - fprintf(stdout," compression factor.\n"); - fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n"); - fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n"); - fprintf(stdout,"\n"); - fprintf(stdout," (options -r and -q cannot be used together)\n "); - fprintf(stdout,"\n"); - - fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n "); - - fprintf(stdout," (options -r and -q cannot be used together)\n "); - - fprintf(stdout,"\n"); - fprintf(stdout,"-n : number of resolutions (-n 3) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-b : size of code block (-b 32,32) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-c : size of precinct (-c 128,128) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-t : size of tile (-t 512,512) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n"); - fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-POC : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n"); - fprintf(stdout," Example: T1=0,0,1,5,3,CPRL \n"); - fprintf(stdout," : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-SOP : write SOP marker before each packet \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-EPH : write EPH marker after each header packet \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n"); - fprintf(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n"); - fprintf(stdout," Indicate multiple modes by adding their values. \n"); - fprintf(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-TP : devide packets of every tile into tile-parts (-TP R) [R, L, C]\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n"); - fprintf(stdout," for component c=%%d [%%d = 0,1,2]\n"); - fprintf(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-d : offset of the origin of the image (-d 150,300) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-F : characteristics of the raw input image\n"); - fprintf(stdout," -F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stdout," Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-m : use array-based MCT, values are coma separated, line by line\n"); - fprintf(stdout," no specific separators between lines, no space allowed between values\n"); - fprintf(stdout,"-jpip : write jpip codestream index box in JP2 output file\n"); - fprintf(stdout," NOTICE: currently supports only RPCL order\n"); - fprintf(stdout,"\n"); -/* UniPG>> */ + /* <<UniPG */ + fprintf(stdout,"\n"); + fprintf(stdout,"Parameters:\n"); + fprintf(stdout,"------------\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"Required Parameters (except with -h):\n"); + fprintf(stdout,"One of the two options -ImgDir or -i must be used\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-ImgDir : Image file Directory path (example ../Images) \n"); + fprintf(stdout," When using this option -OutFor must be used\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-OutFor \n"); + fprintf(stdout," REQUIRED only if -ImgDir is used\n"); + fprintf(stdout," Need to specify only format without filename <BMP> \n"); + fprintf(stdout," Currently accepts PBM, PGM, PPM, PNM, PAM, PGX, PNG, BMP, TIF, RAW (MSB), RAWL (LSB) and TGA formats\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-i : source file (-i source.pnm also *pbm, *.pgm, *.ppm, *.pam, *.pgx, *png, *.bmp, *.tif, *.raw, *.tga) \n"); + fprintf(stdout," When using this option -o must be used\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-o : destination file (-o dest.j2k or .jp2) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"Optional Parameters:\n"); + 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," 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"); + fprintf(stdout," Frames per second not required. Default value is 24fps\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n "); + fprintf(stdout," - The rate specified for each quality level is the desired \n"); + fprintf(stdout," compression factor.\n"); + fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n"); + fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n"); + fprintf(stdout,"\n"); + fprintf(stdout," (options -r and -q cannot be used together)\n "); + fprintf(stdout,"\n"); + + fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n "); + + fprintf(stdout," (options -r and -q cannot be used together)\n "); + + fprintf(stdout,"\n"); + fprintf(stdout,"-n : number of resolutions (-n 3) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-b : size of code block (-b 32,32) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-c : size of precinct (-c 128,128) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-t : size of tile (-t 512,512) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n"); + fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-POC : Progression order change (-POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL) \n"); + fprintf(stdout," Example: T1=0,0,1,5,3,CPRL \n"); + fprintf(stdout," : Ttilenumber=Resolution num start,Component num start,Layer num end,Resolution num end,Component num end,Progression order\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-SOP : write SOP marker before each packet \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-EPH : write EPH marker after each header packet \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n"); + fprintf(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n"); + fprintf(stdout," Indicate multiple modes by adding their values. \n"); + fprintf(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-TP : divide packets of every tile into tile-parts (-TP R) [R, L, C]\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n"); + fprintf(stdout," for component c=%%d [%%d = 0,1,2]\n"); + fprintf(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI c=0,U=25) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-d : offset of the origin of the image (-d 150,300) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-F : characteristics of the raw input image\n"); + fprintf(stdout," -F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); + fprintf(stdout," Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"-mct {0,1,2} : explicitely specifies if an Multiple Component Transform has to be used.\n"); + fprintf(stdout," 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n"); + fprintf(stdout," If custom MCT, \"-m\" option has to be used (see hereunder).\n"); + fprintf(stdout," By default, RGB->YCC conversion is used if there are 3 components or more,\n"); + fprintf(stdout," no conversion otherwise.\n"); + fprintf(stdout,"-m <file> : use array-based MCT, values are coma separated, line by line\n"); + fprintf(stdout," no specific separators between lines, no space allowed between values\n"); + fprintf(stdout," If this option is used, it automatically sets \"-mct\" option to 2.\n"); + fprintf(stdout,"-jpip : write jpip codestream index box in JP2 output file\n"); + fprintf(stdout," NOTICE: currently supports only RPCL order\n"); + fprintf(stdout,"\n"); + /* UniPG>> */ #ifdef USE_JPWL - fprintf(stdout,"-W : adoption of JPWL (Part 11) capabilities (-W params)\n"); - fprintf(stdout," The parameters can be written and repeated in any order:\n"); - fprintf(stdout," [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n"); - fprintf(stdout," ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n"); - fprintf(stdout,"\n"); - fprintf(stdout," h selects the header error protection (EPB): 'type' can be\n"); - fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n"); - fprintf(stdout," if 'tilepart' is absent, it is for main and tile headers\n"); - fprintf(stdout," if 'tilepart' is present, it applies from that tile\n"); - fprintf(stdout," onwards, up to the next h<> spec, or to the last tilepart\n"); - fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS); - fprintf(stdout,"\n"); - fprintf(stdout," p selects the packet error protection (EEP/UEP with EPBs)\n"); - fprintf(stdout," to be applied to raw data: 'type' can be\n"); - fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n"); - fprintf(stdout," if 'tilepart:pack' is absent, it is from tile 0, packet 0\n"); - fprintf(stdout," if 'tilepart:pack' is present, it applies from that tile\n"); - fprintf(stdout," and that packet onwards, up to the next packet spec\n"); - fprintf(stdout," or to the last packet in the last tilepart in the stream\n"); - fprintf(stdout," (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS); - fprintf(stdout,"\n"); - fprintf(stdout," s enables sensitivity data insertion (ESD): 'method' can be\n"); - fprintf(stdout," [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n"); - fprintf(stdout," 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n"); - fprintf(stdout," if 'tilepart' is absent, it is for main header only\n"); - fprintf(stdout," if 'tilepart' is present, it applies from that tile\n"); - fprintf(stdout," onwards, up to the next s<> spec, or to the last tilepart\n"); - fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS); - fprintf(stdout,"\n"); - fprintf(stdout," g determines the addressing mode: <range> can be\n"); - fprintf(stdout," [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n"); - fprintf(stdout,"\n"); - fprintf(stdout," a determines the size of data addressing: <addr> can be\n"); - fprintf(stdout," 2/4 bytes (small/large codestreams). If not set, auto-mode\n"); - fprintf(stdout,"\n"); - fprintf(stdout," z determines the size of sensitivity values: <size> can be\n"); - fprintf(stdout," 1/2 bytes, for the transformed pseudo-floating point value\n"); - fprintf(stdout,"\n"); - fprintf(stdout," ex.:\n"); - fprintf(stdout," h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n"); - fprintf(stdout," s0=6,s3=-1,a=0,g=1,z=1\n"); - fprintf(stdout," means\n"); - fprintf(stdout," predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n"); - fprintf(stdout," CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n"); - fprintf(stdout," UEP rs(78,32) for packets 0 to 23 of tile 0,\n"); - fprintf(stdout," UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n"); - fprintf(stdout," UEP rs default for packets of tilepart 1,\n"); - fprintf(stdout," no UEP for packets 0 to 19 of tilepart 3,\n"); - fprintf(stdout," UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n"); - fprintf(stdout," relative sensitivity ESD for MH,\n"); - fprintf(stdout," TSE ESD from TPH 0 to TPH 2, byte range with automatic\n"); - fprintf(stdout," size of addresses and 1 byte for each sensitivity value\n"); - fprintf(stdout,"\n"); - fprintf(stdout," ex.:\n"); - fprintf(stdout," h,s,p\n"); - fprintf(stdout," means\n"); - fprintf(stdout," default protection to headers (MH and TPHs) as well as\n"); - fprintf(stdout," data packets, one ESD in MH\n"); - fprintf(stdout,"\n"); - fprintf(stdout," N.B.: use the following recommendations when specifying\n"); - fprintf(stdout," the JPWL parameters list\n"); - fprintf(stdout," - when you use UEP, always pair the 'p' option with 'h'\n"); - fprintf(stdout," \n"); + fprintf(stdout,"-W : adoption of JPWL (Part 11) capabilities (-W params)\n"); + fprintf(stdout," The parameters can be written and repeated in any order:\n"); + fprintf(stdout," [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n"); + fprintf(stdout," ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n"); + fprintf(stdout,"\n"); + fprintf(stdout," h selects the header error protection (EPB): 'type' can be\n"); + fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n"); + fprintf(stdout," if 'tilepart' is absent, it is for main and tile headers\n"); + fprintf(stdout," if 'tilepart' is present, it applies from that tile\n"); + fprintf(stdout," onwards, up to the next h<> spec, or to the last tilepart\n"); + fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS); + fprintf(stdout,"\n"); + fprintf(stdout," p selects the packet error protection (EEP/UEP with EPBs)\n"); + fprintf(stdout," to be applied to raw data: 'type' can be\n"); + fprintf(stdout," [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n"); + fprintf(stdout," if 'tilepart:pack' is absent, it is from tile 0, packet 0\n"); + fprintf(stdout," if 'tilepart:pack' is present, it applies from that tile\n"); + fprintf(stdout," and that packet onwards, up to the next packet spec\n"); + fprintf(stdout," or to the last packet in the last tilepart in the stream\n"); + fprintf(stdout," (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS); + fprintf(stdout,"\n"); + fprintf(stdout," s enables sensitivity data insertion (ESD): 'method' can be\n"); + fprintf(stdout," [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n"); + fprintf(stdout," 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n"); + fprintf(stdout," if 'tilepart' is absent, it is for main header only\n"); + fprintf(stdout," if 'tilepart' is present, it applies from that tile\n"); + fprintf(stdout," onwards, up to the next s<> spec, or to the last tilepart\n"); + fprintf(stdout," in the codestream (max. %d specs)\n", JPWL_MAX_NO_TILESPECS); + fprintf(stdout,"\n"); + fprintf(stdout," g determines the addressing mode: <range> can be\n"); + fprintf(stdout," [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n"); + fprintf(stdout,"\n"); + fprintf(stdout," a determines the size of data addressing: <addr> can be\n"); + fprintf(stdout," 2/4 bytes (small/large codestreams). If not set, auto-mode\n"); + fprintf(stdout,"\n"); + fprintf(stdout," z determines the size of sensitivity values: <size> can be\n"); + fprintf(stdout," 1/2 bytes, for the transformed pseudo-floating point value\n"); + fprintf(stdout,"\n"); + fprintf(stdout," ex.:\n"); + fprintf(stdout," h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n"); + fprintf(stdout," s0=6,s3=-1,a=0,g=1,z=1\n"); + fprintf(stdout," means\n"); + fprintf(stdout," predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n"); + fprintf(stdout," CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n"); + fprintf(stdout," UEP rs(78,32) for packets 0 to 23 of tile 0,\n"); + fprintf(stdout," UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n"); + fprintf(stdout," UEP rs default for packets of tilepart 1,\n"); + fprintf(stdout," no UEP for packets 0 to 19 of tilepart 3,\n"); + fprintf(stdout," UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n"); + fprintf(stdout," relative sensitivity ESD for MH,\n"); + fprintf(stdout," TSE ESD from TPH 0 to TPH 2, byte range with automatic\n"); + fprintf(stdout," size of addresses and 1 byte for each sensitivity value\n"); + fprintf(stdout,"\n"); + fprintf(stdout," ex.:\n"); + fprintf(stdout," h,s,p\n"); + fprintf(stdout," means\n"); + fprintf(stdout," default protection to headers (MH and TPHs) as well as\n"); + fprintf(stdout," data packets, one ESD in MH\n"); + fprintf(stdout,"\n"); + fprintf(stdout," N.B.: use the following recommendations when specifying\n"); + fprintf(stdout," the JPWL parameters list\n"); + fprintf(stdout," - when you use UEP, always pair the 'p' option with 'h'\n"); + fprintf(stdout," \n"); #endif /* USE_JPWL */ -/* <<UniPG */ - fprintf(stdout,"IMPORTANT:\n"); - fprintf(stdout,"-----------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"The index file has the structure below:\n"); - fprintf(stdout,"---------------------------------------\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"Image_height Image_width\n"); - fprintf(stdout,"progression order\n"); - fprintf(stdout,"Tiles_size_X Tiles_size_Y\n"); - fprintf(stdout,"Tiles_nb_X Tiles_nb_Y\n"); - fprintf(stdout,"Components_nb\n"); - fprintf(stdout,"Layers_nb\n"); - fprintf(stdout,"decomposition_levels\n"); - fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n"); - fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n"); - fprintf(stdout,"Main_header_start_position\n"); - fprintf(stdout,"Main_header_end_position\n"); - fprintf(stdout,"Codestream_size\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"INFO ON TILES\n"); - fprintf(stdout,"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n"); - fprintf(stdout,"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n"); - fprintf(stdout,"Tile_1 '' '' '' '' '' '' ''\n"); - fprintf(stdout,"...\n"); - fprintf(stdout,"Tile_Nt '' '' '' '' '' '' ''\n"); - fprintf(stdout,"...\n"); - fprintf(stdout,"TILE 0 DETAILS\n"); - fprintf(stdout,"part_nb tileno num_packs start_pos end_tph_pos end_pos\n"); - fprintf(stdout,"...\n"); - fprintf(stdout,"Progression_string\n"); - fprintf(stdout,"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n"); - fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n"); - fprintf(stdout,"...\n"); - fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n"); - - fprintf(stdout,"MaxDisto\n"); - - fprintf(stdout,"TotalDisto\n\n"); + /* <<UniPG */ + fprintf(stdout,"IMPORTANT:\n"); + fprintf(stdout,"-----------\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"The index file has the structure below:\n"); + fprintf(stdout,"---------------------------------------\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"Image_height Image_width\n"); + fprintf(stdout,"progression order\n"); + fprintf(stdout,"Tiles_size_X Tiles_size_Y\n"); + fprintf(stdout,"Tiles_nb_X Tiles_nb_Y\n"); + fprintf(stdout,"Components_nb\n"); + fprintf(stdout,"Layers_nb\n"); + fprintf(stdout,"decomposition_levels\n"); + fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n"); + fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n"); + fprintf(stdout,"Main_header_start_position\n"); + fprintf(stdout,"Main_header_end_position\n"); + fprintf(stdout,"Codestream_size\n"); + fprintf(stdout,"\n"); + fprintf(stdout,"INFO ON TILES\n"); + fprintf(stdout,"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n"); + fprintf(stdout,"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n"); + fprintf(stdout,"Tile_1 '' '' '' '' '' '' ''\n"); + fprintf(stdout,"...\n"); + fprintf(stdout,"Tile_Nt '' '' '' '' '' '' ''\n"); + fprintf(stdout,"...\n"); + fprintf(stdout,"TILE 0 DETAILS\n"); + fprintf(stdout,"part_nb tileno num_packs start_pos end_tph_pos end_pos\n"); + fprintf(stdout,"...\n"); + fprintf(stdout,"Progression_string\n"); + fprintf(stdout,"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n"); + fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n"); + fprintf(stdout,"...\n"); + fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n"); + + fprintf(stdout,"MaxDisto\n"); + + fprintf(stdout,"TotalDisto\n\n"); } static OPJ_PROG_ORDER give_progression(const char progression[4]) { - if(strncmp(progression, "LRCP", 4) == 0) { - return OPJ_LRCP; - } - if(strncmp(progression, "RLCP", 4) == 0) { - return OPJ_RLCP; - } - if(strncmp(progression, "RPCL", 4) == 0) { - return OPJ_RPCL; - } - if(strncmp(progression, "PCRL", 4) == 0) { - return OPJ_PCRL; - } - if(strncmp(progression, "CPRL", 4) == 0) { - return OPJ_CPRL; - } - - return OPJ_PROG_UNKNOWN; + if(strncmp(progression, "LRCP", 4) == 0) { + return OPJ_LRCP; + } + if(strncmp(progression, "RLCP", 4) == 0) { + return OPJ_RLCP; + } + if(strncmp(progression, "RPCL", 4) == 0) { + return OPJ_RPCL; + } + if(strncmp(progression, "PCRL", 4) == 0) { + return OPJ_PCRL; + } + if(strncmp(progression, "CPRL", 4) == 0) { + return OPJ_CPRL; + } + + return OPJ_PROG_UNKNOWN; } static unsigned int get_num_images(char *imgdirpath){ - DIR *dir; - struct dirent* content; - unsigned int num_images = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 0; - } - - num_images=0; - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - num_images++; - } - return num_images; + DIR *dir; + struct dirent* content; + unsigned int num_images = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir){ + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 0; + } + + num_images=0; + while((content=readdir(dir))!=NULL){ + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + num_images++; + } + return num_images; } static int load_images(dircnt_t *dirptr, char *imgdirpath){ - DIR *dir; - struct dirent* content; - int i = 0; - - /*Reading the input images from given input directory*/ - - dir= opendir(imgdirpath); - if(!dir){ - fprintf(stderr,"Could not open Folder %s\n",imgdirpath); - return 1; - }else { - fprintf(stderr,"Folder opened successfully\n"); - } - - while((content=readdir(dir))!=NULL){ - if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) - continue; - - strcpy(dirptr->filename[i],content->d_name); - i++; - } - return 0; + DIR *dir; + struct dirent* content; + int i = 0; + + /*Reading the input images from given input directory*/ + + dir= opendir(imgdirpath); + if(!dir){ + fprintf(stderr,"Could not open Folder %s\n",imgdirpath); + return 1; + }else { + fprintf(stderr,"Folder opened successfully\n"); + } + + while((content=readdir(dir))!=NULL){ + if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 ) + continue; + + strcpy(dirptr->filename[i],content->d_name); + i++; + } + return 0; } static int get_file_format(char *filename) { - unsigned int i; - static const char *extension[] = { - "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc" + unsigned int i; + static const char *extension[] = { + "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc" }; - static const int format[] = { - PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT + static const int format[] = { + PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT }; - char * ext = strrchr(filename, '.'); - if (ext == NULL) - return -1; - ext++; - for(i = 0; i < sizeof(format)/sizeof(*format); i++) { - if(strcasecmp(ext, extension[i]) == 0) { - return format[i]; - } - } - return -1; + char * ext = strrchr(filename, '.'); + if (ext == NULL) + return -1; + ext++; + for(i = 0; i < sizeof(format)/sizeof(*format); i++) { + if(strcasecmp(ext, extension[i]) == 0) { + return format[i]; + } + } + return -1; } static char * get_file_name(char *name){ - char *fname; - fname= (char*)malloc(OPJ_PATH_LEN*sizeof(char)); - fname= strtok(name,"."); - return fname; + char *fname = strtok(name,"."); + return fname; } static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){ - char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; - char *temp_p, temp1[OPJ_PATH_LEN]=""; - - strcpy(image_filename,dirptr->filename[imageno]); - fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); - parameters->decod_format = get_file_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*/ - strcpy(temp_ofname,get_file_name(image_filename)); - while((temp_p = strtok(NULL,".")) != NULL){ - strcat(temp_ofname,temp1); - sprintf(temp1,".%s",temp_p); - } - if(img_fol->set_out_format==1){ - sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); - strncpy(parameters->outfile, outfilename, sizeof(outfilename)); - } - return 0; + char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; + char *temp_p, temp1[OPJ_PATH_LEN]=""; + + strcpy(image_filename,dirptr->filename[imageno]); + fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename); + parameters->decod_format = get_file_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*/ + strcpy(temp_ofname,get_file_name(image_filename)); + while((temp_p = strtok(NULL,".")) != NULL){ + strcat(temp_ofname,temp1); + sprintf(temp1,".%s",temp_p); + } + if(img_fol->set_out_format==1){ + sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); + strncpy(parameters->outfile, outfilename, sizeof(outfilename)); + } + return 0; } -static int initialise_4K_poc(opj_poc_t *POC, int numres){ - POC[0].tile = 1; - POC[0].resno0 = 0; - POC[0].compno0 = 0; - POC[0].layno1 = 1; - POC[0].resno1 = numres-1; - POC[0].compno1 = 3; - 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 = OPJ_CPRL; - return 2; -} +/* ------------------------------------------------------------------------------------ */ -static void cinema_parameters(opj_cparameters_t *parameters){ - parameters->tile_size_on = OPJ_FALSE; - parameters->cp_tdx=1; - parameters->cp_tdy=1; +static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, + img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename) { + OPJ_UINT32 i, j; + int totlen, c; + opj_option_t long_option[]={ + {"cinema2K",REQ_ARG, NULL ,'w'}, + {"cinema4K",NO_ARG, NULL ,'y'}, + {"ImgDir",REQ_ARG, NULL ,'z'}, + {"TP",REQ_ARG, NULL ,'u'}, + {"SOP",NO_ARG, NULL ,'S'}, + {"EPH",NO_ARG, NULL ,'E'}, + {"OutFor",REQ_ARG, NULL ,'O'}, + {"POC",REQ_ARG, NULL ,'P'}, + {"ROI",REQ_ARG, NULL ,'R'}, + {"jpip",NO_ARG, NULL, 'J'}, + {"mct",REQ_ARG, NULL, 'Y'} + }; - /*Tile part*/ - parameters->tp_flag = 'C'; - parameters->tp_on = 1; + /* parse the command line */ + const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:" + #ifdef USE_JPWL + "W:" + #endif /* USE_JPWL */ + "h"; + + totlen=sizeof(long_option); + img_fol->set_out_format=0; + raw_cp->rawWidth = 0; + + do{ + c = opj_getopt_long(argc, argv, optlist,long_option,totlen); + if (c == -1) + break; + switch (c) { + case 'i': /* input file */ + { + char *infile = opj_optarg; + parameters->decod_format = get_file_format(infile); + switch(parameters->decod_format) { + case PGX_DFMT: + case PXM_DFMT: + case BMP_DFMT: + case TIF_DFMT: + case RAW_DFMT: + case RAWL_DFMT: + case TGA_DFMT: + case PNG_DFMT: + break; + default: + fprintf(stderr, + "!! Unrecognized format for infile : %s " + "[accept only *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif, *.raw or *.tga] !!\n\n", + infile); + return 1; + } + strncpy(parameters->infile, infile, sizeof(parameters->infile)-1); + } + break; + + /* ----------------------------------------------------- */ + + case 'o': /* output file */ + { + char *outfile = opj_optarg; + parameters->cod_format = get_file_format(outfile); + switch(parameters->cod_format) { + case J2K_CFMT: + case JP2_CFMT: + break; + default: + fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile); + return 1; + } + strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1); + } + break; + + /* ----------------------------------------------------- */ + case 'O': /* output format */ + { + char outformat[50]; + char *of = opj_optarg; + sprintf(outformat,".%s",of); + img_fol->set_out_format = 1; + parameters->cod_format = get_file_format(outformat); + switch(parameters->cod_format) { + case J2K_CFMT: + case JP2_CFMT: + img_fol->out_format = opj_optarg; + break; + default: + fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n"); + return 1; + } + } + break; + + + /* ----------------------------------------------------- */ + + + case 'r': /* rates rates/distorsion */ + { + char *s = opj_optarg; + parameters->tcp_numlayers = 0; + while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) { + parameters->tcp_numlayers++; + while (*s && *s != ',') { + s++; + } + if (!*s) + break; + s++; + } + parameters->cp_disto_alloc = 1; + } + break; + + /* ----------------------------------------------------- */ + + + case 'F': /* Raw image format parameters */ + { + char signo; + char *s = opj_optarg; + if (sscanf(s, "%d,%d,%d,%d,%c", &raw_cp->rawWidth, &raw_cp->rawHeight, &raw_cp->rawComp, &raw_cp->rawBitDepth, &signo) == 5) { + if (signo == 's') { + raw_cp->rawSigned = OPJ_TRUE; + fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Signed\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth); + } + else if (signo == 'u') { + raw_cp->rawSigned = OPJ_FALSE; + fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Unsigned\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth); + } + else { + fprintf(stderr,"\nError: invalid raw image parameters: Unknown sign of raw file\n"); + fprintf(stderr,"Please use the Format option -F:\n"); + fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stderr,"Aborting\n"); + } + } + else { + fprintf(stderr,"\nError: invalid raw image parameters\n"); + fprintf(stderr,"Please use the Format option -F:\n"); + fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stderr,"Aborting\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + + case 'q': /* add fixed_quality */ + { + char *s = opj_optarg; + while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) { + parameters->tcp_numlayers++; + while (*s && *s != ',') { + s++; + } + if (!*s) + break; + s++; + } + parameters->cp_fixed_quality = 1; + } + break; + + /* dda */ + /* ----------------------------------------------------- */ + + case 'f': /* mod fixed_quality (before : -q) */ + { + int *row = NULL, *col = NULL; + OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0; + + char *s = opj_optarg; + sscanf(s, "%ud", &numlayers); + s++; + if (numlayers > 9) + s++; + + parameters->tcp_numlayers = (int)numlayers; + numresolution = (OPJ_UINT32)parameters->numresolution; + matrix_width = numresolution * 3; + parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int)); + s = s + 2; + + for (i = 0; i < numlayers; i++) { + row = ¶meters->cp_matrice[i * matrix_width]; + col = row; + parameters->tcp_rates[i] = 1; + sscanf(s, "%d,", &col[0]); + s += 2; + if (col[0] > 9) + s++; + col[1] = 0; + col[2] = 0; + for (j = 1; j < numresolution; j++) { + col += 3; + sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]); + s += 6; + if (col[0] > 9) + s++; + if (col[1] > 9) + s++; + if (col[2] > 9) + s++; + } + if (i < numlayers - 1) + s++; + } + parameters->cp_fixed_alloc = 1; + } + break; - /*Tile and Image shall be at (0,0)*/ - parameters->cp_tx0 = 0; - parameters->cp_ty0 = 0; - parameters->image_offset_x0 = 0; - parameters->image_offset_y0 = 0; + /* ----------------------------------------------------- */ - /*Codeblock size= 32*32*/ - parameters->cblockw_init = 32; - parameters->cblockh_init = 32; - parameters->csty |= 0x01; + case 't': /* tiles */ + { + sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy); + parameters->tile_size_on = OPJ_TRUE; + } + break; - /*The progression order shall be CPRL*/ - parameters->prog_order = OPJ_CPRL; + /* ----------------------------------------------------- */ - /* No ROI */ - parameters->roi_compno = -1; + case 'n': /* resolution */ + { + sscanf(opj_optarg, "%d", ¶meters->numresolution); + } + break; + + /* ----------------------------------------------------- */ + case 'c': /* precinct dimension */ + { + char sep; + int res_spec = 0; + + char *s = opj_optarg; + int ret; + do { + sep = 0; + ret = sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec], + ¶meters->prch_init[res_spec], &sep); + if( !(ret == 2 && sep == 0) && !(ret == 3 && sep == ',') ) + { + fprintf(stderr,"\nError: could not parse precinct dimension: '%s' %x\n", s, sep); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n"); + return 1; + } + parameters->csty |= 0x01; + res_spec++; + s = strpbrk(s, "]") + 2; + } + while (sep == ','); + parameters->res_spec = res_spec; + } + break; + + /* ----------------------------------------------------- */ + + case 'b': /* code-block dimension */ + { + int cblockw_init = 0, cblockh_init = 0; + sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init); + if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024 + || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) { + fprintf(stderr, + "!! Size of code_block error (option -b) !!\n\nRestriction :\n" + " * width*height<=4096\n * 4<=width,height<= 1024\n\n"); + return 1; + } + parameters->cblockw_init = cblockw_init; + parameters->cblockh_init = cblockh_init; + } + break; - parameters->subsampling_dx = 1; parameters->subsampling_dy = 1; + /* ----------------------------------------------------- */ - /* 9-7 transform */ - parameters->irreversible = 1; + case 'x': /* creation of index file */ + { + char *index = opj_optarg; + strncpy(indexfilename, index, OPJ_PATH_LEN); + } + break; -} + /* ----------------------------------------------------- */ -static void cinema_setup_encoder(opj_cparameters_t *parameters,opj_image_t *image, img_fol_t *img_fol){ - int i; - float temp_rate; + case 'p': /* progression order */ + { + char progression[4]; - switch (parameters->cp_cinema){ - case OPJ_CINEMA2K_24: - case OPJ_CINEMA2K_48: - if(parameters->numresolution > 6){ - parameters->numresolution = 6; - } - if (!((image->comps[0].w == 2048) | (image->comps[0].h == 1080))){ - 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 = OPJ_STD_RSIZ; - } - break; - - case OPJ_CINEMA4K_24: - if(parameters->numresolution < 1){ - parameters->numresolution = 1; - }else if(parameters->numresolution > 7){ - parameters->numresolution = 7; - } - if (!((image->comps[0].w == 4096) | (image->comps[0].h == 2160))){ - 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 = OPJ_STD_RSIZ; - } - parameters->numpocs = initialise_4K_poc(parameters->POC,parameters->numresolution); - break; - default : - break; - } - - switch (parameters->cp_cinema){ - 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){ - parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy); - }else{ - temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy); - if (temp_rate > CINEMA_24_CS ){ - parameters->tcp_rates[i]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy); - }else{ - parameters->tcp_rates[i]= img_fol->rates[i]; - } - } - } - parameters->max_comp_size = COMP_24_CS; - break; - - case OPJ_CINEMA2K_48: - for(i=0 ; i<parameters->tcp_numlayers ; i++){ - temp_rate = 0 ; - if (img_fol->rates[i]== 0){ - parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy); - }else{ - temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (img_fol->rates[i] * 8 * image->comps[0].dx * image->comps[0].dy); - if (temp_rate > CINEMA_48_CS ){ - parameters->tcp_rates[0]= ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/ - (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy); - }else{ - parameters->tcp_rates[i]= img_fol->rates[i]; - } - } - } - parameters->max_comp_size = COMP_48_CS; - break; - default: - break; - } - parameters->cp_disto_alloc = 1; -} + strncpy(progression, opj_optarg, 4); + parameters->prog_order = give_progression(progression); + if (parameters->prog_order == -1) { + fprintf(stderr, "Unrecognized progression order " + "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); + return 1; + } + } + break; -/* ------------------------------------------------------------------------------------ */ + /* ----------------------------------------------------- */ -static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters, - img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename) { - int i, j, totlen, c; - opj_option_t long_option[]={ - {"cinema2K",REQ_ARG, NULL ,'w'}, - {"cinema4K",NO_ARG, NULL ,'y'}, - {"ImgDir",REQ_ARG, NULL ,'z'}, - {"TP",REQ_ARG, NULL ,'u'}, - {"SOP",NO_ARG, NULL ,'S'}, - {"EPH",NO_ARG, NULL ,'E'}, - {"OutFor",REQ_ARG, NULL ,'O'}, - {"POC",REQ_ARG, NULL ,'P'}, - {"ROI",REQ_ARG, NULL ,'R'}, - {"jpip",NO_ARG, NULL, 'J'} - }; - - /* parse the command line */ - const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:J" -#ifdef USE_JPWL - "W:" -#endif /* USE_JPWL */ - "h"; - - totlen=sizeof(long_option); - img_fol->set_out_format=0; - raw_cp->rawWidth = 0; - - do{ - c = opj_getopt_long(argc, argv, optlist,long_option,totlen); - if (c == -1) - break; - switch (c) { - case 'i': /* input file */ - { - char *infile = opj_optarg; - parameters->decod_format = get_file_format(infile); - switch(parameters->decod_format) { - case PGX_DFMT: - case PXM_DFMT: - case BMP_DFMT: - case TIF_DFMT: - case RAW_DFMT: - case RAWL_DFMT: - case TGA_DFMT: - case PNG_DFMT: - break; - default: - fprintf(stderr, - "!! Unrecognized format for infile : %s " - "[accept only *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif, *.raw or *.tga] !!\n\n", - infile); - return 1; - } - strncpy(parameters->infile, infile, sizeof(parameters->infile)-1); - } - break; - - /* ----------------------------------------------------- */ - - case 'o': /* output file */ - { - char *outfile = opj_optarg; - parameters->cod_format = get_file_format(outfile); - switch(parameters->cod_format) { - case J2K_CFMT: - case JP2_CFMT: - break; - default: - fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile); - return 1; - } - strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1); - } - break; - - /* ----------------------------------------------------- */ - case 'O': /* output format */ - { - char outformat[50]; - char *of = opj_optarg; - sprintf(outformat,".%s",of); - img_fol->set_out_format = 1; - parameters->cod_format = get_file_format(outformat); - switch(parameters->cod_format) { - case J2K_CFMT: - case JP2_CFMT: - img_fol->out_format = opj_optarg; - break; - default: - fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n"); - return 1; - } - } - break; - - - /* ----------------------------------------------------- */ - - - case 'r': /* rates rates/distorsion */ - { - char *s = opj_optarg; - parameters->tcp_numlayers = 0; - while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) == 1) { - parameters->tcp_numlayers++; - while (*s && *s != ',') { - s++; - } - if (!*s) - break; - s++; - } - parameters->cp_disto_alloc = 1; - } - break; - - /* ----------------------------------------------------- */ - - - case 'F': /* Raw image format parameters */ - { - char signo; - char *s = opj_optarg; - if (sscanf(s, "%d,%d,%d,%d,%c", &raw_cp->rawWidth, &raw_cp->rawHeight, &raw_cp->rawComp, &raw_cp->rawBitDepth, &signo) == 5) { - if (signo == 's') { - raw_cp->rawSigned = OPJ_TRUE; - fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Signed\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth); - } - else if (signo == 'u') { - raw_cp->rawSigned = OPJ_FALSE; - fprintf(stdout,"\nRaw file parameters: %d,%d,%d,%d Unsigned\n", raw_cp->rawWidth, raw_cp->rawHeight, raw_cp->rawComp, raw_cp->rawBitDepth); - } - else { - fprintf(stderr,"\nError: invalid raw image parameters: Unknown sign of raw file\n"); - fprintf(stderr,"Please use the Format option -F:\n"); - fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); - fprintf(stderr,"Aborting\n"); - } - } - else { - fprintf(stderr,"\nError: invalid raw image parameters\n"); - fprintf(stderr,"Please use the Format option -F:\n"); - fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); - fprintf(stderr,"Aborting\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'q': /* add fixed_quality */ - { - char *s = opj_optarg; - while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers]) == 1) { - parameters->tcp_numlayers++; - while (*s && *s != ',') { - s++; - } - if (!*s) - break; - s++; - } - parameters->cp_fixed_quality = 1; - } - break; - - /* dda */ - /* ----------------------------------------------------- */ - - case 'f': /* mod fixed_quality (before : -q) */ - { - int *row = NULL, *col = NULL; - int numlayers = 0, numresolution = 0, matrix_width = 0; - - char *s = opj_optarg; - sscanf(s, "%d", &numlayers); - s++; - if (numlayers > 9) - s++; - - parameters->tcp_numlayers = numlayers; - numresolution = parameters->numresolution; - matrix_width = numresolution * 3; - parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int)); - s = s + 2; - - for (i = 0; i < numlayers; i++) { - row = ¶meters->cp_matrice[i * matrix_width]; - col = row; - parameters->tcp_rates[i] = 1; - sscanf(s, "%d,", &col[0]); - s += 2; - if (col[0] > 9) - s++; - col[1] = 0; - col[2] = 0; - for (j = 1; j < numresolution; j++) { - col += 3; - sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]); - s += 6; - if (col[0] > 9) - s++; - if (col[1] > 9) - s++; - if (col[2] > 9) - s++; - } - if (i < numlayers - 1) - s++; - } - parameters->cp_fixed_alloc = 1; - } - break; - - /* ----------------------------------------------------- */ - - case 't': /* tiles */ - { - sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy); - parameters->tile_size_on = OPJ_TRUE; - } - break; - - /* ----------------------------------------------------- */ - - case 'n': /* resolution */ - { - sscanf(opj_optarg, "%d", ¶meters->numresolution); - } - break; - - /* ----------------------------------------------------- */ - case 'c': /* precinct dimension */ - { - char sep; - int res_spec = 0; - - char *s = opj_optarg; - do { - sep = 0; - sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec], - ¶meters->prch_init[res_spec], &sep); - parameters->csty |= 0x01; - res_spec++; - s = strpbrk(s, "]") + 2; - } - while (sep == ','); - parameters->res_spec = res_spec; - } - break; - - /* ----------------------------------------------------- */ - - case 'b': /* code-block dimension */ - { - int cblockw_init = 0, cblockh_init = 0; - sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init); - if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024 - || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) { - fprintf(stderr, - "!! Size of code_block error (option -b) !!\n\nRestriction :\n" - " * width*height<=4096\n * 4<=width,height<= 1024\n\n"); - return 1; - } - parameters->cblockw_init = cblockw_init; - parameters->cblockh_init = cblockh_init; - } - break; - - /* ----------------------------------------------------- */ - - case 'x': /* creation of index file */ - { - char *index = opj_optarg; - strncpy(indexfilename, index, OPJ_PATH_LEN); - } - break; - - /* ----------------------------------------------------- */ - - case 'p': /* progression order */ - { - char progression[4]; - - strncpy(progression, opj_optarg, 4); - parameters->prog_order = give_progression(progression); - if (parameters->prog_order == -1) { - fprintf(stderr, "Unrecognized progression order " - "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 's': /* subsampling factor */ - { - if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx, - ¶meters->subsampling_dy) != 2) { - fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'd': /* coordonnate of the reference grid */ - { - if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0, - ¶meters->image_offset_y0) != 2) { - fprintf(stderr, "-d 'coordonnate of the reference grid' argument " - "error !! [-d x0,y0]\n"); - return 1; - } - } - break; - - /* ----------------------------------------------------- */ - - case 'h': /* display an help description */ - encode_help_display(); - return 1; - - /* ----------------------------------------------------- */ - - case 'P': /* POC */ - { - int numpocs = 0; /* number of progression order change (POC) default 0 */ - opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */ - - char *s = opj_optarg; - POC = parameters->POC; - - while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile, - &POC[numpocs].resno0, &POC[numpocs].compno0, - &POC[numpocs].layno1, &POC[numpocs].resno1, - &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { - POC[numpocs].prg1 = give_progression(POC[numpocs].progorder); - numpocs++; - while (*s && *s != '/') { - s++; - } - if (!*s) { - break; - } - s++; - } - parameters->numpocs = numpocs; - } - break; - - /* ------------------------------------------------------ */ - - case 'S': /* SOP marker */ - { - parameters->csty |= 0x02; - } - break; - - /* ------------------------------------------------------ */ - - case 'E': /* EPH marker */ - { - parameters->csty |= 0x04; - } - break; - - /* ------------------------------------------------------ */ - - case 'M': /* Mode switch pas tous au point !! */ - { - int value = 0; - if (sscanf(opj_optarg, "%d", &value) == 1) { - for (i = 0; i <= 5; i++) { - int cache = value & (1 << i); - if (cache) - parameters->mode |= (1 << i); - } - } - } - break; - - /* ------------------------------------------------------ */ - - case 'R': /* ROI */ - { - if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno, - ¶meters->roi_shift) != 2) { - fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n"); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'T': /* Tile offset */ - { - if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0) != 2) { - fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]"); - return 1; - } - } - break; - - /* ------------------------------------------------------ */ - - case 'C': /* add a comment */ - { - parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1); - if(parameters->cp_comment) { - strcpy(parameters->cp_comment, opj_optarg); - } - } - break; - - - /* ------------------------------------------------------ */ - - case 'I': /* reversible or not */ - { - parameters->irreversible = 1; - } - break; - - /* ------------------------------------------------------ */ - - case 'u': /* Tile part generation*/ - { - parameters->tp_flag = opj_optarg[0]; - parameters->tp_on = 1; - } - break; - - /* ------------------------------------------------------ */ - - case 'z': /* Image Directory path */ - { - img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); - strcpy(img_fol->imgdirpath,opj_optarg); - img_fol->set_imgdir=1; - } - break; - - /* ------------------------------------------------------ */ - - case 'w': /* Digital Cinema 2K profile compliance*/ - { - int fps=0; - sscanf(opj_optarg,"%d",&fps); - if(fps == 24){ - parameters->cp_cinema = OPJ_CINEMA2K_24; - }else if(fps == 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 = OPJ_CINEMA2K; - - } - break; - - /* ------------------------------------------------------ */ - - case 'y': /* Digital Cinema 4K profile compliance*/ - { - parameters->cp_cinema = OPJ_CINEMA4K_24; - fprintf(stdout,"CINEMA 4K compliant codestream\n"); - parameters->cp_rsiz = OPJ_CINEMA4K; - } - break; - - /* ------------------------------------------------------ */ - case 'm': /* mct input file */ - { - char *lFilename = opj_optarg; - char *lMatrix; - char *lCurrentPtr ; - float *lCurrentDoublePtr; - float *lSpace; - int *l_int_ptr; - int lNbComp = 0, lTotalComp, lMctComp, i, lStrLen; - - /* Open file */ - FILE * lFile = fopen(lFilename,"r"); - if (lFile == NULL) { - return 1; - } - - /* Set size of file and read its content*/ - fseek(lFile,0,SEEK_END); - lStrLen = ftell(lFile); - fseek(lFile,0,SEEK_SET); - lMatrix = (char *) malloc(lStrLen + 1); - fread(lMatrix, lStrLen, 1, lFile); - fclose(lFile); - - lMatrix[lStrLen] = 0; - lCurrentPtr = lMatrix; - - /* replace ',' by 0 */ - while (*lCurrentPtr != 0 ) { - if (*lCurrentPtr == ' ') { - *lCurrentPtr = 0; - ++lNbComp; - } - ++lCurrentPtr; - } - ++lNbComp; - lCurrentPtr = lMatrix; - - lNbComp = (int) (sqrt(4*lNbComp + 1)/2. - 0.5); - lMctComp = lNbComp * lNbComp; - lTotalComp = lMctComp + lNbComp; - lSpace = (float *) malloc(lTotalComp * sizeof(float)); - lCurrentDoublePtr = lSpace; - for (i=0;i<lMctComp;++i) { - lStrLen = strlen(lCurrentPtr) + 1; - *lCurrentDoublePtr++ = (float) atof(lCurrentPtr); - lCurrentPtr += lStrLen; - } - - l_int_ptr = (int*) lCurrentDoublePtr; - for (i=0;i<lNbComp;++i) { - lStrLen = strlen(lCurrentPtr) + 1; - *l_int_ptr++ = atoi(lCurrentPtr); - lCurrentPtr += lStrLen; - } - - /* TODO should not be here ! */ - opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp), lNbComp); - - /* Free memory*/ - free(lSpace); - free(lMatrix); - } - break; - - - /* ------------------------------------------------------ */ - -/* UniPG>> */ + case 's': /* subsampling factor */ + { + if (sscanf(opj_optarg, "%d,%d", ¶meters->subsampling_dx, + ¶meters->subsampling_dy) != 2) { + fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + + case 'd': /* coordonnate of the reference grid */ + { + if (sscanf(opj_optarg, "%d,%d", ¶meters->image_offset_x0, + ¶meters->image_offset_y0) != 2) { + fprintf(stderr, "-d 'coordonnate of the reference grid' argument " + "error !! [-d x0,y0]\n"); + return 1; + } + } + break; + + /* ----------------------------------------------------- */ + + case 'h': /* display an help description */ + encode_help_display(); + return 1; + + /* ----------------------------------------------------- */ + + case 'P': /* POC */ + { + int numpocs = 0; /* number of progression order change (POC) default 0 */ + opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */ + + char *s = opj_optarg; + POC = parameters->POC; + + while (sscanf(s, "T%ud=%ud,%ud,%ud,%ud,%ud,%4s", &POC[numpocs].tile, + &POC[numpocs].resno0, &POC[numpocs].compno0, + &POC[numpocs].layno1, &POC[numpocs].resno1, + &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { + POC[numpocs].prg1 = give_progression(POC[numpocs].progorder); + numpocs++; + while (*s && *s != '/') { + s++; + } + if (!*s) { + break; + } + s++; + } + parameters->numpocs = (OPJ_UINT32)numpocs; + } + break; + + /* ------------------------------------------------------ */ + + case 'S': /* SOP marker */ + { + parameters->csty |= 0x02; + } + break; + + /* ------------------------------------------------------ */ + + case 'E': /* EPH marker */ + { + parameters->csty |= 0x04; + } + break; + + /* ------------------------------------------------------ */ + + case 'M': /* Mode switch pas tous au point !! */ + { + int value = 0; + if (sscanf(opj_optarg, "%d", &value) == 1) { + for (i = 0; i <= 5; i++) { + int cache = value & (1 << i); + if (cache) + parameters->mode |= (1 << i); + } + } + } + break; + + /* ------------------------------------------------------ */ + + case 'R': /* ROI */ + { + if (sscanf(opj_optarg, "c=%d,U=%d", ¶meters->roi_compno, + ¶meters->roi_shift) != 2) { + fprintf(stderr, "ROI error !! [-ROI c='compno',U='shift']\n"); + return 1; + } + } + break; + + /* ------------------------------------------------------ */ + + case 'T': /* Tile offset */ + { + if (sscanf(opj_optarg, "%d,%d", ¶meters->cp_tx0, ¶meters->cp_ty0) != 2) { + fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]"); + return 1; + } + } + break; + + /* ------------------------------------------------------ */ + + case 'C': /* add a comment */ + { + parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1); + if(parameters->cp_comment) { + strcpy(parameters->cp_comment, opj_optarg); + } + } + break; + + + /* ------------------------------------------------------ */ + + case 'I': /* reversible or not */ + { + parameters->irreversible = 1; + } + break; + + /* ------------------------------------------------------ */ + + case 'u': /* Tile part generation*/ + { + parameters->tp_flag = opj_optarg[0]; + parameters->tp_on = 1; + } + break; + + /* ------------------------------------------------------ */ + + case 'z': /* Image Directory path */ + { + img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + strcpy(img_fol->imgdirpath,opj_optarg); + img_fol->set_imgdir=1; + } + break; + + /* ------------------------------------------------------ */ + + case 'w': /* Digital Cinema 2K profile compliance*/ + { + int fps=0; + sscanf(opj_optarg,"%d",&fps); + if(fps == 24){ + parameters->cp_cinema = OPJ_CINEMA2K_24; + }else if(fps == 48 ){ + parameters->cp_cinema = OPJ_CINEMA2K_48; + }else { + fprintf(stderr,"Incorrect value!! must be 24 or 48\n"); + return 1; + } + fprintf(stdout,"CINEMA 2K profile activated\n" + "Other options specified could be overriden\n"); + + } + break; + + /* ------------------------------------------------------ */ + + case 'y': /* Digital Cinema 4K profile compliance*/ + { + parameters->cp_cinema = OPJ_CINEMA4K_24; + fprintf(stdout,"CINEMA 4K profile activated\n" + "Other options specified could be overriden\n"); + } + break; + + /* ------------------------------------------------------ */ + + case 'Y': /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/ + { + int mct_mode=0; + sscanf(opj_optarg,"%d",&mct_mode); + if(mct_mode < 0 || mct_mode > 2){ + fprintf(stderr,"MCT incorrect value!! Current accepted values are 0, 1 or 2.\n"); + return 1; + } + parameters->tcp_mct = (char) mct_mode; + } + break; + + /* ------------------------------------------------------ */ + + + case 'm': /* mct input file */ + { + char *lFilename = opj_optarg; + char *lMatrix; + char *lCurrentPtr ; + float *lCurrentDoublePtr; + float *lSpace; + int *l_int_ptr; + int lNbComp = 0, lTotalComp, lMctComp, i2; + size_t lStrLen, lStrFread; + + /* Open file */ + FILE * lFile = fopen(lFilename,"r"); + if (lFile == NULL) { + return 1; + } + + /* Set size of file and read its content*/ + fseek(lFile,0,SEEK_END); + lStrLen = (size_t)ftell(lFile); + fseek(lFile,0,SEEK_SET); + lMatrix = (char *) malloc(lStrLen + 1); + lStrFread = fread(lMatrix, 1, lStrLen, lFile); + fclose(lFile); + if( lStrLen != lStrFread ) return 1; + + lMatrix[lStrLen] = 0; + lCurrentPtr = lMatrix; + + /* replace ',' by 0 */ + while (*lCurrentPtr != 0 ) { + if (*lCurrentPtr == ' ') { + *lCurrentPtr = 0; + ++lNbComp; + } + ++lCurrentPtr; + } + ++lNbComp; + lCurrentPtr = lMatrix; + + lNbComp = (int) (sqrt(4*lNbComp + 1)/2. - 0.5); + lMctComp = lNbComp * lNbComp; + lTotalComp = lMctComp + lNbComp; + lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float)); + lCurrentDoublePtr = lSpace; + for (i2=0;i2<lMctComp;++i2) { + lStrLen = strlen(lCurrentPtr) + 1; + *lCurrentDoublePtr++ = (float) atof(lCurrentPtr); + lCurrentPtr += lStrLen; + } + + l_int_ptr = (int*) lCurrentDoublePtr; + for (i2=0;i2<lNbComp;++i2) { + lStrLen = strlen(lCurrentPtr) + 1; + *l_int_ptr++ = atoi(lCurrentPtr); + lCurrentPtr += lStrLen; + } + + /* TODO should not be here ! */ + opj_set_MCT(parameters, lSpace, (int *)(lSpace + lMctComp), (OPJ_UINT32)lNbComp); + + /* Free memory*/ + free(lSpace); + free(lMatrix); + } + break; + + + /* ------------------------------------------------------ */ + + /* UniPG>> */ #ifdef USE_JPWL - /* ------------------------------------------------------ */ - - case 'W': /* JPWL capabilities switched on */ - { - char *token = NULL; - int hprot, pprot, sens, addr, size, range; - - /* we need to enable indexing */ - if (!indexfilename || !*indexfilename) { - strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN); - } - - /* search for different protection methods */ - - /* break the option in comma points and parse the result */ - token = strtok(opj_optarg, ","); - while(token != NULL) { - - /* search header error protection method */ - if (*token == 'h') { - - static int tile = 0, tilespec = 0, lasttileno = 0; - - hprot = 1; /* predefined method */ - - if(sscanf(token, "h=%d", &hprot) == 1) { - /* Main header, specified */ - if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || - ((hprot >= 37) && (hprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot); - return 1; - } - parameters->jpwl_hprot_MH = hprot; - - } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) { - /* Tile part header, specified */ - if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || - ((hprot >= 37) && (hprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot); - return 1; - } - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile); - return 1; - } - if (tilespec < JPWL_MAX_NO_TILESPECS) { - parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile; - parameters->jpwl_hprot_TPH[tilespec++] = hprot; - } - - } else if(sscanf(token, "h%d", &tile) == 1) { - /* Tile part header, unspecified */ - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile); - return 1; - } - if (tilespec < JPWL_MAX_NO_TILESPECS) { - parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile; - parameters->jpwl_hprot_TPH[tilespec++] = hprot; - } - - - } else if (!strcmp(token, "h")) { - /* Main header, unspecified */ - parameters->jpwl_hprot_MH = hprot; - - } else { - fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token); - return 1; - }; - - } - - /* search packet error protection method */ - if (*token == 'p') { - - static int pack = 0, tile = 0, packspec = 0; - - pprot = 1; /* predefined method */ - - if (sscanf(token, "p=%d", &pprot) == 1) { - /* Method for all tiles and all packets */ - if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot); - return 1; - } - parameters->jpwl_pprot_tileno[0] = 0; - parameters->jpwl_pprot_packno[0] = 0; - parameters->jpwl_pprot[0] = pprot; - - } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) { - /* method specified from that tile on */ - if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); - return 1; - } - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); - return 1; - } - if (packspec < JPWL_MAX_NO_PACKSPECS) { - parameters->jpwl_pprot_tileno[packspec] = tile; - parameters->jpwl_pprot_packno[packspec] = 0; - parameters->jpwl_pprot[packspec++] = pprot; - } - - } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) { - /* method fully specified from that tile and that packet on */ - if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); - return 1; - } - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); - return 1; - } - if (pack < 0) { - fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack); - return 1; - } - if (packspec < JPWL_MAX_NO_PACKSPECS) { - parameters->jpwl_pprot_tileno[packspec] = tile; - parameters->jpwl_pprot_packno[packspec] = pack; - parameters->jpwl_pprot[packspec++] = pprot; - } - - } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) { - /* default method from that tile and that packet on */ - if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || - ((pprot >= 37) && (pprot <= 128)))) { - fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); - return 1; - } - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); - return 1; - } - if (pack < 0) { - fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack); - return 1; - } - if (packspec < JPWL_MAX_NO_PACKSPECS) { - parameters->jpwl_pprot_tileno[packspec] = tile; - parameters->jpwl_pprot_packno[packspec] = pack; - parameters->jpwl_pprot[packspec++] = pprot; - } - - } else if (sscanf(token, "p%d", &tile) == 1) { - /* default from a tile on */ - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); - return 1; - } - if (packspec < JPWL_MAX_NO_PACKSPECS) { - parameters->jpwl_pprot_tileno[packspec] = tile; - parameters->jpwl_pprot_packno[packspec] = 0; - parameters->jpwl_pprot[packspec++] = pprot; - } - - - } else if (!strcmp(token, "p")) { - /* all default */ - parameters->jpwl_pprot_tileno[0] = 0; - parameters->jpwl_pprot_packno[0] = 0; - parameters->jpwl_pprot[0] = pprot; - - } else { - fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token); - return 1; - }; - - } - - /* search sensitivity method */ - if (*token == 's') { - - static int tile = 0, tilespec = 0, lasttileno = 0; - - sens = 0; /* predefined: relative error */ - - if(sscanf(token, "s=%d", &sens) == 1) { - /* Main header, specified */ - if ((sens < -1) || (sens > 7)) { - fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n", sens); - return 1; - } - parameters->jpwl_sens_MH = sens; - - } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) { - /* Tile part header, specified */ - if ((sens < -1) || (sens > 7)) { - fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens); - return 1; - } - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile); - return 1; - } - if (tilespec < JPWL_MAX_NO_TILESPECS) { - parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile; - parameters->jpwl_sens_TPH[tilespec++] = sens; - } - - } else if(sscanf(token, "s%d", &tile) == 1) { - /* Tile part header, unspecified */ - if (tile < 0) { - fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile); - return 1; - } - if (tilespec < JPWL_MAX_NO_TILESPECS) { - parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile; - parameters->jpwl_sens_TPH[tilespec++] = hprot; - } - - } else if (!strcmp(token, "s")) { - /* Main header, unspecified */ - parameters->jpwl_sens_MH = sens; - - } else { - fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token); - return 1; - }; - - parameters->jpwl_sens_size = 2; /* 2 bytes for default size */ - } - - /* search addressing size */ - if (*token == 'a') { - - - addr = 0; /* predefined: auto */ - - if(sscanf(token, "a=%d", &addr) == 1) { - /* Specified */ - if ((addr != 0) && (addr != 2) && (addr != 4)) { - fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr); - return 1; - } - parameters->jpwl_sens_addr = addr; - - } else if (!strcmp(token, "a")) { - /* default */ - parameters->jpwl_sens_addr = addr; /* auto for default size */ - - } else { - fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token); - return 1; - }; - - } - - /* search sensitivity size */ - if (*token == 'z') { - - - size = 1; /* predefined: 1 byte */ - - if(sscanf(token, "z=%d", &size) == 1) { - /* Specified */ - if ((size != 0) && (size != 1) && (size != 2)) { - fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size); - return 1; - } - parameters->jpwl_sens_size = size; - - } else if (!strcmp(token, "a")) { - /* default */ - parameters->jpwl_sens_size = size; /* 1 for default size */ - - } else { - fprintf(stderr, "ERROR -> invalid size selection = %s\n", token); - return 1; - }; - - } - - /* search range method */ - if (*token == 'g') { - - - range = 0; /* predefined: 0 (packet) */ - - if(sscanf(token, "g=%d", &range) == 1) { - /* Specified */ - if ((range < 0) || (range > 3)) { - fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range); - return 1; - } - parameters->jpwl_sens_range = range; - - } else if (!strcmp(token, "g")) { - /* default */ - parameters->jpwl_sens_range = range; + /* ------------------------------------------------------ */ + + case 'W': /* JPWL capabilities switched on */ + { + char *token = NULL; + int hprot, pprot, sens, addr, size, range; + + /* we need to enable indexing */ + if (!indexfilename || !*indexfilename) { + strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN); + } + + /* search for different protection methods */ + + /* break the option in comma points and parse the result */ + token = strtok(opj_optarg, ","); + while(token != NULL) { + + /* search header error protection method */ + if (*token == 'h') { + + static int tile = 0, tilespec = 0, lasttileno = 0; + + hprot = 1; /* predefined method */ + + if(sscanf(token, "h=%d", &hprot) == 1) { + /* Main header, specified */ + if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || + ((hprot >= 37) && (hprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid main header protection method h = %d\n", hprot); + return 1; + } + parameters->jpwl_hprot_MH = hprot; + + } else if(sscanf(token, "h%d=%d", &tile, &hprot) == 2) { + /* Tile part header, specified */ + if (!((hprot == 0) || (hprot == 1) || (hprot == 16) || (hprot == 32) || + ((hprot >= 37) && (hprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid tile part header protection method h = %d\n", hprot); + return 1; + } + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile); + return 1; + } + if (tilespec < JPWL_MAX_NO_TILESPECS) { + parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile; + parameters->jpwl_hprot_TPH[tilespec++] = hprot; + } + + } else if(sscanf(token, "h%d", &tile) == 1) { + /* Tile part header, unspecified */ + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method t = %d\n", tile); + return 1; + } + if (tilespec < JPWL_MAX_NO_TILESPECS) { + parameters->jpwl_hprot_TPH_tileno[tilespec] = lasttileno = tile; + parameters->jpwl_hprot_TPH[tilespec++] = hprot; + } + + + } else if (!strcmp(token, "h")) { + /* Main header, unspecified */ + parameters->jpwl_hprot_MH = hprot; + + } else { + fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token); + return 1; + }; + + } + + /* search packet error protection method */ + if (*token == 'p') { + + static int pack = 0, tile = 0, packspec = 0; + + pprot = 1; /* predefined method */ + + if (sscanf(token, "p=%d", &pprot) == 1) { + /* Method for all tiles and all packets */ + if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || + ((pprot >= 37) && (pprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid default packet protection method p = %d\n", pprot); + return 1; + } + parameters->jpwl_pprot_tileno[0] = 0; + parameters->jpwl_pprot_packno[0] = 0; + parameters->jpwl_pprot[0] = pprot; + + } else if (sscanf(token, "p%d=%d", &tile, &pprot) == 2) { + /* method specified from that tile on */ + if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || + ((pprot >= 37) && (pprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); + return 1; + } + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); + return 1; + } + if (packspec < JPWL_MAX_NO_PACKSPECS) { + parameters->jpwl_pprot_tileno[packspec] = tile; + parameters->jpwl_pprot_packno[packspec] = 0; + parameters->jpwl_pprot[packspec++] = pprot; + } + + } else if (sscanf(token, "p%d:%d=%d", &tile, &pack, &pprot) == 3) { + /* method fully specified from that tile and that packet on */ + if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || + ((pprot >= 37) && (pprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); + return 1; + } + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); + return 1; + } + if (pack < 0) { + fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack); + return 1; + } + if (packspec < JPWL_MAX_NO_PACKSPECS) { + parameters->jpwl_pprot_tileno[packspec] = tile; + parameters->jpwl_pprot_packno[packspec] = pack; + parameters->jpwl_pprot[packspec++] = pprot; + } + + } else if (sscanf(token, "p%d:%d", &tile, &pack) == 2) { + /* default method from that tile and that packet on */ + if (!((pprot == 0) || (pprot == 1) || (pprot == 16) || (pprot == 32) || + ((pprot >= 37) && (pprot <= 128)))) { + fprintf(stderr, "ERROR -> invalid packet protection method p = %d\n", pprot); + return 1; + } + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); + return 1; + } + if (pack < 0) { + fprintf(stderr, "ERROR -> invalid packet number on protection method p = %d\n", pack); + return 1; + } + if (packspec < JPWL_MAX_NO_PACKSPECS) { + parameters->jpwl_pprot_tileno[packspec] = tile; + parameters->jpwl_pprot_packno[packspec] = pack; + parameters->jpwl_pprot[packspec++] = pprot; + } + + } else if (sscanf(token, "p%d", &tile) == 1) { + /* default from a tile on */ + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on protection method p = %d\n", tile); + return 1; + } + if (packspec < JPWL_MAX_NO_PACKSPECS) { + parameters->jpwl_pprot_tileno[packspec] = tile; + parameters->jpwl_pprot_packno[packspec] = 0; + parameters->jpwl_pprot[packspec++] = pprot; + } + + + } else if (!strcmp(token, "p")) { + /* all default */ + parameters->jpwl_pprot_tileno[0] = 0; + parameters->jpwl_pprot_packno[0] = 0; + parameters->jpwl_pprot[0] = pprot; + + } else { + fprintf(stderr, "ERROR -> invalid protection method selection = %s\n", token); + return 1; + }; + + } + + /* search sensitivity method */ + if (*token == 's') { + + static int tile = 0, tilespec = 0, lasttileno = 0; + + sens = 0; /* predefined: relative error */ + + if(sscanf(token, "s=%d", &sens) == 1) { + /* Main header, specified */ + if ((sens < -1) || (sens > 7)) { + fprintf(stderr, "ERROR -> invalid main header sensitivity method s = %d\n", sens); + return 1; + } + parameters->jpwl_sens_MH = sens; + + } else if(sscanf(token, "s%d=%d", &tile, &sens) == 2) { + /* Tile part header, specified */ + if ((sens < -1) || (sens > 7)) { + fprintf(stderr, "ERROR -> invalid tile part header sensitivity method s = %d\n", sens); + return 1; + } + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile); + return 1; + } + if (tilespec < JPWL_MAX_NO_TILESPECS) { + parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile; + parameters->jpwl_sens_TPH[tilespec++] = sens; + } + + } else if(sscanf(token, "s%d", &tile) == 1) { + /* Tile part header, unspecified */ + if (tile < 0) { + fprintf(stderr, "ERROR -> invalid tile part number on sensitivity method t = %d\n", tile); + return 1; + } + if (tilespec < JPWL_MAX_NO_TILESPECS) { + parameters->jpwl_sens_TPH_tileno[tilespec] = lasttileno = tile; + parameters->jpwl_sens_TPH[tilespec++] = hprot; + } + + } else if (!strcmp(token, "s")) { + /* Main header, unspecified */ + parameters->jpwl_sens_MH = sens; + + } else { + fprintf(stderr, "ERROR -> invalid sensitivity method selection = %s\n", token); + return 1; + }; + + parameters->jpwl_sens_size = 2; /* 2 bytes for default size */ + } + + /* search addressing size */ + if (*token == 'a') { + + + addr = 0; /* predefined: auto */ + + if(sscanf(token, "a=%d", &addr) == 1) { + /* Specified */ + if ((addr != 0) && (addr != 2) && (addr != 4)) { + fprintf(stderr, "ERROR -> invalid addressing size a = %d\n", addr); + return 1; + } + parameters->jpwl_sens_addr = addr; + + } else if (!strcmp(token, "a")) { + /* default */ + parameters->jpwl_sens_addr = addr; /* auto for default size */ + + } else { + fprintf(stderr, "ERROR -> invalid addressing selection = %s\n", token); + return 1; + }; + + } + + /* search sensitivity size */ + if (*token == 'z') { + + + size = 1; /* predefined: 1 byte */ + + if(sscanf(token, "z=%d", &size) == 1) { + /* Specified */ + if ((size != 0) && (size != 1) && (size != 2)) { + fprintf(stderr, "ERROR -> invalid sensitivity size z = %d\n", size); + return 1; + } + parameters->jpwl_sens_size = size; - } else { - fprintf(stderr, "ERROR -> invalid range selection = %s\n", token); - return 1; - }; + } else if (!strcmp(token, "a")) { + /* default */ + parameters->jpwl_sens_size = size; /* 1 for default size */ - } + } else { + fprintf(stderr, "ERROR -> invalid size selection = %s\n", token); + return 1; + }; + + } - /* next token or bust */ - token = strtok(NULL, ","); - }; + /* search range method */ + if (*token == 'g') { + + + range = 0; /* predefined: 0 (packet) */ + + if(sscanf(token, "g=%d", &range) == 1) { + /* Specified */ + if ((range < 0) || (range > 3)) { + fprintf(stderr, "ERROR -> invalid sensitivity range method g = %d\n", range); + return 1; + } + parameters->jpwl_sens_range = range; + + } else if (!strcmp(token, "g")) { + /* default */ + parameters->jpwl_sens_range = range; + } else { + fprintf(stderr, "ERROR -> invalid range selection = %s\n", token); + return 1; + }; - /* some info */ - fprintf(stdout, "Info: JPWL capabilities enabled\n"); - parameters->jpwl_epc_on = OPJ_TRUE; + } - } - break; + /* next token or bust */ + token = strtok(NULL, ","); + }; + + + /* some info */ + fprintf(stdout, "Info: JPWL capabilities enabled\n"); + parameters->jpwl_epc_on = OPJ_TRUE; + + } + break; #endif /* USE_JPWL */ -/* <<UniPG */ -/* ------------------------------------------------------ */ - - case 'J': /* jpip on */ - { - parameters->jpip_on = OPJ_TRUE; - } - break; - /* ------------------------------------------------------ */ - - - default: - fprintf(stderr, "ERROR -> Command line not valid\n"); - return 1; - } - }while(c != -1); + /* <<UniPG */ + /* ------------------------------------------------------ */ - /* check for possible errors */ - if (parameters->cp_cinema){ - if(parameters->tcp_numlayers > 1){ - 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"); - } - } - if(img_fol->set_imgdir == 1){ - if(!(parameters->infile[0] == 0)){ - fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n"); - return 1; - } - if(img_fol->set_out_format == 0){ - fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n"); - fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n"); - return 1; - } - if(!((parameters->outfile[0] == 0))){ - fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n"); - fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n"); - return 1; - } - }else{ - if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { - fprintf(stderr, "Example: %s -i image.ppm -o image.j2k\n",argv[0]); - fprintf(stderr, " Try: %s -h\n",argv[0]); - return 1; - } - } - - if ( (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0) - || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) { - fprintf(stderr,"\nError: invalid raw image parameters\n"); - fprintf(stderr,"Please use the Format option -F:\n"); - fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); - fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); - fprintf(stderr,"Aborting\n"); - return 1; - } - - if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality) - && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) { - fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n"); - return 1; - } /* mod fixed_quality */ - - /* if no rate entered, lossless by default */ - if (parameters->tcp_numlayers == 0) { - parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */ - parameters->tcp_numlayers++; - parameters->cp_disto_alloc = 1; - } - - if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) { - fprintf(stderr, - "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n", - parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0); - return 1; - } - - for (i = 0; i < parameters->numpocs; i++) { - if (parameters->POC[i].prg == -1) { - fprintf(stderr, - "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n", - i + 1); - } - } + case 'J': /* jpip on */ + { + parameters->jpip_on = OPJ_TRUE; + } + break; + /* ------------------------------------------------------ */ - return 0; -} -/* -------------------------------------------------------------------------- */ + default: + fprintf(stderr, "ERROR -> Command line not valid\n"); + return 1; + } + }while(c != -1); -/** -sample error callback expecting a FILE* client object -*/ -static void error_file_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[ERROR] %s", msg); -} -/** -sample warning callback expecting a FILE* client object -*/ -static void warning_file_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[WARNING] %s", msg); -} -/** -sample debug callback expecting a FILE* client object -*/ -static void info_file_callback(const char *msg, void *client_data) { - FILE *stream = (FILE*)client_data; - fprintf(stream, "[INFO] %s", msg); + if(img_fol->set_imgdir == 1){ + if(!(parameters->infile[0] == 0)){ + fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n"); + return 1; + } + if(img_fol->set_out_format == 0){ + fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n"); + fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n"); + return 1; + } + if(!((parameters->outfile[0] == 0))){ + fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n"); + fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n"); + return 1; + } + }else{ + if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { + fprintf(stderr, "Example: %s -i image.ppm -o image.j2k\n",argv[0]); + fprintf(stderr, " Try: %s -h\n",argv[0]); + return 1; + } + } + + if ( (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0) + || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) { + fprintf(stderr,"\nError: invalid raw image parameters\n"); + fprintf(stderr,"Please use the Format option -F:\n"); + fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n"); + fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n"); + fprintf(stderr,"Aborting\n"); + return 1; + } + + if ((parameters->cp_disto_alloc || parameters->cp_fixed_alloc || parameters->cp_fixed_quality) + && (!(parameters->cp_disto_alloc ^ parameters->cp_fixed_alloc ^ parameters->cp_fixed_quality))) { + fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n"); + return 1; + } /* mod fixed_quality */ + + /* if no rate entered, lossless by default */ + if (parameters->tcp_numlayers == 0) { + parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */ + parameters->tcp_numlayers++; + parameters->cp_disto_alloc = 1; + } + + if((parameters->cp_tx0 > parameters->image_offset_x0) || (parameters->cp_ty0 > parameters->image_offset_y0)) { + fprintf(stderr, + "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n", + parameters->cp_tx0, parameters->image_offset_x0, parameters->cp_ty0, parameters->image_offset_y0); + return 1; + } + + for (i = 0; i < parameters->numpocs; i++) { + if (parameters->POC[i].prg == -1) { + fprintf(stderr, + "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n", + i + 1); + } + } + + return 0; } +/* -------------------------------------------------------------------------- */ + /** sample error debug callback expecting no client object */ static void error_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[ERROR] %s", msg); + (void)client_data; + fprintf(stdout, "[ERROR] %s", msg); } /** sample warning debug callback expecting no client object */ static void warning_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[WARNING] %s", msg); + (void)client_data; + fprintf(stdout, "[WARNING] %s", msg); } /** sample debug callback expecting no client object */ static void info_callback(const char *msg, void *client_data) { - (void)client_data; - fprintf(stdout, "[INFO] %s", msg); + (void)client_data; + fprintf(stdout, "[INFO] %s", msg); } /* -------------------------------------------------------------------------- */ @@ -1590,239 +1452,241 @@ 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 */ + opj_cparameters_t parameters; /* compression parameters */ - opj_stream_t *l_stream = 00; - opj_codec_t* l_codec = 00; - opj_image_t *image = NULL; - raw_cparameters_t raw_cp; + opj_stream_t *l_stream = 00; + opj_codec_t* l_codec = 00; + opj_image_t *image = NULL; + raw_cparameters_t raw_cp; - char indexfilename[OPJ_PATH_LEN]; /* index file name */ + char indexfilename[OPJ_PATH_LEN]; /* index file name */ - unsigned int i, num_images, imageno; - img_fol_t img_fol; - dircnt_t *dirptr = NULL; + unsigned int i, num_images, imageno; + img_fol_t img_fol; + dircnt_t *dirptr = NULL; - OPJ_BOOL bSuccess; - OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */ - OPJ_UINT32 l_nb_tiles = 4; + OPJ_BOOL bSuccess; + OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */ + OPJ_UINT32 l_nb_tiles = 4; - /* set encoding parameters to default values */ - opj_set_default_encoder_parameters(¶meters); + /* set encoding parameters to default values */ + opj_set_default_encoder_parameters(¶meters); - /* Initialize indexfilename and img_fol */ - *indexfilename = 0; - memset(&img_fol,0,sizeof(img_fol_t)); + /* Initialize indexfilename and img_fol */ + *indexfilename = 0; + memset(&img_fol,0,sizeof(img_fol_t)); - /* parse input and get user encoding parameters */ - if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename) == 1) { - return 1; - } + /* parse input and get user encoding parameters */ + parameters.tcp_mct = -1; /* This will be set later according to the input image or the provided option */ + if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename) == 1) { + return 1; + } - if (parameters.cp_cinema){ - img_fol.rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float)); - for(i=0; i< parameters.tcp_numlayers; i++){ - img_fol.rates[i] = parameters.tcp_rates[i]; - } - cinema_parameters(¶meters); - } - - /* Create comment for codestream */ - if(parameters.cp_comment == NULL) { - const char comment[] = "Created by OpenJPEG version "; - const size_t clen = strlen(comment); - const char *version = opj_version(); -/* UniPG>> */ + /* Create comment for codestream */ + if(parameters.cp_comment == NULL) { + const char comment[] = "Created by OpenJPEG version "; + const size_t clen = strlen(comment); + const char *version = opj_version(); + /* UniPG>> */ #ifdef USE_JPWL - parameters.cp_comment = (char*)malloc(clen+strlen(version)+11); - sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version); + parameters.cp_comment = (char*)malloc(clen+strlen(version)+11); + sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version); #else - parameters.cp_comment = (char*)malloc(clen+strlen(version)+1); - sprintf(parameters.cp_comment,"%s%s", comment, version); + parameters.cp_comment = (char*)malloc(clen+strlen(version)+1); + sprintf(parameters.cp_comment,"%s%s", comment, version); #endif -/* <<UniPG */ - } - - /* Read directory if necessary */ - if(img_fol.set_imgdir==1){ - num_images=get_num_images(img_fol.imgdirpath); - 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 = (char**) malloc(num_images*sizeof(char*)); - if(!dirptr->filename_buf){ - return 0; - } - 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; - } - if (num_images==0){ - fprintf(stdout,"Folder is empty\n"); - return 0; - } - }else{ - num_images=1; - } - /*Encoding image one by one*/ - for(imageno=0;imageno<num_images;imageno++) { - image = NULL; - fprintf(stderr,"\n"); - - if(img_fol.set_imgdir==1){ - if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) { - fprintf(stderr,"skipping file...\n"); - continue; - } - } + /* <<UniPG */ + } - switch(parameters.decod_format) { - case PGX_DFMT: - break; - case PXM_DFMT: - break; - case BMP_DFMT: - break; - case TIF_DFMT: - break; - case RAW_DFMT: - case RAWL_DFMT: - break; - case TGA_DFMT: - break; - case PNG_DFMT: - break; - default: - fprintf(stderr,"skipping file...\n"); - continue; - } + /* Read directory if necessary */ + if(img_fol.set_imgdir==1){ + num_images=get_num_images(img_fol.imgdirpath); + 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 = (char**) malloc(num_images*sizeof(char*)); + if(!dirptr->filename_buf){ + return 0; + } + 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; + } + if (num_images==0){ + fprintf(stdout,"Folder is empty\n"); + return 0; + } + }else{ + num_images=1; + } + /*Encoding image one by one*/ + for(imageno=0;imageno<num_images;imageno++) { + image = NULL; + fprintf(stderr,"\n"); + + if(img_fol.set_imgdir==1){ + if (get_next_file((int)imageno, dirptr,&img_fol, ¶meters)) { + fprintf(stderr,"skipping file...\n"); + continue; + } + } - /* decode the source image */ - /* ----------------------- */ - - switch (parameters.decod_format) { - case PGX_DFMT: - image = pgxtoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load pgx file\n"); - return 1; - } - break; - - case PXM_DFMT: - image = pnmtoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load pnm file\n"); - return 1; - } - break; - - case BMP_DFMT: - image = bmptoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load bmp file\n"); - return 1; - } - break; - -#ifdef HAVE_LIBTIFF - case TIF_DFMT: - image = tiftoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load tiff file\n"); - return 1; - } - break; -#endif /* HAVE_LIBTIFF */ - - case RAW_DFMT: - image = rawtoimage(parameters.infile, ¶meters, &raw_cp); - if (!image) { - fprintf(stderr, "Unable to load raw file\n"); - return 1; - } - break; - - case RAWL_DFMT: - image = rawltoimage(parameters.infile, ¶meters, &raw_cp); - if (!image) { - fprintf(stderr, "Unable to load raw file\n"); - return 1; - } - break; - - case TGA_DFMT: - image = tgatoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load tga file\n"); - return 1; - } - break; - -#ifdef HAVE_LIBPNG - case PNG_DFMT: - image = pngtoimage(parameters.infile, ¶meters); - if (!image) { - fprintf(stderr, "Unable to load png file\n"); - return 1; - } - break; -#endif /* HAVE_LIBPNG */ - } + switch(parameters.decod_format) { + case PGX_DFMT: + break; + case PXM_DFMT: + break; + case BMP_DFMT: + break; + case TIF_DFMT: + break; + case RAW_DFMT: + case RAWL_DFMT: + break; + case TGA_DFMT: + break; + case PNG_DFMT: + break; + default: + fprintf(stderr,"skipping file...\n"); + continue; + } -/* Can happen if input file is TIFF or PNG - * and HAVE_LIBTIF or HAVE_LIBPNG is undefined + /* decode the source image */ + /* ----------------------- */ + + switch (parameters.decod_format) { + case PGX_DFMT: + image = pgxtoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load pgx file\n"); + return 1; + } + break; + + case PXM_DFMT: + image = pnmtoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load pnm file\n"); + return 1; + } + break; + + case BMP_DFMT: + image = bmptoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load bmp file\n"); + return 1; + } + break; + +#ifdef OPJ_HAVE_LIBTIFF + case TIF_DFMT: + image = tiftoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load tiff file\n"); + return 1; + } + break; +#endif /* OPJ_HAVE_LIBTIFF */ + + case RAW_DFMT: + image = rawtoimage(parameters.infile, ¶meters, &raw_cp); + if (!image) { + fprintf(stderr, "Unable to load raw file\n"); + return 1; + } + break; + + case RAWL_DFMT: + image = rawltoimage(parameters.infile, ¶meters, &raw_cp); + if (!image) { + fprintf(stderr, "Unable to load raw file\n"); + return 1; + } + break; + + case TGA_DFMT: + image = tgatoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load tga file\n"); + return 1; + } + break; + +#ifdef OPJ_HAVE_LIBPNG + case PNG_DFMT: + image = pngtoimage(parameters.infile, ¶meters); + if (!image) { + fprintf(stderr, "Unable to load png file\n"); + return 1; + } + break; +#endif /* OPJ_HAVE_LIBPNG */ + } + + /* Can happen if input file is TIFF or PNG + * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined */ - if( !image) { - fprintf(stderr, "Unable to load file: got no image\n"); - return 1; - } + if( !image) { + fprintf(stderr, "Unable to load file: got no image\n"); + return 1; + } - /* Decide if MCT should be used */ - parameters.tcp_mct = image->numcomps == 3 ? 1 : 0; + /* Decide if MCT should be used */ + if (parameters.tcp_mct == -1) { /* mct mode has not been set in commandline */ + parameters.tcp_mct = image->numcomps >= 3 ? 1 : 0; + } else { /* mct mode has been set in commandline */ + if ((parameters.tcp_mct == 1) && (image->numcomps < 3)){ + fprintf(stderr, "RGB->YCC conversion cannot be used:\n"); + fprintf(stderr, "Input image has less than 3 components\n"); + return 1; + } + if ((parameters.tcp_mct == 2) && (!parameters.mct_data)){ + fprintf(stderr, "Custom MCT has been set but no array-based MCT\n"); + fprintf(stderr, "has been provided. Aborting.\n"); + return 1; + } + } - if(parameters.cp_cinema){ - cinema_setup_encoder(¶meters,image,&img_fol); - } + /* encode the destination image */ + /* ---------------------------- */ - /* encode the destination image */ - /* ---------------------------- */ - - switch(parameters.cod_format) { - case J2K_CFMT: /* JPEG-2000 codestream */ - { - /* Get a decoder handle */ - 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(OPJ_CODEC_JP2); - break; - } - default: - fprintf(stderr, "skipping file..\n"); - opj_stream_destroy(l_stream); - continue; - } - - /* 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( bUseTiles ) { - parameters.cp_tx0 = 0; - parameters.cp_ty0 = 0; - parameters.tile_size_on = OPJ_TRUE; - parameters.cp_tdx = 512; - parameters.cp_tdy = 512; - } - opj_setup_encoder(l_codec, ¶meters, image); + switch(parameters.cod_format) { + case J2K_CFMT: /* JPEG-2000 codestream */ + { + /* Get a decoder handle */ + 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(OPJ_CODEC_JP2); + break; + } + default: + fprintf(stderr, "skipping file..\n"); + opj_stream_destroy(l_stream); + continue; + } + + /* 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( bUseTiles ) { + parameters.cp_tx0 = 0; + parameters.cp_ty0 = 0; + parameters.tile_size_on = OPJ_TRUE; + parameters.cp_tdx = 512; + parameters.cp_tdy = 512; + } + opj_setup_encoder(l_codec, ¶meters, image); /* Open the output file*/ fout = fopen(parameters.outfile, "wb"); @@ -1832,72 +1696,71 @@ int main(int argc, char **argv) { 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); - if (! 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); + if (! l_stream){ + return 1; + } - /* encode the image */ - bSuccess = opj_start_compress(l_codec,image,l_stream); - if (!bSuccess) { - fprintf(stderr, "failed to encode image: opj_start_compress\n"); - } - if( bUseTiles ) { - OPJ_BYTE *l_data; - OPJ_UINT32 l_data_size = 512*512*3; - l_data = (OPJ_BYTE*) malloc( l_data_size * sizeof(OPJ_BYTE)); - memset(l_data, 0, l_data_size ); - assert( l_data ); - 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); + /* encode the image */ + bSuccess = opj_start_compress(l_codec,image,l_stream); + if (!bSuccess) { + fprintf(stderr, "failed to encode image: opj_start_compress\n"); + } + if( bUseTiles ) { + OPJ_BYTE *l_data; + OPJ_UINT32 l_data_size = 512*512*3; + l_data = (OPJ_BYTE*) malloc( l_data_size * sizeof(OPJ_BYTE)); + memset(l_data, 0, l_data_size ); + assert( l_data ); + 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_destroy_codec(l_codec); - opj_image_destroy(image); - return 1; + opj_destroy_codec(l_codec); + opj_image_destroy(image); + return 1; + } + } + free(l_data); + } + else { + bSuccess = bSuccess && opj_encode(l_codec, l_stream); + if (!bSuccess) { + fprintf(stderr, "failed to encode image: opj_encode\n"); + } + } + bSuccess = bSuccess && opj_end_compress(l_codec, l_stream); + if (!bSuccess) { + fprintf(stderr, "failed to encode image: opj_end_compress\n"); } - } - free(l_data); - } - else { - bSuccess = bSuccess && opj_encode(l_codec, l_stream); - if (!bSuccess) { - fprintf(stderr, "failed to encode image: opj_encode\n"); - } - } - bSuccess = bSuccess && opj_end_compress(l_codec, l_stream); - if (!bSuccess) { - fprintf(stderr, "failed to encode image: opj_end_compress\n"); - } - if (!bSuccess) { - opj_stream_destroy(l_stream); - fclose(fout); - opj_destroy_codec(l_codec); - opj_image_destroy(image); - fprintf(stderr, "failed to encode image\n"); - return 1; - } + if (!bSuccess) { + opj_stream_destroy(l_stream); + fclose(fout); + opj_destroy_codec(l_codec); + opj_image_destroy(image); + fprintf(stderr, "failed to encode image\n"); + return 1; + } - fprintf(stderr,"Generated outfile %s\n",parameters.outfile); - /* close and free the byte stream */ - opj_stream_destroy(l_stream); + fprintf(stderr,"Generated outfile %s\n",parameters.outfile); + /* close and free the byte stream */ + opj_stream_destroy(l_stream); fclose(fout); - /* free remaining compression structures */ - opj_destroy_codec(l_codec); + /* free remaining compression structures */ + opj_destroy_codec(l_codec); - /* free image data */ - opj_image_destroy(image); + /* free image data */ + opj_image_destroy(image); - } + } - /* free user parameters structure */ - if(parameters.cp_comment) free(parameters.cp_comment); - if(parameters.cp_matrice) free(parameters.cp_matrice); - if(parameters.cp_cinema) free(img_fol.rates); + /* free user parameters structure */ + if(parameters.cp_comment) free(parameters.cp_comment); + if(parameters.cp_matrice) free(parameters.cp_matrice); - return 0; + return 0; } diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index df2978db..9adebd91 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -31,7 +31,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#include "opj_config.h" +#include "opj_apps_config.h" #include <stdio.h> #include <string.h> @@ -57,10 +57,10 @@ #include "convert.h" #include "index.h" -#ifdef HAVE_LIBLCMS2 +#ifdef OPJ_HAVE_LIBLCMS2 #include <lcms2.h> #endif -#ifdef HAVE_LIBLCMS1 +#ifdef OPJ_HAVE_LIBLCMS1 #include <lcms.h> #endif #include "color.h" @@ -271,7 +271,7 @@ static int infile_format(const char *fname) const char *s, *magic_s; int ext_format, magic_format; unsigned char buf[12]; - unsigned int l_nb_read; + OPJ_SIZE_T l_nb_read; reader = fopen(fname, "rb"); @@ -447,7 +447,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i case 'r': /* reduce option */ { - sscanf(opj_optarg, "%d", ¶meters->cp_reduce); + sscanf(opj_optarg, "%ud", ¶meters->cp_reduce); } break; @@ -456,7 +456,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i case 'l': /* layering option */ { - sscanf(opj_optarg, "%d", ¶meters->cp_layer); + sscanf(opj_optarg, "%ud", ¶meters->cp_layer); } break; @@ -481,7 +481,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i case 'd': /* Input decode ROI */ { int size_optarg = (int)strlen(opj_optarg) + 1; - char *ROI_values = (char*) malloc(size_optarg); + char *ROI_values = (char*) malloc((size_t)size_optarg); ROI_values[0] = '\0'; strncpy(ROI_values, opj_optarg, strlen(opj_optarg)); ROI_values[strlen(opj_optarg)] = '\0'; @@ -496,7 +496,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i case 't': /* Input tile index */ { - sscanf(opj_optarg, "%d", ¶meters->tile_index); + sscanf(opj_optarg, "%ud", ¶meters->tile_index); parameters->nb_tile_to_decode = 1; } break; @@ -639,8 +639,8 @@ int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsi return EXIT_FAILURE; } else{ - *DA_x0 = values[0]; *DA_y0 = values[1]; - *DA_x1 = values[2]; *DA_y1 = values[3]; + *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1]; + *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3]; return EXIT_SUCCESS; } } @@ -689,6 +689,7 @@ int main(int argc, char **argv) OPJ_INT32 num_images, imageno; img_fol_t img_fol; dircnt_t *dirptr = NULL; + int failed = 0; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); @@ -711,8 +712,8 @@ int main(int argc, char **argv) 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 = (char**) malloc(num_images*sizeof(char*)); + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); if(!dirptr->filename_buf){ return EXIT_FAILURE; @@ -794,7 +795,7 @@ int main(int argc, char **argv) /* Setup the decoder decoding parameters using user parameters */ if ( !opj_setup_decoder(l_codec, ¶meters) ){ - fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); + fprintf(stderr, "ERROR -> opj_compress: failed to setup the decoder\n"); opj_stream_destroy(l_stream); fclose(fsrc); opj_destroy_codec(l_codec); @@ -814,8 +815,8 @@ int main(int argc, char **argv) if (!parameters.nb_tile_to_decode) { /* Optional if you want decode the entire image */ - if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, - parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){ + if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, + (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); @@ -864,9 +865,16 @@ int main(int argc, char **argv) if(image->color_space == OPJ_CLRSPC_SYCC){ color_sycc_to_rgb(image); /* FIXME */ } + + if( image->color_space != OPJ_CLRSPC_SYCC + && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy + && image->comps[1].dx != 1 ) + image->color_space = OPJ_CLRSPC_SYCC; + else if (image->numcomps <= 2) + image->color_space = OPJ_CLRSPC_GRAY; if(image->icc_profile_buf) { -#if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) +#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2) color_apply_icc_profile(image); /* FIXME */ #endif free(image->icc_profile_buf); @@ -878,7 +886,8 @@ int main(int argc, char **argv) switch (parameters.cod_format) { case PXM_DFMT: /* PNM PGM PPM */ if (imagetopnm(image, parameters.outfile)) { - fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); @@ -887,7 +896,8 @@ int main(int argc, char **argv) case PGX_DFMT: /* PGX */ if(imagetopgx(image, parameters.outfile)){ - fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); @@ -896,25 +906,28 @@ int main(int argc, char **argv) case BMP_DFMT: /* BMP */ if(imagetobmp(image, parameters.outfile)){ - fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; -#ifdef HAVE_LIBTIFF +#ifdef OPJ_HAVE_LIBTIFF case TIF_DFMT: /* TIFF */ if(imagetotif(image, parameters.outfile)){ - fprintf(stdout,"Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Generated Outfile %s\n",parameters.outfile); } break; -#endif /* HAVE_LIBTIFF */ +#endif /* OPJ_HAVE_LIBTIFF */ case RAW_DFMT: /* RAW */ if(imagetoraw(image, parameters.outfile)){ - fprintf(stdout,"Error generating raw file. Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Error generating raw file. Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); @@ -923,7 +936,8 @@ int main(int argc, char **argv) case RAWL_DFMT: /* RAWL */ if(imagetorawl(image, parameters.outfile)){ - fprintf(stdout,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); @@ -932,27 +946,30 @@ int main(int argc, char **argv) case TGA_DFMT: /* TGA */ if(imagetotga(image, parameters.outfile)){ - fprintf(stdout,"Error generating tga file. Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Error generating tga file. Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; -#ifdef HAVE_LIBPNG +#ifdef OPJ_HAVE_LIBPNG case PNG_DFMT: /* PNG */ if(imagetopng(image, parameters.outfile)){ - fprintf(stdout,"Error generating png file. Outfile %s not generated\n",parameters.outfile); + fprintf(stderr,"Error generating png file. Outfile %s not generated\n",parameters.outfile); + failed = 1; } else { fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile); } break; -#endif /* HAVE_LIBPNG */ +#endif /* OPJ_HAVE_LIBPNG */ /* Can happen if output file is TIFF or PNG - * and HAVE_LIBTIF or HAVE_LIBPNG is undefined + * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined */ default: fprintf(stderr,"Outfile %s not generated\n",parameters.outfile); + failed = 1; } /* free remaining structures */ @@ -968,7 +985,7 @@ int main(int argc, char **argv) opj_destroy_cstr_index(&cstr_index); } - return EXIT_SUCCESS; + return failed ? EXIT_FAILURE : EXIT_SUCCESS; } /*end main*/ diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c index 2bc1893a..7d2200a1 100644 --- a/src/bin/jp2/opj_dump.c +++ b/src/bin/jp2/opj_dump.c @@ -71,18 +71,18 @@ typedef struct img_folder{ /** Enable Cod Format for output*/ char set_out_format; + int flag; }img_fol_t; /* -------------------------------------------------------------------------- */ /* Declarations */ -int get_num_images(char *imgdirpath); -int load_images(dircnt_t *dirptr, char *imgdirpath); -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 get_num_images(char *imgdirpath); +static int load_images(dircnt_t *dirptr, char *imgdirpath); +static int get_file_format(const char *filename); +static 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); -int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1); +static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol); /* -------------------------------------------------------------------------- */ static void decode_help_display(void) { @@ -116,7 +116,7 @@ static void decode_help_display(void) { } /* -------------------------------------------------------------------------- */ -int get_num_images(char *imgdirpath){ +static int get_num_images(char *imgdirpath){ DIR *dir; struct dirent* content; int num_images = 0; @@ -138,7 +138,7 @@ int get_num_images(char *imgdirpath){ } /* -------------------------------------------------------------------------- */ -int load_images(dircnt_t *dirptr, char *imgdirpath){ +static int load_images(dircnt_t *dirptr, char *imgdirpath){ DIR *dir; struct dirent* content; int i = 0; @@ -164,7 +164,7 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){ } /* -------------------------------------------------------------------------- */ -int get_file_format(const char *filename) { +static 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 }; @@ -184,7 +184,7 @@ 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 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){ char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN]; char *temp_p, temp1[OPJ_PATH_LEN]=""; @@ -221,7 +221,7 @@ static int infile_format(const char *fname) const char *s, *magic_s; int ext_format, magic_format; unsigned char buf[12]; - unsigned int l_nb_read; + size_t l_nb_read; reader = fopen(fname, "rb"); @@ -269,12 +269,12 @@ static int infile_format(const char *fname) * Parse the command line */ /* -------------------------------------------------------------------------- */ -int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) { +static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) { int totlen, c; opj_option_t long_option[]={ {"ImgDir",REQ_ARG, NULL ,'y'}, }; - const char optlist[] = "i:o:hv"; + const char optlist[] = "i:o:f:hv"; totlen=sizeof(long_option); img_fol->set_out_format = 0; @@ -314,6 +314,10 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i break; /* ----------------------------------------------------- */ + case 'f': /* flag */ + img_fol->flag = atoi(opj_optarg); + break; + /* ----------------------------------------------------- */ case 'h': /* display an help description */ decode_help_display(); @@ -425,6 +429,7 @@ int main(int argc, char *argv[]) /* Initialize img_fol */ memset(&img_fol,0,sizeof(img_fol_t)); + img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND; /* Parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { @@ -438,8 +443,8 @@ int main(int argc, char *argv[]) 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 = (char**) malloc(num_images*sizeof(char*)); + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); if(!dirptr->filename_buf){ return EXIT_FAILURE; @@ -553,7 +558,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - opj_dump_codec(l_codec, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, fout ); + opj_dump_codec(l_codec, img_fol.flag, fout ); cstr_info = opj_get_cstr_info(l_codec); |
