image
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, upright = true) -> gfx.Surface or ImageError | PNG, JPEG, WebP, BMP, TGA, HV3 or HV4 (told apart by their first bytes; TGA, which has none, by its header) as a new Argb surface; release it with free(). Turned upright by its EXIF orientation unless upright is false (below) |
orientation(data) -> u8 | The EXIF orientation the file stores, 1 to 8; 1 when it has none |
encode(&s, format) -> collections.Vec_u8 or ImageError | The bytes of a file in Format.Png, Format.Bmp, Format.Tga (32-bit with alpha, uncompressed), 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). For Format.Tga, any quality writes a run-length encoded file (smaller where pixels repeat, the same pixels). Bmp and Huva ignore it |
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, BMP and TGA keep every pixel (TGA its alpha too); 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, and TGA: true-colour (15, 16, 24 and 32 bits), greyscale (8 bits) and colour-mapped (8- or 16-bit indexes), uncompressed or run-length encoded, rows in any order.
TGA is a game developer’s format: many engines and art tools read and write it. A TGA file’s alpha is used when its descriptor gives alpha bits, or, in a TGA 2.0 file with an extension area, when that area says the image has alpha (straight, or premultiplied: then the colour is divided back); otherwise the image is opaque. A TGA file has no signature, so decode checks its header strictly (known image type and colour-map fields, pixel depth, a size the data can hold) and tries it only after every other format: data that is not an image is UnknownFormat, not a garbled picture.
A phone stores a portrait photo as the sensor saw it, sideways, with an
EXIF orientation saying how to turn it. decode applies it, as browsers
do: a portrait photo comes out portrait, its width and height swapped.
decode(data, false) gives the pixels as stored, and orientation(data)
says what the file asks for:
| Orientation | To show it upright |
|---|---|
| 1 | as stored |
| 2 | mirror left to right |
| 3 | turn 180° |
| 4 | mirror top to bottom |
| 5 | mirror about the top-left to bottom-right diagonal (transpose) |
| 6 | turn 90° clockwise |
| 7 | mirror about the other diagonal (transverse) |
| 8 | turn 90° counter-clockwise |
It is read from a JPEG’s EXIF segment, a PNG’s eXIf chunk and a WebP
file’s EXIF chunk. A file without one, or with one that is damaged or
gives a value outside 1 to 8, is orientation 1: decode never fails over
EXIF. Turning is an exact pixel copy. encode writes no EXIF, so a
decoded and re-encoded photo is upright on its own. Other metadata
(camera, date, location, colour profiles) is ignored.
Show an image in a
user interface with widgets.Image.
Last modified: