Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/h2non/vips
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/h2non/vips:
  feat(DAddYE#12): add libwebp install in docs
  feat(DAddYE#13): support webp images
  fix: test
  fix(DAddYE#13): test
  feat(DAddYE#13): infer image type with go net/http primitive
  feat(DAddYE#12): initial support for webp
  • Loading branch information
xinuc committed Nov 30, 2016
2 parents 6954415 + 9862930 commit 979ee0e
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _libvips_ can take advantage of [liborc](http://code.entropywave.com/orc/) if pr

### Install libvips on Mac OS

brew install homebrew/science/vips --without-fftw --without-libexif --without-libgsf \
brew install homebrew/science/vips --with-webp --without-fftw --without-libexif --without-libgsf \
--without-little-cms2 --without-orc --without-pango --without-pygobject3 \
--without-gobject-introspection --without-python

Expand All @@ -36,12 +36,13 @@ _libvips_ can take advantage of [liborc](http://code.entropywave.com/orc/) if pr
Compiling from source is recommended:

sudo apt-get install automake build-essential git gobject-introspection \
libglib2.0-dev libjpeg-turbo8-dev libpng12-dev gtk-doc-tools
libglib2.0-dev webp libjpeg-turbo8-dev libpng12-dev gtk-doc-tools
git clone https://github.com/jcupitt/libvips.git
cd libvips
./bootstrap.sh
./configure --enable-debug=no --without-python --without-fftw --without-libexif \
--without-libgf --without-little-cms --without-orc --without-pango --prefix=/usr
./configure --enable-debug=no --with-webp --without-python --without-fftw \
--without-libexif --without-libgf --without-little-cms --without-orc \
--without-pango --prefix=/usr
make
sudo make install
sudo ldconfig
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added fixtures/test.webp
Binary file not shown.
31 changes: 21 additions & 10 deletions vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ package vips
import "C"

import (
"bytes"
"errors"
"fmt"
"math"
"net/http"
"os"
"runtime"
"unsafe"
)

const DEBUG = false

var (
MARKER_JPEG = []byte{0xff, 0xd8}
MARKER_PNG = []byte{0x89, 0x50}
const (
JPEG_MIME = "image/jpeg"
WEBP_MIME = "image/webp"
PNG_MIME = "image/png"
)

type ImageType int

const (
UNKNOWN ImageType = iota
JPEG
WEBP
PNG
)

const QUALITY = 80

type Interpolator int

const (
Expand Down Expand Up @@ -86,7 +90,7 @@ func Initialize() {
}

C.vips_concurrency_set(1)
C.vips_cache_set_max_mem(100 * 1048576) // 100Mb
C.vips_cache_set_max_mem(100 * 1024 * 1024)
C.vips_cache_set_max(500)

initialized = true
Expand All @@ -112,9 +116,11 @@ func Resize(buf []byte, o Options) ([]byte, error) {
// detect (if possible) the file type
typ := UNKNOWN
switch {
case bytes.Equal(buf[:2], MARKER_JPEG):
case http.DetectContentType(buf) == JPEG_MIME:
typ = JPEG
case bytes.Equal(buf[:2], MARKER_PNG):
case http.DetectContentType(buf) == WEBP_MIME:
typ = WEBP
case http.DetectContentType(buf) == PNG_MIME:
typ = PNG
default:
return nil, errors.New("unknown image format")
Expand All @@ -124,11 +130,16 @@ func Resize(buf []byte, o Options) ([]byte, error) {
var image, tmpImage *C.struct__VipsImage

// feed it
imageLength := C.size_t(len(buf))
imageBuf := unsafe.Pointer(&buf[0])

switch typ {
case JPEG:
C.vips_jpegload_buffer_seq(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &image)
C.vips_jpegload_buffer_seq(imageBuf, imageLength, &image)
case WEBP:
C.vips_webpload_buffer_seq(imageBuf, imageLength, &image)
case PNG:
C.vips_pngload_buffer_seq(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &image)
C.vips_pngload_buffer_seq(imageBuf, imageLength, &image)
}

// cleanup
Expand All @@ -139,7 +150,7 @@ func Resize(buf []byte, o Options) ([]byte, error) {

// defaults
if o.Quality == 0 {
o.Quality = 100
o.Quality = QUALITY
}

// get WxH
Expand Down
6 changes: 6 additions & 0 deletions vips.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ vips_jpegload_buffer_shrink(void *buf, size_t len, VipsImage **out, int shrink)
return vips_jpegload_buffer(buf, len, out, "shrink", shrink, NULL);
};

int
vips_webpload_buffer_seq(void *buf, size_t len, VipsImage **out)
{
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
};

int
vips_pngload_buffer_seq(void *buf, size_t len, VipsImage **out)
{
Expand Down
32 changes: 29 additions & 3 deletions vips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"image"
"image/jpeg"
"io/ioutil"
"net/http"
"os"
"testing"
)

func BenchmarkParallel(b *testing.B) {
options := Options{Width: 800, Height: 600, Crop: true}
f, err := os.Open("testdata/1.jpg")
f, err := os.Open("fixtures/1.jpg")
if err != nil {
b.Fatal(err)
}
Expand All @@ -34,7 +35,7 @@ func BenchmarkParallel(b *testing.B) {

func BenchmarkSerialized(b *testing.B) {
options := Options{Width: 800, Height: 600, Crop: true}
f, err := os.Open("testdata/1.jpg")
f, err := os.Open("fixtures/1.jpg")
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -110,15 +111,40 @@ func TestResize(t *testing.T) {

newWidth := uint(outImg.Bounds().Dx())
newHeight := uint(outImg.Bounds().Dy())

if newWidth != mt.expectedWidth ||
newHeight != mt.expectedHeight {
t.Fatalf("%d. Resize(imgData, %#v) => "+
"width: %v, height: %v, want width: %v, height: %v, "+
"originl size: %vx%v",
index, options,
newWidth, newHeight, mt.expectedWidth, mt.expectedHeight,
newWidth, newHeight,
mt.expectedWidth, mt.expectedHeight,
mt.origWidth, mt.origHeight,
)
}
}
}

func TestWebpResize(t *testing.T) {
options := Options{Width: 800, Height: 600, Crop: true}
img, err := os.Open("fixtures/test.webp")
if err != nil {
t.Fatal(err)
}
defer img.Close()

buf, err := ioutil.ReadAll(img)
if err != nil {
t.Fatal(err)
}

newImg, err := Resize(buf, options)
if err != nil {
t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
}

if http.DetectContentType(newImg) != JPEG_MIME {
t.Fatal("Image is not webp valid")
}
}

0 comments on commit 979ee0e

Please sign in to comment.