photoprism/pkg/colors/srgb.go
Michael Mayer 5be456a09f JPEG: Convert Apple "Display P3" colors to standard sRGB #1474
Other color profiles and file formats are not supported yet. Should
be easy to add though. Main difficulty will be profile name comparison:
For example "Adobe RGB (1998)" vs just "Adobe RGB".
2021-12-09 07:00:39 +01:00

32 lines
713 B
Go

package colors
import (
"image"
_ "image/jpeg"
"runtime"
"github.com/mandykoh/prism"
"github.com/mandykoh/prism/displayp3"
"github.com/mandykoh/prism/srgb"
)
// ToSRGB converts an image to sRGB colors.
func ToSRGB(img image.Image, profile Profile) image.Image {
switch profile {
case ProfileDisplayP3:
in := prism.ConvertImageToNRGBA(img, runtime.NumCPU())
out := image.NewNRGBA(in.Rect)
for i := in.Rect.Min.Y; i < in.Rect.Max.Y; i++ {
for j := in.Rect.Min.X; j < in.Rect.Max.X; j++ {
inCol, alpha := displayp3.ColorFromNRGBA(in.NRGBAAt(j, i))
outCol := srgb.ColorFromXYZ(inCol.ToXYZ())
out.SetNRGBA(j, i, outCol.ToNRGBA(alpha))
}
}
return out
default:
return img
}
}