-
Notifications
You must be signed in to change notification settings - Fork 11
/
image.go
270 lines (216 loc) · 5.7 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package imghash
import (
"image"
"image/color"
)
// grayscale turns the image into a grayscale image.
func grayscale(img image.Image) image.Image {
rect := img.Bounds()
gray := image.NewGray(rect)
var x, y int
for y = rect.Min.Y; y < rect.Max.Y; y++ {
for x = rect.Min.X; x < rect.Max.X; x++ {
gray.Set(x, y, img.At(x, y))
}
}
return gray
}
// average converts the sums to averages and returns the result.
func average(sum []uint64, w, h int, n uint64) image.Image {
ret := image.NewRGBA(image.Rect(0, 0, w, h))
pix := ret.Pix
var x, y, idx int
for y = 0; y < h; y++ {
for x = 0; x < w; x++ {
idx = 4 * (y*w + x)
pix[idx] = uint8(sum[idx] / n)
pix[idx+1] = uint8(sum[idx+1] / n)
pix[idx+2] = uint8(sum[idx+2] / n)
pix[idx+3] = uint8(sum[idx+3] / n)
}
}
return ret
}
// resize returns a scaled copy of the image slice r of m.
// The returned image has width w and height h.
func resize(m image.Image, w, h int) image.Image {
if w < 0 || h < 0 {
return nil
}
r := m.Bounds()
if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
return image.NewRGBA64(image.Rect(0, 0, w, h))
}
switch m := m.(type) {
case *image.RGBA:
return resizeRGBA(m, r, w, h)
case *image.YCbCr:
if m, ok := resizeYCbCr(m, r, w, h); ok {
return m
}
}
ww, hh := uint64(w), uint64(h)
dx, dy := uint64(r.Dx()), uint64(r.Dy())
n, sum := dx*dy, make([]uint64, 4*w*h)
var x, y int
var r32, g32, b32, a32 uint32
var r64, g64, b64, a64, remx, remy, index uint64
var py, px, qx, qy uint64
minx, miny := r.Min.X, r.Min.Y
maxx, maxy := r.Max.X, r.Max.Y
for y = miny; y < maxy; y++ {
for x = minx; x < maxx; x++ {
// Get the source pixel.
r32, g32, b32, a32 = m.At(x, y).RGBA()
r64 = uint64(r32)
g64 = uint64(g32)
b64 = uint64(b32)
a64 = uint64(a32)
// Spread the source pixel over 1 or more destination rows.
py = uint64(y) * hh
for remy = hh; remy > 0; {
qy = dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px = uint64(x) * ww
index = 4 * ((py/dy)*ww + (px / dx))
for remx = ww; remx > 0; {
qx = dx - (px % dx)
if qx > remx {
qx = remx
}
sum[index] += r64 * qx * qy
sum[index+1] += g64 * qx * qy
sum[index+2] += b64 * qx * qy
sum[index+3] += a64 * qx * qy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
return average(sum, w, h, n*0x0101)
}
// resizeYCbCr returns a scaled copy of the YCbCr image slice r of m.
// The returned image has width w and height h.
func resizeYCbCr(m *image.YCbCr, r image.Rectangle, w, h int) (image.Image, bool) {
var verticalRes int
switch m.SubsampleRatio {
case image.YCbCrSubsampleRatio420:
verticalRes = 2
case image.YCbCrSubsampleRatio422:
verticalRes = 1
default:
return nil, false
}
ww, hh := uint64(w), uint64(h)
dx, dy := uint64(r.Dx()), uint64(r.Dy())
n, sum := dx*dy, make([]uint64, 4*w*h)
var x, y int
var r8, g8, b8 uint8
var r64, g64, b64, remx, remy, index uint64
var py, px, qx, qy, qxy uint64
var Y, Cb, Cr []uint8
minx, miny := r.Min.X, r.Min.Y
maxx, maxy := r.Max.X, r.Max.Y
for y = miny; y < maxy; y++ {
Y = m.Y[y*m.YStride:]
Cb = m.Cb[y/verticalRes*m.CStride:]
Cr = m.Cr[y/verticalRes*m.CStride:]
for x = minx; x < maxx; x++ {
// Get the source pixel.
r8, g8, b8 = color.YCbCrToRGB(Y[x], Cb[x/2], Cr[x/2])
r64 = uint64(r8)
g64 = uint64(g8)
b64 = uint64(b8)
// Spread the source pixel over 1 or more destination rows.
py = uint64(y) * hh
for remy = hh; remy > 0; {
qy = dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px = uint64(x) * ww
index = 4 * ((py/dy)*ww + (px / dx))
for remx = ww; remx > 0; {
qx = dx - (px % dx)
if qx > remx {
qx = remx
}
qxy = qx * qy
sum[index] += r64 * qxy
sum[index+1] += g64 * qxy
sum[index+2] += b64 * qxy
sum[index+3] += 0xFFFF * qxy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
return average(sum, w, h, n), true
}
// resizeRGBA returns a scaled copy of the RGBA image slice r of m.
// The returned image has width w and height h.
func resizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) image.Image {
ww, hh := uint64(w), uint64(h)
dx, dy := uint64(r.Dx()), uint64(r.Dy())
n, sum := dx*dy, make([]uint64, 4*w*h)
var x, y int
var pixOffset int
var r64, g64, b64, a64, remx, remy, index uint64
var py, px, qx, qy, qxy uint64
minx, miny := r.Min.X, r.Min.Y
maxx, maxy := r.Max.X, r.Max.Y
for y = miny; y < maxy; y++ {
pixOffset = m.PixOffset(minx, y)
for x = minx; x < maxx; x++ {
// Get the source pixel.
r64 = uint64(m.Pix[pixOffset+0])
g64 = uint64(m.Pix[pixOffset+1])
b64 = uint64(m.Pix[pixOffset+2])
a64 = uint64(m.Pix[pixOffset+3])
pixOffset += 4
// Spread the source pixel over 1 or more destination rows.
py = uint64(y) * hh
for remy = hh; remy > 0; {
qy = dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px = uint64(x) * ww
index = 4 * ((py/dy)*ww + (px / dx))
for remx = ww; remx > 0; {
qx = dx - (px % dx)
if qx > remx {
qx = remx
}
qxy = qx * qy
sum[index] += r64 * qxy
sum[index+1] += g64 * qxy
sum[index+2] += b64 * qxy
sum[index+3] += a64 * qxy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
return average(sum, w, h, n)
}