Skip to content
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

feautre/exr added support for exr type #419

Open
wants to merge 2 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
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
Binary file added testdata/test.exr
Binary file not shown.
3 changes: 3 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
HEIF
// AVIF represents the AVIF image type.
AVIF
// EXR represents the EXR image type.
EXR
)

var (
Expand All @@ -51,6 +53,7 @@ var ImageTypes = map[ImageType]string{
MAGICK: "magick",
HEIF: "heif",
AVIF: "avif",
EXR: "exr",
}

// imageMutex is used to provide thread-safe synchronization
Expand Down
3 changes: 3 additions & 0 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestDeterminateImageType(t *testing.T) {
{"test2.heic", HEIF},
{"test3.heic", HEIF},
{"test.avif", AVIF},
{"test.exr", EXR},
}

for _, file := range files {
Expand Down Expand Up @@ -53,6 +54,7 @@ func TestDeterminateImageTypeName(t *testing.T) {
// {"test.jp2", "magick"},
{"test.heic", "heif"},
{"test.avif", "avif"},
{"test.exr", "exr"},
}

for _, file := range files {
Expand Down Expand Up @@ -106,6 +108,7 @@ func TestIsTypeNameSupported(t *testing.T) {
{"pdf", true},
{"heif", true},
{"avif", true},
{"exr", true},
}

for _, n := range types {
Expand Down
18 changes: 18 additions & 0 deletions vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func VipsIsTypeSupported(t ImageType) bool {
if t == AVIF {
return int(C.vips_type_find_bridge(C.HEIF)) != 0
}
if t == EXR {
return int(C.vips_type_find_bridge(C.MAGICK)) != 0
}
return false
}

Expand Down Expand Up @@ -767,6 +770,10 @@ func vipsImageType(buf []byte) ImageType {
buf[8] == 0x61 && buf[9] == 0x76 && buf[10] == 0x69 && buf[11] == 0x66 {
return AVIF
}
if IsTypeSupported(EXR) && buf[0] == 0x76 && buf[1] == 0x2f &&
buf[2] == 0x31 && buf[3] == 0x01 {
return EXR
}

return UNKNOWN
}
Expand Down Expand Up @@ -818,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