forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (98 loc) · 2.22 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2015 Dmitry Vyukov. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"bytes"
"github.com/dvyukov/go-fuzz/gen"
"image"
"image/color"
"image/png"
)
func main() {
for {
img := createRandomImage(256)
buf := new(bytes.Buffer)
enc := &png.Encoder{CompressionLevel: png.CompressionLevel(gen.Rand(4) - 3)}
if err := enc.Encode(buf, img); err != nil {
panic(err)
}
gen.Emit(buf.Bytes(), nil, true)
}
}
type Image interface {
Set(x, y int, c color.Color)
}
func createRandomImage(maxPaletteSize int) image.Image {
r := image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{gen.Rand(32) + 1, gen.Rand(32) + 1}}
var img Image
switch gen.Rand(10) {
case 0:
img = image.NewAlpha(r)
case 1:
img = image.NewAlpha16(r)
case 2:
img = image.NewCMYK(r)
case 3:
img = image.NewGray(r)
case 4:
img = image.NewGray16(r)
case 5:
img = image.NewNRGBA(r)
case 6:
img = image.NewNRGBA64(r)
case 7:
img = image.NewPaletted(r, randPalette(maxPaletteSize))
case 8:
img = image.NewRGBA(r)
case 9:
img = image.NewRGBA64(r)
default:
panic("bad")
}
fill := gen.Rand(19)
var palette []color.Color
if fill == 17 {
palette = randPalette(maxPaletteSize)
}
for y := 0; y < r.Max.Y; y++ {
for x := 0; x < r.Max.X; x++ {
switch {
case fill <= 15:
img.Set(x, y, color.RGBA64{
^uint16(0) * uint16((fill>>0)&1),
^uint16(0) * uint16((fill>>1)&1),
^uint16(0) * uint16((fill>>2)&1),
^uint16(0) * uint16((fill>>3)&1),
})
case fill == 16:
img.Set(x, y, randColor())
case fill == 17:
img.Set(x, y, palette[gen.Rand(len(palette))])
case fill == 18:
if gen.Rand(3) != 0 {
img.Set(x, y, color.RGBA64{})
} else {
img.Set(x, y, randColor())
}
default:
panic("bad")
}
}
}
return img.(image.Image)
}
func randColor() color.Color {
return color.RGBA64{
uint16(gen.Rand(1 << 16)),
uint16(gen.Rand(1 << 16)),
uint16(gen.Rand(1 << 16)),
uint16(gen.Rand(1 << 16)),
}
}
func randPalette(maxPaletteSize int) color.Palette {
palette := make([]color.Color, gen.Rand(maxPaletteSize)+1)
for i := range palette {
palette[i] = randColor()
}
return palette
}