
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int original[256][256][256];
int cumulative[256][256][256];

void process( int r, int g, int b ) {
	cumulative[r][g][b] = original[r][g][b];
	if (r>0) cumulative[r][g][b] += cumulative[r-1][g][b];
	if (g>0) cumulative[r][g][b] += cumulative[r][g-1][b];
	if (b>0) cumulative[r][g][b] += cumulative[r][g][b-1];
	if (r>0 && g>0) cumulative[r][g][b] -= cumulative[r-1][g-1][b];
	if (r>0 && b>0) cumulative[r][g][b] -= cumulative[r-1][g][b-1];
	if (g>0 && b>0) cumulative[r][g][b] -= cumulative[r][g-1][b-1];
	if (r>0 && g>0 && b>0) cumulative[r][g][b] += cumulative[r-1][g-1][b-1];
}

void process_all() {
	for ( int r=0; r<256; r++ ) {
		for ( int g=0; g<256; g++ ) {
			for ( int b=0; b<256; b++ ) {
				process(r,g,b);
				//char temp[1000];
				//sprintf(temp,"%2X%2X%2X:%d\n",r,g,b,cumulative[r][g][b]);
				//for ( int c=0; c<strlen(temp); c++ ) {
				//	if ( temp[c] == ' ' ) {
				//		temp[c] = '0';
				//	}
				//}
				printf("%d\n",cumulative[r][g][b]);
			}
		}
	}
}

int htoi(char c) {
	switch(c) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9': return c-'0';
		case 'A':
		case 'B':
		case 'C':
		case 'D':
		case 'E':
		case 'F': return 10+(c-'A');
	}
}

int main(int argc,char** argv) {
	if ( argc != 2 ) {
		printf("usage!\n");
		return -1;
	}
	int id = atoi(argv[1]);
	for ( int i=0; i<256; i++ ) {
		for ( int j=0; j<256; j++ ) {
			for ( int k=0; k<256; k++ ) {
				original[i][j][k] = 0;
			}
		}
	}

	char temp[1000];
	sprintf(temp,"fixed_histograms/%d.txt",id);
	ifstream in(temp);
	string line = "";
	getline(in,line);
	while ( line != "" ) {
		int red = htoi(line[0])*16 + htoi(line[1]);
		int green = htoi(line[2])*16 + htoi(line[3]);
		int blue = htoi(line[4])*16 + htoi(line[5]);
		int count = atoi(line.substr(7,line.length()-7).c_str());
		//printf("red=%d,green=%d,blue=%d,count=%d\n",red,green,blue,count);
		original[red][green][blue] = count;
		line = "";
		getline(in,line);
	}
	in.close();

	process_all();
	return 0;
}

