/*

Copyright 2003 The University of New South Wales (UNSW) and National  
ICT Australia (NICTA).

This file is part of the 2003 team rUNSWift RoboCup entry.  You may  
redistribute it and/or modify it under the terms of the GNU General  
Public License as published by the Free Software Foundation; either  
version 2 of the License, or (at your option) any later version as  
modified below.  As the original licensors, we add the following  
conditions to that license:

In paragraph 2.b), the phrase "distribute or publish" should be  
interpreted to include entry into a competition, and hence the source  
of any derived work entered into a competition must be made available  
to all parties involved in that competition under the terms of this  
license.

In addition, if the authors of a derived work publish any conference  
proceedings, journal articles or other academic papers describing that  
derived work, then appropriate academic citations to the original work  
must be included in that publication.

This rUNSWift source is distributed in the hope that it will be useful,  
but WITHOUT ANY WARRANTY; without even the implied warranty of  
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
General Public License for more details.

You should have received a copy of the GNU General Public License along  
with this source code; if not, write to the Free Software Foundation,  
Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

/* 
 * Shrinks a 256 cubed colour space by FACTOR.
 * i.e. if FACTOR is 4 the resulting colour space is 256/4 = 64, 64^3 colourspace.
 * Input to this file is usually the training data from the pictures
 * taken generated by java Convert
 * written by Tim Tam 10/1/02
 */

#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include "colour_definition.h"

#define FACTOR 2
#define DIMENSION 256/FACTOR

using std::cerr;
using std::endl;
using std::ifstream;
using std::string;

typedef struct pixel
{
    // The number of occurrences of each colour in the compressed pixel.
    int orange,
        blue,
        green,
        yellow,
        pink,
        blue_dog,
        red_dog,
        green_field,
        robot_grey,
        field_white,
        black,
	    none;
} efpixel;


int main(int argc, char* argv[])
{
//    printf("%d %d %d\n", DIMENSION, DIMENSION, DIMENSION);
    if (argc != 2) {
        cerr << "Usage: compressor <filename>" << endl;
        exit(1);
    }
    
    ifstream file(argv[1]);
    string line;
    int y, u, v, colour;
    
    // The array to store all the identical y, u, v point after compression.
    pixel *list = (pixel *)calloc(DIMENSION * DIMENSION * DIMENSION, sizeof(pixel));

    // Read the pixels into the array.
    while (getline(file, line)) {
        sscanf(line.c_str(), "%d %d %d %d\n", &y, &u, &v, &colour);
        y  = y / FACTOR;
        u = u / FACTOR;
        v = v / FACTOR;
        
        switch(colour) {
            case BALL:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].orange)++;
                break;
            case BLUE:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].blue)++;
                break;
            case GREEN:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].green)++;
                break;
            case YELLOW:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].yellow)++;
                break;
            case PINK:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].pink)++;
                break;
            case BLUE_DOG:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].blue_dog)++;
                break;
            case RED_DOG:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].red_dog)++;
                break;
            case GREEN_FIELD:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].green_field)++;
                break;
            case FIELD_WHITE:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].field_white)++;
                break;
            case ROBOT_GREY:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].robot_grey)++;
                break;
            case BLACK:
                (list[y*DIMENSION*DIMENSION + u*DIMENSION + v].black)++;
                break;
            case NONE:
                break;
            default:
                cerr << "Unknown colour " << colour << endl;
        }                
    }
    
    // Iterate through the whole array and determine the colour of each pixel
    // in the compressed colour space.
    for (y = 0; y < DIMENSION; y++) {
        for (u = 0; u < DIMENSION; u++) {
            for (v = 0; v < DIMENSION; v++) {
                pixel p = list[y*DIMENSION*DIMENSION + u*DIMENSION + v];
                int max = p.none;
                unsigned char colour = NONE;
                
                // Do not print out pixel if it did not originate from
                // a training data point.

                if (p.orange == 0 && p.blue == 0 && p.green == 0 && 
                    p.yellow == 0 && p.pink == 0 && p.blue_dog == 0 && 
                    p.red_dog == 0 && p.green_field == 0 && p.field_white == 0 && 
                    p.robot_grey == 0 && p.black == 0) {
                    continue;
                }
                
                if (max <= p.orange) {
                    max = p.orange;
                    colour = BALL;
                }
                if (max <= p.blue) {
                    max = p.blue;
                    colour = BLUE;
                }
                if (max <= p.green) {
                    max = p.green;
                    colour = GREEN;
                }
                if (max <= p.yellow) {
                    max = p.yellow;
                    colour = YELLOW;
                }
                if (max <= p.pink) {
                    max = p.pink;
                    colour = PINK;
                }
                if (max <= p.blue_dog) {
                    max = p.blue_dog;
                    colour = BLUE_DOG;
                }
                if (max <= p.red_dog) {
                    max = p.red_dog;
                    colour = RED_DOG;
                }
                if (max <= p.green_field) {
                    max = p.green_field;
                    colour = GREEN_FIELD;
                }
                if (max <= p.field_white) {
                    max = p.field_white;       
                    colour = FIELD_WHITE;
                }
                if (max <= p.robot_grey) {
                    max = p.robot_grey;    
                    colour = ROBOT_GREY;
                }
                if (max <= p.black) {
                    max = p.black;
                    colour = BLACK;
                }        
                printf("%d, %d, %d, %d\n", y, u, v, colour);

            }
        }
    }
}
