Debian Codegraph @

cvd_src/noarch/yuv422.cpp

raw symbols SCIP
1 #include "cvd/colourspace.h"
2 #include "cvd/colourspaces.h"
3 #include "cvd/convert_image.h"
4 #include "cvd/image.h"
5 #include "cvd/utility.h"
6
7 #include <iostream>
8 namespace CVD
9 {
10
11 namespace
12 {
13 unsigned char saturate(int i)
14 {
15 if(i < 0)
16 return 0;
17 else if(i > 255)
18 return 255;
19 else
20 return static_cast<unsigned char>(i);
21 }
22
23 struct yuv422_ind
24 {
25 static const int y1 = 0;
26 static const int uu = 1;
27 static const int y2 = 2;
28 static const int vv = 3;
29 };
30
31 struct vuy422_ind
32 {
33 static const int y1 = 1;
34 static const int uu = 0;
35 static const int vv = 2;
36 static const int y2 = 3;
37 };
38
39 template <class C, class Ind>
40 void convert_422(const BasicImage<C>& from, BasicImage<Rgb<byte>>& to)
41 {
42 int yy, uu, vv, ug_plus_vg, ub, vr;
43 int r, g, b;
44
45 size_t bytes_per_row = from.size().x * 2;
46
47 for(int y = 0; y < from.size().y; y++)
48 {
49 const unsigned char* yuv = reinterpret_cast<const unsigned char*>(from.data()) + bytes_per_row * y;
50
51 for(int x = 0; x < from.size().x; x += 2, yuv += 4)
52 {
53 uu = yuv[Ind::uu] - 128;
54 vv = yuv[Ind::vv] - 128;
55 ug_plus_vg = uu * 88 + vv * 183;
56 ub = uu * 454;
57 vr = vv * 359;
58
59 yy = yuv[Ind::y1] << 8;
60 r = (yy + vr) >> 8;
61 g = (yy - ug_plus_vg) >> 8;
62 b = (yy + ub) >> 8;
63 to[y][x + 0].red = saturate(r);
64 to[y][x + 0].green = saturate(g);
65 to[y][x + 0].blue = saturate(b);
66
67 yy = yuv[Ind::y2] << 8;
68 r = (yy + vr) >> 8;
69 g = (yy - ug_plus_vg) >> 8;
70 b = (yy + ub) >> 8;
71 to[y][x + 1].red = saturate(r);
72 to[y][x + 1].green = saturate(g);
73 to[y][x + 1].blue = saturate(b);
74 }
75 }
76 }
77
78 template <class C, class Ind>
79 void convert_422_grey(const BasicImage<C>& from, BasicImage<byte>& to)
80 {
81 //yuv422 / vuy422 is along the lines of yuyv
82 //which is 4 bytes for 2 pixels, i.e. 2 bytes per pixel
83
84 size_t bytes_per_row = from.size().x * 2;
85
86 for(int y = 0; y < from.size().y; y++)
87 {
88 const unsigned char* yuv = reinterpret_cast<const unsigned char*>(from.data()) + bytes_per_row * y;
89 for(int x = 0; x < from.size().x; x += 2, yuv += 4)
90 {
91 to[y][x + 0] = yuv[Ind::y1];
92 to[y][x + 1] = yuv[Ind::y2];
93 }
94 }
95 }
96 }
97
98 template <>
99 void convert_image(const BasicImage<yuv422>& from, BasicImage<Rgb<byte>>& to)
100 {
101 convert_422<yuv422, yuv422_ind>(from, to);
102 }
103
104 template <>
105 void convert_image(const BasicImage<yuv422>& from, BasicImage<byte>& to)
106 {
107 convert_422_grey<yuv422, yuv422_ind>(from, to);
108 }
109
110 template <>
111 void convert_image(const BasicImage<vuy422>& from, BasicImage<Rgb<byte>>& to)
112 {
113 convert_422<vuy422, vuy422_ind>(from, to);
114 }
115
116 template <>
117 void convert_image(const BasicImage<vuy422>& from, BasicImage<byte>& to)
118 {
119 convert_422_grey<vuy422, vuy422_ind>(from, to);
120 }
121 }