Debian Codegraph @

fmt/yuyv/image.go

raw
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 provides support for the YUYV format.
18 package yuyv
19
20 import (
21 "image"
22 "image/color"
23 )
24
25 // FourCC of the YUYV format.
26 const FourCC = 'Y' | 'U'<<8 | 'Y'<<16 | 'V'<<24
27
28 // An Image is an in-memory YUYV image. It implements the image.Image interface.
29 type Image struct {
30 // Pix holds the pixel data. Every four bytes corresponds to a pair of
31 // horizontally adjacent pixels. The two pixels have separate luma values
32 // and shared chroma. The sample order is Y0, Cb, Y1, Cr.
33 Pix []uint8
34
35 // Stride is the distance in bytes between vertically adjacent pixels.
36 // Line y starts at index (y-Rect.Min.Y)*Stride.
37 Stride int
38
39 // Rect is the bounds of the image.
40 Rect image.Rectangle
41 }
42
43 // New returns a new YUYV image with the given bounds.
44 func New(rect image.Rectangle) *Image {
45 l, r := rect.Min.X&^1, (rect.Max.X+1)&^1
46 w, h := r-l, rect.Dy()
47 return &Image{make([]uint8, 2*w*h), 2 * w, rect}
48 }
49
50 // ColorModel returns color.YCbCrModel.
51 func (img *Image) ColorModel() color.Model {
52 return color.YCbCrModel
53 }
54
55 // Bounds returns the bounds of the image.
56 func (img *Image) Bounds() image.Rectangle {
57 return img.Rect
58 }
59
60 // At returns the color of the pixel at (x, y).
61 func (img *Image) At(x, y int) color.Color {
62 return img.YCbCrAt(x, y)
63 }
64
65 // YCbCrAt returns the color of the pixel at (x, y).
66 func (img *Image) YCbCrAt(x, y int) color.YCbCr {
67 if !(image.Point{x, y}.In(img.Rect)) {
68 return color.YCbCr{}
69 }
70 return color.YCbCr{
71 img.Pix[img.YOffset(x, y)],
72 img.Pix[img.CbOffset(x, y)],
73 img.Pix[img.CrOffset(x, y)],
74 }
75 }
76
77 // YOffset returns the index at which the Y component of the pixel at (x, y) is
78 // located in Pix.
79 func (img *Image) YOffset(x, y int) int {
80 return img.PixPairOffset(x, y) + 2*(x&1)
81 }
82
83 // CbOffset returns the index at which the Cb component of the pixel at (x, y)
84 // is located in Pix.
85 func (img *Image) CbOffset(x, y int) int {
86 return img.PixPairOffset(x, y) + 1
87 }
88
89 // CrOffset returns the index at which the Cr component of the pixel at (x, y)
90 // is located in Pix.
91 func (img *Image) CrOffset(x, y int) int {
92 return img.PixPairOffset(x, y) + 3
93 }
94
95 // PixPairOffset returns the index of the first element in Pix that corresponds
96 // to the pixel pair to which (x, y) belongs.
97 func (img *Image) PixPairOffset(x, y int) int {
98 return (y-img.Rect.Min.Y)*img.Stride + (x&^1-img.Rect.Min.X&^1)*2
99 }
100
101 // SubImage returns an image representing the portion of img visible through
102 // rect. The returned value shares pixels with the original image.
103 func (img *Image) SubImage(rect image.Rectangle) image.Image {
104 rect = rect.Intersect(img.Rect)
105 if rect.Empty() {
106 return &Image{}
107 }
108 i := img.PixPairOffset(rect.Min.X, rect.Min.Y)
109 return &Image{img.Pix[i:], img.Stride, rect}
110 }
111
112 // Opaque returns true.
113 func (img *Image) Opaque() bool {
114 return true
115 }