package kitty
import (
)
type Encoder struct {
Compress bool
Format int
}
func ( *Encoder) ( io.Writer, image.Image) error {
if == nil {
return nil
}
if .Compress {
:= zlib.NewWriter()
defer .Close()
=
}
if .Format == 0 {
.Format = RGBA
}
switch .Format {
case RGBA, RGB:
:= .Bounds()
for := .Min.Y; < .Max.Y; ++ {
for := .Min.X; < .Max.X; ++ {
, , , := .At(, ).RGBA()
switch .Format {
case RGBA:
.Write([]byte{byte( >> 8), byte( >> 8), byte( >> 8), byte( >> 8)})
case RGB:
.Write([]byte{byte( >> 8), byte( >> 8), byte( >> 8)})
}
}
}
case PNG:
if := png.Encode(, ); != nil {
return fmt.Errorf("failed to encode PNG: %w", )
}
default:
return fmt.Errorf("unsupported format: %d", .Format)
}
return nil
}