Merge pull request #374 from Mereng/brightness_contrast

Support brightness and contrast
master
Tom 4 years ago committed by GitHub
commit ffadb9e4b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -241,6 +241,8 @@ type Options struct {
Sharpen Sharpen
Threshold float64
Gamma float64
Brightness float64
Contrast float64
OutputICC string
InputICC string
Palette bool

@ -142,6 +142,18 @@ func resizer(buf []byte, o Options) ([]byte, error) {
return nil, err
}
// Apply brightness, if necessary
image, err = applyBrightness(image, o)
if err != nil {
return nil, err
}
// Apply contrast, if necessary
image, err = applyContrast(image, o)
if err != nil {
return nil, err
}
return saveImage(image, o)
}
@ -621,3 +633,25 @@ func getAngle(angle Angle) Angle {
}
return Angle(math.Min(float64(angle), 270))
}
func applyBrightness(image *C.VipsImage, o Options) (*C.VipsImage, error) {
var err error
if o.Brightness != 0 {
image, err = vipsBrightness(image, o.Brightness)
if err != nil {
return nil, err
}
}
return image, nil
}
func applyContrast(image *C.VipsImage, o Options) (*C.VipsImage, error) {
var err error
if o.Contrast > 0 {
image, err = vipsContrast(image, o.Contrast)
if err != nil {
return nil, err
}
}
return image, nil
}

@ -847,3 +847,25 @@ func vipsGamma(image *C.VipsImage, Gamma float64) (*C.VipsImage, error) {
}
return out, nil
}
func vipsBrightness(image *C.VipsImage, brightness float64) (*C.VipsImage, error) {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(image))
err := C.vips_brightness_bridge(image, &out, C.double(brightness))
if err != 0 {
return nil, catchVipsError()
}
return out, nil
}
func vipsContrast(image *C.VipsImage, contrast float64) (*C.VipsImage, error) {
var out *C.VipsImage
defer C.g_object_unref(C.gpointer(image))
err := C.vips_contrast_bridge(image, &out, C.double(contrast))
if err != 0 {
return nil, catchVipsError()
}
return out, nil
}

@ -677,3 +677,13 @@ int vips_gamma_bridge(VipsImage *in, VipsImage **out, double exponent)
{
return vips_gamma(in, out, "exponent", 1.0 / exponent, NULL);
}
int vips_brightness_bridge(VipsImage *in, VipsImage **out, double k)
{
return vips_linear1(in, out, 1.0 , k, NULL);
}
int vips_contrast_bridge(VipsImage *in, VipsImage **out, double k)
{
return vips_linear1(in, out, k , 0.0, NULL);
}

Loading…
Cancel
Save