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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
# RGB and YUV crop routines
# (C) 2008 Hans de Goede <hdegoede@redhat.com>
# 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 <string.h>
#include "libv4lconvert-priv.h"
static void v4lconvert_reduceandcrop_rgbbgr24(
unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int x, y;
int startx = src_fmt->fmt.pix.width / 2 - dest_fmt->fmt.pix.width;
int starty = src_fmt->fmt.pix.height / 2 - dest_fmt->fmt.pix.height;
src += starty * src_fmt->fmt.pix.bytesperline + 3 * startx;
for (y = 0; y < dest_fmt->fmt.pix.height; y++) {
unsigned char *mysrc = src;
for (x = 0; x < dest_fmt->fmt.pix.width; x++) {
*(dest++) = *(mysrc++);
*(dest++) = *(mysrc++);
*(dest++) = *(mysrc++);
mysrc += 3; /* skip one pixel */
}
src += 2 * src_fmt->fmt.pix.bytesperline; /* skip one line */
}
}
static void v4lconvert_crop_rgbbgr24(unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int x;
int startx = (src_fmt->fmt.pix.width - dest_fmt->fmt.pix.width) / 2;
int starty = (src_fmt->fmt.pix.height - dest_fmt->fmt.pix.height) / 2;
src += starty * src_fmt->fmt.pix.bytesperline + 3 * startx;
for (x = 0; x < dest_fmt->fmt.pix.height; x++) {
memcpy(dest, src, dest_fmt->fmt.pix.width * 3);
src += src_fmt->fmt.pix.bytesperline;
dest += dest_fmt->fmt.pix.bytesperline;
}
}
static void v4lconvert_reduceandcrop_yuv420(
unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int x,y;
int dest_height_half = dest_fmt->fmt.pix.height / 2;
int dest_width_half = dest_fmt->fmt.pix.width / 2;
int startx = (src_fmt->fmt.pix.width / 2 - dest_fmt->fmt.pix.width) & ~1;
int starty = (src_fmt->fmt.pix.height / 2 - dest_fmt->fmt.pix.height) & ~1;
unsigned char *mysrc, *mysrc2;
/* Y */
mysrc = src + starty * src_fmt->fmt.pix.bytesperline + startx;
for (y = 0; y < dest_fmt->fmt.pix.height; y++){
mysrc2 = mysrc;
for (x = 0; x < dest_fmt->fmt.pix.width; x++){
*(dest++) = *mysrc2;
mysrc2 += 2; /* skip one pixel */
}
mysrc += 2 * src_fmt->fmt.pix.bytesperline; /* skip one line */
}
/* U */
mysrc = src + src_fmt->fmt.pix.height * src_fmt->fmt.pix.bytesperline +
(starty / 2) * src_fmt->fmt.pix.bytesperline / 2 + startx / 2;
for (y = 0; y < dest_height_half; y++){
mysrc2 = mysrc;
for (x = 0; x < dest_width_half; x++){
*(dest++) = *mysrc2;
mysrc2 += 2; /* skip one pixel */
}
mysrc += src_fmt->fmt.pix.bytesperline ; /* skip one line */
}
/* V */
mysrc = src + src_fmt->fmt.pix.height * src_fmt->fmt.pix.bytesperline * 5 / 4
+ (starty / 2) * src_fmt->fmt.pix.bytesperline / 2 + startx / 2;
for (y = 0; y < dest_height_half; y++){
mysrc2 = mysrc;
for (x = 0; x < dest_width_half; x++){
*(dest++) = *mysrc2;
mysrc2 += 2; /* skip one pixel */
}
mysrc += src_fmt->fmt.pix.bytesperline ; /* skip one line */
}
}
static void v4lconvert_crop_yuv420(unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int x;
int startx = ((src_fmt->fmt.pix.width - dest_fmt->fmt.pix.width) / 2) & ~1;
int starty = ((src_fmt->fmt.pix.height - dest_fmt->fmt.pix.height) / 2) & ~1;
unsigned char *mysrc = src + starty * src_fmt->fmt.pix.bytesperline + startx;
/* Y */
for (x = 0; x < dest_fmt->fmt.pix.height; x++) {
memcpy(dest, mysrc, dest_fmt->fmt.pix.width);
mysrc += src_fmt->fmt.pix.bytesperline;
dest += dest_fmt->fmt.pix.bytesperline;
}
/* U */
mysrc = src + src_fmt->fmt.pix.height * src_fmt->fmt.pix.bytesperline +
(starty / 2) * src_fmt->fmt.pix.bytesperline / 2 + startx / 2;
for (x = 0; x < dest_fmt->fmt.pix.height / 2; x++) {
memcpy(dest, mysrc, dest_fmt->fmt.pix.width / 2);
mysrc += src_fmt->fmt.pix.bytesperline / 2;
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
/* V */
mysrc = src + src_fmt->fmt.pix.height * src_fmt->fmt.pix.bytesperline * 5 / 4
+ (starty / 2) * src_fmt->fmt.pix.bytesperline / 2 + startx / 2;
for (x = 0; x < dest_fmt->fmt.pix.height / 2; x++) {
memcpy(dest, mysrc, dest_fmt->fmt.pix.width / 2);
mysrc += src_fmt->fmt.pix.bytesperline / 2;
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
}
/* Ok, so this is not really cropping, but more the reverse, whatever */
static void v4lconvert_add_border_rgbbgr24(
unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int y;
int borderx = (dest_fmt->fmt.pix.width - src_fmt->fmt.pix.width) / 2;
int bordery = (dest_fmt->fmt.pix.height - src_fmt->fmt.pix.height) / 2;
for (y = 0; y < bordery; y++) {
memset(dest, 0, dest_fmt->fmt.pix.width * 3);
dest += dest_fmt->fmt.pix.bytesperline;
}
for (y = 0; y < src_fmt->fmt.pix.height; y++) {
memset(dest, 0, borderx * 3);
dest += borderx * 3;
memcpy(dest, src, src_fmt->fmt.pix.width * 3);
src += src_fmt->fmt.pix.bytesperline;
dest += src_fmt->fmt.pix.width * 3;
memset(dest, 0, borderx * 3);
dest += dest_fmt->fmt.pix.bytesperline -
(borderx + src_fmt->fmt.pix.width) * 3;
}
for (y = 0; y < bordery; y++) {
memset(dest, 0, dest_fmt->fmt.pix.width * 3);
dest += dest_fmt->fmt.pix.bytesperline;
}
}
static void v4lconvert_add_border_yuv420(
unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
int y;
int borderx = ((dest_fmt->fmt.pix.width - src_fmt->fmt.pix.width) / 2) & ~1;
int bordery = ((dest_fmt->fmt.pix.height - src_fmt->fmt.pix.height) / 2) & ~1;
/* Y */
for (y = 0; y < bordery; y++) {
memset(dest, 16, dest_fmt->fmt.pix.width);
dest += dest_fmt->fmt.pix.bytesperline;
}
for (y = 0; y < src_fmt->fmt.pix.height; y++) {
memset(dest, 16, borderx);
dest += borderx;
memcpy(dest, src, src_fmt->fmt.pix.width);
src += src_fmt->fmt.pix.bytesperline;
dest += src_fmt->fmt.pix.width;
memset(dest, 16, borderx);
dest += dest_fmt->fmt.pix.bytesperline -
(borderx + src_fmt->fmt.pix.width);
}
for (y = 0; y < bordery; y++) {
memset(dest, 16, dest_fmt->fmt.pix.width);
dest += dest_fmt->fmt.pix.bytesperline;
}
/* U */
for (y = 0; y < bordery / 2; y++) {
memset(dest, 128, dest_fmt->fmt.pix.width / 2);
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
for (y = 0; y < src_fmt->fmt.pix.height / 2; y++) {
memset(dest, 128, borderx / 2);
dest += borderx / 2;
memcpy(dest, src, src_fmt->fmt.pix.width / 2);
src += src_fmt->fmt.pix.bytesperline / 2;
dest += src_fmt->fmt.pix.width / 2;
memset(dest, 128, borderx / 2);
dest += (dest_fmt->fmt.pix.bytesperline -
(borderx + src_fmt->fmt.pix.width)) / 2;
}
for (y = 0; y < bordery / 2; y++) {
memset(dest, 128, dest_fmt->fmt.pix.width / 2);
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
/* V */
for (y = 0; y < bordery / 2; y++) {
memset(dest, 128, dest_fmt->fmt.pix.width / 2);
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
for (y = 0; y < src_fmt->fmt.pix.height / 2; y++) {
memset(dest, 128, borderx / 2);
dest += borderx / 2;
memcpy(dest, src, src_fmt->fmt.pix.width / 2);
src += src_fmt->fmt.pix.bytesperline / 2;
dest += src_fmt->fmt.pix.width / 2;
memset(dest, 128, borderx / 2);
dest += (dest_fmt->fmt.pix.bytesperline -
(borderx + src_fmt->fmt.pix.width)) / 2;
}
for (y = 0; y < bordery / 2; y++) {
memset(dest, 128, dest_fmt->fmt.pix.width / 2);
dest += dest_fmt->fmt.pix.bytesperline / 2;
}
}
void v4lconvert_crop(unsigned char *src, unsigned char *dest,
const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt)
{
switch (dest_fmt->fmt.pix.pixelformat) {
case V4L2_PIX_FMT_RGB24:
case V4L2_PIX_FMT_BGR24:
if (src_fmt->fmt.pix.width <= dest_fmt->fmt.pix.width &&
src_fmt->fmt.pix.height <= dest_fmt->fmt.pix.height)
v4lconvert_add_border_rgbbgr24(src, dest, src_fmt, dest_fmt);
else if (src_fmt->fmt.pix.width >= 2 * dest_fmt->fmt.pix.width &&
src_fmt->fmt.pix.height >= 2 * dest_fmt->fmt.pix.height)
v4lconvert_reduceandcrop_rgbbgr24(src, dest, src_fmt, dest_fmt);
else
v4lconvert_crop_rgbbgr24(src, dest, src_fmt, dest_fmt);
break;
case V4L2_PIX_FMT_YUV420:
case V4L2_PIX_FMT_YVU420:
if (src_fmt->fmt.pix.width <= dest_fmt->fmt.pix.width &&
src_fmt->fmt.pix.height <= dest_fmt->fmt.pix.height)
v4lconvert_add_border_yuv420(src, dest, src_fmt, dest_fmt);
else if (src_fmt->fmt.pix.width >= 2 * dest_fmt->fmt.pix.width &&
src_fmt->fmt.pix.height >= 2 * dest_fmt->fmt.pix.height)
v4lconvert_reduceandcrop_yuv420(src, dest, src_fmt, dest_fmt);
else
v4lconvert_crop_yuv420(src, dest, src_fmt, dest_fmt);
break;
}
}
|