Tiffile.cpp 13 KB

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