Skip to content

Instantly share code, notes, and snippets.

@agiletalk
Created April 3, 2011 21:51
Show Gist options
  • Save agiletalk/900858 to your computer and use it in GitHub Desktop.
Save agiletalk/900858 to your computer and use it in GitHub Desktop.
bayer pattern -> rgb (DIPS Homework #2 1.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 128
// Bayer pattern
char BAYER[2][2] = {
{'G', 'B'},
{'R', 'G'}
};
void bayer2rgb(int** bayer, int m, int n)
{
int i, j, sum[2] = {0, 0}, total[2] = {0, 0};
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
sum[0] = sum[1] = 0;
total[0] = total[1] = 0;
// Green
if(BAYER[i%2][j%2] == 'G')
{
// Red, Blue or Blue, Red
if(i-1 >= 0)
{
sum[0] = sum[0] + bayer[i-1][j];
total[0]++;
}
if(i+1 < m)
{
sum[0] = sum[0] + bayer[i+1][j];
total[0]++;
}
if(j-1 >= 0)
{
sum[1] = sum[1] + bayer[i][j-1];
total[1]++;
}
if(j+1 < n)
{
sum[1] = sum[1] + bayer[i][j+1];
total[1]++;
}
// Print
if(i%2 == 0)
{
printf("[%3d %3d %3d]\t", sum[0]/total[0], bayer[i][j], sum[1]/total[1]);
}
else
{
printf("[%3d %3d %3d]\t", sum[1]/total[1], bayer[i][j], sum[0]/total[0]);
}
}
// Red or Blue
if(BAYER[i%2][j%2] == 'R' || BAYER[i%2][j%2] == 'B')
{
// Green
if(i-1 >= 0)
{
sum[0] = sum[0] + bayer[i-1][j];
total[0]++;
}
if(i+1 < m)
{
sum[0] = sum[0] + bayer[i+1][j];
total[0]++;
}
if(j-1 >= 0)
{
sum[0] = sum[0] + bayer[i][j-1];
total[0]++;
}
if(j+1 < n)
{
sum[0] = sum[0] + bayer[i][j+1];
total[0]++;
}
// Blue or Red
if(i-1 >= 0 && j-1 >= 0)
{
sum[1] = sum[1] + bayer[i-1][j-1];
total[1]++;
}
if(i-1 >= 0 && j+1 < n)
{
sum[1] = sum[1] + bayer[i-1][j+1];
total[1]++;
}
if(i+1 < m && j-1 >= 0)
{
sum[1] = sum[1] + bayer[i+1][j-1];
total[1]++;
}
if(i+1 < m && j+1 < n)
{
sum[1] = sum[1] + bayer[i+1][j+1];
total[1]++;
}
// Print
if(BAYER[i%2][j%2] == 'R')
{
printf("[%3d %3d %3d]\t", bayer[i][j], sum[0]/total[0], sum[1]/total[1]);
}
else
{
printf("[%3d %3d %3d]\t", sum[1]/total[1], sum[0]/total[0], bayer[i][j]);
}
}
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
char buf[BUFFER];
char* ptr;
int i, j, m, n;
int** input;
m = atoi(argv[1]);
n = atoi(argv[2]);
// malloc
input = (int**)malloc(sizeof(int) * m);
for(i = 0; i < m; i++)
{
input[i] = (int*)malloc(sizeof(int) * n);
}
// bayer2rgb [no. of rows] [no. of columns] <return>
// for instance, bayer2rgb 3 4
if(argc != 3)
{
printf("usage: %s [no. of rows] [no. of columns]\n", argv[0]);
printf("\tfor instance, %s 3 4\n", argv[0]);
return -1;
}
// input array
for(i = 0; i < m; i++)
{
j = 0;
gets(buf);
ptr = strtok(buf, " ");
input[i][j++] = atoi(ptr);
while(ptr = strtok(NULL, " "))
{
input[i][j++] = atoi(ptr);
}
}
// bayer pattern -> rgb
bayer2rgb(input, m, n);
// free
for(i = 0; i < m; i++)
{
free(input[i]);
}
free(input);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment