-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathimage.go
68 lines (59 loc) · 1.33 KB
/
image.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
package faker
import (
"image"
"image/color"
"image/png"
"io"
"os"
)
// PngEncoder encodes a image.Image to a io.Writer
type PngEncoder interface {
Encode(w io.Writer, m image.Image) error
}
// PngEncoderImpl is the default implementation of the PngEncoder
type PngEncoderImpl struct{}
// Encode does the encoding of the image.Image to an io.Writer
func (PngEncoderImpl) Encode(w io.Writer, m image.Image) error {
return png.Encode(w, m)
}
// Image is a faker struct for Image
type Image struct {
faker *Faker
TempFileCreator TempFileCreator
PngEncoder PngEncoder
}
// Image returns a fake image file
func (i Image) Image(width, height int) *os.File {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
black := color.RGBA{0, 0, 0, 0xff}
white := color.RGBA{0xff, 0xff, 0xff, 0xff}
step := 4
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
if y > 0 {
if x%step == 0 {
if y%step == 0 {
img.Set(x, y, black)
} else {
img.Set(x, y, white)
}
} else {
img.Set(x, y, white)
}
} else {
img.Set(x, y, white)
}
}
}
f, err := i.TempFileCreator.TempFile("fake-img-*.png")
if err != nil {
panic(err)
}
err = i.PngEncoder.Encode(f, img)
if err != nil {
panic(err)
}
return f
}