forked from bmharper/cimg
/
image.go
291 lines (279 loc) · 7.34 KB
/
image.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package cimg
import (
"errors"
"fmt"
"image"
)
// Image is the concrete image type that is used by all functions inside cimg
type Image struct {
Pixels []byte
Width int
Height int
Stride int
Format PixelFormat
Premultiplied bool
}
// NChan returns the number of channels of the pixel format
func NChan(pf PixelFormat) int {
switch pf {
case PixelFormatRGB:
return 3
case PixelFormatBGR:
return 3
case PixelFormatRGBX:
return 4
case PixelFormatBGRX:
return 4
case PixelFormatXBGR:
return 4
case PixelFormatXRGB:
return 4
case PixelFormatGRAY:
return 1
case PixelFormatRGBA:
return 4
case PixelFormatBGRA:
return 4
case PixelFormatABGR:
return 4
case PixelFormatARGB:
return 4
case PixelFormatCMYK:
return 4
}
panic(fmt.Errorf("Unrecognized pixel format %v", pf))
}
// NewImage creates a new 8-bit image
func NewImage(width, height int, format PixelFormat) *Image {
return &Image{
Width: width,
Height: height,
Stride: width * NChan(format),
Format: format,
Pixels: make([]byte, height*width*NChan(format)),
Premultiplied: false,
}
}
// Wrap an array of bytes into an Image object (do not copy pixels)
func WrapImage(width, height int, format PixelFormat, pixels []byte) *Image {
return &Image{
Width: width,
Height: height,
Stride: width * NChan(format),
Format: format,
Pixels: pixels,
Premultiplied: false,
}
}
// Wrap an array of bytes into an Image object, with controllable stride (do not copy pixels)
func WrapImageStrided(width, height int, format PixelFormat, pixels []byte, stride int) *Image {
return &Image{
Width: width,
Height: height,
Stride: stride,
Format: format,
Pixels: pixels,
Premultiplied: false,
}
}
// Convert a Go image.Image into a cimg.Image
// If allowDeepClone is true, and the source image is type GRAY, NRGBA, or RGBA,
// then the resulting Image points directly to the pixel buffer of the source image.
func FromImage(src image.Image, allowDeepClone bool) (*Image, error) {
dst := &Image{
Width: src.Bounds().Dx(),
Height: src.Bounds().Dy(),
}
switch v := src.(type) {
case *image.Gray:
dst.Format = PixelFormatGRAY
dst.Stride = NChan(dst.Format) * dst.Width
if allowDeepClone {
dst.Pixels = v.Pix
} else {
dst.Pixels = make([]byte, dst.Stride*dst.Height)
copy(dst.Pixels, v.Pix)
}
return dst, nil
case *image.RGBA:
dst.Format = PixelFormatRGBA
dst.Premultiplied = true
dst.Stride = NChan(dst.Format) * dst.Width
if allowDeepClone {
dst.Pixels = v.Pix
} else {
dst.Pixels = make([]byte, dst.Stride*dst.Height)
copy(dst.Pixels, v.Pix)
}
return dst, nil
case *image.NRGBA:
dst.Format = PixelFormatRGBA
dst.Premultiplied = false
dst.Stride = NChan(dst.Format) * dst.Width
if allowDeepClone {
dst.Pixels = v.Pix
} else {
dst.Pixels = make([]byte, dst.Stride*dst.Height)
copy(dst.Pixels, v.Pix)
}
return dst, nil
}
return nil, errors.New("Unsupported source image type")
}
// ToImage returns an image from the Go standard library 'image' package
func (img *Image) ToImage() (image.Image, error) {
if img.Format == PixelFormatGRAY {
dst := image.NewGray(image.Rect(0, 0, img.Width, img.Height))
srcBuf := img.Pixels
dstBuf := dst.Pix
for y := 0; y < img.Height; y++ {
srcP := img.Stride * y
dstP := dst.Stride * y
copy(dstBuf[dstP:dstP+dst.Stride], srcBuf[srcP:srcP+img.Stride])
}
return dst, nil
} else if img.Format == PixelFormatRGB || img.Format == PixelFormatBGR {
dst := image.NewRGBA(image.Rect(0, 0, img.Width, img.Height))
srcBuf := img.Pixels
dstBuf := dst.Pix
width := img.Width
for y := 0; y < img.Height; y++ {
srcP := img.Stride * y
dstP := dst.Stride * y
if img.Format == PixelFormatBGR {
// BGR -> RGB
for x := 0; x < width; x++ {
dstBuf[dstP] = srcBuf[srcP+2]
dstBuf[dstP+1] = srcBuf[srcP+1]
dstBuf[dstP+2] = srcBuf[srcP]
dstBuf[dstP+3] = 255
srcP += 3
dstP += 4
}
} else {
for x := 0; x < width; x++ {
dstBuf[dstP] = srcBuf[srcP]
dstBuf[dstP+1] = srcBuf[srcP+1]
dstBuf[dstP+2] = srcBuf[srcP+2]
dstBuf[dstP+3] = 255
srcP += 3
dstP += 4
}
}
}
return dst, nil
} else if img.Format == PixelFormatRGBA {
var dst image.Image
var dstStride int
var dstBuf []uint8
if img.Premultiplied {
d := image.NewRGBA(image.Rect(0, 0, img.Width, img.Height))
dstStride = d.Stride
dstBuf = d.Pix
dst = d
} else {
d := image.NewNRGBA(image.Rect(0, 0, img.Width, img.Height))
dstStride = d.Stride
dstBuf = d.Pix
dst = d
}
srcBuf := img.Pixels
for y := 0; y < img.Height; y++ {
srcP := img.Stride * y
dstP := dstStride * y
copy(dstBuf[dstP:dstP+dstStride], srcBuf[srcP:srcP+img.Stride])
}
return dst, nil
} else if img.Format == PixelFormatBGRA || img.Format == PixelFormatABGR || img.Format == PixelFormatARGB {
var dst image.Image
var dstStride int
var dstBuf []uint8
if img.Premultiplied {
d := image.NewRGBA(image.Rect(0, 0, img.Width, img.Height))
dstStride = d.Stride
dstBuf = d.Pix
dst = d
} else {
d := image.NewNRGBA(image.Rect(0, 0, img.Width, img.Height))
dstStride = d.Stride
dstBuf = d.Pix
dst = d
}
srcBuf := img.Pixels
width := img.Width
for y := 0; y < img.Height; y++ {
srcP := img.Stride * y
dstP := dstStride * y
switch img.Format {
case PixelFormatBGRA:
for x := 0; x < width; x++ {
dstBuf[dstP] = srcBuf[srcP+2]
dstBuf[dstP+1] = srcBuf[srcP+1]
dstBuf[dstP+2] = srcBuf[srcP]
dstBuf[dstP+3] = srcBuf[srcP+3]
srcP += 4
dstP += 4
}
case PixelFormatABGR:
for x := 0; x < width; x++ {
dstBuf[dstP] = srcBuf[srcP+3]
dstBuf[dstP+1] = srcBuf[srcP+2]
dstBuf[dstP+2] = srcBuf[srcP+1]
dstBuf[dstP+3] = srcBuf[srcP]
srcP += 4
dstP += 4
}
case PixelFormatARGB:
for x := 0; x < width; x++ {
dstBuf[dstP] = srcBuf[srcP+1]
dstBuf[dstP+1] = srcBuf[srcP+2]
dstBuf[dstP+2] = srcBuf[srcP+3]
dstBuf[dstP+3] = srcBuf[srcP]
srcP += 4
dstP += 4
}
}
}
return dst, nil
} else {
return nil, fmt.Errorf("Unsupported image type %v", img.Format)
}
}
// Clone returns a deep clone of the image
func (img *Image) Clone() *Image {
copy := NewImage(img.Width, img.Height, img.Format)
copy.Premultiplied = img.Premultiplied
copy.CopyImage(img, 0, 0)
return copy
}
// NChan returns the number of channels of the pixel format of the image
func (img *Image) NChan() int {
return NChan(img.Format)
}
// ResizeNew 缩放图像
func (img *Image) ResizeNew(w, h int) *Image {
return ResizeNew(img, w, h)
}
// ChannelSpliter 图像通道分离
func (img *Image) ChannelSpliter() []byte {
// 提取颜色通道数
channelNum := img.NChan()
// 单色通道大小
channelSize := img.Width * img.Height
// 图像大小
imgSize := channelSize * channelNum
// 声明目标图像空间
dstBytes := make([]byte, imgSize)
// 开始拷贝
for pix := 0; pix < channelSize; pix++ {
// pix: 表示第几个像素
for c := 0; c < channelNum; c++ {
// c: 表示第几个颜色通道
// pix*channelNum+c: 表示第pix个像素第c个通道的位置
// c*channelSize+pix: 表示第c个通道第pix像素的通道颜色
dstBytes[c*channelSize+pix] = img.Pixels[pix*channelNum+c]
}
}
// OK
return dstBytes
}