@@ -1,242 +0,0 @@
1−From cadff5fb6e73398de26a92e96d3d7cac893af255 Mon Sep 17 00:00:00 2001
2−From: szukw000 <szukw000@arcor.de>
3−Date: Fri, 9 Dec 2016 08:29:55 +0100
4−Subject: [PATCH] These changes repair bugs of #871 and #872
5−
6−---
7− src/bin/jp2/converttif.c | 107 +++++++++++++++++++++++++++++++----------------
8− 1 file changed, 70 insertions(+), 37 deletions(-)
9−
10−diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c
11−index 143d3be..c690f8b 100644
12−--- a/src/bin/jp2/converttif.c
13−+++ b/src/bin/jp2/converttif.c
14−@@ -553,20 +553,18 @@ static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T len
15−
16− int imagetotif(opj_image_t * image, const char *outfile)
17− {
18−- int width, height;
19−- int bps,adjust, sgnd;
20−- int tiPhoto;
21−+ uint32 width, height, bps, tiPhoto;
22−+ int adjust, sgnd;
23− TIFF *tif;
24− tdata_t buf;
25−- tsize_t strip_size;
26−+ tmsize_t strip_size, rowStride;
27− OPJ_UINT32 i, numcomps;
28−- OPJ_SIZE_T rowStride;
29− OPJ_INT32* buffer32s = NULL;
30− OPJ_INT32 const* planes[4];
31− convert_32s_PXCX cvtPxToCx = NULL;
32− convert_32sXXx_C1R cvt32sToTif = NULL;
33−
34−- bps = (int)image->comps[0].prec;
35−+ bps = (uint32)image->comps[0].prec;
36− planes[0] = image->comps[0].data;
37−
38− numcomps = image->numcomps;
39−@@ -674,13 +672,13 @@ int imagetotif(opj_image_t * image, const char *outfile)
40− break;
41− }
42− sgnd = (int)image->comps[0].sgnd;
43−- adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
44−- width = (int)image->comps[0].w;
45−- height = (int)image->comps[0].h;
46−+ adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0;
47−+ width = (uint32)image->comps[0].w;
48−+ height = (uint32)image->comps[0].h;
49−
50− TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
51− TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
52−- TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
53−+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint32)numcomps);
54− TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
55− TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
56− TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
57−@@ -688,8 +686,8 @@ int imagetotif(opj_image_t * image, const char *outfile)
58− TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
59−
60− strip_size = TIFFStripSize(tif);
61−- rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
62−- if (rowStride != (OPJ_SIZE_T)strip_size) {
63−+ rowStride = (width * numcomps * bps + 7U) / 8U;
64−+ if (rowStride != strip_size) {
65− fprintf(stderr, "Invalid TIFF strip size\n");
66− TIFFClose(tif);
67− return 1;
68−@@ -699,7 +697,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
69− TIFFClose(tif);
70− return 1;
71− }
72−- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32));
73−+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(OPJ_INT32)));
74− if (buffer32s == NULL) {
75− _TIFFfree(buf);
76− TIFFClose(tif);
77−@@ -1211,20 +1209,19 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
78− TIFF *tif;
79− tdata_t buf;
80− tstrip_t strip;
81−- tsize_t strip_size;
82−+ tmsize_t strip_size;
83− int j, currentPlane, numcomps = 0, w, h;
84− OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
85− opj_image_cmptparm_t cmptparm[4]; /* RGBA */
86− opj_image_t *image = NULL;
87− int has_alpha = 0;
88−- unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
89−- unsigned int tiWidth, tiHeight;
90−+ uint32 tiBps, tiPhoto, tiSf, tiSpp, tiPC, tiWidth, tiHeight;
91− OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
92− convert_XXx32s_C1R cvtTifTo32s = NULL;
93− convert_32s_CXPX cvtCxToPx = NULL;
94− OPJ_INT32* buffer32s = NULL;
95− OPJ_INT32* planes[4];
96−- OPJ_SIZE_T rowStride;
97−+ tmsize_t rowStride;
98−
99− tif = TIFFOpen(filename, "r");
100−
101−@@ -1243,22 +1240,35 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
102− TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp);
103− TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto);
104− TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC);
105−- w= (int)tiWidth;
106−- h= (int)tiHeight;
107−-
108−- if(tiBps > 16U) {
109−- fprintf(stderr,"tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n",tiBps);
110−- fprintf(stderr,"\tAborting\n");
111−+
112−+ if(tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */
113−+ fprintf(stderr,"tiftoimage: Bad value for samples per pixel == %hu.\n"
114−+ "\tAborting.\n", tiSpp);
115−+ TIFFClose(tif);
116−+ return NULL;
117−+ }
118−+ if(tiBps > 16U || tiBps == 0) {
119−+ fprintf(stderr,"tiftoimage: Bad values for Bits == %d.\n"
120−+ "\tMax. 16 Bits are allowed here.\n\tAborting.\n",tiBps);
121− TIFFClose(tif);
122− return NULL;
123− }
124− if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
125−- fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
126−+ fprintf(stderr,"tiftoimage: Bad color format %d.\n"
127−+ "\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto);
128− fprintf(stderr,"\tAborting\n");
129− TIFFClose(tif);
130− return NULL;
131− }
132−-
133−+ if(tiWidth == 0 || tiHeight == 0) {
134−+ fprintf(stderr,"tiftoimage: Bad values for width(%u) "
135−+ "and/or height(%u)\n\tAborting.\n",tiWidth,tiHeight);
136−+ TIFFClose(tif);
137−+ return NULL;
138−+ }
139−+ w= (int)tiWidth;
140−+ h= (int)tiHeight;
141−+
142− switch (tiBps) {
143− case 1:
144− case 2:
145−@@ -1312,7 +1322,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
146−
147− TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
148− &extrasamples, &sampleinfo);
149−-
150−+
151− if(extrasamples >= 1)
152− {
153− switch(sampleinfo[0])
154−@@ -1333,7 +1343,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
155− else /* extrasamples == 0 */
156− if(tiSpp == 4 || tiSpp == 2) has_alpha = 1;
157− }
158−-
159−+
160− /* initialize image components */
161− memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
162−
163−@@ -1346,7 +1356,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
164− } else {
165− is_cinema = 0U;
166− }
167−-
168−+
169− if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */
170− {
171− numcomps = 3 + has_alpha;
172−@@ -1384,10 +1394,24 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
173− image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
174− image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
175− image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
176−- image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
177−+ image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
178−+ if(image->x1 <= image->x0) {
179−+ fprintf(stderr,"tiftoimage: Bad value for image->x1(%d) vs. "
180−+ "image->x0(%d)\n\tAborting.\n",image->x1,image->x0);
181−+ TIFFClose(tif);
182−+ opj_image_destroy(image);
183−+ return NULL;
184−+ }
185− image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
186−- image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
187−-
188−+ image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
189−+ if(image->y1 <= image->y0) {
190−+ fprintf(stderr,"tiftoimage: Bad value for image->y1(%d) vs. "
191−+ "image->y0(%d)\n\tAborting.\n",image->y1,image->y0);
192−+ TIFFClose(tif);
193−+ opj_image_destroy(image);
194−+ return NULL;
195−+ }
196−+
197− for(j = 0; j < numcomps; j++)
198− {
199− planes[j] = image->comps[j].data;
200−@@ -1395,15 +1419,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
201− image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
202−
203− strip_size = TIFFStripSize(tif);
204−-
205−+
206− buf = _TIFFmalloc(strip_size);
207− if (buf == NULL) {
208− TIFFClose(tif);
209− opj_image_destroy(image);
210− return NULL;
211− }
212−- rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
213−- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
214−+ rowStride = (w * tiSpp * tiBps + 7U) / 8U;
215−+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(w * tiSpp * sizeof(OPJ_INT32)));
216− if (buffer32s == NULL) {
217− _TIFFfree(buf);
218− TIFFClose(tif);
219−@@ -1421,11 +1445,20 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
220− for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++)
221− {
222− const OPJ_UINT8 *dat8;
223−- OPJ_SIZE_T ssize;
224−+ tmsize_t ssize;
225−
226−- ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
227−+ ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
228−+ if(ssize < 1 || ssize > strip_size) {
229−+ fprintf(stderr,"tiftoimage: Bad value for ssize(%ld) "
230−+ "vs. strip_size(%ld).\n\tAborting.\n",ssize,strip_size);
231−+ _TIFFfree(buf);
232−+ _TIFFfree(buffer32s);
233−+ TIFFClose(tif);
234−+ opj_image_destroy(image);
235−+ return NULL;
236−+ }
237− dat8 = (const OPJ_UINT8*)buf;
238−-
239−+
240− while (ssize >= rowStride) {
241− cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp);
242− cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w);