/**
 * \file disparity.c
 * \brief This file helps to handle the disparity buffer
 *
 *
 *  \ingroup corefiles
 *  \date May 13, 2009
 *  \author Alexander Pimenov
 */
#include <stdlib.h>


#include "disparity.h"
#include "global.h"



#ifdef ASSERT
int disparityBufferCounter = 0;
#endif

/**
 * \brief This function creates a disparity buffer for the given width and height.
 *
 * The created image is filled with #DISPARITY_UNKNOWN values
 *
 * You should not forget to destroy the created buffer with the call to disparityBufferDelete(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.
 */
DisparityBuffer *disparityBufferCreate(int h, int w)
{
	DisparityBuffer *toReturn = (DisparityBuffer *) dv_malloc (sizeof (DisparityBuffer));
	int i;
	toReturn->h = h;
	toReturn->w = w;
	toReturn->data = (uint16_t *) dv_malloc(h * w * sizeof(uint16_t));

	for (i = 0; i < h * w; i++)
	{
		toReturn->data[i] = DISPARITY_UNKNOWN;
	}


#ifdef ASSERT
	disparityBufferCounter++;
#endif

	return toReturn;
}

/**
 * \brief This function deletes a disparity buffer.
 */
void disparityBufferDelete(DisparityBuffer *toDelete)
{
	if (toDelete->data != NULL)
		dv_free(toDelete->data);
	dv_free(toDelete);
#ifdef ASSERT
	disparityBufferCounter++;
#endif

}
