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. */
15 /*#include "fswebcam.h"*/
19 int fswc_add_image_bayer(avgbmp_t *dst, uint8_t *img, uint32_t length, uint32_t w, uint32_t h, int palette)
21 uint32_t x = 0, y = 0;
24 if(length < i) return(-1);
26 /* SBGGR8 bayer pattern:
33 * SGBRG8 bayer pattern:
40 * SGRBG8 bayer pattern:
55 /* Setup pointers to this pixel's neighbours. */
65 /* Juggle pointers if they are out of bounds. */
66 if(!y) { p[0]=p[5]; p[1]=p[6]; p[2]=p[7]; }
67 else if(y == h - 1) { p[5]=p[0]; p[6]=p[1]; p[7]=p[2]; }
68 if(!x) { p[0]=p[2]; p[3]=p[4]; p[5]=p[7]; }
69 else if(x == w - 1) { p[2]=p[0]; p[4]=p[3]; p[7]=p[5]; }
71 /* Average matching neighbours. */
72 hn = (*p[3] + *p[4]) / 2;
73 vn = (*p[1] + *p[6]) / 2;
74 di = (*p[0] + *p[2] + *p[5] + *p[7]) / 4;
77 if(palette == SRC_PAL_BAYER) mode = (x + y) & 0x01;
78 else mode = ~(x + y) & 0x01;
83 if(y & 0x01) { r = hn; b = vn; }
84 else { r = vn; b = hn; }
86 else if(y & 0x01) { r = *img; g = (vn + hn) / 2; b = di; }
87 else { b = *img; g = (vn + hn) / 2; r = di; }
89 if(palette == SRC_PAL_SGRBG8)
100 /* Move to the next pixel (or line) */
101 if(++x == w) { x = 0; y++; }