Skip to content

Commit

Permalink
BandSplit() split an n-band image into n separate images (#326)
Browse files Browse the repository at this point in the history
* BandSplit() split an n-band image into n separate images

* move tests

Co-authored-by: Roffe <roffe@roffe.nu>
  • Loading branch information
roffe and Roffe authored Nov 6, 2022
1 parent e407ec7 commit 49e1b1f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
13 changes: 13 additions & 0 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,19 @@ func (r *ImageRef) BandJoin(images ...*ImageRef) error {
return nil
}

// BandSplit split an n-band image into n separate images..
func (r *ImageRef) BandSplit() ([]*ImageRef, error) {
var out []*ImageRef
for i := 0; i < r.Bands(); i++ {
img, err := vipsExtractBand(r.image, i, 1)
if err != nil {
return out, err
}
out = append(out, &ImageRef{image: img})
}
return out, nil
}

// BandJoinConst appends a set of constant bands to an image.
func (r *ImageRef) BandJoinConst(constants []float64) error {
out, err := vipsBandJoinConst(r.image, constants)
Expand Down
7 changes: 0 additions & 7 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,6 @@ 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
33 changes: 33 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,39 @@ func TestBandJoin(t *testing.T) {
require.NoError(t, err)
}

func TestExtractBandToImage(t *testing.T) {
Startup(nil)
image1, err := NewImageFromFile(resources + "png-24bit.png")
require.NoError(t, err)

v, err := image1.ExtractBandToImage(0, 2)
require.NoError(t, err)
require.Equal(t, v.Bands(), 2)

_, err = v.ExtractBandToImage(0, 3)
require.Error(t, err)
}

func TestBandSplit(t *testing.T) {
Startup(nil)

image1, err := NewImageFromFile(resources + "png-24bit.png")
require.NoError(t, err)

bands, err := image1.BandSplit()
require.NoError(t, err)

require.Len(t, bands, 3)

image2, err := NewImageFromFile(resources + "with_alpha.png")
require.NoError(t, err)

bands2, err := image2.BandSplit()
require.NoError(t, err)

require.Len(t, bands2, 4)
}

func TestIsColorSpaceSupport(t *testing.T) {
Startup(nil)

Expand Down

0 comments on commit 49e1b1f

Please sign in to comment.