aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/source/GImage_bayer.cpp
blob: 3d08e8ade5f76c032ff55a6e12f094f38b6d6cfa (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
/**
  @file GImage_bayer.cpp
  @author Morgan McGuire, http://graphics.cs.williams.edu
  @created 2002-05-27
  @edited  2006-05-10
 */
#include "G3D/platform.h"
#include "G3D/GImage.h"

namespace G3D {

void GImage::BAYER_G8B8_R8G8_to_Quarter_R8G8B8(int width, int height, const uint8* in, uint8* out) {
    debugAssert(in != out);

    int halfHeight = height / 2;
    int halfWidth  = width / 2;

    int dst_off = 0;
    for (int y = 0; y < halfHeight; ++y) {
        for (int x = 0; x < halfWidth; ++x) {
            // GBRG
            int src_off = x*2 + y*2*width;
            out[dst_off] = in[src_off+width]; // red
            out[dst_off+1] = ((int)in[src_off] + (int)in[src_off+width+1])/2; // green
            out[dst_off+2] = in[src_off+1]; // blue            

            dst_off = dst_off + 3;
        }
    }
}


void GImage::Quarter_R8G8B8_to_BAYER_G8B8_R8G8(int inWidth, int inHeight, const uint8* in, uint8* out) {
    // Undo quarter-size Bayer as best we can.  This code isn't very efficient, but it
    // also isn't used very frequently.

    debugAssert(out != in);

    int outWidth  = 2 * inWidth;
    int outHeight = 2 * inHeight;

    for (int y = 0; y < outHeight; ++y) {
        for (int x = 0; x < outWidth; ++x) {
            const Color3uint8* inp = ((const Color3uint8*)in) + ((x/2) + (y/2)* inWidth);
            uint8* outp = out + x + y * outWidth;

            if (isEven(y)) {
                // GB row
                if (isEven(x)) {
                    // Green
                    *outp = inp->g;
                } else {
                    // Blue
                    *outp = inp->b;
                }
            } else {
                // RG row
                if (isEven(x)) {
                    // Red
                    *outp = inp->r;
                } else {
                    // Green
                    *outp = inp->g;
                }
            }
        }
    }
}


/** Applies a 5x5 filter to monochrome image I (wrapping at the boundaries) */
static uint8 applyFilter(
    const uint8*    I,
    int             x,
    int             y,
    int             w,
    int             h,
    const float     filter[5][5]) {

    debugAssert(isEven(w));
    debugAssert(isEven(h));

    float sum = 0.0f;
    float denom = 0.0f;

    for (int dy = 0; dy < 5; ++dy) {
        int offset = ((y + dy + h - 2) % h) * w;

        for (int dx = 0; dx < 5; ++dx) {
            float f = filter[dy][dx];
            sum += f * I[((x + dx + w - 2) % w) + offset];
            denom += f;
        }
    }

    return (uint8)iClamp(iRound(sum / denom), 0, 255);
}

////////////////////////////////////////////////////////////////////////////////////////////////
//
// Bayer conversions
//

// There are two kinds of rows (GR and BG).
// In each row, there are two kinds of pixels (G/R, B/G).
// We express the four kinds of INPUT pixels as:
//    GRG, GRG, BGB, BGG
//
// There are three kinds of OUTPUT pixels: R, G, B.
// Thus there are nominally 12 different I/O combinations, 
// but several are impulses because needed output at that 
// location *is* the input (e.g., G_GRG and G_BGG).
//
// The following 5x5 row-major filters are named as output_input.

// Green
static const float G_GRR[5][5] =
{{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f},
{     0.0f,      0.0f,      2.0f,      0.0f,      0.0f},
{    -1.0f,      2.0f,      4.0f,      2.0f,     -1.0f},
{     0.0f,      0.0f,      2.0f,      0.0f,      0.0f},
{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f}};

static const float G_BGB[5][5] =
{{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f},
{     0.0f,      0.0f,      2.0f,      0.0f,      0.0f},
{    -1.0f,      2.0f,      4.0f,      2.0f,     -1.0f},
{     0.0f,      0.0f,      2.0f,      0.0f,      0.0f},
{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f}};

// Red 
//(the caption in the paper is wrong for this case:
// "R row B column really means R row G column"
static const float R_GRG[5][5] =
{{     0.0f,      0.0f,      0.5f,      0.0f,      0.0f},
{     0.0f,     -1.0f,      0.0f,     -1.0f,      0.0f},
{    -1.0f,      4.0f,      5.0f,      4.0f,     -1.0f},
{     0.0f,     -1.0f,      0.0f,     -1.0f,      0.0f},
{     0.0f,      0.0f,      0.5f,      0.0f,      0.0f}};

static const float R_BGG[5][5] =
{{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f},
{     0.0f,     -1.0f,      4.0f,     -1.0f,      0.0f},
{     0.5f,      0.0f,      5.0f,      0.0f,      0.5f},
{     0.0f,     -1.0f,      4.0f,     -1.0f,      0.0f},
{     0.0f,      0.0f,     -1.0f,      0.0f,      0.0f}};

static const float R_BGB[5][5] =
{{     0.0f,      0.0f, -3.0f/2.0f,      0.0f,      0.0f},
{     0.0f,      2.0f,      0.0f,      2.0f,      0.0f},
{-3.0f/2.0f,      0.0f,      6.0f,      0.0f, -3.0f/2.0f},
{     0.0f,      2.0f,      0.0f,      2.0f,      0.0f},
{     0.0f,      0.0f, -3.0f/2.0f,      0.0f,      0.0f}};


// Blue 
//(the caption in the paper is wrong for this case:
// "B row R column really means B row G column")
#define B_BGG R_GRG
#define B_GRG R_BGG
#define B_GRR R_BGB


void GImage::BAYER_R8G8_G8B8_to_R8G8B8_MHC(int w, int h, const uint8* in, uint8* _out) {
    debugAssert(in != _out);

    Color3uint8* out = (Color3uint8*)_out;

    for (int y = 0; y < h; ++y) {

        // Row beginning in the input array.
        int offset = y * w;

        // RG row
        for (int x = 0; x < w; ++x, ++out) {
            // R pixel
            {
                out->r = in[x + offset];
                out->g = applyFilter(in, x, y, w, h, G_GRR);
                out->b = applyFilter(in, x, y, w, h, B_GRR);
            }
            ++x; ++out;

            // G pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_GRG);
                out->g = in[x + offset];
                out->b = applyFilter(in, x, y, w, h, B_GRG);
            }
        }

        ++y;
        offset += w;

        // GB row
        for (int x = 0; x < w; ++x, ++out) {
            // G pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_BGG);
                out->g = in[x + offset];
                out->b = applyFilter(in, x, y, w, h, B_BGG);
            }
            ++x; ++out;

            // B pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_BGB);
                out->g = applyFilter(in, x, y, w, h, G_BGB);
                out->b = in[x + offset];
            }
        }
    }
}

