| 1 |
package libwebp |
| 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 |
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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
} |