lib/lowleveljpeg: add Abs method, nil checks

This commit is contained in:
Nigel Tao committed 2025-02-23 12:24:43 +11:00
1 parent 12b404f58d
commit f483ef0849
4 files changed
+42 -2

No files matched your search

+31
View File
@@ -54,6 +54,9 @@ func (b BlockU8) String() string {
// SetToNeutral sets each element to BlockU8NeutralValue.
func (b *BlockU8) SetToNeutral() {
if b == nil {
return
}
for i := range b {
b[i] = BlockU8NeutralValue
}
@@ -139,8 +142,26 @@ func (b BlockI16) String() string {
return string(buf[:])
}
// Abs returns the elementwise absolute value of b.
func (b *BlockI16) Abs() (ret BlockI16) {
if b == nil {
return ret
}
for i, v := range b {
if v < 0 {
ret[i] = -v
} else {
ret[i] = +v
}
}
return ret
}
// SetToNeutral sets each element to BlockI16NeutralValue.
func (b *BlockI16) SetToNeutral() {
if b == nil {
return
}
for i := range b {
b[i] = BlockI16NeutralValue
}
@@ -241,6 +262,16 @@ func (b QuadBlockU8) String() string {
return string(buf[:])
}
// SetToNeutral sets each element to BlockU8NeutralValue.
func (b *QuadBlockU8) SetToNeutral() {
if b == nil {
return
}
for i := range b {
b[i] = BlockU8NeutralValue
}
}
// These are "1 / 2" and "1 / (2 * √2)" as 16.16 fixed point values.
const (
fixedPointHalf = +0x8000
+4 -1
View File
@@ -16,7 +16,10 @@ package lowleveljpeg
//
// It uses a triangle filter.
func (dst *QuadBlockU8) UpsampleFrom(src *BlockU8) {
if (dst == nil) || (src == nil) {
if dst == nil {
return
} else if src == nil {
dst.SetToNeutral()
return
}
dst[0x00] = src[0x00]
+6
View File
@@ -236,6 +236,12 @@ func downsample4x4(dst []uint8, src *BlockU8) {
// DownsampleFrom reduces one 16×16 quad-block to one 8×8 block.
func (dst *BlockU8) DownsampleFrom(src *QuadBlockU8) {
if dst == nil {
return
} else if src == nil {
dst.SetToNeutral()
return
}
for y := 0; y < 8; y++ {
for x := 0; x < 8; x++ {
d := (y << 3) | (x << 0)
+1 -1
View File
@@ -46,7 +46,7 @@ func genUpsampleFrom(w *bytes.Buffer) {
w.WriteString("//\n")
w.WriteString("// It uses a triangle filter.\n")
w.WriteString("func (dst *QuadBlockU8) UpsampleFrom(src *BlockU8) {\n")
w.WriteString("\tif (dst == nil) || (src == nil) {\n\t\treturn\n\t}\n")
w.WriteString("\tif dst == nil {\n\t\treturn\n\t} else if src == nil {\n\t\tdst.SetToNeutral()\n\t\treturn\n\t}\n")
for y := 0; y < 16; y++ {
for x := 0; x < 16; x++ {
d := (y << 4) | x