/*
 * short_buffer.cpp
 *
 *  Created on: 09.03.2011
 *      Author: uwe
 */

#include "short_buffer.h"

namespace MobilePhotoTool{

	short_buffer::short_buffer(MPT_Exception *exception, int width, int length, int extra_size) {
		int temp_width, temp_length , temp_size, i;

		this->width 	= width;
		this->length	= length;

		temp_width 	= width + 2 * extra_size;
		temp_length = length + 2 * extra_size;
		temp_size	= temp_width * temp_length;

		this->delete_buffer = (unsigned short*) malloc(sizeof(unsigned short) * temp_size);
		if(this->delete_buffer == NULL){
			MPT_EXCEPTION("No Memory");
		}
		for(i = 0; i < temp_size; i++){
			delete_buffer[i] = 0;
		}

		this->buffer = &(this->delete_buffer[extra_size * temp_width + extra_size]);
		this->linestride = temp_width;

	}

	short_buffer::~short_buffer() {
		if(this->delete_buffer != NULL){
			free(delete_buffer) ;
			delete_buffer = NULL;
		}
	}

	unsigned short *short_buffer::get_Row(int row){
		return &(this->buffer[row * linestride]);
	}


	int_buffer::int_buffer(MPT_Exception *exception, int width, int length, int extra_size) {
		int temp_width, temp_length, temp_size, i;

		this->width 	= width;
		this->length	= length;

		temp_width 	= width + 2 * extra_size;
		temp_length = length + 2 * extra_size;
		temp_size	= temp_width * temp_length;

		this->delete_buffer = (int*) malloc(sizeof(int) * temp_size);
		if(this->delete_buffer == NULL){
			MPT_EXCEPTION("No Memory");
		}
		for(i = 0; i < temp_size; i++){
			delete_buffer[i] = 0;
		}

		this->buffer = &(this->delete_buffer[extra_size * temp_width + extra_size]);
		this->linestride = temp_width;

	}

	int_buffer::~int_buffer() {
		if(delete_buffer != NULL){
			free(delete_buffer) ;
			delete_buffer = NULL;
		}
	}

	int *int_buffer::get_Row(int row){
		return &(this->buffer[row * linestride]);
	}


	uchar_buffer::uchar_buffer(MPT_Exception *exception, int width, int length, int extra_size) {
		int temp_width, temp_length, temp_size, i;

		this->width 	= width;
		this->length	= length;

		temp_width 	= width + 2 * extra_size;
		temp_length = length + 2 * extra_size;
		temp_size	= temp_width * temp_length;

		this->delete_buffer = (unsigned char*) malloc(sizeof(unsigned char) * temp_size);
		if(this->delete_buffer == NULL){
			MPT_EXCEPTION("No Memory");
		}
		for(i = 0; i < temp_size; i++){
			delete_buffer[i] = 0;
		}

		this->buffer = &(this->delete_buffer[extra_size * temp_width + extra_size]);
		this->linestride = temp_width;

	}

	uchar_buffer::~uchar_buffer() {
		if(this->delete_buffer != NULL){
			free(this->delete_buffer);
			this->delete_buffer = NULL;
		}
	}

	unsigned char *uchar_buffer::get_Row(int row){
		return &(this->buffer[row * linestride]);
	}
}
