#ifndef __PATCH_H
#define __PATCH_H

#include "Image.h"

class Patch
{
 public:
  Patch(): data(0)
    {}

  Patch& operator=(const Patch& other)
    {
      size = other.size;
      if(other.data)
	{
	  data = (unsigned *)(new long long[size * size / 2]);
	  memcpy(data, other.data, size * size * 4);
	}
      return *this;
    }


  Patch(const Image& image, const unsigned x, const unsigned y, const unsigned _size)
    {
      size = _size;
      data = (unsigned *)(new long long[size * size / 2]);
      
      unsigned char *d = (unsigned char *)data;
      for(int j = 0; j < size; j++)
	{
	  for(int i = 0; i < size; i++)
	    {
	      unsigned iindex = (x + i) * 3 + (y + j) * image.pitch;
	      unsigned pindex = i * 4 + j * size * 4;
	      d[pindex] = image.data[iindex];
	      d[pindex + 1] = image.data[iindex + 1];
	      d[pindex + 2] = image.data[iindex + 2];
	      d[pindex + 3] = 0;
	    }
	}
    }

  ~Patch()
    {
      if(data) delete[] ((long long *)data);
    }

 public:
  unsigned int *data;
  unsigned int size;
};

#endif //__PATCH_H
