-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 49ce674
Showing
21 changed files
with
1,022 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea | ||
/vendor | ||
.DS_Store | ||
/example/output* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Google LLC (https://opensource.google.com/) | ||
Amangeldy Kadyl <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 Amangeldy Kadyl | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# go-webp | ||
Golang Webp library for encoding and decoding, with C binding for Google [libwebp](https://developers.google.com/speed/webp/docs/api) | ||
|
||
## Install libwebp | ||
#### MacOS: | ||
```bash | ||
brew install webp | ||
``` | ||
#### Linux: | ||
```bash | ||
sudo apt-get update | ||
sudo apt-get install libwebp-dev | ||
``` | ||
|
||
## Install go-webp | ||
`go get -u github.com/kolesa-team/go-webp` | ||
|
||
## Example | ||
#### Before run | ||
```bash | ||
export CGO_CFLAGS="-I /usr/local/opt/webp/include" | ||
export CGO_LDFLAGS="-L /usr/local/opt/webp/lib -lwebp" | ||
export LD_LIBRARY_PATH=/usr/local/opt/webp/lib | ||
``` | ||
|
||
#### Decode: | ||
```go | ||
package main | ||
|
||
import ( | ||
"image/jpeg" | ||
"log" | ||
"os" | ||
|
||
"github.com/kolesa-team/go-webp/decoder" | ||
"github.com/kolesa-team/go-webp/webp" | ||
) | ||
|
||
func main() { | ||
file, err := os.Open("test_data/images/m4_q75.webp") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
output, err := os.Create("example/output_decode.jpg") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer output.Close() | ||
|
||
img, err := webp.Decode(file, &decoder.Options{}) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if err = jpeg.Encode(output, img, &jpeg.Options{Quality:75}); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
``` | ||
|
||
```bash | ||
go run example/decode/main.go | ||
``` | ||
|
||
#### Encode | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/kolesa-team/go-webp/encoder" | ||
"github.com/kolesa-team/go-webp/webp" | ||
"image/jpeg" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
file, err := os.Open("test_data/images/source.jpg") | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
img, err := jpeg.Decode(file) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
output, err := os.Create("example/output_decode.webp") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer output.Close() | ||
|
||
options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
if err := webp.Encode(output, img, options); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
``` | ||
```bash | ||
go run example/encode/main.go | ||
``` | ||
|
||
## TODO | ||
- return aux stats | ||
- container api | ||
- incremental decoding | ||
|
||
## License | ||
BSD licensed. See the LICENSE file for details. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package decoder | ||
|
||
/* | ||
#include <stdlib.h> | ||
#include <webp/decode.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"image" | ||
"io" | ||
"io/ioutil" | ||
"unsafe" | ||
|
||
"github.com/kolesa-team/go-webp/utils" | ||
) | ||
|
||
type Decoder struct { | ||
data []byte | ||
options *Options | ||
config *C.WebPDecoderConfig | ||
dPtr *C.uint8_t | ||
sPtr C.size_t | ||
} | ||
|
||
func NewDecoder(r io.Reader, options *Options) (d *Decoder, err error) { | ||
var data []byte | ||
|
||
if data, err = ioutil.ReadAll(r); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(data) == 0 { | ||
return nil, errors.New("data is empty") | ||
} | ||
|
||
d = &Decoder{data: data, options: options} | ||
|
||
if d.config, err = d.options.GetConfig(); err != nil { | ||
return nil, err | ||
} | ||
|
||
d.dPtr = (*C.uint8_t)(&d.data[0]) | ||
d.sPtr = (C.size_t)(len(d.data)) | ||
|
||
// получаем WebPBitstreamFeatures | ||
if status := d.parseFeatures(d.dPtr, d.sPtr); status != utils.Vp8StatusOk { | ||
return nil, errors.New(fmt.Sprintf("cannot fetch features: %s", status.String())) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (d *Decoder) Decode() (image.Image, error) { | ||
// вписываем размеры итоговой картинки | ||
d.config.output.width, d.config.output.height = d.getOutputDimensions() | ||
// указываем что декодируем в RGBA | ||
d.config.output.colorspace = C.MODE_RGBA | ||
d.config.output.is_external_memory = 1 | ||
|
||
img := image.NewNRGBA(image.Rectangle{Max: image.Point{ | ||
X: int(d.config.output.width), | ||
Y: int(d.config.output.height), | ||
}}) | ||
|
||
buff := (*C.WebPRGBABuffer)(unsafe.Pointer(&d.config.output.u[0])) | ||
buff.stride = C.int(img.Stride) | ||
buff.rgba = (*C.uint8_t)(&img.Pix[0]) | ||
buff.size = (C.size_t)(len(img.Pix)) | ||
|
||
if status := utils.VP8StatusCode(C.WebPDecode(d.dPtr, d.sPtr, d.config)); status != utils.Vp8StatusOk { | ||
return nil, errors.New(fmt.Sprintf("cannot decode picture: %s", status.String())) | ||
} | ||
|
||
return img, nil | ||
} | ||
|
||
func (d *Decoder) GetFeatures() utils.BitstreamFeatures { | ||
return utils.BitstreamFeatures{ | ||
Width: int(d.config.input.width), | ||
Height: int(d.config.input.height), | ||
HasAlpha: int(d.config.input.has_alpha) == 1, | ||
HasAnimation: int(d.config.input.has_animation) == 1, | ||
Format: utils.FormatType(d.config.input.format), | ||
} | ||
} | ||
|
||
func (d *Decoder) parseFeatures(dataPtr *C.uint8_t, sizePtr C.size_t) utils.VP8StatusCode { | ||
return utils.VP8StatusCode(C.WebPGetFeatures(dataPtr, sizePtr, &d.config.input)) | ||
} | ||
|
||
func (d *Decoder) getOutputDimensions() (width, height C.int) { | ||
width = d.config.input.width | ||
height = d.config.input.height | ||
|
||
if d.config.options.use_scaling > 0 { | ||
width = d.config.options.scaled_width | ||
height = d.config.options.scaled_height | ||
} else if d.config.options.use_cropping > 0 { | ||
width = d.config.options.crop_width | ||
height = d.config.options.crop_height | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package decoder | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/kolesa-team/go-webp/utils" | ||
) | ||
|
||
func TestNewDecoder(t *testing.T) { | ||
t.Run("create success", func(t *testing.T) { | ||
file, err := os.Open("../test_data/images/m4_q75.webp") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := NewDecoder(file, &Options{}); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
t.Run("empty file", func(t *testing.T) { | ||
file, err := os.Open("../test_data/images/invalid.webp") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := NewDecoder(file, &Options{}); err == nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} | ||
|
||
func TestDecoder_GetFeatures(t *testing.T) { | ||
file, err := os.Open("../test_data/images/m4_q75.webp") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
dec, err := NewDecoder(file, &Options{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
features := dec.GetFeatures() | ||
|
||
if features.Width != 675 || features.Height != 900 { | ||
t.Fatal("incorrect dimensions") | ||
} | ||
|
||
if features.Format != utils.FormatLossy { | ||
t.Fatal("file format is invalid") | ||
} | ||
|
||
if features.HasAlpha { | ||
t.Fatal("file has_alpha is invalid") | ||
} | ||
|
||
if features.HasAlpha { | ||
t.Fatal("file has_animation is invalid") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package decoder | ||
|
||
/* | ||
#include <webp/decode.h> | ||
*/ | ||
import "C" | ||
import ( | ||
"errors" | ||
"image" | ||
) | ||
|
||
type Options struct { | ||
BypassFiltering bool | ||
NoFancyUpsampling bool | ||
Crop image.Rectangle | ||
Scale image.Rectangle | ||
UseThreads bool | ||
Flip bool | ||
DitheringStrength int | ||
AlphaDitheringStrength int | ||
} | ||
|
||
func (o *Options) GetConfig() (*C.WebPDecoderConfig, error) { | ||
config := C.WebPDecoderConfig{} | ||
|
||
if C.WebPInitDecoderConfig(&config) == 0 { | ||
return nil, errors.New("cannot init decoder config") | ||
} | ||
|
||
if o.BypassFiltering { | ||
config.options.bypass_filtering = 1 | ||
} | ||
|
||
if o.NoFancyUpsampling { | ||
config.options.no_fancy_upsampling = 1 | ||
} | ||
|
||
// проверяем надо ли кропнуть | ||
if o.Crop.Max.X > 0 && o.Crop.Max.Y > 0 { | ||
config.options.use_cropping = 1 | ||
config.options.crop_left = C.int(o.Crop.Min.X) | ||
config.options.crop_top = C.int(o.Crop.Min.Y) | ||
config.options.crop_width = C.int(o.Crop.Max.X) | ||
config.options.crop_height = C.int(o.Crop.Max.Y) | ||
} | ||
|
||
// проверяем надо ли заскейлить | ||
if o.Scale.Max.X > 0 && o.Scale.Max.Y > 0 { | ||
config.options.use_scaling = 1 | ||
config.options.scaled_width = C.int(o.Scale.Max.X) | ||
config.options.scaled_height = C.int(o.Scale.Max.Y) | ||
} | ||
|
||
if o.UseThreads { | ||
config.options.use_threads = 1 | ||
} | ||
|
||
config.options.dithering_strength = C.int(o.DitheringStrength) | ||
|
||
if o.Flip { | ||
config.options.flip = 1 | ||
} | ||
|
||
config.options.alpha_dithering_strength = C.int(o.AlphaDitheringStrength) | ||
|
||
return &config, nil | ||
} |
Oops, something went wrong.