mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
Improve handling of non-sRGB images
This commit is contained in:
parent
05d021bddf
commit
327429a1e5
20
process.go
20
process.go
@ -258,16 +258,24 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
|
||||
return err
|
||||
}
|
||||
|
||||
iccImported := false
|
||||
convertToLinear := conf.UseLinearColorspace && (scale != 1 || po.Dpr != 1)
|
||||
|
||||
if convertToLinear {
|
||||
if convertToLinear || !img.IsSRGB() {
|
||||
if err = img.ImportColourProfile(true); err != nil {
|
||||
return err
|
||||
}
|
||||
iccImported = true
|
||||
}
|
||||
|
||||
if convertToLinear {
|
||||
if err = img.LinearColourspace(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = img.RgbColourspace(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
hasAlpha := img.HasAlpha()
|
||||
@ -336,16 +344,16 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
|
||||
|
||||
checkTimeout(ctx)
|
||||
|
||||
if convertToLinear {
|
||||
if err = img.RgbColourspace(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if !iccImported {
|
||||
if err = img.ImportColourProfile(false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err = img.RgbColourspace(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) {
|
||||
if err = img.Flatten(po.Background); err != nil {
|
||||
return err
|
||||
|
32
vips.go
32
vips.go
@ -503,28 +503,28 @@ func (img *vipsImage) ImportColourProfile(evenSRGB bool) error {
|
||||
|
||||
profile := (*C.char)(nil)
|
||||
|
||||
if img.VipsImage.Type == C.VIPS_INTERPRETATION_sRGB {
|
||||
// No embedded profile for sRGB, ignore
|
||||
if C.vips_has_embedded_icc(img.VipsImage) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Don't import sRGB IEC61966 2.1 unless evenSRGB
|
||||
if !evenSRGB && C.vips_icc_is_srgb_iec61966(img.VipsImage) != 0 {
|
||||
return nil
|
||||
}
|
||||
} else if img.VipsImage.Type == C.VIPS_INTERPRETATION_CMYK && C.vips_has_embedded_icc(img.VipsImage) == 0 {
|
||||
if C.vips_support_builtin_icc() != 0 {
|
||||
profile = cachedCString("cmyk")
|
||||
} else {
|
||||
// No embedded profile
|
||||
// If vips doesn't have built-in profile, use profile built-in to imgproxy for CMYK
|
||||
// TODO: Remove this. Supporting built-in profiles is pain, vips does it better
|
||||
if img.VipsImage.Type == C.VIPS_INTERPRETATION_CMYK && C.vips_support_builtin_icc() == 0 {
|
||||
p, err := cmykProfilePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
profile = cachedCString(p)
|
||||
} else {
|
||||
// imgproxy doesn't have built-in profile for other interpretations,
|
||||
// so we can't do anything here
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Don't import sRGB IEC61966 2.1 unless evenSRGB
|
||||
if img.VipsImage.Type == C.VIPS_INTERPRETATION_sRGB && !evenSRGB && C.vips_icc_is_srgb_iec61966(img.VipsImage) != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if C.vips_icc_import_go(img.VipsImage, &tmp, profile) == 0 {
|
||||
C.swap_and_clear(&img.VipsImage, tmp)
|
||||
} else {
|
||||
@ -534,6 +534,10 @@ func (img *vipsImage) ImportColourProfile(evenSRGB bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (img *vipsImage) IsSRGB() bool {
|
||||
return img.VipsImage.Type == C.VIPS_INTERPRETATION_sRGB
|
||||
}
|
||||
|
||||
func (img *vipsImage) LinearColourspace() error {
|
||||
return img.Colorspace(C.VIPS_INTERPRETATION_scRGB)
|
||||
}
|
||||
@ -543,7 +547,7 @@ func (img *vipsImage) RgbColourspace() error {
|
||||
}
|
||||
|
||||
func (img *vipsImage) Colorspace(colorspace C.VipsInterpretation) error {
|
||||
if C.vips_image_guess_interpretation(img.VipsImage) != colorspace {
|
||||
if img.VipsImage.Type != colorspace {
|
||||
var tmp *C.VipsImage
|
||||
|
||||
if C.vips_colourspace_go(img.VipsImage, &tmp, colorspace) != 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user