-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayimage.go
48 lines (36 loc) · 878 Bytes
/
grayimage.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
package main
import "os"
import "image"
import "image/color"
import "image/png"
type grayImage struct {
rect *image.Rectangle
img *image.Gray
f *os.File
}
func NewGrayImage(r image.Rectangle, fname string) *grayImage {
if thef, err := os.Create(fname) ; err != nil {
return nil
} else {
theimg := image.NewGray(r)
return &grayImage{rect: &r, f: thef, img: theimg}
}
}
func (gimg grayImage) Set(x,y, col int) {
gray := 255-uint8((col*5) % 256)
// gray := uint8(255)
if col == 0 {
gray = 0
}
gimg.img.SetGray(x,y, color.Gray{gray})
}
func (gimg grayImage) Max() image.Point { return gimg.rect.Max }
func (gimg grayImage) Min() image.Point { return gimg.rect.Min }
func (gimg grayImage) Sync() error {
gimg.f.Seek(0,0)
png.Encode(gimg.f, gimg.img)
return gimg.f.Sync()
}
func (gimg grayImage) Close() error {
return gimg.f.Close()
}