Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
bayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 The Paparazzi Community
3  * 2015 Freek van Tienen <freek.v.tienen@gmail.com>
4  *
5  * This file is part of Paparazzi.
6  *
7  * Paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * Paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
27 #ifndef Bayer_H
28 #define Bayer_H
29 
30 #include "lib/vision/image.h"
31 
39 inline void BayerToYUV(struct image_t *in, struct image_t *out,
40  int RedX, int RedY)
41 {
42  uint16_t *ii = (uint16_t *) in->buf;
43  uint8_t *oi = (uint8_t *) out->buf;
44  int x, y;
45 
46  for (y = 0; y < out->h; y++) {
47  for (x = 0; x < out->w; x += 2) {
48  /* RGB Bayer:
49  * RBRBRBRBRBRBRBRB
50  * GRGRGRGRGRGRGRGR
51  */
52  int i = 2 * (out->h - y) + RedX;
53  int j = 2 * x + RedY;
54  if ((i < in->w) && (j < in->h)) {
55  uint16_t G1 = ii[i + j * in->w] / 2;
56  uint16_t R1 = ii[i + j * in->w + 1] / 2;
57  uint16_t B1 = ii[i + (j + 1) * in->w] / 2;
58  j += 2;
59  uint16_t G2 = ii[i + j * in->w] / 2;
60  uint16_t R2 = ii[i + j * in->w + 1] / 2;
61  uint16_t B2 = ii[i + (j + 1) * in->w] / 2;
62 
63  uint32_t u, my1, v, my2;
64 
65  my1 = (0.256788 * R1 + 0.504129 * G1 + 0.097906 * B1) / 128 + 16;
66  my2 = (0.256788 * R2 + 0.504129 * G2 + 0.097906 * B2) / 128 + 16;
67  u = (-0.148223 * (R1 + R2) - 0.290993 * (G1 + G2) + 0.439216 * (B1 + B2)) / 256 + 128;
68  v = (0.439216 * (R1 + R2) - 0.367788 * (G1 + G2) - 0.071427 * (B1 + B2)) / 256 + 128;
69 
70  oi[(x + y * out->w) * 2] = Clip(u, 0, 255);
71  oi[(x + y * out->w) * 2 + 1] = Clip(my1, 0, 255);
72  oi[(x + y * out->w) * 2 + 2] = Clip(v, 0, 255);
73  oi[(x + y * out->w) * 2 + 3] = Clip(my2, 0, 255);
74  } else {
75  oi[(x + y * out->w) * 2] = 0;
76  oi[(x + y * out->w) * 2 + 1] = 0;
77  oi[(x + y * out->w) * 2 + 2] = 0;
78  oi[(x + y * out->w) * 2 + 3] = 0;
79  }
80  }
81  }
82 }
83 
84 #endif /* Bayer_H */
unsigned short uint16_t
Definition: types.h:16
Definition: image.h:43
Image helper functions like resizing, color filter, converters...
void BayerToYUV(struct image_t *in, struct image_t *out, int RedX, int RedY)
Decode Bayer Pattern.
Definition: bayer.h:39
uint16_t w
Image width.
Definition: image.h:45
unsigned long uint32_t
Definition: types.h:18
uint16_t h
Image height.
Definition: image.h:46
void * buf
Image buffer (depending on the image_type)
Definition: image.h:53
unsigned char uint8_t
Definition: types.h:14