-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
68 lines (55 loc) · 1.37 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 tigrfont
import (
"image"
"image/color"
)
func contentBounds(img *image.NRGBA) image.Rectangle {
minNonTransparentRow := img.Bounds().Max.Y
maxNonTransparentRow := img.Bounds().Min.Y
rows:
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
cols:
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
color := img.At(x, y)
if color == Border.C {
continue cols
}
_, _, _, a := color.RGBA()
if a != 0 {
// non-transparent
if y < minNonTransparentRow {
minNonTransparentRow = y
}
if y > maxNonTransparentRow {
maxNonTransparentRow = y
}
continue rows
}
}
}
return image.Rect(img.Bounds().Min.X, minNonTransparentRow, img.Bounds().Max.X, maxNonTransparentRow+1)
}
func whitePalette() color.Palette {
p := make(color.Palette, 256)
for a := 0; a < 256; a++ {
p[a] = color.NRGBA{255, 255, 255, uint8(a)}
}
return p
}
func palettize(img *image.NRGBA) (image.Image, error) {
palette := whitePalette()
pi := image.NewPaletted(img.Bounds(), palette)
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
alpha := img.Pix[img.PixOffset(x, y)+3]
pi.Pix[pi.PixOffset(x, y)] = alpha
}
}
return pi, nil
}
func clear(img *image.NRGBA) {
rgba := [...]uint8{0xff, 0xff, 0xff, 0x00}
for i := range img.Pix {
img.Pix[i] = rgba[i%4]
}
}