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
|
/*
# RGB / YUV flip/rotate routines
# (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libv4lconvert-priv.h"
static void v4lconvert_rotate180_rgbbgr24(const unsigned char *src,
unsigned char *dst, int width, int height)
{
int i;
src += 3 * width * height - 3;
for (i = 0; i < width * height; i++) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst += 3;
src -= 3;
}
}
static void v4lconvert_rotate180_yuv420(const unsigned char *src,
unsigned char *dst, int width, int height)
{
int i;
/* First flip x and y of the Y plane */
src += width * height - 1;
for (i = 0; i < width * height; i++)
*dst++ = *src--;
/* Now flip the U plane */
src += width * height * 5 / 4;
for (i = 0; i < width * height / 4; i++)
*dst++ = *src--;
/* Last flip the V plane */
src += width * height / 2;
for (i = 0; i < width * height / 4; i++)
*dst++ = *src--;
}
static void v4lconvert_rotate90_rgbbgr24(const unsigned char *src,
unsigned char *dst, int destwidth, int destheight)
{
int x, y;
#define srcwidth destheight
#define srcheight destwidth
for (y = 0; y < destheight; y++)
for (x = 0; x < destwidth; x++) {
int offset = ((srcheight - x - 1) * srcwidth + y) * 3;
*dst++ = src[offset++];
*dst++ = src[offset++];
*dst++ = src[offset];
}
}
static void v4lconvert_rotate90_yuv420(const unsigned char *src,
unsigned char *dst, int destwidth, int destheight)
{
int x, y;
/* Y-plane */
for (y = 0; y < destheight; y++)
for (x = 0; x < destwidth; x++) {
int offset = (srcheight - x - 1) * srcwidth + y;
*dst++ = src[offset];
}
/* U-plane */
src += srcwidth * srcheight;
destwidth /= 2;
destheight /= 2;
for (y = 0; y < destheight; y++)
for (x = 0; x < destwidth; x++) {
int offset = (srcheight - x - 1) * srcwidth + y;
*dst++ = src[offset];
}
/* V-plane */
src += srcwidth * srcheight;
for (y = 0; y < destheight; y++)
for (x = 0; x < destwidth; x++) {
int offset = (srcheight - x - 1) * srcwidth + y;
*dst++ = src[offset];
}
}
void v4lconvert_rotate(unsigned char *src, unsigned char *dest,
int width, int height, unsigned int pix_fmt, int rotate)
{
switch (rotate) {
case 0:
break;
case 90:
switch (pix_fmt) {
case V4L2_PIX_FMT_RGB24:
case V4L2_PIX_FMT_BGR24:
v4lconvert_rotate90_rgbbgr24(src, dest, width, height);
break;
case V4L2_PIX_FMT_YUV420:
v4lconvert_rotate90_yuv420(src, dest, width, height);
break;
}
break;
case 180:
switch (pix_fmt) {
case V4L2_PIX_FMT_RGB24:
case V4L2_PIX_FMT_BGR24:
v4lconvert_rotate180_rgbbgr24(src, dest, width, height);
break;
case V4L2_PIX_FMT_YUV420:
v4lconvert_rotate180_yuv420(src, dest, width, height);
break;
}
break;
default:
printf("FIXME add %d degrees rotation\n", rotate);
}
}
|