-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpng.go
56 lines (45 loc) · 811 Bytes
/
png.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"image"
"image/color"
"image/png"
"os"
)
type PngImage struct {
img *Image128
}
func (p *PngImage) ColorModel() color.Model {
return color.RGBA64Model
}
func (p *PngImage) Bounds() image.Rectangle {
return p.img.Bounds()
}
func (p *PngImage) At(x int, y int) color.Color {
c := p.img.At(x, y)
r, g, b, a := c.RGBA()
return color.RGBA64{
R: uint16(r >> 16),
G: uint16(g >> 16),
B: uint16(b >> 16),
A: uint16(a >> 16),
}
}
func savePng(img *Image128, fileName string) error {
pi := &PngImage{img: img}
outFile, err := os.Create(fileName)
if err != nil {
return err
}
defer outFile.Close()
bf := bufio.NewWriter(outFile)
err = png.Encode(bf, pi)
if err != nil {
return err
}
err = bf.Flush()
if err != nil {
return err
}
return nil
}