-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfennec.go
More file actions
212 lines (185 loc) · 6.45 KB
/
fennec.go
File metadata and controls
212 lines (185 loc) · 6.45 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
// Package fennec provides intelligent image compression that dramatically reduces
// file size while preserving perceptual quality. It uses SSIM (Structural Similarity
// Index) guided optimization to find the sweet spot between size and quality.
//
// Fennec — Tiny Fox. Giant Ears. Hears what matters, drops what doesn't.
//
// Unlike traditional image processing libraries that apply fixed compression,
// Fennec analyzes each image and adapts its strategy:
//
// - SSIM-guided quality search: binary search for minimum file size at target quality
// - Perceptual color quantization: reduce colors where human eyes can't tell
// - Content-aware downscaling: smart resize that preserves important detail
// - Adaptive format selection: picks JPEG/PNG based on image characteristics
// - EXIF orientation handling: auto-rotates images from camera sensors
// - Batch processing: concurrent compression with worker pools
package fennec
import (
"bytes"
"context"
"fmt"
"image"
"io"
"os"
)
// CompressFile compresses an image file and writes the result to dst.
// It reads EXIF orientation data and auto-rotates if opts.AutoOrient is true.
// The context can be used to cancel long-running operations.
func CompressFile(ctx context.Context, src, dst string, opts Options) (*Result, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
if err := opts.reportProgress(ctx, StageAnalyzing, 0); err != nil {
return nil, err
}
img, orient, fileSize, err := openWithOrientation(src)
if err != nil {
return nil, err
}
result, err := compressImageInternal(ctx, img, orient, opts)
if err != nil {
return nil, err
}
result.OriginalSize = fileSize
result.computeStats()
if err := opts.reportProgress(ctx, StageWriting, 0.9); err != nil {
return nil, err
}
// Write the pre-computed compressed bytes directly.
data := result.CompressedData
if len(data) == 0 {
data, err = encodeToBytes(result.Image, result.Format, result.JPEGQuality)
if err != nil {
return nil, err
}
result.CompressedData = data
result.CompressedSize = int64(len(data))
result.computeStats()
}
if err := os.WriteFile(dst, data, 0644); err != nil {
return nil, fmt.Errorf("fennec: write %q: %w", dst, err)
}
if err := opts.reportProgress(ctx, StageWriting, 1.0); err != nil {
return nil, err
}
return result, nil
}
// CompressImage compresses an already-decoded image.
// The context can be used to cancel long-running operations.
func CompressImage(ctx context.Context, img image.Image, opts Options) (*Result, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
return compressImageInternal(ctx, img, OrientNormal, opts)
}
// Compress reads an image from r and returns the optimally compressed version.
// The context can be used to cancel long-running operations.
func Compress(ctx context.Context, r io.Reader, opts Options) (*Result, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
img, _, err := image.Decode(r)
if err != nil {
return nil, fmt.Errorf("fennec: decode: %w", err)
}
return compressImageInternal(ctx, img, OrientNormal, opts)
}
// CompressBytes compresses image data from a byte slice and returns the result.
// This is the most common API for server-side use: receive bytes → compress → return bytes.
func CompressBytes(ctx context.Context, data []byte, opts Options) (*Result, error) {
return Compress(ctx, bytes.NewReader(data), opts)
}
// compressImageInternal is the shared compression pipeline.
func compressImageInternal(ctx context.Context, img image.Image, orient Orientation, opts Options) (*Result, error) {
if img == nil {
return nil, ErrNilImage
}
bounds := img.Bounds()
if bounds.Dx() <= 0 || bounds.Dy() <= 0 {
return nil, ErrEmptyImage
}
result := &Result{OriginalDimensions: image.Pt(bounds.Dx(), bounds.Dy())}
src := toNRGBA(img)
if opts.AutoOrient && orient > OrientNormal {
src = ApplyOrientation(src, orient)
result.OriginalDimensions = image.Pt(src.Bounds().Dx(), src.Bounds().Dy())
}
if err := opts.reportProgress(ctx, StageResizing, 0.1); err != nil {
return nil, err
}
if opts.MaxWidth > 0 || opts.MaxHeight > 0 {
src = smartResize(src, opts.MaxWidth, opts.MaxHeight)
}
result.Image = src
result.FinalDimensions = image.Pt(src.Bounds().Dx(), src.Bounds().Dy())
if err := opts.reportProgress(ctx, StageCompressing, 0.2); err != nil {
return nil, err
}
if opts.TargetSize > 0 {
return handleTargetSizeMode(ctx, src, opts, result)
}
return handleStandardMode(ctx, src, opts, result)
}
func handleTargetSizeMode(ctx context.Context, src *image.NRGBA, opts Options, result *Result) (*Result, error) {
sr, err := hitTargetSize(ctx, src, opts.TargetSize, opts)
if err != nil {
return nil, fmt.Errorf("fennec: target-size compression: %w", err)
}
result.CompressedData = sr.data
result.Format = sr.format
result.JPEGQuality = sr.quality
result.SSIM = sr.ssim
result.FinalDimensions = image.Pt(sr.finalW, sr.finalH)
if sr.img != nil {
result.Image = sr.img
}
result.CompressedSize = int64(len(sr.data))
result.computeStats()
return result, nil
}
func handleStandardMode(ctx context.Context, src *image.NRGBA, opts Options, result *Result) (*Result, error) {
if opts.Format == Auto {
opts.Format = analyzeFormat(src)
}
result.Format = opts.Format
if err := opts.reportProgress(ctx, StageOptimizing, 0.3); err != nil {
return nil, err
}
var compressed encodingBuffer
switch opts.Format {
case PNG:
if err := compressPNG(src, &compressed, opts); err != nil {
return nil, fmt.Errorf("fennec: PNG compression: %w", err)
}
result.SSIM = 1.0
case JPEG:
target := opts.Quality.targetSSIM()
if opts.TargetSSIM > 0 && opts.TargetSSIM <= 1.0 {
target = opts.TargetSSIM
}
q, ssim, cachedData, err := compressJPEGOptimal(src, &compressed, target, opts)
if err != nil {
return nil, fmt.Errorf("fennec: JPEG compression: %w", err)
}
result.JPEGQuality, result.SSIM = q, ssim
if cachedData != nil {
compressed.Reset()
compressed.Write(cachedData)
}
default:
return nil, ErrUnsupportedFormat
}
if err := opts.reportProgress(ctx, StageEncoding, 0.9); err != nil {
return nil, err
}
result.CompressedData = compressed.Bytes()
result.CompressedSize = int64(compressed.Len())
result.computeStats()
return result, nil
}
// encodingBuffer is a bytes.Buffer wrapper that satisfies io.Writer.
// Named to reflect its purpose: buffering encoded image data during compression.
// It is NOT safe for concurrent use.
type encodingBuffer struct {
bytes.Buffer
}