[trunk] Add new behavior for codecs. Now supports little endian RAWL format. It is now possible to encode directly little endian raw file, by simply using the rawl extension. Code has been modified to use strcasecmp instead of strncasecmp to cope with 4 letters files extensions.
Update issue 141
This commit is contained in:
parent
0c5fb4a81b
commit
bdb4d96678
|
@ -2802,8 +2802,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
|
|||
RAW IMAGE FORMAT
|
||||
|
||||
<<-- <<-- <<-- <<-- */
|
||||
|
||||
opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
|
||||
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;
|
||||
|
||||
|
@ -2877,17 +2876,26 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw
|
|||
unsigned short value;
|
||||
for(compno = 0; compno < numcomps; compno++) {
|
||||
for (i = 0; i < w * h; i++) {
|
||||
unsigned char temp;
|
||||
if (!fread(&temp, 1, 1, f)) {
|
||||
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;
|
||||
}
|
||||
value = temp << 8;
|
||||
if (!fread(&temp, 1, 1, f)) {
|
||||
if (!fread(&temp2, 1, 1, f)) {
|
||||
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
|
||||
return NULL;
|
||||
}
|
||||
value += temp;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -2905,7 +2913,15 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw
|
|||
return image;
|
||||
}
|
||||
|
||||
int imagetoraw(opj_image_t * image, const char *outfile)
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int imagetoraw_common(opj_image_t * image, const char *outfile, opj_bool big_endian)
|
||||
{
|
||||
FILE *rawFile = NULL;
|
||||
size_t res;
|
||||
|
@ -2982,16 +2998,17 @@ int imagetoraw(opj_image_t * image, const char *outfile)
|
|||
ptr = image->comps[compno].data;
|
||||
for (line = 0; line < h; line++) {
|
||||
for(row = 0; row < w; row++) {
|
||||
unsigned char temp;
|
||||
unsigned char temp1;
|
||||
unsigned char temp2;
|
||||
curr = (signed short int) (*ptr & mask);
|
||||
temp = (unsigned char) (curr >> 8);
|
||||
res = fwrite(&temp, 1, 1, rawFile);
|
||||
temp1 = (unsigned char) (curr >> 8);
|
||||
temp2 = (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;
|
||||
}
|
||||
temp = (unsigned char) curr;
|
||||
res = fwrite(&temp, 1, 1, rawFile);
|
||||
res = fwrite(&temp2, 1, 1, rawFile);
|
||||
if( res < 1 ) {
|
||||
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
|
||||
return 1;
|
||||
|
@ -3007,16 +3024,25 @@ int imagetoraw(opj_image_t * image, const char *outfile)
|
|||
ptr = image->comps[compno].data;
|
||||
for (line = 0; line < h; line++) {
|
||||
for(row = 0; row < w; row++) {
|
||||
unsigned char temp;
|
||||
unsigned char temp1;
|
||||
unsigned char temp2;
|
||||
curr = (unsigned short int) (*ptr & mask);
|
||||
temp = (unsigned char) (curr >> 8);
|
||||
res = fwrite(&temp, 1, 1, rawFile);
|
||||
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;
|
||||
}
|
||||
temp = (unsigned char) curr;
|
||||
res = fwrite(&temp, 1, 1, rawFile);
|
||||
res = fwrite(&temp2, 1, 1, rawFile);
|
||||
if( res < 1 ) {
|
||||
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
|
||||
return 1;
|
||||
|
@ -3041,6 +3067,16 @@ int imagetoraw(opj_image_t * image, const char *outfile)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int imagetoraw(opj_image_t * image, const char *outfile)
|
||||
{
|
||||
return imagetoraw_common(image, outfile, OPJ_TRUE);
|
||||
}
|
||||
|
||||
int imagetorawl(opj_image_t * image, const char *outfile)
|
||||
{
|
||||
return imagetoraw_common(image, outfile, OPJ_FALSE);
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBPNG
|
||||
|
||||
#define PNG_MAGIC "\x89PNG\x0d\x0a\x1a\x0a"
|
||||
|
|
|
@ -72,7 +72,9 @@ int imagetopnm(opj_image_t *image, const char *outfile);
|
|||
|
||||
/* RAW conversion */
|
||||
int imagetoraw(opj_image_t * image, const char *outfile);
|
||||
int imagetorawl(opj_image_t * image, const char *outfile);
|
||||
opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp);
|
||||
opj_image_t* rawltoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp);
|
||||
|
||||
/* PNG conversion*/
|
||||
extern int imagetopng(opj_image_t *image, const char *write_idf);
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windirent.h"
|
||||
|
@ -44,10 +45,10 @@
|
|||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
#define _stricmp strcasecmp
|
||||
#define _strnicmp strncasecmp
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#include "opj_config.h"
|
||||
|
@ -137,7 +138,7 @@ void encode_help_display(void) {
|
|||
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 and TGA formats\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");
|
||||
|
@ -394,17 +395,17 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
|
|||
int get_file_format(char *filename) {
|
||||
unsigned int i;
|
||||
static const char *extension[] = {
|
||||
"pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "tga", "png", "j2k", "jp2", "j2c", "jpc"
|
||||
"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, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
|
||||
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(_strnicmp(ext, extension[i], 3) == 0) {
|
||||
if(strcasecmp(ext, extension[i]) == 0) {
|
||||
return format[i];
|
||||
}
|
||||
}
|
||||
|
@ -621,6 +622,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
|
|||
case BMP_DFMT:
|
||||
case TIF_DFMT:
|
||||
case RAW_DFMT:
|
||||
case RAWL_DFMT:
|
||||
case TGA_DFMT:
|
||||
case PNG_DFMT:
|
||||
break;
|
||||
|
@ -1491,7 +1493,8 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
|
|||
}
|
||||
}
|
||||
|
||||
if (parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0) {
|
||||
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");
|
||||
|
@ -1600,6 +1603,8 @@ int main(int argc, char **argv) {
|
|||
dircnt_t *dirptr = NULL;
|
||||
|
||||
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);
|
||||
|
@ -1683,6 +1688,7 @@ int main(int argc, char **argv) {
|
|||
case TIF_DFMT:
|
||||
break;
|
||||
case RAW_DFMT:
|
||||
case RAWL_DFMT:
|
||||
break;
|
||||
case TGA_DFMT:
|
||||
break;
|
||||
|
@ -1739,6 +1745,14 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
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) {
|
||||
|
@ -1800,6 +1814,13 @@ int main(int argc, char **argv) {
|
|||
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_v2(l_codec, ¶meters, image);
|
||||
|
||||
/* Open the output file*/
|
||||
|
@ -1821,9 +1842,29 @@ int main(int argc, char **argv) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
free(l_data);
|
||||
}
|
||||
else {
|
||||
bSuccess = bSuccess && opj_encode_v2(l_codec, l_stream);
|
||||
if (!bSuccess) {
|
||||
fprintf(stderr, "failed to encode image: opj_encode_v2\n");
|
||||
}
|
||||
}
|
||||
bSuccess = bSuccess && opj_end_compress(l_codec, l_stream);
|
||||
if (!bSuccess) {
|
||||
|
@ -1833,6 +1874,8 @@ int main(int argc, char **argv) {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -44,10 +44,10 @@
|
|||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
#define _stricmp strcasecmp
|
||||
#define _strnicmp strncasecmp
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#include "openjpeg.h"
|
||||
|
@ -214,15 +214,15 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
|
|||
/* -------------------------------------------------------------------------- */
|
||||
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 };
|
||||
static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "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, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
|
||||
char * ext = strrchr(filename, '.');
|
||||
if (ext == NULL)
|
||||
return -1;
|
||||
ext++;
|
||||
if(ext) {
|
||||
for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
|
||||
if(_strnicmp(ext, extension[i], 3) == 0) {
|
||||
if(strcasecmp(ext, extension[i]) == 0) {
|
||||
return format[i];
|
||||
}
|
||||
}
|
||||
|
@ -379,6 +379,8 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
|
|||
break;
|
||||
case RAW_DFMT:
|
||||
break;
|
||||
case RAWL_DFMT:
|
||||
break;
|
||||
case TGA_DFMT:
|
||||
break;
|
||||
case PNG_DFMT:
|
||||
|
@ -416,6 +418,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
|
|||
case RAW_DFMT:
|
||||
img_fol->out_format = "raw";
|
||||
break;
|
||||
case RAWL_DFMT:
|
||||
img_fol->out_format = "rawl";
|
||||
break;
|
||||
case TGA_DFMT:
|
||||
img_fol->out_format = "raw";
|
||||
break;
|
||||
|
@ -909,6 +914,15 @@ int main(int argc, char **argv)
|
|||
}
|
||||
break;
|
||||
|
||||
case RAWL_DFMT: /* RAWL */
|
||||
if(imagetorawl(image, parameters.outfile)){
|
||||
fprintf(stdout,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
|
||||
}
|
||||
else {
|
||||
fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
|
||||
}
|
||||
break;
|
||||
|
||||
case TGA_DFMT: /* TGA */
|
||||
if(imagetotga(image, parameters.outfile)){
|
||||
fprintf(stdout,"Error generating tga file. Outfile %s not generated\n",parameters.outfile);
|
||||
|
|
|
@ -44,5 +44,6 @@
|
|||
#define RAW_DFMT 15
|
||||
#define TGA_DFMT 16
|
||||
#define PNG_DFMT 17
|
||||
#define RAWL_DFMT 18
|
||||
|
||||
#endif /* _OPJ_FORMAT_DEFS_H_ */
|
||||
|
|
Loading…
Reference in New Issue