image
tape
import fs from "fs";
import gfx from "gfx";
import image from "image";
import collections from "collections";
let data: string = fs.read_file("photo.png") or { return 1; };
var img: gfx.Surface = image.decode(data) or {
io.println("cannot read the picture: {err}");
return 1;
};
let png: collections.Vec_u8 = image.encode(&img, image.Format.Png) or { return 1; };
fs.write_file("copy.png", png.slice()) or { return 1; };
img.free();image turns image bytes into a gfx.Surface and back. It is pure: you
read and write the files with fs.
| Function | Does |
|---|---|
decode(data) -> gfx.Surface or ImageError | PNG, JPEG, WebP, BMP, HV3 or HV4 (told apart by their first bytes) as a new Argb surface; release it with free() |
encode(&s, format) -> collections.Vec_u8 or ImageError | The bytes of a file in Format.Png, Format.Bmp, Format.Huva (HV3), Format.Huva4 (HV4) or Format.Huva4Text (an HV4 text image), in a byte vector; v.slice() is what fs.write_file and decode take |
encode_quality(&s, format, quality) -> collections.Vec_u8 or ImageError | encode at a quality from 1 (smallest file) to 100 (best picture) for Format.Huva4 (encode uses 50) and Format.Huva4Text (90). For Format.Png it writes a compressed file, still lossless: the same pixels as encode’s uncompressed one in about half the bytes for a photo, the quality being how hard it tries (1 fastest, 100 smallest). Bmp and Huva ignore it |
tape
pub enum ImageError : u8 {
UnknownFormat = 1: "unknown image format";
Corrupt = 2: "corrupt or unsupported image data";
TooLarge = 3: "image too large";
OutOfMemory = 4: "out of memory";
}PNG and BMP keep every pixel; HV3 and HV4 are compact lossy formats. Format.Huva4Text is for a transparent layer of text (any font effect): it keeps the alpha and is near-lossless; decode tells an HV4 text image from the file. Supported:
PNG of any colour type and bit depth (1 to 16 bits, interlaced or not, with transparency), JPEG, baseline and progressive (restart markers included), WebP (lossy, lossless and with transparency; an animated WebP gives its first frame; tape doesn’t write WebP), 24-bit BMP. Show an image in a
user interface with widgets.Image.
Last modified: