From 8c9fd1b35199f439e8e8e87e12c0c73ac4b01c1c Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Thu, 1 Mar 2012 16:10:27 +0000 Subject: [PATCH] [1.5] Apply big-endian patch from winfried: libopenjpeg and WORDS_BIGENDIAN, 2012/02/14 --- applications/codec/convert.c | 136 +++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 53 deletions(-) diff --git a/applications/codec/convert.c b/applications/codec/convert.c index 4189d83f..e715629a 100644 --- a/applications/codec/convert.c +++ b/applications/codec/convert.c @@ -94,8 +94,8 @@ struct tga_header 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 @@ -179,6 +179,16 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, 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) { @@ -216,8 +226,15 @@ static int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, 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; @@ -493,7 +510,7 @@ int imagetotga(opj_image_t * image, const char *outfile) { 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) */ @@ -583,7 +600,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) 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; } @@ -997,7 +1014,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { 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 @@ -1251,6 +1268,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { 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]; @@ -1307,6 +1325,16 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { } 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; @@ -1330,7 +1358,17 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { for (i = 0; i < w * h; i++) { int v; - if (comp->prec <= 8) { + if(force8) + { + v = readuchar(f) + adjustS; + v = (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 { @@ -2043,16 +2081,6 @@ int imagetopnm(opj_image_t * image, const char *outfile) <<-- <<-- <<-- <<-- */ -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; @@ -2424,7 +2452,6 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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; @@ -2434,6 +2461,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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"); @@ -2442,38 +2471,36 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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 : */ @@ -2490,7 +2517,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without correct info about alpha channel */ - if(Info.tiSpp > 3) + if(tiSpp > 3) has_alpha = 1; break; @@ -2500,12 +2527,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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; @@ -2519,8 +2549,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } 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; @@ -2559,7 +2589,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); dat8 = (unsigned char*)buf; - if(Info.tiBps == 16) + if(tiBps == 16) { step = 6 + has_alpha + has_alpha; @@ -2592,9 +2622,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) else break; }/*for(i = 0)*/ - }/*if(Info.tiBps == 16)*/ + }/*if(tiBps == 16)*/ else - if(Info.tiBps == 8) + if(tiBps == 8) { step = 3 + has_alpha; @@ -2623,9 +2653,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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; @@ -2656,15 +2686,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) 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; @@ -2702,7 +2732,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); dat8 = (unsigned char*)buf; - if(Info.tiBps == 16) + if(tiBps == 16) { step = 2 + has_alpha + has_alpha; @@ -2720,7 +2750,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) }/*for(i )*/ } else - if(Info.tiBps == 8) + if(tiBps == 8) { step = 1 + has_alpha;