summaryrefslogtreecommitdiff
path: root/v4l2-apps/libv4l/libv4lconvert/bayer.c
blob: d299629a66a88d5bff62028071c423296cdd7dd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 * lib4lconvert, video4linux2 format conversion lib
 *             (C) 2008 Hans de Goede <hdegoede@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Note: original bayer_to_bgr24 code from :
 * 1394-Based Digital Camera Control Library
 *
 * Bayer pattern decoding functions
 *
 * Written by Damien Douxchamps and Frederic Devernay
 *
 * Note that the original bayer.c in libdc1394 supports many different
 * bayer decode algorithms, for lib4lconvert the one in this file has been
 * chosen (and optimized a bit) and the other algorithm's have been removed,
 * see bayer.c from libdc1394 for all supported algorithms
 */

#include <string.h>
#include "libv4lconvert-priv.h"

/**************************************************************
 *     Color conversion functions for cameras that can        *
 * output raw-Bayer pattern images, such as some Basler and   *
 * Point Grey camera. Most of the algos presented here come   *
 * from http://www-ise.stanford.edu/~tingchen/ and have been  *
 * converted from Matlab to C and extended to all elementary  *
 * patterns.                                                  *
 **************************************************************/

/* insprired by OpenCV's Bayer decoding */
static void v4lconvert_border_bayer_line_to_bgr24(
  const unsigned char* bayer, const unsigned char* adjacent_bayer,
  unsigned char *bgr, int width, int start_with_green, int blue_line)
{
  int t0, t1;

  if (start_with_green) {
    /* First pixel */
    if (blue_line) {
      *bgr++ = bayer[1];
      *bgr++ = bayer[0];
      *bgr++ = adjacent_bayer[0];
    } else {
      *bgr++ = adjacent_bayer[0];
      *bgr++ = bayer[0];
      *bgr++ = bayer[1];
    }
    /* Second pixel */
    t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;
    t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;
    if (blue_line) {
      *bgr++ = bayer[1];
      *bgr++ = t0;
      *bgr++ = t1;
    } else {
      *bgr++ = t1;
      *bgr++ = t0;
      *bgr++ = bayer[1];
    }
    bayer++;
    adjacent_bayer++;
    width -= 2;
  } else {
    /* First pixel */
    t0 = (bayer[1] + adjacent_bayer[0] + 1) >> 1;
    if (blue_line) {
      *bgr++ = bayer[0];
      *bgr++ = t0;
      *bgr++ = adjacent_bayer[1];
    } else {
      *bgr++ = adjacent_bayer[1];
      *bgr++ = t0;
      *bgr++ = bayer[0];
    }
    width--;
  }

  if (blue_line) {
    for ( ; width > 2; width -= 2) {
      t0 = (bayer[0] + bayer[2] + 1) >> 1;
      *bgr++ = t0;
      *bgr++ = bayer[1];
      *bgr++ = adjacent_bayer[1];
      bayer++;
      adjacent_bayer++;

      t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;
      t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;
      *bgr++ = bayer[1];
      *bgr++ = t0;
      *bgr++ = t1;
      bayer++;
      adjacent_bayer++;
    }
  } else {
    for ( ; width > 2; width -= 2) {
      t0 = (bayer[0] + bayer[2] + 1) >> 1;
      *bgr++ = adjacent_bayer[1];
      *bgr++ = bayer[1];
      *bgr++ = t0;
      bayer++;
      adjacent_bayer++;

      t0 = (bayer[0] + bayer[2] + adjacent_bayer[1] + 1) / 3;
      t1 = (adjacent_bayer[0] + adjacent_bayer[2] + 1) >> 1;
      *bgr++ = t1;
      *bgr++ = t0;
      *bgr++ = bayer[1];
      bayer++;
      adjacent_bayer++;
    }
  }

  if (width == 2) {
    /* Second to last pixel */
    t0 = (bayer[0] + bayer[2] + 1) >> 1;
    if (blue_line) {
      *bgr++ = t0;
      *bgr++ = bayer[1];
      *bgr++ = adjacent_bayer[1];
    } else {
      *bgr++ = adjacent_bayer[1];
      *bgr++ = bayer[1];
      *bgr++ = t0;
    }
    /* Last pixel */
    t0 = (bayer[1] + adjacent_bayer[2] + 1) >> 1;
    if (blue_line) {
      *bgr++ = bayer[2];
      *bgr++ = t0;
      *bgr++ = adjacent_bayer[1];
    } else {
      *bgr++ = adjacent_bayer[1];
      *bgr++ = t0;
      *bgr++ = bayer[2];
    }
  } else {
    /* Last pixel */
    if (blue_line) {
      *bgr++ = bayer[0];
      *bgr++ = bayer[1];
      *bgr++ = adjacent_bayer[1];
    } else {
      *bgr++ = adjacent_bayer[1];
      *bgr++ = bayer[1];
      *bgr++ = bayer[0];
    }
  }
}

