#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define BUFLEN 512
#define WIDTH 356
#define HEIGHT 292


/* also quickcam seems to parse bayer data,
   http://cvs.sourceforge.net/viewcvs.py/qce-ga/quickcam/hdcs.c?rev=1.18&view=markup */

/* Conversion tables ripped from linux-2.4.31/drivers/usb/stv680.h */

unsigned char red[256] = { 
	0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 
	18, 18, 18, 18, 18, 18, 18, 25, 30, 35, 38, 42, 
	44, 47, 50, 53, 54, 57, 59, 61, 63, 65, 67, 69, 
	71, 71, 73, 75, 77, 78, 80, 81, 82, 84, 85, 87, 
	88, 89, 90, 91, 93, 94, 95, 97, 98, 98, 99, 101, 
	102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 
	114, 115, 116, 116, 117, 118, 119, 120, 121, 122, 123, 124, 
	125, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133, 134, 
	134, 135, 135, 136, 137, 138, 139, 140, 140, 141, 142, 143, 
	143, 143, 144, 145, 146, 147, 147, 148, 149, 150, 150, 151, 
	152, 152, 152, 153, 154, 154, 155, 156, 157, 157, 158, 159, 
	159, 160, 161, 161, 161, 162, 163, 163, 164, 165, 165, 166, 
	167, 167, 168, 168, 169, 170, 170, 170, 171, 171, 172, 173, 
	173, 174, 174, 175, 176, 176, 177, 178, 178, 179, 179, 179, 
	180, 180, 181, 181, 182, 183, 183, 184, 184, 185, 185, 186, 
	187, 187, 188, 188, 188, 188, 189, 190, 190, 191, 191, 192, 
	192, 193, 193, 194, 195, 195, 196, 196, 197, 197, 197, 197, 
	198, 198, 199, 199, 200, 201, 201, 202, 202, 203, 203, 204, 
	204, 205, 205, 206, 206, 206, 206, 207, 207, 208, 208, 209, 
	209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 
	215, 215, 215, 216, 216, 217, 217, 218, 218, 218, 219, 219, 
	220, 220, 221, 221 
}; 

unsigned char green[256] = {
	0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
	21, 21, 21, 21, 21, 21, 21, 28, 34, 39, 43, 47, 
	50, 53, 56, 59, 61, 64, 66, 68, 71, 73, 75, 77, 
	79, 80, 82, 84, 86, 87, 89, 91, 92, 94, 95, 97, 
	98, 100, 101, 102, 104, 105, 106, 108, 109, 110, 111, 113, 
	114, 115, 116, 117, 118, 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, 144, 145, 146, 147, 148, 149, 
	150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159, 
	160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168, 
	169, 170, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177, 
	177, 178, 179, 179, 180, 181, 182, 182, 183, 184, 184, 185, 
	186, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193, 
	193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200, 
	201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 
	208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 
	214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 
	221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 
	227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 
	233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239, 
	239, 240, 240, 241, 241, 242, 242, 243, 243, 243, 244, 244, 
	245, 245, 246, 246 
}; 

unsigned char blue[256] = {
	0, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 
	23, 23, 23, 23, 23, 23, 23, 30, 37, 42, 47, 51, 
	55, 58, 61, 64, 67, 70, 72, 74, 78, 80, 82, 84, 
	86, 88, 90, 92, 94, 95, 97, 100, 101, 103, 104, 106, 
	107, 110, 111, 112, 114, 115, 116, 118, 119, 121, 122, 124, 
	125, 126, 127, 128, 129, 132, 133, 134, 135, 136, 137, 138, 
	139, 140, 141, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
	152, 154, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163, 
	165, 166, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 
	176, 176, 177, 178, 179, 180, 180, 181, 182, 183, 183, 184, 
	185, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194, 
	194, 195, 196, 196, 198, 199, 200, 200, 201, 202, 202, 203, 
	204, 204, 205, 205, 206, 207, 207, 209, 210, 210, 211, 212, 
	212, 213, 213, 214, 215, 215, 216, 217, 217, 218, 218, 220, 
	221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 
	228, 228, 229, 229, 231, 231, 232, 233, 233, 234, 234, 235, 
	235, 236, 236, 237, 238, 238, 239, 239, 240, 240, 242, 242, 
	243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249, 
	249, 250, 250, 251, 251, 253, 253, 254, 254, 255, 255, 255, 
	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
	255, 255, 255, 255 
}; 


