mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 01:01:35 +01:00
std/handsum: redo Handsum image file format
This commit is contained in:
1 parent
32b741db7f
commit
f8c6b35ff5
25 files changed
+1795
-901
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// 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 handsum
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func makePlainColorImage(grayValue uint8) image.Image {
|
||||
m := image.NewGray(image.Rect(0, 0, 5, 5))
|
||||
for i := range m.Pix {
|
||||
m.Pix[i] = grayValue
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func testRoundTrip(tt *testing.T, src image.Image, pixelValue uint8, justCheckThatItsGray bool) {
|
||||
for c := Color(0); c < 2; c++ {
|
||||
for q := Quality(0); q < 4; q++ {
|
||||
buf := &bytes.Buffer{}
|
||||
err := Encode(buf, src, &EncodeOptions{
|
||||
Color: MakeOptionColor(c),
|
||||
Quality: MakeOptionQuality(q),
|
||||
})
|
||||
if err != nil {
|
||||
tt.Fatalf("c=%d: q=%d: Encode: %v", c, q, err)
|
||||
}
|
||||
dst, err := Decode(buf)
|
||||
if err != nil {
|
||||
tt.Fatalf("c=%d: q=%d: Decode: %v", c, q, err)
|
||||
}
|
||||
|
||||
pix, stride := []byte(nil), 0
|
||||
switch dst := dst.(type) {
|
||||
case *image.Gray:
|
||||
pix, stride = dst.Pix, 1
|
||||
case *image.RGBA:
|
||||
pix, stride = dst.Pix, 4
|
||||
}
|
||||
|
||||
if !justCheckThatItsGray {
|
||||
for i := 0; i < len(pix); i += stride {
|
||||
if pix[i] != pixelValue {
|
||||
tt.Fatalf("c=%d: q=%d: i=%d: pix: got [% 02X], want %02X",
|
||||
c, q, i, pix, pixelValue)
|
||||
}
|
||||
}
|
||||
|
||||
} else if stride == 4 {
|
||||
for i := 0; i < len(pix); i += 4 {
|
||||
if (pix[i] != pix[i+1]) || (pix[i] != pix[i+2]) {
|
||||
tt.Fatalf("c=%d: q=%d: i=%d: pix: got [% 02X], want gray",
|
||||
c, q, i, pix)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTripPureBlack(tt *testing.T) {
|
||||
testRoundTrip(tt, makePlainColorImage(0x00), 0x00, false)
|
||||
}
|
||||
|
||||
func TestRoundTripPureWhite(tt *testing.T) {
|
||||
testRoundTrip(tt, makePlainColorImage(0xFF), 0xFF, false)
|
||||
}
|
||||
|
||||
func TestRoundTripGray(tt *testing.T) {
|
||||
src := image.NewGray(image.Rect(0, 0, 16, 16))
|
||||
for i := range src.Pix {
|
||||
src.Pix[i] = uint8(37 * i)
|
||||
}
|
||||
testRoundTrip(tt, src, 0, true)
|
||||
}
|
||||
Reference in new issue
Block a user