[trunk] Import applications modifications from 1.5 over to trunk

This commit is contained in:
Mathieu Malaterre 2012-03-02 10:01:00 +00:00
parent 9f77b065d3
commit 3d9a41e4b4
4 changed files with 265 additions and 159 deletions

View File

@ -30,8 +30,14 @@ ENDIF(WIN32)
FOREACH(exe j2k_to_image image_to_j2k j2k_dump) FOREACH(exe j2k_to_image image_to_j2k j2k_dump)
ADD_EXECUTABLE(${exe} ${exe}.c ${common_SRCS}) ADD_EXECUTABLE(${exe} ${exe}.c ${common_SRCS})
TARGET_LINK_LIBRARIES(${exe} ${OPENJPEG_LIBRARY_NAME} TARGET_LINK_LIBRARIES(${exe} ${OPENJPEG_LIBRARY_NAME}
${Z_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME} ${LCMS_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME} ${LCMS_LIBNAME}
) )
# To support universal exe:
IF(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(${exe} z)
ELSe(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(${exe} ${Z_LIBNAME})
ENDIF(ZLIB_FOUND AND APPLE)
# On unix you need to link to the math library: # On unix you need to link to the math library:
IF(UNIX) IF(UNIX)
@ -50,6 +56,7 @@ INSTALL(
${OPENJPEG_SOURCE_DIR}/doc/man/man1/j2k_dump.1 ${OPENJPEG_SOURCE_DIR}/doc/man/man1/j2k_dump.1
${OPENJPEG_SOURCE_DIR}/doc/man/man1/j2k_to_image.1 ${OPENJPEG_SOURCE_DIR}/doc/man/man1/j2k_to_image.1
DESTINATION ${OPENJPEG_INSTALL_MAN_DIR}/man1) DESTINATION ${OPENJPEG_INSTALL_MAN_DIR}/man1)
#
IF(BUILD_JPWL) IF(BUILD_JPWL)
ADD_EXECUTABLE(JPWL_j2k_to_image ADD_EXECUTABLE(JPWL_j2k_to_image
@ -63,25 +70,38 @@ IF(BUILD_JPWL)
) )
TARGET_LINK_LIBRARIES(JPWL_j2k_to_image ${OPENJPEG_LIBRARY_NAME}_JPWL TARGET_LINK_LIBRARIES(JPWL_j2k_to_image ${OPENJPEG_LIBRARY_NAME}_JPWL
${LCMS_LIBNAME} ${Z_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME}) ${LCMS_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME})
# To support universal exe:
IF(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(JPWL_j2k_to_image z)
ELSe(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(JPWL_j2k_to_image ${Z_LIBNAME})
ENDIF(ZLIB_FOUND AND APPLE)
IF(UNIX) IF(UNIX)
TARGET_LINK_LIBRARIES(JPWL_j2k_to_image m) TARGET_LINK_LIBRARIES(JPWL_j2k_to_image m)
ENDIF(UNIX) ENDIF(UNIX)
ADD_EXECUTABLE(JPWL_image_to_j2k ADD_EXECUTABLE(JPWL_image_to_j2k
image_to_j2k.c image_to_j2k.c
${common_SRCS} ${common_SRCS}
) )
SET_PROPERTY( SET_PROPERTY(
TARGET JPWL_image_to_j2k TARGET JPWL_image_to_j2k
APPEND PROPERTY COMPILE_DEFINITIONS USE_JPWL APPEND PROPERTY COMPILE_DEFINITIONS USE_JPWL
) )
TARGET_LINK_LIBRARIES(JPWL_image_to_j2k ${OPENJPEG_LIBRARY_NAME}_JPWL TARGET_LINK_LIBRARIES(JPWL_image_to_j2k ${OPENJPEG_LIBRARY_NAME}_JPWL
${LCMS_LIBNAME} ${Z_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME} ${LCMS_LIBNAME} ${PNG_LIBNAME} ${TIFF_LIBNAME})
)
# To support universal exe:
IF(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(JPWL_image_to_j2k z)
ELSe(ZLIB_FOUND AND APPLE)
TARGET_LINK_LIBRARIES(JPWL_image_to_j2k ${Z_LIBNAME})
ENDIF(ZLIB_FOUND AND APPLE)
IF(UNIX) IF(UNIX)
TARGET_LINK_LIBRARIES(JPWL_image_to_j2k m) TARGET_LINK_LIBRARIES(JPWL_image_to_j2k m)

View File

@ -68,7 +68,7 @@ static int int_floorlog2(int a) {
<<-- <<-- <<-- <<-- */ <<-- <<-- <<-- <<-- */
#ifdef INFORMATION_ONLY #ifdef INFORMATION_ONLY
// TGA header definition. /* TGA header definition. */
struct tga_header struct tga_header
{ {
unsigned char id_length; /* Image id field length */ unsigned char id_length; /* Image id field length */
@ -94,8 +94,8 @@ struct tga_header
static unsigned short get_ushort(unsigned short val) { static unsigned short get_ushort(unsigned short val) {
#ifdef OPJ_BIG_ENDIAN #ifdef WORDS_BIGENDIAN
return( ((val & 0xff) << 8) + (val >> 8) ); return( ((val & 0xff) << 8) + (val >> 8) );
#else #else
return( val ); return( val );
#endif #endif
@ -104,7 +104,7 @@ static unsigned short get_ushort(unsigned short val) {
#define TGA_HEADER_SIZE 18 #define TGA_HEADER_SIZE 18
int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 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; int palette_size;
@ -144,7 +144,7 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
*width = (unsigned int)image_w; *width = (unsigned int)image_w;
*height = (unsigned int)image_h; *height = (unsigned int)image_h;
// Ignore tga identifier, if present ... /* Ignore tga identifier, if present ... */
if (id_len) if (id_len)
{ {
unsigned char *id = (unsigned char *) malloc(id_len); unsigned char *id = (unsigned char *) malloc(id_len);
@ -157,9 +157,9 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
free(id); free(id);
} }
// Test for compressed formats ... not yet supported ... /* Test for compressed formats ... not yet supported ...
// Note :- 9 - RLE encoded palettized. // Note :- 9 - RLE encoded palettized.
// 10 - RLE encoded RGB. // 10 - RLE encoded RGB. */
if (image_type > 8) if (image_type > 8)
{ {
fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n");
@ -168,7 +168,7 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
*flip_image = !(image_desc & 32); *flip_image = !(image_desc & 32);
// Palettized formats are not yet supported, skip over the palette, if present ... /* Palettized formats are not yet supported, skip over the palette, if present ... */
palette_size = cmap_len * (cmap_entry_size/8); palette_size = cmap_len * (cmap_entry_size/8);
if (palette_size>0) if (palette_size>0)
@ -179,7 +179,17 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
return 1; return 1;
} }
int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, #if WORDS_BIGENDIAN == 1
static inline int16_t swap16(int16_t x)
{
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 short image_w, image_h, us0;
@ -199,29 +209,36 @@ int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
} }
uc0 = 0; uc0 = 0;
if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; // id_length 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; /* colour_map_type */
image_type = 2; // Uncompressed. image_type = 2; /* Uncompressed. */
if(fwrite(&image_type, 1, 1, fp) != 1) goto fails; if(fwrite(&image_type, 1, 1, fp) != 1) goto fails;
us0 = 0; 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_index */
if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // colour_map_length 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(&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; /* x_origin */
if(fwrite(&us0, 2, 1, fp) != 1) goto fails; // y_origin if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */
image_w = (unsigned short)width; image_w = (unsigned short)width;
image_h = (unsigned short) height; image_h = (unsigned short) height;
#if WORDS_BIGENDIAN == 0
if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
if(fwrite(&image_h, 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;
#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) if (flip_image)
image_desc |= 32; image_desc |= 32;
@ -257,15 +274,15 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
return NULL; return NULL;
// We currently only support 24 & 32 bit tga's ... /* We currently only support 24 & 32 bit tga's ... */
if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
return NULL; return NULL;
/* initialize image components */ /* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha. 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 save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */
if (mono) { if (mono) {
color_space = CLRSPC_GRAY; color_space = CLRSPC_GRAY;
@ -395,6 +412,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
unsigned char value; unsigned char value;
float scale; float scale;
FILE *fdest; FILE *fdest;
size_t res;
fdest = fopen(outfile, "wb"); fdest = fopen(outfile, "wb");
if (!fdest) { if (!fdest) {
@ -414,10 +432,10 @@ int imagetotga(opj_image_t * image, const char *outfile) {
width = image->comps[0].w; width = image->comps[0].w;
height = image->comps[0].h; height = image->comps[0].h;
// Mono with alpha, or RGB with alpha. /* Mono with alpha, or RGB with alpha. */
write_alpha = (image->numcomps==2) || (image->numcomps==4); write_alpha = (image->numcomps==2) || (image->numcomps==4);
// Write TGA header /* Write TGA header */
bpp = write_alpha ? 32 : 24; bpp = write_alpha ? 32 : 24;
if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE)) if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE))
return 1; return 1;
@ -440,25 +458,41 @@ int imagetotga(opj_image_t * image, const char *outfile) {
g = (float)(image->comps[1].data[index] + adjustG); g = (float)(image->comps[1].data[index] + adjustG);
b = (float)(image->comps[2].data[index] + adjustB); b = (float)(image->comps[2].data[index] + adjustB);
} }
else {// Greyscale ... else {/* Greyscale ... */
g = r; g = r;
b = r; b = r;
} }
// TGA format writes BGR ... /* TGA format writes BGR ... */
value = (unsigned char)(b*scale); value = (unsigned char)(b*scale);
fwrite(&value,1,1,fdest); res = fwrite(&value,1,1,fdest);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
value = (unsigned char)(g*scale); value = (unsigned char)(g*scale);
fwrite(&value,1,1,fdest); res = fwrite(&value,1,1,fdest);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
value = (unsigned char)(r*scale); value = (unsigned char)(r*scale);
fwrite(&value,1,1,fdest); res = fwrite(&value,1,1,fdest);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
if (write_alpha) { if (write_alpha) {
a = (float)(image->comps[alpha_channel].data[index]); a = (float)(image->comps[alpha_channel].data[index]);
value = (unsigned char)(a*scale); value = (unsigned char)(a*scale);
fwrite(&value,1,1,fdest); res = fwrite(&value,1,1,fdest);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
} }
} }
} }
@ -476,7 +510,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
typedef unsigned short int WORD; typedef unsigned short int WORD;
/* DWORD defines a four byte word */ /* DWORD defines a four byte word */
typedef unsigned long int DWORD; typedef unsigned int DWORD;
typedef struct { typedef struct {
WORD bfType; /* 'BM' for Bitmap (19776) */ WORD bfType; /* 'BM' for Bitmap (19776) */
@ -566,7 +600,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
if(Info_h.biSize != 40) if(Info_h.biSize != 40)
{ {
fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize); fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
fclose(IN); fclose(IN);
return NULL; return NULL;
} }
@ -691,7 +725,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
free(RGB); free(RGB);
}/* if (Info_h.biBitCount == 24) */ }/* if (Info_h.biBitCount == 24) */
else else
if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)//RGB if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
{ {
if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256; if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
else else
@ -802,7 +836,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
free(table_B); free(table_B);
}/* RGB8 */ }/* RGB8 */
else else
if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)//RLE8 if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
{ {
unsigned char *pix, *beyond; unsigned char *pix, *beyond;
int *gray, *red, *green, *blue; int *gray, *red, *green, *blue;
@ -938,7 +972,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
{ {
//int *red, *green, *blue; /*int *red, *green, *blue;*/
red = image->comps[0].data; red = image->comps[0].data;
green = image->comps[1].data; green = image->comps[1].data;
@ -976,7 +1010,11 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
FILE *fdest = NULL; FILE *fdest = NULL;
int adjustR, adjustG, adjustB; int adjustR, adjustG, adjustB;
if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx 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[1].dx == image->comps[2].dx
&& image->comps[0].dy == image->comps[1].dy && image->comps[0].dy == image->comps[1].dy
&& image->comps[1].dy == image->comps[2].dy && image->comps[1].dy == image->comps[2].dy
@ -1164,7 +1202,7 @@ PGX IMAGE FORMAT
<<-- <<-- <<-- <<-- */ <<-- <<-- <<-- <<-- */
unsigned char readuchar(FILE * f) static unsigned char readuchar(FILE * f)
{ {
unsigned char c1; unsigned char c1;
if ( !fread(&c1, 1, 1, f) ) if ( !fread(&c1, 1, 1, f) )
@ -1175,7 +1213,7 @@ unsigned char readuchar(FILE * f)
return c1; return c1;
} }
unsigned short readushort(FILE * f, int bigendian) static unsigned short readushort(FILE * f, int bigendian)
{ {
unsigned char c1, c2; unsigned char c1, c2;
if ( !fread(&c1, 1, 1, f) ) if ( !fread(&c1, 1, 1, f) )
@ -1194,7 +1232,7 @@ unsigned short readushort(FILE * f, int bigendian)
return (c2 << 8) + c1; return (c2 << 8) + c1;
} }
unsigned int readuint(FILE * f, int bigendian) static unsigned int readuint(FILE * f, int bigendian)
{ {
unsigned char c1, c2, c3, c4; unsigned char c1, c2, c3, c4;
if ( !fread(&c1, 1, 1, f) ) if ( !fread(&c1, 1, 1, f) )
@ -1230,6 +1268,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
OPJ_COLOR_SPACE color_space; OPJ_COLOR_SPACE color_space;
opj_image_cmptparm_t cmptparm; /* maximum of 1 component */ opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
opj_image_t * image = NULL; opj_image_t * image = NULL;
int adjustS, ushift, dshift, force8;
char endian1,endian2,sign; char endian1,endian2,sign;
char signtmp[32]; char signtmp[32];
@ -1286,6 +1325,16 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
} else { } else {
cmptparm.sgnd = 0; 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.prec = prec;
cmptparm.bpp = prec; cmptparm.bpp = prec;
cmptparm.dx = parameters->subsampling_dx; cmptparm.dx = parameters->subsampling_dx;
@ -1309,7 +1358,17 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
for (i = 0; i < w * h; i++) { for (i = 0; i < w * h; i++) {
int v; int v;
if (comp->prec <= 8) { 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) { if (!comp->sgnd) {
v = readuchar(f); v = readuchar(f);
} else { } else {
@ -1348,6 +1407,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
char bname[256]; /* buffer for name */ char bname[256]; /* buffer for name */
char *name = bname; /* pointer */ char *name = bname; /* pointer */
int nbytes = 0; int nbytes = 0;
size_t res;
const size_t olen = strlen(outfile); const size_t olen = strlen(outfile);
const size_t dotpos = olen - 4; const size_t dotpos = olen - 4;
const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
@ -1360,7 +1420,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
name = (char*)malloc(total+1); name = (char*)malloc(total+1);
} }
strncpy(name, outfile, dotpos); strncpy(name, outfile, dotpos);
//if (image->numcomps > 1) { /*if (image->numcomps > 1) {*/
sprintf(name+dotpos, "_%d.pgx", compno); sprintf(name+dotpos, "_%d.pgx", compno);
/*} else { /*} else {
strcpy(name+dotpos, ".pgx"); strcpy(name+dotpos, ".pgx");
@ -1390,7 +1450,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) {
int v = image->comps[compno].data[i]; int v = image->comps[compno].data[i];
for (j = nbytes - 1; j >= 0; j--) { for (j = nbytes - 1; j >= 0; j--) {
char byte = (char) (v >> (j * 8)); char byte = (char) (v >> (j * 8));
fwrite(&byte, 1, 1, fdest); res = fwrite(&byte, 1, 1, fdest);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", name);
return 1;
}
} }
} }
fclose(fdest); fclose(fdest);
@ -1802,7 +1866,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
} }
} }
else else
if((format == 7 && header_info.bw)) //MONO if((format == 7 && header_info.bw)) /*MONO*/
{ {
unsigned char uc; unsigned char uc;
@ -1829,7 +1893,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
FILE *fdest = NULL; FILE *fdest = NULL;
const char *tmp = outfile; const char *tmp = outfile;
char *destname; char *destname;
alpha = NULL;
if((prec = image->comps[0].prec) > 16) if((prec = image->comps[0].prec) > 16)
{ {
fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16" fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
@ -1891,7 +1955,7 @@ int imagetopnm(opj_image_t * image, const char *outfile)
{ {
fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
opj_version(), wr, hr, max); opj_version(), wr, hr, max);
alpha = NULL; adjustA = 0; adjustA = 0;
} }
adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
@ -2017,16 +2081,6 @@ int imagetopnm(opj_image_t * image, const char *outfile)
<<-- <<-- <<-- <<-- */ <<-- <<-- <<-- <<-- */
typedef struct tiff_infoheader{
DWORD tiWidth; // Width of Image in pixel
DWORD tiHeight; // Height of Image in pixel
DWORD tiPhoto; // Photometric
WORD tiBps; // Bits per sample
WORD tiSf; // Sample Format
WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
}tiff_infoheader_t;
int imagetotif(opj_image_t * image, const char *outfile) int imagetotif(opj_image_t * image, const char *outfile)
{ {
int width, height, imgsize; int width, height, imgsize;
@ -2130,7 +2184,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
} }
else else
break; break;
}//for(i = 0;) }/*for(i = 0;)*/
if(last_i < ssize) if(last_i < ssize)
{ {
@ -2163,10 +2217,10 @@ int imagetotif(opj_image_t * image, const char *outfile)
} }
else else
break; break;
}//for(i) }/*for(i)*/
}//if(last_i < ssize) }/*if(last_i < ssize)*/
} //if(bps == 8) } /*if(bps == 8)*/
else else
if(bps == 16) if(bps == 16)
{ {
@ -2198,8 +2252,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
b = (b<<ushift) + (b>>dshift); b = (b<<ushift) + (b>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift); if(has_alpha) a = (a<<ushift) + (a>>dshift);
} }
dat8[i+0] = r;//LSB dat8[i+0] = r;/*LSB*/
dat8[i+1] = (r >> 8);//MSB dat8[i+1] = (r >> 8);/*MSB*/
dat8[i+2] = g; dat8[i+2] = g;
dat8[i+3] = (g >> 8); dat8[i+3] = (g >> 8);
dat8[i+4] = b; dat8[i+4] = b;
@ -2214,7 +2268,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
} }
else else
break; break;
}//for(i = 0;) }/*for(i = 0;)*/
if(last_i < ssize) if(last_i < ssize)
{ {
@ -2243,8 +2297,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
b = (b<<ushift) + (b>>dshift); b = (b<<ushift) + (b>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift); if(has_alpha) a = (a<<ushift) + (a>>dshift);
} }
dat8[i+0] = r;//LSB dat8[i+0] = r;/*LSB*/
if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/
if(i+2 < ssize) dat8[i+2] = g; else break; if(i+2 < ssize) dat8[i+2] = g; else break;
if(i+3 < ssize) dat8[i+3] = (g >> 8);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+4 < ssize) dat8[i+4] = b; else break;
@ -2259,18 +2313,18 @@ int imagetotif(opj_image_t * image, const char *outfile)
} }
else else
break; break;
}//for(i) }/*for(i)*/
}//if(last_i < ssize) }/*if(last_i < ssize)*/
}//if(bps == 16) }/*if(bps == 16)*/
(void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
}//for(strip = 0; ) }/*for(strip = 0; )*/
_TIFFfree((void*)buf); _TIFFfree((void*)buf);
TIFFClose(tif); TIFFClose(tif);
return 0; return 0;
}//RGB(A) }/*RGB(A)*/
if(image->numcomps == 1 /* GRAY */ if(image->numcomps == 1 /* GRAY */
|| ( image->numcomps == 2 /* GRAY_ALPHA */ || ( image->numcomps == 2 /* GRAY_ALPHA */
@ -2331,8 +2385,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
} }
else else
break; break;
}//for(i ) }/*for(i )*/
}//if(bps == 8 }/*if(bps == 8*/
else else
if(bps == 16) if(bps == 16)
{ {
@ -2357,21 +2411,21 @@ int imagetotif(opj_image_t * image, const char *outfile)
r = (r<<ushift) + (r>>dshift); r = (r<<ushift) + (r>>dshift);
if(has_alpha) a = (a<<ushift) + (a>>dshift); if(has_alpha) a = (a<<ushift) + (a>>dshift);
} }
dat8[i+0] = r;//LSB dat8[i+0] = r;/*LSB*/
dat8[i+1] = r >> 8;//MSB dat8[i+1] = r >> 8;/*MSB*/
if(has_alpha) if(has_alpha)
{ {
dat8[i+2] = a; dat8[i+2] = a;
dat8[i+3] = a >> 8; dat8[i+3] = a >> 8;
} }
index++; index++;
}//if(index < imgsize) }/*if(index < imgsize)*/
else else
break; break;
}//for(i ) }/*for(i )*/
} }
(void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size);
}//for(strip }/*for(strip*/
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
@ -2398,7 +2452,6 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
int subsampling_dx = parameters->subsampling_dx; int subsampling_dx = parameters->subsampling_dx;
int subsampling_dy = parameters->subsampling_dy; int subsampling_dy = parameters->subsampling_dy;
TIFF *tif; TIFF *tif;
tiff_infoheader_t Info;
tdata_t buf; tdata_t buf;
tstrip_t strip; tstrip_t strip;
tsize_t strip_size; tsize_t strip_size;
@ -2408,6 +2461,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
opj_image_t *image = NULL; opj_image_t *image = NULL;
int imgsize = 0; int imgsize = 0;
int has_alpha = 0; int has_alpha = 0;
unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
unsigned int tiWidth, tiHeight;
tif = TIFFOpen(filename, "r"); tif = TIFFOpen(filename, "r");
@ -2416,38 +2471,36 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename); fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
return 0; return 0;
} }
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth); tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight); tiWidth = tiHeight = 0;
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
Info.tiPhoto = 0;
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
w= Info.tiWidth;
h= Info.tiHeight;
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;
if( !tiBps || !tiPhoto)
{ {
int b, p; if( !tiBps)
if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
if( !b || !p)
{
if( !b)
fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits" fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
" implemented\n",Info.tiBps); " implemented\n",tiBps);
else else
if( !p) if( !tiPhoto)
fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)" fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
" and GRAY(A) has been implemented\n",(int) Info.tiPhoto); " and GRAY(A) has been implemented\n",(int) tiPhoto);
fprintf(stderr,"\tAborting\n"); fprintf(stderr,"\tAborting\n");
TIFFClose(tif); TIFFClose(tif);
return NULL; return NULL;
}
} }
{/* From: tiff-4.0.x/libtiff/tif_getimage.c : */ {/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
@ -2464,7 +2517,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
case EXTRASAMPLE_UNSPECIFIED: case EXTRASAMPLE_UNSPECIFIED:
/* Workaround for some images without correct info about alpha channel /* Workaround for some images without correct info about alpha channel
*/ */
if(Info.tiSpp > 3) if(tiSpp > 3)
has_alpha = 1; has_alpha = 1;
break; break;
@ -2474,12 +2527,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
break; 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)); memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */ if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
{ {
numcomps = 3 + has_alpha; numcomps = 3 + has_alpha;
color_space = CLRSPC_SRGB; color_space = CLRSPC_SRGB;
@ -2493,8 +2549,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
{ {
cmptparm[j].prec = Info.tiBps; cmptparm[j].prec = tiBps;
cmptparm[j].bpp = Info.tiBps; cmptparm[j].bpp = tiBps;
} }
cmptparm[j].dx = subsampling_dx; cmptparm[j].dx = subsampling_dx;
cmptparm[j].dy = subsampling_dy; cmptparm[j].dy = subsampling_dy;
@ -2533,7 +2589,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
dat8 = (unsigned char*)buf; dat8 = (unsigned char*)buf;
if(Info.tiBps == 16) if(tiBps == 16)
{ {
step = 6 + has_alpha + has_alpha; step = 6 + has_alpha + has_alpha;
@ -2541,9 +2597,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
{ {
if(index < imgsize) if(index < imgsize)
{ {
image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R 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[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */
image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */
if(has_alpha) if(has_alpha)
image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6]; image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6];
@ -2565,10 +2621,10 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
break; break;
}//for(i = 0) }/*for(i = 0)*/
}//if(Info.tiBps == 16) }/*if(tiBps == 16)*/
else else
if(Info.tiBps == 8) if(tiBps == 8)
{ {
step = 3 + has_alpha; step = 3 + has_alpha;
@ -2576,9 +2632,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
{ {
if(index < imgsize) if(index < imgsize)
{ {
image->comps[0].data[index] = dat8[i+0];// R image->comps[0].data[index] = dat8[i+0];/* R */
image->comps[1].data[index] = dat8[i+1];// G image->comps[1].data[index] = dat8[i+1];/* G */
image->comps[2].data[index] = dat8[i+2];// B image->comps[2].data[index] = dat8[i+2];/* B */
if(has_alpha) if(has_alpha)
image->comps[3].data[index] = dat8[i+3]; image->comps[3].data[index] = dat8[i+3];
@ -2593,13 +2649,13 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
image->comps[3].data[index] = image->comps[3].data[index] << 4 ; image->comps[3].data[index] = image->comps[3].data[index] << 4 ;
} }
index++; index++;
}//if(index }/*if(index*/
else else
break; break;
}//for(i ) }/*for(i )*/
}//if( Info.tiBps == 8) }/*if( tiBps == 8)*/
else else
if(Info.tiBps == 12)/* CINEMA file */ if(tiBps == 12)/* CINEMA file */
{ {
step = 9; step = 9;
@ -2620,25 +2676,25 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
break; break;
}//for(i ) }/*for(i )*/
} }
}//for(strip = 0; ) }/*for(strip = 0; )*/
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
return image; return image;
}//RGB(A) }/*RGB(A)*/
if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */ if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
{ {
numcomps = 1 + has_alpha; numcomps = 1 + has_alpha;
color_space = CLRSPC_GRAY; color_space = CLRSPC_GRAY;
for(j = 0; j < numcomps; ++j) for(j = 0; j < numcomps; ++j)
{ {
cmptparm[j].prec = Info.tiBps; cmptparm[j].prec = tiBps;
cmptparm[j].bpp = Info.tiBps; cmptparm[j].bpp = tiBps;
cmptparm[j].dx = subsampling_dx; cmptparm[j].dx = subsampling_dx;
cmptparm[j].dy = subsampling_dy; cmptparm[j].dy = subsampling_dy;
cmptparm[j].w = w; cmptparm[j].w = w;
@ -2676,7 +2732,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
dat8 = (unsigned char*)buf; dat8 = (unsigned char*)buf;
if(Info.tiBps == 16) if(tiBps == 16)
{ {
step = 2 + has_alpha + has_alpha; step = 2 + has_alpha + has_alpha;
@ -2691,10 +2747,10 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
break; break;
}//for(i ) }/*for(i )*/
} }
else else
if(Info.tiBps == 8) if(tiBps == 8)
{ {
step = 1 + has_alpha; step = 1 + has_alpha;
@ -2709,14 +2765,14 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
} }
else else
break; break;
}//for(i ) }/*for(i )*/
} }
}//for(strip = 0; }/*for(strip = 0;*/
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
}//GRAY(A) }/*GRAY(A)*/
return image; return image;
@ -2835,6 +2891,7 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw
int imagetoraw(opj_image_t * image, const char *outfile) int imagetoraw(opj_image_t * image, const char *outfile)
{ {
FILE *rawFile = NULL; FILE *rawFile = NULL;
size_t res;
int compno; int compno;
int w, h; int w, h;
int line, row; int line, row;
@ -2872,7 +2929,11 @@ int imagetoraw(opj_image_t * image, const char *outfile)
for (line = 0; line < h; line++) { for (line = 0; line < h; line++) {
for(row = 0; row < w; row++) { for(row = 0; row < w; row++) {
curr = (signed char) (*ptr & mask); curr = (signed char) (*ptr & mask);
fwrite(&curr, sizeof(signed char), 1, rawFile); 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++; ptr++;
} }
} }
@ -2885,7 +2946,11 @@ int imagetoraw(opj_image_t * image, const char *outfile)
for (line = 0; line < h; line++) { for (line = 0; line < h; line++) {
for(row = 0; row < w; row++) { for(row = 0; row < w; row++) {
curr = (unsigned char) (*ptr & mask); curr = (unsigned char) (*ptr & mask);
fwrite(&curr, sizeof(unsigned char), 1, rawFile); 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++; ptr++;
} }
} }
@ -2903,9 +2968,17 @@ int imagetoraw(opj_image_t * image, const char *outfile)
unsigned char temp; unsigned char temp;
curr = (signed short int) (*ptr & mask); curr = (signed short int) (*ptr & mask);
temp = (unsigned char) (curr >> 8); temp = (unsigned char) (curr >> 8);
fwrite(&temp, 1, 1, rawFile); res = fwrite(&temp, 1, 1, rawFile);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
temp = (unsigned char) curr; temp = (unsigned char) curr;
fwrite(&temp, 1, 1, rawFile); res = fwrite(&temp, 1, 1, rawFile);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
ptr++; ptr++;
} }
} }
@ -2920,9 +2993,17 @@ int imagetoraw(opj_image_t * image, const char *outfile)
unsigned char temp; unsigned char temp;
curr = (unsigned short int) (*ptr & mask); curr = (unsigned short int) (*ptr & mask);
temp = (unsigned char) (curr >> 8); temp = (unsigned char) (curr >> 8);
fwrite(&temp, 1, 1, rawFile); res = fwrite(&temp, 1, 1, rawFile);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
temp = (unsigned char) curr; temp = (unsigned char) curr;
fwrite(&temp, 1, 1, rawFile); res = fwrite(&temp, 1, 1, rawFile);
if( res < 1 ) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
return 1;
}
ptr++; ptr++;
} }
} }

