This is function in C to convert from Bayer to RGB24 pixel formats.
The linear interpolation is more efficient than the ones I saw in other
similar functions on internet, as the code omits "if-then-else" conditional
statements in the loops.

Best regards,
Luca Risolia
---

/***************************************************************************
 * Color convertion from Bayer to RGB24                                    *
 *                                                                         *
 * Copyright (C) Luca Risolia <[EMAIL PROTECTED]>               *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program 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 General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program; if not, write to the Free Software             *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               *
 ***************************************************************************/

/*
   bayer bgbg...grgr...to rgb24
*/

#define RED 2
#define GREEN 1
#define BLUE 0

#define avg2(x,y) ((((int)(x)) + ((int)(y))) / 2)
#define avg3(x,y,z) ((((int)(x)) + ((int)(y)) + ((int)(z))) / 3)
#define avg4(w,x,y,z) ((((int)(w)) + ((int)(x)) + ((int)(y)) + ((int)(z))) / 4)

void sbggr8_to_rgb24(unsigned char *s, unsigned int x, unsigned int y,
                     unsigned char *d)
{
        unsigned long int i, j;

        /* upper left corner */
        d[0 + RED] = s[x + 1];
        d[0 + BLUE] = s[0];
        d[0 + GREEN] = avg2(s[1], s[x]);

        /* top line (minus corners) */
        i = 1;
        while (i < x - 2) {
                d[i * 3 + RED] = s[x + i];
                d[i * 3 + GREEN] = s[i];
                d[i * 3 + BLUE] = avg2(s[i - 1], s[i + 1]);

                i++;

                d[i * 3 + RED] = avg2(s[x + i - 1], s[x + i + 1]);
                d[i * 3 + GREEN] = avg3(s[i - 1], s[i + 1], s[x + i]);
                d[i * 3 + BLUE] = s[i];

                i++;
        }

        /* upper right corner */
        d[i * 3 + RED] = s[x + i];
        d[i * 3 + GREEN] = s[i];
        d[i * 3 + BLUE] = s[i - 1];

        /* middle lines */
        j = 1;
        while (j < y - 2) {
                d[j * x * 3 + RED] = s[j * x + 1];
                d[j * x * 3 + GREEN] = s[j * x];
                d[j * x * 3 + BLUE] = avg2(s[(j - 1) * x], s[(j + 1) * x]);

                i = 1;
                while (i < x - 2) {
                        d[(j * x + i) * 3 + RED] = s[j * x + i];
                        d[(j * x + i) * 3 + GREEN] = avg4(s[(j - 1) * x + i],
                                                          s[j * x + i - 1],
                                                          s[j * x + i + 1],
                                                          s[(j + 1) * x + i]);
                        d[(j * x + i) * 3 + BLUE] =
                                                  avg4(s[(j - 1) * x + i - 1],
                                                       s[(j - 1) * x + i + 1],
                                                       s[(j + 1) * x + i - 1],
                                                       s[(j + 1) * x + i + 1]);

                        i++;

                        d[(j * x + i) * 3 + RED] = avg2(s[j * x + i - 1],
                                                        s[j * x + i + 1]);
                        d[(j * x + i) * 3 + GREEN] = s[j * x + i];
                        d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i],
                                                         s[(j + 1) * x + i]);

                        i++;
                }

                d[(j * x + i) * 3 + RED] = s[j * x + i];
                d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i - 1],
                                                 s[(j + 1) * x + i - 1]);
                d[(j * x + i) * 3 + GREEN] = avg3(s[(j - 1) * x + i],
                                                  s[(j + 1) * x + i],
                                                  s[j * x + i - 1]);

                j++;

                d[j * x * 3 + RED] = avg2(s[(j - 1) * x + 1],
                                          s[(j + 1) * x + 1]);
                d[j * x * 3 + BLUE] = s[j * x];
                d[j * x * 3 + GREEN] = avg3(s[(j - 1) * x],
                                            s[j * x + 1],
                                            s[(j + 1) * x]);

                i = 1;
                while (i < x - 2) {
                        d[(j * x + i) * 3 + RED] = avg2(s[(j - 1) * x + i],
                                                        s[(j + 1) * x + i]);
                        d[(j * x + i) * 3 + GREEN] = s[j * x + i];
                        d[(j * x + i) * 3 + BLUE] = avg2(s[j * x + i - 1],
                                                         s[j * x + i + 1]);

                        i++;

                        d[(j * x + i) * 3 + RED] = avg4(s[(j - 1) * x + i - 1],
                                                        s[(j - 1) * x + i + 1],
                                                        s[(j + 1) * x + i - 1],
                                                       s[(j + 1) * x + i + 1]);
                        d[(j * x + i) * 3 + GREEN] = avg4(s[(j - 1) * x + i],
                                                          s[j * x + i - 1],
                                                          s[j * x + i + 1],
                                                          s[(j + 1) * x + i]);
                        d[(j * x + i) * 3 + BLUE] = s[j * x + i];

                        i++;
                }

                d[(j * x + i) * 3 + RED] = avg2(s[(j - 1) * x + i],
                                                s[(j + 1) * x + i]);
                d[(j * x + i) * 3 + BLUE] = s[j * x + i - 1];
                d[(j * x + i) * 3 + GREEN] = s[j * x + i];

                j++;
        }

        /* lower left corner */
        d[(j * x) * 3 + RED] = s[j * x + 1];
        d[(j * x) * 3 + GREEN] = s[j * x];
        d[(j * x) * 3 + BLUE] = s[(j - 1) * x];

        /* bottom line */
        i = 1;
        while (i < x - 2) {
                d[(j * x + i) * 3 + RED] = s[j * x + i];
                d[(j * x + i) * 3 + GREEN] = avg3(s[(j - 1) * x + i],
                                                  s[j * x + i - 1],
                                                  s[j * x + i + 1]);
                d[(j * x + i) * 3 + BLUE] = avg2(s[(j - 1) * x + i - 1],
                                                 s[(j - 1) * x + i + 1]);

                i++;

                d[(j * x + i) * 3 + RED] = avg2(s[j * x + i - 1],
                                                s[j * x + i + 1]);
                d[(j * x + i) * 3 + GREEN] = s[j * x + i];
                d[(j * x + i) * 3 + BLUE] = s[(j - 1) * x + i];

                i++;
        }

        /* lower right corner */
        d[(j * x + i) * 3 + RED] = s[j * x + i];
        d[(j * x + i) * 3 + GREEN] = avg2(s[(j - 1) * x + i],
                                          s[j * x + i - 1]);
        d[(j * x + i) * 3 + BLUE] = s[(j - 1) * x + i - 1];
}
_______________________________________________
Linux-uvc-devel mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/linux-uvc-devel

Reply via email to