package core:image/png

Overview

Copyright 2021 Jeroen van Rijn .

Made available under Odin's BSD-2 license.

List of contributors:
	Jeroen van Rijn: Initial implementation.
	Ginger Bill:     Cosmetic changes.

These are a few useful utility functions to work with PNG images.

package png implements a PNG image reader

The PNG specification is at https://www.w3.org/TR/PNG/.

Types

CIE_1931 ¶

CIE_1931 :: struct #packed {
	x: f32,
	y: f32,
}

CIE_1931_Raw ¶

CIE_1931_Raw :: struct #packed {
	x: u32be,
	y: u32be,
}

Error ¶

Error :: image.Error

Exif ¶

Exif :: struct {
	byte_order: enum int {
		little_endian, 
		big_endian, 
	},
	data:       []u8,
}

Filter_Params ¶

Filter_Params :: struct #packed {
	src:      []u8,
	dest:     []u8,
	width:    int,
	height:   int,
	depth:    int,
	channels: int,
	rescale:  bool,
}

Image ¶

Image :: image.Image

Options ¶

Options :: image.Options

PLTE ¶

PLTE :: struct #packed {
	entries: [256][3]u8,
	used:    u16,
}

PLTE_Entry ¶

PLTE_Entry :: [3]u8

Row_Filter ¶

Row_Filter :: enum u8 {
	None    = 0, 
	Sub     = 1, 
	Up      = 2, 
	Average = 3, 
	Paeth   = 4, 
}

Signature ¶

Signature :: enum u64be {
	// 0x89504e470d0a1a0a
	PNG = 9894494448401390090, 
}

Text ¶

Text :: struct {
	keyword:           string,
	keyword_localized: string,
	language:          string,
	text:              string,
}

cHRM ¶

cHRM :: struct #packed {
	w: CIE_1931,
	r: CIE_1931,
	g: CIE_1931,
	b: CIE_1931,
}

cHRM_Raw ¶

cHRM_Raw :: struct #packed {
	w: CIE_1931_Raw,
	r: CIE_1931_Raw,
	g: CIE_1931_Raw,
	b: CIE_1931_Raw,
}

gAMA ¶

gAMA :: struct {
	gamma_100k: u32be,
}

hIST ¶

hIST :: struct #packed {
	entries: [256]u16,
	used:    u16,
}

iCCP ¶

iCCP :: struct {
	name:    string,
	profile: []u8,
}

pHYs ¶

pHYs :: struct #packed {
	ppu_x: u32be,
	ppu_y: u32be,
	unit:  pHYs_Unit,
}

pHYs_Unit ¶

pHYs_Unit :: enum u8 {
	Unknown = 0, 
	Meter   = 1, 
}

sPLT ¶

sPLT :: struct #packed {
	name:    string,
	depth:   u8,
	entries: union {
		[][4]u8, 
		[][4]u16, 
	},
	used:    u16,
}

sRGB ¶

sRGB :: struct #packed {
	intent: sRGB_Rendering_Intent,
}

sRGB_Rendering_Intent ¶

sRGB_Rendering_Intent :: enum u8 {
	Perceptual            = 0, 
	Relative_colorimetric = 1, 
	Saturation            = 2, 
	Absolute_colorimetric = 3, 
}

tIME ¶

tIME :: struct #packed {
	year:   u16be,
	month:  u8,
	day:    u8,
	hour:   u8,
	minute: u8,
	second: u8,
}
 

Other chunks

Constants

INCHES_PER_METER ¶

INCHES_PER_METER :: 1000.0 / 25.4

MAX_CHUNK_SIZE ¶

