Skip to content

Commit f7fd17a

Browse files
authored
Merge pull request #3 from setanarut/bugfix
paletted image encoding
2 parents 161a1df + 641cb3d commit f7fd17a

File tree

3 files changed

+210
-58
lines changed

3 files changed

+210
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515

1616
.gvm
1717
vendor/
18+
outPaletted.png
19+
outRGBA.png

_example/main.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,42 @@ import (
88
"github.com/setanarut/apng"
99
)
1010

11-
const FrameCount int = 40
11+
const FrameCount int = 30
1212

1313
func main() {
14-
1514
frames := make([]image.Image, FrameCount)
15+
// frames2 := make([]image.Image, FrameCount)
16+
1617
for i := range FrameCount {
17-
frames[i] = generateNoiseImage(600, 200)
18+
frames[i] = generatePalettedFrames(600, 200)
19+
// frames2[i] = generateRGBAFrames(600, 200)
1820
}
19-
apng.Save("out.png", frames, 3)
21+
apng.Save("outPaletted.png", frames, 7)
22+
// apng.Save("outRGBA.png", frames2, 7)
2023
}
2124

22-
func generateNoiseImage(width, height int) *image.RGBA {
25+
func generateRGBAFrames(width, height int) *image.RGBA {
2326
img := image.NewRGBA(image.Rect(0, 0, width, height))
2427
for y := range height {
2528
for x := range width {
26-
col := noisePalette[rand.IntN(4)]
27-
img.SetRGBA(x, y, col)
29+
c := plt[rand.IntN(3)]
30+
img.SetRGBA(x, y, c.(color.RGBA))
31+
}
32+
}
33+
return img
34+
}
35+
func generatePalettedFrames(width, height int) *image.Paletted {
36+
img := image.NewPaletted(image.Rect(0, 0, width, height), plt)
37+
for y := range height {
38+
for x := range width {
39+
img.SetColorIndex(x, y, uint8(rand.IntN(3)))
2840
}
2941
}
3042
return img
3143
}
3244

33-
var noisePalette = []color.RGBA{
34-
{R: 0, G: 0, B: 0, A: 255}, // Black
35-
{R: 255, G: 0, B: 0, A: 255}, // Red
36-
{R: 0, G: 255, B: 0, A: 255}, // Green
37-
{R: 0, G: 0, B: 255, A: 255}, // Blue
45+
var plt = []color.Color{
46+
color.RGBA{R: 0, G: 0, B: 0, A: 255}, // Black
47+
color.RGBA{R: 255, G: 255, B: 255, A: 255}, // White
48+
color.RGBA{R: 255, G: 0, B: 255, A: 255}, // White
3849
}

0 commit comments

Comments
 (0)