Skip to content

Commit

Permalink
add ExtractBandToImage (davidbyttow#324)
Browse files Browse the repository at this point in the history
* Update image.go

Add function to extract a band into a new image instead of replacing the current image ref

* Update image.go

* add test

Co-authored-by: Roffe <roffe@roffe.nu>
  • Loading branch information
2 people authored and Elad-Laufer committed Nov 22, 2022
1 parent 0f0526f commit 6fcb30f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,15 @@ func (r *ImageRef) ExtractBand(band int, num int) error {
return nil
}

// ExtractBandToImage extracts one or more bands out of the image to a new image
func (r *ImageRef) ExtractBandToImage(band int, num int) (*ImageRef, error) {
out, err := vipsExtractBand(r.image, band, num)
if err != nil {
return nil, err
}
return &ImageRef{image: out}, nil
}

// BandJoin joins a set of images together, bandwise.
func (r *ImageRef) BandJoin(images ...*ImageRef) error {
vipsImages := []*C.VipsImage{r.image}
Expand Down
7 changes: 7 additions & 0 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,13 @@ func TestImage_ExtractBand(t *testing.T) {
}, nil, nil)
}

func TestImage_ExtractBandToImage(t *testing.T) {
goldenTest(t, resources+"with_alpha.png", func(img *ImageRef) error {
_, err := img.ExtractBandToImage(2, 1)
return err
}, nil, nil)
}

func TestImage_Flatten(t *testing.T) {
goldenTest(t, resources+"with_alpha.png", func(img *ImageRef) error {
return img.Flatten(&Color{R: 32, G: 64, B: 128})
Expand Down

0 comments on commit 6fcb30f

Please sign in to comment.