/*========================================================================
    Image.h : Simple Image class for loading and manipulating images
  ------------------------------------------------------------------------
    Copyright (C) 1999-2002  James R. Bruce
    School of Computer Science, Carnegie Mellon University
  ------------------------------------------------------------------------
    This software is distributed under the GNU General Public License,
    version 2.  If you do not have a copy of this licence, visit
    www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
    Suite 330 Boston, MA 02111-1307 USA.  This program is distributed
    in the hope that it will be useful, but WITHOUT ANY WARRANTY,
    including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  ========================================================================*/

#ifndef __IMAGE_H__
#define __IMAGE_H__

#define USE_IMAGE_MAGICK

#include <stdlib.h>
#include <math.h>

#include "../../agent/Vision/colors.h"
#include "util.h"

#ifndef USE_IMAGE_MAGICK
#include "load.h"
#endif

//==== RGB Utilities =================================================//

rgb blend(rgb a,rgb b,float t);

rgb to_rgb(rgbf c);
rgbf to_rgbf(rgb c);
rgbf clip(rgbf c);
rgbf operator +(const rgbf a,const rgbf b);
rgbf operator *(const rgbf a,const rgbf b);
rgbf operator *(const rgbf a,const double m);

rgbf pow(const rgbf x,const double p);
double sse(rgbf a,rgbf b);

//==== SImage Class ==================================================//

class SImage{
public:
  rgb *img;
  int w,h;

  SImage()
    {img=NULL; w=h=0;}
  ~SImage();

  bool allocate(int nw,int nh);
  bool load(const char *filename);
  bool save(const char *filename);
  bool copy(const SImage &a);
  bool copy_geometry(const SImage &a);

  void clear(rgb c);
  bool blend(const SImage &a,const SImage &b,float t);
  bool samesize(const SImage &a)
    {return(w==a.w && h==a.h);}

  bool decimate(const SImage &src,int pw,int ph);
  bool decimate(const SImage &src,int p)
    {return(decimate(src,p,p));}

  bool blit(const SImage &src,int sx,int sy,int sw,int sh,int dx,int dy);
  bool blit(const SImage &src,int dx,int dy)
    {return(blit(src,0,0,src.w,src.h,dx,dy));}
  void fill_rect(int x,int y,int dw,int dh,rgb c);
  void draw_line(int x0,int y0,int x1,int y1,rgb c);
  void zoom(const SImage &src,int ox,int oy,int zoom_x,int zoom_y,rgb def);

  rgb getpixel(int x,int y,rgb def) const;

  rgb getpixel_fast(int x,int y) const
    {return(img[y*w + x]);}
  rgb getpixel_clip(int x,int y) const;
  rgb getpixel_clip(float x,float y) const;

  rgb getpixel_wrap(int x,int y) const
    {return(img[(y % h)*w + (x % w)]);}
  rgb getpixel_wrap(float x,float y) const;

  bool setpixel(int x,int y,rgb c);
};

#endif
