2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt / Thomas Alten
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CImage.h"
|
|
|
|
#include "irrString.h"
|
|
|
|
#include "CColorConverter.h"
|
|
|
|
#include "CBlit.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "SoftwareDriver2_helper.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace video
|
|
|
|
{
|
|
|
|
|
|
|
|
//! Constructor from raw data
|
2024-03-20 19:35:52 +01:00
|
|
|
CImage::CImage(ECOLOR_FORMAT format, const core::dimension2d<u32> &size, void *data,
|
|
|
|
bool ownForeignMemory, bool deleteMemory) :
|
|
|
|
IImage(format, size, deleteMemory)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (ownForeignMemory) {
|
|
|
|
Data = (u8 *)data;
|
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 dataSize = getDataSizeFromFormat(Format, Size.Width, Size.Height);
|
2024-03-10 13:57:48 +01:00
|
|
|
const u32 allocSize = align_next(dataSize, 16);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-10 13:57:48 +01:00
|
|
|
// allocate as u32 to ensure enough alignment when casted
|
2024-03-20 19:35:52 +01:00
|
|
|
Data = reinterpret_cast<u8 *>(new u32[allocSize / 4]);
|
2023-10-03 20:37:00 +02:00
|
|
|
memcpy(Data, data, dataSize);
|
|
|
|
DeleteMemory = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Constructor of empty image
|
2024-03-20 19:35:52 +01:00
|
|
|
CImage::CImage(ECOLOR_FORMAT format, const core::dimension2d<u32> &size) :
|
|
|
|
IImage(format, size, true)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-10 13:57:48 +01:00
|
|
|
const u32 dataSize = getDataSizeFromFormat(Format, Size.Width, Size.Height);
|
|
|
|
const u32 allocSize = align_next(dataSize, 16);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
Data = reinterpret_cast<u8 *>(new u32[allocSize / 4]);
|
2023-10-03 20:37:00 +02:00
|
|
|
DeleteMemory = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! sets a pixel
|
|
|
|
void CImage::setPixel(u32 x, u32 y, const SColor &color, bool blend)
|
|
|
|
{
|
|
|
|
if (x >= Size.Width || y >= Size.Height)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (Format) {
|
|
|
|
case ECF_A1R5G5B5: {
|
|
|
|
u16 *dest = (u16 *)(Data + (y * Pitch) + (x << 1));
|
|
|
|
*dest = video::A8R8G8B8toA1R5G5B5(color.color);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ECF_R5G6B5: {
|
|
|
|
u16 *dest = (u16 *)(Data + (y * Pitch) + (x << 1));
|
|
|
|
*dest = video::A8R8G8B8toR5G6B5(color.color);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ECF_R8G8B8: {
|
|
|
|
u8 *dest = Data + (y * Pitch) + (x * 3);
|
|
|
|
dest[0] = (u8)color.getRed();
|
|
|
|
dest[1] = (u8)color.getGreen();
|
|
|
|
dest[2] = (u8)color.getBlue();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ECF_A8R8G8B8: {
|
|
|
|
u32 *dest = (u32 *)(Data + (y * Pitch) + (x << 2));
|
|
|
|
*dest = blend ? PixelBlend32(*dest, color.color) : color.color;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case ECF_UNKNOWN:
|
|
|
|
os::Printer::log("IImage::setPixel unknown format.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns a pixel
|
|
|
|
SColor CImage::getPixel(u32 x, u32 y) const
|
|
|
|
{
|
|
|
|
if (x >= Size.Width || y >= Size.Height)
|
|
|
|
return SColor(0);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (Format) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case ECF_A1R5G5B5:
|
2024-03-20 19:35:52 +01:00
|
|
|
return A1R5G5B5toA8R8G8B8(((u16 *)Data)[y * Size.Width + x]);
|
2023-10-03 20:37:00 +02:00
|
|
|
case ECF_R5G6B5:
|
2024-03-20 19:35:52 +01:00
|
|
|
return R5G6B5toA8R8G8B8(((u16 *)Data)[y * Size.Width + x]);
|
2023-10-03 20:37:00 +02:00
|
|
|
case ECF_A8R8G8B8:
|
2024-03-20 19:35:52 +01:00
|
|
|
return ((u32 *)Data)[y * Size.Width + x];
|
|
|
|
case ECF_R8G8B8: {
|
|
|
|
u8 *p = Data + (y * 3) * Size.Width + (x * 3);
|
|
|
|
return SColor(255, p[0], p[1], p[2]);
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
case ECF_UNKNOWN:
|
|
|
|
os::Printer::log("IImage::getPixel unknown format.", ELL_WARNING);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SColor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another at given position
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyTo(IImage *target, const core::position2d<s32> &pos)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyTo method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!Blit(BLITTER_TEXTURE, target, 0, &pos, this, 0, 0) && target && pos.X == 0 && pos.Y == 0 &&
|
|
|
|
CColorConverter::canConvertFormat(Format, target->getColorFormat())) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// No fast blitting, but copyToScaling uses other color conversions and might work
|
|
|
|
irr::core::dimension2du dim(target->getDimension());
|
|
|
|
copyToScaling(target->getData(), dim.Width, dim.Height, target->getColorFormat(), target->getPitch());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface partially into another at given position
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyTo(IImage *target, const core::position2d<s32> &pos, const core::rect<s32> &sourceRect, const core::rect<s32> *clipRect)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyTo method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Blit(BLITTER_TEXTURE, target, clipRect, &pos, this, &sourceRect, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another, using the alpha mask, a cliprect and a color to add with
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyToWithAlpha(IImage *target, const core::position2d<s32> &pos, const core::rect<s32> &sourceRect, const SColor &color, const core::rect<s32> *clipRect, bool combineAlpha)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyToWithAlpha method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
eBlitter op = combineAlpha ? BLITTER_TEXTURE_COMBINE_ALPHA : color.color == 0xFFFFFFFF ? BLITTER_TEXTURE_ALPHA_BLEND
|
|
|
|
: BLITTER_TEXTURE_ALPHA_COLOR_BLEND;
|
|
|
|
Blit(op, target, clipRect, &pos, this, &sourceRect, color.color);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another, if it has the exact same size and format.
|
|
|
|
bool CImage::copyToNoScaling(void *target, u32 width, u32 height, ECOLOR_FORMAT format, u32 pitch) const
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyToNoScaling method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target || !width || !height || !Size.Width || !Size.Height)
|
|
|
|
return false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const u32 bpp = getBitsPerPixelFromFormat(format) / 8;
|
|
|
|
if (0 == pitch)
|
|
|
|
pitch = width * bpp;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!(Format == format && Size.Width == width && Size.Height == height))
|
2023-10-03 20:37:00 +02:00
|
|
|
return false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (pitch == Pitch) {
|
|
|
|
memcpy(target, Data, (size_t)height * pitch);
|
|
|
|
} else {
|
|
|
|
u8 *tgtpos = (u8 *)target;
|
|
|
|
u8 *srcpos = Data;
|
|
|
|
const u32 bwidth = width * bpp;
|
|
|
|
const u32 rest = pitch - bwidth;
|
|
|
|
for (u32 y = 0; y < height; ++y) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// copy scanline
|
|
|
|
memcpy(tgtpos, srcpos, bwidth);
|
|
|
|
// clear pitch
|
2024-03-20 19:35:52 +01:00
|
|
|
memset(tgtpos + bwidth, 0, rest);
|
2023-10-03 20:37:00 +02:00
|
|
|
tgtpos += pitch;
|
|
|
|
srcpos += Pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another, scaling it to the target image size
|
|
|
|
// note: this is very very slow.
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyToScaling(void *target, u32 width, u32 height, ECOLOR_FORMAT format, u32 pitch)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyToScaling method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target || !width || !height || !Size.Width || !Size.Height)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const u32 bpp = getBitsPerPixelFromFormat(format) / 8;
|
|
|
|
if (0 == pitch)
|
|
|
|
pitch = width * bpp;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (copyToNoScaling(target, width, height, format, pitch))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// NOTE: Scaling is coded to keep the border pixels intact.
|
|
|
|
// Alternatively we could for example work with first pixel being taken at half step-size.
|
|
|
|
// Then we have one more step here and it would be:
|
|
|
|
// sourceXStep = (f32)(Size.Width-1) / (f32)(width);
|
|
|
|
// And sx would start at 0.5f + sourceXStep / 2.f;
|
|
|
|
// Similar for y.
|
|
|
|
// As scaling is done without any antialiasing it doesn't matter too much which outermost pixels we use and keeping
|
|
|
|
// border pixels intact is probably mostly better (with AA the other solution would be more correct).
|
|
|
|
// This is however unnecessary (and unexpected) for scaling to integer multiples, so don't do it there.
|
|
|
|
f32 sourceXStep, sourceYStep;
|
|
|
|
f32 sourceXStart = 0.f, sourceYStart = 0.f;
|
|
|
|
if (width % Size.Width == 0)
|
|
|
|
sourceXStep = (f32)(Size.Width) / (f32)(width);
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
|
|
|
sourceXStep = width > 1 ? (f32)(Size.Width - 1) / (f32)(width - 1) : 0.f;
|
|
|
|
sourceXStart = 0.5f; // for rounding to nearest pixel
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
if (height % Size.Height == 0)
|
|
|
|
sourceYStep = (f32)(Size.Height) / (f32)(height);
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
|
|
|
sourceYStep = height > 1 ? (f32)(Size.Height - 1) / (f32)(height - 1) : 0.f;
|
|
|
|
sourceYStart = 0.5f; // for rounding to nearest pixel
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
s32 yval = 0, syval = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
f32 sy = sourceYStart;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 y = 0; y < height; ++y) {
|
2023-10-03 20:37:00 +02:00
|
|
|
f32 sx = sourceXStart;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 x = 0; x < width; ++x) {
|
|
|
|
CColorConverter::convert_viaFormat(Data + syval + ((s32)sx) * BytesPerPixel, Format, 1, ((u8 *)target) + yval + (x * bpp), format);
|
|
|
|
sx += sourceXStep;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
sy += sourceYStep;
|
|
|
|
syval = (s32)(sy)*Pitch;
|
|
|
|
yval += pitch;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another, scaling it to the target image size
|
|
|
|
// note: this is very very slow.
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyToScaling(IImage *target)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyToScaling method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const core::dimension2d<u32> &targetSize = target->getDimension();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (targetSize == Size) {
|
2023-10-03 20:37:00 +02:00
|
|
|
copyTo(target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
copyToScaling(target->getData(), targetSize.Width, targetSize.Height, target->getColorFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies this surface into another, scaling it to fit it.
|
2024-03-20 19:35:52 +01:00
|
|
|
void CImage::copyToScalingBoxFilter(IImage *target, s32 bias, bool blend)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::copyToScalingBoxFilter method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const core::dimension2d<u32> destSize = target->getDimension();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const f32 sourceXStep = (f32)Size.Width / (f32)destSize.Width;
|
|
|
|
const f32 sourceYStep = (f32)Size.Height / (f32)destSize.Height;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
s32 fx = core::ceil32(sourceXStep);
|
|
|
|
s32 fy = core::ceil32(sourceYStep);
|
2023-10-03 20:37:00 +02:00
|
|
|
f32 sx;
|
|
|
|
f32 sy;
|
|
|
|
|
|
|
|
sy = 0.f;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 y = 0; y != destSize.Height; ++y) {
|
2023-10-03 20:37:00 +02:00
|
|
|
sx = 0.f;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 x = 0; x != destSize.Width; ++x) {
|
|
|
|
target->setPixel(x, y,
|
|
|
|
getPixelBox(core::floor32(sx), core::floor32(sy), fx, fy, bias), blend);
|
2023-10-03 20:37:00 +02:00
|
|
|
sx += sourceXStep;
|
|
|
|
}
|
|
|
|
sy += sourceYStep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! fills the surface with given color
|
|
|
|
void CImage::fill(const SColor &color)
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::fill method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 c;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (Format) {
|
|
|
|
case ECF_A1R5G5B5:
|
|
|
|
c = color.toA1R5G5B5();
|
|
|
|
c |= c << 16;
|
|
|
|
break;
|
|
|
|
case ECF_R5G6B5:
|
|
|
|
c = video::A8R8G8B8toR5G6B5(color.color);
|
|
|
|
c |= c << 16;
|
|
|
|
break;
|
|
|
|
case ECF_A8R8G8B8:
|
|
|
|
c = color.color;
|
2023-10-03 20:37:00 +02:00
|
|
|
break;
|
2024-03-20 19:35:52 +01:00
|
|
|
case ECF_R8G8B8: {
|
|
|
|
u8 rgb[3];
|
|
|
|
CColorConverter::convert_A8R8G8B8toR8G8B8(&color, 1, rgb);
|
|
|
|
const u32 size = getImageDataSizeInBytes();
|
|
|
|
for (u32 i = 0; i < size; i += 3) {
|
|
|
|
memcpy(Data + i, rgb, 3);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} break;
|
|
|
|
default:
|
2023-10-03 20:37:00 +02:00
|
|
|
// TODO: Handle other formats
|
2024-03-20 19:35:52 +01:00
|
|
|
return;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
memset32(Data, c, getImageDataSizeInBytes());
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! get a filtered pixel
|
2024-03-20 19:35:52 +01:00
|
|
|
inline SColor CImage::getPixelBox(s32 x, s32 y, s32 fx, s32 fy, s32 bias) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IImage::isCompressedFormat(Format)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("IImage::getPixelBox method doesn't work with compressed images.", ELL_WARNING);
|
|
|
|
return SColor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SColor c;
|
|
|
|
s32 a = 0, r = 0, g = 0, b = 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (s32 dx = 0; dx != fx; ++dx) {
|
|
|
|
for (s32 dy = 0; dy != fy; ++dy) {
|
|
|
|
c = getPixel(core::s32_min(x + dx, Size.Width - 1),
|
|
|
|
core::s32_min(y + dy, Size.Height - 1));
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
a += c.getAlpha();
|
|
|
|
r += c.getRed();
|
|
|
|
g += c.getGreen();
|
|
|
|
b += c.getBlue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 sdiv = s32_log2_s32(fx * fy);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
a = core::s32_clamp((a >> sdiv) + bias, 0, 255);
|
|
|
|
r = core::s32_clamp((r >> sdiv) + bias, 0, 255);
|
|
|
|
g = core::s32_clamp((g >> sdiv) + bias, 0, 255);
|
|
|
|
b = core::s32_clamp((b >> sdiv) + bias, 0, 255);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
c.set(a, r, g, b);
|
2023-10-03 20:37:00 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace video
|
|
|
|
} // end namespace irr
|