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

#define WIDTH 356
#define HEIGHT 292

unsigned char input[WIDTH*HEIGHT];
unsigned char output[WIDTH*HEIGHT*3];

/* Conversion tables ripped from linux-2.4.31/drivers/usb/stv680.h */
/* also quickcam seems to parse bayer data,
   http://cvs.sourceforge.net/viewcvs.py/qce-ga/quickcam/hdcs.c?rev=1.18&view=markup */
const unsigned char const 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 
}; 

const unsigned char const 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 
}; 

const unsigned char const 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

/*
GRGRGRGRGRGRGRGRGRGRGRGRGRGRGR ... ..RGRGR
BGbGBGBGBGBGBGBGBGBGBGBGBGBGBG ... ..GBGBG
GRGRGRGRGRGRGRGRGRGRGRGRGRGRGR
BGBGBGBGBGBGBGBGBGBGBGBGBGBGBG
GRGRGRGRGRGRGRGRGRGRGRGRGRGRGR
BGBGBGBGBGBGBGBGBGBGBGBGBGBGBG
GRGRGRGRGRGRGRGRGRGRGRGRGRGRGR
BGBGBGBGBGBGBGBGBGBGBGBGBGBGBG
GRGRGRGRGRGRGRGRGRGRGRGRGRGRGR
...
GRGRGRGRGRGRGRGRGR
BGBGBGBGBGBGBGBGBG       ...  BGBGBGBGBGBG*/
void bayer2rgb() {
	int x, y, i, bayer = 0;
        unsigned char p = 0;
	for (y = 0; y < HEIGHT; y++) {
                i = (y * WIDTH + /*x=*/0) * 3;
		for (x = 0; x < WIDTH; x++) {
			if (y & 1)
				bayer = 2; // y odd
			else
				bayer = 0; // y even
			if (x & 1)
				p = input[y * WIDTH + x/2];
			else {
				p = input[y * WIDTH + x/2 + WIDTH/2];
                                bayer++;
                        }
			switch (bayer) {
			case 1: // color = 1
			case 2: // color = 1
                                output[i+1] = green[p];
                                if (1 <= x && x < WIDTH-1 && 2 <= y && y < HEIGHT)
                                        output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
				break;
			case 0:// colour = 0;
                                output[i] = red[p];
                                if (1 <= x && y<HEIGHT-1)
                                        output[i-3] = (output[i-6] + output[i])/2;
                                if (x<WIDTH-1 && 2 <= y)
                                        output[i-3*WIDTH]=(output[i-6*WIDTH]+output[i])/2;
                                if (1 <= x && x < WIDTH && 2 <= y && y < HEIGHT)
                                        output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
				break;
			case 3: //colour = 2;
                                output[i+2] = blue[p];
                                if (1 <= x && y<HEIGHT-1)
                                        output[i+2-3] = (output[i+2-6] + output[i+2])/2;
                                if (x<WIDTH-1 && 2 <= y)
                                        output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                                if (1 <= x && x < WIDTH && 2 <= y && y < HEIGHT)
                                        output[i+2-3-3*WIDTH] = (output[i+2] + output[i+2-6] + output[i+2-6*WIDTH]+output[i+2-6-6*WIDTH])/4;
				break;
			}
                        i += 3;
		}
	}
}
void bayer2rgb2() {
	int x, y, i, o;
        unsigned char p = 0;
	for (y = 0; y < HEIGHT; y++) {
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
		for (x = 0; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        if (1 <= x && x < WIDTH-1 && 2 <= y)
                                output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o+=1;
		}
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i] = red[p];
                        if (1 <= x && y<HEIGHT-1)
                                output[i-3] = (output[i-6] + output[i])/2;
                        if (x<WIDTH-1 && 2 <= y)
                                output[i-3*WIDTH]=(output[i-6*WIDTH]+output[i])/2;
                        if (1 <= x && x < WIDTH && 2 <= y && y < HEIGHT)
                                output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
                        i+=6;
                        o+=1;
                }
                y++;
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
		for (x = 0; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+2] = blue[p];
                        if (1 <= x && y<HEIGHT-1)
                                output[i+2-3] = (output[i+2-6] + output[i+2])/2;
                        if (x<WIDTH-1 && 2 <= y)
                                output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                        if (1 <= x && x < WIDTH && 2 <= y && y < HEIGHT)
                                output[i+2-3-3*WIDTH] = (output[i+2] + output[i+2-6] + output[i+2-6*WIDTH]+output[i+2-6-6*WIDTH])/4;
                        i+=6;
                        o++;
		}
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        if (1 <= x && x < WIDTH-1 && 2 <= y && y < HEIGHT)
                                output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o++;
                }
	}
}
void bayer2rgb3_1() {
        int x, y, i, o;
        unsigned char p = 0;
	for (y = 0; y < 2; y++) {
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
		for (x = 0; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        i+=6;
                        o+=1;
		}
                p = input[o];
                output[i+1] = green[p];
                
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i] = red[p];
                        output[i-3] = (output[i-6] + output[i])/2;
                        i+=6;
                        o+=1;
                }
                y++;
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
                p = input[o];
                output[i+2] = blue[p];
                i+=6;
                o++;
		for (x = 2; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+2] = blue[p];
                        output[i+2-3] = (output[i+2-6] + output[i+2])/2;
                        i+=6;
                        o++;
		}
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        i+=6;
                        o++;
                }
	}
}
void bayer2rgb3_2() {
	int x, y, i, o;
        unsigned char p = 0;
	for (y = 2; y < HEIGHT-2; y++) {
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
		for (x = 0; x < WIDTH-1; x+=2) {
                        output[i+1] = green[input[o]];
                        output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o+=1;
		}
                p = input[o];
                output[i+1] = green[p];
                
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i] = red[p];
                        output[i-3] = (output[i-6] + output[i])/2;
                        output[i-3*WIDTH]=(output[i-6*WIDTH]+output[i])/2;
                        output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
                        i+=6;
                        o+=1;
                }
                p = input[o];
                output[i] = red[p];
                output[i-3] = (output[i-6] + output[i])/2;
                output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
                
                y++;
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
                p = input[o];
                output[i+2] = blue[p];
                output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                i+=6;
                o++;
		for (x = 2; x < WIDTH; x+=2) {
                        p = input[o];
                        output[i+2] = blue[p];
                        output[i+2-3] = (output[i+2-6] + output[i+2])/2;
                        output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                        output[i+2-3-3*WIDTH] = (output[i+2] + output[i+2-6] + output[i+2-6*WIDTH]+output[i+2-6-6*WIDTH])/4;
                        i+=6;
                        o++;
		}
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o++;
                }
                p = input[o];
                output[i+1] = green[p];
	}
}
void bayer2rgb3_3() {
	int x, y, i, o;
        unsigned char p = 0;
	for (y = HEIGHT-2; y < HEIGHT; y++) {
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
		for (x = 0; x < WIDTH-1; x+=2) {
                        output[i+1] = green[input[o]];
                        output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o+=1;
		}
                p = input[o];
                output[i+1] = green[p];
                
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i] = red[p];
                        output[i-3] = (output[i-6] + output[i])/2;
                        output[i-3*WIDTH]=(output[i-6*WIDTH]+output[i])/2;
                        output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
                        i+=6;
                        o+=1;
                }
                p = input[o];
                output[i] = red[p];
                output[i-3] = (output[i-6] + output[i])/2;
                output[i-3-3*WIDTH] = (output[i] + output[i-6] + output[i-6*WIDTH]+output[i-6-6*WIDTH])/4;
                
                y++;
                i = (y * WIDTH + /*x=*/0) * 3;
                o = y * WIDTH + /*x=*/0/2 + WIDTH/2;
                p = input[o];
                output[i+2] = blue[p];
                output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                i+=6;
                o++;
		for (x = 2; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i+2] = blue[p];
                        output[i+2-3*WIDTH] = (output[i+2]+output[i+2-6*WIDTH])/2;
                        output[i+2-3-3*WIDTH] = (output[i+2] + output[i+2-6] + output[i+2-6*WIDTH]+output[i+2-6-6*WIDTH])/4;
                        i+=6;
                        o++;
		}
                        
                i = (y * WIDTH + /*x=*/1) * 3;
                o = y * WIDTH + /*x=*/1/2;
                for (x = 1; x < WIDTH-1; x+=2) {
                        p = input[o];
                        output[i+1] = green[p];
                        output[i+1-3*WIDTH] = (output[i+1] + output[i+1-3-3*WIDTH] + output[i+1+3-3*WIDTH]+output[i+1-6*WIDTH])/4;
                        i+=6;
                        o++;
                }
                p = input[o];
                output[i+1] = green[p];
        }
}        

void fix_edges() {
        int y, i;
        /* fix top and bottom row, left and right side */
	i = WIDTH * 3;
	memcpy (output, (output + i), i);
	memcpy ((output + (HEIGHT * i)), (output + ((HEIGHT - 1) * i)), i);
	for (y = 0; y < HEIGHT; y++) {
		i = y * WIDTH * 3;
                output[i] = output[i+3];
                output[i+1] = output[i+3+1];
                output[i+2] = output[i+3+2];
                output[i+(WIDTH*3)] = output[i+(WIDTH-1)*3];
                output[i+(WIDTH*3)+1] = output[i+(WIDTH-1)*3+1];
                output[i+(WIDTH*3)+2] = output[i+(WIDTH-1)*3+2];
	}
}
void bayer_unshuffle() {
        bayer2rgb3_1();
        bayer2rgb3_2();
        bayer2rgb3_3();
        fix_edges();
}				/* bayer_unshuffle */

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

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