Support brightness and contrast

master
Mereng 5 years ago
parent d0fb333ce4
commit 76894654c7

@ -223,6 +223,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)
}
@ -610,3 +622,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
}

@ -830,3 +830,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
}

@ -656,3 +656,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