| 1 |
// Package v4l, a facade to the Video4Linux video capture interface |
| 2 |
// Copyright (C) 2016 Zoltán Korándi <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package yuyv |
| 18 |
|
| 19 |
import "image" |
| 20 |
|
| 21 |
// ToRGBA aligns r.Min in dst with p in src, and draws the part of src visible |
| 22 |
// through r over src. |
| 23 |
// |
| 24 |
// It's several times faster than the image/draw package. |
| 25 |
func ToRGBA(dst *image.RGBA, r image.Rectangle, src *Image, p image.Point) { |
| 26 |
v := p.Sub(r.Min) |
| 27 |
r = r.Intersect(dst.Rect).Intersect(src.Rect.Sub(v)) |
| 28 |
p = r.Min.Add(v) |
| 29 |
if r.Empty() { |
| 30 |
return |
| 31 |
} |
| 32 |
for y := 0; y < r.Dy(); y++ { |
| 33 |
s := src.Pix[src.PixPairOffset(p.X, p.Y+y):] |
| 34 |
d := dst.Pix[dst.PixOffset(r.Min.X, r.Min.Y+y):] |
| 35 |
n := r.Dx() |
| 36 |
if p.X&1 != 0 { |
| 37 |
cb := int32(s[1]) - 128 |
| 38 |
y2 := int32(s[2]) * 0x010101 |
| 39 |
cr := int32(s[3]) - 128 |
| 40 |
d[0] = clamp(y2 + 91881*cr) |
| 41 |
d[1] = clamp(y2 - 22554*cb - 46802*cr) |
| 42 |
d[2] = clamp(y2 + 116130*cb) |
| 43 |
d[3] = 255 |
| 44 |
s = s[4:] |
| 45 |
d = d[4:] |
| 46 |
n-- |
| 47 |
} |
| 48 |
for x := 0; x < n; x += 2 { |
| 49 |
y1 := int32(s[2*x+0]) * 0x010101 |
| 50 |
cb := int32(s[2*x+1]) - 128 |
| 51 |
y2 := int32(s[2*x+2]) * 0x010101 |
| 52 |
cr := int32(s[2*x+3]) - 128 |
| 53 |
d[4*x+0] = clamp(y1 + 91881*cr) |
| 54 |
d[4*x+1] = clamp(y1 - 22554*cb - 46802*cr) |
| 55 |
d[4*x+2] = clamp(y1 + 116130*cb) |
| 56 |
d[4*x+3] = 255 |
| 57 |
if x < n-1 { |
| 58 |
d[4*x+4] = clamp(y2 + 91881*cr) |
| 59 |
d[4*x+5] = clamp(y2 - 22554*cb - 46802*cr) |
| 60 |
d[4*x+6] = clamp(y2 + 116130*cb) |
| 61 |
d[4*x+7] = 255 |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func clamp(x int32) uint8 { |
| 68 |
if uint32(x)&0xff000000 == 0 { |
| 69 |
return uint8(x >> 16) |
| 70 |
} else { |
| 71 |
return uint8(^(x >> 31)) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// ToGray aligns r.Min in dst with p in src, and draws the part of src visible |
| 76 |
// through r over src. |
| 77 |
// |
| 78 |
// It's several times faster than the image/draw package. |
| 79 |
func ToGray(dst *image.Gray, r image.Rectangle, src *Image, p image.Point) { |
| 80 |
v := p.Sub(r.Min) |
| 81 |
r = r.Intersect(dst.Rect).Intersect(src.Rect.Sub(v)) |
| 82 |
p = r.Min.Add(v) |
| 83 |
if r.Empty() { |
| 84 |
return |
| 85 |
} |
| 86 |
for y := 0; y < r.Dy(); y++ { |
| 87 |
s := src.Pix[src.PixPairOffset(p.X, p.Y+y):] |
| 88 |
d := dst.Pix[dst.PixOffset(r.Min.X, r.Min.Y+y):] |
| 89 |
n := r.Dx() |
| 90 |
if p.X&1 != 0 { |
| 91 |
cb := int32(s[1]) - 128 |
| 92 |
y2 := int32(s[2]) * 0x010101 |
| 93 |
cr := int32(s[3]) - 128 |
| 94 |
r := clamp2(y2 + 91881*cr) |
| 95 |
g := clamp2(y2 - 22554*cb - 46802*cr) |
| 96 |
b := clamp2(y2 + 116130*cb) |
| 97 |
d[0] = uint8((19595*r + 38470*g + 7471*b + 1<<15) >> 24) |
| 98 |
s = s[4:] |
| 99 |
d = d[1:] |
| 100 |
n-- |
| 101 |
} |
| 102 |
for x := 0; x < n; x += 2 { |
| 103 |
y1 := int32(s[2*x+0]) * 0x010101 |
| 104 |
cb := int32(s[2*x+1]) - 128 |
| 105 |
y2 := int32(s[2*x+2]) * 0x010101 |
| 106 |
cr := int32(s[2*x+3]) - 128 |
| 107 |
r := clamp2(y1 + 91881*cr) |
| 108 |
g := clamp2(y1 - 22554*cb - 46802*cr) |
| 109 |
b := clamp2(y1 + 116130*cb) |
| 110 |
d[x] = uint8((19595*r + 38470*g + 7471*b + 1<<15) >> 24) |
| 111 |
if x < n-1 { |
| 112 |
r := clamp2(y2 + 91881*cr) |
| 113 |
g := clamp2(y2 - 22554*cb - 46802*cr) |
| 114 |
b := clamp2(y2 + 116130*cb) |
| 115 |
d[x+1] = uint8((19595*r + 38470*g + 7471*b + 1<<15) >> 24) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func clamp2(x int32) int32 { |
| 122 |
if uint32(x)&0xff000000 == 0 { |
| 123 |
return (x >> 8) & 0xffff |
| 124 |
} else { |
| 125 |
return ^(x >> 31) & 0xffff |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// ToYCbCr aligns r.Min in dst with p in src, and draws the part of src visible |
| 130 |
// through r over src. |
| 131 |
// |
| 132 |
// Panics if the subsample ratio of dst is not 4:4:4. |
| 133 |
func ToYCbCr(dst *image.YCbCr, r image.Rectangle, src *Image, p image.Point) { |
| 134 |
if dst.SubsampleRatio != image.YCbCrSubsampleRatio444 { |
| 135 |
panic("subsample ratio must be 4:4:4") |
| 136 |
} |
| 137 |
v := p.Sub(r.Min) |
| 138 |
r = r.Intersect(dst.Rect).Intersect(src.Rect.Sub(v)) |
| 139 |
p = r.Min.Add(v) |
| 140 |
if r.Empty() { |
| 141 |
return |
| 142 |
} |
| 143 |
for y := 0; y < r.Dy(); y++ { |
| 144 |
s := src.Pix[src.PixPairOffset(p.X, p.Y+y):] |
| 145 |
dy := dst.Y[dst.YOffset(r.Min.X, r.Min.Y+y):] |
| 146 |
dcb := dst.Cb[dst.COffset(r.Min.X, r.Min.Y+y):] |
| 147 |
dcr := dst.Cr[dst.COffset(r.Min.X, r.Min.Y+y):] |
| 148 |
n := r.Dx() |
| 149 |
if p.X&1 != 0 { |
| 150 |
dy[0], dcb[0], dcr[0] = s[2], s[1], s[3] |
| 151 |
s = s[4:] |
| 152 |
dy = dy[1:] |
| 153 |
dcb = dcb[1:] |
| 154 |
dcr = dcr[1:] |
| 155 |
n-- |
| 156 |
} |
| 157 |
for x := 0; x < n; x += 2 { |
| 158 |
dy[x], dcb[x], dcr[x] = s[2*x], s[2*x+1], s[2*x+3] |
| 159 |
if x < n-1 { |
| 160 |
dy[x+1], dcb[x+1], dcr[x+1] = s[2*x+2], s[2*x+1], s[2*x+3] |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |