/**
 * @file Image.h
 * @author Petr Egert (egertpet@fel.cvut.cz)
 * @brief Contains Image class declaration
 */

#ifndef IMAGE_H
#define IMAGE_H

#include "Error.h"

namespace RD
{
    /**
     * @brief Class representing an image
     *
     * Currently only TGA image files can be loaded
     */
    class Image
    {
        public:
            ///Default constructor, sets all params to 0/NULL
            Image() : data(NULL), bpp(0), width(0), height(0), dataSize(0){}

            ///Default destructor, currently does nothing
            ~Image(){}

            Result Init(const char *fileName);
            Result Release();

            char *data;     ///< Pointer to the raw image data

            unsigned char bpp;      ///< Image bits per pixel
            unsigned short width;   ///< Width of the image in pixels
            unsigned short height;  ///< Height of the image in pixels
            unsigned int dataSize;  ///< Size of the data pointed by Image::data

        protected:

        private:
            ///Structure representing a TGA file header
            struct TGAHeader
            {
                unsigned char offset;       ///< Offset from the end of the header to the beginning of the data
                unsigned char indexed;      ///< If 1 the image is indexed
                unsigned char type;         ///< Image type - 0 = none, 1 = indexed, 2 = RGB, 3 = greyscale. +8 if RLE is used
                unsigned short mapStart;    ///< Offset to the beginning of the colormap
                unsigned short mapSize;     ///< Size of the colormap
                unsigned char mapBpp;       ///< Bits per pixel for a colormap entry
                unsigned short xStart;      ///< X coord of the lower left corner of the image
                unsigned short yStart;      ///< Y coord of the lower left corner of the image
                unsigned short width;       ///< Width of the image
                unsigned short height;      ///< Height of the image
                unsigned char bpp;          ///< Image bits per pixel
                unsigned char flags;        ///< Other flags - 32 means origin at upper left

                /**
                 * @brief Fills the header information from the image data
                 *
                 * Userd to solve alignment issues
                 * @param data Header data from the TGA file
                 */
                void Set(char data[18])
                {
                    offset = data[0];
                    indexed = data[1];
                    type = data[2];
                    mapStart = *((short*)&data[3]);
                    mapSize = *((short*)&data[5]);
                    mapBpp = data[7];
                    xStart = *((short*)&data[8]);
                    yStart = *((short*)&data[10]);
                    width = *((short*)&data[12]);
                    height = *((short*)&data[14]);
                    bpp = data[16];
                    flags = data[17];
                }
            };

            Result LoadTga(const char *fileName);

    };
};

#endif // IMAGE_H
