forked from HugoSmits86/nativewebp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.go
executable file
·240 lines (201 loc) · 7.67 KB
/
transform.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package nativewebp
import (
//------------------------------
//general
//------------------------------
"math"
"slices"
//------------------------------
//imaging
//------------------------------
"image/color"
//------------------------------
//errors
//------------------------------
//"log"
"errors"
)
type transform int
const (
transformPredict = transform(0)
transformColor = transform(1)
transformSubGreen = transform(2)
transformColorIndexing = transform(3)
)
func applyPredictTransform(pixels []color.NRGBA, width, height int) (int, int, int, []color.NRGBA) {
tileBits := 4
tileSize := 1 << tileBits
bw := (width + tileSize - 1) / tileSize
bh := (height + tileSize - 1) / tileSize
blocks := make([]color.NRGBA, bw * bh)
deltas := make([]color.NRGBA, width * height)
//TODO: analyze block and pick best filter
best := 1
for y := 0; y < bh; y++ {
for x := 0; x < bw; x++ {
mx := min((x + 1) << tileBits, width)
my := min((y + 1) << tileBits, height)
for tx := x << tileBits; tx < mx; tx++ {
for ty := y << tileBits; ty < my; ty++ {
d := applyFilter(pixels, width, tx, ty, best)
off := ty * width + tx
deltas[off] = color.NRGBA{
R: uint8(pixels[off].R - d.R),
G: uint8(pixels[off].G - d.G),
B: uint8(pixels[off].B - d.B),
A: uint8(pixels[off].A - d.A),
}
}
}
blocks[y * bw + x] = color.NRGBA{0, byte(best), 0, 255}
}
}
copy(pixels, deltas)
return tileBits, bw, bh, blocks
}
func applyFilter(pixels []color.NRGBA, width, x, y, prediction int) color.NRGBA {
if x == 0 && y == 0 {
return color.NRGBA{0, 0, 0, 255}
} else if x == 0 {
return pixels[(y - 1) * width + x]
} else if y == 0 {
return pixels[y * width + (x - 1)]
}
t := pixels[(y - 1) * width + x]
l := pixels[y * width + (x - 1)]
tl := pixels[(y - 1) * width + (x - 1)]
tr := pixels[(y - 1) * width + (x + 1)]
avarage2 := func(a, b color.NRGBA) color.NRGBA {
return color.NRGBA {
uint8((int(a.R) + int(b.R)) / 2),
uint8((int(a.G) + int(b.G)) / 2),
uint8((int(a.B) + int(b.B)) / 2),
uint8((int(a.A) + int(b.A)) / 2),
}
}
filters := []func(t, l, tl, tr color.NRGBA) color.NRGBA {
func(t, l, tl, tr color.NRGBA) color.NRGBA { return color.NRGBA{0, 0, 0, 255} },
func(t, l, tl, tr color.NRGBA) color.NRGBA { return l },
func(t, l, tl, tr color.NRGBA) color.NRGBA { return t },
func(t, l, tl, tr color.NRGBA) color.NRGBA { return tr },
func(t, l, tl, tr color.NRGBA) color.NRGBA { return tl },
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(avarage2(l, tr), t)
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(l, tl)
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(l, t)
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(tl, t)
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(t, tr)
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return avarage2(avarage2(l, tl), avarage2(t, tr))
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
pr := float64(l.R) + float64(t.R) - float64(tl.R)
pg := float64(l.G) + float64(t.G) - float64(tl.G)
pb := float64(l.B) + float64(t.B) - float64(tl.B)
pa := float64(l.A) + float64(t.A) - float64(tl.A)
// Manhattan distances to estimates for left and top pixels.
pl := math.Abs(pa - float64(l.A)) + math.Abs(pr - float64(l.R)) +
math.Abs(pg - float64(l.G)) + math.Abs(pb - float64(l.B))
pt := math.Abs(pa - float64(t.A)) + math.Abs(pr - float64(t.R)) +
math.Abs(pg - float64(t.G)) + math.Abs(pb - float64(t.B))
if pl < pt {
return l
}
return t
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
return color.NRGBA{
uint8(max(min(int(l.R) + int(t.R) - int(tl.R), 255), 0)),
uint8(max(min(int(l.G) + int(t.G) - int(tl.G), 255), 0)),
uint8(max(min(int(l.B) + int(t.B) - int(tl.B), 255), 0)),
uint8(max(min(int(l.A) + int(t.A) - int(tl.A), 255), 0)),
}
},
func(t, l, tl, tr color.NRGBA) color.NRGBA {
a := avarage2(l, t)
return color.NRGBA{
uint8(max(min(int(a.R) + (int(a.R) - int(tl.R)) / 2, 255), 0)),
uint8(max(min(int(a.G) + (int(a.G) - int(tl.G)) / 2, 255), 0)),
uint8(max(min(int(a.B) + (int(a.B) - int(tl.B)) / 2, 255), 0)),
uint8(max(min(int(a.A) + (int(a.A) - int(tl.A)) / 2, 255), 0)),
}
},
}
return filters[prediction](t, l, tl, tr)
}
func applyColorTransform(pixels []color.NRGBA, width, height int) (int, int, int, []color.NRGBA) {
tileBits := 4
tileSize := 1 << tileBits
bw := (width + tileSize - 1) / tileSize
bh := (height + tileSize - 1) / tileSize
blocks := make([]color.NRGBA, bw * bh)
deltas := make([]color.NRGBA, width * height)
//TODO: analyze block and pick best Color transform Element (CTE)
cte := color.NRGBA {
R: 1, //red to blue
G: 2, //green to blue
B: 3, //green to red
A: 255,
}
for y := 0; y < bh; y++ {
for x := 0; x < bw; x++ {
mx := min((x + 1) << tileBits, width)
my := min((y + 1) << tileBits, height)
for tx := x << tileBits; tx < mx; tx++ {
for ty := y << tileBits; ty < my; ty++ {
off := ty * width + tx
r := int(int8(pixels[off].R))
g := int(int8(pixels[off].G))
b := int(int8(pixels[off].B))
b -= int(int8((int16(int8(cte.G)) * int16(g)) >> 5))
b -= int(int8((int16(int8(cte.R)) * int16(r)) >> 5))
r -= int(int8((int16(int8(cte.B)) * int16(g)) >> 5))
pixels[off].R = uint8(r & 0xff)
pixels[off].B = uint8(b & 0xff)
deltas[off] = pixels[off]
}
}
blocks[y * bw + x] = cte
}
}
copy(pixels, deltas)
return tileBits, bw, bh, blocks
}
func applySubtractGreenTransform(pixels []color.NRGBA) {
for i, _ := range pixels {
pixels[i].R = pixels[i].R - pixels[i].G
pixels[i].B = pixels[i].B - pixels[i].G
}
}
func applyPaletteTransform(pixels []color.NRGBA) ([]color.NRGBA, error) {
var pal []color.NRGBA
for _, p := range pixels {
if !slices.Contains(pal, p) {
pal = append(pal, p)
}
if len(pal) > 256 {
return nil, errors.New("palette exceeds 256 colors")
}
}
for i, p := range pixels {
pixels[i] = color.NRGBA{G: uint8(slices.Index(pal, p)), A: 255}
}
for i := len(pal) - 1; i > 0; i-- {
pal[i] = color.NRGBA{
R: pal[i].R - pal[i - 1].R,
G: pal[i].G - pal[i - 1].G,
B: pal[i].B - pal[i - 1].B,
A: pal[i].A - pal[i - 1].A,
}
}
return pal, nil
}