/////////////////////////////////////////////////////////////////////////
//                                                                     //
// This file contains the declaration of the CImage class.             //
//                                                                     //
/////////////////////////////////////////////////////////////////////////
#ifndef __CIMAGE_HPP__
#define __CIMAGE_HPP__

#include "popassert.hpp"
#include "auto_array_ptr.hpp"
#include "CColor.hpp"

#include <string>


/////////////////////////////////////////////////////////////////////////
//                                                                     //
// This class is a simple image class that provides functions to load  //
// and save image files and display images.                            //
//                                                                     //
/////////////////////////////////////////////////////////////////////////
class CImage
{
public:
  CImage();
  CImage(int w, int h);

  void allocate(int w, int h);
  int  loadBMP(const std::string& sFileName);
  int  saveBMP(const std::string& sFileName) const;
  void show(bool wait = true) const;

  // Inline accessors
  inline int width() const;
  inline int height() const;
  inline const CColor& operator() (int x, int y) const;
  inline CColor& operator() (int x, int y);


protected:
  inline int getSize() const { return m_Size; }


private:
  int                     m_Width;
  int                     m_Height;
  auto_array_ptr<CColor>  m_pPixels;
  int                     m_Size;

  // Title of the window containing the image
  static const char *gTitle;

  void executeShow() const;
  static void *showProcedure(void *data);
};




// Returns the width of the image
inline int CImage::width() const
{
  return m_Width;
}

// Return the height of the image
inline int CImage::height() const
{
  return m_Height;
}

// Returns a const color at a given position in the image
inline const CColor& CImage::operator() (int x, int y) const
{
  POP_ASSERT(x+y*m_Width < m_Width*m_Height,
             "Trying to access an out-of-range color");
  return m_pPixels[x+y*m_Width];
}

// Return a color at a given position in the image
inline CColor& CImage::operator() (int x, int y)
{
  POP_ASSERT(x+y*m_Width < m_Width*m_Height,
             "Trying to access an out-of-range color");
  return m_pPixels[x+y*m_Width];
}


#endif // __CIMAGE_HPP__
