2007-12-03 09:14:19 +00:00
|
|
|
// This code is in the public domain -- castanyo@yahoo.es
|
|
|
|
|
2010-05-27 23:18:39 +00:00
|
|
|
#include "Image.h"
|
|
|
|
#include "ImageIO.h"
|
2007-12-03 09:14:19 +00:00
|
|
|
|
2010-05-27 23:18:39 +00:00
|
|
|
#include "nvmath/Color.h"
|
2007-12-03 09:14:19 +00:00
|
|
|
|
2010-05-27 23:18:39 +00:00
|
|
|
#include "nvcore/Debug.h"
|
|
|
|
#include "nvcore/Ptr.h"
|
|
|
|
#include "nvcore/Utils.h" // swap
|
2007-12-03 09:14:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
|
|
|
|
Image::Image() : m_width(0), m_height(0), m_format(Format_RGB), m_data(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-03-05 19:11:41 +00:00
|
|
|
Image::Image(const Image & img) : m_data(NULL)
|
2008-02-03 07:34:32 +00:00
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
allocate(img.m_width, img.m_height);
|
|
|
|
m_format = img.m_format;
|
|
|
|
memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height);
|
2008-02-03 07:34:32 +00:00
|
|
|
}
|
|
|
|
|
2007-12-03 09:14:19 +00:00
|
|
|
Image::~Image()
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
free();
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
2008-02-03 07:34:32 +00:00
|
|
|
const Image & Image::operator=(const Image & img)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
allocate(img.m_width, img.m_height);
|
|
|
|
m_format = img.m_format;
|
|
|
|
memcpy(m_data, img.m_data, sizeof(Color32) * m_width * m_height);
|
|
|
|
return *this;
|
2008-02-03 07:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-03 09:14:19 +00:00
|
|
|
void Image::allocate(uint w, uint h)
|
|
|
|
{
|
2010-10-06 02:56:35 +00:00
|
|
|
free();
|
2010-05-27 23:18:39 +00:00
|
|
|
m_width = w;
|
|
|
|
m_height = h;
|
2010-10-21 18:44:10 +00:00
|
|
|
m_data = realloc<Color32>(m_data, w * h);
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Image::load(const char * name)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
free();
|
|
|
|
|
|
|
|
AutoPtr<Image> img(ImageIO::load(name));
|
|
|
|
if (img == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
swap(m_width, img->m_width);
|
|
|
|
swap(m_height, img->m_height);
|
|
|
|
swap(m_format, img->m_format);
|
|
|
|
swap(m_data, img->m_data);
|
|
|
|
|
|
|
|
return true;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Image::wrap(void * data, uint w, uint h)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
free();
|
|
|
|
m_data = (Color32 *)data;
|
|
|
|
m_width = w;
|
|
|
|
m_height = h;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Image::unwrap()
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
m_data = NULL;
|
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Image::free()
|
|
|
|
{
|
2010-10-06 02:56:35 +00:00
|
|
|
::free(m_data);
|
2010-05-27 23:18:39 +00:00
|
|
|
m_data = NULL;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint Image::width() const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
return m_width;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint Image::height() const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
return m_height;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color32 * Image::scanline(uint h) const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
nvDebugCheck(h < m_height);
|
|
|
|
return m_data + h * m_width;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Color32 * Image::scanline(uint h)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
nvDebugCheck(h < m_height);
|
|
|
|
return m_data + h * m_width;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color32 * Image::pixels() const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
return m_data;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Color32 * Image::pixels()
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
return m_data;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color32 & Image::pixel(uint idx) const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
nvDebugCheck(idx < m_width * m_height);
|
|
|
|
return m_data[idx];
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Color32 & Image::pixel(uint idx)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
nvDebugCheck(idx < m_width * m_height);
|
|
|
|
return m_data[idx];
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Image::Format Image::format() const
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
return m_format;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Image::setFormat(Image::Format f)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
m_format = f;
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Image::fill(Color32 c)
|
|
|
|
{
|
2010-05-27 23:18:39 +00:00
|
|
|
const uint size = m_width * m_height;
|
|
|
|
for (uint i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
m_data[i] = c;
|
|
|
|
}
|
2007-12-03 09:14:19 +00:00
|
|
|
}
|
|
|
|
|