#include "my_image.h"
//konstruktory:
my_image::my_image(IplImage *img)
{
    if(img==NULL)
        return;

    this->width=img->width;
    this->height=img->height;
    this->depth=img->depth;
    this->nChannels=img->nChannels;


    this->data=this->alocate_mem();

    CvScalar color;
    for(int x=0;x<this->width;x++)
        for(int y=0;y<this->height;y++)
        {
            color=cvGet2D(img,y,x);
            this->data[x][y]=(uchar)color.val[0];
        }

}


my_image::my_image(my_image *img){
this->width=img->width;
this->height=img->height;
this->depth=img->depth;
this->nChannels=img->nChannels;

this->data=this->alocate_mem();

for(int x=0;x<this->width;x++)
    for(int y=0;y<this->height;y++)
        this->data[x][y]=img->data[x][y];

}

uchar my_image::get(int x, int y){
    if(this->data==NULL){
        // throw "Neni nacten obraz, nemuzes pouzit metodu my_image.get()";
        std::cout<<"Neni nacten obraz, nemuzes pouzit metodu my_image.get() \n";
        return 0;
    }
    if(x<0 || x>=this->width){
       // throw "Sahas mimo obraz my_image.get() couradnice x";
        std::cout<<"Sahas mimo obraz my_image.get() couradnice x: "<<x<<"ale sirka je "<<this->width<<" \n";
        return 0;
    }

    if(y<0 || y>=this->height){
      //  throw "Sahas mimo obraz my_image.get() couradnice y";
        std::cout<<"Sahas mimo obraz my_image.get() couradnice y: "<<y<<"ale vyska je "<<this->height<<" \n";

        return 0;
    }
    return data[x][y];;
}



void my_image::set(int x, int y,uchar color){
    if(this->data==NULL){
        // throw "Neni nacten obraz, nemuzes pouzit metodu my_image.set()";
        std::cout<<"Neni nacten obraz, nemuzes pouzit metodu my_image.set() \n";
        return;
    }
    if(x<0 || x>=this->width){
       // throw "Sahas mimo obraz my_image.set() couradnice x";
        std::cout<<"Sahas mimo obraz my_image.set() couradnice x: "<<x<<"ale sirka je "<<this->width<<" \n";
        return;
    }

    if(y<0 || y>=this->height){
      //  throw "Sahas mimo obraz my_image.set() couradnice y";
        std::cout<<"Sahas mimo obraz my_image.set() couradnice y: "<<y<<"ale vyska je "<<this->height<<" \n";

        return;
    }
    data[x][y]=color;
}



IplImage * my_image::get_cv_image()
{

    if(this->data==NULL){
        std::cout<<"nenacten zadny obraz :-/";
        return NULL;
    }

    CvScalar color;
   IplImage * img= cvCreateImage( cvSize(this->width,this->height ),this->depth,this->nChannels);


for(int x=0;x<this->width;x++)
    for(int y=0;y<this->height;y++)
    {
        color.val[0]=this->data[x][y];
        color.val[1]=color.val[0];
        color.val[2]=color.val[0];

        cvSet2D(img,y,x,color); // set the (i,j) pixel value
    }

   return img;
}


uchar ** my_image::alocate_mem(){
    //alokace pameti
    uchar **d=NULL;
    d=(uchar **)calloc((this->width),sizeof(uchar*));
    if(d==NULL)
        std::cout<<"NEMAM PAMET my_image\n";
    for(int i=0;i<this->width;i++){
        d[i]=NULL;
        d[i]=(uchar *)calloc(this->height,sizeof(uchar));
        if(d[i]==NULL)
            std::cout<<"NEMAM PAMET my_image\n";
    }

    return d;
}

int my_image::Width(){
    return this->width;
}
int my_image::Height(){
    return this->height;
}

void my_image::clean_all(){
    for(int x=0;x<this->width;x++)
        for(int y=0;y<this->height;y++)
        {
            this->data[x][y]=0;
        }
}


void my_image::white_all(){
    for(int x=0;x<this->width;x++)
        for(int y=0;y<this->height;y++)
        {
            this->data[x][y]=255;
        }
}

my_image::~my_image()
{
    for(int i=0;i<this->width;i++)
        free (this->data[i]);
    free(this->data);
}
