main.go raw
1 package main
2
3 import (
4 "image"
5 "image/color"
6 "image/png"
7 "os"
8 )
9
10 func main() {
11 // Create a 64x64 gradient image
12 img := image.NewRGBA(image.Rect(0, 0, 64, 64))
13 for y := 0; y < 64; y++ {
14 for x := 0; x < 64; x++ {
15 img.Set(x, y, color.RGBA{
16 R: uint8(x * 4),
17 G: uint8(y * 4),
18 B: uint8((x + y) * 2),
19 A: 255,
20 })
21 }
22 }
23
24 // Write to file
25 f, err := os.Create("/tmp/moxie_test.png")
26 if err != nil {
27 print("create error: ")
28 println(err.Error())
29 return
30 }
31
32 err = png.Encode(f, img)
33 f.Close()
34 if err != nil {
35 print("encode error: ")
36 println(err.Error())
37 return
38 }
39
40 // Read back
41 f2, err := os.Open("/tmp/moxie_test.png")
42 if err != nil {
43 print("open error: ")
44 println(err.Error())
45 return
46 }
47 decoded, err := png.Decode(f2)
48 f2.Close()
49 if err != nil {
50 print("decode error: ")
51 println(err.Error())
52 return
53 }
54
55 // Verify dimensions
56 b := decoded.Bounds()
57 print("decoded: ")
58 print(b.Dx())
59 print("x")
60 println(b.Dy())
61
62 // Verify a pixel
63 r, g, bl, a := decoded.At(32, 32).RGBA()
64 print("pixel(32,32): R=")
65 print(r >> 8)
66 print(" G=")
67 print(g >> 8)
68 print(" B=")
69 print(bl >> 8)
70 print(" A=")
71 println(a >> 8)
72
73 println("PNG encode/decode: PASS")
74 }
75