-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprism_example_test.go
More file actions
205 lines (162 loc) · 5.59 KB
/
prism_example_test.go
File metadata and controls
205 lines (162 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package prism_test
import (
"fmt"
"github.com/mandykoh/prism"
"github.com/mandykoh/prism/adobergb"
"github.com/mandykoh/prism/ciexyz"
"github.com/mandykoh/prism/displayp3"
"github.com/mandykoh/prism/prophotorgb"
"github.com/mandykoh/prism/srgb"
"golang.org/x/image/draw"
"image"
_ "image/jpeg"
"image/png"
_ "image/png"
"log"
"os"
"path/filepath"
"runtime"
)
func loadImage(path string) *image.NRGBA {
imgFile, err := os.Open(path)
if err != nil {
panic(err)
}
defer imgFile.Close()
img, _, err := image.Decode(imgFile)
if err != nil {
panic(err)
}
return prism.ConvertImageToNRGBA(img, runtime.NumCPU())
}
func compare(img1, img2 *image.NRGBA, threshold int) float64 {
diffCount := 0
for i := img1.Rect.Min.Y; i < img1.Rect.Max.Y; i++ {
for j := img1.Rect.Min.X; j < img1.Rect.Max.X; j++ {
c1 := img1.NRGBAAt(j, i)
d1 := [4]int{int(c1.R), int(c1.G), int(c1.B), int(c1.A)}
c2 := img2.NRGBAAt(j, i)
d2 := [4]int{int(c2.R), int(c2.G), int(c2.B), int(c2.A)}
diff := 0
for k := range d1 {
if d1[k] > d2[k] {
diff += d1[k] - d2[k]
} else {
diff += d2[k] - d1[k]
}
}
if diff > threshold {
diffCount++
}
}
}
return float64(diffCount) / float64(img1.Rect.Dx()*img1.Rect.Dy())
}
func writeImage(path string, img image.Image) {
_ = os.MkdirAll(filepath.Dir(path), os.ModePerm)
imgFile, err := os.Create(path)
if err != nil {
panic(err)
}
defer imgFile.Close()
err = png.Encode(imgFile, img)
if err != nil {
panic(err)
}
log.Printf("Output written to %s", path)
}
func Example_convertAdobeRGBToSRGB() {
referenceImg := loadImage("test-images/pizza-rgb8-srgb.jpg")
inputImg := loadImage("test-images/pizza-rgb8-adobergb.jpg")
convertedImg := image.NewNRGBA(inputImg.Rect)
for i := inputImg.Rect.Min.Y; i < inputImg.Rect.Max.Y; i++ {
for j := inputImg.Rect.Min.X; j < inputImg.Rect.Max.X; j++ {
inCol, alpha := adobergb.ColorFromNRGBA(inputImg.NRGBAAt(j, i))
outCol := srgb.ColorFromXYZ(inCol.ToXYZ())
convertedImg.SetNRGBA(j, i, outCol.ToNRGBA(alpha))
}
}
writeImage("example-output/adobergb-to-srgb.png", convertedImg)
if difference := compare(convertedImg, referenceImg, 5); difference > 0.01 {
fmt.Printf("Images differ by %.2f%% of pixels exceeding difference threshold", difference*100)
} else {
fmt.Printf("Images match")
}
// Output: Images match
}
func Example_convertDisplayP3ToSRGB() {
referenceImg := loadImage("test-images/pizza-rgb8-srgb.jpg")
inputImg := loadImage("test-images/pizza-rgb8-displayp3.jpg")
convertedImg := image.NewNRGBA(inputImg.Rect)
for i := inputImg.Rect.Min.Y; i < inputImg.Rect.Max.Y; i++ {
for j := inputImg.Rect.Min.X; j < inputImg.Rect.Max.X; j++ {
inCol, alpha := displayp3.ColorFromNRGBA(inputImg.NRGBAAt(j, i))
outCol := srgb.ColorFromXYZ(inCol.ToXYZ())
convertedImg.SetNRGBA(j, i, outCol.ToNRGBA(alpha))
}
}
writeImage("example-output/displayp3-to-srgb.png", convertedImg)
if difference := compare(convertedImg, referenceImg, 5); difference > 0.005 {
fmt.Printf("Images differ by %.2f%% of pixels exceeding difference threshold", difference*100)
} else {
fmt.Printf("Images match")
}
// Output: Images match
}
func Example_convertProPhotoRGBToSRGB() {
referenceImg := loadImage("test-images/pizza-rgb8-srgb.jpg")
inputImg := loadImage("test-images/pizza-rgb8-prophotorgb.jpg")
adaptation := ciexyz.AdaptBetweenXYYWhitePoints(
prophotorgb.StandardWhitePoint,
srgb.StandardWhitePoint,
)
convertedImg := image.NewNRGBA(inputImg.Rect)
for i := inputImg.Rect.Min.Y; i < inputImg.Rect.Max.Y; i++ {
for j := inputImg.Rect.Min.X; j < inputImg.Rect.Max.X; j++ {
inCol, alpha := prophotorgb.ColorFromNRGBA(inputImg.NRGBAAt(j, i))
xyz := inCol.ToXYZ()
xyz = adaptation.Apply(xyz)
outCol := srgb.ColorFromXYZ(xyz)
convertedImg.SetNRGBA(j, i, outCol.ToNRGBA(alpha))
}
}
writeImage("example-output/prophotorgb-to-srgb.png", convertedImg)
if difference := compare(convertedImg, referenceImg, 5); difference > 0.015 {
fmt.Printf("Images differ by %.2f%% of pixels exceeding difference threshold", difference*100)
} else {
fmt.Printf("Images match")
}
// Output: Images match
}
func Example_convertSRGBToAdobeRGB() {
referenceImg := loadImage("test-images/pizza-rgb8-adobergb.jpg")
inputImg := loadImage("test-images/pizza-rgb8-srgb.jpg")
convertedImg := image.NewNRGBA(inputImg.Rect)
for i := inputImg.Rect.Min.Y; i < inputImg.Rect.Max.Y; i++ {
for j := inputImg.Rect.Min.X; j < inputImg.Rect.Max.X; j++ {
inCol, alpha := srgb.ColorFromNRGBA(inputImg.NRGBAAt(j, i))
outCol := adobergb.ColorFromXYZ(inCol.ToXYZ())
convertedImg.SetNRGBA(j, i, outCol.ToNRGBA(alpha))
}
}
// Output will be written without an embedded colour profile (software used
// to examine this image will assume sRGB unless told otherwise).
//writeImage("example-output/srgb-to-adobergb.png", convertedImg)
if difference := compare(convertedImg, referenceImg, 4); difference > 0.01 {
fmt.Printf("Images differ by %.2f%% of pixels exceeding difference threshold", difference*100)
} else {
fmt.Printf("Images match")
}
// Output: Images match
}
func Example_linearisedResampling() {
img := loadImage("test-images/checkerboard-srgb.png")
rgba64 := image.NewRGBA64(img.Bounds())
srgb.LineariseImage(rgba64, img, runtime.NumCPU())
resampled := image.NewNRGBA64(image.Rect(0, 0, rgba64.Rect.Dx()/2, rgba64.Rect.Dy()/2))
draw.BiLinear.Scale(resampled, resampled.Rect, rgba64, rgba64.Rect, draw.Src, nil)
rgba := image.NewRGBA(resampled.Rect)
srgb.EncodeImage(rgba, resampled, runtime.NumCPU())
writeImage("example-output/checkerboard-resampled.png", rgba)
// Output:
}