#ifndef __CIMAGE_H__
#define __CIMAGE_H__

#include <string>
#include "popassert.h"
#include "auto_array_ptr.h"

#include "CColor.h"


class CImage
{
 public:
  CImage();
  CImage(int w, int h);

  void Allocate(int w, int h);

  int width() const { return m_Width; }
  int height() const { return m_Height; }

  const CColor& 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];
    }
  CColor& 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];
    }

  int LoadBMP(const std::string& sFileName);
  int SaveBMP(const std::string& sFileName) const;

  void Show(bool wait = true) const;


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


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

  void ExecuteShow() const;
  static void *ShowProcedure(void *data);
};

#endif // __CIMAGE_H__
