tiff

    TIFF, BigTIFF and GeoTIFF reading in MoonBit, as geotiff.js 3.0.5 reads them: IFDs, geo tags, blocks decoded into windows

    tiff
    geotiff
    bigtiff
    raster
    decoder
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0 AND MIT
    Last updated
    5 hours ago
    Downloads
    3

    No dependencies

    #tiff

    TIFF, BigTIFF and GeoTIFF reading in MoonBit, following geotiff.js 3.0.5: the image directories with their geo tags, and blocks (tiles or strips) decoded and copied into a window of pixels. Pure MoonBit on every backend (wasm, wasm-gc, js, native).

    Part of mbt-map, a WebGIS library, published on its own.

    moon add t-ujiie-g/tiff

    /// The first sample of the first image of an uncompressed, LZW or PackBits TIFF held whole in `file`.
    fn first_band(file : Bytes) -> FixedArray[Byte] raise @tiff.TiffError {
    let (images, little) = @tiff.read_tiff(file[:])
    let image = images[0]
    let window = @tiff.Window::new(image, little, 0, 0, image.width, image.height, [0], 0.0)
    for i in 0..<(image.across() * image.down()) {
    let start = image.offsets[i].to_int()
    let end = start + image.counts[i].to_int()
    window.block(i, file[start:end], false)
    }
    window.bands[0]
    }

    #How it reads

    • read_tiff(bytes) reads the IFDs from the start of a file. It raises Truncated(need) when they lie past the bytes given, so a reader over HTTP range requests can fetch need bytes and try again.
    • Image is one IFD: size, tiling, bits and sample formats, compression and predictor, block offsets and counts, and the geo tags (pixel_scale, tiepoint, transform, epsg, nodata).
    • Window::new(image, little, x0, y0, x1, y1, samples, fill) is a rectangle of pixels to fill; Window::block(index, data, decoded) decodes one block and copies its part; Window::empty(index, value) fills a block the file leaves out. bands then holds one little-endian buffer per sample.
    • The caller fetches the bytes: the module does no I/O, which is how one reads only the blocks a window needs from a Cloud Optimized GeoTIFF.

    Decoded here: uncompressed, LZW and PackBits blocks, predictors 2 and 3, bit depths under a byte. Blocks compressed otherwise (Deflate, JPEG, …) are for the caller to decode first: pass the decoded bytes with decoded = true. Writing TIFFs is not covered.

    #Matching geotiff.js

    The repository compares rasters read through this module with geotiff 3.0.5's readRasters on TIFFs of every layout read (strips, tiles, planar 2, LZW, Deflate, PackBits, predictors, sample formats, BigTIFF) (geotiff:golden).

    #License

    Apache-2.0 for the port; geotiff.js is under the MIT License (see NOTICE).

    TiffError

    pub(all) suberror TiffError {
    Invalid(Int, String)
    Truncated(Double)
    }

    Errors reading a TIFF: Invalid(pos, message), or Truncated(need) when the bytes end before the IFDs do and need bytes from the start would hold what is missing.

    TiffError::message

    fn TiffError::message(self : TiffError) -> String

    A one-line description, e.g. malformed TIFF at 8: too many values.

    Image

    pub struct Image {
    width : Int
    height : Int
    tiled : Bool
    block_width : Int
    block_height : Int
    samples : Int
    bits : Array[Int]
    formats : Array[Int]
    planar : Int
    compression : Int
    predictor : Int
    photometric : Int
    subfile : Int
    old_subfile : Int
    offsets : Array[Double]
    counts : Array[Double]
    pixel_scale : Array[Double]
    tiepoint : Array[Double]
    transform : Array[Double]
    epsg : Int
    raster_type : Int
    nodata : String?
    jpeg_tables : Bytes?
    }

    One image (IFD) as geotiff's GeoTIFFImage sees it: blocks (a strip as wide as the image), per sample bits and formats (1 unsigned, 2 signed, 3 float), offsets and counts per block (and sample with planar 2), geo tags (empty when absent) and epsg (0 for none).

    Image::across

    fn Image::across(self : Image) -> Int

    Blocks across the image.

    Image::down

    fn Image::down(self : Image) -> Int

    Blocks down the image.

    Image::pixel_bytes

    fn Image::pixel_bytes(self : Image, sample : Int) -> Int

    Bytes of one decoded pixel of a block: every sample's, or with planar 2 one sample's.

    Image::rows

    fn Image::rows(self : Image, y : Int) -> Int

    Rows of block row y: a strip at the bottom may be shorter, a tile never is.

    Image::sample_bytes

    fn Image::sample_bytes(self : Image, i : Int) -> Int

    Bytes of one decoded sample of sample i (after unpacking bits narrower than a byte).

    Window

    pub struct Window {
    image : Image
    little : Bool
    x0 : Int
    y0 : Int
    x1 : Int
    y1 : Int
    samples : Array[Int]
    bands : Array[FixedArray[Byte]]
    }

    A window of an image being read: x0, y0 to x1, y1 (pixels, end excluded; it may reach past the image), the samples read, one band of little-endian values per sample.

    Window::block

    fn Window::block(self : Window, index : Int, data : BytesView, decoded : Bool) -> Unit raise TiffError

    Decodes block index (in the order of the image's offsets) from data, compressed as the image says unless decoded (the host inflated it), and copies its pixels into the window.

    Window::empty

    fn Window::empty(self : Window, index : Int, value : Double) -> Unit raise TiffError

    Fills block index's pixels of the window with value (a block of byte count 0, which geotiff fills with the GDAL no-data value when it is truthy, else 0).

    Window::new

    fn Window::new(image : Image, little : Bool, x0 : Int, y0 : Int, x1 : Int, y1 : Int, samples : Array[Int], fill : Double) -> Window raise TiffError

    A window of image (of a file little-endian or not) over the pixels x0, y0 to x1, y1, for samples; bands start as fill when it is truthy (geotiff's fillValue), else 0.

    MAX_VALUES

    let MAX_VALUES : Int

    Most values of one tag (a tile offset per block, at most).

    lzw

    fn lzw(data : BytesView, max : Int) -> FixedArray[Byte] raise TiffError

    The LZW data of a block as geotiff's LZWDecoder decodes it (TIFF's early change, codes up to 12 bits; past the end of the data codes read as zeros, or as EOI from the byte after the last), stopping after max bytes.

    packbits

    fn packbits(data : BytesView, max : Int) -> FixedArray[Byte]

    PackBits data as TIFF 6.0 §9 decodes it (a header of -128 is a no-op), at most max bytes.

    read_tiff

    fn read_tiff(b : BytesView) -> (Array[Image], Bool) raise TiffError

    Reads the IFDs of the TIFF or BigTIFF in b (its first bytes at least) as geotiff's GeoTIFF.fromSource and getImage read them; gives the images and whether the file is little-endian. Raises Truncated when an IFD or a tag's values lie past b.

    unpack_bits

    fn unpack_bits(b : FixedArray[Byte], bits : Int, width : Int, rows : Int, count : Int) -> FixedArray[Byte] raise TiffError

    Samples of bits (not a whole number of bytes) unpacked, most significant bit first and each row starting on a byte, into little-endian samples of 1, 2 or 4 bytes as geotiff's normalizeArray does: width x rows pixels of count samples each.

    unpredict

    fn unpredict(b : FixedArray[Byte], predictor : Int, width : Int, height : Int, stride : Int, bytes : Int, little : Bool) -> Unit raise TiffError

    Undoes predictor 2 (horizontal differencing, in the file's byte order) or 3 (floating point: byte planes, giving little-endian samples) over the rows of b, as geotiff's applyPredictor does with width x height blocks of stride samples per pixel.