These changes repair bugs of #871 and #872

This commit is contained in:
szukw000 2016-12-09 08:29:55 +01:00
parent 7113c4e3bb
commit cadff5fb6e
1 changed files with 70 additions and 37 deletions

View File

@ -553,20 +553,18 @@ static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T len
int imagetotif(opj_image_t * image, const char *outfile) int imagetotif(opj_image_t * image, const char *outfile)
{ {
int width, height; uint32 width, height, bps, tiPhoto;
int bps,adjust, sgnd; int adjust, sgnd;
int tiPhoto;
TIFF *tif; TIFF *tif;
tdata_t buf; tdata_t buf;
tsize_t strip_size; tmsize_t strip_size, rowStride;
OPJ_UINT32 i, numcomps; OPJ_UINT32 i, numcomps;
OPJ_SIZE_T rowStride;
OPJ_INT32* buffer32s = NULL; OPJ_INT32* buffer32s = NULL;
OPJ_INT32 const* planes[4]; OPJ_INT32 const* planes[4];
convert_32s_PXCX cvtPxToCx = NULL; convert_32s_PXCX cvtPxToCx = NULL;
convert_32sXXx_C1R cvt32sToTif = NULL; convert_32sXXx_C1R cvt32sToTif = NULL;
bps = (int)image->comps[0].prec; bps = (uint32)image->comps[0].prec;
planes[0] = image->comps[0].data; planes[0] = image->comps[0].data;
numcomps = image->numcomps; numcomps = image->numcomps;
@ -674,13 +672,13 @@ int imagetotif(opj_image_t * image, const char *outfile)
break; break;
} }
sgnd = (int)image->comps[0].sgnd; sgnd = (int)image->comps[0].sgnd;
adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0;
width = (int)image->comps[0].w; width = (uint32)image->comps[0].w;
height = (int)image->comps[0].h; height = (uint32)image->comps[0].h;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint32)numcomps);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
@ -688,8 +686,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
strip_size = TIFFStripSize(tif); strip_size = TIFFStripSize(tif);
rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U; rowStride = (width * numcomps * bps + 7U) / 8U;
if (rowStride != (OPJ_SIZE_T)strip_size) { if (rowStride != strip_size) {
fprintf(stderr, "Invalid TIFF strip size\n"); fprintf(stderr, "Invalid TIFF strip size\n");
TIFFClose(tif); TIFFClose(tif);
return 1; return 1;
@ -699,7 +697,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
TIFFClose(tif); TIFFClose(tif);
return 1; return 1;
} }
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32)); buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(OPJ_INT32)));
if (buffer32s == NULL) { if (buffer32s == NULL) {
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
@ -1211,20 +1209,19 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
TIFF *tif; TIFF *tif;
tdata_t buf; tdata_t buf;
tstrip_t strip; tstrip_t strip;
tsize_t strip_size; tmsize_t strip_size;
int j, currentPlane, numcomps = 0, w, h; int j, currentPlane, numcomps = 0, w, h;
OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN; OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
opj_image_cmptparm_t cmptparm[4]; /* RGBA */ opj_image_cmptparm_t cmptparm[4]; /* RGBA */
opj_image_t *image = NULL; opj_image_t *image = NULL;
int has_alpha = 0; int has_alpha = 0;
unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; uint32 tiBps, tiPhoto, tiSf, tiSpp, tiPC, tiWidth, tiHeight;
unsigned int tiWidth, tiHeight;
OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz); OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
convert_XXx32s_C1R cvtTifTo32s = NULL; convert_XXx32s_C1R cvtTifTo32s = NULL;
convert_32s_CXPX cvtCxToPx = NULL; convert_32s_CXPX cvtCxToPx = NULL;
OPJ_INT32* buffer32s = NULL; OPJ_INT32* buffer32s = NULL;
OPJ_INT32* planes[4]; OPJ_INT32* planes[4];
OPJ_SIZE_T rowStride; tmsize_t rowStride;
tif = TIFFOpen(filename, "r"); tif = TIFFOpen(filename, "r");
@ -1243,21 +1240,34 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
w= (int)tiWidth;
h= (int)tiHeight;
if(tiBps > 16U) { if(tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */
fprintf(stderr,"tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n",tiBps); fprintf(stderr,"tiftoimage: Bad value for samples per pixel == %hu.\n"
fprintf(stderr,"\tAborting\n"); "\tAborting.\n", tiSpp);
TIFFClose(tif);
return NULL;
}
if(tiBps > 16U || tiBps == 0) {
fprintf(stderr,"tiftoimage: Bad values for Bits == %d.\n"
"\tMax. 16 Bits are allowed here.\n\tAborting.\n",tiBps);
TIFFClose(tif); TIFFClose(tif);
return NULL; return NULL;
} }
if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) { if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto); fprintf(stderr,"tiftoimage: Bad color format %d.\n"
"\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
fprintf(stderr,"\tAborting\n"); fprintf(stderr,"\tAborting\n");
TIFFClose(tif); TIFFClose(tif);
return NULL; return NULL;
} }
if(tiWidth == 0 || tiHeight == 0) {
fprintf(stderr,"tiftoimage: Bad values for width(%u) "
"and/or height(%u)\n\tAborting.\n",tiWidth,tiHeight);
TIFFClose(tif);
return NULL;
}
w= (int)tiWidth;
h= (int)tiHeight;
switch (tiBps) { switch (tiBps) {
case 1: case 1:
@ -1384,9 +1394,23 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
image->x0 = (OPJ_UINT32)parameters->image_offset_x0; image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
image->y0 = (OPJ_UINT32)parameters->image_offset_y0; image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
if(image->x1 <= image->x0) {
fprintf(stderr,"tiftoimage: Bad value for image->x1(%d) vs. "
"image->x0(%d)\n\tAborting.\n",image->x1,image->x0);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
if(image->y1 <= image->y0) {
fprintf(stderr,"tiftoimage: Bad value for image->y1(%d) vs. "
"image->y0(%d)\n\tAborting.\n",image->y1,image->y0);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
for(j = 0; j < numcomps; j++) for(j = 0; j < numcomps; j++)
{ {
@ -1402,8 +1426,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
opj_image_destroy(image); opj_image_destroy(image);
return NULL; return NULL;
} }
rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U; rowStride = (w * tiSpp * tiBps + 7U) / 8U;
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32)); buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(w * tiSpp * sizeof(OPJ_INT32)));
if (buffer32s == NULL) { if (buffer32s == NULL) {
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
@ -1421,9 +1445,18 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
{ {
const OPJ_UINT8 *dat8; const OPJ_UINT8 *dat8;
OPJ_SIZE_T ssize; tmsize_t ssize;
ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size); ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
if(ssize < 1 || ssize > strip_size) {
fprintf(stderr,"tiftoimage: Bad value for ssize(%ld) "
"vs. strip_size(%ld).\n\tAborting.\n",ssize,strip_size);
_TIFFfree(buf);
_TIFFfree(buffer32s);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
dat8 = (const OPJ_UINT8*)buf; dat8 = (const OPJ_UINT8*)buf;
while (ssize >= rowStride) { while (ssize >= rowStride) {