-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathimage.go
144 lines (123 loc) · 3.74 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"sort"
"github.com/esimov/colorquant"
"github.com/nfnt/resize"
)
// ColorMappedImage represents a gray image with reduced color palette,
// and an indexed map of quantized colors in the image, sorted from
// light to dark.
type ColorMappedImage struct {
image *image.Gray
colorMap map[int]int
}
// GithubPalette is the palette of GitHub contribution graph.
var GithubPalette = color.Palette([]color.Color{
color.RGBA{235, 237, 240, 255}, // No activity
color.RGBA{155, 233, 168, 255}, // Low activity
color.RGBA{64, 196, 99, 255},
color.RGBA{48, 161, 78, 255},
color.RGBA{33, 110, 57, 255}, // High activity
})
// ReadImage reads the image file at path and decodes it to PNG.
func ReadImage(imageFile string) (image.Image, error) {
file, err := os.Open(imageFile)
if err != nil {
return nil, err
}
defer file.Close()
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
img, _, err := image.Decode(file)
return img, err
}
// ResizeImage resizes the image to fit the GitHub contribution graph bounds.
func ResizeImage(img image.Image) image.Image {
bounds := img.Bounds()
if bounds.Max.Y > 7 {
w := uint(bounds.Max.X / (bounds.Max.Y / 7))
img = resize.Resize(w, 7, img, resize.NearestNeighbor)
}
return img
}
// ToGithubPalette converts gray colors in the quantified image to the GitHub
// activity palette.
func ToGithubPalette(img image.Image) image.Image {
colorMapped := getColorMappedImage(img)
bounds := img.Bounds()
githubImg := image.NewRGBA(bounds)
// Replace colors
for y := 0; y < bounds.Max.Y; y++ {
for x := 0; x < bounds.Max.X; x++ {
gray := colorMapped.image.GrayAt(x, y)
paletteIndex := colorMapped.colorMap[int(gray.Y)]
githubImg.Set(x, y, GithubPalette[paletteIndex])
}
}
return githubImg
}
// PreviewResult show a preview of the image in shell using block characters.
func PreviewResult(img image.Image) {
colorMapped := getColorMappedImage(img)
bounds := img.Bounds()
// Replace colors
for y := 0; y < bounds.Max.Y; y++ {
fmt.Println()
for x := 0; x < bounds.Max.X; x++ {
gray := colorMapped.image.GrayAt(x, y)
idx := colorMapped.colorMap[int(gray.Y)]
fmt.Print(PixelChr(idx))
}
}
fmt.Println()
}
// SavePNG stores the image as a PNG file.
func SavePNG(img image.Image, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return png.Encode(f, img)
}
// getImageWithColorMap returns the image with reduced colors in grayscale and
// includes its colormap.
func getColorMappedImage(img image.Image) ColorMappedImage {
bounds := img.Bounds()
// Drawing image onto a white surface to remove transparency
opaqueImg := image.NewRGBA(bounds)
draw.Draw(opaqueImg, bounds, image.White, image.Point{}, draw.Src)
draw.Draw(opaqueImg, bounds, img, image.Point{}, draw.Over)
// Reduce palette to number of possible colors in GitHub graph
quantizedImg := image.NewGray(bounds)
numColors := len(GithubPalette)
colorquant.NoDither.Quantize(opaqueImg, quantizedImg, numColors, true, true)
// Create map of all unique grays in the quantized image
// Mapped from gray.Y to GithubPalette index (populated later)
colorMap := map[int]int{}
for y := 0; y < bounds.Max.Y; y++ {
for x := 0; x < bounds.Max.X; x++ {
gray := quantizedImg.GrayAt(x, y)
colorMap[int(gray.Y)] = 0
}
}
// Create slice with grays sorted asc, darkest first
colorsInMap := []int{}
for k := range colorMap {
colorsInMap = append(colorsInMap, k)
}
sort.Ints(colorsInMap)
// Populate color map values with order of grays
for index, k := range colorsInMap {
colorMap[k] = len(colorsInMap) - 1 - index
}
return ColorMappedImage{
image: quantizedImg,
colorMap: colorMap,
}
}