image reference
The image package: image.
image
error Malformed
offer error MalformedThe file is not what it says it is: bad signature, bad checksum, a length that runs past the end, or data that stops early.
error Unsupported
offer error UnsupportedA real format feature this reader does not do (compressed BMP, a PNG with an unknown critical chunk, ...).
error TooLarge
offer error TooLargeThe picture is bigger than kMaxSide on a side or kMaxPixels in all.
constant kMaxSide
offer const kMaxSide: int = 16384The longest side a picture may have.
constant kMaxPixels
offer const kMaxPixels: int = 67108864The most pixels a picture may have (64 Mi, so 256 MiB of RGBA): a header can claim any size, and the check happens before the buffer.
class Image
offer class ImageA picture: width * height pixels of RGBA, four bytes each.
Owns its storage and gives it back when it goes out of scope.
field pixels
var pixels: []mut u8field width
var width: int = 0field height
var height: int = 0method deinit
deinitmethod Contains
fun Contains(x: int, y: int) -> boolWhether (x, y) is inside the picture.
method Get
fun Get(x: int, y: int) -> u32The pixel as 0xAARRGGBB, or 0 outside the picture.
method Set
fun Set(x: int, y: int, argb: u32)Sets a pixel from 0xAARRGGBB; outside the picture does nothing.
function ImageNew
offer fun ImageNew(width: int, height: int) -> Image!(Malformed, TooLarge) uses unsafeA transparent black picture of the given size, or TooLarge, or
Malformed when a side is not positive.
function PngDecode
offer fun PngDecode(data: []u8) -> Image!(Malformed, TooLarge, Unsupported) uses unsafeReads a PNG: every colour type, bit depths 1 to 16 (16 is cut to 8),
all five filters, Adam7, PLTE and tRNS. Chunk checksums, the zlib
header and its Adler-32 are checked, and the inflater is held to the
size the header implies, so a small file cannot inflate into a large
one (D227, D245).
function PngEncode
offer fun PngEncode(image: &Image) -> []u8 uses unsafeWrites an RGBA8 PNG, choosing for each row whichever of the five filters leaves the smallest sum of magnitudes (the usual heuristic), then deflating. The caller owns the result.
function QoiDecode
offer fun QoiDecode(data: []u8) -> Image!(Malformed, TooLarge) uses unsafeReads a QOI file, all six operations. Anything after the last pixel
is ignored; running out of bytes before the last pixel is Malformed.
function QoiEncode
offer fun QoiEncode(image: &Image) -> []u8 uses unsafeWrites a QOI file (four channels, sRGB) with the run, index, diff, luma and literal operations. The caller owns the result.
function BmpDecode
offer fun BmpDecode(data: []u8) -> Image!(Malformed, TooLarge, Unsupported) uses unsafeReads a BMP: 24 or 32 bits, uncompressed, bottom-up or top-down, with
a 40-byte or larger header. A 32-bit file's fourth byte is taken as
alpha. Palettes, RLE and bit fields are Unsupported.
function BmpEncode
offer fun BmpEncode(image: &Image) -> []u8 uses unsafeWrites a 32-bit uncompressed bottom-up BMP, alpha in the fourth byte. The caller owns the result.