[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;
}
#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 w, h;
int i, j, compno, fails;
int i, j, compno, fails = 1;
FILE *fdest = NULL;
for (compno = 0; compno < image->numcomps; compno++)
@ -1435,55 +1453,48 @@ int imagetopgx(opj_image_t * image, const char *outfile)
if( outfile[dotpos] != '.' )
{
/* `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." );
return 1;
goto fin;
}
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");
/* dont need name anymore */
if( total > 256 ) free(name);
if (!fdest)
{
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;
h = image->comps[compno].h;
fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+',
comp->prec, w, 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)
else if (comp->prec <= 16)
nbytes = 2;
else
nbytes = 4;
for (i = 0; i < w * h; i++)
{
int v, val = image->comps[compno].data[i];
unsigned char byte;
/* FIXME: clamp func is being called within a loop */
const int val = clamp(image->comps[compno].data[i],
comp->prec, comp->sgnd);
for (j = nbytes - 1; j >= 0; j--)
{
v = (int)(val >> (j * 8));
if(v > 255) v = 255; else if(v < 0) v = 0;
byte = (unsigned char)v;
int v = (int)(val >> (j * 8));
unsigned char byte = (unsigned char)v;
res = fwrite(&byte, 1, 1, fdest);
if( res < 1 )