Skip to content

Commit

Permalink
feautre/exr added AddAlpha method
Browse files Browse the repository at this point in the history
  • Loading branch information
prOOrc committed Apr 23, 2024
1 parent 06cdc0e commit 653176c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ func (i *Image) Gamma(exponent float64) ([]byte, error) {
return i.Process(options)
}

// AddGamma returns the alpha added image buffer.
func (i *Image) AddAlpha() ([]byte, error) {
options := Options{AddAlpha: true}
return i.Process(options)
}

// Process processes the image based on the given transformation options,
// talking with libvips bindings accordingly and returning the resultant
// image buffer.
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ type Options struct {
OutputICC string
InputICC string
Palette bool
AddAlpha bool
// Speed defines the AVIF encoders CPU effort. Valid values are:
// 0-8 for AVIF encoding.
// 0-9 for PNG encoding.
Expand Down
17 changes: 17 additions & 0 deletions resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ func resizer(buf []byte, o Options) ([]byte, error) {
return nil, err
}

// Add Alpha, if necessary
image, err = addAlpha(image, o)
if err != nil {
return nil, err
}

return saveImage(image, o)
}

Expand Down Expand Up @@ -437,6 +443,17 @@ func applyGamma(image *C.VipsImage, o Options) (*C.VipsImage, error) {
return image, nil
}

func addAlpha(image *C.VipsImage, o Options) (*C.VipsImage, error) {
var err error
if o.AddAlpha {
image, err = vipsAddAlpha(image, 255.0)
if err != nil {
return nil, err
}
}
return image, nil
}

func zoomImage(image *C.VipsImage, zoom int) (*C.VipsImage, error) {
if zoom == 0 {
return image, nil
Expand Down
11 changes: 11 additions & 0 deletions vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,17 @@ func max(x int) int {
return int(math.Max(float64(x), 0))
}

func vipsAddAlpha(image *C.VipsImage, alpha float64) (*C.VipsImage, error) {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(image))

err := C.vips_add_band(image, &out, C.double(alpha))
if err != 0 {
return nil, catchVipsError()
}
return out, nil
}

func vipsDrawWatermark(image *C.VipsImage, o WatermarkImage) (*C.VipsImage, error) {
var out *C.VipsImage

Expand Down

0 comments on commit 653176c

Please sign in to comment.