1
2
3 package imageutil
4
5 import (
6 "image"
7 )
8
9
10
11
12
13
14
15
16 func DrawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Point) (ok bool) {
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 x0 := (r.Min.X - dst.Rect.Min.X) * 4
33 x1 := (r.Max.X - dst.Rect.Min.X) * 4
34 y0 := r.Min.Y - dst.Rect.Min.Y
35 y1 := r.Max.Y - dst.Rect.Min.Y
36 switch src.SubsampleRatio {
37
38 case image.YCbCrSubsampleRatio444:
39 for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 {
40 dpix := dst.Pix[y*dst.Stride:]
41 yi := (sy-src.Rect.Min.Y)*src.YStride + (sp.X - src.Rect.Min.X)
42
43 ci := (sy-src.Rect.Min.Y)*src.CStride + (sp.X - src.Rect.Min.X)
44 for x := x0; x != x1; x, yi, ci = x+4, yi+1, ci+1 {
45
46
47 yy1 := int32(src.Y[yi]) * 0x10101
48 cb1 := int32(src.Cb[ci]) - 128
49 cr1 := int32(src.Cr[ci]) - 128
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 r := yy1 + 91881*cr1
65 if uint32(r)&0xff000000 == 0 {
66 r >>= 16
67 } else {
68 r = ^(r >> 31)
69 }
70
71 g := yy1 - 22554*cb1 - 46802*cr1
72 if uint32(g)&0xff000000 == 0 {
73 g >>= 16
74 } else {
75 g = ^(g >> 31)
76 }
77
78 b := yy1 + 116130*cb1
79 if uint32(b)&0xff000000 == 0 {
80 b >>= 16
81 } else {
82 b = ^(b >> 31)
83 }
84
85
86 rgba := dpix[x : x+4 : len(dpix)]
87 rgba[0] = uint8(r)
88 rgba[1] = uint8(g)
89 rgba[2] = uint8(b)
90 rgba[3] = 255
91 }
92 }
93
94 case image.YCbCrSubsampleRatio422:
95 for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 {
96 dpix := dst.Pix[y*dst.Stride:]
97 yi := (sy-src.Rect.Min.Y)*src.YStride + (sp.X - src.Rect.Min.X)
98
99 ciBase := (sy-src.Rect.Min.Y)*src.CStride - src.Rect.Min.X/2
100 for x, sx := x0, sp.X; x != x1; x, sx, yi = x+4, sx+1, yi+1 {
101 ci := ciBase + sx/2
102
103
104 yy1 := int32(src.Y[yi]) * 0x10101
105 cb1 := int32(src.Cb[ci]) - 128
106 cr1 := int32(src.Cr[ci]) - 128
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 r := yy1 + 91881*cr1
122 if uint32(r)&0xff000000 == 0 {
123 r >>= 16
124 } else {
125 r = ^(r >> 31)
126 }
127
128 g := yy1 - 22554*cb1 - 46802*cr1
129 if uint32(g)&0xff000000 == 0 {
130 g >>= 16
131 } else {
132 g = ^(g >> 31)
133 }
134
135 b := yy1 + 116130*cb1
136 if uint32(b)&0xff000000 == 0 {
137 b >>= 16
138 } else {
139 b = ^(b >> 31)
140 }
141
142
143 rgba := dpix[x : x+4 : len(dpix)]
144 rgba[0] = uint8(r)
145 rgba[1] = uint8(g)
146 rgba[2] = uint8(b)
147 rgba[3] = 255
148 }
149 }
150
151 case image.YCbCrSubsampleRatio420:
152 for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 {
153 dpix := dst.Pix[y*dst.Stride:]
154 yi := (sy-src.Rect.Min.Y)*src.YStride + (sp.X - src.Rect.Min.X)
155
156 ciBase := (sy/2-src.Rect.Min.Y/2)*src.CStride - src.Rect.Min.X/2
157 for x, sx := x0, sp.X; x != x1; x, sx, yi = x+4, sx+1, yi+1 {
158 ci := ciBase + sx/2
159
160
161 yy1 := int32(src.Y[yi]) * 0x10101
162 cb1 := int32(src.Cb[ci]) - 128
163 cr1 := int32(src.Cr[ci]) - 128
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 r := yy1 + 91881*cr1
179 if uint32(r)&0xff000000 == 0 {
180 r >>= 16
181 } else {
182 r = ^(r >> 31)
183 }
184
185 g := yy1 - 22554*cb1 - 46802*cr1
186 if uint32(g)&0xff000000 == 0 {
187 g >>= 16
188 } else {
189 g = ^(g >> 31)
190 }
191
192 b := yy1 + 116130*cb1
193 if uint32(b)&0xff000000 == 0 {
194 b >>= 16
195 } else {
196 b = ^(b >> 31)
197 }
198
199
200 rgba := dpix[x : x+4 : len(dpix)]
201 rgba[0] = uint8(r)
202 rgba[1] = uint8(g)
203 rgba[2] = uint8(b)
204 rgba[3] = 255
205 }
206 }
207
208 case image.YCbCrSubsampleRatio440:
209 for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 {
210 dpix := dst.Pix[y*dst.Stride:]
211 yi := (sy-src.Rect.Min.Y)*src.YStride + (sp.X - src.Rect.Min.X)
212
213 ci := (sy/2-src.Rect.Min.Y/2)*src.CStride + (sp.X - src.Rect.Min.X)
214 for x := x0; x != x1; x, yi, ci = x+4, yi+1, ci+1 {
215
216
217 yy1 := int32(src.Y[yi]) * 0x10101
218 cb1 := int32(src.Cb[ci]) - 128
219 cr1 := int32(src.Cr[ci]) - 128
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 r := yy1 + 91881*cr1
235 if uint32(r)&0xff000000 == 0 {
236 r >>= 16
237 } else {
238 r = ^(r >> 31)
239 }
240
241 g := yy1 - 22554*cb1 - 46802*cr1
242 if uint32(g)&0xff000000 == 0 {
243 g >>= 16
244 } else {
245 g = ^(g >> 31)
246 }
247
248 b := yy1 + 116130*cb1
249 if uint32(b)&0xff000000 == 0 {
250 b >>= 16
251 } else {
252 b = ^(b >> 31)
253 }
254
255
256 rgba := dpix[x : x+4 : len(dpix)]
257 rgba[0] = uint8(r)
258 rgba[1] = uint8(g)
259 rgba[2] = uint8(b)
260 rgba[3] = 255
261 }
262 }
263
264 default:
265 return false
266 }
267 return true
268 }
269
View as plain text