Module: graphics.image.png
Source: ./graphics/image/png.reef
Overview
graphics/image/png.reef - PNG file format loader Part of reef-stdlib graphics module
This module provides PNG (Portable Network Graphics) file loading. Uses system zlib for DEFLATE decompression via FFI. Supports: Grayscale, RGB, Indexed (palette), GrayAlpha, RGBA Supports: 8-bit and 16-bit depth (16-bit downsampled to 8-bit)
Types
PngResult
Fields:
| Name | Type |
|---|---|
ok |
bool |
img |
image.Image |
error_msg |
string |
PngChunk
Fields:
| Name | Type |
|---|---|
length |
int |
chunk_type |
string |
data_offset |
int |
crc |
int |
IhdrData
IHDR data
Fields:
| Name | Type |
|---|---|
width |
int |
height |
int |
bit_depth |
int |
color_type |
int |
compression |
int |
filter_method |
int |
interlace |
int |
EncoderOutput
Holder for an encoder output buffer + its valid length.
Fields:
| Name | Type |
|---|---|
buffer |
[uint8] |
length |
int |
Functions
fn png_result_ok(result: PngResult): bool
fn png_result_image(result: PngResult): image.Image
fn png_result_error(result: PngResult): string
fn make_png_error(msg: string): PngResult
fn make_png_ok(img: image.Image): PngResult
fn read_u8(data: [int], offset: int): int
fn read_u32_be(data: [int], offset: int): int
fn read_chunk_type(data: [int], offset: int): string
fn string_to_bytes(s: string): [int]
fn validate_png_signature(data: [int]): bool
fn parse_chunk(data: [int], offset: int): PngChunk
fn chunk_size(chunk: PngChunk): int
fn parse_ihdr(data: [int], chunk: PngChunk): IhdrData
fn bytes_per_pixel(color_type: int, bit_depth: int): int
fn paeth_predictor(a: int, b: int, c: int): int
fn total_idat_size(data: [int], start_offset: int): int
Sum IDAT chunk lengths without copying; returns a value strictly greater than the cap if the total overflows or exceeds it, so the caller can reject before any allocation. Cap = 67108864.
fn collect_idat_data(data: [int], start_offset: int): [int]
fn parse_palette(data: [int], start_offset: int): [core.Color]
fn decompress_idat(idat_data: [int], expected_size: int): [int]
fn decode_pixel(scanline: [int], x: int, color_type: int, palette: [core.Color]): core.Color
fn decode_scanlines(raw_data: [int], ihdr: IhdrData, palette: [core.Color]): image.Image
fn load_png(filepath: string): PngResult
fn load_png_bytes(data: [int]): PngResult
fn png_color_type_for_format(format: image.PixelFormat): int
Determine PNG color type for an Image's pixel format
fn png_bpp_for_color_type(color_type: int): int
Bytes per pixel in the encoded raw PNG stream for a color type
fn build_raw_image_data(img: image.Image, color_type: int, total_out: int): [uint8]
Build the raw, unfiltered PNG image data into a calloc-backed [byte] suitable for passing to zlib_compress. Each scanline is prefixed with filter byte 0 (None), with pixels in PNG channel order.
fn chunk_crc(chunk_type: string, data: [uint8], data_len: int): int
Compute CRC32 over chunk type (4 ASCII bytes) plus chunk data. Uses the incremental crc32 API so we don't need to splice arrays.
fn write_chunk(out: [uint8], offset: int, chunk_type: string, data: [uint8], data_len: int): int
Write a PNG chunk (length + type + data + CRC) into out at offset. Returns the new offset after the chunk.
fn encode_png_internal(img: image.Image): EncoderOutput
Internal: produce the encoded PNG into a calloc-backed [byte] plus its length.
fn encode_png_bytes(img: image.Image): [uint8]
fn save_png(filepath: string, img: image.Image): bool
Procedures
proc apply_filter(filter_type: int, scanline: [int], prev_scanline: [int], bpp: int, width: int)
proc write_u32_be(buf: [uint8], offset: int, value: int)
Write a 32-bit big-endian integer into a [byte] buffer at offset. Treats the int as the low 32 bits (so negative ints are handled as their two's-complement byte pattern).
Generated by reefc doc