#include <tiffio.h>
#include "dms/TIFFImage.h"

namespace dms
{

TIFFImage::TIFFImage(char *filename)
    {
    if (filename)
        loadFile(filename);
    }


void TIFFImage::loadFile(char *filename)
    {
    TIFF *tif = TIFFOpen(filename, "r");
    if (tif)
        {
        uint32 * data;
        uint32 width, height;
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
        data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
        if ((data) &&
            (TIFFReadRGBAImage(tif, width, height, data, 0)))
                {
                setSize(width, height);
                setData(data);
                }
        else
            {
            if (data)
                _TIFFfree(data);
            }
        TIFFClose(tif);
        }
    }

}
