Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 
32 static inline uint8_t clip(uint16_t x)
33 {
34  if (x < 0) {
35  return 0;
36  }
37  if (x > 255) {
38  return 255;
39  }
40  return (uint8_t) x;
41 }
42 
43 
44 void BayerToYUV(struct image_t *Input, struct image_t *out,
45  int RedX, int RedY);
46 
47 
55 void BayerToYUV(struct image_t *in, struct image_t *out,
56  int RedX, int RedY)
57 {
58  uint16_t *ii = (uint16_t *) in->buf;
59  uint8_t *oi = (uint8_t *) out->buf;
60  int x, y;
61 
62 
63  for (y = 0; y < out->h; y++) {
64  for (x = 0; x < out->w; x += 2) {
65  /* RGB Bayer:
66  * RBRBRBRBRBRBRBRB
67  * GRGRGRGRGRGRGRGR
68  */
69  int i = 2 * (out->h - y) + RedX;
70  int j = 2 * x + RedY;
71  if ((i < in->w) && (j < in->h)) {
72  uint16_t G1 = ii[i + j * in->w] / 2;
73  uint16_t R1 = ii[i + j * in->w + 1] / 2;
74  uint16_t B1 = ii[i + (j + 1) * in->w] / 2;
75  j += 2;
76  uint16_t G2 = ii[i + j * in->w] / 2;
77  uint16_t R2 = ii[i + j * in->w + 1] / 2;
78  uint16_t B2 = ii[i + (j + 1) * in->w] / 2;
79 
80  uint32_t u, my1, v, my2;
81 
82  my1 = (0.256788 * R1 + 0.504129 * G1 + 0.097906 * B1) / 128 + 16;
83  my2 = (0.256788 * R2 + 0.504129 * G2 + 0.097906 * B2) / 128 + 16;
84  u = (-0.148223 * (R1 + R2) - 0.290993 * (G1 + G2) + 0.439216 * (B1 + B2)) / 256 + 128;
85  v = (0.439216 * (R1 + R2) - 0.367788 * (G1 + G2) - 0.071427 * (B1 + B2)) / 256 + 128;
86 
87 
88  oi[(x + y * out->w) * 2] = clip(u);
89  oi[(x + y * out->w) * 2 + 1] = clip(my1);
90  oi[(x + y * out->w) * 2 + 2] = clip(v);
91  oi[(x + y * out->w) * 2 + 3] = clip(my2);
92  } else {
93  oi[(x + y * out->w) * 2] = 0;
94  oi[(x + y * out->w) * 2 + 1] = 0;
95  oi[(x + y * out->w) * 2 + 2] = 0;
96  oi[(x + y * out->w) * 2 + 3] = 0;
97  }
98  }
99  }
100 }
101 
102 #endif /* Bayer_H */
unsigned short uint16_t
Definition: types.h:16
Definition: image.h:42
static uint8_t clip(uint16_t x)
Definition: bayer.h:32
Image helper functions like resizing, color filter, converters...
void BayerToYUV(struct image_t *Input, struct image_t *out, int RedX, int RedY)
Decode Bayer Pattern.
Definition: bayer.h:55
uint16_t w
Image width.
Definition: image.h:44
unsigned long uint32_t
Definition: types.h:18
uint16_t h
Image height.
Definition: image.h:45
void * buf
Image buffer (depending on the image_type)
Definition: image.h:51
unsigned char uint8_t
Definition: types.h:14
uint16_t w
The width of the image.
Definition: v4l2.h:53