View File

@ -29,7 +29,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "opj_config.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -50,6 +49,7 @@
#define _strnicmp strncasecmp #define _strnicmp strncasecmp
#endif /* _WIN32 */ #endif /* _WIN32 */
#include "opj_config.h"
#include "openjpeg.h" #include "openjpeg.h"
#include "opj_getopt.h" #include "opj_getopt.h"
#include "convert.h" #include "convert.h"
@ -427,7 +427,7 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparamet
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename); sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename)); strncpy(parameters->infile, infilename, sizeof(infilename));
//Set output file /*Set output file*/
strcpy(temp_ofname,get_file_name(image_filename)); strcpy(temp_ofname,get_file_name(image_filename));
while((temp_p = strtok(NULL,".")) != NULL){ while((temp_p = strtok(NULL,".")) != NULL){
strcat(temp_ofname,temp1); strcat(temp_ofname,temp1);
@ -1550,7 +1550,7 @@ int main(int argc, char **argv) {
num_images=get_num_images(img_fol.imgdirpath); num_images=get_num_images(img_fol.imgdirpath);
dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
if(dirptr){ if(dirptr){
dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images*sizeof(char*)); dirptr->filename = (char**) malloc(num_images*sizeof(char*));
if(!dirptr->filename_buf){ if(!dirptr->filename_buf){
return 0; return 0;
@ -1681,6 +1681,7 @@ int main(int argc, char **argv) {
if (parameters.cod_format == J2K_CFMT) { /* J2K format output */ if (parameters.cod_format == J2K_CFMT) { /* J2K format output */
int codestream_length; int codestream_length;
size_t res;
opj_cio_t *cio = NULL; opj_cio_t *cio = NULL;
FILE *f = NULL; FILE *f = NULL;
@ -1698,7 +1699,7 @@ int main(int argc, char **argv) {
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
/* encode the image */ /* encode the image */
if (*indexfilename) // If need to extract codestream information if (*indexfilename) /* If need to extract codestream information*/
bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info); bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
else else
bSuccess = opj_encode(cinfo, cio, image, NULL); bSuccess = opj_encode(cinfo, cio, image, NULL);
@ -1715,7 +1716,11 @@ int main(int argc, char **argv) {
fprintf(stderr, "failed to open %s for writing\n", parameters.outfile); fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
return 1; return 1;
} }
fwrite(cio->buffer, 1, codestream_length, f); res = fwrite(cio->buffer, 1, codestream_length, f);
if( res < (size_t)codestream_length ) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length, parameters.outfile);
return 1;
}
fclose(f); fclose(f);
fprintf(stderr,"Generated outfile %s\n",parameters.outfile); fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
@ -1736,6 +1741,7 @@ int main(int argc, char **argv) {
opj_destroy_cstr_info(&cstr_info); opj_destroy_cstr_info(&cstr_info);
} else { /* JP2 format output */ } else { /* JP2 format output */
int codestream_length; int codestream_length;
size_t res;
opj_cio_t *cio = NULL; opj_cio_t *cio = NULL;
FILE *f = NULL; FILE *f = NULL;
opj_cinfo_t *cinfo = NULL; opj_cinfo_t *cinfo = NULL;
@ -1754,7 +1760,7 @@ int main(int argc, char **argv) {
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
/* encode the image */ /* encode the image */
if (*indexfilename || parameters.jpip_on) // If need to extract codestream information if (*indexfilename || parameters.jpip_on) /* If need to extract codestream information*/
bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info); bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
else else
bSuccess = opj_encode(cinfo, cio, image, NULL); bSuccess = opj_encode(cinfo, cio, image, NULL);
@ -1771,7 +1777,11 @@ int main(int argc, char **argv) {
fprintf(stderr, "failed to open %s for writing\n", parameters.outfile); fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
return 1; return 1;
} }
fwrite(cio->buffer, 1, codestream_length, f); res = fwrite(cio->buffer, 1, codestream_length, f);
if( res < (size_t)codestream_length ) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length, parameters.outfile);
return 1;
}
fclose(f); fclose(f);
fprintf(stderr,"Generated outfile %s\n",parameters.outfile); fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
/* close and free the byte stream */ /* close and free the byte stream */

View File

@ -25,13 +25,10 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "opj_config.h"
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "openjpeg.h" #include "openjpeg.h"
#include "index.h" #include "index.h"
@ -263,7 +260,7 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
int x1 = x0 + cstr_info->tile_x; int x1 = x0 + cstr_info->tile_x;
int y1 = y0 + cstr_info->tile_y; int y1 = y0 + cstr_info->tile_y;
// Count the maximum number of precincts /* Count the maximum number of precincts */
int max_numprec = 0; int max_numprec = 0;
for (resno = 0; resno < max_numdecompos + 1; resno++) { for (resno = 0; resno < max_numdecompos + 1; resno++) {
int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
@ -314,7 +311,7 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
} /* PCRL */ } /* PCRL */
else { /* CPRL */ else { /* CPRL */
// Count the maximum number of precincts /* Count the maximum number of precincts */
int max_numprec = 0; int max_numprec = 0;
for (resno = 0; resno < max_numdecompos + 1; resno++) { for (resno = 0; resno < max_numdecompos + 1; resno++) {
int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno]; int numprec = cstr_info->tile[tileno].pw[resno] * cstr_info->tile[tileno].ph[resno];
@ -383,7 +380,7 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
fprintf(stream, "%d\n", cstr_info->marknum); fprintf(stream, "%d\n", cstr_info->marknum);
fprintf(stream, "type\tstart_pos length\n"); fprintf(stream, "type\tstart_pos length\n");
for (x = 0; x < cstr_info->marknum; x++) for (x = 0; x < cstr_info->marknum; x++)
fprintf(stream, "%X\t%9" OPJ_OFF_F "d %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len); fprintf(stream, "%X\t%9d %9d\n", cstr_info->marker[x].type, cstr_info->marker[x].pos, cstr_info->marker[x].len);
} }
/* <<UniPG */ /* <<UniPG */
fclose(stream); fclose(stream);
@ -392,5 +389,3 @@ int write_index_file(opj_codestream_info_t *cstr_info, char *index) {
return 0; return 0;
} }