/*********************************************************************

Name:		Raytracer
Author:		Kamil Szatkowski "Netrix" (C)
Website:	http://www.netrix.org.pl/
License:	GNU GPL

*********************************************************************/
#pragma once
#include "NLib/nBase.hpp"
#include "NLib/nDynamicTable.hpp"

class NBitmap
{
	struct BitmapMagic	// Probably aligned to 4 bytes
	{
		NIne::NUint8 magic[2];				// "BM"
	};

	struct BitmapHeader
	{
		NIne::NUint32 uFileSize;			// Bitmap file size
		NIne::NUint16 uCreator[2];			// dunno
		NIne::NUint32 uPixelArrayOffset;
	};

	struct BitmapInfoHeader
	{
		NIne::NUint32 uHeaderSize;			// This header size
		NIne::NInt32 uWidth;
		NIne::NInt32 uHeight;
		NIne::NUint16 uNPlanes;				// must be 1
		NIne::NUint16 uBitsPerPixel;		// in this class 24
		NIne::NUint32 uCompressType;		// Typical bitmap 0 - RGB
		NIne::NUint32 uBitmapSize;			// Bitmap data size
		NIne::NInt32 uHRes;
		NIne::NInt32 uVRes;					// DPI ?
		NIne::NUint32 uNColors;				// 0
		NIne::NUint32 uNImpColors;			// 0
	};

public:
	NBitmap();
	~NBitmap();

	void create(NIne::NUint32 uWidth, NIne::NUint32 uHeight);
	void release();

	bool loadFromFile(const char* szFile);
	bool saveToFile(const char* szFile) const;

	NIne::NUint32 getWidth() const		{ return m_uWidth; }
	NIne::NUint32 getHeight() const		{ return m_uHeight; }

	NIne::NUint8* getBuffer() { return m_aBitmap.GetBuffer(); }

	void setPixel(NIne::NUint32 x, NIne::NUint32 y, NIne::NUint32 color);
	NIne::NUint32 getPixel(NIne::NUint32 x, NIne::NUint32 y) const;

private:
	NIne::NDynamicTable<NIne::NUint8> m_aBitmap; // 3 * m_uWidth * m_uHeight
	NIne::NUint32 m_uWidth;
	NIne::NUint32 m_uHeight;
};

#define NBITMAP_RGB(r, g, b) ((b | (g << 8) | (r << 16)) & 0x00FFFFFF)
#define NBITMAP_R(color) ((color >> 16) & 0xFF)
#define NBITMAP_G(color) ((color >> 8) & 0xFF)
#define NBITMAP_B(color) ((color) & 0xFF)