static uint64 _tiffSizeProc(thandle_t fd) { _TIFF_stat_s sb; fd_as_handle_union_t fdh; fdh.h = fd; if (_TIFF_fstat_f(fdh.fd,&sb)<0) return(0); else return((uint64)sb.st_size); }
static int guessSize(int fd, TIFFDataType dtype, _TIFF_off_t hdr_size, uint32 nbands, int swab, uint32 *width, uint32 *length) { const float longt = 40.0; /* maximum possible height/width ratio */ char *buf1, *buf2; _TIFF_stat_s filestat; uint32 w, h, scanlinesize, imagesize; uint32 depth = TIFFDataWidth(dtype); float cor_coef = 0, tmp; if (_TIFF_fstat_f(fd, &filestat) == -1) { fprintf(stderr, "Failed to obtain file size.\n"); return -1; } if (filestat.st_size < hdr_size) { fprintf(stderr, "Too large header size specified.\n"); return -1; } imagesize = (filestat.st_size - hdr_size) / nbands / depth; if (*width != 0 && *length == 0) { fprintf(stderr, "Image height is not specified.\n"); *length = imagesize / *width; fprintf(stderr, "Height is guessed as %lu.\n", (unsigned long)*length); return 1; } else if (*width == 0 && *length != 0) { fprintf(stderr, "Image width is not specified.\n"); *width = imagesize / *length; fprintf(stderr, "Width is guessed as %lu.\n", (unsigned long)*width); return 1; } else if (*width == 0 && *length == 0) { unsigned int fail = 0; fprintf(stderr, "Image width and height are not specified.\n"); w = (uint32) sqrt(imagesize / longt); if( w == 0 ) { fprintf(stderr, "Too small image size.\n"); return -1; } for (; w < sqrt(imagesize * longt); w++) { if (imagesize % w == 0) { scanlinesize = w * depth; buf1 = _TIFFmalloc(scanlinesize); buf2 = _TIFFmalloc(scanlinesize); h = imagesize / w; do { if (_TIFF_lseek_f(fd, hdr_size + (int)(h/2)*scanlinesize, SEEK_SET) == (_TIFF_off_t)-1) { fprintf(stderr, "seek error.\n"); fail=1; break; } if (read(fd, buf1, scanlinesize) != (long) scanlinesize) { fprintf(stderr, "read error.\n"); fail=1; break; } if (read(fd, buf2, scanlinesize) != (long) scanlinesize) { fprintf(stderr, "read error.\n"); fail=1; break; } if (swab) { swapBytesInScanline(buf1, w, dtype); swapBytesInScanline(buf2, w, dtype); } tmp = (float) fabs(correlation(buf1, buf2, w, dtype)); if (tmp > cor_coef) { cor_coef = tmp; *width = w, *length = h; } } while (0); _TIFFfree(buf1); _TIFFfree(buf2); } } if (fail) { return -1; } fprintf(stderr, "Width is guessed as %lu, height is guessed as %lu.\n", (unsigned long)*width, (unsigned long)*length); return 1; } else { if (filestat.st_size<(_TIFF_off_t)(hdr_size+(*width)*(*length)*nbands*depth)) { fprintf(stderr, "Input file too small.\n"); return -1; } } return 1; }
static toff_t _tiffSizeProc(thandle_t fd) { _TIFF_stat_s sb; return (toff_t) (_TIFF_fstat_f((int) fd, &sb) < 0 ? 0 : sb.st_size); }