mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
lib/lowleveljpeg: add YCoCg methods
This commit is contained in:
1 parent
c400a47bb9
commit
11dfe340a1
2 files changed
+318
No files matched your search
@@ -358,3 +358,128 @@ func TestUpsampleFrom(tt *testing.T) {
|
||||
tt.Fatalf("got:\n%v\nwant:\n%v", dst, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRGBToYCoCg(tt *testing.T) {
|
||||
testCases := []struct {
|
||||
r, g, b, yy, co, cg uint8
|
||||
}{
|
||||
// RGB corners.
|
||||
{0x00, 0x00, 0x00, 0x00, 0x80, 0x80},
|
||||
{0x00, 0x00, 0xFF, 0x40, 0x00, 0x40},
|
||||
{0x00, 0xFF, 0x00, 0x7F, 0x80, 0xFF},
|
||||
{0x00, 0xFF, 0xFF, 0xBF, 0x00, 0xBF},
|
||||
{0xFF, 0x00, 0x00, 0x40, 0xFF, 0x40},
|
||||
{0xFF, 0x00, 0xFF, 0x80, 0x80, 0x00},
|
||||
{0xFF, 0xFF, 0x00, 0xBF, 0xFF, 0xC0},
|
||||
{0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80},
|
||||
|
||||
// Same corners, with Co and Cg clamped to [0x40, 0xBF].
|
||||
{0x00, 0x00, 0x00, 0x00, 0x80, 0x80},
|
||||
{0x40, 0x00, 0xBF, 0x40, 0x40, 0x40},
|
||||
{0x40, 0xBF, 0x40, 0x7F, 0x80, 0xBF},
|
||||
{0x40, 0xFF, 0xBF, 0xBF, 0x40, 0xBF},
|
||||
{0xBF, 0x00, 0x40, 0x40, 0xBF, 0x40},
|
||||
{0xBF, 0x40, 0xBF, 0x80, 0x80, 0x40},
|
||||
{0xBF, 0xFE, 0x41, 0xBF, 0xBF, 0xBF},
|
||||
{0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80},
|
||||
|
||||
// Grays.
|
||||
{0x01, 0x01, 0x01, 0x01, 0x80, 0x80},
|
||||
{0x12, 0x12, 0x12, 0x12, 0x80, 0x80},
|
||||
{0x13, 0x13, 0x13, 0x13, 0x80, 0x80},
|
||||
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80},
|
||||
{0xFE, 0xFE, 0xFE, 0xFE, 0x80, 0x80},
|
||||
|
||||
// Not quite pure-black or pure-white.
|
||||
{0x00, 0x00, 0x01, 0x01, 0x7F, 0x7F},
|
||||
{0x00, 0x01, 0x00, 0x00, 0x80, 0x80},
|
||||
{0x01, 0x00, 0x00, 0x00, 0x80, 0x80},
|
||||
{0xFF, 0xFF, 0xFE, 0xFE, 0x80, 0x80},
|
||||
{0xFF, 0xFE, 0xFF, 0xFF, 0x80, 0x7F},
|
||||
{0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0x80},
|
||||
|
||||
// R=0x55, B=0x55.
|
||||
{0x55, 0x00, 0x55, 0x2B, 0x80, 0x55},
|
||||
{0x55, 0x40, 0x55, 0x4B, 0x80, 0x75},
|
||||
{0x55, 0x80, 0x55, 0x6A, 0x80, 0x95},
|
||||
{0x55, 0xC0, 0x55, 0x8A, 0x80, 0xB5},
|
||||
{0x55, 0xFF, 0x55, 0xAA, 0x80, 0xD5},
|
||||
|
||||
// R=0xFF, G=0xAA.
|
||||
{0xFF, 0xAA, 0x00, 0x94, 0xFF, 0x95},
|
||||
{0xFF, 0xAA, 0x40, 0xA4, 0xDF, 0x85},
|
||||
{0xFF, 0xAA, 0x80, 0xB5, 0xBF, 0x75},
|
||||
{0xFF, 0xAA, 0xC0, 0xC5, 0x9F, 0x65},
|
||||
{0xFF, 0xAA, 0xFF, 0xD5, 0x80, 0x55},
|
||||
|
||||
// An ad-hoc collection of Material Design 500 colors.
|
||||
{0xF4, 0x43, 0x36, 0x6C, 0xDF, 0x57}, // red
|
||||
{0x9C, 0x27, 0xB0, 0x67, 0x76, 0x40}, // purple
|
||||
{0x21, 0x96, 0xF3, 0x90, 0x17, 0x86}, // blue
|
||||
{0x00, 0x96, 0x88, 0x6D, 0x3C, 0xA9}, // teal
|
||||
{0xCD, 0xDC, 0x39, 0xAF, 0xCA, 0xAC}, // lime
|
||||
{0xFF, 0xC1, 0x07, 0xA2, 0xFC, 0x9F}, // amber
|
||||
{0x79, 0x55, 0x48, 0x5B, 0x98, 0x7A}, // brown
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
yy1, co1, cg1 := RGBToYCoCg(tc.r, tc.g, tc.b)
|
||||
if (yy1 != tc.yy) || (co1 != tc.co) || (cg1 != tc.cg) {
|
||||
tt.Errorf("RGBToYCoCg(0x%02X, 0x%02X, 0x%02X): "+
|
||||
"got {0x%02X, 0x%02X, 0x%02X}, "+
|
||||
"want {0x%02X, 0x%02X, 0x%02X}",
|
||||
tc.r, tc.g, tc.b, yy1, co1, cg1, tc.yy, tc.co, tc.cg)
|
||||
}
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tolerance := 0
|
||||
if (tc.r != tc.g) || (tc.g != tc.b) {
|
||||
tolerance = 2
|
||||
}
|
||||
|
||||
r1, g1, b1 := YCoCgToRGB(tc.yy, tc.co, tc.cg)
|
||||
if outsideTolerance(r1, tc.r, tolerance) ||
|
||||
outsideTolerance(g1, tc.g, tolerance) ||
|
||||
outsideTolerance(b1, tc.b, tolerance) {
|
||||
tt.Errorf("YCoCgToRGB(0x%02X, 0x%02X, 0x%02X): "+
|
||||
"got {0x%02X, 0x%02X, 0x%02X}, "+
|
||||
"want {0x%02X, 0x%02X, 0x%02X}",
|
||||
tc.yy, tc.co, tc.cg, r1, g1, b1, tc.r, tc.g, tc.b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRGBToYCoCgToRGBExhaustive(tt *testing.T) {
|
||||
if testing.Short() {
|
||||
tt.Skip()
|
||||
}
|
||||
|
||||
const tolerance = 2
|
||||
|
||||
for r := range 256 {
|
||||
r0 := uint8(r)
|
||||
for g := range 256 {
|
||||
g0 := uint8(g)
|
||||
for b := range 256 {
|
||||
b0 := uint8(b)
|
||||
|
||||
yy, co, cg := RGBToYCoCg(r0, g0, b0)
|
||||
r1, g1, b1 := YCoCgToRGB(yy, co, cg)
|
||||
if outsideTolerance(r1, r0, tolerance) ||
|
||||
outsideTolerance(g1, g0, tolerance) ||
|
||||
outsideTolerance(b1, b0, tolerance) {
|
||||
tt.Fatalf("{0x%02X, 0x%02X, 0x%02X} -> "+
|
||||
"{0x%02X, 0x%02X, 0x%02X} -> "+
|
||||
"{0x%02X, 0x%02X, 0x%02X}",
|
||||
r0, g0, b0, yy, co, cg, r1, b1, g1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func outsideTolerance(i uint8, j uint8, tolerance int) bool {
|
||||
delta := int(i) - int(j)
|
||||
return (delta < -tolerance) || (+tolerance < delta)
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
// Copyright 2026 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"
|
||||
)
|
||||
|
||||
// RGBToYCoCg converts an RGB triple to a YCoCg triple.
|
||||
//
|
||||
// JPEG uses YCbCr, not YCoCg, but this function allows for experimenting with
|
||||
// JPEG-inspired color transformations.
|
||||
func RGBToYCoCg(r, g, b uint8) (uint8, uint8, uint8) {
|
||||
// https://en.wikipedia.org/wiki/YCoCg#The_lifting-based_YCoCg-R_variation
|
||||
co := int(r) - int(b)
|
||||
tm := int(b) + (co / 2)
|
||||
cg := int(g) - tm
|
||||
yy := tm + (cg / 2)
|
||||
|
||||
// Normalize to the range [0x00, 0xFF], halving (and biasing) Co and Cg.
|
||||
return uint8(yy), uint8((0x100 + co) / 2), uint8((0x100 + cg) / 2)
|
||||
}
|
||||
|
||||
// YCoCgToRGB converts a YCoCg triple to an RGB triple.
|
||||
//
|
||||
// JPEG uses YCbCr, not YCoCg, but this function allows for experimenting with
|
||||
// JPEG-inspired color transformations.
|
||||
func YCoCgToRGB(yy, co, cg uint8) (uint8, uint8, uint8) {
|
||||
// https://en.wikipedia.org/wiki/YCoCg#The_lifting-based_YCoCg-R_variation
|
||||
// adjusted for the fact that the co and cg arguments have already been
|
||||
// halved-and-biased, per RGBToYCoCg.
|
||||
yy1 := int(yy)
|
||||
co1 := int(co) - 0x80
|
||||
cg1 := int(cg) - 0x80
|
||||
g := yy1 + cg1
|
||||
b := yy1 - cg1 - co1
|
||||
r := yy1 - cg1 + co1
|
||||
|
||||
return uint8(max(0x00, min(0xFF, r))),
|
||||
uint8(max(0x00, min(0xFF, g))),
|
||||
uint8(max(0x00, min(0xFF, b)))
|
||||
}
|
||||
|
||||
// ExtractYCoCgFrom is like ExtractYCbCrFrom but produces Gray (Luma) values
|
||||
// according to the (Luma, Chroma-orange, Chroma-green) formulae instead of
|
||||
// (Luma, Chroma-blue, Chroma-red).
|
||||
//
|
||||
// JPEG uses YCbCr, not YCoCg, but this method allows for experimenting with
|
||||
// JPEG-inspired image transformations.
|
||||
func (dst *Array1BlockU8) ExtractYCoCgFrom(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)
|
||||
y, _, _ := RGBToYCoCg(
|
||||
uint8(pix.R>>8),
|
||||
uint8(pix.G>>8),
|
||||
uint8(pix.B>>8),
|
||||
)
|
||||
dst[0][i] = uint8(y)
|
||||
|
||||
} else {
|
||||
r, g, b, _ := m.At(p.X, p.Y).RGBA()
|
||||
y, _, _ := RGBToYCoCg(
|
||||
uint8(r>>8),
|
||||
uint8(g>>8),
|
||||
uint8(b>>8),
|
||||
)
|
||||
dst[0][i] = uint8(y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fillRightAndDown(&dst[0], topLeftX, topLeftY, bounds.Max.X, bounds.Max.Y)
|
||||
}
|
||||
|
||||
// ExtractYCoCgFrom is like ExtractYCbCrFrom but produces (Luma, Chroma-orange,
|
||||
// Chroma-green) instead of (Luma, Chroma-blue, Chroma-red).
|
||||
//
|
||||
// JPEG uses YCbCr, not YCoCg, but this method allows for experimenting with
|
||||
// JPEG-inspired image transformations.
|
||||
func (dst *Array3BlockU8) ExtractYCoCgFrom(m image.Image, topLeftX int, topLeftY int) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
dst.SetToNeutral()
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
bounds := m.Bounds()
|
||||
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 mRGBA64Image != nil {
|
||||
pix := mRGBA64Image.RGBA64At(p.X, p.Y)
|
||||
y, co, cg := RGBToYCoCg(
|
||||
uint8(pix.R>>8),
|
||||
uint8(pix.G>>8),
|
||||
uint8(pix.B>>8),
|
||||
)
|
||||
dst[0][i] = y
|
||||
dst[1][i] = co
|
||||
dst[2][i] = cg
|
||||
|
||||
} else {
|
||||
r, g, b, _ := m.At(p.X, p.Y).RGBA()
|
||||
y, co, cg := RGBToYCoCg(
|
||||
uint8(r>>8),
|
||||
uint8(g>>8),
|
||||
uint8(b>>8),
|
||||
)
|
||||
dst[0][i] = y
|
||||
dst[1][i] = co
|
||||
dst[2][i] = cg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// ExtractYCoCgFrom is like ExtractYCbCrFrom but produces (Luma, Chroma-orange,
|
||||
// Chroma-green) instead of (Luma, Chroma-blue, Chroma-red).
|
||||
//
|
||||
// JPEG uses YCbCr, not YCoCg, but this method allows for experimenting with
|
||||
// JPEG-inspired image transformations.
|
||||
func (dst *Array6BlockU8) ExtractYCoCgFrom(m image.Image, topLeftX int, topLeftY int) {
|
||||
if dst == nil {
|
||||
return
|
||||
}
|
||||
if m == nil {
|
||||
dst.SetToNeutral()
|
||||
return
|
||||
}
|
||||
|
||||
tmp := [4]Array3BlockU8{}
|
||||
tmp[0].ExtractYCoCgFrom(m, topLeftX+0, topLeftY+0)
|
||||
tmp[1].ExtractYCoCgFrom(m, topLeftX+8, topLeftY+0)
|
||||
tmp[2].ExtractYCoCgFrom(m, topLeftX+0, topLeftY+8)
|
||||
tmp[3].ExtractYCoCgFrom(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]
|
||||
|
||||
downsample4x4(dst[4][0x00:], &tmp[0][1])
|
||||
downsample4x4(dst[4][0x04:], &tmp[1][1])
|
||||
downsample4x4(dst[4][0x20:], &tmp[2][1])
|
||||
downsample4x4(dst[4][0x24:], &tmp[3][1])
|
||||
|
||||
downsample4x4(dst[5][0x00:], &tmp[0][2])
|
||||
downsample4x4(dst[5][0x04:], &tmp[1][2])
|
||||
downsample4x4(dst[5][0x20:], &tmp[2][2])
|
||||
downsample4x4(dst[5][0x24:], &tmp[3][2])
|
||||
}
|
||||
Reference in new issue
Block a user