-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathanalyze.go
More file actions
230 lines (197 loc) · 4.93 KB
/
analyze.go
File metadata and controls
230 lines (197 loc) · 4.93 KB
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
package fennec
import (
"image"
"math"
)
// ImageStats contains analysis results for an image.
type ImageStats struct {
Width, Height int
HasAlpha bool
IsGrayscale bool
UniqueColors int
Entropy float64
EdgeDensity float64
MeanBrightness float64
Contrast float64
RecommendedFormat Format
RecommendedQuality Quality
EstimatedCompression float64
}
// Analyze performs comprehensive image analysis to inform compression decisions.
// Uses toNRGBARef for zero-copy when the input is already NRGBA.
func Analyze(img image.Image) ImageStats {
src := toNRGBARef(img)
w := src.Bounds().Dx()
h := src.Bounds().Dy()
stats := ImageStats{
Width: w,
Height: h,
}
if w == 0 || h == 0 {
return stats
}
// Single pass: collect color info, brightness, alpha.
histogram := [256]float64{}
var brightSum float64
colorSet := make(map[uint32]struct{})
maxSample := 50000
step := 1
if w*h > maxSample {
step = w * h / maxSample
}
allGray := true
hasAlpha := false
idx := 0
for y := 0; y < h; y++ {
off := y * src.Stride
for x := 0; x < w; x++ {
i := off + x*4
r := src.Pix[i]
g := src.Pix[i+1]
b := src.Pix[i+2]
a := src.Pix[i+3]
lum := 0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)
brightSum += lum
histogram[int(lum+0.5)]++
if a < 255 {
hasAlpha = true
}
if r != g || g != b {
allGray = false
}
if idx%step == 0 && len(colorSet) < 1024 {
key := uint32(r)<<24 | uint32(g)<<16 | uint32(b)<<8 | uint32(a)
colorSet[key] = struct{}{}
}
idx++
}
}
n := float64(w * h)
stats.HasAlpha = hasAlpha
stats.IsGrayscale = allGray
stats.UniqueColors = len(colorSet)
stats.MeanBrightness = brightSum / n
// Compute contrast with consistent fixed-grid sampling.
stepY := int(math.Max(1, math.Ceil(float64(h)/100)))
stepX := int(math.Max(1, math.Ceil(float64(w)/100)))
var varianceSum float64
var sampleCount int
mean := stats.MeanBrightness
for y := 0; y < h; y += stepY {
off := y * src.Stride
for x := 0; x < w; x += stepX {
i := off + x*4
lum := 0.299*float64(src.Pix[i]) + 0.587*float64(src.Pix[i+1]) + 0.114*float64(src.Pix[i+2])
d := lum - mean
varianceSum += d * d
sampleCount++
}
}
if sampleCount > 0 {
stats.Contrast = math.Sqrt(varianceSum / float64(sampleCount))
}
// Compute entropy from luminance histogram.
stats.Entropy = computeEntropy(histogram[:], n)
// Compute edge density using Sobel operator (sampled).
stats.EdgeDensity = computeEdgeDensity(src)
// Make recommendations.
stats.RecommendedFormat = recommendFormat(stats)
stats.RecommendedQuality = recommendQuality(stats)
stats.EstimatedCompression = estimateCompression(stats)
return stats
}
// computeEntropy calculates Shannon entropy from a histogram.
func computeEntropy(histogram []float64, total float64) float64 {
if total == 0 {
return 0
}
var entropy float64
for _, count := range histogram {
if count > 0 {
p := count / total
entropy -= p * math.Log2(p)
}
}
return entropy
}
// computeEdgeDensity uses a Sobel operator to detect edges.
func computeEdgeDensity(img *image.NRGBA) float64 {
w := img.Bounds().Dx()
h := img.Bounds().Dy()
if w < 3 || h < 3 {
return 0
}
stepX := int(math.Max(1, float64(w)/200))
stepY := int(math.Max(1, float64(h)/200))
edgeCount := 0
totalCount := 0
threshold := 30.0
for y := 1; y < h-1; y += stepY {
for x := 1; x < w-1; x += stepX {
gx := sobelLum(img, x+1, y-1) - sobelLum(img, x-1, y-1) +
2*sobelLum(img, x+1, y) - 2*sobelLum(img, x-1, y) +
sobelLum(img, x+1, y+1) - sobelLum(img, x-1, y+1)
gy := sobelLum(img, x-1, y+1) - sobelLum(img, x-1, y-1) +
2*sobelLum(img, x, y+1) - 2*sobelLum(img, x, y-1) +
sobelLum(img, x+1, y+1) - sobelLum(img, x+1, y-1)
mag := math.Sqrt(gx*gx + gy*gy)
if mag > threshold {
edgeCount++
}
totalCount++
}
}
if totalCount == 0 {
return 0
}
return float64(edgeCount) / float64(totalCount)
}
func sobelLum(img *image.NRGBA, x, y int) float64 {
off := y*img.Stride + x*4
return 0.299*float64(img.Pix[off]) + 0.587*float64(img.Pix[off+1]) + 0.114*float64(img.Pix[off+2])
}
func recommendFormat(stats ImageStats) Format {
if stats.HasAlpha {
return PNG
}
if stats.UniqueColors <= 256 {
return PNG
}
if stats.EdgeDensity > 0.3 && stats.UniqueColors < 1000 {
return PNG
}
return JPEG
}
func recommendQuality(stats ImageStats) Quality {
if stats.Entropy > 6 && stats.EdgeDensity < 0.15 {
return Balanced
}
if stats.Entropy < 4 {
return Aggressive
}
if stats.EdgeDensity > 0.25 {
return High
}
return Balanced
}
func estimateCompression(stats ImageStats) float64 {
if stats.RecommendedFormat == PNG {
if stats.UniqueColors <= 256 {
return 5.0 + (256-float64(stats.UniqueColors))/50
}
if stats.IsGrayscale {
return 3.0
}
return 2.0
}
base := 10.0
if stats.Entropy > 7 {
base = 5.0
} else if stats.Entropy > 5 {
base = 8.0
}
if stats.EdgeDensity > 0.2 {
base *= 0.7
}
return base
}