// Pure MoonBit port of stb_image_write v1.19 JPEG writer
// (baseline sequential, 4:2:0 subsampling above quality 90, derived from
// Jon Olick's jo_jpeg). The canonical Huffman code tables are built from the
// standard BITS/HUFFVAL arrays instead of being transcribed.
///|
let stbiw_jpg_zigzag : FixedArray[Int] = [
0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41,
43, 9, 11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38,
46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63,
]
///|
let stbiw_jpg_yqt : FixedArray[Int] = [
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,
]
///|
let stbiw_jpg_uvqt : FixedArray[Int] = [
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,
]
///|
let stbiw_jpg_aasf : FixedArray[Float] = [
(2.828427125 : Float),
(3.923141122 : Float),
(3.695518130 : Float),
(3.325874224 : Float),
(2.828427125 : Float),
(2.222190367 : Float),
(1.530733730 : Float),
(0.780362301 : Float),
]
///|
let stbiw_jpg_dc_lum_nrcodes : FixedArray[Int] = [
0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
]
///|
let stbiw_jpg_dc_lum_values : FixedArray[Int] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
]
///|
let stbiw_jpg_ac_lum_nrcodes : FixedArray[Int] = [
0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7D,
]
///|
let stbiw_jpg_ac_lum_values : FixedArray[Int] = [
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51,
0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1,
0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75,
0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92,
0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8,
0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2,
0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
]
///|
let stbiw_jpg_dc_chr_nrcodes : FixedArray[Int] = [
0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
]
///|
let stbiw_jpg_dc_chr_values : FixedArray[Int] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
]
///|
let stbiw_jpg_ac_chr_nrcodes : FixedArray[Int] = [
0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
]
///|
let stbiw_jpg_ac_chr_values : FixedArray[Int] = [
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07,
0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09,
0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25,
0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2,
0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
]
///|
/// Canonical JPEG Huffman table: code and bit length per symbol.
priv struct StbiwJpgHuff {
codes : FixedArray[Int]
sizes : FixedArray[Int]
}
///|
fn stbiw_jpg_build_huff(
nrcodes : FixedArray[Int],
values : FixedArray[Int],
) -> StbiwJpgHuff {
let codes : FixedArray[Int] = FixedArray::make(256, 0)
let sizes : FixedArray[Int] = FixedArray::make(256, 0)
let mut code = 0
let mut k = 0
for l = 1; l <= 16; l = l + 1 {
for _i = 0; _i < nrcodes[l - 1]; _i = _i + 1 {
let sym = values[k]
codes[sym] = code
sizes[sym] = l
code += 1
k += 1
}
code = code << 1
}
{ codes, sizes }
}
///|
/// MSB-first bit sink that stuffs 0x00 after every 0xFF (`stbiw__jpg_writeBits`).
priv struct StbiwJpgBits {
out : StbiwWriter
mut buf : UInt
mut cnt : Int
}
///|
fn StbiwJpgBits::write_bits(self : StbiwJpgBits, code : Int, size : Int) -> Unit {
self.cnt += size
self.buf = self.buf | (code.reinterpret_as_uint() << (24 - self.cnt))
while self.cnt >= 8 {
let c = ((self.buf >> 16) & (0xFF : UInt)).reinterpret_as_int()
self.out.write_u8(c)
if c == 255 {
self.out.write_u8(0)
}
self.buf = self.buf << 8
self.cnt -= 8
}
}
///|
fn StbiwJpgBits::write_huff(
self : StbiwJpgBits,
huff : StbiwJpgHuff,
sym : Int,
) -> Unit {
self.write_bits(huff.codes[sym], huff.sizes[sym])
}
///|
/// In-place 8-point DCT (AAN) over eight strided slots (`stbiw__jpg_DCT`).
fn stbiw_jpg_dct(
d : FixedArray[Float],
i0 : Int,
i1 : Int,
i2 : Int,
i3 : Int,
i4 : Int,
i5 : Int,
i6 : Int,
i7 : Int,
) -> Unit {
let d0 = d[i0]
let d1 = d[i1]
let d2 = d[i2]
let d3 = d[i3]
let d4 = d[i4]
let d5 = d[i5]
let d6 = d[i6]
let d7 = d[i7]
let tmp0 = d0 + d7
let tmp7 = d0 - d7
let tmp1 = d1 + d6
let tmp6 = d1 - d6
let tmp2 = d2 + d5
let tmp5 = d2 - d5
let tmp3 = d3 + d4
let tmp4 = d3 - d4
let tmp10 = tmp0 + tmp3
let tmp13 = tmp0 - tmp3
let tmp11 = tmp1 + tmp2
let tmp12 = tmp1 - tmp2
let out0 = tmp10 + tmp11
let out4 = tmp10 - tmp11
let z1 = (tmp12 + tmp13) * (0.707106781 : Float)
let out2 = tmp13 + z1
let out6 = tmp13 - z1
let t10 = tmp4 + tmp5
let t11 = tmp5 + tmp6
let t12 = tmp6 + tmp7
let z5 = (t10 - t12) * (0.382683433 : Float)
let z2 = t10 * (0.541196100 : Float) + z5
let z4 = t12 * (1.306562965 : Float) + z5
let z3 = t11 * (0.707106781 : Float)
let z11 = tmp7 + z3
let z13 = tmp7 - z3
d[i5] = z13 + z2
d[i3] = z13 - z2
d[i1] = z11 + z4
d[i7] = z11 - z4
d[i0] = out0
d[i2] = out2
d[i4] = out4
d[i6] = out6
}
///|
/// Magnitude category and amplitude bits (`stbiw__jpg_calcBits`).
fn stbiw_jpg_calc_bits(val : Int) -> (Int, Int) {
let mut tmp1 = if val < 0 { -val } else { val }
let v = if val < 0 { val - 1 } else { val }
let mut n = 1
tmp1 = tmp1 >> 1
while tmp1 != 0 {
n += 1
tmp1 = tmp1 >> 1
}
(v & ((1 << n) - 1), n)
}
///|
/// DCT, quantize and entropy-code one 8x8 data unit (`stbiw__jpg_processDU`).
fn stbiw_jpg_process_du(
bits : StbiwJpgBits,
cdu : FixedArray[Float],
base : Int,
du_stride : Int,
fdtbl : FixedArray[Float],
dc : Int,
dch : StbiwJpgHuff,
ach : StbiwJpgHuff,
) -> Int {
let du : FixedArray[Int] = FixedArray::make(64, 0)
let end = base + du_stride * 8
for off = base; off < end; off = off + du_stride {
stbiw_jpg_dct(
cdu,
off,
off + 1,
off + 2,
off + 3,
off + 4,
off + 5,
off + 6,
off + 7,
)
}
for i = base; i < base + 8; i = i + 1 {
stbiw_jpg_dct(
cdu,
i,
i + du_stride,
i + du_stride * 2,
i + du_stride * 3,
i + du_stride * 4,
i + du_stride * 5,
i + du_stride * 6,
i + du_stride * 7,
)
}
let mut j = 0
for y = 0; y < 8; y = y + 1 {
for x = 0; x < 8; x = x + 1 {
let v = cdu[base + y * du_stride + x] * fdtbl[j]
du[stbiw_jpg_zigzag[j]] = (if v < (0.0 : Float) { v - (0.5 : Float) } else { v + (0.5 : Float) }).to_int()
j += 1
}
}
let diff = du[0] - dc
if diff == 0 {
bits.write_huff(dch, 0)
} else {
let (val, n) = stbiw_jpg_calc_bits(diff)
bits.write_huff(dch, n)
bits.write_bits(val, n)
}
let mut end0pos = 63
while end0pos > 0 && du[end0pos] == 0 {
end0pos -= 1
}
if end0pos == 0 {
bits.write_huff(ach, 0)
return du[0]
}
let mut i = 1
while i <= end0pos {
let startpos = i
while du[i] == 0 && i <= end0pos {
i += 1
}
let mut nrzeroes = i - startpos
if nrzeroes >= 16 {
let lng = nrzeroes >> 4
for _m = 1; _m <= lng; _m = _m + 1 {
bits.write_huff(ach, 0xF0)
}
nrzeroes = nrzeroes & 15
}
let (val, n) = stbiw_jpg_calc_bits(du[i])
bits.write_huff(ach, (nrzeroes << 4) + n)
bits.write_bits(val, n)
i += 1
}
if end0pos != 63 {
bits.write_huff(ach, 0)
}
du[0]
}
///|
fn stbiw_jpg_write_bytes(w : StbiwWriter, bytes : FixedArray[Int]) -> Unit {
for i = 0; i < bytes.length(); i = i + 1 {
w.write_u8(bytes[i])
}
}
///|
/// Encode 8-bit pixels as a baseline JPEG file in memory.
///
/// `quality` is 1..100 (0 means the default 90); quality <= 90 uses 4:2:0
/// chroma subsampling, above that 4:4:4. `comp == 2` is grey+alpha where the
/// alpha channel is ignored.
pub fn stbi_write_jpg_to_memory(
width : Int,
height : Int,
comp : Int,
data : Bytes,
quality? : Int = 90,
stride_in_bytes? : Int = 0,
options? : StbiwOptions,
) -> Bytes? {
let opts = match options {
Some(o) => o
None => StbiwOptions::default()
}
let stride = match stbiw_resolve_byte_stride(
width,
height,
comp,
stride_in_bytes,
data.length(),
) {
Some(s) => s
None => return None
}
let q0 = if quality == 0 { 90 } else { quality }
let subsample = q0 <= 90
let q1 = if q0 < 1 { 1 } else if q0 > 100 { 100 } else { q0 }
let scale = if q1 < 50 { 5000 / q1 } else { 200 - q1 * 2 }
let ytable : FixedArray[Int] = FixedArray::make(64, 0)
let uvtable : FixedArray[Int] = FixedArray::make(64, 0)
for i = 0; i < 64; i = i + 1 {
let mut yti = (stbiw_jpg_yqt[i] * scale + 50) / 100
yti = if yti < 1 { 1 } else if yti > 255 { 255 } else { yti }
ytable[stbiw_jpg_zigzag[i]] = yti
let mut uvti = (stbiw_jpg_uvqt[i] * scale + 50) / 100
uvti = if uvti < 1 { 1 } else if uvti > 255 { 255 } else { uvti }
uvtable[stbiw_jpg_zigzag[i]] = uvti
}
let fdtbl_y : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let fdtbl_uv : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let mut k = 0
for row = 0; row < 8; row = row + 1 {
for col = 0; col < 8; col = col + 1 {
let aasf = stbiw_jpg_aasf[row] * stbiw_jpg_aasf[col]
fdtbl_y[k] = (1.0 : Float) /
(Float::from_int(ytable[stbiw_jpg_zigzag[k]]) * aasf)
fdtbl_uv[k] = (1.0 : Float) /
(Float::from_int(uvtable[stbiw_jpg_zigzag[k]]) * aasf)
k += 1
}
}
let w = StbiwWriter::new(capacity=width * height * comp + 1024)
// SOI + JFIF APP0 + DQT marker head
stbiw_jpg_write_bytes(w, [
0xFF, 0xD8, 0xFF, 0xE0, 0, 0x10, 0x4A, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0,
0, 0xFF, 0xDB, 0, 0x84, 0,
])
stbiw_jpg_write_bytes(w, ytable)
w.write_u8(1)
stbiw_jpg_write_bytes(w, uvtable)
// SOF0
stbiw_jpg_write_bytes(w, [
0xFF, 0xC0, 0, 0x11, 8, (height >> 8) & 0xFF, height & 0xFF, (width >> 8) & 0xFF,
width & 0xFF, 3, 1, if subsample { 0x22 } else { 0x11 }, 0, 2, 0x11, 1, 3, 0x11, 1,
0xFF, 0xC4, 0x01, 0xA2, 0,
])
stbiw_jpg_write_bytes(w, stbiw_jpg_dc_lum_nrcodes)
stbiw_jpg_write_bytes(w, stbiw_jpg_dc_lum_values)
w.write_u8(0x10)
stbiw_jpg_write_bytes(w, stbiw_jpg_ac_lum_nrcodes)
stbiw_jpg_write_bytes(w, stbiw_jpg_ac_lum_values)
w.write_u8(1)
stbiw_jpg_write_bytes(w, stbiw_jpg_dc_chr_nrcodes)
stbiw_jpg_write_bytes(w, stbiw_jpg_dc_chr_values)
w.write_u8(0x11)
stbiw_jpg_write_bytes(w, stbiw_jpg_ac_chr_nrcodes)
stbiw_jpg_write_bytes(w, stbiw_jpg_ac_chr_values)
// SOS
stbiw_jpg_write_bytes(w, [
0xFF, 0xDA, 0, 0xC, 3, 1, 0, 2, 0x11, 3, 0x11, 0, 0x3F, 0,
])
let ydc = stbiw_jpg_build_huff(stbiw_jpg_dc_lum_nrcodes, stbiw_jpg_dc_lum_values)
let yac = stbiw_jpg_build_huff(stbiw_jpg_ac_lum_nrcodes, stbiw_jpg_ac_lum_values)
let uvdc = stbiw_jpg_build_huff(stbiw_jpg_dc_chr_nrcodes, stbiw_jpg_dc_chr_values)
let uvac = stbiw_jpg_build_huff(stbiw_jpg_ac_chr_nrcodes, stbiw_jpg_ac_chr_values)
let bits : StbiwJpgBits = { out: w, buf: (0 : UInt), cnt: 0 }
let ofs_g = if comp > 2 { 1 } else { 0 }
let ofs_b = if comp > 2 { 2 } else { 0 }
let mut dcy = 0
let mut dcu = 0
let mut dcv = 0
let sample = fn(row : Int, col : Int) -> (Float, Float, Float) {
let clamped_row = if row < height { row } else { height - 1 }
let base_p = (if opts.flip_vertically {
height - 1 - clamped_row
} else {
clamped_row
}) *
stride
let c = if col < width { col } else { width - 1 }
let p = base_p + c * comp
let r = Float::from_int(data[p].to_int())
let g = Float::from_int(data[p + ofs_g].to_int())
let b = Float::from_int(data[p + ofs_b].to_int())
(r, g, b)
}
if subsample {
for y = 0; y < height; y = y + 16 {
for x = 0; x < width; x = x + 16 {
let yy : FixedArray[Float] = FixedArray::make(256, (0.0 : Float))
let uu : FixedArray[Float] = FixedArray::make(256, (0.0 : Float))
let vv : FixedArray[Float] = FixedArray::make(256, (0.0 : Float))
let mut pos = 0
for row = y; row < y + 16; row = row + 1 {
for col = x; col < x + 16; col = col + 1 {
let (r, g, b) = sample(row, col)
yy[pos] = (0.299 : Float) * r +
(0.587 : Float) * g +
(0.114 : Float) * b -
(128.0 : Float)
uu[pos] = -(0.16874 : Float) * r -
(0.33126 : Float) * g +
(0.5 : Float) * b
vv[pos] = (0.5 : Float) * r -
(0.41869 : Float) * g -
(0.08131 : Float) * b
pos += 1
}
}
dcy = stbiw_jpg_process_du(bits, yy, 0, 16, fdtbl_y, dcy, ydc, yac)
dcy = stbiw_jpg_process_du(bits, yy, 8, 16, fdtbl_y, dcy, ydc, yac)
dcy = stbiw_jpg_process_du(bits, yy, 128, 16, fdtbl_y, dcy, ydc, yac)
dcy = stbiw_jpg_process_du(bits, yy, 136, 16, fdtbl_y, dcy, ydc, yac)
let subu : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let subv : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let mut p2 = 0
for sy = 0; sy < 8; sy = sy + 1 {
for sx = 0; sx < 8; sx = sx + 1 {
let jj = sy * 32 + sx * 2
subu[p2] = (uu[jj] + uu[jj + 1] + uu[jj + 16] + uu[jj + 17]) *
(0.25 : Float)
subv[p2] = (vv[jj] + vv[jj + 1] + vv[jj + 16] + vv[jj + 17]) *
(0.25 : Float)
p2 += 1
}
}
dcu = stbiw_jpg_process_du(bits, subu, 0, 8, fdtbl_uv, dcu, uvdc, uvac)
dcv = stbiw_jpg_process_du(bits, subv, 0, 8, fdtbl_uv, dcv, uvdc, uvac)
}
}
} else {
for y = 0; y < height; y = y + 8 {
for x = 0; x < width; x = x + 8 {
let yy : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let uu : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let vv : FixedArray[Float] = FixedArray::make(64, (0.0 : Float))
let mut pos = 0
for row = y; row < y + 8; row = row + 1 {
for col = x; col < x + 8; col = col + 1 {
let (r, g, b) = sample(row, col)
yy[pos] = (0.299 : Float) * r +
(0.587 : Float) * g +
(0.114 : Float) * b -
(128.0 : Float)
uu[pos] = -(0.16874 : Float) * r -
(0.33126 : Float) * g +
(0.5 : Float) * b
vv[pos] = (0.5 : Float) * r -
(0.41869 : Float) * g -
(0.08131 : Float) * b
pos += 1
}
}
dcy = stbiw_jpg_process_du(bits, yy, 0, 8, fdtbl_y, dcy, ydc, yac)
dcu = stbiw_jpg_process_du(bits, uu, 0, 8, fdtbl_uv, dcu, uvdc, uvac)
dcv = stbiw_jpg_process_du(bits, vv, 0, 8, fdtbl_uv, dcv, uvdc, uvac)
}
}
}
bits.write_bits(0x7F, 7)
w.write_u8(0xFF)
w.write_u8(0xD9)
Some(w.to_bytes())
}