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"
"\n\t: refused.\n",__FILE__,__LINE__,opj_prec);
return 1;
} }
tmp--; prec = opj_prec;
tmp--; is16 = force16 = ushift = dshift = has_alpha = 0; fails = 1;
S2 = *tmp;
if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx if(prec > 8 && prec < 16)
{
prec = 16; force16 = 1;
}
while (*tmp) ++tmp; tmp -= 2;
want_gray = (*tmp == 'g' || *tmp == 'G');
ncomp = image->numcomps;
fprintf(stderr,"%s:%d:ncomp(%d) opj_prec(%d) prec(%d)\n",
__FILE__,__LINE__,ncomp,opj_prec,prec);
if (ncomp > 2
&& image->comps[0].dx == image->comps[1].dx
&& image->comps[1].dx == image->comps[2].dx && image->comps[1].dx == image->comps[2].dx
&& image->comps[0].dy == image->comps[1].dy && image->comps[0].dy == image->comps[1].dy
&& image->comps[1].dy == image->comps[2].dy && image->comps[1].dy == image->comps[2].dy
&& image->comps[0].prec == image->comps[1].prec && image->comps[0].prec == image->comps[1].prec
&& image->comps[1].prec == image->comps[2].prec && image->comps[1].prec == image->comps[2].prec
&& S2 !='g' && S2 !='G') { && !want_gray
)
{
fdest = fopen(outfile, "wb"); fdest = fopen(outfile, "wb");
if (!fdest) {
if (!fdest)
{
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return 1; return fails;
} }
wr = image->comps[0].w; hr = image->comps[0].h;
w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); max = (1<<prec) - 1; has_alpha = (ncomp == 4);
wr = image->comps[0].w;
h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); is16 = (prec == 16);
hr = image->comps[0].h;
max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1; red = image->comps[0].data;
green = image->comps[1].data;
blue = image->comps[2].data;
image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor); if(has_alpha)
image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor); {
fprintf(fdest, "P7\n# OpenJPEG-%s\nWIDTH %d\nHEIGHT %d\nDEPTH 4\n"
fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max); "MAXVAL %d\nTUPLTYPE RGB_ALPHA\nENDHDR\n", opj_version(),
wr, hr, max);
if (image->comps[0].prec > 8) { alpha = image->comps[3].data;
adjustR = image->comps[0].prec - 8;
printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
} }
else else
adjustR = 0; {
if (image->comps[1].prec > 8) { fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n",
adjustG = image->comps[1].prec - 8; opj_version(), wr, hr, max);
printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec); alpha = NULL;
} }
else if(force16)
adjustG = 0; {
if (image->comps[2].prec > 8) { ushift = 16 - opj_prec; dshift = opj_prec - ushift;
adjustB = image->comps[2].prec - 8;
printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
} }
else adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
adjustB = 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]; #if BYTE_ORDER == LITTLE_ENDIAN
b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); fprintf(fdest, "%c%c",(unsigned char)v, (unsigned char)(v/256));
bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2)); #else
fprintf(fdest, "%c%c",(unsigned char)(v/256), (unsigned char)v);
#endif
v = *green + adjustG; ++green;
fprintf(fdest, "%c%c%c", rc, gc, bc); 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
v = *blue + adjustB; ++blue;
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
} }
fclose(fdest); continue;
} else { }
int ncomp=(S2=='g' || S2=='G')?1:image->numcomps; /* prec <= 8: */
if (image->numcomps > ncomp) {
fprintf(stderr,"WARNING -> [PGM files] Only the first component\n"); fprintf(fdest, "%c%c%c",(unsigned char)*red++, (unsigned char)*green++,
(unsigned char)*blue++);
if(has_alpha)
fprintf(fdest, "%c", (unsigned char)*alpha++);
} /* 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"); fprintf(stderr," is written to the file\n");
} }
for (compno = 0; compno < ncomp; compno++) { destname = (char*)malloc(strlen(outfile) + 8);
char name[256];
if (ncomp > 1) {
sprintf(name, "%d.%s", compno, outfile);
} else {
sprintf(name, "%s", outfile);
}
fdest = fopen(name, "wb"); for (compno = 0; compno < ncomp; compno++)
if (!fdest) { {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); 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; return 1;
} }
wr = image->comps[compno].w; hr = image->comps[compno].h;
w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx); max = (1<<prec) - 1;
wr = image->comps[compno].w;
h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy); fprintf(fdest, "P5\n#OpenJPEG-%s\n%d %d\n%d\n",
hr = image->comps[compno].h; opj_version(), wr, hr, max);
max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1; red = image->comps[compno].data;
adjustR =
(image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor); if(prec > 8)
image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor); {
/* MSB first: 'man ppm' */
fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max); for (i = 0; i < wr * hr; i++)
{
if (image->comps[compno].prec > 8) { v = *red + adjustR; ++red;
adjustX = image->comps[0].prec - 8;
printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec); 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
} }
else
adjustX = 0;
for (i = 0; i < wr * hr; i++) { }/* for(i */
int l; }
unsigned char lc; else /* prec <= 8 */
l = image->comps[compno].data[i]; {
l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); for(i = 0; i < wr * hr; ++i)
lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2)); {
fprintf(fdest, "%c", lc); fprintf(fdest, "%c", (unsigned char)(*red + adjustR)); ++red;
}
} }
fclose(fdest); 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); }
#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
} }
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 */