#include "arxiu.h"
#include <string>
#include <stdio.h>
#include <math.h>

struct header_key                       /* header key   */
       {                                /* off + size      */
       int sizeof_hdr;                   /* 0 + 4           */
       char data_type[10];              /* 4 + 10          */
       char db_name[18];                /* 14 + 18         */
       int extents;                     /* 32 + 4          */
       short int session_error;         /* 36 + 2          */
       char regular;                    /* 38 + 1          */
       char hkey_un0;                   /* 39 + 1          */
       };                               /* total=40 bytes  */

struct image_dimension
       {                                /* off + size      */
       short int dim[8];                /* 0 + 16          */
       short int unused8;               /* 16 + 2          */
       short int unused9;               /* 18 + 2          */
       short int unused10;              /* 20 + 2          */
       short int unused11;              /* 22 + 2          */
       short int unused12;              /* 24 + 2          */
       short int unused13;              /* 26 + 2          */
       short int unused14;              /* 28 + 2          */
       short int datatype;              /* 30 + 2          */
       short int bitpix;                /* 32 + 2          */
       short int dim_un0;               /* 34 + 2          */
       float pixdim[8];                 /* 36 + 32         */
                       /*
                            pixdim[] specifies the voxel dimensitons:
                            pixdim[1] - voxel width
                            pixdim[2] - voxel height
                            pixdim[3] - interslice distance
                                ...etc
                       */
       float vox_offset;                /* 68 + 4          */
       float funused1;                  /* 72 + 4          */
       float funused2;                  /* 76 + 4          */
       float funused3;                  /* 80 + 4          */
       float cal_max;                   /* 84 + 4          */
       float cal_min;                   /* 88 + 4          */
       float compressed;                /* 92 + 4          */
       float verified;                  /* 96 + 4          */
       int glmax,glmin;                 /* 100 + 8         */
       };                               /* total=108 bytes */

struct data_history
       {                                /* off + size      */
       char descrip[80];                /* 0 + 80          */
       char aux_file[24];               /* 80 + 24         */
       char orient;                     /* 104 + 1         */
       char originator[10];             /* 105 + 10        */
       char generated[10];              /* 115 + 10        */
       char scannum[10];                /* 125 + 10        */
       char patient_id[10];             /* 135 + 10        */
       char exp_date[10];               /* 145 + 10        */
       char exp_time[10];               /* 155 + 10        */
       char hist_un0[3];                /* 165 + 3         */
       int views;                        /* 168 + 4         */
       int vols_added;                  /* 172 + 4         */
       int start_field;                 /* 176 + 4         */
       int field_skip;                  /* 180 + 4         */
       int omax, omin;                  /* 184 + 8         */
       int smax, smin;                  /* 192 + 8         */
       };

struct dsr
       {
       struct header_key hk;            /* 0 + 40          */
       struct image_dimension dime;     /* 40 + 108        */
       struct data_history hist;        /* 148 + 200       */
       };

using namespace std;

Arxiu::Arxiu()
{
}
Parametres Arxiu::carregarVolum(){

}
void Arxiu::llegirHDR(/*char *img*/){
    struct dsr hdr;
    //int size;
    //double cmax, cmin;
    FILE *fp;

    char *file = "/home/alex/svn/TFGProject/datasets/spmpet1246094_20110228.hdr";

       if((fp=fopen(file,"r"))==NULL)
    {
        fprintf(stderr,"Can't open:<%s>\n", file);
        exit(0);
    }
    fread(&hdr,1,sizeof(struct dsr),fp);

    //cout << hdr.dime.dim[0]<<" "<< hdr.dime.dim[1]<<" "<< hdr.dime.dim[2]<<" "<< hdr.dime.dim[3]<<endl;

    //ShowHdr(file, &hdr);

}
void Arxiu::getTamanyHDR(char* file, vec4* tv){
    struct dsr hdr;
   // int size;
  //  double cmax, cmin;
    FILE *fp;

    //char *file = "/home/alex/svn/TFGProject/datasets/spmpet1246094_20110228.hdr";

       if((fp=fopen(file,"r"))==NULL)
    {
        fprintf(stderr,"Can't open:<%s>\n", file);
        exit(0);
    }
    fread(&hdr,1,sizeof(struct dsr),fp);



    tv->x = hdr.dime.dim[1];
    tv->y = hdr.dime.dim[2];
    tv->z = hdr.dime.dim[3];

  /* tv->x = 128;
    tv->y = 128;
    tv->z = 128;
*/
     //cout <<" tamany dataset "<< hdr.dime.dim[0]<<" "<< tv->x<<" "<< tv->y<<" "<< tv->z<<endl;
    //ShowHdr(file, &hdr);

}

void Arxiu::swap_long(unsigned char *pntr){
    unsigned char b0, b1, b2, b3;

    b0 = *pntr;
    b1 = *(pntr+1);
    b2 = *(pntr+2);
    b3 = *(pntr+3);

    *pntr = b3;
    *(pntr+1) = b2;
    *(pntr+2) = b1;
    *(pntr+3) = b0;
}

void Arxiu::swap_short(unsigned char *pntr) {
    unsigned char b0, b1;

    b0 = *pntr;
    b1 = *(pntr+1);

    *pntr = b1;
    *(pntr+1) = b0;
}


