#include "lut.h"

#include <QtGui>
#include <QtOpenGL>
#include <stdio.h>
#include "iostream"

Lut::Lut()
{
    for (int i = 0; i < 256; i++)
        for(int j = 0; j < 4; j++)
            matriz[i][j] = 0;
}

Lut::Lut(const char* l)
{
    nombreLut = (char*)malloc(1+strlen(l)*sizeof(char));
    strcpy(nombreLut,l);

    //nombreLut = l;

    FILE *lu = fopen(nombreLut, "r");

    if (lu == NULL) {
        fputs ("LUT: File error",stderr);
        exit (1);
    } else {

        for (int i = 0; i < 256; i++){
            for(int j = 0; j < 4; j++){
                matriz[i][j] = 0;
            }
        }
            int m = 0;

        while(!feof(lu) && m < 256){

            int r = 0;
            int g = 0;
            int b = 0;
            int a = 0;

            fscanf(lu, "%d %d %d %d\n",&r,&g,&b,&a);

//            printf("R: %d G: %d B: %d A: %d \n",r,g,b,a);
            matriz[m][0]=r/255.0;
            matriz[m][1]=g/255.0;
            matriz[m][2]=b/255.0;
            matriz[m][3]=a/255.0;
            m++;
        }
        printf("\n");
    }
   fclose(lu);
}

Lut::Lut(QVector<unsigned int> colors){

    int size = colors.size();
    float alfa;
    for(int i = 0; i < size; i++){

        QColor *color = new QColor;
      //  color->setRgb(colors.at(i));
        color->setRgba(colors.at(i));

        alfa = color->alpha()/255.0;
        matriz[i][0]=(1.0-(color->red()/255.0));
        matriz[i][1]=(1.0-(color->green()/255.0));
        matriz[i][2]=(1.0-(color->blue()/255.0));
       // matriz[i][3]=1.0;
        matriz[i][3]=((i*1.0)/255.0)*alfa;
      //  matriz[i][3]=1.0-((i*color->alpha())/255.0);
      //  matriz[i][3]=1.0-(i*color->alpha()/255.0);
       // matriz[i][3] =1.0-((i*color->alpha())/255.0);
     //   std::cout <<color->alpha() <<" ";
    }
  //  std::cout <<std::endl;
}


float* Lut::getColor(float valor)
{
    int v = valor;
    if (v < 0)
        v = 0;
    if (v > 255)
        v = 255;

    return matriz[v];
}

/*void Lut::setColor(int indice, float cR, float cG, float cB, float transparencia)
{
    if (indice == 0){
        transparencia = 0.f;
        //matriz[indice][0] = (float)co.redF();
        //matriz[indice][1] = (float)co.blueF();
        //matriz[indice][2] = (float)co.greenF();
        //matriz[indice][3] = (float)transparencia;
        matriz[indice][0] = cR;
        matriz[indice][1] = cG;
        matriz[indice][2] = cB;
        matriz[indice][3] = transparencia;

    } else {
        //matriz[indice][0] = (float)co.redF();
        //matriz[indice][1] = (float)co.blueF();
        //matriz[indice][2] = (float)co.greenF();
        //matriz[indice][3] = (float)transparencia/256;
        matriz[indice][0] = cR;
        matriz[indice][1] = cG;
        matriz[indice][2] = cB;
        matriz[indice][3] = transparencia;
    }
    //printf("Set Color: %d R:%f G:%f B:%f T:%f\n",indice,(float)co.redF(),(float)co.blueF(),(float)co.greenF(),(float)transparencia/256);
    printf("Set Color: %d R:%f G:%f B:%f T:%f\n",indice,cR*255,cG*255,cB*255,transparencia*255);
}
*/


void Lut::rellenarLutFichero(char *nF)
{
    char *nFLUT;

    nFLUT = (char *)malloc(200*sizeof(char));

    sprintf(nFLUT, "%s.lut",nF);

    FILE *f3 = fopen(nFLUT, "wb+");

    int r = 0;
    int g = 0;
    int b = 0;
    int a = 0;

    //Recorro la matriz y guardo los valores en las variables para luego imprimirlas en el fichero
    for (int i = 0;i < 256;i++){
        r = (matriz[i][0]*255);
        g = (matriz[i][1]*255);
        b = (matriz[i][2]*255);
        a = (matriz[i][3]*255);
        //Son floats he de multiplicar por 255
        fprintf(f3, "%d %d %d %d\n",r,g,b,a);
    }
    fclose(f3);
}


