~/bend-docscommunity

main.bend checks

raw source on the hub · import emerging-ezimg@1.5.0.0/main.bend as Main

ezimg: images for Bend 2, with PNG and baseline JPEG decode and encode

A Raster is a width, a height, and row-major samples. PNG decode reads every colour type and bit depth, interlaced or not. PNG encode writes colour type 2 or 6. JPEG decode reads a baseline sequential frame, and encode_jpeg writes one.

4 imports
import Base
import ./src/png.bend as Png
import ./src/jpeg.bend as Jpeg
import ./src/jpeg_enc.bend as Jenc

Types

type Raster source · line 13 · raw

Data

a picture: width, height, and row-major samples, each packed 0xAARRGGBB

Definitions

def png_sig source · line 17 · raw

List<&2, U32>

the PNG signature: 137 80 78 71 13 10 26 10

def jpeg_soi source · line 21 · raw

List<&2, U32>

the JPEG start-of-image marker: 255 216

def nbytes source · line 25 · raw

@bytes:List<&2, U32> -> U32

how many samples a byte list holds

def raster source · line 31 · raw

@ww:U32 -> @hh:U32 -> @pixels:List<&2, U32> -> Raster

a picture from its width, height, and samples. The samples are taken as they are: when there are not w * h of them the raster is not well formed, and encode_png and encode_jpeg refuse it. checked_raster checks the count.

def area.is source · line 37 · raw

@wn:Nat -> @over:Bool -> @+hn:Nat -> @+ll:Nat -> @+acc:Nat -> Bool

is ll the start acc plus w steps of h: ll == acc + w * h. over says acc has already passed ll, and the walk stops there, so no number past ll + h is built.

def area.of source · line 48 · raw

@+wn:Nat -> @hn:Nat -> @+ll:Nat -> Bool

is ll the area w * h. A zero height answers at once; otherwise the area is walked a row of h at a time.

def checked.keep source · line 55 · raw

@ok:Bool -> @+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> Maybe<&2, Raster>

def checked_raster source · line 64 · raw

@+ww:U32 -> @+hh:U32 -> @+px:List<&2, U32> -> Maybe<&2, Raster>

a picture from its width, height, and samples, or none unless there are exactly w * h samples (w * h a natural number, never wrapped)

def width source · line 68 · raw

@img:Raster -> U32

the width

def height source · line 74 · raw

@img:Raster -> U32

the height

def pixels source · line 80 · raw

@img:Raster -> List<&2, U32>

the samples

def size source · line 86 · raw

@img:Raster -> Pair(U32, U32)

width and height together

def count source · line 94 · raw

@img:Raster -> Nat

how many samples the picture has, as a natural number: w * h for a well-formed raster. It counts the samples rather than multiply the sides, so it never wraps and never builds a number larger than the samples.

def fill.go source · line 100 · raw

@wn:Nat -> @+hn:Nat -> @+color:U32 -> List<&2, U32>

w rows of h samples of one colour: no count past a side is built

def fill.px source · line 108 · raw

@+wn:Nat -> @hn:Nat -> @+color:U32 -> List<&2, U32>

the samples of a solid picture; a zero height has none

def fill source · line 119 · raw

@+ww:U32 -> @+hh:U32 -> @+color:U32 -> Raster

a solid picture of one sample, w * h of them, for every w and h. The samples are built a row at a time and their count is never computed, so a side past 2^16 cannot wrap it; a picture too large for memory fails the way any allocation does.

def u.min.pick source · line 122 · raw

@le:Bool -> @+aa:U32 -> @+bb:U32 -> U32

def u.min source · line 129 · raw

@+aa:U32 -> @+bb:U32 -> U32

def skip.rows source · line 135 · raw

@nn:Nat -> @+px:List<&2, U32> -> @+ww:Nat -> List<&2, U32>

the first n rows of w samples dropped, a row at a time: the row-major index n * w is never built, so it cannot wrap or pass a Nat's range, and the walk stops when the samples run out

def take.rows source · line 145 · raw

@nn:Nat -> @+px:List<&2, U32> -> @+ww:Nat -> List<&2, U32>

the first n rows of w samples, a row at a time

