Skip to content

Commit

Permalink
Merge pull request #9 from davidbyttow/d/transform
Browse files Browse the repository at this point in the history
Implements new transform API
  • Loading branch information
davidbyttow authored Oct 9, 2017
2 parents 30a97dd + 4817028 commit bae4a88
Show file tree
Hide file tree
Showing 22 changed files with 742 additions and 452 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ script:
- diff -u <(echo -n) <(gofmt -s -d ./)
- diff -u <(echo -n) <(go vet ./)
# - diff -u <(echo -n) <(golint ./)
- go test -v -race -covermode=atomic -coverprofile=coverage.out
- go test -short -v -race -covermode=atomic -coverprofile=coverage.out

after_success:
- goveralls -coverprofile=coverage.out -service=travis-ci
45 changes: 35 additions & 10 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

const (
defaultQuality = 80
defaultQuality = 90
defaultCompression = 6
)

var stringBuffer4096 = fixedString(4096)
Expand Down Expand Up @@ -123,34 +124,38 @@ func vipsCallOperation(operation *C.VipsOperation, options []*Option) error {
return nil
}

func vipsPrepareForExport(image *C.VipsImage, params *ExportParams) (*C.VipsImage, error) {
var outImage *C.VipsImage

func vipsPrepareForExport(input *C.VipsImage, params *ExportParams) (*C.VipsImage, error) {
if params.StripProfile {
C.remove_icc_profile(image)
C.remove_icc_profile(input)
}

if params.Quality == 0 {
params.Quality = defaultQuality
}

if params.Compression == 0 {
params.Compression = defaultCompression
}

// Use a default interpretation and cast it to C type
if params.Interpretation == 0 {
params.Interpretation = InterpretationSRGB
params.Interpretation = Interpretation(input.Type)
}

interpretation := C.VipsInterpretation(params.Interpretation)

// Apply the proper colour space
if int(C.is_colorspace_supported(image)) == 1 {
err := C.to_colorspace(image, &outImage, interpretation)
if int(C.is_colorspace_supported(input)) == 1 && interpretation != input.Type {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
err := C.to_colorspace(input, &out, interpretation)
if int(err) != 0 {
return nil, handleVipsError()
}
image = outImage
input = out
}

return image, nil
return input, nil
}

func vipsLoadFromBuffer(buf []byte) (*C.VipsImage, ImageType, error) {
Expand Down Expand Up @@ -287,6 +292,26 @@ func vipsShrink(input *C.VipsImage, shrink int) (*C.VipsImage, error) {
return image, nil
}

func vipsFlattenBackground(input *C.VipsImage, color Color) (*C.VipsImage, error) {
var image *C.VipsImage
defer C.g_object_unref(C.gpointer(input))

bg := [3]C.double{
C.double(color.R),
C.double(color.G),
C.double(color.B),
}

if int(C.has_alpha_channel(input)) > 0 {
err := C.flatten_image_background(input, &image, bg[0], bg[1], bg[2])
if int(err) != 0 {
return nil, handleVipsError()
}
}

return image, nil
}

func handleVipsError() error {
defer C.vips_thread_shutdown()
defer C.vips_error_clear()
Expand Down
8 changes: 8 additions & 0 deletions bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ int flatten_image_background(VipsImage *in, VipsImage **out, double r, double g,
int transform_image(VipsImage *in, VipsImage **out, double a, double b, double c, double d, VipsInterpolate *interpolator);

void gobject_set_property(VipsObject* object, const char* name, const GValue* value);

static int has_alpha_channel(VipsImage *image) {
return (
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
) ? 1 : 0;
}
6 changes: 4 additions & 2 deletions examples/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func main() {
}

func embed(inputFile, outputFile string) error {
return vips.NewPipeline().
_, err := vips.NewTransform().
LoadFile(inputFile).
PadStrategy(vips.ExtendBlack).
Resize(1200, 1200).
OutputFile(outputFile)
OutputFile(outputFile).
Apply()
return err
}
6 changes: 4 additions & 2 deletions examples/invert/invert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func main() {
}

func invert(inputFile, outputFile string) error {
return vips.NewPipeline().
_, err := vips.NewTransform().
LoadFile(inputFile).
Invert().
OutputFile(outputFile)
OutputFile(outputFile).
Apply()
return err
}
6 changes: 4 additions & 2 deletions examples/resize/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func main() {
}

func resize(inputFile, outputFile string) error {
return vips.NewPipeline().
_, err := vips.NewTransform().
LoadFile(inputFile).
Reduce(0.2).
OutputFile(outputFile)
OutputFile(outputFile).
Apply()
return err
}
Binary file added fixtures/colors.TestResizeCrop.golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/shapes.TestBottomRightCrop.golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/shapes.TestCenterCrop.golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/shapes.TestResizeShapes.golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/shapes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/tomatoes.TestOffsetCrop.golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixtures/tomatoes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "C"

import (
"errors"
"io"
"io/ioutil"
"runtime"
"unsafe"
Expand All @@ -18,6 +19,15 @@ type ImageRef struct {
format ImageType
}

// LoadImage loads an ImageRef from the given reader
func LoadImage(r io.Reader) (*ImageRef, error) {
buf, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return NewImageFromBuffer(buf)
}

// NewImageFromFile loads an image from file and creates a new ImageRef
func NewImageFromFile(file string) (*ImageRef, error) {
startupIfNeeded()
Expand All @@ -31,6 +41,7 @@ func NewImageFromFile(file string) (*ImageRef, error) {

// NewImageFromBuffer loads an image buffer and creates a new Image
func NewImageFromBuffer(buf []byte) (*ImageRef, error) {
defer runtime.KeepAlive(buf)
startupIfNeeded()

image, format, err := vipsLoadFromBuffer(buf)
Expand Down
8 changes: 0 additions & 8 deletions image_test.go

This file was deleted.

17 changes: 9 additions & 8 deletions leak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var (
vips.ResizeStrategyEmbed,
}
sizes = []size{
size{100, 100},
size{500, 0},
size{0, 500},
size{1000, 1000},
{100, 100},
{500, 0},
{0, 500},
{1000, 1000},
}
formats = []vips.ImageType{
vips.ImageTypeJPEG,
Expand All @@ -35,7 +35,7 @@ type transform struct {
Resize vips.ResizeStrategy
Width int
Height int
Flip vips.Direction
Flip vips.FlipDirection
Format vips.ImageType
Zoom int
Blur float64
Expand All @@ -57,7 +57,7 @@ func TestCleanup(t *testing.T) {
Resize: resize,
Width: size.w,
Height: size.h,
Flip: vips.DirectionHorizontal,
Flip: vips.FlipBoth,
Kernel: vips.KernelLanczos3,
Format: format,
Blur: 4,
Expand All @@ -77,7 +77,7 @@ func TestCleanup(t *testing.T) {
go func(i int) {
defer wg.Done()

buf, err := vips.NewPipeline().
buf, err := vips.NewTransform().
LoadFile("fixtures/canyon.jpg").
ResizeStrategy(transform.Resize).
Resize(transform.Width, transform.Height).
Expand All @@ -88,7 +88,8 @@ func TestCleanup(t *testing.T) {
Interpolator(transform.Interp).
Zoom(transform.Zoom, transform.Zoom).
Quality(transform.Quality).
Output()
OutputBytes().
Apply()
require.NoError(t, err)

image, err := vips.NewImageFromBuffer(buf)
Expand Down
Loading

0 comments on commit bae4a88

Please sign in to comment.