Simple and fast webp library for golang

Overview

logo

go-webp

Build Status GoDoc Go Report codecov

Golang Webp library for encoding and decoding, using C binding for Google libwebp

Requirements

libwebp

Benchmarks

% go test -bench "^BenchmarkDecode" ./webp                                                                                
goos: darwin
goarch: amd64
pkg: github.com/kolesa-team/go-webp/webp
BenchmarkDecodeLossy-12                       45          25965139 ns/op
BenchmarkDecodeXImageLossy-12                 13          90735879 ns/op
BenchmarkDecodeLossless-12                    64          18887482 ns/op
BenchmarkDecodeXImageLossless-12              27          42422596 ns/op
PASS
ok      github.com/kolesa-team/go-webp/webp     7.877s

Install libwebp

MacOS:

brew install webp

Linux:

sudo apt-get update
sudo apt-get install libwebp-dev

Install

go get -u github.com/kolesa-team/go-webp

Examples

Decode:

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)
	}
}
go run example/decode/main.go

Encode

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)
	}
}
go run example/encode/main.go

TODO

  • return aux stats
  • container api
  • incremental decoding

License

MIT licensed. See the LICENSE file for details.

Issues
  • Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64

    Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64

    Build error with cgo_enabled=1 GOOS=linux GOARCH=amd64. How to solve this issue?

    github.com/kolesa-team/go-webp/encoder
    187 | /go/pkg/mod/github.com/kolesa-team/[email protected]/encoder/options.go:112:5: could not determine kind of name for C.WebPConfigLosslessPreset
    188 | # github.com/kolesa-team/go-webp/decoder
    189 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/decoder.go:111:48: d.config.input.format undefined (type _Ctype_struct_WebPBitstreamFeatures has no field or method format)
    190 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:82:16: config.options.dithering_strength undefined (type _Ctype_struct_WebPDecoderOptions has no field or method dithering_strength)
    191 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:85:17: config.options.flip undefined (type _Ctype_struct_WebPDecoderOptions has no field or method flip)
    192 | /go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/options.go:88:16: config.options.alpha_dithering_strength undefined (type _Ctype_struct_WebPDecoderOptions has no field or method alpha_dithering_strength)
    
    opened by rohith-pc 4
  • cannot fetch features: VP8_STATUS_BITSTREAM_ERROR

    cannot fetch features: VP8_STATUS_BITSTREAM_ERROR

    I'm trying to convert png to webp and getting error "cannot fetch features: VP8_STATUS_BITSTREAM_ERROR".

    func (c *ConvertService) ConvertToWebp(file io.Reader, d entities.Dimensions) (image.Image, error) {
    	img, err := webp.Decode(file, &decoder.Options{})
    
    	if err != nil {
    		return nil, err
    	}
    
    	return transform.Resize(img, d.Width, d.Height, transform.Linear), nil
    }
    

    OS: macOS Catalina, 10.15.7 Lib: webp 1.2.1 Go: 1.17

    opened by frops 3
  • fatal error: 'webp/encode.h' file not found

    fatal error: 'webp/encode.h' file not found

    Hello,I'm using M1 MacBook Pro,MacOs Monterey.And I have installed webp package, but when I try to run this lib,I got this error:

    # github.com/kolesa-team/go-webp/encoder
    ../../../go/pkg/mod/github.com/kolesa-team/[email protected]/encoder/encoder.go:27:10: fatal error: 'webp/encode.h' file not found
    #include <webp/encode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    # github.com/kolesa-team/go-webp/decoder
    ../../../go/pkg/mod/github.com/kolesa-team/[email protected]/decoder/decoder.go:27:10: fatal error: 'webp/decode.h' file not found
    #include <webp/decode.h>
             ^~~~~~~~~~~~~~~
    1 error generated.
    

    I installed webp lib with brew, and I found header files in this path,but I don't know how to link it:

    /opt/homebrew/include/webp
    

    Some one says that new MacOS seemed to have stopped using /usr/local/include/. See this link I'm not proficient in cgo and C,I don't known how to change the path to let cgo link these header files.I try to google but got no answer.Can you help me to solve this problem?Thank you!

    opened by LainNetWork 1
  • Decoder.GetFeatures().HasAnimation() untested

    Decoder.GetFeatures().HasAnimation() untested

    Noticed this while looking to make use of this package. Patch suggested by inspection, but not run yet.

    *** decoder_test.go.orig 2022-01-21 13:39:38.000000000 -0500 --- decoder_test.go 2022-01-21 13:40:22.000000000 -0500


    *** 100,106 **** t.Fatal("file has_alpha is invalid") }

    ! if features.HasAlpha { t.Fatal("file has_animation is invalid") } } --- 100,106 ---- t.Fatal("file has_alpha is invalid") }

    ! if features.HasAnimation { t.Fatal("file has_animation is invalid") } }

    opened by jrb3dev 0