def set.rows source · line 156 · raw

@nn:Nat -> @+px:List<&2, U32> -> @+ww:Nat -> @+xx:Nat -> @+color:U32 -> List<&2, U32>

sample x of row n replaced by color, the rows before it walked one at a time

def get.y source · line 165 · raw

@yin:Bool -> @px:List<&2, U32> -> @+ww:U32 -> @+xx:U32 -> @+yy:U32 -> Maybe<&2, U32>

def get.x source · line 172 · raw

@xin:Bool -> @px:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+xx:U32 -> @+yy:U32 -> Maybe<&2, U32>

def get source · line 181 · raw

@img:Raster -> @+xx:U32 -> @+yy:U32 -> Maybe<&2, U32>

the sample at (x, y), or none when that point is outside the picture. Sample y * w + x is reached a row at a time, so the index never wraps.

def set.y source · line 186 · raw

@yin:Bool -> @+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> @+xx:U32 -> @+yy:U32 -> @+color:U32 -> Raster

def set.x source · line 201 · raw

@xin:Bool -> @+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> @+xx:U32 -> @+yy:U32 -> @+color:U32 -> Raster

def set source · line 218 · raw

@img:Raster -> @+xx:U32 -> @+yy:U32 -> @+color:U32 -> Raster

the picture with (x, y) replaced by color; outside, the picture is unchanged. Sample y * w + x is reached a row at a time.

def crop.rows source · line 223 · raw

@+nn:Nat -> @+px:List<&2, U32> -> @+rw:Nat -> @+gap:Nat -> List<&2, U32>

def crop.use source · line 231 · raw

@zh:Bool -> @+px:List<&2, U32> -> @+ww:U32 -> @+xx:U32 -> @+yy:U32 -> @+rw:U32 -> @+rh:U32 -> Raster

def crop.open source · line 248 · raw

@zw:Bool -> @zh:Bool -> @+px:List<&2, U32> -> @+ww:U32 -> @+xx:U32 -> @+yy:U32 -> @+rw:U32 -> @+rh:U32 -> Raster

def crop.box source · line 264 · raw

@+px:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+xx:U32 -> @+yy:U32 -> @+cw:U32 -> @+ch:U32 -> Raster

def crop.y source · line 277 · raw

@yin:Bool -> @+px:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+xx:U32 -> @+yy:U32 -> @+cw:U32 -> @+ch:U32 -> Raster

def crop.x source · line 293 · raw

@xin:Bool -> @+px:List<&2, U32> -> @+ww:U32 -> @+hh:U32 -> @+xx:U32 -> @+yy:U32 -> @+cw:U32 -> @+ch:U32 -> Raster

def crop source · line 311 · raw

@img:Raster -> @+xx:U32 -> @+yy:U32 -> @+cw:U32 -> @+ch:U32 -> Raster

the intersection of rectangle (x, y, cw, ch) with the picture. A miss, or a zero side, is an empty picture.

def blit.row source · line 316 · raw

@+dp:List<&2, U32> -> @+sp:List<&2, U32> -> @+left:Nat -> @+cover:Nat -> @+tail:Nat -> List<&2, U32>

def blit.rows source · line 327 · raw

@+nn:Nat -> @+dp:List<&2, U32> -> @+sp:List<&2, U32> -> @+left:Nat -> @+cover:Nat -> @+tail:Nat -> @+dw:Nat -> @+sw:Nat -> List<&2, U32>

def blit.join source · line 344 · raw

@+dp:List<&2, U32> -> @+sp:List<&2, U32> -> @+dw:U32 -> @+dh:U32 -> @+sw:U32 -> @+sh:U32 -> @+ox:U32 -> @+oy:U32 -> List<&2, U32>

def blit.y source · line 367 · raw

@inside:Bool -> @+dp:List<&2, U32> -> @src:Raster -> @+dw:U32 -> @+dh:U32 -> @+ox:U32 -> @+oy:U32 -> List<&2, U32>

def blit.src source · line 374 · raw

@src:Raster -> @+dw:U32 -> @+dh:U32 -> @dp:List<&2, U32> -> @+xx:U32 -> @+yy:U32 -> Raster

def blit source · line 379 · raw

