Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/win_dib/Tiffile.cpp

raw
1 #include "StdAfx.h"
2
3 // #define STRICT
4 #include "diblib.h"
5 #include <commdlg.h>
6 #include <stdlib.h> // MAX_ constants
7 #include <windows.h>
8 #include <windowsx.h>
9
10 /*--------------------------------------------------------------------
11 READ TIFF
12 Load the TIFF data from the file into memory. Return
13 a pointer to a valid DIB (or NULL for errors).
14 Uses the TIFFRGBA interface to libtiff.lib to convert
15 most file formats to a useable form. We just keep the 32 bit
16 form of the data to display, rather than optimizing for the
17 display.
18
19 Main entry points:
20
21 int ChkTIFF ( LPCTSTR lpszPath )
22 PVOID ReadTIFF ( LPCTSTR lpszPath )
23
24 RETURN
25 A valid DIB pointer for success; NULL for failure.
26
27 --------------------------------------------------------------------*/
28
29 #include "TiffLib/tiff.h"
30 #include "TiffLib/tiffio.h"
31 #include <assert.h>
32 #include <stdio.h>
33
34 // piggyback some data on top of the RGBA Image
35 struct TIFFDibImage
36 {
37 TIFFRGBAImage tif;
38 int dibinstalled;
39 };
40
41 HANDLE LoadTIFFinDIB(LPCTSTR lpFileName);
42 HANDLE TIFFRGBA2DIB(TIFFDibImage *dib, uint32_t *raster);
43
44 static void MyWarningHandler(const char *module, const char *fmt, va_list ap)
45 {
46 // ignore all warnings (unused tags, etc)
47 return;
48 }
49
50 static void MyErrorHandler(const char *module, const char *fmt, va_list ap)
51 {
52 return;
53 }
54
55 // Turn off the error and warning handlers to check if a valid file.
56 // Necessary because of the way that the Doc loads images and restart files.
57 int ChkTIFF(LPCTSTR lpszPath)
58 {
59 int rtn = 0;
60
61 TIFFErrorHandler eh;
62 TIFFErrorHandler wh;
63
64 eh = TIFFSetErrorHandler(NULL);
65 wh = TIFFSetWarningHandler(NULL);
66
67 TIFF *tif = TIFFOpen(lpszPath, "r");
68 if (tif)
69 {
70 rtn = 1;
71 TIFFClose(tif);
72 }
73
74 TIFFSetErrorHandler(eh);
75 TIFFSetWarningHandler(wh);
76
77 return rtn;
78 }
79
80 void DibInstallHack(TIFFDibImage *img);
81
82 PVOID ReadTIFF(LPCTSTR lpszPath)
83 {
84 void *pDIB = 0;
85 TIFFErrorHandler wh;
86
87 wh = TIFFSetWarningHandler(MyWarningHandler);
88
89 if (ChkTIFF(lpszPath))
90 {
91 TIFF *tif = TIFFOpen(lpszPath, "r");
92 if (tif)
93 {
94 char emsg[1024];
95
96 if (TIFFRGBAImageOK(tif, emsg))
97 {
98 TIFFDibImage img;
99 char emsg[1024];
100
101 if (TIFFRGBAImageBegin(&img.tif, tif, -1, emsg))
102 {
103 size_t npixels;
104 uint32_t *raster;
105
106 DibInstallHack(&img);
107
108 npixels = img.tif.width * img.tif.height;
109 raster =
110 (uint32_t *)_TIFFmalloc(npixels * sizeof(uint32_t));
111 if (raster != NULL)
112 {
113 if (TIFFRGBAImageGet(&img.tif, raster, img.tif.width,
114 img.tif.height))
115 {
116 pDIB = TIFFRGBA2DIB(&img, raster);
117 }
118 }
119 _TIFFfree(raster);
120 }
121 TIFFRGBAImageEnd(&img.tif);
122 }
123 else
124 {
125 TRACE("Unable to open image(%s): %s\n", lpszPath, emsg);
126 }
127 TIFFClose(tif);
128 }
129 }
130
131 TIFFSetWarningHandler(wh);
132
133 return pDIB;
134 }
135
136 HANDLE TIFFRGBA2DIB(TIFFDibImage *dib, uint32_t *raster)
137 {
138 void *pDIB = 0;
139 TIFFRGBAImage *img = &dib->tif;
140
141 uint32_t imageLength;
142 uint32_t imageWidth;
143 uint16_t BitsPerSample;
144 uint16_t SamplePerPixel;
145 uint32_t RowsPerStrip;
146 uint16_t PhotometricInterpretation;
147
148 BITMAPINFOHEADER bi;
149 int dwDIBSize;
150
151 TIFFGetField(img->tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
152 TIFFGetField(img->tif, TIFFTAG_IMAGELENGTH, &imageLength);
153 TIFFGetField(img->tif, TIFFTAG_BITSPERSAMPLE, &BitsPerSample);
154 TIFFGetField(img->tif, TIFFTAG_ROWSPERSTRIP, &RowsPerStrip);
155 TIFFGetField(img->tif, TIFFTAG_SAMPLESPERPIXEL, &SamplePerPixel);
156 TIFFGetField(img->tif, TIFFTAG_PHOTOMETRIC, &PhotometricInterpretation);
157
158 if (BitsPerSample == 1 && SamplePerPixel == 1 && dib->dibinstalled)
159 { // bilevel
160 bi.biSize = sizeof(BITMAPINFOHEADER);
161 bi.biWidth = imageWidth;
162 bi.biHeight = imageLength;
163 bi.biPlanes = 1; // always
164 bi.biBitCount = 1;
165 bi.biCompression = BI_RGB;
166 bi.biSizeImage = WIDTHBYTES(bi.biWidth * bi.biBitCount) * bi.biHeight;
167 bi.biXPelsPerMeter = 0;
168 bi.biYPelsPerMeter = 0;
169 bi.biClrUsed = 0; // must be zero for RGB compression (none)
170 bi.biClrImportant = 0; // always
171
172 // Get the size of the DIB
173 dwDIBSize = GetDIBSize(&bi);
174
175 // Allocate for the BITMAPINFO structure and the color table.
176 pDIB = GlobalAllocPtr(GHND, dwDIBSize);
177 if (pDIB == 0)
178 {
179 return (NULL);
180 }
181
182 // Copy the header info
183 *((BITMAPINFOHEADER *)pDIB) = bi;
184
185 // Get a pointer to the color table
186 RGBQUAD *pRgbq = (RGBQUAD *)((LPSTR)pDIB + sizeof(BITMAPINFOHEADER));
187
188 pRgbq[0].rgbRed = 0;
189 pRgbq[0].rgbBlue = 0;
190 pRgbq[0].rgbGreen = 0;
191 pRgbq[0].rgbReserved = 0;
192 pRgbq[1].rgbRed = 255;
193 pRgbq[1].rgbBlue = 255;
194 pRgbq[1].rgbGreen = 255;
195 pRgbq[1].rgbReserved = 255;
196
197 // Pointers to the bits
198 // PVOID pbiBits = (LPSTR)pRgbq + bi.biClrUsed * sizeof(RGBQUAD);
199 //
200 // In the BITMAPINFOHEADER documentation, it appears that
201 // there should be no color table for 32 bit images, but
202 // experience shows that the image is off by 3 words if it
203 // is not included. So here it is.
204 PVOID pbiBits = GetDIBImagePtr(
205 (BITMAPINFOHEADER *)pDIB); //(LPSTR)pRgbq + 3 * sizeof(RGBQUAD);
206
207 int sizeWords = bi.biSizeImage / 4;
208 RGBQUAD *rgbDib = (RGBQUAD *)pbiBits;
209 long *rgbTif = (long *)raster;
210
211 _TIFFmemcpy(pbiBits, raster, bi.biSizeImage);
212 }
213
214 // For now just always default to the RGB 32 bit form. // save as 32 bit
215 // for simplicity
216 else if (true /*BitsPerSample == 8 && SamplePerPixel == 3*/)
217 { // 24 bit color
218
219 bi.biSize = sizeof(BITMAPINFOHEADER);
220 bi.biWidth = imageWidth;
221 bi.biHeight = imageLength;
222 bi.biPlanes = 1; // always
223 bi.biBitCount = 32;
224 bi.biCompression = BI_RGB;
225 bi.biSizeImage = WIDTHBYTES(bi.biWidth * bi.biBitCount) * bi.biHeight;
226 bi.biXPelsPerMeter = 0;
227 bi.biYPelsPerMeter = 0;
228 bi.biClrUsed = 0; // must be zero for RGB compression (none)
229 bi.biClrImportant = 0; // always
230
231 // Get the size of the DIB
232 dwDIBSize = GetDIBSize(&bi);
233
234 // Allocate for the BITMAPINFO structure and the color table.
235 pDIB = GlobalAllocPtr(GHND, dwDIBSize);
236 if (pDIB == 0)
237 {
238 return (NULL);
239 }
240
241 // Copy the header info
242 *((BITMAPINFOHEADER *)pDIB) = bi;
243
244 // Get a pointer to the color table
245 RGBQUAD *pRgbq = (RGBQUAD *)((LPSTR)pDIB + sizeof(BITMAPINFOHEADER));
246
247 // Pointers to the bits
248 // PVOID pbiBits = (LPSTR)pRgbq + bi.biClrUsed * sizeof(RGBQUAD);
249 //
250 // In the BITMAPINFOHEADER documentation, it appears that
251 // there should be no color table for 32 bit images, but
252 // experience shows that the image is off by 3 words if it
253 // is not included. So here it is.
254 PVOID pbiBits = (LPSTR)pRgbq + 3 * sizeof(RGBQUAD);
255
256 int sizeWords = bi.biSizeImage / 4;
257 RGBQUAD *rgbDib = (RGBQUAD *)pbiBits;
258 long *rgbTif = (long *)raster;
259
260 // Swap the byte order while copying
261 for (int i = 0; i < sizeWords; ++i)
262 {
263 rgbDib[i].rgbRed = TIFFGetR(rgbTif[i]);
264 rgbDib[i].rgbBlue = TIFFGetB(rgbTif[i]);
265 rgbDib[i].rgbGreen = TIFFGetG(rgbTif[i]);
266 rgbDib[i].rgbReserved = 0;
267 }
268 }
269
270 return pDIB;
271 }
272
273 ///////////////////////////////////////////////////////////////
274 //
275 // Hacked from tif_getimage.c in libtiff in v3.5.7
276 //
277 //
278 typedef unsigned char u_char;
279
280 #define DECLAREContigPutFunc(name) \
281 static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
282 uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
283 u_char *pp)
284
285 #define DECLARESepPutFunc(name) \
286 static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \
287 uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \
288 u_char *r, u_char *g, u_char *b, u_char *a)
289
290 DECLAREContigPutFunc(putContig1bitTile);
291 static int getStripContig1Bit(TIFFRGBAImage *img, uint32_t *uraster, uint32_t w,
292 uint32_t h);
293
294 // typdef struct TIFFDibImage {
295 // TIFFRGBAImage tif;
296 // dibinstalled;
297 // } TIFFDibImage ;
298
299 void DibInstallHack(TIFFDibImage *dib)
300 {
301 TIFFRGBAImage *img = &dib->tif;
302 dib->dibinstalled = false;
303 switch (img->photometric)
304 {
305 case PHOTOMETRIC_MINISWHITE:
306 case PHOTOMETRIC_MINISBLACK:
307 switch (img->bitspersample)
308 {
309 case 1:
310 img->put.contig = putContig1bitTile;
311 img->get = getStripContig1Bit;
312 dib->dibinstalled = true;
313 break;
314 }
315 break;
316 }
317 }
318
319 /*
320 * 1-bit packed samples => 1-bit
321 *
322 * Override to just copy the data
323 */
324 DECLAREContigPutFunc(putContig1bitTile)
325 {
326 int samplesperpixel = img->samplesperpixel;
327
328 (void)y;
329 fromskew *= samplesperpixel;
330 int wb = WIDTHBYTES(w);
331 u_char *ucp = (u_char *)cp;
332
333 /* Convert 'w' to bytes from pixels (rounded up) */
334 w = (w + 7) / 8;
335
336 while (h-- > 0)
337 {
338 _TIFFmemcpy(ucp, pp, w);
339 /*
340 for (x = wb; x-- > 0;) {
341 *cp++ = rgbi(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
342 pp += samplesperpixel;
343 }
344 */
345 ucp += (wb + toskew);
346 pp += (w + fromskew);
347 }
348 }
349
350 /*
351 * Hacked from the tif_getimage.c file.
352 */
353 static uint32_t setorientation(TIFFRGBAImage *img, uint32_t h)
354 {
355 TIFF *tif = img->tif;
356 uint32_t y;
357
358 switch (img->orientation)
359 {
360 case ORIENTATION_BOTRIGHT:
361 case ORIENTATION_RIGHTBOT: /* XXX */
362 case ORIENTATION_LEFTBOT: /* XXX */
363 TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
364 img->orientation = ORIENTATION_BOTLEFT;
365 /* fall through... */
366 case ORIENTATION_BOTLEFT:
367 y = 0;
368 break;
369 case ORIENTATION_TOPRIGHT:
370 case ORIENTATION_RIGHTTOP: /* XXX */
371 case ORIENTATION_LEFTTOP: /* XXX */
372 default:
373 TIFFWarning(TIFFFileName(tif), "using top-left orientation");
374 img->orientation = ORIENTATION_TOPLEFT;
375 /* fall through... */
376 case ORIENTATION_TOPLEFT:
377 y = h - 1;
378 break;
379 }
380 return (y);
381 }
382
383 /*
384 * Get a strip-organized image that has
385 * PlanarConfiguration contiguous if SamplesPerPixel > 1
386 * or
387 * SamplesPerPixel == 1
388 *
389 * Hacked from the tif_getimage.c file.
390 *
391 * This is set up to allow us to just copy the data to the raster
392 * for 1-bit bitmaps
393 */
394 static int getStripContig1Bit(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
395 uint32_t h)
396 {
397 TIFF *tif = img->tif;
398 tileContigRoutine put = img->put.contig;
399 uint16_t orientation;
400 uint32_t row, y, nrow, rowstoread;
401 uint32_t pos;
402 u_char *buf;
403 uint32_t rowsperstrip;
404 uint32_t imagewidth = img->width;
405 tsize_t scanline;
406 int32_t fromskew, toskew;
407 tstrip_t strip;
408 tsize_t stripsize;
409 u_char *braster = (u_char *)raster; // byte wide raster
410 uint32_t wb = WIDTHBYTES(w);
411 int ret = 1;
412
413 buf = (u_char *)_TIFFmalloc(TIFFStripSize(tif));
414 if (buf == 0)
415 {
416 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
417 return (0);
418 }
419 y = setorientation(img, h);
420 orientation = img->orientation;
421 toskew = -(int32_t)(orientation == ORIENTATION_TOPLEFT ? wb + wb : wb - wb);
422 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
423 scanline = TIFFScanlineSize(tif);
424 fromskew = (w < imagewidth ? imagewidth - w : 0) / 8;
425 for (row = 0; row < h; row += nrow)
426 {
427 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
428 nrow = (row + rowstoread > h ? h - row : rowstoread);
429 strip = TIFFComputeStrip(tif, row + img->row_offset, 0);
430 stripsize = ((row + img->row_offset) % rowsperstrip + nrow) * scanline;
431 if (TIFFReadEncodedStrip(tif, strip, buf, stripsize) < 0 &&
432 img->stoponerr)
433 {
434 ret = 0;
435 break;
436 }
437
438 pos = ((row + img->row_offset) % rowsperstrip) * scanline;
439 (*put)(img, (uint32_t *)(braster + y * wb), 0, y, w, nrow, fromskew,
440 toskew, buf + pos);
441 y += (orientation == ORIENTATION_TOPLEFT ? -(int32_t)nrow
442 : (int32_t)nrow);
443 }
444 _TIFFfree(buf);
445 return (ret);
446 }