# image Images read from PNG, JPEG, and SVG, and written to PNG and JPEG, each the same `image`: RGBA, eight bits a channel, in sRGB, with straight alpha, rows from the top. That's what a GPU takes as `rgba8_srgb`, so an image is a texture as it is. It's Sakura's, built with [blossom](https://git.malunal.studio/sakura/blossom), and reads std's `containers`, `io`, `math`, and `xml`. ```sakura import image; val icon := image::load("assets/icon.svg", image::svg_size { width = 256 }) else (e) { ... }; val photo := image::load("assets/photo.jpg") else (e) { ... }; image::save(&photo, "out/photo.png") else (e) { ... }; # PNG, by the extension val small := image::jpeg::encode(&photo, image::jpeg::options { quality = 80 }) else (e) { ... }; val tight := image::png::encode(&icon, image::png::options { level = 9 }) else (e) { ... }; ``` `image::decode(bytes)` reads bytes already in memory, and `image::format_of(bytes)` says which format they're in, by their first few. `image::encode(&picture, format)` writes bytes, `image::write_to` writes them into any `writable`, as std's `json::write_to` does, and `image::save` writes a file in the format its extension names. Each format's own `encode` takes its options. An SVG is drawn at the size asked for, its own by default, and a raster image is read at its own whatever is asked. SVG is read, and not written. A project reads it by requiring this folder, or its remote, and naming `image` among a target's `deps`: ```yaml requires: - ../image targets: - name: game type: program deps: [image] ``` ## Layout `image/` is the module `image`, and each format a module beneath it. | Path | Holds | |---|---| | `image.sakura` | `image`, `image_error`, and `max_pixels` | | `formats.sakura` | `format`, `svg_size`, and which format bytes, or an extension, say | | `reading.sakura` | `decode` and `load`, by the format the bytes are in | | `writing.sakura` | `encode`, `write_to`, and `save`, in the format asked for or the extension names | `png/`, `image::png`: | Path | Holds | |---|---| | `png.sakura` | The color types, Adam7's passes, and the chunks read: `decode` | | `header.sakura` | `IHDR`, `PLTE`, and `tRNS` | | `pixels.sakura` | Rows of samples made RGBA, RGBA made rows, and the sizes of rows | | `filters.sakura` | Filters undone, and the one leaving the least chosen | | `inflate.sakura` | zlib's data inflated | | `deflate.sakura` | Runs found back through chains of hashes, at each level | | `blocks.sakura` | Bits written, and each block written stored, by the fixed codes, or by its own | | `huffman.sakura` | Codes read by table, and codes made of how often each symbol is used | | `symbols.sakura` | DEFLATE's lengths and distances, and their symbols | | `checksums.sakura` | CRC-32 and Adler-32 | | `colors.sakura` | What colors an image holds, and the color type and depth chosen of them | | `encode.sakura` | `options` and `encode`: rows filtered and deflated, then the chunks | `jpeg/`, `image::jpeg`: | Path | Holds | |---|---| | `jpeg.sakura` | Zigzag order, and the markers read: `decode` | | `markers.sakura` | The decoder's state, and the frame, tables, and scans' headers | | `huffman.sakura` | Huffman tables, and the bits of a scan read | | `scan.sakura` | A scan's blocks decoded, sequential and progressive | | `idct.sakura` | The inverse DCT | | `color.sakura` | Components upsampled, and pixels' colors of them | | `tables.sakura` | Annex K's quantization and Huffman tables, and quality's scaling | | `planes.sakura` | YCbCr, or gray, of an image's pixels, padded and subsampled | | `fdct.sakura` | The forward DCT, and quantizing | | `entropy.sakura` | Huffman codes, the bits of a scan written, and each block coded | | `encode.sakura` | `options`, `subsampling`, and `encode`: the markers, then the scan | `svg/`, `image::svg`: | Path | Holds | |---|---| | `svg.sakura` | The renderer, and its size worked out: `render` | | `draw.sakura` | The document indexed, styled, and walked, and layers for opacity, clips, and masks | | `viewports.sakura` | Nested `svg`s, `use`, `symbol`, `viewBox`, and `preserveAspectRatio` | | `shapes.sakura` | Each shape's path, filled and stroked | | `gradients.sakura` | Paints, and linear and radial gradients by their `href` chains | | `masks.sakura` | `clipPath` and `mask` | | `style.sakura` | Each element's properties, set and inherited | | `sheet.sakura` | `