88 lines
2.7 KiB
C
88 lines
2.7 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.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);
|
|||
|
|
|||
|
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 tileBufSize = TIFFTileSize(tif);
|
|||
|
if (tileBufSize == 0)
|
|||
|
{
|
|||
|
printf("TIFFTileSize returned 0. Possibly not tiled.\n");
|
|||
|
TIFFClose(tif);
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
// 分配一个缓冲区用于存放一个 tile 的像素数据(未压缩)
|
|||
|
uint8_t *tileBuf = (uint8_t *)_TIFFmalloc(tileBufSize);
|
|||
|
if (!tileBuf)
|
|||
|
{
|
|||
|
fprintf(stderr, "Failed to allocate tile buffer.\n");
|
|||
|
TIFFClose(tif);
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
// 遍历每个 tile 并解码为像素数据
|
|||
|
for (uint32 i = 0; i < totalTiles; i++)
|
|||
|
{
|
|||
|
// 读取 tile 编号 i,解码为原始像素,存入 tileBuf
|
|||
|
tsize_t readBytes = TIFFReadEncodedTile(tif, i, tileBuf, tileBufSize);
|
|||
|
if (readBytes == -1)
|
|||
|
{
|
|||
|
fprintf(stderr, "Failed to decode tile %d\n", i);
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
printf("Tile %3d: Decoded %6ld bytes (uncompressed pixel data)\n",
|
|||
|
i, (long)readBytes);
|
|||
|
|
|||
|
// 示例:打印前4个像素(假设每像素 samplesPerPixel 个 uint8 通道)
|
|||
|
for (int px = 0; px < 4 && px < (readBytes / samplesPerPixel); px++)
|
|||
|
{
|
|||
|
printf(" Pixel %d: ", px);
|
|||
|
for (int s = 0; s < samplesPerPixel; s++)
|
|||
|
{
|
|||
|
printf("%3u ", tileBuf[px * samplesPerPixel + s]);
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
_TIFFfree(tileBuf);
|
|||
|
TIFFClose(tif);
|
|||
|
return 0;
|
|||
|
}
|