-
Notifications
You must be signed in to change notification settings - Fork 2
/
jpeg.go
321 lines (291 loc) · 8.73 KB
/
jpeg.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package imagecoding
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"math"
"unsafe"
"go.uber.org/zap"
)
// #cgo pkg-config: libturbojpeg
// #include <stdlib.h>
// #include <turbojpeg.h>
// int goTjCompress2(tjhandle handle, const unsigned char *srcBuf,
// int width, int pitch, int height, int pixelFormat,
// unsigned char *jpegBuf, unsigned long *jpegSize,
// int jpegSubsamp, int jpegQual, int flags) {
// return tjCompress2(handle, srcBuf, width, pitch, height,
// pixelFormat, &jpegBuf, jpegSize,
// jpegSubsamp, jpegQual, flags | TJFLAG_NOREALLOC);
//}
import "C"
type TurboJpegOperation C.int
func EncodeJpeg(buf *bytes.Buffer, img image.Image, quality int) ([]byte, error) {
var pix []uint8
var format, stride, jpegSubsamp, cWidth, cHeight, flags, jpegQual, res C.int
cWidth = C.int(img.Bounds().Dx())
cHeight = C.int(img.Bounds().Dy())
switch v := img.(type) {
case *image.Gray:
pix = v.Pix
stride = C.int(v.Stride)
format = C.TJPF_GRAY
jpegSubsamp = C.TJSAMP_GRAY
case *RGBImage:
pix = v.Pix
stride = C.int(v.Stride)
format = C.TJPF_RGB
jpegSubsamp = C.TJSAMP_420
case *image.RGBA:
pix = v.Pix
stride = C.int(v.Stride)
format = C.TJPF_RGBX
jpegSubsamp = C.TJSAMP_420
default:
return nil, errors.New("unsupported image type")
}
tjHandle := C.tjInitCompress()
if tjHandle == nil {
return nil, fmt.Errorf("could not init libjpeg-turbo: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
defer C.tjDestroy(tjHandle)
jpegQual = C.int(quality)
flags = C.TJFLAG_NOREALLOC
imageBytes := C.tjBufSize(cWidth, cHeight, jpegSubsamp)
// Prepare a buffer
buf.Reset()
buf.Grow(int(imageBytes))
buf.WriteByte(0)
res = C.goTjCompress2(
tjHandle,
(*C.uchar)(unsafe.Pointer(&pix[0])),
cWidth, stride, cHeight,
format,
(*C.uchar)(unsafe.Pointer(&(buf.Bytes())[0])),
&imageBytes,
jpegSubsamp, jpegQual, flags,
)
if res != 0 {
if C.tjGetErrorCode(tjHandle) == C.TJERR_WARNING {
zap.L().Warn(
"jpeg compress warning",
zap.String("jpgerror", C.GoString(C.tjGetErrorStr2(tjHandle))),
)
} else {
return nil, fmt.Errorf("could not compress jpeg: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
}
return buf.Bytes()[:int(imageBytes)], nil
}
func getTransformOperation(orient Orientation) []TurboJpegOperation {
switch orient {
case TopLeft:
return []TurboJpegOperation{C.TJXOP_NONE}
case TopRight:
return []TurboJpegOperation{C.TJXOP_HFLIP}
case BottomRight:
return []TurboJpegOperation{C.TJXOP_ROT180}
case BottomLeft:
return []TurboJpegOperation{C.TJXOP_VFLIP}
case LeftTop:
return []TurboJpegOperation{C.TJXOP_TRANSPOSE}
case RightTop:
return []TurboJpegOperation{C.TJXOP_ROT90}
case RightBottom:
return []TurboJpegOperation{C.TJXOP_TRANSVERSE}
case LeftBottom:
return []TurboJpegOperation{C.TJXOP_ROT270}
default:
fmt.Printf("Unexpected orientation %v", orient)
return []TurboJpegOperation{C.TJXOP_NONE}
}
}
// ReOrientJpeg will transform a JPEG into a top left (normal) orientation
// It returns a buffer with JPEG encoding
func ReOrientJpeg(file []byte, orient Orientation) ([]byte, error) {
if len(file) == 0 {
return nil, ErrEmptyInput
}
// Init a transform
tjHandle := C.tjInitTransform()
if tjHandle == nil {
return nil, fmt.Errorf("could not init libjpeg-turbo: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
defer C.tjDestroy(tjHandle)
operations := getTransformOperation(orient)
var destBuf *C.uchar
var destSize C.ulong
for _, operation := range operations {
transform := C.tjtransform{
op: C.int(operation),
options: C.TJXOPT_GRAY,
}
res := C.tjTransform(tjHandle,
(*C.uchar)(unsafe.Pointer(&file[0])), C.ulong(len(file)),
1,
&destBuf,
&destSize,
&transform,
0, // nolint:gocritic,staticcheck
)
if res != 0 {
if C.tjGetErrorCode(tjHandle) == C.TJERR_WARNING {
zap.L().Warn(
"jpeg transform warning",
zap.String("jpgerror", C.GoString(C.tjGetErrorStr())),
)
} else {
return nil, fmt.Errorf("could not transform jpeg: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
}
}
result := C.GoBytes(unsafe.Pointer(destBuf), C.int(destSize))
C.tjFree(destBuf)
return result, nil
}
func ConfigJpeg(data []byte) (image.Config, string, error) {
if len(data) == 0 {
return image.Config{}, string(Jpeg), ErrEmptyInput
}
// Init Turbo-JPEG Decompression
tjHandle := C.tjInitDecompress()
if tjHandle == nil {
return image.Config{}, string(Jpeg), fmt.Errorf("could not init libjpeg-turbo: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
defer C.tjDestroy(tjHandle)
var cWidth, cHeight, jpegsubsamp, jpegcolorspace C.int
res := C.tjDecompressHeader3(
tjHandle,
(*C.uchar)(unsafe.Pointer(&data[0])), C.ulong(len(data)),
&cWidth,
&cHeight,
&jpegsubsamp,
&jpegcolorspace,
)
if res != 0 {
return image.Config{}, string(Jpeg), fmt.Errorf("could not read JPEG header: %v", C.GoString(C.tjGetErrorStr2(tjHandle)))
}
var model color.Model
switch jpegcolorspace {
case C.TJCS_RGB:
model = RGBModel
case C.TJCS_YCbCr:
model = color.YCbCrModel
case C.TJCS_GRAY:
model = color.GrayModel
case C.TJCS_CMYK, C.TJCS_YCCK:
model = color.CMYKModel
default:
return image.Config{}, string(Jpeg), fmt.Errorf("unknown jpeg color space: %d", jpegcolorspace)
}
return image.Config{
ColorModel: model,
Width: int(cWidth),
Height: int(cHeight),
}, string(Jpeg), nil
}
// TransformJpeg will scale and colormap an input JPEG file to an image.Gray or RGBImage
// This will use libjpeg-turbo to do it as efficiently as possible, utilizing DCT factors for fast scaling
func TransformJpeg(data []byte, grayscale bool, scale ScaleFunc) (out image.Image, width, height int, scaleFactor float64, err error) {
if len(data) == 0 {
return nil, 0, 0, -1, ErrEmptyInput
}
// Init Turbo-JPEG Decompression
tjHandle := C.tjInitDecompress()
if tjHandle == nil {
return nil, 0, 0, -1, fmt.Errorf("could not init libjpeg-turbo: %v", C.GoString(C.tjGetErrorStr()))
}
defer C.tjDestroy(tjHandle)
// Detect orientation
orientation := GetOrientation(bytes.NewReader(data))
if orientation != TopLeft {
data, err = ReOrientJpeg(data, orientation)
if err != nil {
return nil, 0, 0, -1, err
}
}
// Read the size of the jpeg
conf, _, err := ConfigJpeg(data)
if err != nil {
return nil, 0, 0, -1, err
}
width = conf.Width
height = conf.Height
// Calculate our preferred scaling factor
_, _, prefScaleFactor := scale(width, height)
// Find the closest match for a DCT scaling
var cNumScaleFactor C.int
cScaleFactors := C.tjGetScalingFactors(&cNumScaleFactor)
if cScaleFactors == nil {
return nil, 0, 0, -1, errors.New("could not get libjpeg-turbo scale factors")
}
scaleFactors := uintptr(unsafe.Pointer(cScaleFactors))
// Find the closest JPEG DCT scale factor
selectedScaleFactorDiff := math.MaxFloat64
var selectedScaleFactor uintptr
var jpegScaleFactor float64
for i := 0; i < int(cNumScaleFactor); i++ {
offset := uintptr(C.sizeof_tjscalingfactor * i)
sf := (*C.tjscalingfactor)(unsafe.Pointer(scaleFactors + offset))
jpegScaleFactor = float64(sf.num) / float64(sf.denom)
diff := math.Abs(prefScaleFactor - jpegScaleFactor)
if diff < selectedScaleFactorDiff {
selectedScaleFactorDiff = diff
selectedScaleFactor = offset
}
}
// Calculate the final image size
sf := (*C.tjscalingfactor)(unsafe.Pointer(scaleFactors + selectedScaleFactor))
scaledW := int(math.RoundToEven(float64(C.int(width)*sf.num+sf.denom-1) / float64(sf.denom)))
scaledH := int(math.RoundToEven(float64(C.int(height)*sf.num+sf.denom-1) / float64(sf.denom)))
scaleFactor = float64(sf.num) / float64(sf.denom)
// Calculate the image stride and pitch
var pixelFormat C.int
var pitch int
if grayscale {
pixelFormat = C.TJPF_GRAY
pitch = scaledW * 1 // C.tjPixelSize[C.TJPF_GRAY]
} else {
pixelFormat = C.TJPF_RGB
pitch = scaledW * 3 // C.tjPixelSize[C.TJPF_RGB]
}
pixsize := scaledH * pitch
buf := make([]uint8, pixsize)
res := C.tjDecompress2(
tjHandle,
(*C.uchar)(unsafe.Pointer(&data[0])), C.ulong(len(data)),
(*C.uchar)(unsafe.Pointer(&buf[0])),
C.int(scaledW),
C.int(pitch),
C.int(scaledH),
pixelFormat,
0,
)
if res != 0 {
if C.tjGetErrorCode(tjHandle) == C.TJERR_WARNING {
zap.L().Warn(
"jpeg decompress warning",
zap.String("jpgerror", C.GoString(C.tjGetErrorStr())),
)
} else {
return nil, 0, 0, -1, fmt.Errorf("could not decompress jpeg: %v", C.GoString(C.tjGetErrorStr()))
}
}
var img image.Image
if grayscale {
img = &image.Gray{
Pix: buf,
Stride: pitch,
Rect: image.Rect(0, 0, scaledW, scaledH),
}
} else {
img = &RGBImage{
Pix: buf,
Stride: pitch,
Rect: image.Rect(0, 0, scaledW, scaledH),
}
}
return img, width, height, scaleFactor, nil
}