-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
161 lines (135 loc) · 3.59 KB
/
encode.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
package golibjpegturbo
/*
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
typedef unsigned char *PUCHAR;
void error_panic(j_common_ptr cinfo);
typedef struct {
unsigned char *buf;
unsigned long buf_size;
} mem_helper;
mem_helper *alloc_mem_helper();
*/
import "C"
import (
"fmt"
"image"
"io"
"unsafe"
)
// DefaultQuality is the default quality encoding parameter.
const DefaultQuality = 75
// Options are the encoding parameters.
// Quality ranges from 1 to 100 inclusive, higher is better.
type Options struct {
Quality int
}
// Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given
// options. Default parameters are used if a nil *Options is passed.
func Encode(w io.Writer, m image.Image, o *Options) (err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("JPEG error: %v", r)
}
}
}()
b := m.Bounds()
dx := b.Dx()
dy := b.Dy()
if dx <= 0 || dy <= 0 {
return fmt.Errorf("image with invalid size, dx: %d, dy: %d (both must be > 0)", dx, dy)
}
quality := 75
if o != nil {
quality = o.Quality
}
cinfoSize := C.size_t(unsafe.Sizeof(C.struct_jpeg_compress_struct{}))
cinfo := (*C.struct_jpeg_compress_struct)(C.malloc(cinfoSize))
defer C.free(unsafe.Pointer(cinfo))
cinfoErrSize := C.size_t(unsafe.Sizeof(C.struct_jpeg_error_mgr{}))
cinfo.err = (*C.struct_jpeg_error_mgr)(C.malloc(cinfoErrSize))
defer C.free(unsafe.Pointer(cinfo.err))
C.jpeg_std_error(cinfo.err)
cinfo.err.error_exit = (*[0]byte)(C.error_panic)
memHelper := C.alloc_mem_helper()
C.jpeg_CreateCompress(cinfo, C.JPEG_LIB_VERSION, cinfoSize)
C.jpeg_mem_dest(cinfo, &memHelper.buf, &memHelper.buf_size)
nBytes := dx * 3 // for a line, 3 bytes per pixel
cinfo.image_width = C.JDIMENSION(dx)
cinfo.image_height = C.JDIMENSION(dy)
gray, isGray := m.(*image.Gray)
rgba, isRgba := m.(*image.RGBA)
cinfo.input_components = 3
cinfo.in_color_space = C.JCS_RGB
if isGray {
nBytes = dx
cinfo.input_components = 1
cinfo.in_color_space = C.JCS_GRAYSCALE
}
// Note: for more speed could try to go directly to JCS_EXT_RGBA but not
// sure if libjpeg matches Go and treats JCS_EXT_RGBA as alpha-premultipled
if isRgba {
nBytes = dx * 3
cinfo.input_components = 3
cinfo.in_color_space = C.JCS_RGB
}
C.jpeg_set_defaults(cinfo)
C.jpeg_set_quality(cinfo, C.int(quality), C.TRUE)
C.jpeg_start_compress(cinfo, C.TRUE)
bufBytes := C.malloc(C.size_t(nBytes))
rowPtr := C.JSAMPROW(bufBytes)
buf := sliceFromCBytes(bufBytes, nBytes)
if isGray {
for y := 0; y < dy; y++ {
off := y * gray.Stride
copy(buf[:], gray.Pix[off:off+nBytes])
C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
}
} else if isRgba {
for y := 0; y < dy; y++ {
off := y * rgba.Stride
p := rgba.Pix[off:]
dstOff := 0
srcOff := 0
for x := 0; x < dx; x++ {
buf[dstOff] = p[srcOff]
dstOff++
srcOff++
buf[dstOff] = p[srcOff]
dstOff++
srcOff++
buf[dstOff] = p[srcOff]
dstOff++
srcOff += 2
}
C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
}
} else {
for y := 0; y < dy; y++ {
off := 0
for x := 0; x < dx; x++ {
r, g, b, _ := m.At(x, y).RGBA()
buf[off] = byte(r >> 8)
off++
buf[off] = byte(g >> 8)
off++
buf[off] = byte(b >> 8)
off++
}
C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
}
}
C.jpeg_finish_compress(cinfo)
C.jpeg_destroy_compress(cinfo)
outBs := C.GoBytes(unsafe.Pointer(memHelper.buf), C.int(memHelper.buf_size))
w.Write(outBs)
C.free(unsafe.Pointer(memHelper.buf))
C.free(bufBytes)
C.free(unsafe.Pointer(memHelper))
return nil
}