/* From libdc1394, which on turn was based on OpenCV's Bayer decoding */
static void bayer_to_rgbbgr24(const unsigned char *bayer,
  unsigned char *bgr, int width, int height, unsigned int pixfmt,
	int start_with_green, int blue_line)
{
    /* render the first line */
    v4lconvert_border_bayer_line_to_bgr24(bayer, bayer + width, bgr, width,
      start_with_green, blue_line);
    bgr += width * 3;

    /* reduce height by 2 because of the special case top/bottom line */
    for (height -= 2; height; height--) {
	int t0, t1;
	/* (width - 2) because of the border */
	const unsigned char *bayerEnd = bayer + (width - 2);

	if (start_with_green) {
	    /* OpenCV has a bug in the next line, which was
	       t0 = (bayer[0] + bayer[width * 2] + 1) >> 1; */
	    t0 = (bayer[1] + bayer[width * 2 + 1] + 1) >> 1;
	    /* Write first pixel */
	    t1 = (bayer[0] + bayer[width * 2] + bayer[width + 1] + 1) / 3;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = t1;
	      *bgr++ = bayer[width];
	    } else {
	      *bgr++ = bayer[width];
	      *bgr++ = t1;
	      *bgr++ = t0;
	    }

	    /* Write second pixel */
	    t1 = (bayer[width] + bayer[width + 2] + 1) >> 1;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = bayer[width + 1];
	      *bgr++ = t1;
	    } else {
	      *bgr++ = t1;
	      *bgr++ = bayer[width + 1];
	      *bgr++ = t0;
	    }
	    bayer++;
	} else {
	    /* Write first pixel */
	    t0 = (bayer[0] + bayer[width * 2] + 1) >> 1;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = bayer[width];
	      *bgr++ = bayer[width + 1];
	    } else {
	      *bgr++ = bayer[width + 1];
	      *bgr++ = bayer[width];
	      *bgr++ = t0;
	    }
	}

	if (blue_line) {
	    for (; bayer <= bayerEnd - 2; bayer += 2) {
		t0 = (bayer[0] + bayer[2] + bayer[width * 2] +
		      bayer[width * 2 + 2] + 2) >> 2;
		t1 = (bayer[1] + bayer[width] +
		      bayer[width + 2] + bayer[width * 2 + 1] +
		      2) >> 2;
		*bgr++ = t0;
		*bgr++ = t1;
		*bgr++ = bayer[width + 1];

		t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;
		t1 = (bayer[width + 1] + bayer[width + 3] +
		      1) >> 1;
		*bgr++ = t0;
		*bgr++ = bayer[width + 2];
		*bgr++ = t1;
	    }
	} else {
	    for (; bayer <= bayerEnd - 2; bayer += 2) {
		t0 = (bayer[0] + bayer[2] + bayer[width * 2] +
		      bayer[width * 2 + 2] + 2) >> 2;
		t1 = (bayer[1] + bayer[width] +
		      bayer[width + 2] + bayer[width * 2 + 1] +
		      2) >> 2;
		*bgr++ = bayer[width + 1];
		*bgr++ = t1;
		*bgr++ = t0;

		t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;
		t1 = (bayer[width + 1] + bayer[width + 3] +
		      1) >> 1;
		*bgr++ = t1;
		*bgr++ = bayer[width + 2];
		*bgr++ = t0;
	    }
	}

	if (bayer < bayerEnd) {
	    /* write second to last pixel */
	    t0 = (bayer[0] + bayer[2] + bayer[width * 2] +
		  bayer[width * 2 + 2] + 2) >> 2;
	    t1 = (bayer[1] + bayer[width] +
		  bayer[width + 2] + bayer[width * 2 + 1] +
		  2) >> 2;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = t1;
	      *bgr++ = bayer[width + 1];
	    } else {
	      *bgr++ = bayer[width + 1];
	      *bgr++ = t1;
	      *bgr++ = t0;
	    }
	    /* write last pixel */
	    t0 = (bayer[2] + bayer[width * 2 + 2] + 1) >> 1;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = bayer[width + 2];
	      *bgr++ = bayer[width + 1];
	    } else {
	      *bgr++ = bayer[width + 1];
	      *bgr++ = bayer[width + 2];
	      *bgr++ = t0;
	    }
	    bayer++;
	} else {
	    /* write last pixel */
	    t0 = (bayer[0] + bayer[width * 2] + 1) >> 1;
	    t1 = (bayer[1] + bayer[width * 2 + 1] + bayer[width] + 1) / 3;
	    if (blue_line) {
	      *bgr++ = t0;
	      *bgr++ = t1;
	      *bgr++ = bayer[width + 1];
	    } else {
	      *bgr++ = bayer[width + 1];
	      *bgr++ = t1;
	      *bgr++ = t0;
	    }
	}

	/* skip 2 border pixels */
	bayer += 2;

	blue_line = !blue_line;
	start_with_green = !start_with_green;
    }

    /* render the last line */
    v4lconvert_border_bayer_line_to_bgr24(bayer + width, bayer, bgr, width,
      !start_with_green, !blue_line);
}

