[trunk] Partially rework r2506, original patch from issue 171 was totally bogus (untested?) and did break most of the test (eg. p1_04 family)

convert.c duplicate a lot of code, this patch only adresses the PGX codec section of the code.
Update issue 171
Update issue 264
This commit is contained in:
Mathieu Malaterre 2014-02-28 17:29:28 +00:00
parent 978de6fb97
commit ec593a2549
1 changed files with 82 additions and 71 deletions

View File

@ -1416,10 +1416,28 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
return image; return image;
} }
#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,-2147483648,2147483647);
}
else
{
if (prec <= 8) return CLAMP(value,0,255);
else if (prec <= 16) return CLAMP(value,0,65535);
else return CLAMP(value,0,4294967295);
}
}
int imagetopgx(opj_image_t * image, const char *outfile) int imagetopgx(opj_image_t * image, const char *outfile)
{ {
int w, h; int w, h;
int i, j, compno, fails; int i, j, compno, fails = 1;
FILE *fdest = NULL; FILE *fdest = NULL;
for (compno = 0; compno < image->numcomps; compno++) for (compno = 0; compno < image->numcomps; compno++)
@ -1437,53 +1455,46 @@ int imagetopgx(opj_image_t * image, const char *outfile)
{ {
/* `pgx` was recognized but there is no dot at expected position */ /* `pgx` was recognized but there is no dot at expected position */
fprintf(stderr, "ERROR -> Impossible happen." ); fprintf(stderr, "ERROR -> Impossible happen." );
return 1; goto fin;
} }
if( total > 256 ) if( total > 256 )
{ {
name = (char*)malloc(total+1); name = (char*)malloc(total+1);
} }
strncpy(name, outfile, dotpos); strncpy(name, outfile, dotpos);
/*if (image->numcomps > 1) {*/
sprintf(name+dotpos, "_%d.pgx", compno); sprintf(name+dotpos, "_%d.pgx", compno);
/*} else {
strcpy(name+dotpos, ".pgx");
}*/
fdest = fopen(name, "wb"); fdest = fopen(name, "wb");
/* dont need name anymore */
if( total > 256 ) free(name);
if (!fdest) if (!fdest)
{ {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
return 1; goto fin;
} }
/* dont need name anymore */
if( total > 256 )
free(name);
fails = 1;
w = image->comps[compno].w; w = image->comps[compno].w;
h = image->comps[compno].h; h = image->comps[compno].h;
fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
comp->prec, w, h); w, h);
if (comp->prec <= 8) if (comp->prec <= 8)
nbytes = 1; nbytes = 1;
else else if (comp->prec <= 16)
if (comp->prec <= 16)
nbytes = 2; nbytes = 2;
else else
nbytes = 4; nbytes = 4;
for (i = 0; i < w * h; i++) for (i = 0; i < w * h; i++)
{ {
int v, val = image->comps[compno].data[i]; /* FIXME: clamp func is being called within a loop */
unsigned char byte; const int val = clamp(image->comps[compno].data[i],
comp->prec, comp->sgnd);
for (j = nbytes - 1; j >= 0; j--) for (j = nbytes - 1; j >= 0; j--)
{ {
v = (int)(val >> (j * 8)); int v = (int)(val >> (j * 8));
if(v > 255) v = 255; else if(v < 0) v = 0; unsigned char byte = (unsigned char)v;
byte = (unsigned char)v;
res = fwrite(&byte, 1, 1, fdest); res = fwrite(&byte, 1, 1, fdest);
if( res < 1 ) if( res < 1 )