]> git.sesse.net Git - kdenlive/blob - src/v4l/dec_bayer.c
Fix compile warning
[kdenlive] / src / v4l / dec_bayer.c
1 /* fswebcam - Small and simple webcam for *nix                */
2 /*============================================================*/
3 /* Copyright (C)2005-2010 Philip Heron <phil@sanslogic.co.uk> */
4 /*                                                            */
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.                  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdint.h>
15 /*#include "fswebcam.h"*/
16 #include "src.h"
17
18
19 int fswc_add_image_bayer(avgbmp_t *dst, uint8_t *img, uint32_t length, uint32_t w, uint32_t h, int palette)
20 {
21         uint32_t x = 0, y = 0;
22         uint32_t i = w * h;
23         
24         if(length < i) return(-1);
25         
26         /* SBGGR8 bayer pattern:
27          * 
28          * BGBGBGBGBG
29          * GRGRGRGRGR
30          * BGBGBGBGBG
31          * GRGRGRGRGR
32          * 
33          * SGBRG8 bayer pattern:
34          * 
35          * GBGBGBGBGB
36          * RGRGRGRGRG
37          * GBGBGBGBGB
38          * RGRGRGRGRG
39          *
40          * SGRBG8 bayer pattern:
41          *
42          * GRGRGRGRGR
43          * BGBGBGBGBG
44          * GRGRGRGRGR
45          * BGBGBGBGBG
46         */
47         
48         while(i-- > 0)
49         {
50                 uint8_t *p[8];
51                 uint8_t hn, vn, di;
52                 uint8_t r, g, b;
53                 int mode;
54                 
55                 /* Setup pointers to this pixel's neighbours. */
56                 p[0] = img - w - 1;
57                 p[1] = img - w;
58                 p[2] = img - w + 1;
59                 p[3] = img - 1;
60                 p[4] = img + 1;
61                 p[5] = img + w - 1;
62                 p[6] = img + w;
63                 p[7] = img + w + 1;
64                 
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]; }
70                 
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;
75                 
76                 /* Calculate RGB */
77                 if(palette == SRC_PAL_BAYER) mode = (x + y) & 0x01;
78                 else mode = ~(x + y) & 0x01;
79                 
80                 if(mode)
81                 {
82                         g = *img;
83                         if(y & 0x01) { r = hn; b = vn; }
84                         else         { r = vn; b = hn; }
85                 }
86                 else if(y & 0x01) { r = *img; g = (vn + hn) / 2; b = di; }
87                 else              { b = *img; g = (vn + hn) / 2; r = di; }
88                 
89                 if(palette == SRC_PAL_SGRBG8)
90                 {
91                         uint8_t t = r;
92                         r = b;
93                         b = t;
94                 }
95                 
96                 *(dst++) += r;
97                 *(dst++) += g;
98                 *(dst++) += b;
99                 
100                 /* Move to the next pixel (or line) */
101                 if(++x == w) { x = 0; y++; }
102                 img++;
103         }
104         
105         return(0);
106 }
107