void v4lconvert_bayer_to_rgb24(const unsigned char *bayer,
  unsigned char *bgr, int width, int height, unsigned int pixfmt)
{
	bayer_to_rgbbgr24(bayer, bgr, width, height, pixfmt,
		pixfmt == V4L2_PIX_FMT_SGBRG8		/* start with green */
			|| pixfmt == V4L2_PIX_FMT_SGRBG8,
		pixfmt != V4L2_PIX_FMT_SBGGR8		/* blue line */
			&& pixfmt != V4L2_PIX_FMT_SGBRG8);
}

void v4lconvert_bayer_to_bgr24(const unsigned char *bayer,
  unsigned char *bgr, int width, int height, unsigned int pixfmt)
{
	bayer_to_rgbbgr24(bayer, bgr, width, height, pixfmt,
		pixfmt == V4L2_PIX_FMT_SGBRG8		/* start with green */
			|| pixfmt == V4L2_PIX_FMT_SGRBG8,
		pixfmt == V4L2_PIX_FMT_SBGGR8		/* blue line */
			|| pixfmt == V4L2_PIX_FMT_SGBRG8);
}

static void v4lconvert_border_bayer_line_to_y(
  const unsigned char* bayer, const unsigned char* adjacent_bayer,
  unsigned char *y, int width, int start_with_green, int blue_line)
{
  int t0, t1;

  if (start_with_green) {
    /* First pixel */
    if (blue_line) {
      *y++ = (8453*adjacent_bayer[0] + 16594*bayer[0] + 3223*bayer[1] + 524288)
	     >> 15;
    } else {
      *y++ = (8453*bayer[1] + 16594*bayer[0] + 3223*adjacent_bayer[0] + 524288)
	     >> 15;
    }
    /* Second pixel */
    t0 = bayer[0] + bayer[2] + adjacent_bayer[1];
    t1 = adjacent_bayer[0] + adjacent_bayer[2];
    if (blue_line) {
      *y++ = (4226*t1 + 5531*t0 + 3223*bayer[1] + 524288) >> 15;
    } else {
      *y++ = (8453*bayer[1] + 5531*t0 + 1611*t1 + 524288) >> 15;
    }
    bayer++;
    adjacent_bayer++;
    width -= 2;
  } else {
    /* First pixel */
    t0 = bayer[1] + adjacent_bayer[0];
    if (blue_line) {
      *y++ = (8453*adjacent_bayer[1] + 8297*t0 + 3223*bayer[0] + 524288)
	     >> 15;
    } else {
      *y++ = (8453*bayer[0] + 8297*t0 + 3223*adjacent_bayer[1] + 524288)
	     >> 15;
    }
    width--;
  }

  if (blue_line) {
    for ( ; width > 2; width -= 2) {
      t0 = bayer[0] + bayer[2];
      *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 1611*t0 + 524288)
	     >> 15;
      bayer++;
      adjacent_bayer++;

      t0 = bayer[0] + bayer[2] + adjacent_bayer[1];
      t1 = adjacent_bayer[0] + adjacent_bayer[2];
      *y++ = (4226*t1 + 5531*t0 + 3223*bayer[1] + 524288) >> 15;
      bayer++;
      adjacent_bayer++;
    }
  } else {
    for ( ; width > 2; width -= 2) {
      t0 = bayer[0] + bayer[2];
      *y++ = (4226*t0 + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288)
	     >> 15;
      bayer++;
      adjacent_bayer++;

      t0 = bayer[0] + bayer[2] + adjacent_bayer[1];
      t1 = adjacent_bayer[0] + adjacent_bayer[2];
      *y++ = (8453*bayer[1] + 5531*t0 + 1611*t1 + 524288) >> 15;
      bayer++;
      adjacent_bayer++;
    }
  }

  if (width == 2) {
    /* Second to last pixel */
    t0 = bayer[0] + bayer[2];
    if (blue_line) {
      *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 1611*t0 + 524288)
	     >> 15;
    } else {
      *y++ = (4226*t0 + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288)
	     >> 15;
    }
    /* Last pixel */
    t0 = bayer[1] + adjacent_bayer[2];
    if (blue_line) {
      *y++ = (8453*adjacent_bayer[1] + 8297*t0 + 3223*bayer[2] + 524288)
	     >> 15;
    } else {
      *y++ = (8453*bayer[2] + 8297*t0 + 3223*adjacent_bayer[1] + 524288)
	     >> 15;
    }
  } else {
    /* Last pixel */
    if (blue_line) {
      *y++ = (8453*adjacent_bayer[1] + 16594*bayer[1] + 3223*bayer[0] + 524288)
	     >> 15;
    } else {
      *y++ = (8453*bayer[0] + 16594*bayer[1] + 3223*adjacent_bayer[1] + 524288)
	     >> 15;
    }
  }
}

