100 #include "config_auto.h" 104 #include <sys/types.h> 111 #include "allheaders.h" 120 static const l_int32 DEFAULT_RESOLUTION = 300;
121 static const l_int32 MANY_PAGES_IN_TIFF_FILE = 3000;
129 l_int32 *pheight, l_int32 *pbps,
130 l_int32 *pspp, l_int32 *pres,
131 l_int32 *pcmap, l_int32 *pformat);
138 static TIFF *
fopenTiff(FILE *fp,
const char *modestring);
139 static TIFF *
openTiff(
const char *filename,
const char *modestring);
146 l_uint8 **pdata,
size_t *pdatasize);
149 static void dummyHandler(
const char *module,
const char *fmt, va_list ap) {};
182 static struct tiff_transform tiff_partial_orientation_transforms[] = {
210 lept_read_proc(thandle_t cookie,
214 FILE* fp = (FILE *)cookie;
216 if (!buff || !cookie || !fp)
218 done = fread(buff, 1, size, fp);
223 lept_write_proc(thandle_t cookie,
227 FILE* fp = (FILE *)cookie;
229 if (!buff || !cookie || !fp)
231 done = fwrite(buff, 1, size, fp);
236 lept_seek_proc(thandle_t cookie,
240 FILE* fp = (FILE *)cookie;
241 #if defined(_MSC_VER) 253 _fseeki64(fp, 0, SEEK_END);
257 pos = (__int64)(pos + offs);
258 _fseeki64(fp, pos, SEEK_SET);
259 if (pos == _ftelli64(fp))
261 #elif defined(_LARGEFILE_SOURCE) 273 fseeko(fp, 0, SEEK_END);
277 pos = (off64_t)(pos + offs);
278 fseeko(fp, pos, SEEK_SET);
279 if (pos == ftello(fp))
293 fseek(fp, 0, SEEK_END);
297 pos = (off_t)(pos + offs);
298 fseek(fp, pos, SEEK_SET);
299 if (pos == ftell(fp))
306 lept_close_proc(thandle_t cookie)
308 FILE* fp = (FILE *)cookie;
311 fseek(fp, 0, SEEK_SET);
316 lept_size_proc(thandle_t cookie)
318 FILE* fp = (FILE *)cookie;
319 #if defined(_MSC_VER) 325 _fseeki64(fp, 0, SEEK_END);
326 size = _ftelli64(fp);
327 _fseeki64(fp, pos, SEEK_SET);
328 #elif defined(_LARGEFILE_SOURCE) 334 fseeko(fp, 0, SEEK_END);
336 fseeko(fp, pos, SEEK_SET);
343 fseek(fp, 0, SEEK_END);
345 fseek(fp, pos, SEEK_SET);
377 PROCNAME(
"pixReadTiff");
380 return (
PIX *)ERROR_PTR(
"filename not defined", procName, NULL);
383 return (
PIX *)ERROR_PTR(
"image file not found", procName, NULL);
414 PROCNAME(
"pixReadStreamTiff");
417 return (
PIX *)ERROR_PTR(
"stream not defined", procName, NULL);
420 return (
PIX *)ERROR_PTR(
"tif not opened", procName, NULL);
422 if (TIFFSetDirectory(tif, n) == 0) {
471 l_uint8 *linebuf, *data;
472 l_uint16 spp, bps, bpp, photometry, tiffcomp, orientation;
473 l_uint16 *redmap, *greenmap, *bluemap;
474 l_int32 d, wpl, bpl, comptype, i, j, ncolors, rval, gval, bval;
476 l_uint32 w, h, tiffbpl, tiffword;
477 l_uint32 *line, *ppixel, *tiffdata;
478 l_uint32 read_oriented;
482 PROCNAME(
"pixReadFromTiffStream");
485 return (
PIX *)ERROR_PTR(
"tif not defined", procName, NULL);
490 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bps);
491 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
494 L_WARNING(
"bpp = %d; stripping 16 bit rgb samples down to 8\n",
498 else if (spp == 3 || spp == 4)
501 return (
PIX *)ERROR_PTR(
"spp not in set {1,3,4}", procName, NULL);
503 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
504 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
505 tiffbpl = TIFFScanlineSize(tif);
508 return (
PIX *)ERROR_PTR(
"pix not made", procName, NULL);
509 pixSetInputFormat(pix, IFF_TIFF);
511 wpl = pixGetWpl(pix);
516 linebuf = (l_uint8 *)LEPT_CALLOC(tiffbpl + 1,
sizeof(l_uint8));
517 for (i = 0 ; i < h ; i++) {
518 if (TIFFReadScanline(tif, linebuf, i, 0) < 0) {
521 return (
PIX *)ERROR_PTR(
"line read fail", procName, NULL);
523 memcpy((
char *)data, (
char *)linebuf, tiffbpl);
533 if ((tiffdata = (l_uint32 *)LEPT_CALLOC(w * h,
sizeof(l_uint32)))
536 return (
PIX *)ERROR_PTR(
"calloc fail for tiffdata", procName, NULL);
539 if (!TIFFReadRGBAImageOriented(tif, w, h, (uint32 *)tiffdata,
540 ORIENTATION_TOPLEFT, 0)) {
543 return (
PIX *)ERROR_PTR(
"failed to read tiffdata", procName, NULL);
549 for (i = 0 ; i < h ; i++, line += wpl) {
550 for (j = 0, ppixel = line; j < w; j++) {
552 tiffword = tiffdata[i * w + j];
553 rval = TIFFGetR(tiffword);
554 gval = TIFFGetG(tiffword);
555 bval = TIFFGetB(tiffword);
564 pixSetXRes(pix, xres);
565 pixSetYRes(pix, yres);
569 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &tiffcomp);
571 pixSetInputFormat(pix, comptype);
573 if (TIFFGetField(tif, TIFFTAG_COLORMAP, &redmap, &greenmap, &bluemap)) {
580 return (
PIX *)ERROR_PTR(
"invalid bps; > 8", procName, NULL);
584 return (
PIX *)ERROR_PTR(
"cmap not made", procName, NULL);
587 for (i = 0; i < ncolors; i++)
592 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometry)) {
595 if (tiffcomp == COMPRESSION_CCITTFAX3 ||
596 tiffcomp == COMPRESSION_CCITTFAX4 ||
597 tiffcomp == COMPRESSION_CCITTRLE ||
598 tiffcomp == COMPRESSION_CCITTRLEW) {
599 photometry = PHOTOMETRIC_MINISWHITE;
601 photometry = PHOTOMETRIC_MINISBLACK;
604 if ((d == 1 && photometry == PHOTOMETRIC_MINISBLACK) ||
605 (d == 8 && photometry == PHOTOMETRIC_MINISWHITE))
609 if (TIFFGetField(tif, TIFFTAG_ORIENTATION, &orientation)) {
610 if (orientation >= 1 && orientation <= 8) {
612 &tiff_partial_orientation_transforms[orientation - 1] :
613 &tiff_orientation_transforms[orientation - 1];
614 if (transform->vflip)
pixFlipTB(pix, pix);
615 if (transform->hflip)
pixFlipLR(pix, pix);
616 if (transform->rotate) {
660 NULL, NULL, NULL, NULL);
723 PROCNAME(
"pixWriteTiffCustom");
726 return ERROR_INT(
"filename not defined", procName, 1);
728 return ERROR_INT(
"pix not defined", procName, 1);
730 if ((tif =
openTiff(filename, modestr)) == NULL)
731 return ERROR_INT(
"tif not opened", procName, 1);
797 PROCNAME(
"pixWriteStreamTiffWA");
800 return ERROR_INT(
"stream not defined", procName, 1 );
802 return ERROR_INT(
"pix not defined", procName, 1 );
803 if (strcmp(modestr,
"w") && strcmp(modestr,
"a"))
804 return ERROR_INT(
"modestr not 'w' or 'a'", procName, 1 );
806 if (pixGetDepth(pix) != 1 && comptype != IFF_TIFF &&
807 comptype != IFF_TIFF_LZW && comptype != IFF_TIFF_ZIP) {
808 L_WARNING(
"invalid compression type for bpp > 1\n", procName);
809 comptype = IFF_TIFF_ZIP;
812 if ((tif =
fopenTiff(fp, modestr)) == NULL)
813 return ERROR_INT(
"tif not opened", procName, 1);
817 return ERROR_INT(
"tif write error", procName, 1);
868 l_uint8 *linebuf, *data;
869 l_uint16 redmap[256], greenmap[256], bluemap[256];
870 l_int32 w, h, d, i, j, k, wpl, bpl, tiffbpl, ncolors, cmapsize;
871 l_int32 *rmap, *gmap, *bmap;
873 l_uint32 *line, *ppixel;
878 PROCNAME(
"pixWriteToTiffStream");
881 return ERROR_INT(
"tif stream not defined", procName, 1);
883 return ERROR_INT(
"pix not defined", procName, 1 );
887 xres = pixGetXRes(pix);
888 yres = pixGetYRes(pix);
889 if (xres == 0) xres = DEFAULT_RESOLUTION;
890 if (yres == 0) yres = DEFAULT_RESOLUTION;
893 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (l_uint32)RESUNIT_INCH);
894 TIFFSetField(tif, TIFFTAG_XRESOLUTION, (l_float64)xres);
895 TIFFSetField(tif, TIFFTAG_YRESOLUTION, (l_float64)yres);
897 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (l_uint32)w);
898 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (l_uint32)h);
899 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
902 TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, text);
905 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
906 else if (d == 32 || d == 24) {
907 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
908 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE,
909 (l_uint16)8, (l_uint16)8, (l_uint16)8);
910 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (l_uint16)3);
911 }
else if ((cmap = pixGetColormap(pix)) == NULL) {
912 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
916 ncolors = L_MIN(256, ncolors);
918 cmapsize = L_MIN(256, cmapsize);
919 if (ncolors > cmapsize) {
920 L_WARNING(
"too many colors in cmap for tiff; truncating\n",
924 for (i = 0; i < ncolors; i++) {
925 redmap[i] = (rmap[i] << 8) | rmap[i];
926 greenmap[i] = (gmap[i] << 8) | gmap[i];
927 bluemap[i] = (bmap[i] << 8) | bmap[i];
929 for (i = ncolors; i < cmapsize; i++)
930 redmap[i] = greenmap[i] = bluemap[i] = 0;
935 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
936 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (l_uint16)1);
937 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (l_uint16)d);
938 TIFFSetField(tif, TIFFTAG_COLORMAP, redmap, greenmap, bluemap);
941 if (d != 24 && d != 32) {
942 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (l_uint16)d);
943 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (l_uint16)1);
946 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
947 if (comptype == IFF_TIFF) {
948 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
949 }
else if (comptype == IFF_TIFF_G4) {
950 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
951 }
else if (comptype == IFF_TIFF_G3) {
952 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3);
953 }
else if (comptype == IFF_TIFF_RLE) {
954 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTRLE);
955 }
else if (comptype == IFF_TIFF_PACKBITS) {
956 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
957 }
else if (comptype == IFF_TIFF_LZW) {
958 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
959 }
else if (comptype == IFF_TIFF_ZIP) {
960 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
962 L_WARNING(
"unknown tiff compression; using none\n", procName);
963 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
970 tiffbpl = TIFFScanlineSize(tif);
971 wpl = pixGetWpl(pix);
974 fprintf(stderr,
"Big trouble: tiffbpl = %d, bpl = %d\n", tiffbpl, bpl);
975 if ((linebuf = (l_uint8 *)LEPT_CALLOC(1, bpl)) == NULL)
976 return ERROR_INT(
"calloc fail for linebuf", procName, 1);
979 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, h);
981 if (d != 24 && d != 32) {
987 for (i = 0; i < h; i++, data += bpl) {
988 memcpy((
char *)linebuf, (
char *)data, tiffbpl);
989 if (TIFFWriteScanline(tif, linebuf, i, 0) < 0)
993 }
else if (d == 24) {
994 for (i = 0; i < h; i++) {
996 if (TIFFWriteScanline(tif, (l_uint8 *)line, i, 0) < 0)
1000 for (i = 0; i < h; i++) {
1002 for (j = 0, k = 0, ppixel = line; j < w; j++) {
1008 if (TIFFWriteScanline(tif, linebuf, i, 0) < 0)
1057 l_int32 i, n, ns, size, tagval, val;
1059 l_uint32 uval, uval2;
1061 PROCNAME(
"writeCustomTiffTags");
1064 return ERROR_INT(
"tif stream not defined", procName, 1);
1065 if (!natags && !savals && !satypes)
1067 if (!natags || !savals || !satypes)
1068 return ERROR_INT(
"not all arrays defined", procName, 1);
1071 return ERROR_INT(
"not all sa the same size", procName, 1);
1077 return ERROR_INT(
"too many 4-arg tag calls", procName, 1);
1078 for (i = 0; i < ns; i++) {
1083 if (strcmp(type,
"char*") && strcmp(type,
"l_uint8*"))
1084 L_WARNING(
"array type not char* or l_uint8*; ignore\n",
1086 TIFFSetField(tif, tagval, size, sval);
1093 for (i = ns; i < n; i++) {
1097 if (!strcmp(type,
"char*")) {
1098 TIFFSetField(tif, tagval, sval);
1099 }
else if (!strcmp(type,
"l_uint16")) {
1100 if (sscanf(sval,
"%u", &uval) == 1) {
1101 TIFFSetField(tif, tagval, (l_uint16)uval);
1103 fprintf(stderr,
"val %s not of type %s\n", sval, type);
1104 return ERROR_INT(
"custom tag(s) not written", procName, 1);
1106 }
else if (!strcmp(type,
"l_uint32")) {
1107 if (sscanf(sval,
"%u", &uval) == 1) {
1108 TIFFSetField(tif, tagval, uval);
1110 fprintf(stderr,
"val %s not of type %s\n", sval, type);
1111 return ERROR_INT(
"custom tag(s) not written", procName, 1);
1113 }
else if (!strcmp(type,
"l_int32")) {
1114 if (sscanf(sval,
"%d", &val) == 1) {
1115 TIFFSetField(tif, tagval, val);
1117 fprintf(stderr,
"val %s not of type %s\n", sval, type);
1118 return ERROR_INT(
"custom tag(s) not written", procName, 1);
1120 }
else if (!strcmp(type,
"l_float64")) {
1121 if (sscanf(sval,
"%lf", &dval) == 1) {
1122 TIFFSetField(tif, tagval, dval);
1124 fprintf(stderr,
"val %s not of type %s\n", sval, type);
1125 return ERROR_INT(
"custom tag(s) not written", procName, 1);
1127 }
else if (!strcmp(type,
"l_uint16-l_uint16")) {
1128 if (sscanf(sval,
"%u-%u", &uval, &uval2) == 2) {
1129 TIFFSetField(tif, tagval, (l_uint16)uval, (l_uint16)uval2);
1131 fprintf(stderr,
"val %s not of type %s\n", sval, type);
1132 return ERROR_INT(
"custom tag(s) not written", procName, 1);
1135 return ERROR_INT(
"unknown type; tag(s) not written", procName, 1);
1185 PROCNAME(
"pixReadFromMultipageTiff");
1188 return (
PIX *)ERROR_PTR(
"fname not defined", procName, NULL);
1190 return (
PIX *)ERROR_PTR(
"&offset not defined", procName, NULL);
1192 if ((tif =
openTiff(fname,
"r")) == NULL) {
1193 L_ERROR(
"tif open failed for %s\n", procName, fname);
1199 retval = (offset == 0) ? TIFFSetDirectory(tif, 0)
1200 : TIFFSetSubDirectory(tif, offset);
1212 TIFFReadDirectory(tif);
1213 *poffset = TIFFCurrentDirOffset(tif);
1234 PROCNAME(
"pixaReadMultipageTiff");
1237 return (
PIXA *)ERROR_PTR(
"filename not defined", procName, NULL);
1240 return (
PIXA *)ERROR_PTR(
"stream not opened", procName, NULL);
1243 L_INFO(
" Tiff: %d pages\n", procName, npages);
1245 return (
PIXA *)ERROR_PTR(
"file not tiff", procName, NULL);
1249 return (
PIXA *)ERROR_PTR(
"tif not opened", procName, NULL);
1253 for (i = 0; i < npages; i++) {
1257 L_WARNING(
"pix not read for page %d\n", procName, i);
1261 if (TIFFReadDirectory(tif) == 0)
1289 const char *modestr;
1293 PROCNAME(
"pixaWriteMultipageTiff");
1296 return ERROR_INT(
"fname not defined", procName, 1);
1298 return ERROR_INT(
"pixa not defined", procName, 1);
1301 for (i = 0; i < n; i++) {
1302 modestr = (i == 0) ?
"w" :
"a";
1304 if (pixGetDepth(pix1) == 1) {
1307 if (pixGetColormap(pix1)) {
1349 const char *fileout)
1353 PROCNAME(
"writeMultipageTiff");
1356 return ERROR_INT(
"dirin not defined", procName, 1);
1358 return ERROR_INT(
"fileout not defined", procName, 1);
1384 const char *fileout)
1388 l_int32 i, nfiles, firstfile, format;
1391 PROCNAME(
"writeMultipageTiffSA");
1394 return ERROR_INT(
"sa not defined", procName, 1);
1396 return ERROR_INT(
"fileout not defined", procName, 1);
1400 for (i = 0; i < nfiles; i++) {
1401 op = (firstfile) ?
"w" :
"a";
1404 if (format == IFF_UNKNOWN) {
1405 L_INFO(
"format of %s not known\n", procName, fname);
1409 if ((pix =
pixRead(fname)) == NULL) {
1410 L_WARNING(
"pix not made for file: %s\n", procName, fname);
1413 if (pixGetDepth(pix) == 1) {
1416 if (pixGetColormap(pix)) {
1444 const char *tiffile)
1448 PROCNAME(
"fprintTiffInfo");
1451 return ERROR_INT(
"tiffile not defined", procName, 1);
1453 return ERROR_INT(
"stream out not defined", procName, 1);
1455 if ((tif =
openTiff(tiffile,
"rb")) == NULL)
1456 return ERROR_INT(
"tif not open for read", procName, 1);
1458 TIFFPrintDirectory(tif, fpout, 0);
1482 PROCNAME(
"tiffGetCount");
1485 return ERROR_INT(
"stream not defined", procName, 1);
1487 return ERROR_INT(
"&n not defined", procName, 1);
1491 return ERROR_INT(
"tif not open for read", procName, 1);
1493 for (i = 1; ; i++) {
1494 if (TIFFReadDirectory(tif) == 0)
1496 if (i == MANY_PAGES_IN_TIFF_FILE + 1) {
1497 L_WARNING(
"big file: more than %d pages\n", procName,
1498 MANY_PAGES_IN_TIFF_FILE);
1530 PROCNAME(
"getTiffResolution");
1532 if (!pxres || !pyres)
1533 return ERROR_INT(
"&xres and &yres not both defined", procName, 1);
1534 *pxres = *pyres = 0;
1536 return ERROR_INT(
"stream not opened", procName, 1);
1539 return ERROR_INT(
"tif not open for read", procName, 1);
1565 l_int32 foundxres, foundyres;
1566 l_float32 fxres, fyres;
1568 PROCNAME(
"getTiffStreamResolution");
1571 return ERROR_INT(
"tif not opened", procName, 1);
1572 if (!pxres || !pyres)
1573 return ERROR_INT(
"&xres and &yres not both defined", procName, 1);
1574 *pxres = *pyres = 0;
1576 TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
1577 foundxres = TIFFGetField(tif, TIFFTAG_XRESOLUTION, &fxres);
1578 foundyres = TIFFGetField(tif, TIFFTAG_YRESOLUTION, &fyres);
1579 if (!foundxres && !foundyres)
return 1;
1580 if (!foundxres && foundyres)
1582 else if (foundxres && !foundyres)
1585 if (resunit == RESUNIT_CENTIMETER) {
1586 *pxres = (l_int32)(2.54 * fxres + 0.5);
1587 *pyres = (l_int32)(2.54 * fyres + 0.5);
1589 *pxres = (l_int32)fxres;
1590 *pyres = (l_int32)fyres;
1634 PROCNAME(
"readHeaderTiff");
1637 return ERROR_INT(
"filename not defined", procName, 1);
1638 if (!pwidth || !pheight || !pbps || !pspp)
1639 return ERROR_INT(
"input ptr(s) not all defined", procName, 1);
1640 *pwidth = *pheight = *pbps = *pspp = 0;
1641 if (pres) *pres = 0;
1642 if (pcmap) *pcmap = 0;
1645 return ERROR_INT(
"image file not found", procName, 1);
1647 pres, pcmap, pformat);
1684 l_int32 i, ret, format;
1687 PROCNAME(
"freadHeaderTiff");
1690 return ERROR_INT(
"stream not defined", procName, 1);
1692 return ERROR_INT(
"image index must be >= 0", procName, 1);
1693 if (!pwidth || !pheight || !pbps || !pspp)
1694 return ERROR_INT(
"input ptr(s) not all defined", procName, 1);
1695 *pwidth = *pheight = *pbps = *pspp = 0;
1696 if (pres) *pres = 0;
1697 if (pcmap) *pcmap = 0;
1698 if (pformat) *pformat = 0;
1701 if (format != IFF_TIFF &&
1702 format != IFF_TIFF_G3 && format != IFF_TIFF_G4 &&
1703 format != IFF_TIFF_RLE && format != IFF_TIFF_PACKBITS &&
1704 format != IFF_TIFF_LZW && format != IFF_TIFF_ZIP)
1705 return ERROR_INT(
"file not tiff format", procName, 1);
1708 return ERROR_INT(
"tif not open for read", procName, 1);
1710 for (i = 0; i < n; i++) {
1711 if (TIFFReadDirectory(tif) == 0)
1712 return ERROR_INT(
"image n not found in file", procName, 1);
1716 pres, pcmap, pformat);
1758 PROCNAME(
"readHeaderMemTiff");
1761 return ERROR_INT(
"cdata not defined", procName, 1);
1762 if (!pwidth || !pheight || !pbps || !pspp)
1763 return ERROR_INT(
"input ptr(s) not all defined", procName, 1);
1764 *pwidth = *pheight = *pbps = *pspp = 0;
1765 if (pres) *pres = 0;
1766 if (pcmap) *pcmap = 0;
1767 if (pformat) *pformat = 0;
1770 data = (l_uint8 *)cdata;
1772 return ERROR_INT(
"tiff stream not opened", procName, 1);
1774 for (i = 0; i < n; i++) {
1775 if (TIFFReadDirectory(tif) == 0) {
1777 return ERROR_INT(
"image n not found in file", procName, 1);
1782 pres, pcmap, pformat);
1813 l_uint16 *rmap, *gmap, *bmap;
1817 PROCNAME(
"tiffReadHeaderTiff");
1820 return ERROR_INT(
"tif not opened", procName, 1);
1822 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
1824 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
1826 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bps);
1828 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
1834 *pres = (l_int32)xres;
1839 if (TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap))
1844 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &tiffcomp);
1877 PROCNAME(
"findTiffCompression");
1880 return ERROR_INT(
"&comptype not defined", procName, 1);
1881 *pcomptype = IFF_UNKNOWN;
1883 return ERROR_INT(
"stream not defined", procName, 1);
1886 return ERROR_INT(
"tif not opened", procName, 1);
1887 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &tiffcomp);
1915 case COMPRESSION_CCITTFAX4:
1916 comptype = IFF_TIFF_G4;
1918 case COMPRESSION_CCITTFAX3:
1919 comptype = IFF_TIFF_G3;
1921 case COMPRESSION_CCITTRLE:
1922 comptype = IFF_TIFF_RLE;
1924 case COMPRESSION_PACKBITS:
1925 comptype = IFF_TIFF_PACKBITS;
1927 case COMPRESSION_LZW:
1928 comptype = IFF_TIFF_LZW;
1930 case COMPRESSION_ADOBE_DEFLATE:
1931 comptype = IFF_TIFF_ZIP;
1934 comptype = IFF_TIFF;
1961 l_int32 *pminisblack)
1963 l_uint8 *inarray, *data;
1964 l_uint16 minisblack, comptype;
1966 l_uint32 w, h, rowsperstrip;
1968 size_t fbytes, nbytes;
1972 PROCNAME(
"extractG4DataFromFile");
1975 return ERROR_INT(
"&data not defined", procName, 1);
1977 return ERROR_INT(
"&nbytes not defined", procName, 1);
1978 if (!pw && !ph && !pminisblack)
1979 return ERROR_INT(
"no output data requested", procName, 1);
1984 return ERROR_INT(
"stream not opened to file", procName, 1);
1988 return ERROR_INT(
"filein not tiff", procName, 1);
1991 return ERROR_INT(
"inarray not made", procName, 1);
1994 if ((tif =
openTiff(filein,
"rb")) == NULL) {
1996 return ERROR_INT(
"tif not open for read", procName, 1);
1998 TIFFGetField(tif, TIFFTAG_COMPRESSION, &comptype);
1999 if (comptype != COMPRESSION_CCITTFAX4) {
2002 return ERROR_INT(
"filein is not g4 compressed", procName, 1);
2005 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
2006 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
2007 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
2008 if (h != rowsperstrip)
2009 L_WARNING(
"more than 1 strip\n", procName);
2010 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &minisblack);
2013 if (pw) *pw = (l_int32)w;
2014 if (ph) *ph = (l_int32)h;
2015 if (pminisblack) *pminisblack = (l_int32)minisblack;
2021 if (inarray[0] == 0x4d) {
2022 diroff = (inarray[4] << 24) | (inarray[5] << 16) |
2023 (inarray[6] << 8) | inarray[7];
2025 diroff = (inarray[7] << 24) | (inarray[6] << 16) |
2026 (inarray[5] << 8) | inarray[4];
2033 nbytes = diroff - 8;
2035 if ((data = (l_uint8 *)LEPT_CALLOC(nbytes,
sizeof(l_uint8))) == NULL) {
2037 return ERROR_INT(
"data not allocated", procName, 1);
2040 memcpy(data, inarray + 8, nbytes);
2072 const char *modestring)
2074 PROCNAME(
"fopenTiff");
2077 return (TIFF *)ERROR_PTR(
"stream not opened", procName, NULL);
2079 return (TIFF *)ERROR_PTR(
"modestring not defined", procName, NULL);
2081 TIFFSetWarningHandler(dummyHandler);
2083 fseek(fp, 0, SEEK_SET);
2084 return TIFFClientOpen(
"TIFFstream", modestring, (thandle_t)fp,
2085 lept_read_proc, lept_write_proc, lept_seek_proc,
2086 lept_close_proc, lept_size_proc, NULL, NULL);
2107 const char *modestring)
2112 PROCNAME(
"openTiff");
2115 return (TIFF *)ERROR_PTR(
"filename not defined", procName, NULL);
2117 return (TIFF *)ERROR_PTR(
"modestring not defined", procName, NULL);
2119 TIFFSetWarningHandler(dummyHandler);
2122 tif = TIFFOpen(fname, modestring);
2177 static L_MEMSTREAM *memstreamCreateForRead(l_uint8 *indata,
size_t pinsize);
2178 static L_MEMSTREAM *memstreamCreateForWrite(l_uint8 **poutdata,
2180 static tsize_t tiffReadCallback(thandle_t handle, tdata_t data, tsize_t length);
2181 static tsize_t tiffWriteCallback(thandle_t handle, tdata_t data,
2183 static toff_t tiffSeekCallback(thandle_t handle, toff_t offset, l_int32 whence);
2184 static l_int32 tiffCloseCallback(thandle_t handle);
2185 static toff_t tiffSizeCallback(thandle_t handle);
2186 static l_int32 tiffMapCallback(thandle_t handle, tdata_t *data, toff_t *length);
2187 static void tiffUnmapCallback(thandle_t handle, tdata_t data, toff_t length);
2191 memstreamCreateForRead(l_uint8 *indata,
2197 mstream->buffer = indata;
2198 mstream->bufsize = insize;
2199 mstream->hw = insize;
2200 mstream->offset = 0;
2206 memstreamCreateForWrite(l_uint8 **poutdata,
2212 mstream->buffer = (l_uint8 *)LEPT_CALLOC(8 * 1024, 1);
2213 mstream->bufsize = 8 * 1024;
2214 mstream->poutdata = poutdata;
2215 mstream->poutsize = poutsize;
2216 mstream->hw = mstream->offset = 0;
2222 tiffReadCallback(thandle_t handle,
2230 amount = L_MIN((
size_t)length, mstream->hw - mstream->offset);
2233 if (mstream->offset + amount > mstream->hw) {
2234 fprintf(stderr,
"Bad file: amount too big: %lu\n",
2235 (
unsigned long)amount);
2239 memcpy(data, mstream->buffer + mstream->offset, amount);
2240 mstream->offset += amount;
2246 tiffWriteCallback(thandle_t handle,
2258 if (mstream->offset + length > mstream->bufsize) {
2259 newsize = 2 * (mstream->offset + length);
2260 mstream->buffer = (l_uint8 *)
reallocNew((
void **)&mstream->buffer,
2261 mstream->hw, newsize);
2262 mstream->bufsize = newsize;
2265 memcpy(mstream->buffer + mstream->offset, data, length);
2266 mstream->offset += length;
2267 mstream->hw = L_MAX(mstream->offset, mstream->hw);
2273 tiffSeekCallback(thandle_t handle,
2279 PROCNAME(
"tiffSeekCallback");
2284 mstream->offset = offset;
2288 mstream->offset += offset;
2293 mstream->offset = mstream->hw - offset;
2296 return (toff_t)ERROR_INT(
"bad whence value", procName,
2300 return mstream->offset;
2305 tiffCloseCallback(thandle_t handle)
2310 if (mstream->poutdata) {
2311 *mstream->poutdata = mstream->buffer;
2312 *mstream->poutsize = mstream->hw;
2320 tiffSizeCallback(thandle_t handle)
2330 tiffMapCallback(thandle_t handle,
2337 *data = mstream->buffer;
2338 *length = mstream->hw;
2344 tiffUnmapCallback(thandle_t handle,
2374 const char *operation,
2380 PROCNAME(
"fopenTiffMemstream");
2383 return (TIFF *)ERROR_PTR(
"filename not defined", procName, NULL);
2385 return (TIFF *)ERROR_PTR(
"operation not defined", procName, NULL);
2387 return (TIFF *)ERROR_PTR(
"&data not defined", procName, NULL);
2389 return (TIFF *)ERROR_PTR(
"&datasize not defined", procName, NULL);
2390 if (strcmp(operation,
"r") && strcmp(operation,
"w"))
2391 return (TIFF *)ERROR_PTR(
"op not 'r' or 'w'", procName, NULL);
2393 if (!strcmp(operation,
"r"))
2394 mstream = memstreamCreateForRead(*pdata, *pdatasize);
2396 mstream = memstreamCreateForWrite(pdata, pdatasize);
2398 TIFFSetWarningHandler(dummyHandler);
2400 return TIFFClientOpen(filename, operation, (thandle_t)mstream,
2401 tiffReadCallback, tiffWriteCallback,
2402 tiffSeekCallback, tiffCloseCallback,
2403 tiffSizeCallback, tiffMapCallback,
2438 PROCNAME(
"pixReadMemTiff");
2441 return (
PIX *)ERROR_PTR(
"cdata not defined", procName, NULL);
2443 data = (l_uint8 *)cdata;
2445 return (
PIX *)ERROR_PTR(
"tiff stream not opened", procName, NULL);
2448 for (i = 0; ; i++) {
2454 pixSetInputFormat(pix, IFF_TIFF);
2457 if (TIFFReadDirectory(tif) == 0)
2459 if (i == MANY_PAGES_IN_TIFF_FILE + 1) {
2460 L_WARNING(
"big file: more than %d pages\n", procName,
2461 MANY_PAGES_IN_TIFF_FILE);
2504 PROCNAME(
"pixReadMemFromMultipageTiff");
2507 return (
PIX *)ERROR_PTR(
"cdata not defined", procName, NULL);
2509 return (
PIX *)ERROR_PTR(
"&offset not defined", procName, NULL);
2511 data = (l_uint8 *)cdata;
2513 return (
PIX *)ERROR_PTR(
"tiff stream not opened", procName, NULL);
2517 retval = (offset == 0) ? TIFFSetDirectory(tif, 0)
2518 : TIFFSetSubDirectory(tif, offset);
2530 TIFFReadDirectory(tif);
2531 *poffset = TIFFCurrentDirOffset(tif);
2557 PROCNAME(
"pixaReadMemMultipageTiff");
2560 return (
PIXA *)ERROR_PTR(
"data not defined", procName, NULL);
2567 }
while (offset != 0);
2594 const char *modestr;
2599 PROCNAME(
"pixaWriteMemMultipageTiff");
2601 if (pdata) *pdata = NULL;
2603 return ERROR_INT(
"pdata not defined", procName, 1);
2605 return ERROR_INT(
"pixa not defined", procName, 1);
2609 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
2611 if ((fp = tmpfile()) == NULL)
2612 return ERROR_INT(
"tmpfile stream not opened", procName, 1);
2616 for (i = 0; i < n; i++) {
2617 modestr = (i == 0) ?
"w" :
"a";
2619 if (pixGetDepth(pix1) == 1) {
2622 if (pixGetColormap(pix1)) {
2662 NULL, NULL, NULL, NULL);
2699 PROCNAME(
"pixWriteMemTiffCustom");
2702 return ERROR_INT(
"&data not defined", procName, 1);
2704 return ERROR_INT(
"&size not defined", procName, 1);
2706 return ERROR_INT(
"&pix not defined", procName, 1);
2707 if (pixGetDepth(pix) != 1 && comptype != IFF_TIFF &&
2708 comptype != IFF_TIFF_LZW && comptype != IFF_TIFF_ZIP) {
2709 L_WARNING(
"invalid compression type for bpp > 1\n", procName);
2710 comptype = IFF_TIFF_ZIP;
2714 return ERROR_INT(
"tiff stream not opened", procName, 1);
PIX * pixFlipLR(PIX *pixd, PIX *pixs)
pixFlipLR()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixReadStreamTiff(FILE *fp, l_int32 n)
pixReadStreamTiff()
static l_int32 getTiffStreamResolution(TIFF *tif, l_int32 *pxres, l_int32 *pyres)
getTiffStreamResolution()
l_int32 pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
PIXA * pixaReadMultipageTiff(const char *filename)
pixaReadMultipageTiff()
l_int32 findFileFormatStream(FILE *fp, l_int32 *pformat)
findFileFormatStream()
l_int32 pixEndianTwoByteSwap(PIX *pixs)
pixEndianTwoByteSwap()
char * genPathname(const char *dir, const char *fname)
genPathname()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
l_int32 pixaWriteMemMultipageTiff(l_uint8 **pdata, size_t *psize, PIXA *pixa)
pixaWriteMemMultipageTiff()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_int32 pixcmapToArrays(PIXCMAP *cmap, l_int32 **prmap, l_int32 **pgmap, l_int32 **pbmap, l_int32 **pamap)
pixcmapToArrays()
l_int32 pixWriteMemTiffCustom(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
pixWriteMemTiffCustom()
Memory stream buffer used with TIFFClientOpen()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
l_int32 findFileFormat(const char *filename, l_int32 *pformat)
findFileFormat()
l_int32 readHeaderTiff(const char *filename, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderTiff()
PIX * pixReadMemFromMultipageTiff(const l_uint8 *cdata, size_t size, size_t *poffset)
pixReadMemFromMultipageTiff()
static TIFF * fopenTiff(FILE *fp, const char *modestring)
fopenTiff()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
l_int32 pixWriteTiff(const char *filename, PIX *pix, l_int32 comptype, const char *modestr)
pixWriteTiff()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
l_int32 writeMultipageTiffSA(SARRAY *sa, const char *fileout)
writeMultipageTiffSA()
PIX * pixEndianTwoByteSwapNew(PIX *pixs)
pixEndianTwoByteSwapNew()
l_int32 fprintTiffInfo(FILE *fpout, const char *tiffile)
fprintTiffInfo()
PIX * pixFlipTB(PIX *pixd, PIX *pixs)
pixFlipTB()
static const l_int32 L_INSERT
l_int32 pixcmapAddColor(PIXCMAP *cmap, l_int32 rval, l_int32 gval, l_int32 bval)
pixcmapAddColor()
l_int32 tiffGetCount(FILE *fp, l_int32 *pn)
tiffGetCount()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_int32 pixWriteMemTiff(l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
pixWriteMemTiff()
l_int32 pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
static TIFF * fopenTiffMemstream(const char *filename, const char *operation, l_uint8 **pdata, size_t *pdatasize)
fopenTiffMemstream()
l_int32 pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
l_int32 pixWriteStreamTiffWA(FILE *fp, PIX *pix, l_int32 comptype, const char *modestr)
pixWriteStreamTiffWA()
l_int32 pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
static PIX * pixReadFromTiffStream(TIFF *tif)
pixReadFromTiffStream()
static l_int32 pixWriteToTiffStream(TIFF *tif, PIX *pix, l_int32 comptype, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
pixWriteToTiffStream()
FILE * fopenWriteWinTempfile()
fopenWriteWinTempfile()
static TIFF * openTiff(const char *filename, const char *modestring)
openTiff()
#define GET_DATA_BYTE(pdata, n)
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
l_int32 pixaWriteMultipageTiff(const char *fname, PIXA *pixa)
pixaWriteMultipageTiff()
PIX * pixClone(PIX *pixs)
pixClone()
void pixDestroy(PIX **ppix)
pixDestroy()
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
l_int32 freadHeaderTiff(FILE *fp, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
freadHeaderTiff()
PIX * pixReadMemTiff(const l_uint8 *cdata, size_t size, l_int32 n)
pixReadMemTiff()
PIX * pixEndianByteSwapNew(PIX *pixs)
pixEndianByteSwapNew()
l_int32 writeMultipageTiff(const char *dirin, const char *substr, const char *fileout)
writeMultipageTiff()
l_int32 pixWriteTiffCustom(const char *filename, PIX *pix, l_int32 comptype, const char *modestr, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
pixWriteTiffCustom()
l_int32 fileFormatIsTiff(FILE *fp)
fileFormatIsTiff()
l_int32 findTiffCompression(FILE *fp, l_int32 *pcomptype)
findTiffCompression()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
l_int32 extractG4DataFromFile(const char *filein, l_uint8 **pdata, size_t *pnbytes, l_int32 *pw, l_int32 *ph, l_int32 *pminisblack)
extractG4DataFromFile()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
PIX * pixRead(const char *filename)
pixRead()
char * pixGetText(PIX *pix)
pixGetText()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
l_int32 composeRGBPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *ppixel)
composeRGBPixel()
static l_int32 getTiffCompressedFormat(l_uint16 tiffcomp)
getTiffCompressedFormat()
l_int32 pixcmapGetCount(PIXCMAP *cmap)
pixcmapGetCount()
PIX * pixReadTiff(const char *filename, l_int32 n)
pixReadTiff()
l_int32 readHeaderMemTiff(const l_uint8 *cdata, size_t size, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
readHeaderMemTiff()
l_int32 pixWriteStreamTiff(FILE *fp, PIX *pix, l_int32 comptype)
pixWriteStreamTiff()
PIX * pixReadFromMultipageTiff(const char *fname, size_t *poffset)
pixReadFromMultipageTiff()
l_int32 numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
PIX * pixRotate90(PIX *pixs, l_int32 direction)
pixRotate90()
static l_int32 writeCustomTiffTags(TIFF *tif, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
writeCustomTiffTags()
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
static l_int32 tiffReadHeaderTiff(TIFF *tif, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
tiffReadHeaderTiff()
l_int32 getTiffResolution(FILE *fp, l_int32 *pxres, l_int32 *pyres)
getTiffResolution()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
PIXA * pixaReadMemMultipageTiff(const l_uint8 *data, size_t size)
pixaReadMemMultipageTiff()
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()