-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative.go
More file actions
43 lines (38 loc) · 716 Bytes
/
negative.go
File metadata and controls
43 lines (38 loc) · 716 Bytes
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
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
// Negative takes pointer f to image file (PNG encoded) and output inverted PNG with fname
func Negative(f *os.File, fname *string) {
ri, err := png.Decode(f)
if err != nil {
fmt.Println(err)
}
or := ri.Bounds()
size := or.Size()
ni := image.NewRGBA(or)
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
r, g, b, _ := ri.At(x, y).RGBA()
ni.Set(x, y, color.RGBA{
R: 255 - uint8(r),
G: 255 - uint8(g),
B: 255 - uint8(b),
A: 255,
})
}
}
nf, err := os.Create(*fname)
if err != nil {
fmt.Println(err)
}
defer nf.Close()
err = png.Encode(nf, ni)
if err != nil {
fmt.Println(err)
}
}