Skip to content

Multiple features #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 vips --without-fftw --without-libexif --without-libgsf \
brew install 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.
33 changes: 23 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 @@ -69,13 +73,15 @@ type Options struct {
func init() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

err := C.vips_initialize()
if err != 0 {
C.vips_shutdown()
panic("unable to start vips!")
}

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)
}

Expand All @@ -89,9 +95,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 @@ -101,17 +109,22 @@ 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)
}
defer C.vips_thread_shutdown()

// 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 @@ -108,15 +109,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")
}
}