-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
213 lines (178 loc) · 3.71 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package notescan
import (
"fmt"
"image"
"image/color"
"image/gif"
"image/png"
"math"
"os"
)
// compressed png output file
func OutputPNG(f string, img image.Image) error {
out, err := os.Create(f)
if err != nil {
return err
}
defer out.Close()
var encoder png.Encoder
encoder.CompressionLevel = png.BestCompression
return encoder.Encode(out, img)
}
var gifPalette color.Palette = nil
// Creation of gif palette to do color reduction
func setGIFPalette(bg *Pixel, fg Pixels) {
gifPalette = make(color.Palette, len(fg)+1)
gifPalette[0] = bg.Color()
for i, pixel := range fg {
gifPalette[i+1] = pixel.Color()
}
}
// compressed gif output file
func OutputGIF(f string, img image.Image) error {
if gifPalette == nil {
return fmt.Errorf("Palette is nil")
}
out, err := os.Create(f)
if err != nil {
return err
}
defer out.Close()
opt := &gif.Options{
NumColors: len(gifPalette),
Quantizer: NewQuantizer(gifPalette),
}
return gif.Encode(out, img, opt)
}
// gif quantizer
type gifQuantizer struct {
palette color.Palette
}
// create new quantizer
func NewQuantizer(p color.Palette) *gifQuantizer {
q := gifQuantizer{}
q.palette = p
return &q
}
// quantizer implementation
func (q gifQuantizer) Quantize(p color.Palette, img image.Image) color.Palette {
return q.palette
}
// convert image into pixels
func convertPixels(img image.Image) (Pixels, error) {
rect := img.Bounds()
cols := rect.Dx()
rows := rect.Dy()
rtn := make(Pixels, cols*rows)
idx := 0
for col := 0; col < cols; col++ {
for row := 0; row < rows; row++ {
color := img.At(col, row)
rtn[idx] = NewPixel(color)
idx++
}
}
return rtn, nil
}
// convert color to RGBA format
func convertColor(c color.Color) (*color.RGBA, error) {
switch c.(type) {
case color.YCbCr:
o := c.(color.YCbCr)
r, g, b := color.YCbCrToRGB(o.Y, o.Cb, o.Cr)
return UIntRGBA(r, g, b), nil
case color.RGBA:
newColor := c.(color.RGBA)
return &newColor, nil
case *color.RGBA:
newColor := c.(*color.RGBA)
return newColor, nil
default:
}
return nil, fmt.Errorf("Not supported color: [%v]", c)
}
// source: https://www.rapidtables.com/convert/color/rgb-to-hsv.html
func RGB2HSV(or, og, ob uint8) (float64, float64, float64) {
r := float64(or) / float64(255)
g := float64(og) / float64(255)
b := float64(ob) / float64(255)
max := math.Max(math.Max(r, g), b)
min := math.Min(math.Min(r, g), b)
d := max - min
h := 0.0
// hue calculation
switch {
case d == 0:
h = 0
case max == r:
h = math.Mod((g-b)/d, 6)
case max == g:
h = (b-r)/d + 2
case max == b:
h = (r-g)/d + 4
}
h = h / 6
if h < 0 {
h += 1.0
}
// saturation calculation
s := 0.0
if max != 0 {
s = d / max
}
// value calculation
v := max
return h, s, v
}
// source: https://www.rapidtables.com/convert/color/hsv-to-rgb.html
func HSV2RGBA(h, s, v float64) *color.RGBA {
hd := h * 360.0
if hd >= 360 {
hd = 359
}
hh := hd / 60
c := v * s
x := c * (1.0 - math.Abs(math.Mod(hh, 2)-1.0))
r := 0.0
g := 0.0
b := 0.0
switch {
case hh < 1:
r, g, b = c, x, 0
case hh < 2:
r, g, b = x, c, 0
case hh < 3:
r, g, b = 0, c, x
case hh < 4:
r, g, b = 0, x, c
case hh < 5:
r, g, b = x, 0, c
default:
r, g, b = c, 0, x
}
m := v - c
r += m
g += m
b += m
return FloatRGBA(r*255.0, g*255.0, b*255.0)
}
// create RGBA from float RGB values
func FloatRGBA(r, g, b float64) *color.RGBA {
ur := uint8(r)
ug := uint8(g)
ub := uint8(b)
if r < 255.0 {
ur = uint8(math.Floor(r + 0.5))
}
if g < 255.0 {
ug = uint8(math.Floor(g + 0.5))
}
if b < 255.0 {
ub = uint8(math.Floor(b + 0.5))
}
return UIntRGBA(ur, ug, ub)
}
// create RGBA from uint RGB values
func UIntRGBA(r, g, b uint8) *color.RGBA {
return &color.RGBA{R: r, G: g, B: b, A: 255}
}