libtiff clib

TIFF image codec for PascalAI. Read and write TIFF files with LZW/ZIP/JPEG compression, multi-page documents, tiled access for large images, and GeoTIFF support. The standard format for scanned documents, scientific imaging, GIS, and archival photography.

Version1.0.0
Typeclib
CategoryMultimedia
System reqlibtiff-dev
CLib package — Ubuntu/Debian: sudo apt install libtiff-dev
ppm install libtiff

Quick Start

Read a TIFF

uses libtiff;

var t    := OpenRead('/data/scan.tif');
var info := GetInfo(t);
WriteLn('Size: ' + IntToStr(info.Width) + 'x' + IntToStr(info.Height));
WriteLn('Pages: ' + IntToStr(info.PagesCount));
WriteLn('Depth: ' + IntToStr(info.Depth) + ' bpp');

{ Read first page as RGBA }
var pixels := ReadRGBA(t);
Close(t);

Write a multi-page TIFF

uses libtiff;

var t     := OpenWrite('/output/report.tif');
var pages := [page1RGBA, page2RGBA, page3RGBA];

for var i := 0 to 2 do
begin
  SetDimensions(t, 2480, 3508, 4, 8);  { A4 at 300dpi, RGBA }
  SetCompression(t, tcLZW);
  SetPhotometric(t, tpRGB);
  SetResolution(t, 300.0, 300.0);
  WriteRGBA(t, pages[i], 2480, 3508);
  WriteDirectory(t);   { advance to next page }
end;
Close(t);

Open/Close API

FunctionSignatureDescription
OpenReadOpenRead(path: string): TTIFFOpen a TIFF file for reading. Raises an error if the file does not exist or is not a valid TIFF.
OpenWriteOpenWrite(path: string): TTIFFCreate or overwrite a TIFF file for writing.
OpenAppendOpenAppend(path: string): TTIFFOpen an existing TIFF to append additional pages.
CloseClose(t: TTIFF)Flush pending writes and release the file handle. Always call Close when done.

Info API

Function / FieldTypeDescription
GetInfoGetInfo(t: TTIFF): TTIFFInfoReturn image metadata for the currently selected page.
PageCountPageCount(t: TTIFF): IntegerReturn the total number of pages (directories) in the file.
SelectPageSelectPage(t: TTIFF; page: Integer)Make the given 0-based page index active for subsequent reads.
TTIFFInfo.WidthIntegerImage width in pixels.
TTIFFInfo.HeightIntegerImage height in pixels.
TTIFFInfo.PagesCountIntegerNumber of pages/directories in the file.
TTIFFInfo.DepthIntegerBits per sample (e.g. 8 or 16).
TTIFFInfo.ChannelsIntegerNumber of samples per pixel (1=gray, 3=RGB, 4=RGBA).
TTIFFInfo.CompressionTTIFFCompressionCompression scheme in use.
TTIFFInfo.IsTiledBooleanTrue if the image is stored in tiles rather than strips.

Read API

FunctionSignatureDescription
ReadRGBAReadRGBA(t: TTIFF): TBytesDecode the current page to a flat RGBA byte array (width × height × 4 bytes). Handles any color type and bit depth automatically.
ReadRawReadRaw(t: TTIFF): TBytesRead raw, unprocessed image data as stored on disk.
ReadStripReadStrip(t: TTIFF; stripIndex: Integer): TBytesRead a single strip of scanlines. Use for memory-efficient sequential access of large files.
ReadTileReadTile(t: TTIFF; x, y: Integer): TBytesRead a single tile at tile coordinates (x, y). Only valid for tiled images.

Write API

FunctionSignatureDescription
SetDimensionsSetDimensions(t: TTIFF; w, h, channels, depth: Integer)Configure the image geometry before writing. Must be called before any write operation on a new page.
SetCompressionSetCompression(t: TTIFF; c: TTIFFCompression)Choose the compression codec for the current page.
SetPhotometricSetPhotometric(t: TTIFF; p: TTIFFPhotometric)Set the color space interpretation (tpRGB, tpGrayscale, tpYCbCr, etc.).
SetResolutionSetResolution(t: TTIFF; xDpi, yDpi: Double)Embed DPI metadata. Common values: 72, 96, 150, 300, 600.
WriteRGBAWriteRGBA(t: TTIFF; rgba: TBytes; w, h: Integer)Write a full page from a flat RGBA byte array.
WriteScanlineWriteScanline(t: TTIFF; row: TBytes; y: Integer)Write a single scanline at row y. Suitable for streaming encoders.
WriteTileWriteTile(t: TTIFF; data: TBytes; x, y: Integer)Write a tile at tile coordinates (x, y). Requires a tiled layout configured via SetDimensions.
WriteDirectoryWriteDirectory(t: TTIFF)Finalize the current page and advance to a new one. Required between pages in a multi-page file.

Convenience API

FunctionSignatureDescription
ReadPageRGBAReadPageRGBA(path: string; page: Integer): TBytesOne-shot: open, seek to page, read RGBA, close.
WritePageRGBAWritePageRGBA(path: string; rgba: TBytes; w, h: Integer; c: TTIFFCompression)One-shot: write a single-page TIFF with the given compression.
TIFFToPNGTIFFToPNG(tiffPath, pngPath: string)Convert the first page of a TIFF file to a PNG file.
WriteMultiPageWriteMultiPage(path: string; pages: array of TBytes; w, h: Integer; c: TTIFFCompression)Write multiple RGBA pages to a single TIFF file in one call.

Compression Types

ConstantDescription
tcNoneNo compression. Fastest write/read, largest files. Good for intermediate processing.
tcLZWLempel-Ziv-Welch. Lossless with good compression ratio. The most common choice for general TIFF.
tcZIP / tcDeflateDeflate (same algorithm as ZIP/gzip). Lossless, slightly better ratio than LZW in some cases. Aliases for the same codec.
tcJPEGLossy JPEG compression embedded in TIFF. Good for photographic content where lossless is not required.
tcCCITT / tcCCITT4Fax compression for 1-bit bi-level (black and white) images. Extremely compact for scanned documents.
tcPackBitsSimple run-length encoding. Very fast encode/decode but weak compression ratio. Useful when speed matters more than size.

Metadata

Common TIFF tag IDs: 270 = ImageDescription • 305 = Software • 315 = Artist • 316 = HostComputer • 33432 = Copyright
FunctionSignatureDescription
GetStringTagGetStringTag(t: TTIFF; tagId: Integer): stringRead a string-valued TIFF tag by its numeric ID. Returns empty string if the tag is absent.
SetStringTagSetStringTag(t: TTIFF; tagId: Integer; value: string)Write a string-valued tag. Must be called before WriteDirectory for the tag to be included in the page.
GetGeoKeysGetGeoKeys(t: TTIFF): stringExtract GeoTIFF key-value pairs as a newline-delimited string (key=value format). Returns empty string if no GeoTIFF metadata is present.