Debian Codegraph @

internal/libwebp/a__encoder.go

raw symbols SCIP
1 package libwebp
2
3 /*
4 #include <stdlib.h>
5 #include <string.h> // for memset
6 #ifndef LIBWEBP_NO_SRC
7 #include <encode.h>
8 #else
9 #include <webp/encode.h>
10 #endif
11
12 static uint8_t* encodeNRGBA(WebPConfig* config, const uint8_t* rgba, int width, int height, int stride, size_t* output_size) {
13 WebPPicture pic;
14 WebPMemoryWriter wrt;
15 int ok;
16 if (!WebPPictureInit(&pic)) {
17 return NULL;
18 }
19 pic.use_argb = 1;
20 pic.width = width;
21 pic.height = height;
22 pic.writer = WebPMemoryWrite;
23 pic.custom_ptr = &wrt;
24 WebPMemoryWriterInit(&wrt);
25 ok = WebPPictureImportRGBA(&pic, rgba, stride) && WebPEncode(config, &pic);
26 WebPPictureFree(&pic);
27 if (!ok) {
28 WebPMemoryWriterClear(&wrt);
29 return NULL;
30 }
31 *output_size = wrt.size;
32 return wrt.mem;
33 }
34
35 static uint8_t* encodeGray(WebPConfig* config, uint8_t *y, int width, int height, int stride, size_t* output_size) {
36 WebPPicture pic;
37 WebPMemoryWriter wrt;
38
39 int ok;
40 if (!WebPPictureInit(&pic)) {
41 return NULL;
42 }
43
44 pic.use_argb = 0;
45 pic.width = width;
46 pic.height = height;
47 pic.y_stride = stride;
48 pic.writer = WebPMemoryWrite;
49 pic.custom_ptr = &wrt;
50 WebPMemoryWriterInit(&wrt);
51
52 const int uvWidth = (int)(((int64_t)width + 1) >> 1);
53 const int uvHeight = (int)(((int64_t)height + 1) >> 1);
54 const int uvStride = uvWidth;
55 const int uvSize = uvStride * uvHeight;
56 const int gray = 128;
57 uint8_t* chroma;
58
59 chroma = malloc(uvSize);
60 if (!chroma) {
61 return 0;
62 }
63 memset(chroma, gray, uvSize);
64
65 pic.y = y;
66 pic.u = chroma;
67 pic.v = chroma;
68 pic.uv_stride = uvStride;
69
70 ok = WebPEncode(config, &pic);
71
72 free(chroma);
73
74 WebPPictureFree(&pic);
75 if (!ok) {
76 WebPMemoryWriterClear(&wrt);
77 return NULL;
78 }
79 *output_size = wrt.size;
80 return wrt.mem;
81
82 }
83
84 */
85 import "C"
86
87 import (
88 "errors"
89 "image"
90 "image/draw"
91 "io"
92 "unsafe"
93
94 "github.com/bep/gowebp/libwebp/webpoptions"
95 )
96
97 type (
98 Encoder struct {
99 config *C.WebPConfig
100 img *image.NRGBA
101 }
102 )
103
104 // Encode encodes src into w considering the options in o.
105 //
106 // Any src that isn't one of *image.RGBA, *image.NRGBA, or *image.Gray
107 // will be converted to *image.NRGBA using draw.Draw first.
108 func Encode(w io.Writer, src image.Image, o webpoptions.EncodingOptions) error {
109 config, err := encodingOptionsToCConfig(o)
110 if err != nil {
111 return err
112 }
113
114 var (
115 bounds = src.Bounds()
116 output *C.uchar
117 size C.size_t
118 )
119
120 switch v := src.(type) {
121 case *image.RGBA:
122 output = C.encodeNRGBA(
123 config,
124 (*C.uint8_t)(&v.Pix[0]),
125 C.int(bounds.Max.X),
126 C.int(bounds.Max.Y),
127 C.int(v.Stride),
128 &size,
129 )
130 case *image.NRGBA:
131 output = C.encodeNRGBA(
132 config,
133 (*C.uint8_t)(&v.Pix[0]),
134 C.int(bounds.Max.X),
135 C.int(bounds.Max.Y),
136 C.int(v.Stride),
137 &size,
138 )
139 case *image.Gray:
140 gray := (*C.uint8_t)(&v.Pix[0])
141 output = C.encodeGray(
142 config,
143 gray,
144 C.int(bounds.Max.X),
145 C.int(bounds.Max.Y),
146 C.int(v.Stride),
147 &size,
148 )
149 default:
150 rgba := ConvertToNRGBA(src)
151 output = C.encodeNRGBA(
152 config,
153 (*C.uint8_t)(&rgba.Pix[0]),
154 C.int(bounds.Max.X),
155 C.int(bounds.Max.Y),
156 C.int(rgba.Stride),
157 &size,
158 )
159 }
160
161 if output == nil || size == 0 {
162 return errors.New("failed to encode")
163 }
164 defer C.free(unsafe.Pointer(output))
165
166 _, err = w.Write(((*[1 << 30]byte)(unsafe.Pointer(output)))[0:int(size):int(size)])
167
168 return err
169 }
170
171 func ConvertToNRGBA(src image.Image) *image.NRGBA {
172 dst := image.NewNRGBA(src.Bounds())
173 draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
174
175 return dst
176 }
177
178 func encodingOptionsToCConfig(o webpoptions.EncodingOptions) (*C.WebPConfig, error) {
179 cfg := &C.WebPConfig{}
180 quality := C.float(o.Quality)
181
182 if C.WebPConfigPreset(cfg, C.WebPPreset(o.EncodingPreset), quality) == 0 {
183 return nil, errors.New("failed to init encoder config")
184 }
185
186 if quality == 0 {
187 // Activate the lossless compression mode with the desired efficiency level
188 // between 0 (fastest, lowest compression) and 9 (slower, best compression).
189 // A good default level is '6', providing a fair tradeoff between compression
190 // speed and final compressed size.
191 if C.WebPConfigLosslessPreset(cfg, C.int(6)) == 0 {
192 return nil, errors.New("failed to init lossless preset")
193 }
194 }
195
196 cfg.use_sharp_yuv = boolToCInt(o.UseSharpYuv)
197
198 if C.WebPValidateConfig(cfg) == 0 {
199 return nil, errors.New("failed to validate config")
200 }
201
202 return cfg, nil
203 }
204
205 func boolToCInt(b bool) (result C.int) {
206 result = 0
207
208 if b {
209 result = 1
210 }
211
212 return
213 }