example/convert-to-nia: allow 16-bit PNG output

This commit is contained in:
Nigel Tao committed 2025-06-23 09:51:31 +10:00
1 parent bb25727729
commit 67e9078eab
8 files changed
+231 -38

No files matched your search

+23
View File
@@ -28,17 +28,32 @@ func encodeImage(w io.Writer, src image.Image) error {
case *image.Gray:
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeGray)
case *image.Gray16:
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeGray)
case *image.RGBA:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeRGBX)
}
case *image.RGBA64:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeRGBX)
}
case *image.NRGBA:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeRGBX)
} else {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth8, ColorTypeNRGBA)
}
case *image.NRGBA64:
if src.Opaque() {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeRGBX)
} else {
return e.Encode(w, src.Pix, b.Dx(), b.Dy(), src.Stride, Depth16, ColorTypeNRGBA)
}
}
tmp := image.NewNRGBA(b)
@@ -50,16 +65,24 @@ func getPix(m image.Image) []byte {
switch m := m.(type) {
case *image.Gray:
return m.Pix
case *image.Gray16:
return m.Pix
case *image.RGBA:
return m.Pix
case *image.RGBA64:
return m.Pix
case *image.NRGBA:
return m.Pix
case *image.NRGBA64:
return m.Pix
}
return nil
}
func TestRoundTrip(tt *testing.T) {
testCases := []string{
"36.png",
"49.png",
"bricks-color.png",
"bricks-gray.png",
"harvesters.png",