@dst:Raster -> @src:Raster -> @+xx:U32 -> @+yy:U32 -> Raster

src pasted onto dst at (x, y). dst keeps its size; samples past its edge are dropped, and an overlapping sample takes the source.

def sig.match source · line 401 · raw

@want:List<&2, U32> -> @bytes:List<&2, U32> -> @ok:Bool -> Bool

do the bytes open with the bytes of want? Each byte is compared with U32.is_eq, not a literal pattern: a literal pattern matches bit by bit, so a law over a symbolic byte cannot get past it, while is_eq has a lemma (ueq). ok carries the answer so far, so the walk is a tail call.

def sig.pick source · line 411 · raw

@ok:Bool -> Maybe<&2, List<&2, U32>>

def parse_signature source · line 419 · raw

@bytes:List<&2, U32> -> Maybe<&2, List<&2, U32>>

the PNG signature when the bytes open with it, otherwise none

def decode_png.keep source · line 424 · raw

@ok:Bool -> @+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> Maybe<&2, Raster>

the raster of the decoded samples when there are exactly h * w of them, otherwise none

def decode_png.pic source · line 434 · raw

@mm:Maybe<&2, 0x82090c86776811917f7c7e0c4eacdbfc/src/png.Pic> -> Maybe<&2, Raster>

a decoded picture as a raster. The sample count is checked against the header's h * w, computed as a natural number, so a header whose scanline length wraps a U32 cannot hand back a raster that is not well formed.

def decode_png.of source · line 442 · raw

@mm:Maybe<&2, List<&2, U32>> -> @+bytes:List<&2, U32> -> Maybe<&2, 0x82090c86776811917f7c7e0c4eacdbfc/src/png.Pic>

the picture of the chunks after the signature, when the bytes open with it

def decode_png source · line 452 · raw

@+bytes:List<&2, U32> -> Maybe<&2, Raster>

PNG bytes, or none when the signature or the encoding is rejected

def encode_png source · line 468 · raw

@img:Raster -> Maybe<&2, List<&2, U32>>

ISO/IEC 15948 / W3C PNG — bytes of an 8-bit picture, interlace method 0. Colour type 2 when every sample is opaque, otherwise colour type 6. None when a side is zero, the sample count is not the area, or the area is 2^28 samples or more: below that bound every count the encoder keeps is exact and its one IDAT chunk is shorter than PNG's 2^31 - 1 byte limit, and decode_png reads every encoding back (laws/PngRoundTrips.bend).

Not compressed: every row has filter type None and the zlib stream is stored (uncompressed) DEFLATE blocks, so a file is the h (k w + 1) filtered bytes (k = 3 or 4 channels), 5 more bytes per 65535 of them, and about 60 bytes of chunks. A 320x260 RGB picture is about 250 KB, where zlib at level 6 makes about 33 KB of it. The round-trip proof rests on the stored blocks; re-compress the file elsewhere when size matters.

def decode_jpeg.out source · line 473 · raw

@got:Maybe<&2, 0x82090c86776811917f7c7e0c4eacdbfc/src/jpeg.Pic> -> Maybe<&2, Raster>

def decode_jpeg source · line 481 · raw

@bytes:List<&2, U32> -> Maybe<&2, Raster>

a baseline sequential JPEG, or none when the bytes are not one

def colour.ch source · line 486 · raw

@+cc:U32 -> @+aa:U32 -> U32

a sample's colour, alpha cleared. The mask is the first operand, so a law one channel of a sample flattened onto white: a / 255 of the channel over (255 - a) / 255 of white, rounded

def colour source · line 491 · raw

@+pp:U32 -> U32

a sample's colour flattened onto white, alpha cleared: a JPEG holds no alpha, so a sample shows as it would over a white page. An opaque sample keeps its colour, (255 c + 127) / 255 = c.

def colours source · line 499 · raw

@px:List<&2, U32> -> List<&2, U32>

every sample's colour flattened onto white

def encode_jpeg.pick source · line 506 · raw

@ok:Bool -> @+ww:U32 -> @+hh:U32 -> @+qq:U32 -> @+sub:Bool -> @px:List<&2, U32> -> Maybe<&2, List<&2, U32>>

