/**
 * \file rgb24buffer.c
 * \brief TODO
 *
 * \date Sep 1, 2009
 * \author Alexander Pimenov
 * \ingroup corefiles
 *
 */

#include "global.h"
#include "rgb24buffer.h"

#ifdef ASSERT
int rgb24BufferCounter = 0;
#endif

/**
 *
 * \brief This function creates an RGB 8-8-8 bit buffer for the given width and height.
 *
 * In fact in the PC implementation a 32bit buffer is used. The created image is filled with 0 values
 *
 * You should not forget to destroy the created buffer with the call to rgb24BufferDelete(toDelete)
 *
 * \param h The height of the image to be created.
 * \param w The width of the image to be created.
 *
 * \return a newly created buffer.
 */

RGB24Buffer *rgb24BufferCreate(int h, int w)
{
	RGB24Buffer *toReturn = (RGB24Buffer *) dv_malloc (sizeof (RGB24Buffer));
	toReturn->h = h;
	toReturn->w = w;
	toReturn->data = (uint32_t *) dv_calloc(h * w , sizeof(uint32_t));

#ifdef ASSERT
	rgb24BufferCounter++;
#endif

	return toReturn;
}

/**
 * This function deletes an RGB 8-8-8 bit buffer and frees a memory
 *
 *  \param toDelete the buffer to be deleted
 */
void rgb24BufferDelete(RGB24Buffer *toDelete)
{
	if (toDelete->data != NULL)
		dv_free(toDelete->data);
	dv_free(toDelete);


#ifdef ASSERT
	rgb24BufferCounter--;
#endif

}

/**
 *  This function clones the buffer.
 *
 *  \param input a buffer to be cloned
 *
 *  \return a newly created buffer with a cloned contence
 **/

RGB24Buffer *rgb24BufferClone(RGB24Buffer *input)
{
	RGB24Buffer *toReturn = rgb24BufferCreate(input->h, input->w);
	memcpy(toReturn->data, input->data, input->h * input->w * sizeof(uint32_t));
	return toReturn;
}
