/** \defgroup Utilities Utility classes */

/** @addtogroup Utilities
    @{
*/

/** @file CImage.h
    \brief This file contains the declaration of the CImage class,
    a class representing an image.
*/


#ifndef CIMAGE_H
#define CIMAGE_H

/**
   \brief This class represents an image.

   It holds an array containing the color of each pixel of an image.
*/
class CImage
{
 public:
  /**
     \brief This is the CImage simple constructor.
  */
  CImage(int w = -1, int h = -1)
    {
      if (w > 0 && h > 0)
        {
          m_pPixels = new float[4*w*h];
          m_Width   = w;
          m_Height  = h;
        }
      else
        {
          m_pPixels = 0;
          m_Width   = -1;
          m_Height  = -1;
        }
    }

  /**
     \brief This is the CImage simple destructor.
  */
  ~CImage()
    {
      if (m_pPixels)
        delete [] m_pPixels;
    }

  /**
     \brief This operator returns a reference to the color
     component corresponding to the coordinates.
  */
  inline float& operator()(unsigned int i, unsigned int j, unsigned int k)
    {
      POP_ASSERT((signed) ((j * m_Width + i) * 4 + k) < m_Width * m_Height * 4,
              "Trying to access a color out of range.");
      return m_pPixels[(j * m_Width + i) * 4 + k];
    }

  /**
     \brief This operator returns a constant reference to the color
     component corresponding to the coordinates.
  */
  inline const float& operator()(unsigned int i, unsigned int j, unsigned int k) const
    {
      POP_ASSERT((signed) ((j * m_Width + i) * 4 + k) < m_Width * m_Height * 4,
              "Trying to access a color out of range.");
      return m_pPixels[(j * m_Width + i) * 4 + k];
    }

  /**
     \brief This function allocates an array of pixels of the
     desired size.
  */
  void Allocate(int w, int h)
    {
      if (m_pPixels)
        delete [] m_pPixels;

      m_pPixels = new float [4*w*h];
      m_Width   = w;
      m_Height  = h;
    }

  /**
     \brief This function writes the CImage data to an image file.
  */
  int WriteBmpFile(const std::string& FileName) const;

 private:
  float *m_pPixels; /** Array to the pixels data. */
  int m_Width;      /** Width of the image.       */
  int m_Height;     /** Height of the image.      */
};


#endif // CIMAGE_H

/** @} */
