#include <stdio.h>
#include <malloc.h>
#include "dms/Image.h"

namespace dms
{

Image::Image(void)
    {
    width_ = height_ = 0;
    imagedata_ = 0;
    allocatedData_ = false;
    }


Image::Image(GLsizei width, GLsizei height, void *data)
    {
    width_ = width;
    height_ = height;
    imagedata_ = data;
    allocatedData_ = false;
    if (!imagedata_)
        allocateData();
    }


Image::~Image(void)
    {
    if (allocatedData_)
        freeData();
    }


void Image::setSize(GLsizei width, GLsizei height)
    {
    width_ = width;
    height_ = height;
    }


void Image::setData(void *data)
    {
    imagedata_ = data;
    allocatedData_ = false;
    }


void Image::allocateData(void)
    {
    imagedata_ = calloc(1, width_ * height_ * sizeof(unsigned long));
    if (!imagedata_)
        perror("Image::allocateData(): calloc()");
    else
        allocatedData_ = true;
    }


void Image::freeData(void)
    {
    if (imagedata_)
        free(imagedata_);
    imagedata_ = 0;
    allocatedData_ = false;
    }

}
