Allow to read 3/5/7/9/11/13/15 bpp TIF files

This commit is contained in:
mayeut 2016-04-23 04:24:04 +02:00
parent 06e8dc9edd
commit 54393d9e38
2 changed files with 416 additions and 2 deletions

View File

@ -313,6 +313,221 @@ int imagetotif(opj_image_t * image, const char *outfile)
return 0;
}/* imagetotif() */
#define GETBITS(dest, nb) { \
int needed = (nb); \
unsigned int dst = 0U; \
if (available == 0) { \
val = *pSrc++; \
available = 8; \
} \
while (needed > available) { \
dst |= val & ((1U << available) - 1U); \
needed -= available; \
dst <<= needed; \
val = *pSrc++; \
available = 8; \
} \
dst |= (val >> (available - needed)) & ((1U << needed) - 1U); \
available -= needed; \
dest = (OPJ_INT32)dst; \
}
static void tif_3uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 >> 5));
pDst[i+1] = (OPJ_INT32)(((val0 & 0x1FU) >> 2));
pDst[i+2] = (OPJ_INT32)(((val0 & 0x3U) << 1) | (val1 >> 7));
pDst[i+3] = (OPJ_INT32)(((val1 & 0x7FU) >> 4));
pDst[i+4] = (OPJ_INT32)(((val1 & 0xFU) >> 1));
pDst[i+5] = (OPJ_INT32)(((val1 & 0x1U) << 2) | (val2 >> 6));
pDst[i+6] = (OPJ_INT32)(((val2 & 0x3FU) >> 3));
pDst[i+7] = (OPJ_INT32)(((val2 & 0x7U)));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 3)
if (length > 1U) {
GETBITS(pDst[i+1], 3)
if (length > 2U) {
GETBITS(pDst[i+2], 3)
if (length > 3U) {
GETBITS(pDst[i+3], 3)
if (length > 4U) {
GETBITS(pDst[i+4], 3)
if (length > 5U) {
GETBITS(pDst[i+5], 3)
if (length > 6U) {
GETBITS(pDst[i+6], 3)
}
}
}
}
}
}
}
}
static void tif_5uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 >> 3));
pDst[i+1] = (OPJ_INT32)(((val0 & 0x7U) << 2) | (val1 >> 6));
pDst[i+2] = (OPJ_INT32)(((val1 & 0x3FU) >> 1));
pDst[i+3] = (OPJ_INT32)(((val1 & 0x1U) << 4) | (val2 >> 4));
pDst[i+4] = (OPJ_INT32)(((val2 & 0xFU) << 1) | (val3 >> 7));
pDst[i+5] = (OPJ_INT32)(((val3 & 0x7FU) >> 2));
pDst[i+6] = (OPJ_INT32)(((val3 & 0x3U) << 3) | (val4 >> 5));
pDst[i+7] = (OPJ_INT32)(((val4 & 0x1FU)));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 5)
if (length > 1U) {
GETBITS(pDst[i+1], 5)
if (length > 2U) {
GETBITS(pDst[i+2], 5)
if (length > 3U) {
GETBITS(pDst[i+3], 5)
if (length > 4U) {
GETBITS(pDst[i+4], 5)
if (length > 5U) {
GETBITS(pDst[i+5], 5)
if (length > 6U) {
GETBITS(pDst[i+6], 5)
}
}
}
}
}
}
}
}
static void tif_7uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
OPJ_UINT32 val5 = *pSrc++;
OPJ_UINT32 val6 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 >> 1));
pDst[i+1] = (OPJ_INT32)(((val0 & 0x1U) << 6) | (val1 >> 2));
pDst[i+2] = (OPJ_INT32)(((val1 & 0x3U) << 5) | (val2 >> 3));
pDst[i+3] = (OPJ_INT32)(((val2 & 0x7U) << 4) | (val3 >> 4));
pDst[i+4] = (OPJ_INT32)(((val3 & 0xFU) << 3) | (val4 >> 5));
pDst[i+5] = (OPJ_INT32)(((val4 & 0x1FU) << 2) | (val5 >> 6));
pDst[i+6] = (OPJ_INT32)(((val5 & 0x3FU) << 1) | (val6 >> 7));
pDst[i+7] = (OPJ_INT32)(((val6 & 0x7FU)));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 7)
if (length > 1U) {
GETBITS(pDst[i+1], 7)
if (length > 2U) {
GETBITS(pDst[i+2], 7)
if (length > 3U) {
GETBITS(pDst[i+3], 7)
if (length > 4U) {
GETBITS(pDst[i+4], 7)
if (length > 5U) {
GETBITS(pDst[i+5], 7)
if (length > 6U) {
GETBITS(pDst[i+6], 7)
}
}
}
}
}
}
}
}
static void tif_9uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
OPJ_UINT32 val5 = *pSrc++;
OPJ_UINT32 val6 = *pSrc++;
OPJ_UINT32 val7 = *pSrc++;
OPJ_UINT32 val8 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 << 1) | (val1 >> 7));
pDst[i+1] = (OPJ_INT32)(((val1 & 0x7FU) << 2) | (val2 >> 6));
pDst[i+2] = (OPJ_INT32)(((val2 & 0x3FU) << 3) | (val3 >> 5));
pDst[i+3] = (OPJ_INT32)(((val3 & 0x1FU) << 4) | (val4 >> 4));
pDst[i+4] = (OPJ_INT32)(((val4 & 0xFU) << 5) | (val5 >> 3));
pDst[i+5] = (OPJ_INT32)(((val5 & 0x7U) << 6) | (val6 >> 2));
pDst[i+6] = (OPJ_INT32)(((val6 & 0x3U) << 7) | (val7 >> 1));
pDst[i+7] = (OPJ_INT32)(((val7 & 0x1U) << 8) | (val8));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 9)
if (length > 1U) {
GETBITS(pDst[i+1], 9)
if (length > 2U) {
GETBITS(pDst[i+2], 9)
if (length > 3U) {
GETBITS(pDst[i+3], 9)
if (length > 4U) {
GETBITS(pDst[i+4], 9)
if (length > 5U) {
GETBITS(pDst[i+5], 9)
if (length > 6U) {
GETBITS(pDst[i+6], 9)
}
}
}
}
}
}
}
}
static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
@ -345,6 +560,60 @@ static void tif_10uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T lengt
}
}
}
static void tif_11uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
OPJ_UINT32 val5 = *pSrc++;
OPJ_UINT32 val6 = *pSrc++;
OPJ_UINT32 val7 = *pSrc++;
OPJ_UINT32 val8 = *pSrc++;
OPJ_UINT32 val9 = *pSrc++;
OPJ_UINT32 val10 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 << 3) | (val1 >> 5));
pDst[i+1] = (OPJ_INT32)(((val1 & 0x1FU) << 6) | (val2 >> 2));
pDst[i+2] = (OPJ_INT32)(((val2 & 0x3U) << 9) | (val3 << 1) | (val4 >> 7));
pDst[i+3] = (OPJ_INT32)(((val4 & 0x7FU) << 4) | (val5 >> 4));
pDst[i+4] = (OPJ_INT32)(((val5 & 0xFU) << 7) | (val6 >> 1));
pDst[i+5] = (OPJ_INT32)(((val6 & 0x1U) << 10) | (val7 << 2) | (val8 >> 6));
pDst[i+6] = (OPJ_INT32)(((val8 & 0x3FU) << 5) | (val9 >> 3));
pDst[i+7] = (OPJ_INT32)(((val9 & 0x7U) << 8) | (val10));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 11)
if (length > 1U) {
GETBITS(pDst[i+1], 11)
if (length > 2U) {
GETBITS(pDst[i+2], 11)
if (length > 3U) {
GETBITS(pDst[i+3], 11)
if (length > 4U) {
GETBITS(pDst[i+4], 11)
if (length > 5U) {
GETBITS(pDst[i+5], 11)
if (length > 6U) {
GETBITS(pDst[i+6], 11)
}
}
}
}
}
}
}
}
static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
@ -362,6 +631,62 @@ static void tif_12uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T lengt
pDst[i+0] = (OPJ_INT32)((val0 << 4) | (val1 >> 4));
}
}
static void tif_13uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
OPJ_UINT32 val5 = *pSrc++;
OPJ_UINT32 val6 = *pSrc++;
OPJ_UINT32 val7 = *pSrc++;
OPJ_UINT32 val8 = *pSrc++;
OPJ_UINT32 val9 = *pSrc++;
OPJ_UINT32 val10 = *pSrc++;
OPJ_UINT32 val11 = *pSrc++;
OPJ_UINT32 val12 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 << 5) | (val1 >> 3));
pDst[i+1] = (OPJ_INT32)(((val1 & 0x7U) << 10) | (val2 << 2) | (val3 >> 6));
pDst[i+2] = (OPJ_INT32)(((val3 & 0x3FU) << 7) | (val4 >> 1));
pDst[i+3] = (OPJ_INT32)(((val4 & 0x1U) << 12) | (val5 << 4) | (val6 >> 4));
pDst[i+4] = (OPJ_INT32)(((val6 & 0xFU) << 9) | (val7 << 1) | (val8 >> 7));
pDst[i+5] = (OPJ_INT32)(((val8 & 0x7FU) << 6) | (val9 >> 2));
pDst[i+6] = (OPJ_INT32)(((val9 & 0x3U) << 11) | (val10 << 3) | (val11 >> 5));
pDst[i+7] = (OPJ_INT32)(((val11 & 0x1FU) << 8) | (val12));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 13)
if (length > 1U) {
GETBITS(pDst[i+1], 13)
if (length > 2U) {
GETBITS(pDst[i+2], 13)
if (length > 3U) {
GETBITS(pDst[i+3], 13)
if (length > 4U) {
GETBITS(pDst[i+4], 13)
if (length > 5U) {
GETBITS(pDst[i+5], 13)
if (length > 6U) {
GETBITS(pDst[i+6], 13)
}
}
}
}
}
}
}
}
static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
@ -398,6 +723,64 @@ static void tif_14uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T lengt
}
}
}
static void tif_15uto32s(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
{
OPJ_SIZE_T i;
for (i = 0; i < (length & ~(OPJ_SIZE_T)7U); i+=8U) {
OPJ_UINT32 val0 = *pSrc++;
OPJ_UINT32 val1 = *pSrc++;
OPJ_UINT32 val2 = *pSrc++;
OPJ_UINT32 val3 = *pSrc++;
OPJ_UINT32 val4 = *pSrc++;
OPJ_UINT32 val5 = *pSrc++;
OPJ_UINT32 val6 = *pSrc++;
OPJ_UINT32 val7 = *pSrc++;
OPJ_UINT32 val8 = *pSrc++;
OPJ_UINT32 val9 = *pSrc++;
OPJ_UINT32 val10 = *pSrc++;
OPJ_UINT32 val11 = *pSrc++;
OPJ_UINT32 val12 = *pSrc++;
OPJ_UINT32 val13 = *pSrc++;
OPJ_UINT32 val14 = *pSrc++;
pDst[i+0] = (OPJ_INT32)((val0 << 7) | (val1 >> 1));
pDst[i+1] = (OPJ_INT32)(((val1 & 0x1U) << 14) | (val2 << 6) | (val3 >> 2));
pDst[i+2] = (OPJ_INT32)(((val3 & 0x3U) << 13) | (val4 << 5) | (val5 >> 3));
pDst[i+3] = (OPJ_INT32)(((val5 & 0x7U) << 12) | (val6 << 4) | (val7 >> 4));
pDst[i+4] = (OPJ_INT32)(((val7 & 0xFU) << 11) | (val8 << 3) | (val9 >> 5));
pDst[i+5] = (OPJ_INT32)(((val9 & 0x1FU) << 10) | (val10 << 2) | (val11 >> 6));
pDst[i+6] = (OPJ_INT32)(((val11 & 0x3FU) << 9) | (val12 << 1) | (val13 >> 7));
pDst[i+7] = (OPJ_INT32)(((val13 & 0x7FU) << 8) | (val14));
}
if (length & 7U) {
unsigned int val;
int available = 0;
length = length & 7U;
GETBITS(pDst[i+0], 15)
if (length > 1U) {
GETBITS(pDst[i+1], 15)
if (length > 2U) {
GETBITS(pDst[i+2], 15)
if (length > 3U) {
GETBITS(pDst[i+3], 15)
if (length > 4U) {
GETBITS(pDst[i+4], 15)
if (length > 5U) {
GETBITS(pDst[i+5], 15)
if (length > 6U) {
GETBITS(pDst[i+6], 15)
}
}
}
}
}
}
}
}
/* seems that libtiff decodes this to machine endianness */
static void tif_16uto32s(const OPJ_UINT16* pSrc, OPJ_INT32* pDst, OPJ_SIZE_T length)
@ -454,8 +837,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
w= (int)tiWidth;
h= (int)tiHeight;
if((tiBps > 16U) || ((tiBps != 1U) && (tiBps & 1U))) {
fprintf(stderr,"tiftoimage: Bits=%d, Only 1, 2, 4, 6, 8, 10, 12, 14 and 16 bits implemented\n",tiBps);
if(tiBps > 16U) {
fprintf(stderr,"tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n",tiBps);
fprintf(stderr,"\tAborting\n");
TIFFClose(tif);
return NULL;
@ -476,15 +859,36 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
cvtTifTo32s = convert_XXu32s_C1R_LUT[tiBps];
break;
/* others are specific to TIFF */
case 3:
cvtTifTo32s = tif_3uto32s;
break;
case 5:
cvtTifTo32s = tif_5uto32s;
break;
case 7:
cvtTifTo32s = tif_7uto32s;
break;
case 9:
cvtTifTo32s = tif_9uto32s;
break;
case 10:
cvtTifTo32s = tif_10uto32s;
break;
case 11:
cvtTifTo32s = tif_11uto32s;
break;
case 12:
cvtTifTo32s = tif_12uto32s;
break;
case 13:
cvtTifTo32s = tif_13uto32s;
break;
case 14:
cvtTifTo32s = tif_14uto32s;
break;
case 15:
cvtTifTo32s = tif_15uto32s;
break;
case 16:
cvtTifTo32s = (convert_XXx32s_C1R)tif_16uto32s;
break;

View File

@ -137,6 +137,16 @@ opj_compress -i @INPUT_NR_PATH@/pngsuite/ftp1n3p08.png -o @TEMP_PATH@/ftp1n3p08.
# issue 571 Lossless is not lossless on linux x86
opj_compress -i @INPUT_NR_PATH@/issue571.tif -o @TEMP_PATH@/issue571.tif.j2k
# issue 729 Allow to read 3/5/7/9/11/13/15 bpp tif files
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-03.tif -o @TEMP_PATH@/flower-minisblack-03.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-05.tif -o @TEMP_PATH@/flower-minisblack-05.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-07.tif -o @TEMP_PATH@/flower-minisblack-07.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-09.tif -o @TEMP_PATH@/flower-minisblack-09.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-11.tif -o @TEMP_PATH@/flower-minisblack-11.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-13.tif -o @TEMP_PATH@/flower-minisblack-13.tif.jp2
opj_compress -i @INPUT_NR_PATH@/flower-minisblack-15.tif -o @TEMP_PATH@/flower-minisblack-15.tif.jp2
# DECODER TEST SUITE
opj_decompress -i @INPUT_NR_PATH@/Bretagne2.j2k -o @TEMP_PATH@/Bretagne2.j2k.pgx
opj_decompress -i @INPUT_NR_PATH@/_00042.j2k -o @TEMP_PATH@/_00042.j2k.pgx