18
18
# along with this program; if not, write to the Free Software
19
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
23
#include <string.h>
24
24
#include "libv4lconvert-priv.h"
26
26
static void v4lconvert_vflip_rgbbgr24(unsigned char *src, unsigned char *dest,
27
struct v4l2_format *fmt)
27
struct v4l2_format *fmt)
31
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline;
32
for (y = 0; y < fmt->fmt.pix.height; y++) {
33
src -= fmt->fmt.pix.bytesperline;
34
memcpy(dest, src, fmt->fmt.pix.width * 3);
35
dest += fmt->fmt.pix.width * 3;
31
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline;
32
for (y = 0; y < fmt->fmt.pix.height; y++) {
33
src -= fmt->fmt.pix.bytesperline;
34
memcpy(dest, src, fmt->fmt.pix.width * 3);
35
dest += fmt->fmt.pix.width * 3;
39
39
static void v4lconvert_vflip_yuv420(unsigned char *src, unsigned char *dest,
40
struct v4l2_format *fmt)
40
struct v4l2_format *fmt)
44
/* First flip the Y plane */
45
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline;
46
for (y = 0; y < fmt->fmt.pix.height; y++) {
47
src -= fmt->fmt.pix.bytesperline;
48
memcpy(dest, src, fmt->fmt.pix.width);
49
dest += fmt->fmt.pix.width;
52
/* Now flip the U plane */
53
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline * 5 / 4;
54
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
55
src -= fmt->fmt.pix.bytesperline / 2;
56
memcpy(dest, src, fmt->fmt.pix.width / 2);
57
dest += fmt->fmt.pix.width / 2;
60
/* Last flip the V plane */
61
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline / 2;
62
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
63
src -= fmt->fmt.pix.bytesperline / 2;
64
memcpy(dest, src, fmt->fmt.pix.width / 2);
65
dest += fmt->fmt.pix.width / 2;
44
/* First flip the Y plane */
45
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline;
46
for (y = 0; y < fmt->fmt.pix.height; y++) {
47
src -= fmt->fmt.pix.bytesperline;
48
memcpy(dest, src, fmt->fmt.pix.width);
49
dest += fmt->fmt.pix.width;
52
/* Now flip the U plane */
53
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline * 5 / 4;
54
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
55
src -= fmt->fmt.pix.bytesperline / 2;
56
memcpy(dest, src, fmt->fmt.pix.width / 2);
57
dest += fmt->fmt.pix.width / 2;
60
/* Last flip the V plane */
61
src += fmt->fmt.pix.height * fmt->fmt.pix.bytesperline / 2;
62
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
63
src -= fmt->fmt.pix.bytesperline / 2;
64
memcpy(dest, src, fmt->fmt.pix.width / 2);
65
dest += fmt->fmt.pix.width / 2;
69
69
static void v4lconvert_hflip_rgbbgr24(unsigned char *src, unsigned char *dest,
70
struct v4l2_format *fmt)
70
struct v4l2_format *fmt)
74
for (y = 0; y < fmt->fmt.pix.height; y++) {
75
src += fmt->fmt.pix.width * 3;
76
for (x = 0; x < fmt->fmt.pix.width; x++) {
83
src += fmt->fmt.pix.bytesperline;
74
for (y = 0; y < fmt->fmt.pix.height; y++) {
75
src += fmt->fmt.pix.width * 3;
76
for (x = 0; x < fmt->fmt.pix.width; x++) {
83
src += fmt->fmt.pix.bytesperline;
87
87
static void v4lconvert_hflip_yuv420(unsigned char *src, unsigned char *dest,
88
struct v4l2_format *fmt)
88
struct v4l2_format *fmt)
92
/* First flip the Y plane */
93
for (y = 0; y < fmt->fmt.pix.height; y++) {
94
src += fmt->fmt.pix.width;
95
for (x = 0; x < fmt->fmt.pix.width; x++)
97
src += fmt->fmt.pix.bytesperline;
100
/* Now flip the U plane */
101
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
102
src += fmt->fmt.pix.width / 2;
103
for (x = 0; x < fmt->fmt.pix.width / 2; x++)
105
src += fmt->fmt.pix.bytesperline / 2;
108
/* Last flip the V plane */
109
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
110
src += fmt->fmt.pix.width / 2;
111
for (x = 0; x < fmt->fmt.pix.width / 2; x++)
113
src += fmt->fmt.pix.bytesperline / 2;
92
/* First flip the Y plane */
93
for (y = 0; y < fmt->fmt.pix.height; y++) {
94
src += fmt->fmt.pix.width;
95
for (x = 0; x < fmt->fmt.pix.width; x++)
97
src += fmt->fmt.pix.bytesperline;
100
/* Now flip the U plane */
101
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
102
src += fmt->fmt.pix.width / 2;
103
for (x = 0; x < fmt->fmt.pix.width / 2; x++)
105
src += fmt->fmt.pix.bytesperline / 2;
108
/* Last flip the V plane */
109
for (y = 0; y < fmt->fmt.pix.height / 2; y++) {
110
src += fmt->fmt.pix.width / 2;
111
for (x = 0; x < fmt->fmt.pix.width / 2; x++)
113
src += fmt->fmt.pix.bytesperline / 2;
117
117
static void v4lconvert_rotate180_rgbbgr24(const unsigned char *src,
118
unsigned char *dst, int width, int height)
118
unsigned char *dst, int width, int height)
122
src += 3 * width * height - 3;
124
for (i = 0; i < width * height; i++) {
122
src += 3 * width * height - 3;
124
for (i = 0; i < width * height; i++) {
133
133
static void v4lconvert_rotate180_yuv420(const unsigned char *src,
134
unsigned char *dst, int width, int height)
134
unsigned char *dst, int width, int height)
138
/* First flip x and y of the Y plane */
139
src += width * height - 1;
140
for (i = 0; i < width * height; i++)
143
/* Now flip the U plane */
144
src += width * height * 5 / 4;
145
for (i = 0; i < width * height / 4; i++)
148
/* Last flip the V plane */
149
src += width * height / 2;
150
for (i = 0; i < width * height / 4; i++)
138
/* First flip x and y of the Y plane */
139
src += width * height - 1;
140
for (i = 0; i < width * height; i++)
143
/* Now flip the U plane */
144
src += width * height * 5 / 4;
145
for (i = 0; i < width * height / 4; i++)
148
/* Last flip the V plane */
149
src += width * height / 2;
150
for (i = 0; i < width * height / 4; i++)
154
154
static void v4lconvert_rotate90_rgbbgr24(const unsigned char *src,
155
unsigned char *dst, int destwidth, int destheight)
155
unsigned char *dst, int destwidth, int destheight)
158
158
#define srcwidth destheight
159
159
#define srcheight destwidth
161
for (y = 0; y < destheight; y++)
162
for (x = 0; x < destwidth; x++) {
163
int offset = ((srcheight - x - 1) * srcwidth + y) * 3;
164
*dst++ = src[offset++];
165
*dst++ = src[offset++];
166
*dst++ = src[offset];
161
for (y = 0; y < destheight; y++)
162
for (x = 0; x < destwidth; x++) {
163
int offset = ((srcheight - x - 1) * srcwidth + y) * 3;
164
*dst++ = src[offset++];
165
*dst++ = src[offset++];
166
*dst++ = src[offset];
170
170
static void v4lconvert_rotate90_yuv420(const unsigned char *src,
171
unsigned char *dst, int destwidth, int destheight)
171
unsigned char *dst, int destwidth, int destheight)
176
for (y = 0; y < destheight; y++)
177
for (x = 0; x < destwidth; x++) {
178
int offset = (srcheight - x - 1) * srcwidth + y;
179
*dst++ = src[offset];
183
src += srcwidth * srcheight;
186
for (y = 0; y < destheight; y++)
187
for (x = 0; x < destwidth; x++) {
188
int offset = (srcheight - x - 1) * srcwidth + y;
189
*dst++ = src[offset];
193
src += srcwidth * srcheight;
194
for (y = 0; y < destheight; y++)
195
for (x = 0; x < destwidth; x++) {
196
int offset = (srcheight - x - 1) * srcwidth + y;
197
*dst++ = src[offset];
176
for (y = 0; y < destheight; y++)
177
for (x = 0; x < destwidth; x++) {
178
int offset = (srcheight - x - 1) * srcwidth + y;
179
*dst++ = src[offset];
183
src += srcwidth * srcheight;
186
for (y = 0; y < destheight; y++)
187
for (x = 0; x < destwidth; x++) {
188
int offset = (srcheight - x - 1) * srcwidth + y;
189
*dst++ = src[offset];
193
src += srcwidth * srcheight;
194
for (y = 0; y < destheight; y++)
195
for (x = 0; x < destwidth; x++) {
196
int offset = (srcheight - x - 1) * srcwidth + y;
197
*dst++ = src[offset];
201
201
void v4lconvert_rotate90(unsigned char *src, unsigned char *dest,
202
struct v4l2_format *fmt)
202
struct v4l2_format *fmt)
206
tmp = fmt->fmt.pix.width;
207
fmt->fmt.pix.width = fmt->fmt.pix.height;
208
fmt->fmt.pix.height = tmp;
210
switch (fmt->fmt.pix.pixelformat) {
211
case V4L2_PIX_FMT_RGB24:
212
case V4L2_PIX_FMT_BGR24:
213
v4lconvert_rotate90_rgbbgr24(src, dest, fmt->fmt.pix.width,
214
fmt->fmt.pix.height);
216
case V4L2_PIX_FMT_YUV420:
217
case V4L2_PIX_FMT_YVU420:
218
v4lconvert_rotate90_yuv420(src, dest, fmt->fmt.pix.width,
219
fmt->fmt.pix.height);
222
v4lconvert_fixup_fmt(fmt);
206
tmp = fmt->fmt.pix.width;
207
fmt->fmt.pix.width = fmt->fmt.pix.height;
208
fmt->fmt.pix.height = tmp;
210
switch (fmt->fmt.pix.pixelformat) {
211
case V4L2_PIX_FMT_RGB24:
212
case V4L2_PIX_FMT_BGR24:
213
v4lconvert_rotate90_rgbbgr24(src, dest, fmt->fmt.pix.width,
214
fmt->fmt.pix.height);
216
case V4L2_PIX_FMT_YUV420:
217
case V4L2_PIX_FMT_YVU420:
218
v4lconvert_rotate90_yuv420(src, dest, fmt->fmt.pix.width,
219
fmt->fmt.pix.height);
222
v4lconvert_fixup_fmt(fmt);
225
225
void v4lconvert_flip(unsigned char *src, unsigned char *dest,
226
struct v4l2_format *fmt, int hflip, int vflip)
226
struct v4l2_format *fmt, int hflip, int vflip)
228
if (vflip && hflip) {
229
switch (fmt->fmt.pix.pixelformat) {
230
case V4L2_PIX_FMT_RGB24:
231
case V4L2_PIX_FMT_BGR24:
232
v4lconvert_rotate180_rgbbgr24(src, dest, fmt->fmt.pix.width,
233
fmt->fmt.pix.height);
235
case V4L2_PIX_FMT_YUV420:
236
case V4L2_PIX_FMT_YVU420:
237
v4lconvert_rotate180_yuv420(src, dest, fmt->fmt.pix.width,
238
fmt->fmt.pix.height);
242
switch (fmt->fmt.pix.pixelformat) {
243
case V4L2_PIX_FMT_RGB24:
244
case V4L2_PIX_FMT_BGR24:
245
v4lconvert_hflip_rgbbgr24(src, dest, fmt);
247
case V4L2_PIX_FMT_YUV420:
248
case V4L2_PIX_FMT_YVU420:
249
v4lconvert_hflip_yuv420(src, dest, fmt);
253
switch (fmt->fmt.pix.pixelformat) {
254
case V4L2_PIX_FMT_RGB24:
255
case V4L2_PIX_FMT_BGR24:
256
v4lconvert_vflip_rgbbgr24(src, dest, fmt);
258
case V4L2_PIX_FMT_YUV420:
259
case V4L2_PIX_FMT_YVU420:
260
v4lconvert_vflip_yuv420(src, dest, fmt);
228
if (vflip && hflip) {
229
switch (fmt->fmt.pix.pixelformat) {
230
case V4L2_PIX_FMT_RGB24:
231
case V4L2_PIX_FMT_BGR24:
232
v4lconvert_rotate180_rgbbgr24(src, dest, fmt->fmt.pix.width,
233
fmt->fmt.pix.height);
235
case V4L2_PIX_FMT_YUV420:
236
case V4L2_PIX_FMT_YVU420:
237
v4lconvert_rotate180_yuv420(src, dest, fmt->fmt.pix.width,
238
fmt->fmt.pix.height);
242
switch (fmt->fmt.pix.pixelformat) {
243
case V4L2_PIX_FMT_RGB24:
244
case V4L2_PIX_FMT_BGR24:
245
v4lconvert_hflip_rgbbgr24(src, dest, fmt);
247
case V4L2_PIX_FMT_YUV420:
248
case V4L2_PIX_FMT_YVU420:
249
v4lconvert_hflip_yuv420(src, dest, fmt);
253
switch (fmt->fmt.pix.pixelformat) {
254
case V4L2_PIX_FMT_RGB24:
255
case V4L2_PIX_FMT_BGR24:
256
v4lconvert_vflip_rgbbgr24(src, dest, fmt);
258
case V4L2_PIX_FMT_YUV420:
259
case V4L2_PIX_FMT_YVU420:
260
v4lconvert_vflip_yuv420(src, dest, fmt);
265
/* Our newly written data has no padding */
266
v4lconvert_fixup_fmt(fmt);
265
/* Our newly written data has no padding */
266
v4lconvert_fixup_fmt(fmt);