void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, unsigned char *yuv,
  int width, int height, unsigned int src_pixfmt, int yvu)
{
  int blue_line = 0, start_with_green = 0, x, y;
  unsigned char *ydst = yuv;
  unsigned char *udst, *vdst;

  if (yvu) {
    vdst = yuv + width * height;
    udst = vdst + width * height / 4;
  } else {
    udst = yuv + width * height;
    vdst = udst + width * height / 4;
  }

  /* First calculate the u and v planes 2x2 pixels at a time */
  switch (src_pixfmt) {
    case V4L2_PIX_FMT_SBGGR8:
      for (y = 0; y < height; y += 2) {
	for (x = 0; x < width; x += 2) {
	  int b, g, r;
	  b  = bayer[x];
	  g  = bayer[x+1];
	  g += bayer[x+width];
	  r  = bayer[x+width+1];
	  *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;
	  *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;
	}
	bayer += 2 * width;
      }
      blue_line = 1;
      break;

    case V4L2_PIX_FMT_SRGGB8:
      for (y = 0; y < height; y += 2) {
	for (x = 0; x < width; x += 2) {
	  int b, g, r;
	  r  = bayer[x];
	  g  = bayer[x+1];
	  g += bayer[x+width];
	  b  = bayer[x+width+1];
	  *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;
	  *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;
	}
	bayer += 2 * width;
      }
      break;

    case V4L2_PIX_FMT_SGBRG8:
      for (y = 0; y < height; y += 2) {
	for (x = 0; x < width; x += 2) {
	  int b, g, r;
	  g  = bayer[x];
	  b  = bayer[x+1];
	  r  = bayer[x+width];
	  g += bayer[x+width+1];
	  *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;
	  *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;
	}
	bayer += 2 * width;
      }
      blue_line = 1;
      start_with_green = 1;
      break;

    case V4L2_PIX_FMT_SGRBG8:
      for (y = 0; y < height; y += 2) {
	for (x = 0; x < width; x += 2) {
	  int b, g, r;
	  g  = bayer[x];
	  r  = bayer[x+1];
	  b  = bayer[x+width];
	  g += bayer[x+width+1];
	  *udst++ = (-4878 * r - 4789 * g + 14456 * b + 4210688) >> 15;
	  *vdst++ = (14456 * r - 6052 * g -  2351 * b + 4210688) >> 15;
	}
	bayer += 2 * width;
      }
      start_with_green = 1;
      break;
  }

  bayer -= width * height;

  /* render the first line */
  v4lconvert_border_bayer_line_to_y(bayer, bayer + width, ydst, width,
    start_with_green, blue_line);
  ydst += width;

  /* reduce height by 2 because of the border */
  for (height -= 2; height; height--) {
    int t0, t1;
    /* (width - 2) because of the border */
    const unsigned char *bayerEnd = bayer + (width - 2);

    if (start_with_green) {
      t0 = bayer[1] + bayer[width * 2 + 1];
      /* Write first pixel */
      t1 = bayer[0] + bayer[width * 2] + bayer[width + 1];
      if (blue_line)
	*ydst++ = (8453*bayer[width] + 5516*t1 + 1661*t0 + 524288) >> 15;
      else
	*ydst++ = (4226*t0 + 5516*t1 + 3223*bayer[width] + 524288) >> 15;

      /* Write second pixel */
      t1 = bayer[width] + bayer[width + 2];
      if (blue_line)
	*ydst++ = (4226*t1 + 16594*bayer[width+1] + 1611*t0 + 524288) >> 15;
      else
	*ydst++ = (4226*t0 + 16594*bayer[width+1] + 1611*t1 + 524288) >> 15;
      bayer++;
    } else {
      /* Write first pixel */
      t0 = bayer[0] + bayer[width * 2];
      if (blue_line) {
	*ydst++ = (8453*bayer[width+1] + 16594*bayer[width] + 1661*t0 +
		   524288) >> 15;
      } else {
	*ydst++ = (4226*t0 + 16594*bayer[width] + 3223*bayer[width+1] +
		   524288) >> 15;
      }
    }

    if (blue_line) {
      for (; bayer <= bayerEnd - 2; bayer += 2) {
	t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];
	t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];
	*ydst++ = (8453*bayer[width+1] + 4148*t1 + 806*t0 + 524288) >> 15;

	t0 = bayer[2] + bayer[width * 2 + 2];
	t1 = bayer[width + 1] + bayer[width + 3];
	*ydst++ = (4226*t1 + 16594*bayer[width+2] + 1611*t0 + 524288) >> 15;
      }
    } else {
      for (; bayer <= bayerEnd - 2; bayer += 2) {
	t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];
	t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];
	*ydst++ = (2113*t0 + 4148*t1 + 3223*bayer[width+1] + 524288) >> 15;

	t0 = bayer[2] + bayer[width * 2 + 2];
	t1 = bayer[width + 1] + bayer[width + 3];
	*ydst++ = (4226*t0 + 16594*bayer[width+2] + 1611*t1 + 524288) >> 15;
      }
    }

    if (bayer < bayerEnd) {
      /* Write second to last pixel */
      t0 = bayer[0] + bayer[2] + bayer[width * 2] + bayer[width * 2 + 2];
      t1 = bayer[1] + bayer[width] + bayer[width + 2] + bayer[width * 2 + 1];
      if (blue_line)
	*ydst++ = (8453*bayer[width+1] + 4148*t1 + 806*t0 + 524288) >> 15;
      else
	*ydst++ = (2113*t0 + 4148*t1 + 3223*bayer[width+1] + 524288) >> 15;

      /* write last pixel */
      t0 = bayer[2] + bayer[width * 2 + 2];
      if (blue_line) {
	*ydst++ = (8453*bayer[width+1] + 16594*bayer[width+2] + 1661*t0 +
		   524288) >> 15;
      } else {
	*ydst++ = (4226*t0 + 16594*bayer[width+2] + 3223*bayer[width+1] +
		   524288) >> 15;
      }
      bayer++;
    } else {
      /* write last pixel */
      t0 = bayer[0] + bayer[width * 2];
      t1 = bayer[1] + bayer[width * 2 + 1] + bayer[width];
      if (blue_line)
	*ydst++ = (8453*bayer[width+1] + 5516*t1 + 1661*t0 + 524288) >> 15;
      else
	*ydst++ = (4226*t0 + 5516*t1 + 3223*bayer[width+1] + 524288) >> 15;
    }

    /* skip 2 border pixels */
    bayer += 2;

    blue_line = !blue_line;
    start_with_green = !start_with_green;
  }

  /* render the last line */
  v4lconvert_border_bayer_line_to_y(bayer + width, bayer, ydst, width,
    !start_with_green, !blue_line);
}