Adding stop stream support for 627b
[microdia.git] / microdia-bayer.c
blob385c74822252436a4681f65ae9586f2914a51c78
1 /**
2 * @file microdia-bayer.c
3 * @author Nicolas VIVIEN
4 * @date 2008-02-01
5 * @version v0.0.0
7 * @brief Driver for Microdia USB video camera
9 * @note Copyright (C) Nicolas VIVIEN
11 * @par Licences
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * @par SubVersion
28 * $Date: $
29 * $Revision: $
30 * $Author: $
31 * $HeadURL: $
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/kref.h>
42 #include <linux/usb.h>
43 #include <media/v4l2-common.h>
45 #include "microdia.h"
48 #define MAX(a,b) ((a)>(b)?(a):(b))
49 #define MIN(a,b) ((a)<(b)?(a):(b))
50 #define CLIP(a,low,high) MAX((low),MIN((high),(a)))
52 //Add by Boris Borisov decode by Vasily Khoruzhick adapt to V4L by Brian Johnson
53 void raw6270_2i420(uint8_t *, uint8_t *, struct microdia_coord *,
54 struct microdia_coord *, const int, const int, const int);
55 void microdia_getraw(uint8_t *, uint8_t *, int);
57 void microdia_raw2i420(uint8_t *, uint8_t *, struct microdia_coord *,
58 struct microdia_coord *, const int, const int, const int);
59 void microdia_raw2bgr24(uint8_t *, uint8_t *, struct microdia_coord *,
60 struct microdia_coord *, const int, const int, const int);
61 void microdia_b2rgb24(uint8_t *, uint8_t *,
62 struct microdia_coord *, struct microdia_coord *,
63 const int, const int, const int);
64 void microdia_b2rgb32(uint8_t *, uint8_t *,
65 struct microdia_coord *, struct microdia_coord *,
66 const int, const int, const int);
67 void microdia_b2bgr24(uint8_t *, uint8_t *,
68 struct microdia_coord *, struct microdia_coord *,
69 const int, const int, const int);
70 void microdia_b2bgr32(uint8_t *, uint8_t *,
71 struct microdia_coord *, struct microdia_coord *,
72 const int, const int, const int);
74 void microdia_b2uyvy(uint8_t *, uint8_t *,
75 struct microdia_coord *, struct microdia_coord *,
76 const int, const int, const int);
77 void microdia_b2yuyv(uint8_t *, uint8_t *,
78 struct microdia_coord *, struct microdia_coord *,
79 const int, const int, const int);
82 void microdia_correct_brightness(uint8_t *, const int, const int,
83 const int, int, int);
86 static signed short microdia_yuv_interp[256][8] = {
87 {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,0,0,0,1,-1,0},
88 {1,2,0,0,-1,2,-1,0},{1,2,0,0,-1,2,-2,0},{1,3,0,-1,-1,3,-2,0},{2,3,0,-1,-2,3,-2,0},
89 {2,4,0,-1,-2,4,-3,0},{2,5,1,-1,-2,4,-3,0},{2,5,1,-1,-3,5,-4,0},{3,6,1,-1,-3,5,-4,0},
90 {3,6,1,-2,-3,6,-5,0},{3,7,1,-2,-4,6,-5,-1},{4,7,1,-2,-4,7,-5,-1},{4,8,1,-2,-4,7,-6,-1},
91 {4,9,1,-2,-5,8,-6,-1},{5,9,1,-2,-5,8,-7,-1},{5,10,2,-3,-5,9,-7,-1},{5,10,2,-3,-6,9,-7,-1},
92 {5,11,2,-3,-6,10,-8,-1},{6,11,2,-3,-6,10,-8,-1},{6,12,2,-3,-7,11,-9,-1},{6,13,2,-3,-7,11,-9,-1},
93 {7,13,2,-4,-7,12,-10,-1},{7,14,2,-4,-8,12,-10,-2},{7,14,2,-4,-8,13,-10,-2},{8,15,3,-4,-8,13,-11,-2},
94 {8,15,3,-4,-9,14,-11,-2},{8,16,3,-4,-9,14,-12,-2},{8,17,3,-5,-9,15,-12,-2},{9,17,3,-5,-10,15,-12,-2},
95 {9,18,3,-5,-10,16,-13,-2},{9,18,3,-5,-10,16,-13,-2},{10,19,3,-5,-11,17,-14,-2},{10,19,3,-5,-11,17,-14,-2},
96 {10,20,4,-6,-11,18,-15,-2},{11,20,4,-6,-12,18,-15,-3},{11,21,4,-6,-12,19,-15,-3},{11,22,4,-6,-12,19,-16,-3},
97 {11,22,4,-6,-13,20,-16,-3},{12,23,4,-6,-13,20,-17,-3},{12,23,4,-7,-13,21,-17,-3},{12,24,4,-7,-14,21,-18,-3},
98 {13,24,5,-7,-14,22,-18,-3},{13,25,5,-7,-14,22,-18,-3},{13,26,5,-7,-15,23,-19,-3},{14,26,5,-7,-15,23,-19,-3},
99 {14,27,5,-8,-15,24,-20,-3},{14,27,5,-8,-16,24,-20,-3},{14,28,5,-8,-16,25,-20,-4},{15,28,5,-8,-16,25,-21,-4},
100 {15,29,5,-8,-17,26,-21,-4},{15,30,6,-8,-17,26,-22,-4},{16,30,6,-9,-17,27,-22,-4},{16,31,6,-9,-18,27,-23,-4},
101 {16,31,6,-9,-18,28,-23,-4},{17,32,6,-9,-18,28,-23,-4},{17,32,6,-9,-19,29,-24,-4},{17,33,6,-9,-19,29,-24,-4},
102 {17,34,6,-10,-19,30,-25,-4},{18,34,6,-10,-20,30,-25,-4},{18,35,7,-10,-20,31,-25,-5},{18,35,7,-10,-20,31,-26,-5},
103 {19,36,7,-10,-21,32,-26,-5},{19,36,7,-10,-21,32,-27,-5},{19,37,7,-11,-21,33,-27,-5},{20,37,7,-11,-22,33,-28,-5},
104 {20,38,7,-11,-22,34,-28,-5},{20,39,7,-11,-22,34,-28,-5},{20,39,7,-11,-23,35,-29,-5},{21,40,8,-11,-23,35,-29,-5},
105 {21,40,8,-12,-23,36,-30,-5},{21,41,8,-12,-24,36,-30,-5},{22,41,8,-12,-24,37,-30,-6},{22,42,8,-12,-24,37,-31,-6},
106 {22,43,8,-12,-25,38,-31,-6},{23,43,8,-12,-25,38,-32,-6},{23,44,8,-13,-25,39,-32,-6},{23,44,9,-13,-26,39,-33,-6},
107 {23,45,9,-13,-26,40,-33,-6},{24,45,9,-13,-26,40,-33,-6},{24,46,9,-13,-27,41,-34,-6},{24,47,9,-14,-27,41,-34,-6},
108 {25,47,9,-14,-27,42,-35,-6},{25,48,9,-14,-28,42,-35,-6},{25,48,9,-14,-28,43,-36,-6},{26,49,9,-14,-28,43,-36,-7},
109 {26,49,10,-14,-29,44,-36,-7},{26,50,10,-15,-29,44,-37,-7},{26,51,10,-15,-29,45,-37,-7},{27,51,10,-15,-30,45,-38,-7},
110 {27,52,10,-15,-30,46,-38,-7},{27,52,10,-15,-30,46,-38,-7},{28,53,10,-15,-31,47,-39,-7},{28,53,10,-16,-31,47,-39,-7},
111 {28,54,10,-16,-31,48,-40,-7},{29,54,11,-16,-32,48,-40,-7},{29,55,11,-16,-32,49,-41,-7},{29,56,11,-16,-32,49,-41,-8},
112 {29,56,11,-16,-33,50,-41,-8},{30,57,11,-17,-33,50,-42,-8},{30,57,11,-17,-33,51,-42,-8},{30,58,11,-17,-34,51,-43,-8},
113 {31,58,11,-17,-34,52,-43,-8},{31,59,11,-17,-34,52,-43,-8},{31,60,12,-17,-35,53,-44,-8},{31,60,12,-18,-35,53,-44,-8},
114 {32,61,12,-18,-35,54,-45,-8},{32,61,12,-18,-36,54,-45,-8},{32,62,12,-18,-36,55,-46,-8},{33,62,12,-18,-36,55,-46,-9},
115 {33,63,12,-18,-37,56,-46,-9},{33,64,12,-19,-37,56,-47,-9},{34,64,12,-19,-37,57,-47,-9},{34,65,13,-19,-38,57,-48,-9},
116 {34,65,13,-19,-38,58,-48,-9},{34,66,13,-19,-38,58,-48,-9},{35,66,13,-19,-39,59,-49,-9},{35,67,13,-20,-39,59,-49,-9},
117 {35,68,13,-20,-39,60,-50,-9},{36,68,13,-20,-40,60,-50,-9},{36,69,13,-20,-40,61,-51,-9},{36,69,14,-20,-40,61,-51,-9},
118 {37,70,14,-20,-41,62,-51,-10},{37,70,14,-21,-41,62,-52,-10},{37,71,14,-21,-41,63,-52,-10},{37,72,14,-21,-42,63,-53,-10},
119 {38,72,14,-21,-42,64,-53,-10},{38,73,14,-21,-42,64,-54,-10},{38,73,14,-21,-43,65,-54,-10},{39,74,14,-22,-43,65,-54,-10},
120 {39,74,15,-22,-43,66,-55,-10},{39,75,15,-22,-44,66,-55,-10},{40,75,15,-22,-44,67,-56,-10},{40,76,15,-22,-44,67,-56,-10},
121 {40,77,15,-22,-45,68,-56,-11},{40,77,15,-23,-45,68,-57,-11},{41,78,15,-23,-45,69,-57,-11},{41,78,15,-23,-46,69,-58,-11},
122 {41,79,15,-23,-46,70,-58,-11},{42,79,16,-23,-46,70,-59,-11},{42,80,16,-23,-47,71,-59,-11},{42,81,16,-24,-47,71,-59,-11},
123 {43,81,16,-24,-47,72,-60,-11},{43,82,16,-24,-48,72,-60,-11},{43,82,16,-24,-48,73,-61,-11},{43,83,16,-24,-48,73,-61,-11},
124 {44,83,16,-24,-49,74,-61,-12},{44,84,16,-25,-49,74,-62,-12},{44,85,17,-25,-49,75,-62,-12},{45,85,17,-25,-50,75,-63,-12},
125 {45,86,17,-25,-50,76,-63,-12},{45,86,17,-25,-50,76,-64,-12},{46,87,17,-25,-51,77,-64,-12},{46,87,17,-26,-51,77,-64,-12},
126 {46,88,17,-26,-51,78,-65,-12},{46,89,17,-26,-52,78,-65,-12},{47,89,18,-26,-52,79,-66,-12},{47,90,18,-26,-52,79,-66,-12},
127 {47,90,18,-26,-53,80,-66,-13},{48,91,18,-27,-53,80,-67,-13},{48,91,18,-27,-53,81,-67,-13},{48,92,18,-27,-54,81,-68,-13},
128 {49,92,18,-27,-54,82,-68,-13},{49,93,18,-27,-54,82,-69,-13},{49,94,18,-28,-54,83,-69,-13},{49,94,19,-28,-55,83,-69,-13},
129 {50,95,19,-28,-55,84,-70,-13},{50,95,19,-28,-55,84,-70,-13},{50,96,19,-28,-56,85,-71,-13},{51,96,19,-28,-56,85,-71,-13},
130 {51,97,19,-29,-56,86,-72,-13},{51,98,19,-29,-57,86,-72,-14},{52,98,19,-29,-57,87,-72,-14},{52,99,19,-29,-57,87,-73,-14},
131 {52,99,20,-29,-58,88,-73,-14},{52,100,20,-29,-58,88,-74,-14},{53,100,20,-30,-58,89,-74,-14},{53,101,20,-30,-59,89,-74,-14},
132 {53,102,20,-30,-59,90,-75,-14},{54,102,20,-30,-59,90,-75,-14},{54,103,20,-30,-60,91,-76,-14},{54,103,20,-30,-60,91,-76,-14},
133 {55,104,20,-31,-60,92,-77,-14},{55,104,21,-31,-61,92,-77,-15},{55,105,21,-31,-61,93,-77,-15},{55,106,21,-31,-61,93,-78,-15},
134 {56,106,21,-31,-62,94,-78,-15},{56,107,21,-31,-62,94,-79,-15},{56,107,21,-32,-62,95,-79,-15},{57,108,21,-32,-63,95,-79,-15},
135 {57,108,21,-32,-63,96,-80,-15},{57,109,22,-32,-63,96,-80,-15},{58,109,22,-32,-64,97,-81,-15},{58,110,22,-32,-64,97,-81,-15},
136 {58,111,22,-33,-64,98,-82,-15},{58,111,22,-33,-65,98,-82,-16},{59,112,22,-33,-65,99,-82,-16},{59,112,22,-33,-65,99,-83,-16},
137 {59,113,22,-33,-66,100,-83,-16},{60,113,22,-33,-66,100,-84,-16},{60,114,23,-34,-66,101,-84,-16},{60,115,23,-34,-67,101,-84,-16},
138 {60,115,23,-34,-67,102,-85,-16},{61,116,23,-34,-67,102,-85,-16},{61,116,23,-34,-68,103,-86,-16},{61,117,23,-34,-68,103,-86,-16},
139 {62,117,23,-35,-68,104,-87,-16},{62,118,23,-35,-69,104,-87,-16},{62,119,23,-35,-69,105,-87,-17},{63,119,24,-35,-69,105,-88,-17},
140 {63,120,24,-35,-70,106,-88,-17},{63,120,24,-35,-70,106,-89,-17},{63,121,24,-36,-70,107,-89,-17},{64,121,24,-36,-71,107,-90,-17},
141 {64,122,24,-36,-71,108,-90,-17},{64,123,24,-36,-71,108,-90,-17},{65,123,24,-36,-72,109,-91,-17},{65,124,24,-36,-72,109,-91,-17},
142 {65,124,25,-37,-72,110,-92,-17},{66,125,25,-37,-73,110,-92,-17},{66,125,25,-37,-73,111,-92,-18},{66,126,25,-37,-73,111,-93,-18},
143 {66,127,25,-37,-74,112,-93,-18},{67,127,25,-37,-74,112,-94,-18},{67,128,25,-38,-74,113,-94,-18},{67,128,25,-38,-75,113,-95,-18},
144 {68,129,25,-38,-75,114,-95,-18},{68,129,26,-38,-75,114,-95,-18},{68,130,26,-38,-76,115,-96,-18},{69,130,26,-38,-76,115,-96,-18},
145 {69,131,26,-39,-76,116,-97,-18},{69,132,26,-39,-77,116,-97,-18},{69,132,26,-39,-77,117,-97,-19},{70,133,26,-39,-77,117,-98,-19},
146 {70,133,26,-39,-78,118,-98,-19},{70,134,27,-39,-78,118,-99,-19},{71,134,27,-40,-78,119,-99,-19},{71,135,27,-40,-79,119,-100,-19},
147 {71,136,27,-40,-79,120,-100,-19},{72,136,27,-40,-79,120,-100,-19},{72,137,27,-40,-80,121,-101,-19},{72,137,27,-40,-80,121,-101,-19},
148 {72,138,27,-41,-80,122,-102,-19},{73,138,27,-41,-81,122,-102,-19},{73,139,28,-41,-81,123,-103,-19},{73,140,28,-41,-81,123,-103,-20},
149 {74,140,28,-41,-82,124,-103,-20},{74,141,28,-42,-82,124,-104,-20},{74,141,28,-42,-82,125,-104,-20},{75,142,28,-42,-83,125,-105,-20},
150 {75,142,28,-42,-83,126,-105,-20},{75,143,28,-42,-83,126,-105,-20},{75,144,28,-42,-84,127,-106,-20},{76,144,29,-43,-84,127,-106,-20}
154 /**
155 * @brief Decompress a frame
157 * This function permits to decompress a frame from the video stream.
159 * @param dev Device structure
161 * @returns 0 if all is OK
163 int microdia_decompress(struct usb_microdia *dev)
165 int size;
166 int factor;
168 void *data;
169 void *image;
170 struct microdia_frame_buf *framebuf;
172 if (dev == NULL)
173 return -EFAULT;
175 framebuf = dev->read_frame;
177 if (framebuf == NULL)
178 return -EFAULT;
180 image = dev->image_data;
181 image += dev->images[dev->fill_image].offset;
183 data = framebuf->data;
184 size = framebuf->filled;
186 switch (dev->resolution) {
187 case MICRODIA_80x60:
188 factor = 8;
189 break;
191 /*case MICRODIA_128x96:
192 factor = 5;
193 break;*/
195 case MICRODIA_160x120:
196 factor = 4;
197 break;
199 /*case MICRODIA_213x160:
200 factor = 3;
201 break;*/
203 case MICRODIA_320x240:
204 factor = 2;
205 break;
207 case MICRODIA_640x480:
208 factor = 1;
209 break;
211 case MICRODIA_800x600:
212 factor = 1;
213 break;
215 case MICRODIA_1024x768:
216 factor = 1;
217 break;
219 case MICRODIA_1280x1024:
220 factor = 1;
221 break;
223 default:
224 return -EFAULT;
228 switch (dev->vsettings.palette) {
229 case MICRODIA_PALETTE_BGR24:
230 microdia_raw2bgr24(data, image, &dev->image, &dev->view,
231 dev->vsettings.hflip, dev->vsettings.vflip, factor);
232 break;
234 /*case MICRODIA_PALETTE_RGB32:
235 microdia_b2rgb32(data, image, &dev->image, &dev->view,
236 dev->vsettings.hflip, dev->vsettings.vflip, factor);
237 break;
239 case MICRODIA_PALETTE_BGR24:
240 microdia_b2bgr24(data, image, &dev->image, &dev->view,
241 dev->vsettings.hflip, dev->vsettings.vflip, factor);
242 break;
244 case MICRODIA_PALETTE_BGR32:
245 microdia_b2bgr32(data, image, &dev->image, &dev->view,
246 dev->vsettings.hflip, dev->vsettings.vflip, factor);
247 break;
249 case MICRODIA_PALETTE_UYVY:
250 microdia_b2uyvy(data, image, &dev->image, &dev->view,
251 dev->vsettings.hflip, dev->vsettings.vflip, factor);
252 break;
254 case MICRODIA_PALETTE_YUYV:
255 microdia_b2yuyv(data, image, &dev->image, &dev->view,
256 dev->vsettings.hflip, dev->vsettings.vflip, factor);
257 break;
259 case MICRODIA_PALETTE_I420:
260 if(dev->webcam_model == MICRODIA_6270)
262 raw6270_2i420(data, image, &dev->image, &dev->view,
263 dev->vsettings.hflip, dev->vsettings.vflip, factor);
264 } else
266 microdia_raw2i420(data, image, &dev->image, &dev->view,
267 dev->vsettings.hflip, dev->vsettings.vflip, factor);
269 break;
270 default:
271 microdia_getraw(data, image, size);
272 break;
275 //microdia_correct_brightness(image, dev->view.x, dev->view.y,
276 // dev->vsettings.brightness, dev->vsettings.palette, dev->vsettings.depth);
278 return 0;
282 /**
283 * @brief Correct the brightness of an image.
285 * This function permits to correct the brightness of an image.
287 * @param img Buffer to RGB/YUV data
288 * @param width Width of frame
289 * @param height Height of frame
290 * @param brightness Brightness correction
291 * @param depth Color depth
293 * @retval rgb Buffer to RGB/YUV data
295 void microdia_correct_brightness(uint8_t *img, const int width, const int height,
296 const int brightness, int palette, int depth)
298 int i;
299 int x;
302 switch (palette) {
303 case MICRODIA_PALETTE_RGB24:
304 case MICRODIA_PALETTE_BGR24:
305 case MICRODIA_PALETTE_RGB32:
306 case MICRODIA_PALETTE_BGR32:
307 depth = (depth == 24) ? 3 : 4;
309 if (brightness >= 32767) {
310 x = (brightness - 32767) / 256;
312 for (i = 0; i < (width * height * depth); i++) {
313 if ((*(img + i) + (unsigned char) x) > 255)
314 *(img + i) = 255;
315 else
316 *(img + i) += (unsigned char) x;
319 else {
320 x = (32767 - brightness) / 256;
322 for (i = 0; i < (width * height * depth); i++) {
323 if ((unsigned char) x > *(img + i))
324 *(img + i) = 0;
325 else
326 *(img + i) -= (unsigned char) x;
330 break;
332 case MICRODIA_PALETTE_UYVY:
333 depth = 2;
335 if (brightness >= 32767) {
336 x = (brightness - 32767) / 256;
338 for (i = 1; i < (width * height * depth); i=i+depth) {
339 if ((*(img + i) + (unsigned char) x) > 255)
340 *(img + i) = 255;
341 else
342 *(img + i) += (unsigned char) x;
345 else {
346 x = (32767 - brightness) / 256;
348 for (i = 1; i < (width * height * depth); i=i+depth) {
349 if ((unsigned char) x > *(img + i))
350 *(img + i) = 0;
351 else
352 *(img + i) -= (unsigned char) x;
356 break;
358 case MICRODIA_PALETTE_YUYV:
359 depth = 2;
361 if (brightness >= 32767) {
362 x = (brightness - 32767) / 256;
364 for (i = 0; i < (width * height * depth); i=i+depth) {
365 if ((*(img + i) + (unsigned char) x) > 255)
366 *(img + i) = 255;
367 else
368 *(img + i) += (unsigned char) x;
371 else {
372 x = (32767 - brightness) / 256;
374 for (i = 0; i < (width * height * depth); i=i+depth) {
375 if ((unsigned char) x > *(img + i))
376 *(img + i) = 0;
377 else
378 *(img + i) -= (unsigned char) x;
382 break;
387 /**
388 * @brief This function permits to get the raw data. (without treatments)
390 * @param bayer Buffer with the bayer data
391 * @param size Length of bayer buffer
393 * @retval raw Buffer with the data from video sensor
395 void microdia_getraw(uint8_t *bayer, uint8_t *raw,
396 int size) {
397 memcpy(raw, bayer, size);
400 void microdia_raw2bgr24(uint8_t *raw, uint8_t *rgb, struct microdia_coord *image,
401 struct microdia_coord *view, const int hflip, const int vflip, const int factor)
403 int i = 0, x = 0, y = 0;
404 unsigned char *buf = raw;
405 unsigned char *buf3 = rgb;
406 int fwidth, fheight;
408 fwidth = image->x / factor;
409 fheight = image->y / factor;
411 while (i < (image->x * image->y + (image->x * image->y) / 2)) {
412 int tile = 0;
413 for (tile = 0; tile < 4; tile++) {
414 int subX = 0;
415 int subY = 0;
416 for (subY = 0; subY < 4; subY++) {
417 for (subX = 0; subX < 8; subX++) {
418 int subI = i + tile * 32 + 8 * subY + subX;
419 int subU = i + 128 + tile * 8 + (subY >> 1) * 4 + (subX >> 1);
420 int subV = subU + 32;
422 int relX = x + (((tile == 0) || (tile == 1)) ? 0 : 8) + subX; //tile 0, 1 to into left comumn
423 int relY = y + (((tile == 0) || (tile == 2)) ? 0 : 4) + subY; //tile 0, 2 go into top row
426 if (hflip) {
427 relX = image->x - relX;
429 if (vflip) {
430 relY = image->y - relY;
433 relX /= factor;
434 relY /= factor;
436 relX += (view->x - fwidth) >> 1;
437 relY += (view->y - fheight) >> 1;
438 if ((relX < view->x) && (relY < view->y)) {
439 unsigned char *ptr;
440 int c, d, e;
441 c = buf[subI] - 16;
442 d = buf[subU] - 128;
443 e = buf[subV] - 128;
445 ptr = buf3 + relY * view->x * 3 + relX * 3;
446 *ptr = CLIP((298 * c + 516 * d + 128) >> 8, 0, 255);
447 ptr++;
448 *ptr = CLIP((298 * c - 100 * d - 208 * e + 128) >> 8, 0, 255);
449 ptr++;
450 *ptr = CLIP((298 * c + 409 * e + 128) >> 8, 0, 255);
456 i += 192;
457 x += 16;
458 if (x >= image->x) {
459 x = 0;
460 y += 8;
465 void microdia_raw2i420(uint8_t *raw, uint8_t *i420, struct microdia_coord *image,
466 struct microdia_coord *view, const int hflip, const int vflip, const int factor)
468 int i = 0, x = 0, y = 0;
469 unsigned char *buf = raw;
470 unsigned char *buf3 = i420;
471 int fwidth, fheight;
473 fwidth = image->x / factor;
474 fheight = image->y / factor;
476 //printk(KERN_INFO "image: %d %d, view: %d %d factor: %d\n", image->x, image->y, view->x, view->y, factor);
478 while (i < (image->x * image->y + (image->x * image->y) / 2)) {
479 int tile = 0;
480 for (tile = 0; tile < 4; tile++) {
481 int subX = 0;
482 int subY = 0;
483 for (subY = 0; subY < 4; subY++) {
484 for (subX = 0; subX < 8; subX++) {
485 int subI = i + tile * 32 + 8 * subY + subX;
486 int subU = i + 128 + tile * 8 + (subY >> 1) * 4 + (subX >> 1);
487 int subV = subU + 32;
489 int relX = x + (((tile == 0) || (tile == 1)) ? 0 : 8) + subX; //tile 0, 1 to into left comumn
490 int relY = y + (((tile == 0) || (tile == 2)) ? 0 : 4) + subY; //tile 0, 2 go into top row
493 if (hflip) {
494 relX = image->x - relX;
496 if (vflip) {
497 relY = image->y - relY;
500 relX /= factor;
501 relY /= factor;
503 relX += (view->x - fwidth) >> 1;
504 relY += (view->y - fheight) >> 1;
505 if ((relX < view->x) && (relY < view->y)) {
506 unsigned char *ptr;
508 ptr = buf3 + relY * view->x + relX;
509 *ptr = buf[subI];
510 ptr = (buf3 + (view->x * view->y) + (relY / 2) * (view->x / 2) + (relX / 2));
511 *ptr = buf[subU];
512 ptr = (buf3 + (view->x * view->y) + (view->x * view->y / 4) + (relY / 2) *
513 (view->x / 2)+ (relX / 2));
514 *ptr = buf[subV];
520 i += 192;
521 x += 16;
522 if (x >= image->x) {
523 x = 0;
524 y += 8;
530 //Add by Boris Borisov decode by Vasily Khoruzhick adapt to V4L by Brian Johnson
531 /**
532 * Add and optimize by Boris Borisov decode by Vasily Khoruzhick adapt to V4L by Brian Johnson
533 * @brief This function permits to convert an image from 6270 raw format to i420
534 * Add by Boris Borisov decode by Vasily Khoruzhick adapt to V4L by Brian Johnson
535 * @param raw Buffer with the bayer data
536 * @param image Size of image
537 * @param view Size of view
538 * @param hflip Horizontal flip
539 * @param vflip Vertical flip
540 * @param factor Factor of redimensioning
542 * @retval i420 Buffer with the i420 data
543 Format of stream is uvyy,uvyy - repeated 320 times for 640x480
544 y - repeated 640 times for 640x480
545 First 1280 bytes is maybe dumy FIX ME
548 void raw6270_2i420(uint8_t *raw, uint8_t *i420, struct microdia_coord *image,
549 struct microdia_coord *view, const int hflip, const int vflip, const int factor)
551 int i, j, RawIndex = 0;
552 unsigned char *y, *u, *v;
553 uint8_t *buf;
554 //For fast calculations
555 int x_div_2,y_div_2;
556 //Skip first 1280 bytes strange dummy bytes
557 raw+=1280;
559 x_div_2=image->x/2; //for fast calculation if x=640 x_div_2=320
560 y_div_2=image->y/2; //for fast calculation if y=480 y_div_4=240
561 buf=raw;
562 y = i420;
563 u = i420 + image->x * image->y;
564 v = i420 + image->x * image->y + x_div_2 * y_div_2;
566 RawIndex = 0;
567 for (i = 0; i < y_div_2; i++) {
568 for (j = 0; j < x_div_2; j ++) {
569 *u = buf[RawIndex++];
570 *v = buf[RawIndex++];
571 *y = buf[RawIndex++];
572 y++;
573 *y = buf[RawIndex++];
574 u++;
575 v++;
576 y++;
578 for (j = 0; j < image->x; j++) {
579 *y = buf[RawIndex++];
580 y++;
587 /**
588 * @brief This function permits to convert an image from bayer to RGB24
590 * @param bayer Buffer with the bayer data
591 * @param image Size of image
592 * @param view Size of view
593 * @param hflip Horizontal flip
594 * @param vflip Vertical flip
595 * @param factor Factor of redimensioning
597 * @retval rgb Buffer with the RGB data
599 void microdia_b2rgb24(uint8_t *bayer, uint8_t *rgb,
600 struct microdia_coord *image,
601 struct microdia_coord *view,
602 const int hflip, const int vflip,
603 const int factor) {
604 uint8_t *b;
606 int x, y; // Position in bayer image
607 int i, j; // Position in rgb image
609 int width = image->x;
610 int height = image->y;
612 int nwidth = width / factor;
613 int nheight = height / factor;
615 int offset;
616 int startx, stepx;
617 int starty, stepy;
620 // Calculate the initial position (on Y axis)
621 if (vflip) {
622 starty = height - 2;
623 stepy = -factor;
625 else {
626 starty = 0;
627 stepy = factor;
630 // Calculate the initial position (on X axis)
631 if (hflip) {
632 startx = width - 1;
633 stepx = -factor;
634 offset = width - 2;
636 else {
637 startx = 0;
638 stepx = factor;
639 offset = 1;
643 // Skip the first line
644 bayer += width;
646 // To center vertically the image in the view
647 rgb += ((view->y - nheight) / 2) * view->x * 3;
649 // To center horizontally the image in the view
650 rgb += ((view->x - nwidth) / 2) * 3;
652 // Clean the first line
653 memset(rgb, 0, nwidth * 3);
654 rgb += nwidth * 3;
657 // For each rgb line without the borders (first and last line)
658 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
659 // Go to the start of line
660 b = bayer + y * width + offset;
662 // Offset to center horizontally the image in the view
663 rgb += (view->x - nwidth) * 3;
665 if (y & 0x1) {
666 // Skip the first pixel
667 *rgb++ = 0;
668 *rgb++ = 0;
669 *rgb++ = 0;
671 // GBGBGB : Line process...
672 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
673 if (x & 0x1) {
674 *rgb++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
675 *rgb++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
676 *rgb++ = *b;
678 else {
679 *rgb++ = (*(b-width) + *(b+width)) >> 1;
680 *rgb++ = *b;
681 *rgb++ = (*(b-1) + *(b+1)) >> 1;
684 b += stepx;
687 // Skip the last pixel
688 *rgb++ = 0;
689 *rgb++ = 0;
690 *rgb++ = 0;
692 else {
693 // Skip the first pixel
694 *rgb++ = 0;
695 *rgb++ = 0;
696 *rgb++ = 0;
698 // RGRGRG : Line process...
699 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
700 if (x & 0x1) {
701 *rgb++ = (*(b-1) + *(b+1)) >> 1;
702 *rgb++ = *b;
703 *rgb++ = (*(b-width) + *(b+width)) >> 1;
705 else {
706 *rgb++ = *b;
707 *rgb++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
708 *rgb++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
711 b += stepx;
714 // Skip the last pixel
715 *rgb++ = 0;
716 *rgb++ = 0;
717 *rgb++ = 0;
721 // Clean the last line
722 memset(rgb, 0, nwidth * 3);
726 /**
727 * @brief This function permits to convert an image from bayer to RGB32
729 * @param bayer Buffer with the bayer data
730 * @param image Size of image
731 * @param view Size of view
732 * @param hflip Horizontal flip
733 * @param vflip Vertical flip
734 * @param factor Factor of redimensioning
736 * @retval rgb Buffer with the RGB data
738 void microdia_b2rgb32(uint8_t *bayer, uint8_t *rgb,
739 struct microdia_coord *image,
740 struct microdia_coord *view,
741 const int hflip, const int vflip,
742 const int factor) {
743 uint8_t *b;
745 int x, y; // Position in bayer image
746 int i, j; // Position in rgb image
748 int width = image->x;
749 int height = image->y;
751 int nwidth = width / factor;
752 int nheight = height / factor;
754 int offset;
755 int startx, stepx;
756 int starty, stepy;
759 // Calculate the initial position (on Y axis)
760 if (vflip) {
761 starty = height - 2;
762 stepy = -factor;
764 else {
765 starty = 0;
766 stepy = factor;
769 // Calculate the initial position (on X axis)
770 if (hflip) {
771 startx = width - 1;
772 stepx = -factor;
773 offset = width - 2;
775 else {
776 startx = 0;
777 stepx = factor;
778 offset = 1;
782 // Skip the first line
783 bayer += width;
785 // To center vertically the image in the view
786 rgb += ((view->y - nheight) / 2) * view->x * 4;
788 // To center horizontally the image in the view
789 rgb += ((view->x - nwidth) / 2) * 4;
791 // Clean the first line
792 memset(rgb, 0, nwidth * 4);
793 rgb += nwidth * 4;
796 // For each rgb line without the borders (first and last line)
797 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
798 // Go to the start of line
799 b = bayer + y * width + offset;
801 // Offset to center horizontally the image in the view
802 rgb += (view->x - nwidth) * 4;
804 if (y & 0x1) {
805 // Skip the first pixel
806 *rgb++ = 0;
807 *rgb++ = 0;
808 *rgb++ = 0;
809 *rgb++ = 0;
811 // GBGBGB : Line process...
812 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
813 if (x & 0x1) {
814 *rgb++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
815 *rgb++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
816 *rgb++ = *b;
817 *rgb++ = 0;
819 else {
820 *rgb++ = (*(b-width) + *(b+width)) >> 1;
821 *rgb++ = *b;
822 *rgb++ = (*(b-1) + *(b+1)) >> 1;
823 *rgb++ = 0;
826 b += stepx;
829 // Skip the last pixel
830 *rgb++ = 0;
831 *rgb++ = 0;
832 *rgb++ = 0;
833 *rgb++ = 0;
835 else {
836 // Skip the first pixel
837 *rgb++ = 0;
838 *rgb++ = 0;
839 *rgb++ = 0;
840 *rgb++ = 0;
842 // RGRGRG : Line process...
843 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
844 if (x & 0x1) {
845 *rgb++ = (*(b-1) + *(b+1)) >> 1;
846 *rgb++ = *b;
847 *rgb++ = (*(b-width) + *(b+width)) >> 1;
848 *rgb++ = 0;
850 else {
851 *rgb++ = *b;
852 *rgb++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
853 *rgb++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
854 *rgb++ = 0;
857 b += stepx;
860 // Skip the last pixel
861 *rgb++ = 0;
862 *rgb++ = 0;
863 *rgb++ = 0;
864 *rgb++ = 0;
868 // Clean the last line
869 memset(rgb, 0, nwidth * 4);
873 /**
874 * @brief This function permits to convert an image from bayer to BGR24
876 * @param bayer Buffer with the bayer data
877 * @param image Size of image
878 * @param view Size of view
879 * @param hflip Horizontal flip
880 * @param vflip Vertical flip
881 * @param factor Factor of redimensioning
883 * @retval bgr Buffer with the BGR data
885 void microdia_b2bgr24(uint8_t *bayer, uint8_t *bgr,
886 struct microdia_coord *image,
887 struct microdia_coord *view,
888 const int hflip, const int vflip,
889 const int factor) {
890 uint8_t *b;
892 int x, y; // Position in bayer image
893 int i, j; // Position in bgr image
895 int width = image->x;
896 int height = image->y;
898 int nwidth = width / factor;
899 int nheight = height / factor;
901 int offset;
902 int startx, stepx;
903 int starty, stepy;
906 // Calculate the initial position (on Y axis)
907 if (vflip) {
908 starty = height - 2;
909 stepy = -factor;
911 else {
912 starty = 0;
913 stepy = factor;
916 // Calculate the initial position (on X axis)
917 if (hflip) {
918 startx = width - 1;
919 stepx = -factor;
920 offset = width - 2;
922 else {
923 startx = 0;
924 stepx = factor;
925 offset = 1;
929 // Skip the first line
930 bayer += width;
932 // To center vertically the image in the view
933 bgr += ((view->y - nheight) / 2) * view->x * 3;
935 // To center horizontally the image in the view
936 bgr += ((view->x - nwidth) / 2) * 3;
938 // Clean the first line
939 memset(bgr, 0, nwidth * 3);
940 bgr += nwidth * 3;
943 // For each bgr line without the borders (first and last line)
944 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
945 // Go to the start of line
946 b = bayer + y * width + offset;
948 // Offset to center horizontally the image in the view
949 bgr += (view->x - nwidth) * 3;
951 if (y & 0x1) {
952 // Skip the first pixel
953 *bgr++ = 0;
954 *bgr++ = 0;
955 *bgr++ = 0;
957 // GBGBGB : Line process...
958 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
959 if (x & 0x1) {
960 *bgr++ = *b;
961 *bgr++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
962 *bgr++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
964 else {
965 *bgr++ = (*(b-1) + *(b+1)) >> 1;
966 *bgr++ = *b;
967 *bgr++ = (*(b-width) + *(b+width)) >> 1;
970 b += stepx;
973 // Skip the last pixel
974 *bgr++ = 0;
975 *bgr++ = 0;
976 *bgr++ = 0;
978 else {
979 // Skip the first pixel
980 *bgr++ = 0;
981 *bgr++ = 0;
982 *bgr++ = 0;
984 // RGRGRG : Line process...
985 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
986 if (x & 0x1) {
987 *bgr++ = (*(b-width) + *(b+width)) >> 1;
988 *bgr++ = *b;
989 *bgr++ = (*(b-1) + *(b+1)) >> 1;
991 else {
992 *bgr++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
993 *bgr++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
994 *bgr++ = *b;
997 b += stepx;
1000 // Skip the last pixel
1001 *bgr++ = 0;
1002 *bgr++ = 0;
1003 *bgr++ = 0;
1007 // Clean the last line
1008 memset(bgr, 0, nwidth * 3);
1012 /**
1013 * @brief This function permits to convert an image from bayer to BGR32
1015 * @param bayer Buffer with the bayer data
1016 * @param image Size of image
1017 * @param view Size of view
1018 * @param hflip Horizontal flip
1019 * @param vflip Vertical flip
1020 * @param factor Factor of redimensioning
1022 * @retval bgr Buffer with the BGR data
1024 void microdia_b2bgr32(uint8_t *bayer, uint8_t *bgr,
1025 struct microdia_coord *image,
1026 struct microdia_coord *view,
1027 const int hflip, const int vflip,
1028 const int factor) {
1029 uint8_t *b;
1031 int x, y; // Position in bayer image
1032 int i, j; // Position in bgr image
1034 int width = image->x;
1035 int height = image->y;
1037 int nwidth = width / factor;
1038 int nheight = height / factor;
1040 int offset;
1041 int startx, stepx;
1042 int starty, stepy;
1045 // Calculate the initial position (on Y axis)
1046 if (vflip) {
1047 starty = height - 2;
1048 stepy = -factor;
1050 else {
1051 starty = 0;
1052 stepy = factor;
1055 // Calculate the initial position (on X axis)
1056 if (hflip) {
1057 startx = width - 1;
1058 stepx = -factor;
1059 offset = width - 2;
1061 else {
1062 startx = 0;
1063 stepx = factor;
1064 offset = 1;
1068 // Skip the first line
1069 bayer += width;
1071 // To center vertically the image in the view
1072 bgr += ((view->y - nheight) / 2) * view->x * 4;
1074 // To center horizontally the image in the view
1075 bgr += ((view->x - nwidth) / 2) * 4;
1077 // Clean the first line
1078 memset(bgr, 0, nwidth * 4);
1079 bgr += nwidth * 4;
1082 // For each bgr line without the borders (first and last line)
1083 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
1084 // Go to the start of line
1085 b = bayer + y * width + offset;
1087 // Offset to center horizontally the image in the view
1088 bgr += (view->x - nwidth) * 4;
1090 if (y & 0x1) {
1091 // Skip the first pixel
1092 *bgr++ = 0;
1093 *bgr++ = 0;
1094 *bgr++ = 0;
1095 *bgr++ = 0;
1097 // GBGBGB : Line process...
1098 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1099 if (x & 0x1) {
1100 *bgr++ = *b;
1101 *bgr++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1102 *bgr++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1103 *bgr++ = 0;
1105 else {
1106 *bgr++ = (*(b-1) + *(b+1)) >> 1;
1107 *bgr++ = *b;
1108 *bgr++ = (*(b-width) + *(b+width)) >> 1;
1109 *bgr++ = 0;
1112 b += stepx;
1115 // Skip the last pixel
1116 *bgr++ = 0;
1117 *bgr++ = 0;
1118 *bgr++ = 0;
1119 *bgr++ = 0;
1121 else {
1122 // Skip the first pixel
1123 *bgr++ = 0;
1124 *bgr++ = 0;
1125 *bgr++ = 0;
1126 *bgr++ = 0;
1128 // RGRGRG : Line process...
1129 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1130 if (x & 0x1) {
1131 *bgr++ = (*(b-width) + *(b+width)) >> 1;
1132 *bgr++ = *b;
1133 *bgr++ = (*(b-1) + *(b+1)) >> 1;
1134 *bgr++ = 0;
1136 else {
1137 *bgr++ = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1138 *bgr++ = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1139 *bgr++ = *b;
1140 *bgr++ = 0;
1143 b += stepx;
1146 // Skip the last pixel
1147 *bgr++ = 0;
1148 *bgr++ = 0;
1149 *bgr++ = 0;
1150 *bgr++ = 0;
1154 // Clean the last line
1155 memset(bgr, 0, nwidth * 4);
1159 /**
1160 * @brief This function permits to convert an image from bayer to YUV (UYVY)
1162 * @param bayer Buffer with the bayer data
1163 * @param image Size of image
1164 * @param view Size of view
1165 * @param hflip Horizontal flip
1166 * @param vflip Vertical flip
1167 * @param factor Factor of redimensioning
1169 * @retval yuv Buffer with the YUV data
1171 void microdia_b2uyvy(uint8_t *bayer, uint8_t *yuv,
1172 struct microdia_coord *image,
1173 struct microdia_coord *view,
1174 const int hflip, const int vflip,
1175 const int factor) {
1176 uint8_t *b;
1178 int x, y; // Position in bayer image
1179 int i, j; // Position in yuv image
1181 int pR, pG, pB;
1182 int pY, pU, pV;
1184 int width = image->x;
1185 int height = image->y;
1187 int nwidth = width / factor;
1188 int nheight = height / factor;
1190 int offset;
1191 int startx, stepx;
1192 int starty, stepy;
1195 // Calculate the initial position (on Y axis)
1196 if (vflip) {
1197 starty = height - 2;
1198 stepy = -factor;
1200 else {
1201 starty = 0;
1202 stepy = factor;
1205 // Calculate the initial position (on X axis)
1206 if (hflip) {
1207 startx = width - 1;
1208 stepx = -factor;
1209 offset = width - 2;
1211 else {
1212 startx = 0;
1213 stepx = factor;
1214 offset = 1;
1217 // Background color...
1218 memset(yuv, 16, width * 2);
1219 for (i=0; i<width*2; i=i+2, *(yuv+i)=128);
1220 for (i=1; i<height; i++)
1221 memcpy(yuv+i*width*2, yuv, width*2);
1223 // Skip the first line
1224 bayer += width;
1226 // To center vertically the image in the view
1227 yuv += ((view->y - nheight) / 2) * view->x * 2;
1229 // To center horizontally the image in the view
1230 yuv += ((view->x - nwidth) / 2) * 2;
1232 // Clean the first line
1233 memset(yuv, 16, nwidth * 2);
1234 for (i=0; i<nwidth*2; i=i+2, *(yuv+i)=128);
1235 yuv += nwidth * 2;
1238 // For each yuv line without the borders (first and last line)
1239 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
1240 // Go to the start of line
1241 b = bayer + y * width + offset;
1243 // Offset to center horizontally the image in the view
1244 yuv += (view->x - nwidth) * 2;
1246 if (y & 0x1) {
1247 // Skip the first pixel
1248 *yuv++ = 128;
1249 *yuv++ = 16;
1251 // GBGBGB : Line process...
1252 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1253 if (x & 0x1) {
1254 pR = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1255 pG = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1256 pB = *b;
1258 else {
1259 pR = (*(b-width) + *(b+width)) >> 1;
1260 pG = *b;
1261 pB = (*(b-1) + *(b+1)) >> 1;
1264 pY = microdia_yuv_interp[pR][0] + microdia_yuv_interp[pG][1] + microdia_yuv_interp[pB][2];
1265 pU = microdia_yuv_interp[pR][3] + microdia_yuv_interp[pG][4] + microdia_yuv_interp[pB][5];
1266 pV = microdia_yuv_interp[pR][5] + microdia_yuv_interp[pG][6] + microdia_yuv_interp[pB][7];
1268 pY = CLIP(pY, 0,255);
1269 pU = CLIP(pU, -127,127);
1270 pV = CLIP(pV, -127,127);
1272 if (i % 2){
1273 *yuv++ = (112 * pU)/127 + 128; // U
1274 *yuv++ = (219 * pY)/255 + 16; // Y
1276 else {
1277 *yuv++ = (112 * pV)/127 + 128; // V
1278 *yuv++ = (219 * pY)/255 + 16; // Y
1281 b += stepx;
1284 // Skip the last pixel
1285 *yuv++ = 128;
1286 *yuv++ = 16;
1288 else {
1289 // Skip the first pixel
1290 *yuv++ = 128;
1291 *yuv++ = 16;
1293 // RGRGRG : Line process...
1294 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1295 if (x & 0x1) {
1296 pR = (*(b-1) + *(b+1)) >> 1;
1297 pG = *b;
1298 pB = (*(b-width) + *(b+width)) >> 1;
1300 else {
1301 pR = *b;
1302 pG = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1303 pB = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1306 pY = microdia_yuv_interp[pR][0] + microdia_yuv_interp[pG][1] + microdia_yuv_interp[pB][2];
1307 pU = microdia_yuv_interp[pR][3] + microdia_yuv_interp[pG][4] + microdia_yuv_interp[pB][5];
1308 pV = microdia_yuv_interp[pR][5] + microdia_yuv_interp[pG][6] + microdia_yuv_interp[pB][7];
1310 pY = CLIP(pY, 0,255);
1311 pU = CLIP(pU, -127,127);
1312 pV = CLIP(pV, -127,127);
1314 if (i % 2){
1315 *yuv++ = (112 * pU)/127 + 128; // U
1316 *yuv++ = (219 * pY)/255 + 16; // Y
1318 else {
1319 *yuv++ = (112 * pV)/127 + 128; // V
1320 *yuv++ = (219 * pY)/255 + 16; // Y
1323 b += stepx;
1326 // Skip the last pixel
1327 *yuv++ = 128;
1328 *yuv++ = 16;
1332 // Clean the last line
1333 memset(yuv, 16, nwidth * 2);
1334 for (i=0; i<nwidth*2; i=i+2, *(yuv+i)=128);
1338 /**
1339 * @brief This function permits to convert an image from bayer to YUV (YUYV)
1341 * @param bayer Buffer with the bayer data
1342 * @param image Size of image
1343 * @param view Size of view
1344 * @param hflip Horizontal flip
1345 * @param vflip Vertical flip
1346 * @param factor Factor of redimensioning
1348 * @retval yuv Buffer with the YUV data
1350 void microdia_b2yuyv(uint8_t *bayer, uint8_t *yuv,
1351 struct microdia_coord *image,
1352 struct microdia_coord *view,
1353 const int hflip, const int vflip,
1354 const int factor) {
1355 uint8_t *b;
1357 int x, y; // Position in bayer image
1358 int i, j; // Position in yuv image
1360 int pR, pG, pB;
1361 int pY, pU, pV;
1363 int width = image->x;
1364 int height = image->y;
1366 int nwidth = width / factor;
1367 int nheight = height / factor;
1369 int offset;
1370 int startx, stepx;
1371 int starty, stepy;
1374 // Calculate the initial position (on Y axis)
1375 if (vflip) {
1376 starty = height - 2;
1377 stepy = -factor;
1379 else {
1380 starty = 0;
1381 stepy = factor;
1384 // Calculate the initial position (on X axis)
1385 if (hflip) {
1386 startx = width - 1;
1387 stepx = -factor;
1388 offset = width - 2;
1390 else {
1391 startx = 0;
1392 stepx = factor;
1393 offset = 1;
1396 // Background color...
1397 memset(yuv, 128, width * 2);
1398 for (i=0; i<width*2; i=i+2, *(yuv+i)=16);
1399 for (i=1; i<height; i++)
1400 memcpy(yuv+i*width*2, yuv, width*2);
1402 // Skip the first line
1403 bayer += width;
1405 // To center vertically the image in the view
1406 yuv += ((view->y - nheight) / 2) * view->x * 2;
1408 // To center horizontally the image in the view
1409 yuv += ((view->x - nwidth) / 2) * 2;
1411 // Clean the first line
1412 memset(yuv, 128, nwidth * 2);
1413 for (i=0; i<nwidth*2; i=i+2, *(yuv+i)=16);
1414 yuv += nwidth * 2;
1417 // For each yuv line without the borders (first and last line)
1418 for (j=0, y=starty; j<nheight-2; j++, y=y+stepy) {
1419 // Go to the start of line
1420 b = bayer + y * width + offset;
1422 // Offset to center horizontally the image in the view
1423 yuv += (view->x - nwidth) * 2;
1425 if (y & 0x1) {
1426 // Skip the first pixel
1427 *yuv++ = 16;
1428 *yuv++ = 128;
1430 // GBGBGB : Line process...
1431 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1432 if (x & 0x1) {
1433 pR = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1434 pG = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1435 pB = *b;
1437 else {
1438 pR = (*(b-width) + *(b+width)) >> 1;
1439 pG = *b;
1440 pB = (*(b-1) + *(b+1)) >> 1;
1443 pY = microdia_yuv_interp[pR][0] + microdia_yuv_interp[pG][1] + microdia_yuv_interp[pB][2];
1444 pU = microdia_yuv_interp[pR][3] + microdia_yuv_interp[pG][4] + microdia_yuv_interp[pB][5];
1445 pV = microdia_yuv_interp[pR][5] + microdia_yuv_interp[pG][6] + microdia_yuv_interp[pB][7];
1447 pY = CLIP(pY, 0,255);
1448 pU = CLIP(pU, -127,127);
1449 pV = CLIP(pV, -127,127);
1451 if (i % 2){
1452 *yuv++ = (219 * pY)/255 + 16; // Y
1453 *yuv++ = (112 * pU)/127 + 128; // U
1455 else {
1456 *yuv++ = (219 * pY)/255 + 16; // Y
1457 *yuv++ = (112 * pV)/127 + 128; // V
1460 b += stepx;
1463 // Skip the last pixel
1464 *yuv++ = 16;
1465 *yuv++ = 128;
1467 else {
1468 // Skip the first pixel
1469 *yuv++ = 16;
1470 *yuv++ = 128;
1472 // RGRGRG : Line process...
1473 for (i=0, x=startx; i<nwidth-2; i++, x=x+stepx) {
1474 if (x & 0x1) {
1475 pR = (*(b-1) + *(b+1)) >> 1;
1476 pG = *b;
1477 pB = (*(b-width) + *(b+width)) >> 1;
1479 else {
1480 pR = *b;
1481 pG = (*(b-width) + *(b-1) + *(b+1) + *(b+width)) >> 2;
1482 pB = (*(b-width-1) + *(b-width+1) + *(b+width-1) + *(b+width+1)) >> 2;
1485 pY = microdia_yuv_interp[pR][0] + microdia_yuv_interp[pG][1] + microdia_yuv_interp[pB][2];
1486 pU = microdia_yuv_interp[pR][3] + microdia_yuv_interp[pG][4] + microdia_yuv_interp[pB][5];
1487 pV = microdia_yuv_interp[pR][5] + microdia_yuv_interp[pG][6] + microdia_yuv_interp[pB][7];
1489 pY = CLIP(pY, 0,255);
1490 pU = CLIP(pU, -127,127);
1491 pV = CLIP(pV, -127,127);
1493 if (i % 2){
1494 *yuv++ = (219 * pY)/255 + 16; // Y
1495 *yuv++ = (112 * pU)/127 + 128; // U
1497 else {
1498 *yuv++ = (219 * pY)/255 + 16; // Y
1499 *yuv++ = (112 * pV)/127 + 128; // V
1502 b += stepx;
1505 // Skip the last pixel
1506 *yuv++ = 16;
1507 *yuv++ = 128;
1511 // Clean the last line
1512 memset(yuv, 128, nwidth * 2);
1513 for (i=0; i<nwidth*2; i=i+2, *(yuv+i)=16);