void Arxiu::ShowHdr(char *fileName,struct dsr *hdr){
    int i;
    char string[128];
    printf("Analyze Header Dump of: <%s> \n", fileName);
    /* Header Key */
    printf("sizeof_hdr: <%d> \n", hdr->hk.sizeof_hdr);
    printf("data_type:  <%s> \n", hdr->hk.data_type);
    printf("db_name:    <%s> \n", hdr->hk.db_name);
    printf("extents:    <%d> \n", hdr->hk.extents);
    printf("session_error: <%d> \n", hdr->hk.session_error);
    printf("regular:  <%c> \n", hdr->hk.regular);
    printf("hkey_un0: <%c> \n", hdr->hk.hkey_un0);


    for(i=0;i<8;i++)
           printf("dim[%d]: <%d> \n", i, hdr->dime.dim[i]);

          // strncpy(string,hdr->dime.vox_units,4);
           printf("vox_units:  <%s> \n", string);

          // strncpy(string,hdr->dime.cal_units,8);
           printf("cal_units: <%s> \n", string);
          // printf("unused1:   <%d> \n", hdr->dime.unused1);
           printf("datatype:  <%d> \n", hdr->dime.datatype);
           printf("bitpix:    <%d> \n", hdr->dime.bitpix);

    for(i=0;i<8;i++)
           printf("pixdim[%d]: <%6.4f> \n",i, hdr->dime.pixdim[i]);

 //   printf("vox_offset: <%6.4> \n",  hdr->dime.vox_offset);
    printf("funused1:   <%6.4f> \n", hdr->dime.funused1);
    printf("funused2:   <%6.4f> \n", hdr->dime.funused2);
    printf("funused3:   <%6.4f> \n", hdr->dime.funused3);
    printf("cal_max:    <%6.4f> \n", hdr->dime.cal_max);
    printf("cal_min:    <%6.4f> \n", hdr->dime.cal_min);
   // printf("compressed: <%d> \n", hdr->dime.compressed);
   // printf("verified:   <%d> \n", hdr->dime.verified);
    printf("glmax:      <%d> \n", hdr->dime.glmax);
    printf("glmin:      <%d> \n", hdr->dime.glmin);

    /* Data History */
    strncpy(string,hdr->hist.descrip,80);
    printf("descrip:  <%s> \n", string);
    strncpy(string,hdr->hist.aux_file,24);
    printf("aux_file: <%s> \n", string);
    printf("orient:   <%d> \n", hdr->hist.orient);

    strncpy(string,hdr->hist.originator,10);
    printf("originator: <%s> \n", string);

    strncpy(string,hdr->hist.generated,10);
    printf("generated: <%s> \n", string);


    strncpy(string,hdr->hist.scannum,10);
    printf("scannum: <%s> \n", string);

    strncpy(string,hdr->hist.patient_id,10);
    printf("patient_id: <%s> \n", string);

    strncpy(string,hdr->hist.exp_date,10);
    printf("exp_date: <%s> \n", string);

    strncpy(string,hdr->hist.exp_time,10);
    printf("exp_time: <%s> \n", string);

    strncpy(string,hdr->hist.hist_un0,10);
    printf("hist_un0: <%s> \n", string);

    printf("views:      <%d> \n", hdr->hist.views);
    printf("vols_added: <%d> \n", hdr->hist.vols_added);
    printf("start_field:<%d> \n", hdr->hist.start_field);
    printf("field_skip: <%d> \n", hdr->hist.field_skip);
    printf("omax: <%d> \n", hdr->hist.omax);
    printf("omin: <%d> \n", hdr->hist.omin);
    printf("smin: <%d> \n", hdr->hist.smax);
    printf("smin: <%d> \n", hdr->hist.smin);

}

char* Arxiu::img2mask(char* file){
    string path = "";
    string nums = "";

    int i = strlen(file)-1-4;
    while( (file[i] >='0' && file[i] <='9') || file[i] =='_'){
        nums = file[i] +nums;
        i--;
    }
    for ( int p = 0; p <i-5; ++p)path += file[p];
    path=  path+"spmmask_"+nums+".img";
    char* mascara = new char[path.size()+1];
    mascara[path.size()]= 0;
    memcpy(mascara,path.c_str(),path.size());
    return mascara;
}

vec3 Arxiu::tamanyCub(int x, int y, int z){

    vec3 tamany;
    double logx,logy,logz;

    logx = log2(x);
    logy = log2(y);
    logz = log2(z);

    if ( logx != int(logx)) logx = int(logx)+1;
    if ( logy != int(logy)) logy = int(logy)+1;
    if ( logz != int(logz)) logz = int(logz)+1;

    logx = pow(2,logx);
    logy = pow(2,logy);
    logz = pow(2,logz);

    //tamany = vec3(logx,logy,logz);

    logx = max(max(logx,logy),logz);
    tamany = vec3(logx,logx,logx);

    //cout <<"tamany cub "<<tamany.x<<" "<<tamany.y<<" "<<tamany.z<<endl;
    return tamany;
}
void Arxiu::img2vecstring(char* fitx, char* img, char* hdr){


    strcpy(img, fitx);
    strcpy(hdr, fitx);
    int i = strlen(fitx)-1;
    img[i] = 'g';hdr[i]='r'; i = i-1;
    img[i] = 'm';hdr[i]='d'; i = i-1;
    img[i] = 'i';hdr[i]='h'; i = i-1;

    //cout << "dfsddsdf" <<img <<" \n "<<hdr <<endl;

}