/*******  routines from the pencam program; hey, they work!  ********/

/*
 * STV0680 Vision Camera Chipset Driver
 * Copyright (C) 2000 Adam Harrison <adam@antispin.org> 
*/

#define RED 0
#define GREEN 1
#define BLUE 2
#define AD(x, y, w) (((y)*(w)+(x))*3)

void bayer_unshuffle (unsigned char *input2, unsigned char *output2)
{
	int x, y, i;
	int w = WIDTH;
	int vw = WIDTH, vh = HEIGHT;
	unsigned int p = 0;
	int colour = 0, bayer = 0;
	unsigned char *raw = input2;
	unsigned char *output = output2;

	memset (output, 0, 3 * vw * vh);	/* clear output matrix. */

	for (y = 0; y < vh; y++) {
		for (x = 0; x < vw; x++) {
			if (x & 1)
				p = *(raw + y * w + (x >> 1));
			else
				p = *(raw + y * w + (x >> 1) + (w >> 1));

			if (y & 1)
				bayer = 2;
			else
				bayer = 0;
			if (x & 1)
				bayer++;

			switch (bayer) {
			case 0:
			case 3:
				colour = 1;
				break;
			case 1:
				colour = 0;
				break;
			case 2:
				colour = 2;
				break;
			}
			i = (y * vw + x) * 3;
			*(output + i + colour) = (unsigned char) p;
		}		/* for x */

	}			/* for y */

#if 0	/* average data from two different types of green sensors */
	for (y = 0; y < vh-1; y+=2) {
		for (x = 0; x < vw-1; x+=2) {
			unsigned char *g1, *g2, average;
			g1 = output + (y * vw + x) * 3 + 1;
			g2 = output + ((y+1) * vw + (x+1)) * 3 + 1;
			average = (*g1 + *g2) / 2;
			*g1 = average;
			*g2 = average;
		}
	}
#endif
	

#if 0 	/* DEBUG color green color sensors */
	for (y=0; y<260; y+=16) {
		for (x=124; x<vw-2; x+= 2) {
			fprintf(stderr, "%d %d\n",
				*(output + (y * vw + x) * 3 + 1),
				*(output + ((y+1) * vw + (x+1)) * 3 + 1));
		}
	}
#endif

#if 1
	/****** gamma correction plus hardcoded white balance */
	/* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
	   Correction values red[], green[], blue[], are generated by 
	   (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255. 
	   White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and 
	   converted to unsigned char. Values are in stv680.h  */

	for (y = 0; y < vh; y++) {
		for (x = 0; x < vw; x++) {
			i = (y * vw + x) * 3;
			*(output + i) = red[*(output + i)];
			*(output + i + 1) = green[*(output + i + 1)];
			*(output + i + 2) = blue[*(output + i + 2)];
		}
	}

	/******  bayer demosaic  ******/
	for (y = 1; y < (vh - 1); y++) {
		for (x = 1; x < (vw - 1); x++) {	/* work out pixel type */
			if (y & 1)
				bayer = 0;
			else
				bayer = 2;
			if (!(x & 1))
				bayer++;

			switch (bayer) {
			case 0:	/* green. blue lr, red tb */
				*(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
				*(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
				break;

			case 1:	/* blue. green lrtb, red diagonals */
				*(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
				*(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
				break;

			case 2:	/* red. green lrtb, blue diagonals */
				*(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
				*(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
				break;

			case 3:	/* green. red lr, blue tb */
				*(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
				*(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
				break;
			}	/* switch */
		}		/* for x */
	}			/* for y  - end demosaic  */

	/* fix top and bottom row, left and right side */
	i = vw * 3;
	memcpy (output, (output + i), i);
	memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
	for (y = 0; y < vh; y++) {
		i = y * vw * 3;
		memcpy ((output + i), (output + i + 3), 3);
		memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
	}
#endif
}				/* bayer_unshuffle */

/*******  end routines from the pencam program  *********/

int main(int argc, char **argv) {
	unsigned char input[WIDTH*HEIGHT];
	unsigned char output[WIDTH*HEIGHT*3];

	for(;;) {
		if (fread(input, WIDTH*HEIGHT, 1, stdin) != 1)
			break;
		bayer_unshuffle(input, output);
		printf("P6\n%d %d\n255\n", WIDTH, HEIGHT);
		fwrite(output, WIDTH*HEIGHT*3, 1, stdout);
	}
	return 0;
}
