1 /* fswebcam - Small and simple webcam for *nix */
2 /*============================================================*/
3 /* Copyright (C)2005-2010 Philip Heron <phil@sanslogic.co.uk> */
5 /* This program is distributed under the terms of the GNU */
6 /* General Public License, version 2. You may use, modify, */
7 /* and redistribute it under the terms of this license. A */
8 /* copy should be included with this source. */
18 /* The following YUV functions are based on code by Vincent Hourdin.
19 * http://vinvin.dyndns.org/projects/
21 * Faster integer maths from camE by Tom Gilbert.
22 * http://linuxbrit.co.uk/camE/
25 int fswc_add_image_yuyv(src_t *src, avgbmp_t *abitmap)
30 if(src->length < (src->width * src->height * 2)) return(-1);
32 /* YUYV and UYVY are very similar and so *
33 * are both handled by this one function. */
35 ptr = (uint8_t *) src->img;
38 for(y = 0; y < src->height; y++)
40 for(x = 0; x < src->width; x++)
45 if(src->palette == SRC_PAL_UYVY)
47 if(!z) y = ptr[1] << 8;
53 else /* SRC_PAL_YUYV */
55 if(!z) y = ptr[0] << 8;
62 r = (y + (359 * v)) >> 8;
63 g = (y - (88 * u) - (183 * v)) >> 8;
64 b = (y + (454 * u)) >> 8;
66 *(abitmap++) += CLIP(r, 0x00, 0xFF);
67 *(abitmap++) += CLIP(g, 0x00, 0xFF);
68 *(abitmap++) += CLIP(b, 0x00, 0xFF);
81 int fswc_add_image_yuv420p(src_t *src, avgbmp_t *abitmap)
83 uint8_t *yptr, *uptr, *vptr;
86 if(src->length < (src->width * src->height * 3) / 2) return(-1);
88 /* Setup pointers to Y, U and V buffers. */
89 yptr = (uint8_t *) src->img;
90 uptr = yptr + (src->width * src->height);
91 vptr = uptr + (src->width * src->height / 4);
95 for(y = 0; y < src->height; y++)
97 for(x = 0; x < src->width; x++)
106 r = (y + (359 * v)) >> 8;
107 g = (y - (88 * u) - (183 * v)) >> 8;
108 b = (y + (454 * u)) >> 8;
110 *(abitmap++) += CLIP(r, 0x00, 0xFF);
111 *(abitmap++) += CLIP(g, 0x00, 0xFF);
112 *(abitmap++) += CLIP(b, 0x00, 0xFF);
117 if(!(y & 1)) p -= src->width / 2;
123 int fswc_add_image_nv12mb(src_t *src, avgbmp_t *abitmap)
128 if(src->length != (src->width * src->height * 3) / 2) return(-1);
130 bw = src->width >> 4;
132 for(y = 0; y < src->height; y++)
134 for(x = 0; x < src->width; x++)
145 py += ((by * bw) + bx) * 0x100;
146 py += ((y - (by << 4)) * 0x10) + (x - (bx << 4));
150 puv = (avgbmp_t *)src->img + (src->width * src->height);
151 puv += ((by * bw) + bx) * 0x100;
152 puv += (((y / 2) - (by << 4)) * 0x10) + ((x - (bx << 4)) &~ 1);
158 cr = (cy + (359 * cv)) >> 8;
159 cg = (cy - (88 * cu) - (183 * cv)) >> 8;
160 cb = (cy + (454 * cu)) >> 8;
162 *(abitmap++) += CLIP(cr, 0x00, 0xFF);
163 *(abitmap++) += CLIP(cg, 0x00, 0xFF);
164 *(abitmap++) += CLIP(cb, 0x00, 0xFF);