-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path10_raw_pixels2.go
More file actions
55 lines (49 loc) · 1.18 KB
/
10_raw_pixels2.go
File metadata and controls
55 lines (49 loc) · 1.18 KB
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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Čtrnáctá část
// Programovací jazyk Go a počítačová grafika (úvod)
// https://www.root.cz/clanky/programovaci-jazyk-go-a-pocitacova-grafika-uvod/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů ze čtrnácté části:
// https://github.com/tisnik/go-root/blob/master/article_14/README.md
//
// Demonstrační příklad číslo 10:
// Přímý přístup k jednotlivým pixelům; druhá varianta
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_14/10_raw_pixels2.html
package main
import (
"image"
"image/png"
"os"
)
const width = 256
const height = 256
func main() {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
outfile, err := os.Create("10.png")
if err != nil {
panic(err)
}
defer outfile.Close()
for y := 0; y < height; y++ {
scanline := img.Pix[img.Stride*y:]
i := 0
for x := 0; x < width; x++ {
scanline[i] = 0
i++
scanline[i] = byte(y)
i++
scanline[i] = 255
i++
scanline[i] = byte(x)
i++
}
}
png.Encode(outfile, img)
}