80 lines
1.7 KiB
C++
80 lines
1.7 KiB
C++
#include "asset/dvc_image.h"
|
|
#include "math/dvc_math_util.h"
|
|
#include "device/dvc_device.h"
|
|
#include "device/dvc_buffer.h"
|
|
|
|
DV_CORE_BEGIN_NAMESPACE
|
|
|
|
Image::Image(int32_t id)
|
|
: m_id(id)
|
|
{
|
|
}
|
|
|
|
Image::~Image()
|
|
{
|
|
}
|
|
|
|
void Image::clear()
|
|
{
|
|
m_width = 0;
|
|
m_height = 0;
|
|
m_pixelFormat = PF_UNKNOWN;
|
|
m_pixelByteSize = 0;
|
|
m_pixelBuffer = nullptr;
|
|
}
|
|
|
|
fVector4 Image::getPixel(int32_t u, int32_t v) const
|
|
{
|
|
if(u<0 || u>=m_width || v<0 || v>=m_height)
|
|
{
|
|
return fVector4::ZERO;
|
|
}
|
|
|
|
const uint8_t* p = (const uint8_t*)(m_pixelBuffer->ptr(0)) + (v * m_width + u) * m_pixelByteSize;
|
|
switch (m_pixelFormat)
|
|
{
|
|
case PF_UINT8_ALPHA:
|
|
return fVector4(0, 0, 0, p[0] / 255.0f);
|
|
case PF_UINT8_RGB:
|
|
return fVector4(p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, 1.0f);
|
|
case PF_UINT8_RGBA:
|
|
return fVector4(p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, p[3] / 255.0f);
|
|
case PF_FLOAT32_ALPHA:
|
|
return fVector4(0, 0, 0, *((const float*)p));
|
|
case PF_FLOAT32_RGB:
|
|
{
|
|
const float* fp = (const float*)p;
|
|
return fVector4(fp[0], fp[1], fp[2], 1.0f);
|
|
}
|
|
case PF_FLOAT32_RGBA:
|
|
{
|
|
const float* fp = (const float*)p;
|
|
return fVector4(fp[0], fp[1], fp[2], fp[3]);
|
|
}
|
|
default:
|
|
return fVector4::ZERO;
|
|
}
|
|
}
|
|
|
|
bool Image::initWithPixelData(Device& device, int32_t width, int32_t height,
|
|
PixelFormat pixelFormat, const uint8_t* data, size_t dataSize)
|
|
{
|
|
clear();
|
|
|
|
size_t pixelByteSize = MathUtil::PixelSize(pixelFormat);
|
|
if (dataSize != pixelByteSize*width*height)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_width = width;
|
|
m_height = height;
|
|
m_pixelFormat = pixelFormat;
|
|
m_pixelByteSize = pixelByteSize;
|
|
m_pixelBuffer = device.newDeviceBuffer(dataSize);
|
|
memcpy(m_pixelBuffer->ptr(0), data, dataSize);
|
|
return true;
|
|
}
|
|
|
|
DV_CORE_END_NAMESPACE
|