imagetopnm() has been rewritten to allow 16-bits precision, and PAM (P7) support. See Netpbm for more info. (patch from szukw000).

This commit is contained in:
Antonin Descampe 2011-01-09 22:54:35 +00:00
parent 7441340ab5
commit 985a163e33
2 changed files with 251 additions and 144 deletions

View File

@ -5,6 +5,9 @@ What's New for OpenJPEG
! : changed ! : changed
+ : added + : added
January 9, 2011
! [antonin] imagetopnm() has been rewritten to allow 16-bits precision, and PAM (P7) support. See Netpbm for more info. (patch from szukw000).
January 7, 2011 January 7, 2011
! [szukw000] changed report code in Makefile.am ! [szukw000] changed report code in Makefile.am

View File

@ -35,6 +35,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef _WIN32
#define BYTE_ORDER LITTLE_ENDIAN
#else
#include <endian.h>
#endif
#ifdef HAVE_LIBTIFF #ifdef HAVE_LIBTIFF
#ifdef _WIN32 #ifdef _WIN32
#include "../libs/libtiff/tiffio.h" #include "../libs/libtiff/tiffio.h"
@ -67,25 +73,6 @@ static int int_floorlog2(int a) {
return l; return l;
} }
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
static int int_ceildivpow2(int a, int b) {
return (a + (1 << b) - 1) >> b;
}
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
static int int_ceildiv(int a, int b) {
return (a + b - 1) / b;
}
/* -->> -->> -->> -->> /* -->> -->> -->> -->>
TGA IMAGE FORMAT TGA IMAGE FORMAT
@ -1278,141 +1265,236 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
return image; return image;
} }
int imagetopnm(opj_image_t * image, const char *outfile) { int imagetopnm(opj_image_t * image, const char *outfile)
int w, wr, h, hr, max; {
int i, compno; int *red, *green, *blue, *alpha;
int adjustR, adjustG, adjustB, adjustX; int wr, hr, max;
int i, compno, ncomp;
int adjustR, adjustG, adjustB;
int fails, is16, force16, want_gray, has_alpha;
int prec, opj_prec, ushift, dshift, v;
FILE *fdest = NULL; FILE *fdest = NULL;
char S2;
const char *tmp = outfile; const char *tmp = outfile;
char *destname;
while (*tmp) { if((opj_prec = image->comps[0].prec) > 16)
tmp++; {
} fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16"
tmp--; "\n\t: refused.\n",__FILE__,__LINE__,opj_prec);
tmp--; return 1;
S2 = *tmp; }
prec = opj_prec;
is16 = force16 = ushift = dshift = has_alpha = 0; fails = 1;
if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx if(prec > 8 && prec < 16)
&& image->comps[1].dx == image->comps[2].dx {
&& image->comps[0].dy == image->comps[1].dy prec = 16; force16 = 1;
&& image->comps[1].dy == image->comps[2].dy }
&& image->comps[0].prec == image->comps[1].prec
&& image->comps[1].prec == image->comps[2].prec while (*tmp) ++tmp; tmp -= 2;
&& S2 !='g' && S2 !='G') { want_gray = (*tmp == 'g' || *tmp == 'G');
ncomp = image->numcomps;
fdest = fopen(outfile, "wb"); fprintf(stderr,"%s:%d:ncomp(%d) opj_prec(%d) prec(%d)\n",
if (!fdest) { __FILE__,__LINE__,ncomp,opj_prec,prec);
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return 1;
}
w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); if (ncomp > 2
wr = image->comps[0].w; && image->comps[0].dx == image->comps[1].dx
&& image->comps[1].dx == image->comps[2].dx
h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); && image->comps[0].dy == image->comps[1].dy
hr = image->comps[0].h; && image->comps[1].dy == image->comps[2].dy
&& image->comps[0].prec == image->comps[1].prec
&& image->comps[1].prec == image->comps[2].prec
&& !want_gray
)
{
fdest = fopen(outfile, "wb");
if (!fdest)
{
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return fails;
}
wr = image->comps[0].w; hr = image->comps[0].h;
max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1; max = (1<<prec) - 1; has_alpha = (ncomp == 4);
image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max); is16 = (prec == 16);
if (image->comps[0].prec > 8) { red = image->comps[0].data;
adjustR = image->comps[0].prec - 8; green = image->comps[1].data;
printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec); blue = image->comps[2].data;
}
else if(has_alpha)
adjustR = 0; {
if (image->comps[1].prec > 8) { fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH 4\n"
adjustG = image->comps[1].prec - 8; "MAXVAL %d\nTUPLTYPE RGB_ALPHA\nENDHDR\n", opj_version(),
printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec); wr, hr, max);
} alpha = image->comps[3].data;
else }
adjustG = 0; else
if (image->comps[2].prec > 8) { {
adjustB = image->comps[2].prec - 8; fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec); opj_version(), wr, hr, max);
} alpha = NULL;
else }
adjustB = 0; if(force16)
{
ushift = 16 - opj_prec; dshift = opj_prec - ushift;
}
adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
for(i = 0; i < wr * hr; ++i)
{
if(is16)
{
/* MSB first: 'man ppm' */
for (i = 0; i < wr * hr; i++) { v = *red + adjustR; ++red;
int r, g, b;
unsigned char rc,gc,bc;
r = image->comps[0].data[i];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
g = image->comps[1].data[i]; if(force16) { v = (v<<ushift) + (v>>dshift); }
g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
b = image->comps[2].data[i];
b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
fprintf(fdest, "%c%c%c", rc, gc, bc);
}
fclose(fdest);
} else { #if BYTE_ORDER == LITTLE_ENDIAN
int ncomp=(S2=='g' || S2=='G')?1:image->numcomps; fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
if (image->numcomps > ncomp) { #else
fprintf(stderr,"WARNING -> [PGM files] Only the first component\n"); fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
fprintf(stderr," is written to the file\n"); #endif
} v = *green + adjustG; ++green;
for (compno = 0; compno < ncomp; compno++) {
char name[256]; if(force16) { v = (v<<ushift) + (v>>dshift); }
if (ncomp > 1) {
sprintf(name, "%d.%s", compno, outfile); #if BYTE_ORDER == LITTLE_ENDIAN
} else { fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
sprintf(name, "%s", outfile); #else
} fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
#endif
fdest = fopen(name, "wb");
if (!fdest) { v = *blue + adjustB; ++blue;
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
return 1; if(force16) { v = (v<<ushift) + (v>>dshift); }
}
#if BYTE_ORDER == LITTLE_ENDIAN
w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx); fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
wr = image->comps[compno].w; #else
fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy); #endif
hr = image->comps[compno].h;
if(has_alpha)
max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1; {
v = *alpha++;
image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor); if(force16) { v = (v<<ushift) + (v>>dshift); }
fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max); #if BYTE_ORDER == LITTLE_ENDIAN
fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
if (image->comps[compno].prec > 8) { #else
adjustX = image->comps[0].prec - 8; fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec); #endif
} }
else continue;
adjustX = 0;
}
for (i = 0; i < wr * hr; i++) { /* prec <= 8: */
int l;
unsigned char lc; fprintf(fdest, "%c%c%c",(unsigned char)*red++, (unsigned char)*green++,
l = image->comps[compno].data[i]; (unsigned char)*blue++);
l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2)); if(has_alpha)
fprintf(fdest, "%c", lc); fprintf(fdest, "%c", (unsigned char)*alpha++);
}
fclose(fdest); } /* for(i */
}
} fclose(fdest); return 0;
}
if(force16)
{
ushift = 16 - opj_prec; dshift = opj_prec - ushift;
}
/* YUV or MONO: */
if(want_gray) ncomp = 1;
//FIXME: with[out] alpha ?
has_alpha = 0; alpha = NULL;
if (image->numcomps > ncomp)
{
fprintf(stderr,"WARNING -> [PGM file] Only the first component\n");
fprintf(stderr," is written to the file\n");
}
destname = (char*)malloc(strlen(outfile) + 8);
for (compno = 0; compno < ncomp; compno++)
{
if (ncomp > 1)
sprintf(destname, "%d.%s", compno, outfile);
else
sprintf(destname, "%s", outfile);
fdest = fopen(destname, "wb");
if (!fdest)
{
fprintf(stderr, "ERROR -> failed to open %s for writing\n", destname);
free(destname);
return 1;
}
wr = image->comps[compno].w; hr = image->comps[compno].h;
max = (1<<prec) - 1;
fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
opj_version(), wr, hr, max);
red = image->comps[compno].data;
adjustR =
(image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
if(prec > 8)
{
/* MSB first: 'man ppm' */
for (i = 0; i < wr * hr; i++)
{
v = *red + adjustR; ++red;
if(force16) { v = (v<<ushift) + (v>>dshift); }
#if BYTE_ORDER == LITTLE_ENDIAN
fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
#else
fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
#endif
if(has_alpha)
{
v = *alpha++;
if(force16) { v = (v<<ushift) + (v>>dshift); }
#if BYTE_ORDER == LITTLE_ENDIAN
fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
#else
fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
#endif
}
}/* for(i */
}
else /* prec <= 8 */
{
for(i = 0; i < wr * hr; ++i)
{
fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
}
}
fclose(fdest);
} /* for (compno */
free(destname);
return 0; return 0;
} }/* imagetopnm() */
#ifdef HAVE_LIBTIFF #ifdef HAVE_LIBTIFF
/* -->> -->> -->> -->> /* -->> -->> -->> -->>
@ -2517,19 +2599,30 @@ int imagetopng(opj_image_t * image, const char *write_idf)
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
#if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
v = *green + adjustG; ++green; v = *green + adjustG; ++green;
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
#if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
v = *blue + adjustB; ++blue; v = *blue + adjustB; ++blue;
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
#if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; *d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
if(has_alpha) if(has_alpha)
{ {
@ -2537,7 +2630,11 @@ int imagetopng(opj_image_t * image, const char *write_idf)
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v; #if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
} }
continue; continue;
} }
@ -2621,7 +2718,11 @@ int imagetopng(opj_image_t * image, const char *write_idf)
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)(v & 0xff); #if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
if(has_alpha) if(has_alpha)
{ {
@ -2629,7 +2730,11 @@ int imagetopng(opj_image_t * image, const char *write_idf)
if(force16) { v = (v<<ushift) + (v>>dshift); } if(force16) { v = (v<<ushift) + (v>>dshift); }
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)(v & 0xff); #if BYTE_ORDER == LITTLE_ENDIAN
*d++ = (unsigned char)(v>>8); *d++ = (unsigned char)v;
#else
*d++ = (unsigned char)(v>>24); *d++ = (unsigned char)(v>>16);
#endif
} }
}/* for(x) */ }/* for(x) */
png_write_row(png, row_buf); png_write_row(png, row_buf);
@ -2683,4 +2788,3 @@ fin:
return fails; return fails;
}/* imagetopng() */ }/* imagetopng() */
#endif /* HAVE_LIBPNG */ #endif /* HAVE_LIBPNG */