def encode_jpeg.side source · line 523 · raw

@+nn:U32 -> Bool

a side a JPEG frame holds: 1 to 65535, the 16 bits T.81 B.2.2 gives X and Y

def encode_jpeg.good source · line 531 · raw

@+ww:U32 -> @+hh:U32 -> @px:List<&2, U32> -> Bool

the rasters encode_jpeg writes: both sides from 1 to 65535, at most 2^31 points, and exactly w * h samples, counted as natural numbers. For sides of at most 65535, w * h is below 2^32, so its U32 product is exact. These are encode_jpeg's own limits, not encode_png's: a JPEG frame cannot hold a side past 65535, and the decoder refuses a frame of more than 2^31 points.

def encode_jpeg_with source · line 562 · raw

@img:Raster -> @+qq:U32 -> @+sub:Bool -> Maybe<&2, List<&2, U32>>

a baseline sequential JPEG of the samples' colour at a quality of 1 to 100 (IJG's scale: T.81 Annex K's tables K.1 and K.2 at 5000 / q percent below 50, else 200 - 2 q percent; a quality outside 1 to 100 is the nearest end), each sample flattened onto white before the encoder sees it (laws/JpegEncodeFlattensAlpha.bend). With sub set, Cb and Cr are sampled at half Y's rate each way (4:2:0, a chrominance sample the mean of 2 by 2, rounded as libjpeg's h2v2 downsampling rounds); without it, at Y's rate (4:4:4). When every flattened sample has red, green and blue the same, it is one gray component with the luminance tables alone, as libjpeg writes a grayscale image, whatever sub says. None exactly when encode_jpeg.good says the raster is not one it writes (laws/JpegEncodeRefusesBadRasters.bend).

Trusted, not proved: for every raster r it writes, every colour channel of decode_jpeg(encode_jpeg_with(r, q, False)) (4:4:4) is within E(q) of r's: E is 8 at quality 100, 32 at 95, 64 at 90 and 128 at 75; below 75 no bound short of 255 is claimed. At 4:2:0 no bound short of 255 is claimed at any quality: a chrominance sample stands for 2 by 2 pixels and the decoder's upsampling blends it with its neighbours, so saturated colour that changes from square to square is lost, in libjpeg-turbo too. Measured, not proved: the largest error over 400 rasters (random, gradients, saturated colours, checkerboards, stripes, gray, sizes 1 to 64) at 4:4:4 is 4 at 100, 24 at 95, 45 at 90, 114 at 75, 209 at 50 and 255 at 25 and below (saturated colour noise), and at 4:2:0 255 at every quality (199 at 100 and 235 at 75 for rasters whose colour is the same over each 2 by 2 square); libjpeg-turbo (Pillow) at the same quality and subsampling reaches 4, 21, 42, 113, 217 and 255 at 4:4:4 and 255 at 4:2:0 (199 and 227). A proof needs error bounds on the fixed-point DCT and IDCT and on the quantisation.

def encode_jpeg_q source · line 569 · raw

@img:Raster -> @+qq:U32 -> Maybe<&2, List<&2, U32>>

encode_jpeg_with at 4:2:0, the IJG library's default subsampling (and Pillow's): its laws hold for both subsamplings, and so for this one

def encode_jpeg.quality source · line 573 · raw

U32

the quality encode_jpeg writes: 75, the IJG library's default (and Pillow's)

def encode_jpeg source · line 581 · raw

@img:Raster -> Maybe<&2, List<&2, U32>>

encode_jpeg_with at quality 75 and 4:2:0, as libjpeg and Pillow write by default (laws/JpegRoundTripKeepsSize.bend states its round trip for every quality and both subsamplings, and this one through it). Its error is encode_jpeg_with's at 4:2:0: no bound short of 255 is claimed; for 4:4:4's bound of 128 at quality 75, use encode_jpeg_with(r, 75, False).

Templates

template map.go source · line 384 · raw

@-ff:(@_:U32 -> U32) -> @px:List<&2, U32> -> List<&2, U32>

template map source · line 392 · raw

@-ff:(@_:U32 -> U32) -> @img:Raster -> Raster

every sample passed through f; the width and the height stay