forked from pion/mediadevices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
142 lines (124 loc) · 3.02 KB
/
convert.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
package video
import (
"fmt"
"image"
"image/color"
)
// imageToYCbCr converts src to *image.YCbCr and store it to dst
// Note: conversion can be lossy
func imageToYCbCr(dst *image.YCbCr, src image.Image) {
if dst == nil {
panic("dst can't be nil")
}
yuvImg, ok := src.(*image.YCbCr)
if ok {
*dst = *yuvImg
return
}
bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
flat := dy * dx
if len(dst.Y)+len(dst.Cb)+len(dst.Cr) < 3*flat {
i0 := 1 * flat
i1 := 2 * flat
i2 := 3 * flat
if cap(dst.Y) < i2 {
dst.Y = make([]uint8, i2)
}
dst.Y = dst.Y[:i0]
dst.Cb = dst.Y[i0:i1]
dst.Cr = dst.Y[i1:i2]
}
dst.SubsampleRatio = image.YCbCrSubsampleRatio444
dst.YStride = dx
dst.CStride = dx
dst.Rect = bounds
switch s := src.(type) {
case *image.RGBA:
rgbaToI444(dst, s)
default:
i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
// TODO: probably try to get the alpha value with something like
// https://en.wikipedia.org/wiki/Alpha_compositing
r, g, b, _ := src.At(xi, yi).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r/256), uint8(g/256), uint8(b/256))
dst.Y[i] = yy
dst.Cb[i] = cb
dst.Cr[i] = cr
i++
}
}
}
}
// ToI420 converts r to a new reader that will output images in I420 format
func ToI420(r Reader) Reader {
var yuvImg image.YCbCr
return ReaderFunc(func() (image.Image, func(), error) {
img, _, err := r.Read()
if err != nil {
return nil, func() {}, err
}
imageToYCbCr(&yuvImg, img)
// Covert pixel format to I420
switch yuvImg.SubsampleRatio {
case image.YCbCrSubsampleRatio444:
i444ToI420(&yuvImg)
case image.YCbCrSubsampleRatio422:
i422ToI420(&yuvImg)
case image.YCbCrSubsampleRatio420:
default:
return nil, func() {}, fmt.Errorf("unsupported pixel format: %s", yuvImg.SubsampleRatio)
}
yuvImg.SubsampleRatio = image.YCbCrSubsampleRatio420
return &yuvImg, func() {}, nil
})
}
// imageToRGBA converts src to *image.RGBA and store it to dst
func imageToRGBA(dst *image.RGBA, src image.Image) {
if dst == nil {
panic("dst can't be nil")
}
if srcRGBA, ok := src.(*image.RGBA); ok {
*dst = *srcRGBA
return
}
bounds := src.Bounds()
dy := bounds.Dy()
dx := bounds.Dx()
if len(dst.Pix) < 4*dx*dy {
dst.Pix = make([]uint8, 4*dx*dy)
}
dst.Stride = 4 * dx
dst.Rect = bounds
if srcYCbCr, ok := src.(*image.YCbCr); ok &&
srcYCbCr.SubsampleRatio == image.YCbCrSubsampleRatio444 {
i444ToRGBA(dst, srcYCbCr)
return
}
i := 0
for yi := 0; yi < dy; yi++ {
for xi := 0; xi < dx; xi++ {
r, g, b, a := src.At(xi, yi).RGBA()
dst.Pix[i+0] = uint8(r / 0x100)
dst.Pix[i+1] = uint8(g / 0x100)
dst.Pix[i+2] = uint8(b / 0x100)
dst.Pix[i+3] = uint8(a / 0x100)
i += 4
}
}
}
// ToRGBA converts r to a new reader that will output images in RGBA format
func ToRGBA(r Reader) Reader {
var dst image.RGBA
return ReaderFunc(func() (image.Image, func(), error) {
img, _, err := r.Read()
if err != nil {
return nil, func() {}, err
}
imageToRGBA(&dst, img)
return &dst, func() {}, nil
})
}