5be456a09f
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".
31 lines
713 B
Go
31 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
|
|
}
|
|
}
|