-
Notifications
You must be signed in to change notification settings - Fork 7
/
prism.go
156 lines (132 loc) · 5.41 KB
/
prism.go
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
// Package prism provides a set of tools for colour management and conversion.
// Subpackages provide support for encoding/decoding image pixel data in
// specific colour spaces, and conversions between those spaces.
package prism
import (
"github.com/mandykoh/go-parallel"
"image"
"image/color"
"image/draw"
)
// ConvertImageToNRGBA is a convenience function for getting an NRGBA image from
// any image. If the specified image isn’t already NRGBA, a conversion is
// performed.
//
// parallelism specifies the maximum degree of parallel processing; a value of 4
// indicates that processing may be spread across up to four threads. However,
// this is not guaranteed as not all conversions are parallelised.
func ConvertImageToNRGBA(img image.Image, parallelism int) *image.NRGBA {
switch inputImg := img.(type) {
case *image.NRGBA:
return inputImg
case *image.YCbCr:
outputImg := image.NewNRGBA(inputImg.Rect)
parallel.RunWorkers(parallelism, func(workerNum, workerCount int) {
for i := outputImg.Rect.Min.Y + workerNum; i < outputImg.Rect.Max.Y; i += workerCount {
for j := outputImg.Rect.Min.X; j < outputImg.Rect.Max.X; j++ {
c := inputImg.YCbCrAt(j, i)
r, g, b := color.YCbCrToRGB(c.Y, c.Cb, c.Cr)
nrgba := color.NRGBA{R: r, G: g, B: b, A: 255}
outputImg.SetNRGBA(j, i, nrgba)
}
}
})
return outputImg
default:
outputImg := image.NewNRGBA(img.Bounds())
draw.Draw(outputImg, outputImg.Rect, img, outputImg.Rect.Min, draw.Src)
return outputImg
}
}
// ConvertImageToRGBA is a convenience function for getting an RGBA image
// from any image. If the specified image isn’t already RGBA, a conversion is
// performed.
//
// parallelism specifies the maximum degree of parallel processing; a value of 4
// indicates that processing may be spread across up to four threads. However,
// this is not guaranteed as not all conversions are parallelised.
func ConvertImageToRGBA(img image.Image, parallelism int) *image.RGBA {
switch inputImg := img.(type) {
case *image.RGBA:
return inputImg
case *image.RGBA64:
outputImg := image.NewRGBA(inputImg.Rect)
parallel.RunWorkers(parallelism, func(workerNum, workerCount int) {
for i := outputImg.Rect.Min.Y + workerNum; i < outputImg.Rect.Max.Y; i += workerCount {
for j := outputImg.Rect.Min.X; j < outputImg.Rect.Max.X; j++ {
inputOffset := inputImg.PixOffset(j, i)
outputOffset := outputImg.PixOffset(j, i)
outputImg.Pix[outputOffset] = inputImg.Pix[inputOffset]
outputImg.Pix[outputOffset+1] = inputImg.Pix[inputOffset+2]
outputImg.Pix[outputOffset+2] = inputImg.Pix[inputOffset+4]
outputImg.Pix[outputOffset+3] = inputImg.Pix[inputOffset+6]
}
}
})
return outputImg
default:
outputImg := image.NewRGBA(img.Bounds())
draw.Draw(outputImg, outputImg.Rect, img, outputImg.Rect.Min, draw.Src)
return outputImg
}
}
// ConvertImageToRGBA64 is a convenience function for getting an RGBA64 image
// from any image. If the specified image isn’t already RGBA64, a conversion is
// performed.
//
// parallelism specifies the maximum degree of parallel processing; a value of 4
// indicates that processing may be spread across up to four threads. However,
// this is not guaranteed as not all conversions are parallelised.
func ConvertImageToRGBA64(img image.Image, parallelism int) *image.RGBA64 {
switch inputImg := img.(type) {
case *image.NRGBA:
outputImg := image.NewRGBA64(inputImg.Rect)
parallel.RunWorkers(parallelism, func(workerNum, workerCount int) {
for i := outputImg.Rect.Min.Y + workerNum; i < outputImg.Rect.Max.Y; i += workerCount {
for j := outputImg.Rect.Min.X; j < outputImg.Rect.Max.X; j++ {
r, g, b, a := inputImg.NRGBAAt(j, i).RGBA()
rgba64 := color.RGBA64{R: uint16(r), G: uint16(g), B: uint16(b), A: uint16(a)}
outputImg.SetRGBA64(j, i, rgba64)
}
}
})
return outputImg
case *image.RGBA:
outputImg := image.NewRGBA64(inputImg.Rect)
parallel.RunWorkers(parallelism, func(workerNum, workerCount int) {
for i := outputImg.Rect.Min.Y + workerNum; i < outputImg.Rect.Max.Y; i += workerCount {
for j := outputImg.Rect.Min.X; j < outputImg.Rect.Max.X; j++ {
inputOffset := inputImg.PixOffset(j, i)
outputOffset := outputImg.PixOffset(j, i)
outputImg.Pix[outputOffset] = inputImg.Pix[inputOffset]
outputImg.Pix[outputOffset+1] = inputImg.Pix[inputOffset]
outputImg.Pix[outputOffset+2] = inputImg.Pix[inputOffset+1]
outputImg.Pix[outputOffset+3] = inputImg.Pix[inputOffset+1]
outputImg.Pix[outputOffset+4] = inputImg.Pix[inputOffset+2]
outputImg.Pix[outputOffset+5] = inputImg.Pix[inputOffset+2]
outputImg.Pix[outputOffset+6] = inputImg.Pix[inputOffset+3]
outputImg.Pix[outputOffset+7] = inputImg.Pix[inputOffset+3]
}
}
})
return outputImg
case *image.RGBA64:
return inputImg
case *image.YCbCr:
outputImg := image.NewRGBA64(inputImg.Rect)
parallel.RunWorkers(parallelism, func(workerNum, workerCount int) {
for i := outputImg.Rect.Min.Y + workerNum; i < outputImg.Rect.Max.Y; i += workerCount {
for j := outputImg.Rect.Min.X; j < outputImg.Rect.Max.X; j++ {
r, g, b, _ := inputImg.YCbCrAt(j, i).RGBA()
rgba64 := color.RGBA64{R: uint16(r), G: uint16(g), B: uint16(b), A: 65535}
outputImg.SetRGBA64(j, i, rgba64)
}
}
})
return outputImg
default:
outputImg := image.NewRGBA64(img.Bounds())
draw.Draw(outputImg, outputImg.Rect, img, outputImg.Rect.Min, draw.Src)
return outputImg
}
}