tiff-asyncio/tile_info.c
2025-07-10 23:44:07 +08:00

59 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <tiffio.h>
int main()
{
TIFF *tif = TIFFOpen("imgs/ortho.tif", "r");
if (!tif)
{
printf("Failed to open file.\n");
return 1;
}
uint32 tileWidth, tileHeight;
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileHeight);
printf("Tile size: %dx%d\n", tileWidth, tileHeight);
uint32 imageWidth, imageHeight;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageHeight);
uint32 tilesAcross = (imageWidth + tileWidth - 1) / tileWidth;
uint32 tilesDown = (imageHeight + tileHeight - 1) / tileHeight;
uint32 totalTiles = tilesAcross * tilesDown;
printf("Tile count: %d x %d = %d\n", tilesAcross, tilesDown, totalTiles);
uint16 samplesPerPixel;
uint16 bitsPerSample;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
// 一个像素有几个通道sample一个sample有多少位
printf("Samples per pixel: %d\n", samplesPerPixel);
printf("Bits per sample: %d\n", bitsPerSample);
uint16 sampleFormat = SAMPLEFORMAT_UINT; // 默认无符号
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleFormat);
printf("Sample format: %d\n", sampleFormat); // 1=uint, 2=int, 3=float
tsize_t *byteCounts = NULL;
toff_t *offsets = NULL;
TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &byteCounts);
TIFFGetField(tif, TIFFTAG_TILEOFFSETS, &offsets);
for (uint32 i = 0; i < totalTiles; i++)
{
printf("Tile %3d: Offset = %8llu, Size = %6lu bytes\n",
i,
(unsigned long long)offsets[i],
(unsigned long)byteCounts[i]);
}
TIFFClose(tif);
return 0;
}