Releases(v1.0.1)
Owner
Kolesa Group
Kolesa Group
A small, fast, reliable pastemyst API wrapper written in Golang

A small, fast, reliable pastemyst API wrapper written in Golang. Official pastemyst API docs found here.

null 11 Nov 18, 2021
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key

Simple Weather API Simple weather api app created using golang and Open Weather

Siva Prakash 3 Feb 6, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Rohan Verma 87 Jun 10, 2022
Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)

WUZAPI WuzAPI is an implementation of @tulir/whatsmeow library as a simple RESTful API service with multiple device support and concurrent sessions. W

Nicolas 3 Jun 14, 2022
Empty an S3 bucket as fast as possible 💨

s3-destroyer Iteratively calls ListObjects, add objects keys to a buffer and calls DeleteObject in goroutines. Usage -access-key string s3 a

Stanislas 2 Dec 13, 2021
"there" also called "GoThere" aims to be a simple Go Library to reduce redundant code for REST APIs.

there "there" also called "GoThere" aims to be a simple Go Library to reduce redundant code for REST APIs. Despite the existence of the other librarie

Christoph Krassnigg 27 Jun 15, 2022
Simples3 : Simple no frills AWS S3 Library using REST with V4 Signing

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Rohan Verma 70 Nov 30, 2021
Simple-api - Create Simple `Hello World` APIs from Various Programming Languages

simple-api Create Simple `Hello World` APIs from Various Programming Languages.

I am Groot 1 Jan 18, 2022
TODO_GO: a simple todo API created in Golang with a minimum number of dependencies and configuration

TODO_GO TODO_GO is a simple todo API created in Golang with a minimum number of

Alejandro Acevedo Gonzalez 0 Jan 1, 2022
A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sway and i3

Untitled Spotifyd Controller A simple Go utility to display track information from, and send commands to, spotifyd from Tiling Window Managers like Sw

Matt 2 Mar 8, 2022
a Go (Golang) MusicBrainz WS2 client library - work in progress

gomusicbrainz a Go (Golang) MusicBrainz WS2 client library - a work in progress. Current state Currently GoMusicBrainz provides methods to perform sea

Michael Wendland 45 May 3, 2022
golang library for textbelt.com

textbelt.com API for GO This library sends text messages to mobile phones using http://textbelt.com Installing go get $ go get gopkg.in/dietsche/textb

Greg Dietsche 17 Oct 14, 2021
Our library to use the sendcloud API endpoints in golang.

gosendcloud With this library it should be possible to interact with the endpoints of the sendcloud API via golang functions. Since we can not impleme

J&J Ideenschmiede GmbH 0 Dec 3, 2021
A Golang Client Library for building Cosmos SDK chain clients

Cosmos Client Lib in Go This is the start of ideas around how to implement the cosmos client libraries in a seperate repo How to instantiate and use t

Jack Zampolin 113 Jun 18, 2022
The library parse the system netrc file for golang

gonetrc This is the library parse the system netrc file, support linux/macos/win

cnmade 0 Dec 21, 2021
Opensea-go - Golang's library for OpenSea APIs

opensea-go Golang's library for OpenSea APIs (https://docs.opensea.io/reference)

null 21 Jun 9, 2022
G-array is a GoLang library, that contains the generic function to do the array operations.

G-array Garray is a library written in Go (Golang). Which have a collection of functions to do the array operations. Installation To install G-array p

99x 2 May 24, 2022
Simple golang airtable API wrapper

Golang Airtable API A simple #golang package to access the Airtable API. Table of contents Golang Airtable API Table of contents Installation Basic us

mehanizm 40 Jun 21, 2022