static unsigned short get_ushort(unsigned short val) {
-#ifdef ORDER_BIGENDIAN
- return( ((val & 0xff) << 8) + (val >> 8) );
+#ifdef WORDS_BIGENDIAN
+ return( ((val & 0xff) << 8) + (val >> 8) );
#else
return( val );
#endif
return 1;
}
+#if WORDS_BIGENDIAN == 1
+
+static inline int16_t swap16(int16_t x)
+{
+ return((((u_int16_t)x & 0x00ffU) << 8) |
+ (((u_int16_t)x & 0xff00U) >> 8));
+}
+
+#endif
+
static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height,
opj_bool flip_image)
{
image_w = (unsigned short)width;
image_h = (unsigned short) height;
+#if WORDS_BIGENDIAN == 0
if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+#else
+ image_w = swap16(image_w);
+ image_h = swap16(image_h);
+ if(fwrite(&image_w, 2, 1, fp) != 1) goto fails;
+ if(fwrite(&image_h, 2, 1, fp) != 1) goto fails;
+#endif
if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails;
typedef unsigned short int WORD;
/* DWORD defines a four byte word */
-typedef unsigned long int DWORD;
+typedef unsigned int DWORD;
typedef struct {
WORD bfType; /* 'BM' for Bitmap (19776) */
if(Info_h.biSize != 40)
{
- fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize);
+ fprintf(stderr,"Error, unknown BMP header size %d\n", Info_h.biSize);
fclose(IN);
return NULL;
}
fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
return 1;
}
- if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
+ if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
&& image->comps[1].dx == image->comps[2].dx
&& image->comps[0].dy == image->comps[1].dy
&& image->comps[1].dy == image->comps[2].dy
OPJ_COLOR_SPACE color_space;
opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
opj_image_t * image = NULL;
+ int adjustS, ushift, dshift, force8;
char endian1,endian2,sign;
char signtmp[32];
} else {
cmptparm.sgnd = 0;
}
+ if(prec < 8)
+ {
+ force8 = 1;
+ ushift = 8 - prec; dshift = prec - ushift;
+ if(cmptparm.sgnd) adjustS = (1<<(prec - 1)); else adjustS = 0;
+ cmptparm.sgnd = 0;
+ prec = 8;
+ }
+ else ushift = dshift = force8 = adjustS = 0;
+
cmptparm.prec = prec;
cmptparm.bpp = prec;
cmptparm.dx = parameters->subsampling_dx;
for (i = 0; i < w * h; i++) {
int v;
- if (comp->prec <= 8) {
+ if(force8)
+ {
+ v = readuchar(f) + adjustS;
+ v = (v<<ushift) + (v>>dshift);
+ comp->data[i] = (unsigned char)v;
+
+ if(v > max) max = v;
+
+ continue;
+ }
+ if (comp->prec == 8) {
if (!comp->sgnd) {
v = readuchar(f);
} else {
<<-- <<-- <<-- <<-- */
-typedef struct tiff_infoheader{
- DWORD tiWidth; /* Width of Image in pixel*/
- DWORD tiHeight; /* Height of Image in pixel */
- DWORD tiPhoto; /* Photometric */
- WORD tiBps; /* Bits per sample */
- WORD tiSf; /* Sample Format */
- WORD tiSpp; /* Sample per pixel 1-bilevel,gray scale , 2- RGB */
- WORD tiPC; /* Planar config (1-Interleaved, 2-Planarcomp) */
-}tiff_infoheader_t;
-
int imagetotif(opj_image_t * image, const char *outfile)
{
int width, height, imgsize;
int subsampling_dx = parameters->subsampling_dx;
int subsampling_dy = parameters->subsampling_dy;
TIFF *tif;
- tiff_infoheader_t Info;
tdata_t buf;
tstrip_t strip;
tsize_t strip_size;
opj_image_t *image = NULL;
int imgsize = 0;
int has_alpha = 0;
+ unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
+ unsigned int tiWidth, tiHeight;
tif = TIFFOpen(filename, "r");
fprintf(stderr, "tiftoimage:Failed to open %s for reading\n", filename);
return 0;
}
- TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
- TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
- TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
- TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
- TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
- Info.tiPhoto = 0;
- TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
- TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
- w= Info.tiWidth;
- h= Info.tiHeight;
-
+ tiBps = tiPhoto = tiSf = tiSpp = tiPC = 0;
+ tiWidth = tiHeight = 0;
+
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tiWidth);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &tiHeight);
+ TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &tiBps);
+ TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &tiSf);
+ TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
+ TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
+ TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
+ w= tiWidth;
+ h= tiHeight;
+
+ if(tiBps != 8 && tiBps != 16 && tiBps != 12) tiBps = 0;
+ if(tiPhoto != 1 && tiPhoto != 2) tiPhoto = 0;
+
+ if( !tiBps || !tiPhoto)
{
- int b, p;
-
- if((b = Info.tiBps) != 8 && b != 16 && b != 12) b = 0;
- if((p = Info.tiPhoto) != 1 && p != 2) p = 0;
-
- if( !b || !p)
- {
- if( !b)
+ if( !tiBps)
fprintf(stderr,"imagetotif: Bits=%d, Only 8 and 16 bits"
- " implemented\n",Info.tiBps);
+ " implemented\n",tiBps);
else
- if( !p)
+ if( !tiPhoto)
fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A)"
- " and GRAY(A) has been implemented\n",(int) Info.tiPhoto);
+ " and GRAY(A) has been implemented\n",(int) tiPhoto);
fprintf(stderr,"\tAborting\n");
TIFFClose(tif);
return NULL;
- }
}
{/* From: tiff-4.0.x/libtiff/tif_getimage.c : */
case EXTRASAMPLE_UNSPECIFIED:
/* Workaround for some images without correct info about alpha channel
*/
- if(Info.tiSpp > 3)
+ if(tiSpp > 3)
has_alpha = 1;
break;
break;
}
}
+ else /* extrasamples == 0 */
+ if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
}
+
/* initialize image components
*/
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
- if(Info.tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
+ if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
{
numcomps = 3 + has_alpha;
color_space = CLRSPC_SRGB;
}
else
{
- cmptparm[j].prec = Info.tiBps;
- cmptparm[j].bpp = Info.tiBps;
+ cmptparm[j].prec = tiBps;
+ cmptparm[j].bpp = tiBps;
}
cmptparm[j].dx = subsampling_dx;
cmptparm[j].dy = subsampling_dy;
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
dat8 = (unsigned char*)buf;
- if(Info.tiBps == 16)
+ if(tiBps == 16)
{
step = 6 + has_alpha + has_alpha;
else
break;
}/*for(i = 0)*/
- }/*if(Info.tiBps == 16)*/
+ }/*if(tiBps == 16)*/
else
- if(Info.tiBps == 8)
+ if(tiBps == 8)
{
step = 3 + has_alpha;
else
break;
}/*for(i )*/
- }/*if( Info.tiBps == 8)*/
+ }/*if( tiBps == 8)*/
else
- if(Info.tiBps == 12)/* CINEMA file */
+ if(tiBps == 12)/* CINEMA file */
{
step = 9;
return image;
}/*RGB(A)*/
- if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
+ if(tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */
{
numcomps = 1 + has_alpha;
color_space = CLRSPC_GRAY;
for(j = 0; j < numcomps; ++j)
{
- cmptparm[j].prec = Info.tiBps;
- cmptparm[j].bpp = Info.tiBps;
+ cmptparm[j].prec = tiBps;
+ cmptparm[j].bpp = tiBps;
cmptparm[j].dx = subsampling_dx;
cmptparm[j].dy = subsampling_dy;
cmptparm[j].w = w;
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
dat8 = (unsigned char*)buf;
- if(Info.tiBps == 16)
+ if(tiBps == 16)
{
step = 2 + has_alpha + has_alpha;
}/*for(i )*/
}
else
- if(Info.tiBps == 8)
+ if(tiBps == 8)
{
step = 1 + has_alpha;