forked from chai2010/tiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
448 lines (402 loc) · 9.59 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"image"
"image/color"
"reflect"
"runtime"
"unsafe"
)
const (
MemPMagic = "MemP" // See https://github.com/chai2010/image
)
const (
isLittleEndian = (runtime.GOARCH == "386" ||
runtime.GOARCH == "amd64" ||
runtime.GOARCH == "arm" ||
runtime.GOARCH == "arm64")
)
var (
_ image.Image = (*MemPImage)(nil)
_ MemP = (*MemPImage)(nil)
)
// MemP Image Spec (Native Endian), see https://github.com/chai2010/image.
type MemP interface {
MemPMagic() string
Bounds() image.Rectangle
Channels() int
DataType() reflect.Kind
Pix() []byte // PixSlice type
// Stride is the Pix stride (in bytes, must align with SizeofKind(p.DataType))
// between vertically adjacent pixels.
Stride() int
}
type MemPImage struct {
XMemPMagic string // MemP
XRect image.Rectangle
XChannels int
XDataType reflect.Kind
XPix PixSlice
XStride int
}
func NewMemPImage(r image.Rectangle, channels int, dataType reflect.Kind) *MemPImage {
m := &MemPImage{
XMemPMagic: MemPMagic,
XRect: r,
XStride: r.Dx() * channels * SizeofKind(dataType),
XChannels: channels,
XDataType: dataType,
}
m.XPix = make([]byte, r.Dy()*m.XStride)
return m
}
// m is MemP or image.Image
func AsMemPImage(m interface{}) (p *MemPImage, ok bool) {
if m, ok := m.(*MemPImage); ok {
return m, true
}
if m, ok := m.(MemP); ok {
return &MemPImage{
XMemPMagic: MemPMagic,
XRect: m.Bounds(),
XChannels: m.Channels(),
XDataType: m.DataType(),
XPix: m.Pix(),
XStride: m.Stride(),
}, true
}
if m, ok := m.(*image.Gray); ok {
return &MemPImage{
XMemPMagic: MemPMagic,
XRect: m.Bounds(),
XChannels: 1,
XDataType: reflect.Uint8,
XPix: m.Pix,
XStride: m.Stride,
}, true
}
if m, ok := m.(*image.RGBA); ok {
return &MemPImage{
XMemPMagic: MemPMagic,
XRect: m.Bounds(),
XChannels: 4,
XDataType: reflect.Uint8,
XPix: m.Pix,
XStride: m.Stride,
}, true
}
return nil, false
}
func NewMemPImageFrom(m image.Image) *MemPImage {
if p, ok := m.(*MemPImage); ok {
return p.Clone()
}
if p, ok := AsMemPImage(m); ok {
return p.Clone()
}
switch m := m.(type) {
case *image.Gray:
b := m.Bounds()
p := NewMemPImage(b, 1, reflect.Uint8)
for y := b.Min.Y; y < b.Max.Y; y++ {
off0 := m.PixOffset(0, y)
off1 := p.PixOffset(0, y)
copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
off0 += m.Stride
off1 += p.XStride
}
return p
case *image.Gray16:
b := m.Bounds()
p := NewMemPImage(b, 1, reflect.Uint16)
for y := b.Min.Y; y < b.Max.Y; y++ {
off0 := m.PixOffset(0, y)
off1 := p.PixOffset(0, y)
copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
off0 += m.Stride
off1 += p.XStride
}
if isLittleEndian {
p.XPix.SwapEndian(p.XDataType)
}
return p
case *image.RGBA:
b := m.Bounds()
p := NewMemPImage(b, 4, reflect.Uint8)
for y := b.Min.Y; y < b.Max.Y; y++ {
off0 := m.PixOffset(0, y)
off1 := p.PixOffset(0, y)
copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
off0 += m.Stride
off1 += p.XStride
}
return p
case *image.RGBA64:
b := m.Bounds()
p := NewMemPImage(b, 4, reflect.Uint16)
for y := b.Min.Y; y < b.Max.Y; y++ {
off0 := m.PixOffset(0, y)
off1 := p.PixOffset(0, y)
copy(p.XPix[off1:][:p.XStride], m.Pix[off0:][:m.Stride])
off0 += m.Stride
off1 += p.XStride
}
if isLittleEndian {
p.XPix.SwapEndian(p.XDataType)
}
return p
case *image.YCbCr:
b := m.Bounds()
p := NewMemPImage(b, 4, reflect.Uint8)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
R, G, B, A := m.At(x, y).RGBA()
i := p.PixOffset(x, y)
p.XPix[i+0] = uint8(R >> 8)
p.XPix[i+1] = uint8(G >> 8)
p.XPix[i+2] = uint8(B >> 8)
p.XPix[i+3] = uint8(A >> 8)
}
}
return p
default:
b := m.Bounds()
p := NewMemPImage(b, 4, reflect.Uint16)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
R, G, B, A := m.At(x, y).RGBA()
i := p.PixOffset(x, y)
p.XPix[i+0] = uint8(R >> 8)
p.XPix[i+1] = uint8(R)
p.XPix[i+2] = uint8(G >> 8)
p.XPix[i+3] = uint8(G)
p.XPix[i+4] = uint8(B >> 8)
p.XPix[i+5] = uint8(B)
p.XPix[i+6] = uint8(A >> 8)
p.XPix[i+7] = uint8(A)
}
}
return p
}
}
func (p *MemPImage) Clone() *MemPImage {
q := new(MemPImage)
*q = *p
q.XPix = append([]byte(nil), p.XPix...)
return q
}
func (p *MemPImage) MemPMagic() string {
return p.XMemPMagic
}
func (p *MemPImage) Bounds() image.Rectangle {
return p.XRect
}
func (p *MemPImage) Channels() int {
return p.XChannels
}
func (p *MemPImage) DataType() reflect.Kind {
return p.XDataType
}
func (p *MemPImage) Pix() []byte {
return p.XPix
}
func (p *MemPImage) Stride() int {
return p.XStride
}
func (p *MemPImage) ColorModel() color.Model {
return ColorModel(p.XChannels, p.XDataType)
}
func (p *MemPImage) At(x, y int) color.Color {
if !(image.Point{x, y}.In(p.XRect)) {
return MemPColor{
Channels: p.XChannels,
DataType: p.XDataType,
}
}
i := p.PixOffset(x, y)
n := SizeofPixel(p.XChannels, p.XDataType)
return MemPColor{
Channels: p.XChannels,
DataType: p.XDataType,
Pix: p.XPix[i:][:n],
}
}
func (p *MemPImage) PixelAt(x, y int) []byte {
if !(image.Point{x, y}.In(p.XRect)) {
return nil
}
i := p.PixOffset(x, y)
n := SizeofPixel(p.XChannels, p.XDataType)
return p.XPix[i:][:n]
}
func (p *MemPImage) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.XRect)) {
return
}
i := p.PixOffset(x, y)
n := SizeofPixel(p.XChannels, p.XDataType)
v := p.ColorModel().Convert(c).(MemPColor)
copy(p.XPix[i:][:n], v.Pix)
}
func (p *MemPImage) SetPixel(x, y int, c []byte) {
if !(image.Point{x, y}.In(p.XRect)) {
return
}
i := p.PixOffset(x, y)
n := SizeofPixel(p.XChannels, p.XDataType)
copy(p.XPix[i:][:n], c)
}
func (p *MemPImage) PixOffset(x, y int) int {
return (y-p.XRect.Min.Y)*p.XStride + (x-p.XRect.Min.X)*SizeofPixel(p.XChannels, p.XDataType)
}
func (p *MemPImage) SubImage(r image.Rectangle) image.Image {
r = r.Intersect(p.XRect)
// If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside
// either r1 or r2 if the intersection is empty. Without explicitly checking for
// this, the Pix[i:] expression below can panic.
if r.Empty() {
return &MemPImage{}
}
i := p.PixOffset(r.Min.X, r.Min.Y)
return &MemPImage{
XRect: r,
XChannels: p.XChannels,
XDataType: p.XDataType,
XPix: p.XPix[i:],
XStride: p.XStride,
}
}
func (p *MemPImage) AsStdImage() (m image.Image, ok bool) {
switch {
case p.XChannels == 1 && p.XDataType == reflect.Uint8:
return &image.Gray{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}, true
case p.XChannels == 4 && p.XDataType == reflect.Uint8:
return &image.RGBA{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}, true
default:
return nil, false
}
}
func (p *MemPImage) StdImage() image.Image {
switch {
case p.XChannels == 1 && p.XDataType == reflect.Uint8:
return &image.Gray{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
case p.XChannels == 1 && p.XDataType == reflect.Uint16:
m := &image.Gray16{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
if isLittleEndian {
m.Pix = append([]byte(nil), m.Pix...)
PixSlice(m.Pix).SwapEndian(p.XDataType)
}
return m
case p.XChannels == 4 && p.XDataType == reflect.Uint8:
return &image.RGBA{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
case p.XChannels == 4 && p.XDataType == reflect.Uint16:
m := &image.RGBA64{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
if isLittleEndian {
m.Pix = append([]byte(nil), m.Pix...)
PixSlice(m.Pix).SwapEndian(p.XDataType)
}
return m
}
return p
}
func ChannelsOf(m image.Image) int {
if m, ok := AsMemPImage(m); ok {
return m.XChannels
}
switch m.(type) {
case *image.Gray:
return 1
case *image.Gray16:
return 1
case *image.YCbCr:
return 3
}
return 4
}
func DepthOf(m image.Image) int {
if m, ok := m.(*MemPImage); ok {
return SizeofKind(m.XDataType) * 8
}
if m, ok := m.(MemP); ok {
return SizeofKind(m.DataType() * 8)
}
switch m.(type) {
case *image.Gray:
return 1 * 8
case *image.Gray16:
return 2 * 8
case *image.NRGBA:
return 1 * 8
case *image.NRGBA64:
return 2 * 8
case *image.RGBA:
return 1 * 8
case *image.RGBA64:
return 2 * 8
case *image.YCbCr:
return 1 * 8
}
return 2 * 8
}
type SizeofImager interface {
SizeofImage() int
}
func SizeofImage(m image.Image) int {
if m, ok := m.(SizeofImager); ok {
return m.SizeofImage()
}
if m, ok := AsMemPImage(m); ok {
return int(unsafe.Sizeof(*m)) + len(m.XPix)
}
b := m.Bounds()
switch m := m.(type) {
case *image.Alpha:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*1
case *image.Alpha16:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*2
case *image.Gray:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*1
case *image.Gray16:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*2
case *image.NRGBA:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*4
case *image.NRGBA64:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*8
case *image.RGBA:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*4
case *image.RGBA64:
return int(unsafe.Sizeof(*m)) + b.Dx()*b.Dy()*8
case *image.Uniform:
return int(unsafe.Sizeof(*m))
case *image.YCbCr:
return int(unsafe.Sizeof(*m)) + len(m.Y) + len(m.Cb) + len(m.Cr)
}
// return same as RGBA64 size
return int(unsafe.Sizeof((*image.RGBA64)(nil))) + b.Dx()*b.Dy()*8
}