MAX_CHUNK_SIZE :: min(#config(PNG_MAX_CHUNK_SIZE, 16_777_216), 268_435_456)
 

For chunks other than IDAT with a variable size like zTXT and eXIf,

limit their size to 16 MiB each by default. Max of 256 MiB each.

MAX_IDAT_SIZE ¶

MAX_IDAT_SIZE :: min(#config(PNG_MAX_IDAT_SIZE, _MAX_IDAT_DEFAULT), _MAX_IDAT)

depth_scale_table ¶

depth_scale_table: []u8 : []u8{0, 0xff, 0x55, 0, 0x11, 0, 0, 0, 0x01}

Variables

ADAM7_X_ORIG ¶

ADAM7_X_ORIG: []int = …

ADAM7_X_SPACING ¶

ADAM7_X_SPACING: []int = …

ADAM7_Y_ORIG ¶

ADAM7_Y_ORIG: []int = …

ADAM7_Y_SPACING ¶

ADAM7_Y_SPACING: []int = …

Procedures

append_chunk ¶

append_chunk :: proc "odin" (list: ^[dynamic]image.PNG_Chunk, src: image.PNG_Chunk, allocator := context.allocator) -> (err: image.Error) {…}

chrm ¶

chrm :: proc "odin" (c: image.PNG_Chunk) -> (res: cHRM, ok: bool) {…}

chunk_type_to_name ¶

chunk_type_to_name :: proc "odin" (type: ^image.PNG_Chunk_Type) -> string {…}

copy_chunk ¶

copy_chunk :: proc "odin" (src: image.PNG_Chunk, allocator := context.allocator) -> (dest: image.PNG_Chunk, err: image.Error) {…}

core_time ¶

core_time :: proc "odin" (c: image.PNG_Chunk) -> (t: time.Time, ok: bool) {…}

defilter ¶

defilter :: proc "odin" (img: ^image.Image, filter_bytes: ^bytes.Buffer, header: ^image.PNG_IHDR, options: image.Options) -> (err: image.Error) {…}

defilter_16 ¶

defilter_16 :: proc "odin" (params: ^Filter_Params) -> (ok: bool) {…}

defilter_8 ¶

defilter_8 :: proc "odin" (params: ^Filter_Params) -> (ok: bool) {…}

defilter_less_than_8 ¶

defilter_less_than_8 :: proc "odin" (params: ^Filter_Params) -> (ok: bool) {…}

demo ¶

demo :: proc "odin" () {…}

destroy ¶

destroy :: proc "odin" (img: ^image.Image) {…}

exif ¶

exif :: proc "odin" (c: image.PNG_Chunk) -> (res: Exif, ok: bool) {…}

filter_paeth ¶

filter_paeth :: proc "odin" (left, up, up_left: u8) -> u8 {…}

gamma ¶

gamma :: proc "odin" (c: image.PNG_Chunk) -> (res: f32, ok: bool) {…}

hist ¶

hist :: proc "odin" (c: image.PNG_Chunk) -> (res: hIST, ok: bool) {…}

iccp ¶

iccp :: proc "odin" (c: image.PNG_Chunk) -> (res: iCCP, ok: bool) {…}

iccp_destroy ¶

iccp_destroy :: proc "odin" (i: iCCP) {…}

load_from_bytes ¶

load_from_bytes :: proc "odin" (data: []u8, options: image.Options, allocator: runtime.Allocator) -> (img: ^image.Image, err: image.Error) {…}

load_from_context ¶

load_from_context :: proc "odin" (ctx: ^$, options: image.Options = Options{}, allocator := context.allocator) -> (img: ^image.Image, err: image.Error) {…}

load_from_file ¶

load_from_file :: proc "odin" (filename: string, options: image.Options = Options{}, allocator := context.allocator) -> (img: ^image.Image, err: image.Error) {…}

main ¶

main :: proc "odin" () {…}

phys ¶

phys :: proc "odin" (c: image.PNG_Chunk) -> (res: pHYs, ok: bool) {…}

phys_to_dpi ¶

phys_to_dpi :: proc "odin" (p: pHYs) -> (x_dpi, y_dpi: f32) {…}

plte ¶

plte :: proc "odin" (c: image.PNG_Chunk) -> (res: PLTE, ok: bool) {…}

read_chunk ¶

read_chunk :: proc "odin" (ctx: ^$) -> (chunk: image.PNG_Chunk, err: image.Error) {…}

read_header ¶

read_header :: proc "odin" (ctx: ^$) -> (image.PNG_IHDR, image.Error) {…}

sbit ¶

sbit :: proc "odin" (c: image.PNG_Chunk) -> (res: [4]u8, ok: bool) {…}

splt ¶

splt :: proc "odin" (c: image.PNG_Chunk) -> (res: sPLT, ok: bool) {…}

splt_destroy ¶

splt_destroy :: proc "odin" (s: sPLT) {…}

srgb ¶

srgb :: proc "odin" (c: image.PNG_Chunk) -> (res: sRGB, ok: bool) {…}

text ¶

text :: proc "odin" (c: image.PNG_Chunk) -> (res: Text, ok: bool) {…}

text_destroy ¶

text_destroy :: proc "odin" (text: Text) {…}

time ¶

time :: proc "odin" (c: image.PNG_Chunk) -> (res: tIME, ok: bool) {…}

write_image_as_ppm ¶

write_image_as_ppm :: proc "odin" (filename: string, image: ^image.Image) -> (success: bool) {…}
 

Crappy PPM writer used during testing. Don't use in production.

Procedure Groups

Source Files

Generation Information

Generated with odin version dev-2022-10 (vendor "odin") Windows_amd64 @ 2022-10-05 21:11:47.476800300 +0000 UTC