mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
lib/lowleveljpeg: add new Go package
This commit is contained in:
1 parent
c7b717ea58
commit
2cac96f56b
10 files changed
+2548
No files matched your search
@@ -0,0 +1,159 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
// ArrayNBlockT are arrays of N BlockT values. N is the number of blocks in a
|
||||
// JPEG MCU (Mininum Coded Unit).
|
||||
//
|
||||
// - N = 1 is for ColorTypeGray.
|
||||
// - N = 3 is for ColorTypeYCbCr444.
|
||||
// - N = 6 is for ColorTypeYCbCr420.
|
||||
type (
|
||||
Array1BlockU8 [1]BlockU8
|
||||
Array1BlockI16 [1]BlockI16
|
||||
Array3BlockU8 [3]BlockU8
|
||||
Array3BlockI16 [3]BlockI16
|
||||
Array6BlockU8 [6]BlockU8
|
||||
Array6BlockI16 [6]BlockI16
|
||||
)
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array1BlockU8) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array1BlockI16) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array3BlockU8) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array3BlockI16) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array6BlockU8) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// SetToNeutral calls SetToNeutral on each element.
|
||||
func (b *Array6BlockI16) SetToNeutral() {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
for i := range b {
|
||||
b[i].SetToNeutral()
|
||||
}
|
||||
}
|
||||
|
||||
// InverseDCTFrom calls InverseDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array1BlockU8) InverseDCTFrom(src *Array1BlockI16) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].InverseDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// ForwardDCTFrom calls ForwardDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array1BlockI16) ForwardDCTFrom(src *Array1BlockU8) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].ForwardDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// InverseDCTFrom calls InverseDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array3BlockU8) InverseDCTFrom(src *Array3BlockI16) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].InverseDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// ForwardDCTFrom calls ForwardDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array3BlockI16) ForwardDCTFrom(src *Array3BlockU8) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].ForwardDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// InverseDCTFrom calls InverseDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array6BlockU8) InverseDCTFrom(src *Array6BlockI16) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].InverseDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// ForwardDCTFrom calls ForwardDCTFrom pairwise on dst and src elements.
|
||||
func (dst *Array6BlockI16) ForwardDCTFrom(src *Array6BlockU8) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
for i := range dst {
|
||||
dst[i].ForwardDCTFrom(&src[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Array2QuantizationFactors is an array of 2 QuantizationFactors. The first
|
||||
// one is for Luma and the second one is for Chroma.
|
||||
type Array2QuantizationFactors [2]QuantizationFactors
|
||||
|
||||
// SetToStandardValues calls SetToStandardValues on each element.
|
||||
func (b *Array2QuantizationFactors) SetToStandardValues(quality int) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
b[0].SetToStandardValues(0, quality)
|
||||
b[1].SetToStandardValues(1, quality)
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
// ifElse is like C's (condition ? whenTrue : whenFalse) expression.
|
||||
func ifElse(condition bool, whenTrue int64, whenFalse int64) int64 {
|
||||
if condition {
|
||||
return whenTrue
|
||||
}
|
||||
return whenFalse
|
||||
}
|
||||
|
||||
const (
|
||||
fmtHex = "0123456789ABCDEF"
|
||||
fmtSep = " \n" // 7 spaces and then a new line.
|
||||
)
|
||||
|
||||
const (
|
||||
// BlockU8NeutralValue is the neutral value for a BlockU8, which holds
|
||||
// unsigned uint8 values in the range [0x00, 0xFF].
|
||||
BlockU8NeutralValue = 0x80
|
||||
|
||||
// BlockI16NeutralValue is the neutral value for a BlockI16, which holds
|
||||
// signed int16 values in the range [-0x8000, +0x7FFF].
|
||||
BlockI16NeutralValue = 0
|
||||
)
|
||||
|
||||
// BlockU8 is an 8×8 block of uint8 values, such as a block of red, green, blue
|
||||
// or gray pixel values.
|
||||
//
|
||||
// It is indexed in XY (not DCT) space. If b is a BlockU8 then b[0] is the
|
||||
// top-left corner and b[8] is one pixel below that.
|
||||
type BlockU8 [64]uint8
|
||||
|
||||
// String returns b in human-readable form.
|
||||
func (b BlockU8) String() string {
|
||||
buf := [64 * 3]byte{}
|
||||
for i, value := range b {
|
||||
buf[(3*i)+0] = fmtHex[15&(value>>0x04)]
|
||||
buf[(3*i)+1] = fmtHex[15&(value>>0x00)]
|
||||
buf[(3*i)+2] = fmtSep[7&i]
|
||||
}
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// SetToNeutral sets each element to BlockU8NeutralValue.
|
||||
func (b *BlockU8) SetToNeutral() {
|
||||
for i := range b {
|
||||
b[i] = BlockU8NeutralValue
|
||||
}
|
||||
}
|
||||
|
||||
// ForwardDCT returns the FDCT (Forward Discrete Cosine Transform) of b.
|
||||
func (b *BlockU8) ForwardDCT() (ret BlockI16) {
|
||||
ret.ForwardDCTFrom(b)
|
||||
return ret
|
||||
}
|
||||
|
||||
// ForwardDCTFrom sets *dst to src.ForwardDCT().
|
||||
func (dst *BlockI16) ForwardDCTFrom(src *BlockU8) {
|
||||
if dst == nil {
|
||||
return
|
||||
} else if src == nil {
|
||||
dst.SetToNeutral()
|
||||
return
|
||||
}
|
||||
|
||||
// Convert from uint8 to int64, applying an 0x80 bias.
|
||||
srcI64 := [64]int64{}
|
||||
for i, v := range src {
|
||||
srcI64[i] = int64(v) - 0x80
|
||||
}
|
||||
|
||||
// Iterate in DCT space (outer) and XY space (inner).
|
||||
for v := 0; v < 8; v++ {
|
||||
halfAlphaV16 := ifElse(v == 0, fixedPointInv2Sqrt2, fixedPointHalf)
|
||||
for u := 0; u < 8; u++ {
|
||||
halfAlphaU16 := ifElse(u == 0, fixedPointInv2Sqrt2, fixedPointHalf)
|
||||
// alphas32 is two 16.16 fixed point values multiplied together, as
|
||||
// a 32.32 fixed point value.
|
||||
alphas32 := halfAlphaV16 * halfAlphaU16
|
||||
sum32 := int64(0)
|
||||
|
||||
for y := 0; y < 8; y++ {
|
||||
for x := 0; x < 8; x++ {
|
||||
// c32 is two 16.16 fixed point values multiplied together,
|
||||
// as a 32.32 fixed point value.
|
||||
//
|
||||
// sum32 accumulates a 32.32 fixed point value.
|
||||
c32 := int64(cosines[(((2*x)+1)*u)&31]) *
|
||||
int64(cosines[(((2*y)+1)*v)&31])
|
||||
sum32 += srcI64[(8*y)+x] * c32
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate alphasSum32 as 32.32 fixed point.
|
||||
alphas16 := (alphas32 + (1 << 15)) >> 16
|
||||
sum16 := (sum32 + (1 << 15)) >> 16
|
||||
alphasSum32 := alphas16 * sum16
|
||||
|
||||
// Store alphasSum32 as 16.0 fixed point.
|
||||
result0 := (alphasSum32 + (1 << 31)) >> 32
|
||||
dst[(8*v)+u] = int16(result0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BlockI16 is an 8×8 block of int16 values, such as a block of JPEG coefficients.
|
||||
//
|
||||
// It is indexed in DCT (not XY) space. If b is a BlockI16 then b[0] is the DC
|
||||
// coefficient and every other element is an AC coefficient.
|
||||
//
|
||||
// The horizontal-only AC coefficients are b[1], b[2], ..., b[7], in order from
|
||||
// low-frequency to high-frequency.
|
||||
//
|
||||
// The vertical-only AC coefficients are b[8], b[16], ..., b[56], in order from
|
||||
// low-frequency to high-frequency.
|
||||
type BlockI16 [64]int16
|
||||
|
||||
// String returns b in human-readable form.
|
||||
func (b BlockI16) String() string {
|
||||
buf := [64 * 5]byte{}
|
||||
for i, value := range b {
|
||||
buf[(5*i)+0] = fmtHex[15&(value>>0x0C)]
|
||||
buf[(5*i)+1] = fmtHex[15&(value>>0x08)]
|
||||
buf[(5*i)+2] = fmtHex[15&(value>>0x04)]
|
||||
buf[(5*i)+3] = fmtHex[15&(value>>0x00)]
|
||||
buf[(5*i)+4] = fmtSep[7&i]
|
||||
}
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// SetToNeutral sets each element to BlockI16NeutralValue.
|
||||
func (b *BlockI16) SetToNeutral() {
|
||||
for i := range b {
|
||||
b[i] = BlockI16NeutralValue
|
||||
}
|
||||
}
|
||||
|
||||
// InverseDCT returns the IDCT (Inverse Discrete Cosine Transform) of b.
|
||||
func (b *BlockI16) InverseDCT() (ret BlockU8) {
|
||||
ret.InverseDCTFrom(b)
|
||||
return ret
|
||||
}
|
||||
|
||||
// InverseDCTFrom sets *dst to src.InverseDCT().
|
||||
func (dst *BlockU8) InverseDCTFrom(src *BlockI16) {
|
||||
if dst == nil {
|
||||
return
|
||||
} else if src == nil {
|
||||
dst.SetToNeutral()
|
||||
return
|
||||
}
|
||||
|
||||
// Convert from int16 to int64.
|
||||
srcI64 := [64]int64{}
|
||||
for i, v := range src {
|
||||
srcI64[i] = int64(v)
|
||||
}
|
||||
|
||||
// Iterate in XY space (outer) and DCT space (inner).
|
||||
for y := 0; y < 8; y++ {
|
||||
for x := 0; x < 8; x++ {
|
||||
alphasSum32 := int64(0)
|
||||
|
||||
for v := 0; v < 8; v++ {
|
||||
halfAlphaV16 := ifElse(v == 0, fixedPointInv2Sqrt2, fixedPointHalf)
|
||||
for u := 0; u < 8; u++ {
|
||||
halfAlphaU16 := ifElse(u == 0, fixedPointInv2Sqrt2, fixedPointHalf)
|
||||
// alphas16 is two 16.16 fixed point values multiplied
|
||||
// together, as a 48.16 fixed point value.
|
||||
alphas16 := ((halfAlphaV16 * halfAlphaU16) + (1 << 15)) >> 16
|
||||
|
||||
// c32 is two 16.16 fixed point values multiplied together,
|
||||
// as a 32.32 fixed point value.
|
||||
//
|
||||
// c16 is c32 as a 48.16 fixed point value.
|
||||
//
|
||||
// alphasSum32 accumulates a 32.32 fixed point value.
|
||||
c32 := int64(cosines[(((2*x)+1)*u)&31]) *
|
||||
int64(cosines[(((2*y)+1)*v)&31])
|
||||
c16 := (c32 + (1 << 15)) >> 16
|
||||
alphasSum32 += srcI64[(8*v)+u] * (alphas16 * c16)
|
||||
}
|
||||
}
|
||||
|
||||
// Store alphasSum32, biased and clamped, as 8.0 fixed point.
|
||||
result0 := (alphasSum32 + (1 << 31)) >> 32
|
||||
dst[(8*y)+x] = biasAndClamp[result0&1023]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IsValid returns whether b does not contain unexpectedly extreme values for
|
||||
// an 8-bit-depth JPEG - one that is invalid to pass to AddN. ForwardDCT or
|
||||
// ForwardDCTFrom always returns or sets a BlockI16 that IsValid.
|
||||
//
|
||||
// Specifically:
|
||||
// - a DC element is out of range when outside [-1024, +1023].
|
||||
// - an AC element is out of range when outside [-1023, +1023].
|
||||
//
|
||||
// A BlockI16 is valid when none of its elements are out of range.
|
||||
func (b *BlockI16) IsValid() bool {
|
||||
if b == nil {
|
||||
return false
|
||||
}
|
||||
if (b[0] < -1024) || (+1023 < b[0]) {
|
||||
return false
|
||||
}
|
||||
for _, v := range b[1:] {
|
||||
if (v < -1023) || (+1023 < v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// These are "1 / 2" and "1 / (2 * √2)" as 16.16 fixed point values.
|
||||
const (
|
||||
fixedPointHalf = +0x8000
|
||||
fixedPointInv2Sqrt2 = +0x5A82
|
||||
)
|
||||
|
||||
// cosines is a cosine table as 16.16 fixed point values.
|
||||
var cosines = [32]int32{
|
||||
+0x10000, // cos(π/16 * 0)
|
||||
+0x0FB14, // cos(π/16 * 1)
|
||||
+0x0EC83, // cos(π/16 * 2)
|
||||
+0x0D4DB, // cos(π/16 * 3)
|
||||
+0x0B504, // cos(π/16 * 4)
|
||||
+0x08E39, // cos(π/16 * 5)
|
||||
+0x061F7, // cos(π/16 * 6)
|
||||
+0x031F1, // cos(π/16 * 7)
|
||||
|
||||
+0x00000, // cos(π/16 * 8)
|
||||
-0x031F1, // cos(π/16 * 9)
|
||||
-0x061F7, // cos(π/16 * 10)
|
||||
-0x08E39, // cos(π/16 * 11)
|
||||
-0x0B504, // cos(π/16 * 12)
|
||||
-0x0D4DB, // cos(π/16 * 13)
|
||||
-0x0EC83, // cos(π/16 * 14)
|
||||
-0x0FB14, // cos(π/16 * 15)
|
||||
|
||||
-0x10000, // cos(π/16 * 16)
|
||||
-0x0FB14, // cos(π/16 * 17)
|
||||
-0x0EC83, // cos(π/16 * 18)
|
||||
-0x0D4DB, // cos(π/16 * 19)
|
||||
-0x0B504, // cos(π/16 * 20)
|
||||
-0x08E39, // cos(π/16 * 21)
|
||||
-0x061F7, // cos(π/16 * 22)
|
||||
-0x031F1, // cos(π/16 * 23)
|
||||
|
||||
+0x00000, // cos(π/16 * 24)
|
||||
+0x031F1, // cos(π/16 * 25)
|
||||
+0x061F7, // cos(π/16 * 26)
|
||||
+0x08E39, // cos(π/16 * 27)
|
||||
+0x0B504, // cos(π/16 * 28)
|
||||
+0x0D4DB, // cos(π/16 * 29)
|
||||
+0x0EC83, // cos(π/16 * 30)
|
||||
+0x0FB14, // cos(π/16 * 31)
|
||||
}
|
||||
|
||||
// biasAndClamp[x & 1023] is (x + 0x80), clamped to the range [0x00, 0xFF], for
|
||||
// a signed integer x in the range [-512, +511].
|
||||
var biasAndClamp = [1024]uint8{
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
|
||||
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
|
||||
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
|
||||
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
|
||||
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
|
||||
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
|
||||
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
|
||||
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
|
||||
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
|
||||
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
|
||||
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
||||
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
|
||||
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
|
||||
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
|
||||
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
const hardCodedDHTSegments = "" +
|
||||
// 212 bytes for the first DHT segment.
|
||||
"\xFF\xC4\x00\xD2\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00" +
|
||||
"\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A" +
|
||||
"\x0B\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00" +
|
||||
"\x01\x7D\x01\x02\x03\x00\x04\x11\x05\x12\x21\x31\x41\x06\x13\x51" +
|
||||
"\x61\x07\x22\x71\x14\x32\x81\x91\xA1\x08\x23\x42\xB1\xC1\x15\x52" +
|
||||
"\xD1\xF0\x24\x33\x62\x72\x82\x09\x0A\x16\x17\x18\x19\x1A\x25\x26" +
|
||||
"\x27\x28\x29\x2A\x34\x35\x36\x37\x38\x39\x3A\x43\x44\x45\x46\x47" +
|
||||
"\x48\x49\x4A\x53\x54\x55\x56\x57\x58\x59\x5A\x63\x64\x65\x66\x67" +
|
||||
"\x68\x69\x6A\x73\x74\x75\x76\x77\x78\x79\x7A\x83\x84\x85\x86\x87" +
|
||||
"\x88\x89\x8A\x92\x93\x94\x95\x96\x97\x98\x99\x9A\xA2\xA3\xA4\xA5" +
|
||||
"\xA6\xA7\xA8\xA9\xAA\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xC2\xC3" +
|
||||
"\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA" +
|
||||
"\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xF1\xF2\xF3\xF4\xF5\xF6" +
|
||||
"\xF7\xF8\xF9\xFA" +
|
||||
// 212 bytes for the second DHT segment.
|
||||
"\xFF\xC4\x00\xD2\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01" +
|
||||
"\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A" +
|
||||
"\x0B\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01" +
|
||||
"\x02\x77\x00\x01\x02\x03\x11\x04\x05\x21\x31\x06\x12\x41\x51\x07" +
|
||||
"\x61\x71\x13\x22\x32\x81\x08\x14\x42\x91\xA1\xB1\xC1\x09\x23\x33" +
|
||||
"\x52\xF0\x15\x62\x72\xD1\x0A\x16\x24\x34\xE1\x25\xF1\x17\x18\x19" +
|
||||
"\x1A\x26\x27\x28\x29\x2A\x35\x36\x37\x38\x39\x3A\x43\x44\x45\x46" +
|
||||
"\x47\x48\x49\x4A\x53\x54\x55\x56\x57\x58\x59\x5A\x63\x64\x65\x66" +
|
||||
"\x67\x68\x69\x6A\x73\x74\x75\x76\x77\x78\x79\x7A\x82\x83\x84\x85" +
|
||||
"\x86\x87\x88\x89\x8A\x92\x93\x94\x95\x96\x97\x98\x99\x9A\xA2\xA3" +
|
||||
"\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA" +
|
||||
"\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2\xD3\xD4\xD5\xD6\xD7\xD8" +
|
||||
"\xD9\xDA\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xF2\xF3\xF4\xF5\xF6" +
|
||||
"\xF7\xF8\xF9\xFA"
|
||||
|
||||
// huffmanBitWriter is a Huffman code LUT (Look-Up Table) for encoding.
|
||||
//
|
||||
// huffmanBitWriter[i] is the Huffman encoding for the value i: two uint16
|
||||
// halves packed as a uint32. The low half holds the bitstring bits. The high
|
||||
// half holds the bitstring length. Bitstrings are also known as code words.
|
||||
//
|
||||
// For example, huffmanBitWriters[0][5] being 0x00030006 means the Luma DC
|
||||
// value 5 has the bitstring "110".
|
||||
type huffmanBitWriter [256]uint32
|
||||
|
||||
var huffmanBitWriters = [4]huffmanBitWriter{{
|
||||
0x00020000, 0x00030002, 0x00030003, 0x00030004, 0x00030005, 0x00030006, 0x0004000E, 0x0005001E,
|
||||
0x0006003E, 0x0007007E, 0x000800FE, 0x000901FE, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
}, {
|
||||
0x0004000A, 0x00020000, 0x00020001, 0x00030004, 0x0004000B, 0x0005001A, 0x00070078, 0x000800F8,
|
||||
0x000A03F6, 0x0010FF82, 0x0010FF83, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0004000C, 0x0005001B, 0x00070079, 0x000901F6, 0x000B07F6, 0x0010FF84, 0x0010FF85,
|
||||
0x0010FF86, 0x0010FF87, 0x0010FF88, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0005001C, 0x000800F9, 0x000A03F7, 0x000C0FF4, 0x0010FF89, 0x0010FF8A, 0x0010FF8B,
|
||||
0x0010FF8C, 0x0010FF8D, 0x0010FF8E, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0006003A, 0x000901F7, 0x000C0FF5, 0x0010FF8F, 0x0010FF90, 0x0010FF91, 0x0010FF92,
|
||||
0x0010FF93, 0x0010FF94, 0x0010FF95, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0006003B, 0x000A03F8, 0x0010FF96, 0x0010FF97, 0x0010FF98, 0x0010FF99, 0x0010FF9A,
|
||||
0x0010FF9B, 0x0010FF9C, 0x0010FF9D, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0007007A, 0x000B07F7, 0x0010FF9E, 0x0010FF9F, 0x0010FFA0, 0x0010FFA1, 0x0010FFA2,
|
||||
0x0010FFA3, 0x0010FFA4, 0x0010FFA5, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0007007B, 0x000C0FF6, 0x0010FFA6, 0x0010FFA7, 0x0010FFA8, 0x0010FFA9, 0x0010FFAA,
|
||||
0x0010FFAB, 0x0010FFAC, 0x0010FFAD, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000800FA, 0x000C0FF7, 0x0010FFAE, 0x0010FFAF, 0x0010FFB0, 0x0010FFB1, 0x0010FFB2,
|
||||
0x0010FFB3, 0x0010FFB4, 0x0010FFB5, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901F8, 0x000F7FC0, 0x0010FFB6, 0x0010FFB7, 0x0010FFB8, 0x0010FFB9, 0x0010FFBA,
|
||||
0x0010FFBB, 0x0010FFBC, 0x0010FFBD, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901F9, 0x0010FFBE, 0x0010FFBF, 0x0010FFC0, 0x0010FFC1, 0x0010FFC2, 0x0010FFC3,
|
||||
0x0010FFC4, 0x0010FFC5, 0x0010FFC6, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901FA, 0x0010FFC7, 0x0010FFC8, 0x0010FFC9, 0x0010FFCA, 0x0010FFCB, 0x0010FFCC,
|
||||
0x0010FFCD, 0x0010FFCE, 0x0010FFCF, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000A03F9, 0x0010FFD0, 0x0010FFD1, 0x0010FFD2, 0x0010FFD3, 0x0010FFD4, 0x0010FFD5,
|
||||
0x0010FFD6, 0x0010FFD7, 0x0010FFD8, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000A03FA, 0x0010FFD9, 0x0010FFDA, 0x0010FFDB, 0x0010FFDC, 0x0010FFDD, 0x0010FFDE,
|
||||
0x0010FFDF, 0x0010FFE0, 0x0010FFE1, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000B07F8, 0x0010FFE2, 0x0010FFE3, 0x0010FFE4, 0x0010FFE5, 0x0010FFE6, 0x0010FFE7,
|
||||
0x0010FFE8, 0x0010FFE9, 0x0010FFEA, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0010FFEB, 0x0010FFEC, 0x0010FFED, 0x0010FFEE, 0x0010FFEF, 0x0010FFF0, 0x0010FFF1,
|
||||
0x0010FFF2, 0x0010FFF3, 0x0010FFF4, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x000B07F9, 0x0010FFF5, 0x0010FFF6, 0x0010FFF7, 0x0010FFF8, 0x0010FFF9, 0x0010FFFA, 0x0010FFFB,
|
||||
0x0010FFFC, 0x0010FFFD, 0x0010FFFE, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
}, {
|
||||
0x00020000, 0x00020001, 0x00020002, 0x00030006, 0x0004000E, 0x0005001E, 0x0006003E, 0x0007007E,
|
||||
0x000800FE, 0x000901FE, 0x000A03FE, 0x000B07FE, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
}, {
|
||||
0x00020000, 0x00020001, 0x00030004, 0x0004000A, 0x00050018, 0x00050019, 0x00060038, 0x00070078,
|
||||
0x000901F4, 0x000A03F6, 0x000C0FF4, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0004000B, 0x00060039, 0x000800F6, 0x000901F5, 0x000B07F6, 0x000C0FF5, 0x0010FF88,
|
||||
0x0010FF89, 0x0010FF8A, 0x0010FF8B, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0005001A, 0x000800F7, 0x000A03F7, 0x000C0FF6, 0x000F7FC2, 0x0010FF8C, 0x0010FF8D,
|
||||
0x0010FF8E, 0x0010FF8F, 0x0010FF90, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0005001B, 0x000800F8, 0x000A03F8, 0x000C0FF7, 0x0010FF91, 0x0010FF92, 0x0010FF93,
|
||||
0x0010FF94, 0x0010FF95, 0x0010FF96, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0006003A, 0x000901F6, 0x0010FF97, 0x0010FF98, 0x0010FF99, 0x0010FF9A, 0x0010FF9B,
|
||||
0x0010FF9C, 0x0010FF9D, 0x0010FF9E, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0006003B, 0x000A03F9, 0x0010FF9F, 0x0010FFA0, 0x0010FFA1, 0x0010FFA2, 0x0010FFA3,
|
||||
0x0010FFA4, 0x0010FFA5, 0x0010FFA6, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00070079, 0x000B07F7, 0x0010FFA7, 0x0010FFA8, 0x0010FFA9, 0x0010FFAA, 0x0010FFAB,
|
||||
0x0010FFAC, 0x0010FFAD, 0x0010FFAE, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x0007007A, 0x000B07F8, 0x0010FFAF, 0x0010FFB0, 0x0010FFB1, 0x0010FFB2, 0x0010FFB3,
|
||||
0x0010FFB4, 0x0010FFB5, 0x0010FFB6, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000800F9, 0x0010FFB7, 0x0010FFB8, 0x0010FFB9, 0x0010FFBA, 0x0010FFBB, 0x0010FFBC,
|
||||
0x0010FFBD, 0x0010FFBE, 0x0010FFBF, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901F7, 0x0010FFC0, 0x0010FFC1, 0x0010FFC2, 0x0010FFC3, 0x0010FFC4, 0x0010FFC5,
|
||||
0x0010FFC6, 0x0010FFC7, 0x0010FFC8, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901F8, 0x0010FFC9, 0x0010FFCA, 0x0010FFCB, 0x0010FFCC, 0x0010FFCD, 0x0010FFCE,
|
||||
0x0010FFCF, 0x0010FFD0, 0x0010FFD1, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901F9, 0x0010FFD2, 0x0010FFD3, 0x0010FFD4, 0x0010FFD5, 0x0010FFD6, 0x0010FFD7,
|
||||
0x0010FFD8, 0x0010FFD9, 0x0010FFDA, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000901FA, 0x0010FFDB, 0x0010FFDC, 0x0010FFDD, 0x0010FFDE, 0x0010FFDF, 0x0010FFE0,
|
||||
0x0010FFE1, 0x0010FFE2, 0x0010FFE3, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000B07F9, 0x0010FFE4, 0x0010FFE5, 0x0010FFE6, 0x0010FFE7, 0x0010FFE8, 0x0010FFE9,
|
||||
0x0010FFEA, 0x0010FFEB, 0x0010FFEC, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x000E3FE0, 0x0010FFED, 0x0010FFEE, 0x0010FFEF, 0x0010FFF0, 0x0010FFF1, 0x0010FFF2,
|
||||
0x0010FFF3, 0x0010FFF4, 0x0010FFF5, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x000A03FA, 0x000F7FC3, 0x0010FFF6, 0x0010FFF7, 0x0010FFF8, 0x0010FFF9, 0x0010FFFA, 0x0010FFFB,
|
||||
0x0010FFFC, 0x0010FFFD, 0x0010FFFE, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
}}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/google/wuffs/lib/lowleveljpeg"
|
||||
)
|
||||
|
||||
// Example_Basic demonstrates creating an opaque RGBA image and saving it as a
|
||||
// JPEG. It uses 4:2:0 chroma subsampling, the most popular choice for colorful
|
||||
// (not gray) JPEG images.
|
||||
func Example_Basic() {
|
||||
const width, height = 40, 30
|
||||
src := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
|
||||
// Fill src with a red/blue gradient.
|
||||
for y := 0; y < height; y++ {
|
||||
blue := uint8((float64(y) * 0xFF) / (height - 1))
|
||||
for x := 0; x < width; x++ {
|
||||
red := uint8((float64(x) * 0xFF) / (width - 1))
|
||||
src.SetRGBA(x, y, color.RGBA{red, 0x00, blue, 0xFF})
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the encoder.
|
||||
bounds := src.Bounds()
|
||||
colorType := lowleveljpeg.ColorTypeYCbCr420
|
||||
buf := &bytes.Buffer{}
|
||||
enc := lowleveljpeg.Encoder{}
|
||||
if err := enc.Reset(buf, colorType, bounds.Dx(), bounds.Dy(), nil); err != nil {
|
||||
log.Fatalf("enc.Reset: %v", err)
|
||||
}
|
||||
|
||||
// Extract, DCT-transform and add each MCU (Mininum Coded Unit). For 4:2:0
|
||||
// Chroma subsampling, each MCU is 16×16 and contains 6 blocks: 4 Y, 1 Cb,
|
||||
// 1 Cr.
|
||||
dx, dy := colorType.MCUDimensions()
|
||||
fmt.Printf("MCU Dimensions: %d, %d.\n", dx, dy)
|
||||
srcU8s := lowleveljpeg.Array6BlockU8{}
|
||||
srcI16s := lowleveljpeg.Array6BlockI16{}
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += dy {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += dx {
|
||||
srcU8s.ExtractFrom(src, x, y)
|
||||
srcI16s.ForwardDCTFrom(&srcU8s)
|
||||
if err := enc.Add6(buf, &srcI16s); err != nil {
|
||||
log.Fatalf("enc.Add6: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Change writeTmpFiles to true if you want to visually inspect the output.
|
||||
const writeTmpFiles = false
|
||||
const dstFilename = "/tmp/lowleveljpeg_example_basic.jpeg"
|
||||
if writeTmpFiles {
|
||||
if err := os.WriteFile(dstFilename, buf.Bytes(), 0644); err != nil {
|
||||
log.Fatalf("os.WriteFile: %v", err)
|
||||
}
|
||||
fmt.Println("Wrote ", dstFilename)
|
||||
} else {
|
||||
fmt.Println("Skipped", dstFilename)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// MCU Dimensions: 16, 16.
|
||||
// Skipped /tmp/lowleveljpeg_example_basic.jpeg
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/google/wuffs/lib/lowleveljpeg"
|
||||
)
|
||||
|
||||
// Example_YCbCr444 demonstrates loading an RGB PNG, converting to YCbCr and to
|
||||
// DCT space and saving as a 4:4:4 JPEG. Multiple JPEG outputs from the one PNG
|
||||
// input demonstrate making further transforms during conversion.
|
||||
func Example_YCbCr444() {
|
||||
// levels holds a rough, ad hoc measure of a coefficient's frequency, in
|
||||
// DCT space. Low (or high) levels[i] mean low (or high) frequency.
|
||||
//
|
||||
// levels[0] == 0 is the DC coefficient. The others are AC coefficients.
|
||||
levels := [64]uint8{
|
||||
0, 1, 2, 3, 4, 5, 6, 7,
|
||||
1, 1, 2, 3, 4, 5, 6, 7,
|
||||
2, 2, 2, 3, 4, 5, 6, 7,
|
||||
3, 3, 3, 3, 4, 5, 6, 7,
|
||||
4, 4, 4, 4, 4, 5, 6, 7,
|
||||
5, 5, 5, 5, 5, 5, 6, 7,
|
||||
6, 6, 6, 6, 6, 6, 6, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
}
|
||||
|
||||
// Load the source PNG. We will make numerous JPEG encodings of transformed
|
||||
// versions of this PNG image.
|
||||
f, err := os.Open("../../test/data/hippopotamus.regular.png")
|
||||
if err != nil {
|
||||
log.Fatalf("os.Open: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
src, err := png.Decode(f)
|
||||
if err != nil {
|
||||
log.Fatalf("png.Decode: %v", err)
|
||||
}
|
||||
|
||||
// Each example (each output JPEG) has a name fragment and a transform from
|
||||
// source BlockU8 values to destination BlockI16 values.
|
||||
examples := []struct {
|
||||
nameFragment string
|
||||
quality int
|
||||
transform func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8)
|
||||
}{{
|
||||
|
||||
// Apply the ForwardDCT with no further changes. The resultant JPEG is
|
||||
// a low quality (but small in file size) version of the source PNG.
|
||||
nameFragment: "0_quality25",
|
||||
quality: 25,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0].ForwardDCTFrom(&src[0])
|
||||
dst[1].ForwardDCTFrom(&src[1])
|
||||
dst[2].ForwardDCTFrom(&src[2])
|
||||
},
|
||||
}, {
|
||||
|
||||
// Ditto, with default quality.
|
||||
nameFragment: "1_quality75",
|
||||
quality: 75,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0].ForwardDCTFrom(&src[0])
|
||||
dst[1].ForwardDCTFrom(&src[1])
|
||||
dst[2].ForwardDCTFrom(&src[2])
|
||||
},
|
||||
}, {
|
||||
|
||||
// Ditto, with high quality.
|
||||
nameFragment: "2_quality100",
|
||||
quality: 100,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0].ForwardDCTFrom(&src[0])
|
||||
dst[1].ForwardDCTFrom(&src[1])
|
||||
dst[2].ForwardDCTFrom(&src[2])
|
||||
},
|
||||
}, {
|
||||
|
||||
// Like 2_quality100 but swap the Chroma-blue and Chroma-red channels.
|
||||
nameFragment: "3_swap_cb_cr",
|
||||
quality: 100,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0].ForwardDCTFrom(&src[0])
|
||||
dst[1].ForwardDCTFrom(&src[2]) // Note the "1" and "2".
|
||||
dst[2].ForwardDCTFrom(&src[1]) // Note the "2" and "1".
|
||||
},
|
||||
}, {
|
||||
|
||||
// Set the 8×8 blocks of the Luma channel to neutral (0x80) except for
|
||||
// each top-left corner, which is black (0x00). Chromas are unchanged.
|
||||
nameFragment: "4_chroma_only",
|
||||
quality: 100,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
s0 := lowleveljpeg.BlockU8{}
|
||||
s0[0] = 0x00
|
||||
for i := 1; i < 64; i++ {
|
||||
s0[i] = lowleveljpeg.BlockU8NeutralValue
|
||||
}
|
||||
dst[0].ForwardDCTFrom(&s0)
|
||||
dst[1].ForwardDCTFrom(&src[1])
|
||||
dst[2].ForwardDCTFrom(&src[2])
|
||||
},
|
||||
}, {
|
||||
|
||||
// Set the Chroma channels to neutral, as well as any Luma coefficients
|
||||
// that have any horizontal variation (within an 8×8 block). What
|
||||
// remains are the DC and vertical-only Luma coefficients.
|
||||
nameFragment: "5_vertical_luma",
|
||||
quality: 100,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0].ForwardDCTFrom(&src[0])
|
||||
for i := 0; i < 64; i++ {
|
||||
if (i & 7) != 0 {
|
||||
dst[0][i] = lowleveljpeg.BlockI16NeutralValue
|
||||
}
|
||||
}
|
||||
dst[1] = lowleveljpeg.BlockI16{}
|
||||
dst[2] = lowleveljpeg.BlockI16{}
|
||||
},
|
||||
}, {
|
||||
|
||||
// Set the Chroma channels to neutral, as well as any medium- or
|
||||
// high-frequency Luma coefficients.
|
||||
nameFragment: "6_low_frequency_luma",
|
||||
quality: 100,
|
||||
transform: func(dst *lowleveljpeg.Array3BlockI16, src *lowleveljpeg.Array3BlockU8) {
|
||||
dst[0] = src[0].ForwardDCT()
|
||||
for i := 0; i < 64; i++ {
|
||||
if levels[i] > 2 {
|
||||
dst[0][i] = 0
|
||||
}
|
||||
}
|
||||
dst[1] = lowleveljpeg.BlockI16{}
|
||||
dst[2] = lowleveljpeg.BlockI16{}
|
||||
},
|
||||
}}
|
||||
|
||||
// Process each example.
|
||||
for _, example := range examples {
|
||||
// Initialize the encoder.
|
||||
quants := lowleveljpeg.Array2QuantizationFactors{}
|
||||
quants.SetToStandardValues(example.quality)
|
||||
opts := &lowleveljpeg.EncoderOptions{
|
||||
QuantizationFactors: &quants,
|
||||
}
|
||||
bounds := src.Bounds()
|
||||
buf := &bytes.Buffer{}
|
||||
enc := lowleveljpeg.Encoder{}
|
||||
if err := enc.Reset(buf, lowleveljpeg.ColorTypeYCbCr444, bounds.Dx(), bounds.Dy(), opts); err != nil {
|
||||
log.Fatalf("enc.Reset: %v", err)
|
||||
}
|
||||
|
||||
// Extract, transform and add each MCU (Mininum Coded Unit). For 4:4:4
|
||||
// Chroma subsampling, each MCU is 8×8 and contains 3 blocks: 1 Y, 1
|
||||
// Cb, 1 Cr.
|
||||
srcU8s := lowleveljpeg.Array3BlockU8{}
|
||||
srcI16s := lowleveljpeg.Array3BlockI16{}
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += 8 {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += 8 {
|
||||
srcU8s.ExtractFrom(src, x, y)
|
||||
example.transform(&srcI16s, &srcU8s)
|
||||
if err := enc.Add3(buf, &srcI16s); err != nil {
|
||||
log.Fatalf("enc.Add3: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// As a basic consistency check, confirm that the resultant bytes are a
|
||||
// valid JPEG. When copy/pasting this code, you can omit this step.
|
||||
dstBytes := buf.Bytes()
|
||||
if _, err := jpeg.Decode(bytes.NewReader(dstBytes)); err != nil {
|
||||
log.Fatalf("jpeg.Decode: %v", err)
|
||||
}
|
||||
|
||||
// Change writeTmpFiles to true if you want to visually inspect the output.
|
||||
const writeTmpFiles = false
|
||||
dstFilename := fmt.Sprintf("/tmp/lowleveljpeg_example_%s.jpeg", example.nameFragment)
|
||||
if writeTmpFiles {
|
||||
if err := os.WriteFile(dstFilename, dstBytes, 0644); err != nil {
|
||||
log.Fatalf("os.WriteFile: %v", err)
|
||||
}
|
||||
fmt.Println("Wrote ", dstFilename)
|
||||
} else {
|
||||
fmt.Println("Skipped", dstFilename)
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Skipped /tmp/lowleveljpeg_example_0_quality25.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_1_quality75.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_2_quality100.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_3_swap_cb_cr.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_4_chroma_only.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_5_vertical_luma.jpeg
|
||||
// Skipped /tmp/lowleveljpeg_example_6_low_frequency_luma.jpeg
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
// ExtractFrom sets dst to a single channel (Gray) 8×8 MCU (Minimum Coded
|
||||
// Unit), with the given top-left corner, from the image m.
|
||||
func (dst *Array1BlockU8) ExtractFrom(m image.Image, topLeftX int, topLeftY int) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
dst.SetToNeutral()
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
bounds := m.Bounds()
|
||||
mGray, _ := m.(*image.Gray)
|
||||
mRGBA64Image, _ := m.(image.RGBA64Image)
|
||||
|
||||
for dy := 0; dy < 8; dy++ {
|
||||
for dx := 0; dx < 8; dx++ {
|
||||
p := image.Point{topLeftX + dx, topLeftY + dy}
|
||||
if !p.In(bounds) {
|
||||
continue
|
||||
}
|
||||
i := (8 * dy) + dx
|
||||
|
||||
if mGray != nil {
|
||||
dst[0][i] = mGray.GrayAt(p.X, p.Y).Y
|
||||
|
||||
} else if mRGBA64Image != nil {
|
||||
pix := mRGBA64Image.RGBA64At(p.X, p.Y)
|
||||
r, g, b := uint32(pix.R), uint32(pix.G), uint32(pix.B)
|
||||
y := ((19595 * r) + (38470 * g) + (7471 * b) + (1 << 15)) >> 24
|
||||
dst[0][i] = uint8(y)
|
||||
|
||||
} else {
|
||||
r, g, b, _ := m.At(p.X, p.Y).RGBA()
|
||||
y := ((19595 * r) + (38470 * g) + (7471 * b) + (1 << 15)) >> 24
|
||||
dst[0][i] = uint8(y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fillRightAndDown(&dst[0], topLeftX, topLeftY, bounds.Max.X, bounds.Max.Y)
|
||||
}
|
||||
|
||||
// ExtractFrom sets dst to a three channel (Luma, Chroma-blue, Chroma-red) 8×8
|
||||
// 4:4:4 MCU (Minimum Coded Unit), with the given top-left corner, from the
|
||||
// image m.
|
||||
func (dst *Array3BlockU8) ExtractFrom(m image.Image, topLeftX int, topLeftY int) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
dst.SetToNeutral()
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
bounds := m.Bounds()
|
||||
mYCbCr, _ := m.(*image.YCbCr)
|
||||
mRGBA64Image, _ := m.(image.RGBA64Image)
|
||||
|
||||
for dy := 0; dy < 8; dy++ {
|
||||
for dx := 0; dx < 8; dx++ {
|
||||
p := image.Point{topLeftX + dx, topLeftY + dy}
|
||||
if !p.In(bounds) {
|
||||
continue
|
||||
}
|
||||
i := (8 * dy) + dx
|
||||
|
||||
if mYCbCr != nil {
|
||||
pix := mYCbCr.YCbCrAt(p.X, p.Y)
|
||||
dst[0][i] = pix.Y
|
||||
dst[1][i] = pix.Cb
|
||||
dst[2][i] = pix.Cr
|
||||
|
||||
} else if mRGBA64Image != nil {
|
||||
pix := mRGBA64Image.RGBA64At(p.X, p.Y)
|
||||
y, cb, cr := color.RGBToYCbCr(
|
||||
uint8(pix.R>>8),
|
||||
uint8(pix.G>>8),
|
||||
uint8(pix.B>>8),
|
||||
)
|
||||
dst[0][i] = y
|
||||
dst[1][i] = cb
|
||||
dst[2][i] = cr
|
||||
|
||||
} else {
|
||||
r, g, b, _ := m.At(p.X, p.Y).RGBA()
|
||||
y, cb, cr := color.RGBToYCbCr(
|
||||
uint8(r>>8),
|
||||
uint8(g>>8),
|
||||
uint8(b>>8),
|
||||
)
|
||||
dst[0][i] = y
|
||||
dst[1][i] = cb
|
||||
dst[2][i] = cr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fillRightAndDown(&dst[0], topLeftX, topLeftY, bounds.Max.X, bounds.Max.Y)
|
||||
fillRightAndDown(&dst[1], topLeftX, topLeftY, bounds.Max.X, bounds.Max.Y)
|
||||
fillRightAndDown(&dst[2], topLeftX, topLeftY, bounds.Max.X, bounds.Max.Y)
|
||||
}
|
||||
|
||||
// ExtractFrom sets dst to a three channel (Luma, Chroma-blue, Chroma-red)
|
||||
// 16×16 4:2:0 MCU (Minimum Coded Unit, with the given top-left corner, from
|
||||
// the image m.
|
||||
//
|
||||
// The 6 elements are:
|
||||
// - Luma top left
|
||||
// - Luma top right
|
||||
// - Luma bottom left
|
||||
// - Luma bottom right
|
||||
// - Chroma-blue
|
||||
// - Chroma-red
|
||||
func (dst *Array6BlockU8) ExtractFrom(m image.Image, topLeftX int, topLeftY int) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
if m == nil {
|
||||
dst.SetToNeutral()
|
||||
return
|
||||
}
|
||||
|
||||
tmp := [4]Array3BlockU8{}
|
||||
tmp[0].ExtractFrom(m, topLeftX+0, topLeftY+0)
|
||||
tmp[1].ExtractFrom(m, topLeftX+8, topLeftY+0)
|
||||
tmp[2].ExtractFrom(m, topLeftX+0, topLeftY+8)
|
||||
tmp[3].ExtractFrom(m, topLeftX+8, topLeftY+8)
|
||||
|
||||
dst[0] = tmp[0][0]
|
||||
dst[1] = tmp[1][0]
|
||||
dst[2] = tmp[2][0]
|
||||
dst[3] = tmp[3][0]
|
||||
|
||||
downsample8x8(&dst[4], &tmp[0][1], &tmp[1][1], &tmp[2][1], &tmp[3][1])
|
||||
downsample8x8(&dst[5], &tmp[0][2], &tmp[1][2], &tmp[2][2], &tmp[3][2])
|
||||
}
|
||||
|
||||
// fillRightAndDown copies the right and bottom edge values to the full 8×8 block.
|
||||
//
|
||||
// For 4:2:0, we call this before chroma downsampling (from 8×8 to 4×4) to
|
||||
// avoid artifacts from averaging with the neutral 0x80.
|
||||
//
|
||||
// For example, if mx = 3 and my = 6 then it updates b from this:
|
||||
//
|
||||
// AC A7 A3 80 80 80 80 80
|
||||
// B6 B1 AD 80 80 80 80 80
|
||||
// C0 BB B7 80 80 80 80 80
|
||||
// C9 C5 C0 80 80 80 80 80
|
||||
// D3 CF CA 80 80 80 80 80
|
||||
// DD D9 D4 80 80 80 80 80
|
||||
// 80 80 80 80 80 80 80 80
|
||||
// 80 80 80 80 80 80 80 80
|
||||
//
|
||||
// to this:
|
||||
//
|
||||
// AC A7 A3 A3 A3 A3 A3 A3
|
||||
// B6 B1 AD AD AD AD AD AD
|
||||
// C0 BB B7 B7 B7 B7 B7 B7
|
||||
// C9 C5 C0 C0 C0 C0 C0 C0
|
||||
// D3 CF CA CA CA CA CA CA
|
||||
// DD D9 D4 D4 D4 D4 D4 D4
|
||||
// DD D9 D4 D4 D4 D4 D4 D4
|
||||
// DD D9 D4 D4 D4 D4 D4 D4
|
||||
func fillRightAndDown(b *BlockU8, topLeftX int, topLeftY int, bottomRightX int, bottomRightY int) {
|
||||
if bottomRightX <= topLeftX {
|
||||
return
|
||||
}
|
||||
mx := bottomRightX - topLeftX
|
||||
if mx >= 8 {
|
||||
mx = 8
|
||||
}
|
||||
|
||||
if bottomRightY <= topLeftY {
|
||||
return
|
||||
}
|
||||
my := bottomRightY - topLeftY
|
||||
if my >= 8 {
|
||||
my = 8
|
||||
}
|
||||
|
||||
if mx < 8 {
|
||||
for y := 0; y < my; y++ {
|
||||
value := b[(8*y)|(mx-1)]
|
||||
for x := mx; x < 8; x++ {
|
||||
b[(8*y)|x] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if my < 8 {
|
||||
src := b[(8 * (my - 1)):(8 * (my - 0))]
|
||||
for y := my; y < 8; y++ {
|
||||
dst := b[(8 * (y + 0)):(8 * (y + 1))]
|
||||
copy(dst, src)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// downsample8x8 reduces four 8×8 blocks to one 8×8 block.
|
||||
func downsample8x8(dst *BlockU8, src0 *BlockU8, src1 *BlockU8, src2 *BlockU8, src3 *BlockU8) {
|
||||
downsample4x4(dst[0x00:], src0)
|
||||
downsample4x4(dst[0x04:], src1)
|
||||
downsample4x4(dst[0x20:], src2)
|
||||
downsample4x4(dst[0x24:], src3)
|
||||
}
|
||||
|
||||
// downsample4x4 reduces one 8×8 block to one 4×4 sub-block.
|
||||
func downsample4x4(dst []uint8, src *BlockU8) {
|
||||
for y := 0; y < 4; y++ {
|
||||
for x := 0; x < 4; x++ {
|
||||
d := (8 * y) + (1 * x)
|
||||
s := (16 * y) + (2 * x)
|
||||
dst[d] = uint8((2 +
|
||||
uint32(src[s+0]) +
|
||||
uint32(src[s+1]) +
|
||||
uint32(src[s+8]) +
|
||||
uint32(src[s+9])) / 4)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// ----------------
|
||||
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
w := &bytes.Buffer{}
|
||||
w.WriteString(header)
|
||||
|
||||
genHardCodedDHTSegments(w)
|
||||
w.WriteString("\n")
|
||||
genHuffmanBitWriters(w)
|
||||
|
||||
return os.WriteFile("data.go", w.Bytes(), 0644)
|
||||
}
|
||||
|
||||
func fillInPlaceholderPayloadLength(payload []byte) {
|
||||
n := len(payload) - 2 // Minus 2 for the JPEG marker.
|
||||
payload[2] = uint8(n >> 8)
|
||||
payload[3] = uint8(n >> 0)
|
||||
}
|
||||
|
||||
func genConstStringData(w *bytes.Buffer, contents []byte) {
|
||||
for {
|
||||
prefix := contents
|
||||
if len(prefix) > 16 {
|
||||
prefix, contents = contents[:16], contents[16:]
|
||||
} else {
|
||||
contents = nil
|
||||
}
|
||||
|
||||
w.WriteString("\t\"")
|
||||
for _, v := range prefix {
|
||||
fmt.Fprintf(w, "\\x%02X", v)
|
||||
}
|
||||
w.WriteString("\"")
|
||||
|
||||
if len(contents) == 0 {
|
||||
break
|
||||
}
|
||||
w.WriteString(" +\n")
|
||||
}
|
||||
}
|
||||
|
||||
func genHardCodedDHTSegments(w *bytes.Buffer) {
|
||||
w.WriteString("const hardCodedDHTSegments = \"\" +\n")
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
contents := append([]byte(nil),
|
||||
0xFF, 0xC4, // DHT marker.
|
||||
0x00, 0x00, // Placeholder payload length.
|
||||
)
|
||||
|
||||
if i == 0 {
|
||||
w.WriteString("\t// 212 bytes for the first DHT segment.\n")
|
||||
} else {
|
||||
w.WriteString(" +\n\t// 212 bytes for the second DHT segment.\n")
|
||||
}
|
||||
|
||||
contents = append(contents, 0x00|uint8(i)) // 0x00 means DC.
|
||||
contents = append(contents, theHuffmanSpec[(2*i)+0].counts[:]...)
|
||||
contents = append(contents, theHuffmanSpec[(2*i)+0].values...)
|
||||
contents = append(contents, 0x10|uint8(i)) // 0x10 means AC.
|
||||
contents = append(contents, theHuffmanSpec[(2*i)+1].counts[:]...)
|
||||
contents = append(contents, theHuffmanSpec[(2*i)+1].values...)
|
||||
|
||||
fillInPlaceholderPayloadLength(contents)
|
||||
genConstStringData(w, contents)
|
||||
}
|
||||
w.WriteString("\n")
|
||||
}
|
||||
|
||||
func genHuffmanBitWriters(w *bytes.Buffer) {
|
||||
w.WriteString("// huffmanBitWriter is a Huffman code LUT (Look-Up Table) for encoding.\n")
|
||||
w.WriteString("//\n")
|
||||
w.WriteString("// huffmanBitWriter[i] is the Huffman encoding for the value i: two uint16\n")
|
||||
w.WriteString("// halves packed as a uint32. The low half holds the bitstring bits. The high\n")
|
||||
w.WriteString("// half holds the bitstring length. Bitstrings are also known as code words.\n")
|
||||
w.WriteString("//\n")
|
||||
w.WriteString("// For example, huffmanBitWriters[0][5] being 0x00030006 means the Luma DC\n")
|
||||
w.WriteString("// value 5 has the bitstring \"110\".\n")
|
||||
w.WriteString("type huffmanBitWriter [256]uint32\n\n")
|
||||
|
||||
w.WriteString("var huffmanBitWriters = [4]huffmanBitWriter{{\n")
|
||||
|
||||
for i := range theHuffmanSpec {
|
||||
if i != 0 {
|
||||
w.WriteString("}, {\n")
|
||||
}
|
||||
|
||||
hbw := huffmanBitWriter{}
|
||||
hbw.initialize(&theHuffmanSpec[i])
|
||||
|
||||
for i, v := range hbw {
|
||||
if (i & 7) == 0 {
|
||||
w.WriteString("\t")
|
||||
}
|
||||
fmt.Fprintf(w, "0x%08X,", v)
|
||||
if (i & 7) == 7 {
|
||||
w.WriteString("\n")
|
||||
} else {
|
||||
w.WriteString(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteString("}}\n")
|
||||
}
|
||||
|
||||
// huffmanSpec specifies a Huffman encoding.
|
||||
type huffmanSpec struct {
|
||||
// counts[i] is the number of code words of length i+1 bits.
|
||||
counts [16]byte
|
||||
// values[i] is the decoded value of the i'th code word.
|
||||
values []byte
|
||||
}
|
||||
|
||||
// theHuffmanSpec corresponds to Section K.3 "Typical Huffman tables for 8-bit
|
||||
// precision luminance and chrominance" of the JPEG spec.
|
||||
//
|
||||
// Each Huffman code is deliberately incomplete. No code word (also known as a
|
||||
// bitstring) is all-1-bits, since that would interact poorly with JPEG's byte
|
||||
// stuffing.
|
||||
//
|
||||
// See the JPEG spec (https://www.w3.org/Graphics/JPEG/itu-t81.pdf) Section C:
|
||||
// "the codes shall be generated such that the all-1-bits code word of any
|
||||
// length is reserved".
|
||||
var theHuffmanSpec = [4]huffmanSpec{{
|
||||
// Luma DC. 12 elements.
|
||||
//
|
||||
// See the JPEG spec, Table K.3.
|
||||
//
|
||||
// value : bitstring : len(bitstring)
|
||||
// 0x00 : 00 : 2
|
||||
// 0x01 : 010 : 3
|
||||
// 0x02 : 011 : 3
|
||||
// 0x03 : 100 : 3
|
||||
// 0x04 : 101 : 3
|
||||
// 0x05 : 110 : 3
|
||||
// 0x06 : 1110 : 4
|
||||
// 0x07 : 11110 : 5
|
||||
// 0x08 : 111110 : 6
|
||||
// 0x09 : 1111110 : 7
|
||||
// 0x0A : 11111110 : 8
|
||||
// 0x0B : 111111110 : 9
|
||||
[16]byte{0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
|
||||
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
}, {
|
||||
|
||||
// Luma AC. 162 elements.
|
||||
//
|
||||
// See the JPEG spec, Table K.5.
|
||||
//
|
||||
// value : bitstring : len(bitstring)
|
||||
// 0x01 : 00 : 2
|
||||
// 0x02 : 01 : 2
|
||||
// 0x03 : 100 : 3
|
||||
// 0x00 : 1010 : 4
|
||||
// 0x04 : 1011 : 4
|
||||
// 0x11 : 1100 : 4
|
||||
// 0x05 : 11010 : 5
|
||||
// etc : etc : etc
|
||||
[16]byte{0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125},
|
||||
[]byte{
|
||||
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,
|
||||
},
|
||||
}, {
|
||||
|
||||
// Chroma DC. 12 elements.
|
||||
//
|
||||
// See the JPEG spec, Table K.4.
|
||||
[16]byte{0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
|
||||
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
||||
}, {
|
||||
|
||||
// Chroma AC. 162 elements.
|
||||
//
|
||||
// See the JPEG spec, Table K.6.
|
||||
[16]byte{0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119},
|
||||
[]byte{
|
||||
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,
|
||||
},
|
||||
}}
|
||||
|
||||
// huffmanBitWriter is a Huffman code LUT (Look-Up Table) for encoding.
|
||||
//
|
||||
// huffmanBitWriter[i] is the Huffman encoding for the value i: two uint16
|
||||
// halves packed as a uint32. The low half holds the bitstring bits. The high
|
||||
// half holds the bitstring length. Bitstrings are also known as code words.
|
||||
type huffmanBitWriter [256]uint32
|
||||
|
||||
func (b *huffmanBitWriter) initialize(s *huffmanSpec) {
|
||||
bits, k := uint32(0), 0
|
||||
for i, count := range s.counts {
|
||||
length16 := uint32(i+1) << 16
|
||||
for j := uint8(0); j < count; j++ {
|
||||
if bits > 0xFFFF {
|
||||
panic("bits overflowed")
|
||||
}
|
||||
b[s.values[k]] = length16 | bits
|
||||
bits++
|
||||
k++
|
||||
}
|
||||
bits <<= 1
|
||||
}
|
||||
}
|
||||
|
||||
const header = `// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
`
|
||||
@@ -0,0 +1,575 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// ----------------
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
// Package lowleveljpeg provides a JPEG encoder that takes in-DCT-space
|
||||
// coefficients instead of in-XY-space pixels.
|
||||
//
|
||||
// It can be useful for experimenting with or visualizing in-DCT-space
|
||||
// transformations, such as quantization, extrapolation and progressive-like
|
||||
// reduction. Or for learning about how the JPEG file format works. But most
|
||||
// programmers should use the Go standard library's image/jpeg package instead.
|
||||
package lowleveljpeg
|
||||
|
||||
// See "the spec", also known as "the JPEG specification" or "ITU/CCITT T.81":
|
||||
// https://www.w3.org/Graphics/JPEG/itu-t81.pdf
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBadAddNForColorType = errors.New("lowleveljpeg: bad AddN for ColorType")
|
||||
ErrBadArgument = errors.New("lowleveljpeg: bad argument")
|
||||
ErrInvalidBlockI16 = errors.New("lowleveljpeg: invalid BlockI16")
|
||||
ErrNilReceiver = errors.New("lowleveljpeg: nil receiver")
|
||||
ErrPreviouslyReturnedError = errors.New("lowleveljpeg: previously returned error")
|
||||
ErrTooManyAddNCalls = errors.New("lowleveljpeg: too many AddN calls")
|
||||
)
|
||||
|
||||
// ColorType is a JPEG image's color type.
|
||||
type ColorType byte
|
||||
|
||||
const (
|
||||
// ColorTypeInvalid, the zero value, is invalid.
|
||||
ColorTypeInvalid = ColorType(0)
|
||||
|
||||
// ColorTypeGray means a single channel (Gray).
|
||||
ColorTypeGray = ColorType(1)
|
||||
|
||||
// ColorTypeYCbCr444 means three channels (Luma, Chroma-blue, Chroma-red),
|
||||
// using 4:4:4 Chroma subsampling.
|
||||
ColorTypeYCbCr444 = ColorType(3)
|
||||
|
||||
// ColorTypeYCbCr420 means three channels (Luma, Chroma-blue, Chroma-red),
|
||||
// using 4:2:0 Chroma subsampling.
|
||||
//
|
||||
// This is the most popular color type for colorful (not gray) JPEG images.
|
||||
ColorTypeYCbCr420 = ColorType(6)
|
||||
|
||||
// As an implementation detail, the constants' numerical value is the
|
||||
// number of blocks in a JPEG MCU (Mininum Coded Unit).
|
||||
)
|
||||
|
||||
// MCUDimensions returns the width and height of a Mininum Coded Unit.
|
||||
func (c ColorType) MCUDimensions() (width int, height int) {
|
||||
switch c {
|
||||
case ColorTypeGray, ColorTypeYCbCr444:
|
||||
return 8, 8
|
||||
case ColorTypeYCbCr420:
|
||||
return 16, 16
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
func (c ColorType) isValid() bool {
|
||||
switch c {
|
||||
case ColorTypeGray, ColorTypeYCbCr444, ColorTypeYCbCr420:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// EncoderOptions are optional arguments to Encoder.Reset. The zero value is
|
||||
// valid and means to use the default configuration.
|
||||
type EncoderOptions struct {
|
||||
// QuantizationFactors are the BlockI16 quantization factors. A nil pointer
|
||||
// is equivalent to using DefaultQuality.
|
||||
QuantizationFactors *Array2QuantizationFactors
|
||||
|
||||
// There may be other fields added in the future.
|
||||
}
|
||||
|
||||
// Encoder writes JPEG-formatted bytes to an io.Writer.
|
||||
//
|
||||
// Call Reset and then AddN multiple times (N depends on the ColorType). Each
|
||||
// AddN call encodes a Minimum Coded Unit (an 8×8 or 16×16 group of pixels,
|
||||
// depending on the ColorType). Add MCUs in left-to-right top-to-bottom order.
|
||||
// The final AddN call also writes the JPEG footer.
|
||||
//
|
||||
// AddN takes ArrayNBlockI16 arguments, which are post-FDCT (when encoding) or
|
||||
// pre-IDCT (when decoding) coefficients.
|
||||
//
|
||||
// To get ArrayNBlockI16 values from pixels, use ExtractFrom on an image.Image
|
||||
// (to get an array of BlockU8 values) and then FowardDCTFrom (to get an array
|
||||
// of BlockI16 values). See Example_Basic for an example.
|
||||
//
|
||||
// An Encoder makes no allocations, other than the Encoder struct itself and
|
||||
// any allocations that the passed io.Writer makes.
|
||||
//
|
||||
// An Encoder can be re-used, by calling Reset.
|
||||
type Encoder struct {
|
||||
hasReturnedError bool
|
||||
|
||||
colorType ColorType
|
||||
|
||||
prevDC [3]int16
|
||||
|
||||
numAddsRemaining uint32
|
||||
|
||||
bitsV uint32
|
||||
bitsN uint32
|
||||
|
||||
quants Array2QuantizationFactors
|
||||
|
||||
// The fields above occupy 148 bytes. Each Reset or AddN call, in the worst
|
||||
// case, emits not much more than 2688 bytes. We round sizeof(Encoder) up
|
||||
// to be 3072, which is 1.5 times a power of 2.
|
||||
|
||||
buf [3072 - 148]byte
|
||||
}
|
||||
|
||||
// Reset makes an Encoder ready to use (ready to make AddN calls), for encoding
|
||||
// a JPEG with the given colorType, width and height.
|
||||
//
|
||||
// colorType should be ColorTypeGray, ColorTypeYCbCr444 or ColorTypeYCbCr420.
|
||||
//
|
||||
// width and height should both be positive but no larger than 0xFFFF, since a
|
||||
// JPEG image cannot represent anything larger.
|
||||
//
|
||||
// options may be nil, which means to use the default configuration.
|
||||
func (e *Encoder) Reset(w io.Writer, colorType ColorType, width int, height int, options *EncoderOptions) error {
|
||||
if e == nil {
|
||||
return ErrNilReceiver
|
||||
} else if (width <= 0) || (0xFFFF < width) || (height <= 0) || (0xFFFF < height) || !colorType.isValid() {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadArgument
|
||||
}
|
||||
|
||||
if (options == nil) || (options.QuantizationFactors == nil) {
|
||||
e.quants.SetToStandardValues(DefaultQuality)
|
||||
} else {
|
||||
q := options.QuantizationFactors
|
||||
if !q[0].IsValid() || !q[1].IsValid() {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadArgument
|
||||
}
|
||||
e.quants = *q
|
||||
}
|
||||
|
||||
e.hasReturnedError = false
|
||||
e.colorType = colorType
|
||||
e.prevDC[0] = 0
|
||||
e.prevDC[1] = 0
|
||||
e.prevDC[2] = 0
|
||||
if colorType != ColorTypeYCbCr420 {
|
||||
e.numAddsRemaining = uint32((width+7)/8) * uint32((height+7)/8)
|
||||
} else {
|
||||
e.numAddsRemaining = uint32((width+15)/16) * uint32((height+15)/16)
|
||||
}
|
||||
e.bitsV = 0
|
||||
e.bitsN = 0
|
||||
|
||||
e.buf[0] = 0xFF // SOI marker.
|
||||
e.buf[1] = 0xD8
|
||||
bufIndex := 2
|
||||
bufIndex = e.encodeDQT(bufIndex)
|
||||
bufIndex = e.encodeSOF0(bufIndex, width, height)
|
||||
bufIndex = e.encodeDHT(bufIndex)
|
||||
bufIndex = e.encodeSOSHeader(bufIndex)
|
||||
if _, err := w.Write(e.buf[:bufIndex]); err != nil {
|
||||
e.hasReturnedError = true
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add1 adds an 8×8 group of pixels to a ColorTypeGray image.
|
||||
func (e *Encoder) Add1(w io.Writer, b *Array1BlockI16) error {
|
||||
if e == nil {
|
||||
return ErrNilReceiver
|
||||
} else if e.hasReturnedError {
|
||||
return ErrPreviouslyReturnedError
|
||||
} else if e.colorType != ColorTypeGray {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadAddNForColorType
|
||||
} else if b == nil {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadArgument
|
||||
}
|
||||
return e.addN(w, b[:])
|
||||
}
|
||||
|
||||
// Add3 adds an 8×8 group of pixels to a ColorTypeYCbCr444 image.
|
||||
func (e *Encoder) Add3(w io.Writer, b *Array3BlockI16) error {
|
||||
if e == nil {
|
||||
return ErrNilReceiver
|
||||
} else if e.hasReturnedError {
|
||||
return ErrPreviouslyReturnedError
|
||||
} else if e.colorType != ColorTypeYCbCr444 {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadAddNForColorType
|
||||
} else if b == nil {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadArgument
|
||||
}
|
||||
return e.addN(w, b[:])
|
||||
}
|
||||
|
||||
// Add6 adds a 16×16 group of pixels to a ColorTypeYCbCr420 image.
|
||||
func (e *Encoder) Add6(w io.Writer, b *Array6BlockI16) error {
|
||||
if e == nil {
|
||||
return ErrNilReceiver
|
||||
} else if e.hasReturnedError {
|
||||
return ErrPreviouslyReturnedError
|
||||
} else if e.colorType != ColorTypeYCbCr420 {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadAddNForColorType
|
||||
} else if b == nil {
|
||||
e.hasReturnedError = true
|
||||
return ErrBadArgument
|
||||
}
|
||||
return e.addN(w, b[:])
|
||||
}
|
||||
|
||||
func (e *Encoder) addN(w io.Writer, blocks []BlockI16) error {
|
||||
for i := range blocks {
|
||||
if !blocks[i].IsValid() {
|
||||
e.hasReturnedError = true
|
||||
return ErrInvalidBlockI16
|
||||
}
|
||||
}
|
||||
if e.numAddsRemaining <= 0 {
|
||||
e.hasReturnedError = true
|
||||
return ErrTooManyAddNCalls
|
||||
}
|
||||
e.numAddsRemaining--
|
||||
|
||||
// In terms of worst case number of bytes emitted, the up-to-six
|
||||
// encodeBlock calls can emit 2688 bytes (6 * 448 bytes).
|
||||
|
||||
whichComponents := ""
|
||||
switch ColorType(len(blocks)) {
|
||||
case ColorTypeGray:
|
||||
whichComponents = "\x00"
|
||||
case ColorTypeYCbCr444:
|
||||
whichComponents = "\x00\x01\x02"
|
||||
case ColorTypeYCbCr420:
|
||||
whichComponents = "\x00\x00\x00\x00\x01\x02"
|
||||
}
|
||||
|
||||
bufIndex := 0
|
||||
for i := range blocks {
|
||||
bufIndex = e.encodeBlock(bufIndex, whichComponents[i], &blocks[i])
|
||||
}
|
||||
|
||||
if e.numAddsRemaining == 0 {
|
||||
bufIndex = e.emitBits(bufIndex, 0x7F, 7)
|
||||
e.buf[bufIndex+0] = 0xFF // EOI marker.
|
||||
e.buf[bufIndex+1] = 0xD9
|
||||
bufIndex += 2
|
||||
}
|
||||
|
||||
if _, err := w.Write(e.buf[:bufIndex]); err != nil {
|
||||
e.hasReturnedError = true
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeDQT(bufIndex int) int {
|
||||
e.buf[bufIndex+0] = 0xFF
|
||||
e.buf[bufIndex+1] = 0xDB
|
||||
e.buf[bufIndex+2] = 0x00
|
||||
if e.colorType == ColorTypeGray {
|
||||
e.buf[bufIndex+3] = 0x43
|
||||
} else {
|
||||
e.buf[bufIndex+3] = 0x84
|
||||
}
|
||||
bufIndex += 4
|
||||
|
||||
for i := range e.quants {
|
||||
e.buf[bufIndex+0] = uint8(i)
|
||||
bufIndex += 1
|
||||
q := &e.quants[i]
|
||||
for z := 0; z < 64; z++ {
|
||||
e.buf[bufIndex+z] = q[zigzag[z]]
|
||||
}
|
||||
bufIndex += 64
|
||||
|
||||
if e.colorType == ColorTypeGray {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return bufIndex
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeSOF0(bufIndex int, width int, height int) int {
|
||||
numComponents := byte(1)
|
||||
if e.colorType != ColorTypeGray {
|
||||
numComponents = byte(3)
|
||||
}
|
||||
|
||||
e.buf[bufIndex+0] = 0xFF
|
||||
e.buf[bufIndex+1] = 0xC0
|
||||
e.buf[bufIndex+2] = 0x00
|
||||
e.buf[bufIndex+3] = 0x08 + (3 * numComponents) // Payload length.
|
||||
e.buf[bufIndex+4] = 0x08 // 8-bit depth.
|
||||
e.buf[bufIndex+5] = byte(height >> 8)
|
||||
e.buf[bufIndex+6] = byte(height >> 0)
|
||||
e.buf[bufIndex+7] = byte(width >> 8)
|
||||
e.buf[bufIndex+8] = byte(width >> 0)
|
||||
e.buf[bufIndex+9] = numComponents
|
||||
|
||||
if e.colorType == ColorTypeGray {
|
||||
e.buf[bufIndex+10] = 0x01 // Component ID 1.
|
||||
e.buf[bufIndex+11] = 0x11 // Subsampling.
|
||||
e.buf[bufIndex+12] = 0x00 // Quant selector 0.
|
||||
return bufIndex + 13
|
||||
}
|
||||
|
||||
lumaComponentSubsampling := byte(0x11)
|
||||
if e.colorType == ColorTypeYCbCr420 {
|
||||
lumaComponentSubsampling = byte(0x22)
|
||||
}
|
||||
|
||||
// 4:4:4 subsampling. Luma and Chroma select Quant tables 0 and 1.
|
||||
e.buf[bufIndex+10] = 0x01 // Component ID 1.
|
||||
e.buf[bufIndex+11] = lumaComponentSubsampling
|
||||
e.buf[bufIndex+12] = 0x00 // Quant selector 0.
|
||||
e.buf[bufIndex+13] = 0x02 // Component ID 2.
|
||||
e.buf[bufIndex+14] = 0x11 // Subsampling.
|
||||
e.buf[bufIndex+15] = 0x01 // Quant selector 1.
|
||||
e.buf[bufIndex+16] = 0x03 // Component ID 3.
|
||||
e.buf[bufIndex+17] = 0x11 // Subsampling.
|
||||
e.buf[bufIndex+18] = 0x01 // Quant selector 1.
|
||||
return bufIndex + 19
|
||||
}
|
||||
func (e *Encoder) encodeDHT(bufIndex int) int {
|
||||
s := ""
|
||||
if e.colorType == ColorTypeGray {
|
||||
s = hardCodedDHTSegments[:len(hardCodedDHTSegments)/2]
|
||||
} else {
|
||||
s = hardCodedDHTSegments
|
||||
}
|
||||
|
||||
n := copy(e.buf[bufIndex:], s)
|
||||
return bufIndex + n
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeSOSHeader(bufIndex int) int {
|
||||
s := ""
|
||||
if e.colorType == ColorTypeGray {
|
||||
s = "\xFF\xDA" + // SOS marker.
|
||||
"\x00\x08" + // Payload length.
|
||||
"\x01" + // Number of components.
|
||||
"\x01\x00" + // Component ID 1, Huffman selectors 0 (DC) and 0 (AC).
|
||||
"\x00\x3F\x00" // Ss=0, Se=63, Ah=0, Al=0.
|
||||
} else {
|
||||
s = "\xFF\xDA" + // SOS marker.
|
||||
"\x00\x0C" + // Payload length.
|
||||
"\x03" + // Number of components.
|
||||
"\x01\x00" + // Component ID 1, Huffman selectors 0 (DC) and 0 (AC).
|
||||
"\x02\x11" + // Component ID 2, Huffman selectors 1 (DC) and 1 (AC).
|
||||
"\x03\x11" + // Component ID 3, Huffman selectors 1 (DC) and 1 (AC).
|
||||
"\x00\x3F\x00" // Ss=0, Se=63, Ah=0, Al=0.
|
||||
}
|
||||
|
||||
n := copy(e.buf[bufIndex:], s)
|
||||
return bufIndex + n
|
||||
}
|
||||
|
||||
// In terms of worst case number of bytes emitted, encodeBlock can emit 448
|
||||
// bytes (64 * 7 bytes), since there are 64 elements and each element could
|
||||
// emit 7 bytes.
|
||||
func (e *Encoder) encodeBlock(bufIndex int, whichComponent byte, b *BlockI16) int {
|
||||
whichHuffmanBase := 0
|
||||
if whichComponent > 0 {
|
||||
whichHuffmanBase = 2
|
||||
}
|
||||
|
||||
dc := div(b[0], int16(e.quants[whichHuffmanBase>>1][0]))
|
||||
deltaDC := dc - e.prevDC[whichComponent]
|
||||
e.prevDC[whichComponent] = dc
|
||||
bufIndex = e.emitHuffmanRun(bufIndex, whichHuffmanBase+0, 0, int32(deltaDC))
|
||||
|
||||
zeroesRunLength := uint32(0)
|
||||
for z := 1; z < 64; z++ {
|
||||
zz := zigzag[z]
|
||||
ac := div(b[zz], int16(e.quants[whichHuffmanBase>>1][zz]))
|
||||
if ac == 0 {
|
||||
zeroesRunLength++
|
||||
continue
|
||||
}
|
||||
for ; zeroesRunLength >= 16; zeroesRunLength -= 16 {
|
||||
bufIndex = e.emitHuffman(bufIndex, whichHuffmanBase+1, 0xF0)
|
||||
}
|
||||
bufIndex = e.emitHuffmanRun(bufIndex, whichHuffmanBase+1, zeroesRunLength, int32(ac))
|
||||
zeroesRunLength = 0
|
||||
}
|
||||
if zeroesRunLength > 0 {
|
||||
bufIndex = e.emitHuffman(bufIndex, whichHuffmanBase+1, 0x00)
|
||||
}
|
||||
|
||||
return bufIndex
|
||||
}
|
||||
|
||||
// emitHuffmanRun emits (zeroesRunLength, value) as two bitstrings. The first
|
||||
// bitstring combines zeroesRunLength (also called RRRR) and the "category"
|
||||
// part of value (also called SSSS). The second bitstring holds the "adjusted
|
||||
// diff" part of value.
|
||||
//
|
||||
// The breakdown from value (a signed integer) into category and adjusted diff
|
||||
// is a little complicated. Category is an unsigned integer less than 12 and
|
||||
// smaller category mean values closer to zero. Adjusted diff is an unsigned
|
||||
// integer less than (1 << category).
|
||||
//
|
||||
// Value Category Adjusted Diff
|
||||
// 0 0 0
|
||||
// -1, +1 1 0, 1
|
||||
// -3, -2, +2, +3 2 0, 1, 2, 3
|
||||
// -7, -6, -5, -4, +4, +5, +6, +7 3 0, 1, 2, 3, 4, 5, 6, 7
|
||||
// -15, -14, ..., -8, +8, ..., +14, +15 4 0, 1, ..., 7, 8, ..., 14, 15
|
||||
// etc etc
|
||||
// -2047, ..., -1024, +1024, ..., +2047 11 0, ..., 1023, 1024, ..., 2047
|
||||
//
|
||||
// Compare with the JPEG spec, Tables F.1 and F.2, for the distinction between
|
||||
// "diff" and "adjusted diff".
|
||||
//
|
||||
// For example, if value is -5 then category is 3 and adjDiff is 2 (0b010).
|
||||
//
|
||||
// For example, if value is -13 then category is 4 and adjDiff is 2 (0b0010).
|
||||
//
|
||||
// For example, if value is +15 then category is 4 and adjDiff is 15 (0b1111).
|
||||
//
|
||||
// At the bit level, the combined number ((zeroesRunLength << 4) | category),
|
||||
// also known as ((RRRR << 4) | SSSS) in the spec, is Huffman-encoded. After
|
||||
// that comes the adjDiff's bits, a bitstring whose length equals category.
|
||||
//
|
||||
// For DC values, zeroesRunLength is always zero and what's passed to this
|
||||
// method is the delta from the previous DC value.
|
||||
//
|
||||
// For AC values, zeroesRunLength ranges from 0 up to 15 (inclusive). Category
|
||||
// 11 is unused. Category 0 is also unused for AC values (as zeroes are run-
|
||||
// length encoded), other than ((RRRR << 4) | SSSS) values of 0x00 and 0xF0
|
||||
// being special cases:
|
||||
//
|
||||
// - 0x00 means that all remaining AC coefficients are zero.
|
||||
// - 0xF0 means that the next 16 AC coefficients are zero.
|
||||
//
|
||||
// Pre-condition: value is in the range [-2047, +2047]. BlockI16.IsValid checks
|
||||
// for [-1024, +1023] for DC and [-1023, +1023] for AC values. For DC values,
|
||||
// what's then passed to this method is the delta from the previous DC value.
|
||||
//
|
||||
// In terms of worst case number of bytes emitted, emitHuffmanRun can emit 54
|
||||
// bits, which rounds up to 7 bytes. To calculate that: category can range up
|
||||
// to 11, so that emitHuffman + emitBits can emit 16 + 11 = 27 bits. Doubling
|
||||
// that, because of 0xFF 0x00 byte stuffing, produces a worst case of 54.
|
||||
func (e *Encoder) emitHuffmanRun(bufIndex int, whichHuffman int, zeroesRunLength uint32, value int32) int {
|
||||
absValue, adjDiffBeforeMasking := uint32(value), uint32(value)
|
||||
if value < 0 {
|
||||
absValue, adjDiffBeforeMasking = uint32(-value), uint32(value-1)
|
||||
}
|
||||
|
||||
category := uint32(0)
|
||||
if absValue < 0x100 {
|
||||
category = uint32(bitCount[absValue])
|
||||
} else {
|
||||
category = uint32(bitCount[absValue>>8]) + 8
|
||||
}
|
||||
|
||||
combined := (zeroesRunLength << 4) | category
|
||||
bufIndex = e.emitHuffman(bufIndex, whichHuffman, uint8(combined))
|
||||
|
||||
// At this point, we could say
|
||||
//
|
||||
// adjDiff := adjDiffBeforeMasking & ((1 << category) - 1)
|
||||
//
|
||||
// but it's equivalent to pass adjDiffBeforeMasking (instead of adjDiff) to
|
||||
// emitBits, which does "v &= (1 << n) - 1" early in its function body.
|
||||
bufIndex = e.emitBits(bufIndex, adjDiffBeforeMasking, category)
|
||||
|
||||
return bufIndex
|
||||
}
|
||||
|
||||
// emitHuffman emits the Huffman-coded bits for value.
|
||||
func (e *Encoder) emitHuffman(bufIndex int, whichHuffman int, value uint8) int {
|
||||
x := huffmanBitWriters[whichHuffman][value]
|
||||
return e.emitBits(bufIndex, x&0xFFFF, x>>16)
|
||||
}
|
||||
|
||||
// emitBits emits the low n bits of v.
|
||||
func (e *Encoder) emitBits(bufIndex int, v uint32, n uint32) int {
|
||||
if n <= 0 {
|
||||
return bufIndex
|
||||
}
|
||||
|
||||
v &= (1 << n) - 1
|
||||
n += e.bitsN
|
||||
v <<= 32 - n
|
||||
v |= e.bitsV
|
||||
|
||||
for ; n >= 8; n -= 8 {
|
||||
vByte := byte(v >> 24)
|
||||
e.buf[bufIndex] = vByte
|
||||
bufIndex++
|
||||
if vByte == 0xFF {
|
||||
e.buf[bufIndex] = 0x00
|
||||
bufIndex++
|
||||
}
|
||||
v <<= 8
|
||||
}
|
||||
|
||||
e.bitsV = v
|
||||
e.bitsN = n
|
||||
return bufIndex
|
||||
}
|
||||
|
||||
// div returns a/b rounded to the nearest integer, instead of rounded to zero.
|
||||
//
|
||||
// Pre-condition: b > 0.
|
||||
func div(a, b int16) int16 {
|
||||
if a >= 0 {
|
||||
return (a + (b >> 1)) / b
|
||||
}
|
||||
return -((-a + (b >> 1)) / b)
|
||||
}
|
||||
|
||||
// zigzag represents JPEG's zig-zag order for visiting coefficients. If b is a
|
||||
// BlockI16 then b[zigzag[1]], b[zigzag[2]], b[zigzag[3]], ..., b[zigzag[63]]
|
||||
// are its AC coefficients ordered from lowest to highest frequency, roughly
|
||||
// speaking. b[0], which is also b[zigzag[0]], is the DC coefficient.
|
||||
//
|
||||
// https://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg
|
||||
var zigzag = [64]uint8{
|
||||
0o00, 0o01, 0o10, 0o20, 0o11, 0o02, 0o03, 0o12, // 0, 1, 8, 16, 9, 2, 3, 10,
|
||||
0o21, 0o30, 0o40, 0o31, 0o22, 0o13, 0o04, 0o05, // 17, 24, 32, 25, 18, 11, 4, 5,
|
||||
0o14, 0o23, 0o32, 0o41, 0o50, 0o60, 0o51, 0o42, // 12, 19, 26, 33, 40, 48, 41, 34,
|
||||
0o33, 0o24, 0o15, 0o06, 0o07, 0o16, 0o25, 0o34, // 27, 20, 13, 6, 7, 14, 21, 28,
|
||||
0o43, 0o52, 0o61, 0o70, 0o71, 0o62, 0o53, 0o44, // 35, 42, 49, 56, 57, 50, 43, 36,
|
||||
0o35, 0o26, 0o17, 0o27, 0o36, 0o45, 0o54, 0o63, // 29, 22, 15, 23, 30, 37, 44, 51,
|
||||
0o72, 0o73, 0o64, 0o55, 0o46, 0o37, 0o47, 0o56, // 58, 59, 52, 45, 38, 31, 39, 46,
|
||||
0o65, 0o74, 0o75, 0o66, 0o57, 0o67, 0o76, 0o77, // 53, 60, 61, 54, 47, 55, 62, 63,
|
||||
}
|
||||
|
||||
// bitCount[i] is the smallest n such that i < (1 << n).
|
||||
var bitCount = [256]byte{
|
||||
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
// pjw8x8 is test/data/pjw-thumbnail.png scaled down to 8×8.
|
||||
pjw8x8 = [...]uint8{
|
||||
0xFF, 0xFF, 0xAF, 0x40, 0x50, 0xBF, 0xFF, 0xFF,
|
||||
0xFF, 0xEF, 0xEF, 0xFF, 0x40, 0x00, 0x50, 0xFF,
|
||||
0xFF, 0x60, 0xDF, 0xFF, 0xAF, 0x00, 0x00, 0xCF,
|
||||
0x9F, 0x00, 0x8F, 0x70, 0x10, 0x00, 0x00, 0xEF,
|
||||
0xEF, 0x8F, 0xCF, 0xAF, 0x50, 0x20, 0x60, 0xFF,
|
||||
0xFF, 0xAF, 0xCF, 0x8F, 0x20, 0x20, 0x80, 0xFF,
|
||||
0xFF, 0xFF, 0xCF, 0x9F, 0x30, 0x50, 0xEF, 0xFF,
|
||||
0xFF, 0xFF, 0xAF, 0x20, 0x00, 0x70, 0xFF, 0xFF,
|
||||
}
|
||||
|
||||
// pjw16x16 is test/data/pjw-thumbnail.png scaled down to 16×16.
|
||||
pjw16x16 = [...]uint8{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0x80, 0x80, 0x80, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0xFF, 0x40, 0x00, 0x00, 0x00, 0x40, 0xBF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x40, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0x80, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF,
|
||||
0xFF, 0xFF, 0x00, 0x80, 0xBF, 0xBF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0xBF, 0xFF,
|
||||
0xFF, 0x40, 0x00, 0x00, 0xBF, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBF, 0xFF,
|
||||
0xFF, 0x40, 0x00, 0x00, 0xBF, 0x80, 0x80, 0xBF, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
|
||||
0xFF, 0xBF, 0x00, 0xBF, 0xFF, 0xBF, 0xBF, 0xFF, 0x40, 0x80, 0x80, 0x00, 0x00, 0x80, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xBF, 0xBF, 0xBF, 0xBF, 0x80, 0x80, 0x40, 0x40, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0x40, 0x00, 0x40, 0x40, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xBF, 0x80, 0xBF, 0x80, 0x80, 0x80, 0x00, 0x40, 0x40, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xBF, 0x80, 0x00, 0x40, 0x40, 0x00, 0x40, 0xBF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0x40, 0x00, 0x40, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
}
|
||||
)
|
||||
|
||||
func diffU8(a uint8, b uint8) uint8 {
|
||||
if a < b {
|
||||
return b - a
|
||||
}
|
||||
return a - b
|
||||
}
|
||||
|
||||
func diffU16(a uint16, b uint16) uint16 {
|
||||
if a < b {
|
||||
return b - a
|
||||
}
|
||||
return a - b
|
||||
}
|
||||
|
||||
func checkPixelApproxEqual(tt *testing.T, dst image.RGBA64Image, src image.RGBA64Image, x int, y int) {
|
||||
const tolerance = 0x300 // RGBA64At returns values in the range [0x0000, 0xFFFF].
|
||||
dpix := dst.RGBA64At(x, y)
|
||||
spix := src.RGBA64At(x, y)
|
||||
if (diffU16(dpix.R, spix.R) > tolerance) ||
|
||||
(diffU16(dpix.G, spix.G) > tolerance) ||
|
||||
(diffU16(dpix.B, spix.B) > tolerance) ||
|
||||
(diffU16(dpix.A, spix.A) > tolerance) {
|
||||
tt.Errorf("At (%d, %d): dpix = %04X, spix = %04X", x, y, dpix, spix)
|
||||
}
|
||||
}
|
||||
|
||||
func testLowLevelJpegEncodeJpegDecode(tt *testing.T, src image.RGBA64Image, colorType ColorType) {
|
||||
quants := Array2QuantizationFactors{}
|
||||
quants.SetToStandardValues(MaximumQuality)
|
||||
opts := &EncoderOptions{
|
||||
QuantizationFactors: &quants,
|
||||
}
|
||||
|
||||
bounds := src.Bounds()
|
||||
buf := &bytes.Buffer{}
|
||||
enc := Encoder{}
|
||||
if err := enc.Reset(buf, colorType, bounds.Dx(), bounds.Dy(), opts); err != nil {
|
||||
tt.Fatalf("enc.Reset: %v", err)
|
||||
}
|
||||
|
||||
if colorType == ColorTypeGray {
|
||||
srcU8s := Array1BlockU8{}
|
||||
srcI16s := Array1BlockI16{}
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += 8 {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += 8 {
|
||||
srcU8s.ExtractFrom(src, x, y)
|
||||
srcI16s.ForwardDCTFrom(&srcU8s)
|
||||
if err := enc.Add1(buf, &srcI16s); err != nil {
|
||||
tt.Fatalf("enc.Add1: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if colorType == ColorTypeYCbCr444 {
|
||||
srcU8s := Array3BlockU8{}
|
||||
srcI16s := Array3BlockI16{}
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y += 8 {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x += 8 {
|
||||
srcU8s.ExtractFrom(src, x, y)
|
||||
srcI16s.ForwardDCTFrom(&srcU8s)
|
||||
if err := enc.Add3(buf, &srcI16s); err != nil {
|
||||
tt.Fatalf("enc.Add3: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
tt.Fatalf("unexpected colorType: %v", colorType)
|
||||
}
|
||||
|
||||
dst, err := jpeg.Decode(buf)
|
||||
if err != nil {
|
||||
tt.Fatalf("jpeg.Decode: %v", err)
|
||||
} else if got := dst.Bounds(); got != bounds {
|
||||
tt.Fatalf("dst.Bounds: got %v, want %v", got, bounds)
|
||||
}
|
||||
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
checkPixelApproxEqual(tt, dst.(image.RGBA64Image), src, x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorTypeGray(tt *testing.T) {
|
||||
src := image.NewGray(image.Rect(0, 0, 8, 8))
|
||||
copy(src.Pix, pjw8x8[:])
|
||||
testLowLevelJpegEncodeJpegDecode(tt, src, ColorTypeGray)
|
||||
}
|
||||
|
||||
func TestColorTypeYCbCr444(tt *testing.T) {
|
||||
const width, height = 12, 15
|
||||
src := image.NewYCbCr(image.Rect(0, 0, width, height), image.YCbCrSubsampleRatio444)
|
||||
bounds := src.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
i := (width * y) + x
|
||||
j := (16 * y) + x
|
||||
src.Y[i] = pjw16x16[j]
|
||||
src.Cb[i] = uint8(j)
|
||||
src.Cr[i] = 0xAA
|
||||
}
|
||||
}
|
||||
testLowLevelJpegEncodeJpegDecode(tt, src, ColorTypeYCbCr444)
|
||||
}
|
||||
|
||||
// TestWikipediaDCT confirms that we can reproduce the FDCT computation from
|
||||
// https://en.wikipedia.org/wiki/JPEG#Discrete_cosine_transform
|
||||
//
|
||||
// We also then check the FDCT+IDCT round trip is approximately equal to the
|
||||
// original BlockU8 values. It's not exactly equal due to rounding errors.
|
||||
func TestWikipediaDCT(tt *testing.T) {
|
||||
srcU8 := BlockU8{
|
||||
52, 55, 61, 66, 70, 61, 64, 73,
|
||||
63, 59, 55, 90, 109, 85, 69, 72,
|
||||
62, 59, 68, 113, 144, 104, 66, 73,
|
||||
63, 58, 71, 122, 154, 106, 70, 69,
|
||||
67, 61, 68, 104, 126, 88, 68, 70,
|
||||
79, 65, 60, 70, 77, 68, 58, 75,
|
||||
85, 71, 64, 59, 55, 61, 65, 83,
|
||||
87, 79, 69, 68, 65, 76, 78, 94,
|
||||
}
|
||||
|
||||
gotI16 := srcU8.ForwardDCT()
|
||||
|
||||
wantI16 := BlockI16{
|
||||
-415, -30, -61, 27, 56, -20, -2, 0,
|
||||
4, -22, -61, 10, 13, -7, -9, 5,
|
||||
-47, 7, 77, -25, -29, 10, 5, -6,
|
||||
-49, 12, 34, -15, -10, 6, 2, 2,
|
||||
12, -7, -13, -4, -2, 2, -3, 3,
|
||||
-8, 3, 2, -6, -2, 1, 4, 2,
|
||||
-1, 0, 0, -2, -1, -3, 4, -1,
|
||||
0, 0, -1, -4, -1, 0, 1, 2,
|
||||
}
|
||||
|
||||
if gotI16 != wantI16 {
|
||||
tt.Fatalf("gotI16:\n%v\nwantI16:\n%v", gotI16, wantI16)
|
||||
}
|
||||
|
||||
gotU8 := gotI16.InverseDCT()
|
||||
|
||||
const tolerance = 1 // BlockU8 values are in the range [0x00, 0xFF].
|
||||
for y := 0; y < 8; y++ {
|
||||
for x := 0; x < 8; x++ {
|
||||
got := gotU8[(8*y)+x]
|
||||
want := srcU8[(8*y)+x]
|
||||
if diff := diffU8(got, want); diff > tolerance {
|
||||
tt.Errorf("At (%d, %d): got 0x%02X, want 0x%02X", x, y, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuantizationFactors(tt *testing.T) {
|
||||
testCases := []struct {
|
||||
quality int
|
||||
want QuantizationFactors
|
||||
}{{
|
||||
|
||||
quality: 1,
|
||||
want: QuantizationFactors{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 25,
|
||||
want: QuantizationFactors{
|
||||
0x20, 0x16, 0x14, 0x20, 0x30, 0x50, 0x66, 0x7A,
|
||||
0x18, 0x18, 0x1C, 0x26, 0x34, 0x74, 0x78, 0x6E,
|
||||
0x1C, 0x1A, 0x20, 0x30, 0x50, 0x72, 0x8A, 0x70,
|
||||
0x1C, 0x22, 0x2C, 0x3A, 0x66, 0xAE, 0xA0, 0x7C,
|
||||
0x24, 0x2C, 0x4A, 0x70, 0x88, 0xDA, 0xCE, 0x9A,
|
||||
0x30, 0x46, 0x6E, 0x80, 0xA2, 0xD0, 0xE2, 0xB8,
|
||||
0x62, 0x80, 0x9C, 0xAE, 0xCE, 0xF2, 0xF0, 0xCA,
|
||||
0x90, 0xB8, 0xBE, 0xC4, 0xE0, 0xC8, 0xCE, 0xC6,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 50,
|
||||
want: QuantizationFactors{
|
||||
0x10, 0x0B, 0x0A, 0x10, 0x18, 0x28, 0x33, 0x3D,
|
||||
0x0C, 0x0C, 0x0E, 0x13, 0x1A, 0x3A, 0x3C, 0x37,
|
||||
0x0E, 0x0D, 0x10, 0x18, 0x28, 0x39, 0x45, 0x38,
|
||||
0x0E, 0x11, 0x16, 0x1D, 0x33, 0x57, 0x50, 0x3E,
|
||||
0x12, 0x16, 0x25, 0x38, 0x44, 0x6D, 0x67, 0x4D,
|
||||
0x18, 0x23, 0x37, 0x40, 0x51, 0x68, 0x71, 0x5C,
|
||||
0x31, 0x40, 0x4E, 0x57, 0x67, 0x79, 0x78, 0x65,
|
||||
0x48, 0x5C, 0x5F, 0x62, 0x70, 0x64, 0x67, 0x63,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 75,
|
||||
want: QuantizationFactors{
|
||||
0x08, 0x06, 0x05, 0x08, 0x0C, 0x14, 0x1A, 0x1F,
|
||||
0x06, 0x06, 0x07, 0x0A, 0x0D, 0x1D, 0x1E, 0x1C,
|
||||
0x07, 0x07, 0x08, 0x0C, 0x14, 0x1D, 0x23, 0x1C,
|
||||
0x07, 0x09, 0x0B, 0x0F, 0x1A, 0x2C, 0x28, 0x1F,
|
||||
0x09, 0x0B, 0x13, 0x1C, 0x22, 0x37, 0x34, 0x27,
|
||||
0x0C, 0x12, 0x1C, 0x20, 0x29, 0x34, 0x39, 0x2E,
|
||||
0x19, 0x20, 0x27, 0x2C, 0x34, 0x3D, 0x3C, 0x33,
|
||||
0x24, 0x2E, 0x30, 0x31, 0x38, 0x32, 0x34, 0x32,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 90,
|
||||
want: QuantizationFactors{
|
||||
0x03, 0x02, 0x02, 0x03, 0x05, 0x08, 0x0A, 0x0C,
|
||||
0x02, 0x02, 0x03, 0x04, 0x05, 0x0C, 0x0C, 0x0B,
|
||||
0x03, 0x03, 0x03, 0x05, 0x08, 0x0B, 0x0E, 0x0B,
|
||||
0x03, 0x03, 0x04, 0x06, 0x0A, 0x11, 0x10, 0x0C,
|
||||
0x04, 0x04, 0x07, 0x0B, 0x0E, 0x16, 0x15, 0x0F,
|
||||
0x05, 0x07, 0x0B, 0x0D, 0x10, 0x15, 0x17, 0x12,
|
||||
0x0A, 0x0D, 0x10, 0x11, 0x15, 0x18, 0x18, 0x14,
|
||||
0x0E, 0x12, 0x13, 0x14, 0x16, 0x14, 0x15, 0x14,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 95,
|
||||
want: QuantizationFactors{
|
||||
0x02, 0x01, 0x01, 0x02, 0x02, 0x04, 0x05, 0x06,
|
||||
0x01, 0x01, 0x01, 0x02, 0x03, 0x06, 0x06, 0x06,
|
||||
0x01, 0x01, 0x02, 0x02, 0x04, 0x06, 0x07, 0x06,
|
||||
0x01, 0x02, 0x02, 0x03, 0x05, 0x09, 0x08, 0x06,
|
||||
0x02, 0x02, 0x04, 0x06, 0x07, 0x0B, 0x0A, 0x08,
|
||||
0x02, 0x04, 0x06, 0x06, 0x08, 0x0A, 0x0B, 0x09,
|
||||
0x05, 0x06, 0x08, 0x09, 0x0A, 0x0C, 0x0C, 0x0A,
|
||||
0x07, 0x09, 0x0A, 0x0A, 0x0B, 0x0A, 0x0A, 0x0A,
|
||||
},
|
||||
}, {
|
||||
|
||||
quality: 100,
|
||||
want: QuantizationFactors{
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
},
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
qf := QuantizationFactors{}
|
||||
qf.SetToStandardValues(QuantizationStandardValuesTypeLuma, tc.quality)
|
||||
if qf != tc.want {
|
||||
tt.Errorf("quality = %d\ngot:\n%v\nwant:\n%v", tc.quality, qf, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoAllocation(tt *testing.T) {
|
||||
enc := Encoder{}
|
||||
srcI16s := Array1BlockI16{}
|
||||
got := testing.AllocsPerRun(100, func() {
|
||||
enc.Reset(io.Discard, ColorTypeGray, 8, 8, nil)
|
||||
enc.Add1(io.Discard, &srcI16s)
|
||||
})
|
||||
if got != 0 {
|
||||
tt.Fatalf("AllocsPerRun: got %v, want 0", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// Copyright 2025 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
package lowleveljpeg
|
||||
|
||||
// QuantizationStandardValuesType selects which table from the JPEG
|
||||
// specification to use for SetToStandardValues.
|
||||
type QuantizationStandardValuesType byte
|
||||
|
||||
const (
|
||||
// QuantizationStandardValuesTypeLuma means to use table K.1 from the JPEG
|
||||
// spec.
|
||||
QuantizationStandardValuesTypeLuma = QuantizationStandardValuesType(0)
|
||||
|
||||
// QuantizationStandardValuesTypeChroma means to use table K.2 from the
|
||||
// JPEG spec.
|
||||
QuantizationStandardValuesTypeChroma = QuantizationStandardValuesType(1)
|
||||
)
|
||||
|
||||
// 12-bit or 16-bit depth JPEGs can use more-than-8-bit quantization factors,
|
||||
// but we only support 8-bit JPEGs and so we use 8-bit quantization factors.
|
||||
// The QuantizationFactors element type is uint8.
|
||||
|
||||
// QuantizationFactors is an 8×8 block of uint8 values, used to quantize a
|
||||
// BlockI16.
|
||||
//
|
||||
// It is indexed in DCT (not XY) space, like a Block16.
|
||||
type QuantizationFactors [64]uint8
|
||||
|
||||
// String returns b in human-readable form.
|
||||
func (b QuantizationFactors) String() string {
|
||||
buf := [64 * 3]byte{}
|
||||
for i, value := range b {
|
||||
buf[(3*i)+0] = fmtHex[15&(value>>0x04)]
|
||||
buf[(3*i)+1] = fmtHex[15&(value>>0x00)]
|
||||
buf[(3*i)+2] = fmtSep[7&i]
|
||||
}
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// IsValid returns whether b does not contain any zero-valued elements.
|
||||
func (b *QuantizationFactors) IsValid() bool {
|
||||
if b == nil {
|
||||
return false
|
||||
}
|
||||
for _, v := range b {
|
||||
if v == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const (
|
||||
// MinimumQuality and MaximumQuality are the minimum and maximum
|
||||
// (inclusive) values for SetToStandardValues' quality parameter.
|
||||
MinimumQuality = 1
|
||||
MaximumQuality = 100
|
||||
|
||||
// DefaultQuality is the default value used by EncoderOptions for
|
||||
// SetToStandardValues' quality parameter. It matches libjpeg's default
|
||||
// both in terms of numerical value and in behavior.
|
||||
DefaultQuality = 75
|
||||
)
|
||||
|
||||
// SetToStandardValues sets b to one of two standard quantization tables
|
||||
// (Tables K.1 or K.2 in the JPEG spec, for Luma and Chroma respectively),
|
||||
// scaled by a quality parameter with the same meaning as that used by
|
||||
// libjpeg's encoder.
|
||||
//
|
||||
// The quality parameter ranges from 1 (low quality) to 100 (high quality),
|
||||
// inclusive. Values outside of that range will be clamped to be in [1, 100].
|
||||
//
|
||||
// Passing quality = MinimumQuality will set every element of b to 0xFF.
|
||||
//
|
||||
// Passing quality = MaximumQuality will set every element of b to 0x01.
|
||||
func (b *QuantizationFactors) SetToStandardValues(which QuantizationStandardValuesType, quality int) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Follow the same algorithm as in libjpeg's jcparam.c.
|
||||
|
||||
if quality < 1 {
|
||||
quality = 1
|
||||
} else if quality > 100 {
|
||||
quality = 100
|
||||
}
|
||||
|
||||
q := 0
|
||||
if quality < 50 {
|
||||
q = 5000 / quality
|
||||
} else {
|
||||
q = 200 - (quality * 2)
|
||||
}
|
||||
|
||||
std := &standardQuantizationFactors[which&1]
|
||||
for i, v := range std {
|
||||
scaled := ((int(v) * q) + 50) / 100
|
||||
if scaled < 0x01 {
|
||||
scaled = 0x01
|
||||
} else if scaled > 0xFF {
|
||||
scaled = 0xFF
|
||||
}
|
||||
b[i] = uint8(scaled)
|
||||
}
|
||||
}
|
||||
|
||||
var standardQuantizationFactors = [2]QuantizationFactors{
|
||||
// Table K.1 from the JPEG spec.
|
||||
QuantizationStandardValuesTypeLuma: {
|
||||
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,
|
||||
},
|
||||
|
||||
// Table K.2 from the JPEG spec.
|
||||
QuantizationStandardValuesTypeChroma: {
|
||||
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,
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user