internal/png: update with Go 1.22

This commit is contained in:
Hajime Hoshi 2024-03-24 20:31:00 +09:00
parent 6552ae1dbe
commit 04c4676b7c
3 changed files with 40 additions and 11 deletions

View File

@ -24,6 +24,9 @@ import (
"go/token"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
exec "golang.org/x/sys/execabs"
@ -47,6 +50,20 @@ func pngFiles() ([]string, error) {
}
func run() error {
reVer := regexp.MustCompile(`^go1\.(\d+)(\.\d+)?$`)
verStr := runtime.Version()
m := reVer.FindStringSubmatch(verStr)
if m == nil {
return fmt.Errorf("png: unexpected Go version: %s", verStr)
}
ver, err := strconv.Atoi(m[1])
if err != nil {
return err
}
if ver < 22 {
return fmt.Errorf("png: use Go 1.22 or newer")
}
dir, err := pngDir()
if err != nil {
return err
@ -115,6 +132,18 @@ func run() error {
fmt.Fprintln(out, "// Code generated by gen.go. DO NOT EDIT.")
fmt.Fprintln(out)
format.Node(out, fset, tree)
if f == "reader.go" {
// The min function was removed as of Go 1.22, but this is needed for old Go.
// TODO: Remove this when Go 1.21 is the minimum supported version.
fmt.Fprintln(out, `
func min(a, b int) int {
if a < b {
return a
}
return b
}`)
}
}
return nil
}

View File

@ -138,13 +138,6 @@ type UnsupportedError string
func (e UnsupportedError) Error() string { return "png: unsupported feature: " + string(e) }
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (d *decoder) parseIHDR(length uint32) error {
if length != 13 {
return FormatError("bad IHDR length")
@ -973,7 +966,7 @@ func (d *decoder) checkHeader() error {
return nil
}
// Decode reads a PNG image from r and returns it as an image.Image.
// Decode reads a PNG image from r and returns it as an [image.Image].
// The type of Image returned depends on the PNG contents.
func Decode(r io.Reader) (image.Image, error) {
d := &decoder{
@ -1061,3 +1054,10 @@ func DecodeConfig(r io.Reader) (image.Config, error) {
func init() {
}
func min(a, b int) int {
if a < b {
return a
}
return b
}

View File

@ -27,7 +27,7 @@ type Encoder struct {
}
// EncoderBufferPool is an interface for getting and returning temporary
// instances of the EncoderBuffer struct. This can be used to reuse buffers
// instances of the [EncoderBuffer] struct. This can be used to reuse buffers
// when encoding multiple images.
type EncoderBufferPool interface {
Get() *EncoderBuffer
@ -192,7 +192,7 @@ func (e *encoder) writePLTEAndTRNS(p color.Palette) {
// An encoder is an io.Writer that satisfies writes by writing PNG IDAT chunks,
// including an 8-byte header and 4-byte CRC checksum per Write call. Such calls
// should be relatively infrequent, since writeIDATs uses a bufio.Writer.
// should be relatively infrequent, since writeIDATs uses a [bufio.Writer].
//
// This method should only be called from writeIDATs (via writeImage).
// No other code should treat an encoder as an io.Writer.
@ -588,7 +588,7 @@ func levelToZlib(l CompressionLevel) int {
func (e *encoder) writeIEND() { e.writeChunk(nil, "IEND") }
// Encode writes the Image m to w in PNG format. Any Image may be
// encoded, but images that are not image.NRGBA might be encoded lossily.
// encoded, but images that are not [image.NRGBA] might be encoded lossily.
func Encode(w io.Writer, m image.Image) error {
var e Encoder
return e.Encode(w, m)