[trunk] modify image_to_j2k and the lib to support functionalities given by the v2 alpha branch

This commit is contained in:
Mickael Savinaud
2012-03-15 10:23:20 +00:00
parent d9940f416b
commit 3a78e8010d
20 changed files with 7050 additions and 263 deletions

View File

@@ -6,6 +6,7 @@
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
* 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>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -211,6 +212,8 @@ void encode_help_display(void) {
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");
@@ -1054,6 +1057,73 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
}
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>> */
@@ -1486,20 +1556,30 @@ void info_callback(const char *msg, void *client_data) {
}
/* -------------------------------------------------------------------------- */
/**
* IMAGE_TO_J2K MAIN
*/
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv) {
opj_bool bSuccess;
FILE *f = NULL;
opj_cparameters_t parameters; /* compression parameters */
img_fol_t img_fol;
opj_event_mgr_t event_mgr; /* event manager */
opj_stream_t *cio = 00;
opj_codec_t* cinfo = 00;
opj_image_t *image = NULL;
int i,num_images;
int imageno;
dircnt_t *dirptr = NULL;
raw_cparameters_t raw_cp;
opj_codestream_info_t cstr_info; /* Codestream information structure */
char indexfilename[OPJ_PATH_LEN]; /* index file name */
int i, num_images, imageno;
img_fol_t img_fol;
dircnt_t *dirptr = NULL;
opj_bool bSuccess;
/*
configure the event callbacks (not required)
setting of each callback is optionnal
@@ -1580,6 +1660,7 @@ int main(int argc, char **argv) {
continue;
}
}
switch(parameters.decod_format) {
case PGX_DFMT:
break;
@@ -1600,213 +1681,151 @@ int main(int argc, char **argv) {
continue;
}
/* decode the source image */
/* ----------------------- */
/* decode the source image */
/* ----------------------- */
switch (parameters.decod_format) {
case PGX_DFMT:
image = pgxtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load pgx file\n");
return 1;
}
break;
switch (parameters.decod_format) {
case PGX_DFMT:
image = pgxtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load pgx file\n");
return 1;
}
break;
case PXM_DFMT:
image = pnmtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load pnm file\n");
return 1;
}
break;
case PXM_DFMT:
image = pnmtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load pnm file\n");
return 1;
}
break;
case BMP_DFMT:
image = bmptoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load bmp file\n");
return 1;
}
break;
case BMP_DFMT:
image = bmptoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load bmp file\n");
return 1;
}
break;
#ifdef HAVE_LIBTIFF
case TIF_DFMT:
image = tiftoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load tiff file\n");
return 1;
}
break;
case TIF_DFMT:
image = tiftoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load tiff file\n");
return 1;
}
break;
#endif /* HAVE_LIBTIFF */
case RAW_DFMT:
image = rawtoimage(parameters.infile, &parameters, &raw_cp);
if (!image) {
fprintf(stderr, "Unable to load raw file\n");
return 1;
}
break;
case TGA_DFMT:
image = tgatoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load tga file\n");
return 1;
}
break;
case RAW_DFMT:
image = rawtoimage(parameters.infile, &parameters, &raw_cp);
if (!image) {
fprintf(stderr, "Unable to load raw file\n");
return 1;
}
break;
case TGA_DFMT:
image = tgatoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load tga file\n");
return 1;
}
break;
#ifdef HAVE_LIBPNG
case PNG_DFMT:
image = pngtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load png file\n");
return 1;
}
break;
case PNG_DFMT:
image = pngtoimage(parameters.infile, &parameters);
if (!image) {
fprintf(stderr, "Unable to load png file\n");
return 1;
}
break;
#endif /* HAVE_LIBPNG */
}
/* Can happen if input file is TIFF or PNG
* and HAVE_LIBTIF or HAVE_LIBPNG is undefined
*/
if( !image)
{
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;
}
if(parameters.cp_cinema){
cinema_setup_encoder(&parameters,image,&img_fol);
/* Decide if MCT should be used */
parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
if(parameters.cp_cinema){
cinema_setup_encoder(&parameters,image,&img_fol);
}
/* encode the destination image */
/* ---------------------------- */
switch(parameters.decod_format) {
case J2K_CFMT: /* JPEG-2000 codestream */
{
/* Get a decoder handle */
cinfo = opj_create_compress_v2(CODEC_J2K);
break;
}
/* encode the destination image */
/* ---------------------------- */
if (parameters.cod_format == J2K_CFMT) { /* J2K format output */
int codestream_length;
size_t res;
opj_cio_t *cio = NULL;
FILE *f = NULL;
/* get a J2K compressor handle */
opj_cinfo_t* cinfo = opj_create_compress(CODEC_J2K);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
/* setup the encoder parameters using the current image and user parameters */
opj_setup_encoder(cinfo, &parameters, image);
/* open a byte stream for writing */
/* allocate memory for all tiles */
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
/* encode the image */
if (*indexfilename) /* If need to extract codestream information*/
bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
else
bSuccess = opj_encode(cinfo, cio, image, NULL);
if (!bSuccess) {
opj_cio_close(cio);
fprintf(stderr, "failed to encode image\n");
return 1;
}
codestream_length = cio_tell(cio);
/* write the buffer to disk */
f = fopen(parameters.outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
return 1;
}
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);
fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
/* close and free the byte stream */
opj_cio_close(cio);
/* Write the index to disk */
if (*indexfilename) {
bSuccess = write_index_file(&cstr_info, indexfilename);
if (bSuccess) {
fprintf(stderr, "Failed to output index file into [%s]\n", indexfilename);
}
}
/* free remaining compression structures */
opj_destroy_compress(cinfo);
if (*indexfilename)
opj_destroy_cstr_info(&cstr_info);
} else { /* JP2 format output */
int codestream_length;
size_t res;
opj_cio_t *cio = NULL;
FILE *f = NULL;
opj_cinfo_t *cinfo = NULL;
/* get a JP2 compressor handle */
cinfo = opj_create_compress(CODEC_JP2);
/* catch events using our callbacks and give a local context */
opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
/* setup the encoder parameters using the current image and using user parameters */
opj_setup_encoder(cinfo, &parameters, image);
/* open a byte stream for writing */
/* allocate memory for all tiles */
cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0);
/* encode the image */
if (*indexfilename || parameters.jpip_on) /* If need to extract codestream information*/
bSuccess = opj_encode_with_info(cinfo, cio, image, &cstr_info);
else
bSuccess = opj_encode(cinfo, cio, image, NULL);
if (!bSuccess) {
opj_cio_close(cio);
fprintf(stderr, "failed to encode image\n");
return 1;
}
codestream_length = cio_tell(cio);
/* write the buffer to disk */
f = fopen(parameters.outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", parameters.outfile);
return 1;
}
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);
fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
/* close and free the byte stream */
opj_cio_close(cio);
/* Write the index to disk */
if (*indexfilename) {
bSuccess = write_index_file(&cstr_info, indexfilename);
if (bSuccess) {
fprintf(stderr, "Failed to output index file\n");
}
}
/* free remaining compression structures */
opj_destroy_compress(cinfo);
if (*indexfilename)
opj_destroy_cstr_info(&cstr_info);
case JP2_CFMT: /* JPEG 2000 compressed image data */
{
/* Get a decoder handle */
cinfo = opj_create_compress_v2(CODEC_JP2);
break;
}
default:
fprintf(stderr, "skipping file..\n");
opj_stream_destroy(cio);
continue;
}
opj_setup_encoder_v2(cinfo, &parameters, image);
/* Open the output file*/
f = fopen(parameters.outfile, "wb");
if (! f) {
fprintf(stderr, "Not enable to create output file!\n");
opj_stream_destroy(cio);
return 1;
}
/* open a byte stream for writing and allocate memory for all tiles */
cio = opj_stream_create_default_file_stream(f,OPJ_FALSE);
if (! cio){
return 1;
}
/* encode the image */
bSuccess = opj_start_compress(cinfo,image,cio);
bSuccess = bSuccess && opj_encode_v2(cinfo, cio);
bSuccess = bSuccess && opj_end_compress(cinfo, cio);
if (!bSuccess) {
opj_stream_destroy(cio);
fclose(f);
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(cio);
fclose(f);
/* free remaining compression structures */
opj_destroy_codec(cinfo);
/* 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_comment) free(parameters.cp_comment);
if(parameters.cp_matrice) free(parameters.cp_matrice);
return 0;