static void swapRedAndBlue(int N, Color3uint8* out) {
    for (int i = N - 1; i >= 0; --i) {
        uint8 tmp = out[i].r;
        out[i].r = out[i].b;
        out[i].b = tmp;
    }
}

void GImage::BAYER_G8R8_B8G8_to_R8G8B8_MHC(int w, int h, const uint8* in, uint8* _out) {
    // Run the equivalent function for red
    BAYER_G8B8_R8G8_to_R8G8B8_MHC(w, h, in, _out);

    // Now swap red and blue
    swapRedAndBlue(w * h, (Color3uint8*)_out);
}


void GImage::BAYER_B8G8_G8R8_to_R8G8B8_MHC(int w, int h, const uint8* in, uint8* _out) {
    // Run the equivalent function for red
    BAYER_R8G8_G8B8_to_R8G8B8_MHC(w, h, in, _out);

    // Now swap red and blue
    swapRedAndBlue(w * h, (Color3uint8*)_out);
}


void GImage::BAYER_G8B8_R8G8_to_R8G8B8_MHC(int w, int h, const uint8* in, uint8* _out) {

    debugAssert(in != _out);

    Color3uint8* out = (Color3uint8*)_out;

    for (int y = 0; y < h; ++y) {

        // Row beginning in the input array.
        int offset = y * w;

        // GB row
        for (int x = 0; x < w; ++x, ++out) {
            // G pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_BGG);
                out->g = in[x + offset];
                out->b = applyFilter(in, x, y, w, h, B_BGG);
            }
            ++x; ++out;

            // B pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_BGB);
                out->g = applyFilter(in, x, y, w, h, G_BGB);
                out->b = in[x + offset];
            }
        }

        ++y;
        offset += w;

        // RG row
        for (int x = 0; x < w; ++x, ++out) {
            // R pixel
            {
                out->r = in[x + offset];
                out->g = applyFilter(in, x, y, w, h, G_GRR);
                out->b = applyFilter(in, x, y, w, h, B_GRR);
            }
            ++x; ++out;

            // G pixel
            {
                out->r = applyFilter(in, x, y, w, h, R_GRG);
                out->g = in[x + offset];
                out->b = applyFilter(in, x, y, w, h, B_GRG);
            }
        }
    }

}

#undef B_BGG
#undef B_GRG
#undef B_GRR

}