std/image holds RGBA pictures and resizes (nearest, box, Lanczos), composites, pastes, rotates and quantizes them with Pillow's arithmetic, so tools give the same pixels Pillow gave. std/png_linux reads every standard PNG and writes RGBA, RGB and palette files, std/jpeg writes baseline JPEGs, and std/zlib_linux handles whole zlib, gzip and raw deflate streams.
250 lines
11 KiB
Ziran
250 lines
11 KiB
Ziran
// Baseline JPEG files from std/image pictures, on every target: JFIF with
|
|
// 4:2:0 chroma subsampling, the standard Huffman tables and the IJG quality
|
|
// scale that libjpeg and Pillow use. Alpha is ignored.
|
|
//
|
|
// bytes: Vec(u8)
|
|
// if WriteJPEG(*title, *bytes, 82) { WriteEntireFile("title.jpg", BuilderFinish(bytes)) }
|
|
#import "image"
|
|
#import "math"
|
|
using Vectors :: #import "vec"
|
|
|
|
#scope_file
|
|
ZIGZAG: [64]u8 = .[0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48,
|
|
41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30,
|
|
37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63];
|
|
|
|
LUMINANCE: [64]u8 = .[16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24,
|
|
40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55,
|
|
64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99];
|
|
|
|
CHROMINANCE: [64]u8 = .[17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99,
|
|
99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
|
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99];
|
|
|
|
// Annex K.3: the code-length counts and symbols of the four tables.
|
|
DC_LUMINANCE_COUNTS: [16]u8 = .[0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0];
|
|
DC_CHROMINANCE_COUNTS: [16]u8 = .[0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];
|
|
DC_SYMBOLS: [12]u8 = .[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
|
AC_LUMINANCE_COUNTS: [16]u8 = .[0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125];
|
|
AC_LUMINANCE_SYMBOLS: [162]u8 = .[1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50,
|
|
129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24,
|
|
25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83,
|
|
84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120,
|
|
121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154,
|
|
162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194,
|
|
195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226,
|
|
227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250];
|
|
AC_CHROMINANCE_COUNTS: [16]u8 = .[0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119];
|
|
AC_CHROMINANCE_SYMBOLS: [162]u8 = .[0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50,
|
|
129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225,
|
|
37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73,
|
|
74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118,
|
|
119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151,
|
|
152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184,
|
|
185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217,
|
|
218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250];
|
|
|
|
Codes :: struct {
|
|
code: [256]u16
|
|
size: [256]u8
|
|
}
|
|
|
|
Build :: (counts: []u8, symbols: []u8) -> Codes {
|
|
codes: Codes
|
|
code: u32 = 0
|
|
at: s64 = 0
|
|
for length: 1..16 {
|
|
for index: 1..cast(s64)counts[length - 1] {
|
|
codes.code[symbols[at]] = cast(u16)code
|
|
codes.size[symbols[at]] = cast(u8)length
|
|
code += 1
|
|
at += 1
|
|
}
|
|
code = code << 1
|
|
}
|
|
return codes
|
|
}
|
|
|
|
Writer :: struct {
|
|
bits: u32
|
|
count: s32
|
|
}
|
|
|
|
Bits :: (output: *Vec(u8), writer: *Writer, value: u32, size: s32) {
|
|
if size == 0 { return }
|
|
writer.bits = (writer.bits << cast(u32)size) | (value & ((cast(u32)1 << cast(u32)size) - 1))
|
|
writer.count += size
|
|
while writer.count >= 8 {
|
|
byte := cast(u8)((writer.bits >> cast(u32)(writer.count - 8)) & 255)
|
|
VecPush(<<output, byte)
|
|
if byte == 255 { VecPush(<<output, cast(u8)0) }
|
|
writer.count -= 8
|
|
}
|
|
writer.bits = writer.bits & ((cast(u32)1 << cast(u32)writer.count) - 1)
|
|
}
|
|
|
|
Magnitude :: (value: s32) -> s32 {
|
|
size: s32 = 0
|
|
magnitude := ifx value < 0 then -value else value
|
|
while magnitude > 0 { size += 1; magnitude = magnitude >> 1 }
|
|
return size
|
|
}
|
|
|
|
Word :: (output: *Vec(u8), value: s32) {
|
|
VecPush(<<output, cast(u8)((value >> 8) & 255))
|
|
VecPush(<<output, cast(u8)(value & 255))
|
|
}
|
|
|
|
HuffmanTable :: (output: *Vec(u8), class_and_id: u8, counts: []u8, symbols: []u8) {
|
|
VecPush(<<output, class_and_id)
|
|
for counts { VecPush(<<output, it) }
|
|
for symbols { VecPush(<<output, it) }
|
|
}
|
|
|
|
Scaled :: (base: []u8, quality: s32, table: []s32) {
|
|
scale := ifx quality < 50 then 5000 / quality else 200 - quality * 2
|
|
for index: 0..63 {
|
|
value := (cast(s32)base[index] * scale + 50) / 100
|
|
if value < 1 { value = 1 }
|
|
if value > 255 { value = 255 }
|
|
table[index] = value
|
|
}
|
|
}
|
|
|
|
// Transforms, quantizes and entropy-codes one 8x8 block of samples
|
|
// centred on zero; PREDICTOR carries the component's last DC value.
|
|
Block :: (output: *Vec(u8), writer: *Writer, samples: []float64, table: []s32,
|
|
cosines: []float64, dc: *Codes, ac: *Codes, predictor: *s32) {
|
|
coefficients: [64]s32
|
|
for v: 0..7 {
|
|
for u: 0..7 {
|
|
sum: float64 = 0.0
|
|
for y: 0..7 {
|
|
row: float64 = 0.0
|
|
for x: 0..7 { row += samples[y * 8 + x] * cosines[x * 8 + u] }
|
|
sum += row * cosines[y * 8 + v]
|
|
}
|
|
scale_u := ifx u == 0 then 0.70710678118654752 else 1.0
|
|
scale_v := ifx v == 0 then 0.70710678118654752 else 1.0
|
|
value := sum * scale_u * scale_v / 4.0 / cast(float64)table[v * 8 + u]
|
|
coefficients[v * 8 + u] = cast(s32)(ifx value < 0.0 then value - 0.5 else value + 0.5)
|
|
}
|
|
}
|
|
difference := coefficients[0] - <<predictor
|
|
<<predictor = coefficients[0]
|
|
size := Magnitude(difference)
|
|
Bits(output, writer, cast(u32)dc.code[size], cast(s32)dc.size[size])
|
|
Bits(output, writer, cast(u32)(ifx difference < 0 then difference - 1 else difference), size)
|
|
run: s32 = 0
|
|
for index: 1..63 {
|
|
value := coefficients[ZIGZAG[index]]
|
|
if value == 0 { run += 1; continue }
|
|
while run > 15 {
|
|
Bits(output, writer, cast(u32)ac.code[240], cast(s32)ac.size[240])
|
|
run -= 16
|
|
}
|
|
size = Magnitude(value)
|
|
symbol := (run << 4) | size
|
|
Bits(output, writer, cast(u32)ac.code[symbol], cast(s32)ac.size[symbol])
|
|
Bits(output, writer, cast(u32)(ifx value < 0 then value - 1 else value), size)
|
|
run = 0
|
|
}
|
|
if run > 0 { Bits(output, writer, cast(u32)ac.code[0], cast(s32)ac.size[0]) }
|
|
}
|
|
|
|
Clamped :: (image: *Image, x: s64, y: s64) -> s64 {
|
|
cx := ifx x < image.width then x else cast(s64)image.width - 1
|
|
cy := ifx y < image.height then y else cast(s64)image.height - 1
|
|
return (cy * image.width + cx) * 4
|
|
}
|
|
|
|
Luma :: (image: *Image, at: s64) -> float64 {
|
|
return 0.299 * cast(float64)image.pixels[at] + 0.587 * cast(float64)image.pixels[at + 1] + 0.114 * cast(float64)image.pixels[at + 2]
|
|
}
|
|
|
|
Chroma :: (image: *Image, at: s64, red: bool) -> float64 {
|
|
r := cast(float64)image.pixels[at]
|
|
g := cast(float64)image.pixels[at + 1]
|
|
b := cast(float64)image.pixels[at + 2]
|
|
if red { return 0.5 * r - 0.418688 * g - 0.081312 * b }
|
|
return -0.168736 * r - 0.331264 * g + 0.5 * b
|
|
}
|
|
|
|
#scope_export
|
|
// Appends IMAGE as a baseline JPEG at QUALITY (1 to 100) to OUTPUT.
|
|
WriteJPEG :: (image: *Image, output: *Vec(u8), quality: s32 = 75) -> bool {
|
|
if !ImageValid(image) || quality < 1 || quality > 100 || image.width > 65535 || image.height > 65535 { return false }
|
|
luminance: [64]s32
|
|
chrominance: [64]s32
|
|
Scaled(LUMINANCE[:], quality, luminance[:])
|
|
Scaled(CHROMINANCE[:], quality, chrominance[:])
|
|
cosines: [64]float64
|
|
for x: 0..7 {
|
|
for u: 0..7 { cosines[x * 8 + u] = Cos((2.0 * cast(float64)x + 1.0) * cast(float64)u * 3.14159265358979323846 / 16.0) }
|
|
}
|
|
dc_luminance := Build(DC_LUMINANCE_COUNTS[:], DC_SYMBOLS[:])
|
|
dc_chrominance := Build(DC_CHROMINANCE_COUNTS[:], DC_SYMBOLS[:])
|
|
ac_luminance := Build(AC_LUMINANCE_COUNTS[:], AC_LUMINANCE_SYMBOLS[:])
|
|
ac_chrominance := Build(AC_CHROMINANCE_COUNTS[:], AC_CHROMINANCE_SYMBOLS[:])
|
|
// Start of image and the JFIF header.
|
|
jfif: [20]u8 = .[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]
|
|
for jfif { VecPush(<<output, it) }
|
|
for table: 0..1 {
|
|
Word(output, 65499)
|
|
Word(output, 67)
|
|
VecPush(<<output, cast(u8)table)
|
|
for index: 0..63 {
|
|
value := ifx table == 0 then luminance[ZIGZAG[index]] else chrominance[ZIGZAG[index]]
|
|
VecPush(<<output, cast(u8)value)
|
|
}
|
|
}
|
|
frame: [5]u8 = .[255, 192, 0, 17, 8]
|
|
for frame { VecPush(<<output, it) }
|
|
Word(output, image.height)
|
|
Word(output, image.width)
|
|
components: [10]u8 = .[3, 1, 34, 0, 2, 17, 1, 3, 17, 1]
|
|
for components { VecPush(<<output, it) }
|
|
Word(output, 65476)
|
|
Word(output, 2 + 4 * 17 + 12 * 2 + 162 * 2)
|
|
HuffmanTable(output, 0, DC_LUMINANCE_COUNTS[:], DC_SYMBOLS[:])
|
|
HuffmanTable(output, 16, AC_LUMINANCE_COUNTS[:], AC_LUMINANCE_SYMBOLS[:])
|
|
HuffmanTable(output, 1, DC_CHROMINANCE_COUNTS[:], DC_SYMBOLS[:])
|
|
HuffmanTable(output, 17, AC_CHROMINANCE_COUNTS[:], AC_CHROMINANCE_SYMBOLS[:])
|
|
scan: [14]u8 = .[255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0]
|
|
for scan { VecPush(<<output, it) }
|
|
writer: Writer
|
|
predictors: [3]s32
|
|
samples: [64]float64
|
|
for top: 0..(image.height + 15) / 16 - 1 {
|
|
for left: 0..(image.width + 15) / 16 - 1 {
|
|
for block: 0..3 {
|
|
for y: 0..7 {
|
|
for x: 0..7 {
|
|
at := Clamped(image, left * 16 + (block % 2) * 8 + x, top * 16 + (block / 2) * 8 + y)
|
|
samples[y * 8 + x] = Luma(image, at) - 128.0
|
|
}
|
|
}
|
|
Block(output, *writer, samples[:], luminance[:], cosines[:], *dc_luminance, *ac_luminance, *predictors[0])
|
|
}
|
|
for component: 1..2 {
|
|
for y: 0..7 {
|
|
for x: 0..7 {
|
|
sum: float64 = 0.0
|
|
for dy: 0..1 {
|
|
for dx: 0..1 {
|
|
sum += Chroma(image, Clamped(image, left * 16 + x * 2 + dx, top * 16 + y * 2 + dy), component == 2)
|
|
}
|
|
}
|
|
samples[y * 8 + x] = sum / 4.0
|
|
}
|
|
}
|
|
Block(output, *writer, samples[:], chrominance[:], cosines[:], *dc_chrominance, *ac_chrominance, *predictors[component])
|
|
}
|
|
}
|
|
}
|
|
// Fill the last byte with one bits, then end the image.
|
|
if writer.count > 0 { Bits(output, *writer, 127, 8 - writer.count) }
|
|
Word(output, 65497)
|
|
return true
|
|
}
|