package png
import (
)
type Encoder struct {
CompressionLevel CompressionLevel
BufferPool EncoderBufferPool
}
type EncoderBufferPool interface {
Get() *EncoderBuffer
Put(*EncoderBuffer)
}
type EncoderBuffer encoder
type encoder struct {
enc *Encoder
w io.Writer
m image.Image
cb int
err error
header [8]byte
footer [4]byte
tmp [4 * 256]byte
cr [nFilter][]uint8
pr []uint8
zw *zlib.Writer
zwLevel int
bw *bufio.Writer
}
type CompressionLevel int
const (
DefaultCompression CompressionLevel = 0
NoCompression CompressionLevel = -1
BestSpeed CompressionLevel = -2
BestCompression CompressionLevel = -3
)
type opaquer interface {
Opaque() bool
}
func ( image.Image) bool {
if , := .(opaquer); {
return .Opaque()
}
:= .Bounds()
for := .Min.Y; < .Max.Y; ++ {
for := .Min.X; < .Max.X; ++ {
, , , := .At(, ).RGBA()
if != 0xffff {
return false
}
}
}
return true
}
func ( uint8) int {
if < 128 {
return int()
}
return 256 - int()
}
func ( *encoder) ( []byte, string) {
if .err != nil {
return
}
:= uint32(len())
if int() != len() {
.err = UnsupportedError( + " chunk is too large: " + strconv.Itoa(len()))
return
}
binary.BigEndian.PutUint32(.header[:4], )
.header[4] = [0]
.header[5] = [1]
.header[6] = [2]
.header[7] = [3]
:= crc32.NewIEEE()
.Write(.header[4:8])
.Write()
binary.BigEndian.PutUint32(.footer[:4], .Sum32())
_, .err = .w.Write(.header[:8])
if .err != nil {
return
}
_, .err = .w.Write()
if .err != nil {
return
}
_, .err = .w.Write(.footer[:4])
}
func ( *encoder) () {
:= .m.Bounds()
binary.BigEndian.PutUint32(.tmp[0:4], uint32(.Dx()))
binary.BigEndian.PutUint32(.tmp[4:8], uint32(.Dy()))
switch .cb {
case cbG8:
.tmp[8] = 8
.tmp[9] = ctGrayscale
case cbTC8:
.tmp[8] = 8
.tmp[9] = ctTrueColor
case cbP8:
.tmp[8] = 8
.tmp[9] = ctPaletted
case cbP4:
.tmp[8] = 4
.tmp[9] = ctPaletted
case cbP2:
.tmp[8] = 2
.tmp[9] = ctPaletted
case cbP1:
.tmp[8] = 1
.tmp[9] = ctPaletted
case cbTCA8:
.tmp[8] = 8
.tmp[9] = ctTrueColorAlpha
case cbG16:
.tmp[8] = 16
.tmp[9] = ctGrayscale
case cbTC16:
.tmp[8] = 16
.tmp[9] = ctTrueColor
case cbTCA16:
.tmp[8] = 16
.tmp[9] = ctTrueColorAlpha
}
.tmp[10] = 0
.tmp[11] = 0
.tmp[12] = 0
.writeChunk(.tmp[:13], "IHDR")
}
func ( *encoder) ( color.Palette) {
if len() < 1 || len() > 256 {
.err = FormatError("bad palette length: " + strconv.Itoa(len()))
return
}
:= -1
for , := range {
:= color.NRGBAModel.Convert().(color.NRGBA)
.tmp[3*+0] = .R
.tmp[3*+1] = .G
.tmp[3*+2] = .B
if .A != 0xff {
=
}
.tmp[3*256+] = .A
}
.writeChunk(.tmp[:3*len()], "PLTE")
if != -1 {
.writeChunk(.tmp[3*256:3*256+1+], "tRNS")
}
}
func ( *encoder) ( []byte) (int, error) {
.writeChunk(, "IDAT")
if .err != nil {
return 0, .err
}
return len(), nil
}
func ( *[nFilter][]byte, []byte, int) int {
:= [0][1:]
:= [1][1:]
:= [2][1:]
:= [3][1:]
:= [4][1:]
:= [1:]
:= len()
:= 0
for := 0; < ; ++ {
[] = [] - []
+= abs8([])
}
:=
:= ftUp
= 0
for := 0; < ; ++ {
[] = [] - []
+= abs8([])
}
for := ; < ; ++ {
[] = [] - paeth([-], [], [-])
+= abs8([])
if >= {
break
}
}
if < {
=
= ftPaeth
}
= 0
for := 0; < ; ++ {
+= abs8([])
if >= {
break
}
}
if < {
=
= ftNone
}
= 0
for := 0; < ; ++ {
[] = []
+= abs8([])
}
for := ; < ; ++ {
[] = [] - [-]
+= abs8([])
if >= {
break
}
}
if < {
=
= ftSub
}
= 0
for := 0; < ; ++ {
[] = [] - []/2
+= abs8([])
}
for := ; < ; ++ {
[] = [] - uint8((int([-])+int([]))/2)
+= abs8([])
if >= {
break
}
}
if < {
= ftAverage
}
return
}
func ( *encoder) ( io.Writer, image.Image, int, int) error {
if .zw == nil || .zwLevel != {
, := zlib.NewWriterLevel(, )
if != nil {
return
}
.zw =
.zwLevel =
} else {
.zw.Reset()
}
defer .zw.Close()
:= 0
switch {
case cbG8:
= 8
case cbTC8:
= 24
case cbP8:
= 8
case cbP4:
= 4
case cbP2:
= 2
case cbP1:
= 1
case cbTCA8:
= 32
case cbTC16:
= 48
case cbTCA16:
= 64
case cbG16:
= 16
}
:= .Bounds()
:= 1 + (*.Dx()+7)/8
for := range .cr {
if cap(.cr[]) < {
.cr[] = make([]uint8, )
} else {
.cr[] = .cr[][:]
}
.cr[][0] = uint8()
}
:= .cr
if cap(.pr) < {
.pr = make([]uint8, )
} else {
.pr = .pr[:]
clear(.pr)
}
:= .pr
, := .(*image.Gray)
, := .(*image.RGBA)
, := .(*image.Paletted)
, := .(*image.NRGBA)
for := .Min.Y; < .Max.Y; ++ {
:= 1
switch {
case cbG8:
if != nil {
:= ( - .Min.Y) * .Stride
copy([0][1:], .Pix[:+.Dx()])
} else {
for := .Min.X; < .Max.X; ++ {
:= color.GrayModel.Convert(.At(, )).(color.Gray)
[0][] = .Y
++
}
}
case cbTC8:
:= [0]
, := 0, []byte(nil)
if != nil {
, = .Stride, .Pix
} else if != nil {
, = .Stride, .Pix
}
if != 0 {
:= ( - .Min.Y) *
:= + .Dx()*4
for := ; < ; += 4 {
[+0] = [+0]
[+1] = [+1]
[+2] = [+2]
+= 3
}
} else {
for := .Min.X; < .Max.X; ++ {
, , , := .At(, ).RGBA()
[+0] = uint8( >> 8)
[+1] = uint8( >> 8)
[+2] = uint8( >> 8)
+= 3
}
}
case cbP8:
if != nil {
:= ( - .Min.Y) * .Stride
copy([0][1:], .Pix[:+.Dx()])
} else {
:= .(image.PalettedImage)
for := .Min.X; < .Max.X; ++ {
[0][] = .ColorIndexAt(, )
+= 1
}
}
case cbP4, cbP2, cbP1:
:= .(image.PalettedImage)
var uint8
var int
:= 8 /
for := .Min.X; < .Max.X; ++ {
= <<uint() | .ColorIndexAt(, )
++
if == {
[0][] =
+= 1
= 0
= 0
}
}
if != 0 {
for != {
= << uint()
++
}
[0][] =
}
case cbTCA8:
if != nil {
:= ( - .Min.Y) * .Stride
copy([0][1:], .Pix[:+.Dx()*4])
} else if != nil {
:= [0][1:]
:= .Pix[.PixOffset(.Min.X, ):.PixOffset(.Max.X, )]
for ; len() >= 4; , = [4:], [4:] {
:= (*[4]byte)()
:= (*[4]byte)()
if [3] == 0x00 {
[0] = 0
[1] = 0
[2] = 0
[3] = 0
} else if [3] == 0xff {
copy([:], [:])
} else {
const = 0x101 * 0xffff
:= uint32([3]) * 0x101
[0] = uint8((uint32([0]) * / ) >> 8)
[1] = uint8((uint32([1]) * / ) >> 8)
[2] = uint8((uint32([2]) * / ) >> 8)
[3] = [3]
}
}
} else {
for := .Min.X; < .Max.X; ++ {
:= color.NRGBAModel.Convert(.At(, )).(color.NRGBA)
[0][+0] = .R
[0][+1] = .G
[0][+2] = .B
[0][+3] = .A
+= 4
}
}
case cbG16:
for := .Min.X; < .Max.X; ++ {
:= color.Gray16Model.Convert(.At(, )).(color.Gray16)
[0][+0] = uint8(.Y >> 8)
[0][+1] = uint8(.Y)
+= 2
}
case cbTC16:
for := .Min.X; < .Max.X; ++ {
, , , := .At(, ).RGBA()
[0][+0] = uint8( >> 8)
[0][+1] = uint8()
[0][+2] = uint8( >> 8)
[0][+3] = uint8()
[0][+4] = uint8( >> 8)
[0][+5] = uint8()
+= 6
}
case cbTCA16:
for := .Min.X; < .Max.X; ++ {
:= color.NRGBA64Model.Convert(.At(, )).(color.NRGBA64)
[0][+0] = uint8(.R >> 8)
[0][+1] = uint8(.R)
[0][+2] = uint8(.G >> 8)
[0][+3] = uint8(.G)
[0][+4] = uint8(.B >> 8)
[0][+5] = uint8(.B)
[0][+6] = uint8(.A >> 8)
[0][+7] = uint8(.A)
+= 8
}
}
:= ftNone
if != zlib.NoCompression && != cbP8 && != cbP4 && != cbP2 && != cbP1 {
:= / 8
= filter(&, , )
}
if , := .zw.Write([]); != nil {
return
}
, [0] = [0],
}
return nil
}
func ( *encoder) () {
if .err != nil {
return
}
if .bw == nil {
.bw = bufio.NewWriterSize(, 1<<15)
} else {
.bw.Reset()
}
.err = .writeImage(.bw, .m, .cb, levelToZlib(.enc.CompressionLevel))
if .err != nil {
return
}
.err = .bw.Flush()
}
func ( CompressionLevel) int {
switch {
case DefaultCompression:
return zlib.DefaultCompression
case NoCompression:
return zlib.NoCompression
case BestSpeed:
return zlib.BestSpeed
case BestCompression:
return zlib.BestCompression
default:
return zlib.DefaultCompression
}
}
func ( *encoder) () { .writeChunk(nil, "IEND") }
func ( io.Writer, image.Image) error {
var Encoder
return .Encode(, )
}
func ( *Encoder) ( io.Writer, image.Image) error {
, := int64(.Bounds().Dx()), int64(.Bounds().Dy())
if <= 0 || <= 0 || >= 1<<32 || >= 1<<32 {
return FormatError("invalid image size: " + strconv.FormatInt(, 10) + "x" + strconv.FormatInt(, 10))
}
var *encoder
if .BufferPool != nil {
:= .BufferPool.Get()
= (*encoder)()
}
if == nil {
= &encoder{}
}
if .BufferPool != nil {
defer .BufferPool.Put((*EncoderBuffer)())
}
.enc =
.w =
.m =
var color.Palette
if , := .(image.PalettedImage); {
, _ = .ColorModel().(color.Palette)
}
if != nil {
if len() <= 2 {
.cb = cbP1
} else if len() <= 4 {
.cb = cbP2
} else if len() <= 16 {
.cb = cbP4
} else {
.cb = cbP8
}
} else {
switch .ColorModel() {
case color.GrayModel:
.cb = cbG8
case color.Gray16Model:
.cb = cbG16
case color.RGBAModel, color.NRGBAModel, color.AlphaModel:
if opaque() {
.cb = cbTC8
} else {
.cb = cbTCA8
}
default:
if opaque() {
.cb = cbTC16
} else {
.cb = cbTCA16
}
}
}
_, .err = io.WriteString(, pngHeader)
.writeIHDR()
if != nil {
.writePLTEAndTRNS()
}
.writeIDATs()
.writeIEND()
return .err
}