274 lines
6.0 KiB
C++
274 lines
6.0 KiB
C++
#include "render/dl_image.h"
|
||
|
||
#define STB_IMAGE_IMPLEMENTATION
|
||
#include <stb_image.h>
|
||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||
#include <stb_image_write.h>
|
||
#include <lunasvg.h>
|
||
#include <webp/decode.h>
|
||
|
||
#include "base/dl_color.h"
|
||
#include "dl_align.h"
|
||
#include "math/dl_math.h"
|
||
#include "io/dl_file.h"
|
||
|
||
namespace dl
|
||
{
|
||
|
||
dl::Image* createImage(const Size& size, const Color& col)
|
||
{
|
||
Image* ret = new Image(size);
|
||
ret->Fill(RGBA::Create(col));
|
||
return ret;
|
||
}
|
||
|
||
dl::Image* createImage(std::string_view path_name)
|
||
{
|
||
int texWidth, texHeight, texChannels;
|
||
stbi_uc* pixels = stbi_load(std::string{ path_name }.c_str(),
|
||
&texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
||
|
||
if (!pixels)
|
||
{
|
||
log_err("加载图像失败:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
Image* ret = new Image(
|
||
Size{ (unsigned)texWidth, (unsigned)texHeight },
|
||
(uint32_t*)pixels);
|
||
stbi_image_free(pixels);
|
||
return ret;
|
||
}
|
||
|
||
Image* createImageSvg(std::string_view path_name, const Size& size)
|
||
{
|
||
using namespace lunasvg;
|
||
auto doc = Document::loadFromFile(std::string{ path_name });
|
||
if (!doc)
|
||
{
|
||
log_err("加载svg图像失败:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
auto bitmap = doc->renderToBitmap(size[0], size[1], 0);
|
||
if (!bitmap.valid())
|
||
{
|
||
log_err("加载svg图像失败1:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
bitmap.convertToRGBA();
|
||
Image* ret = new Image(size, (uint32_t*)bitmap.data());
|
||
return ret;
|
||
}
|
||
|
||
Image* createImageWebp(std::string_view path_name)
|
||
{
|
||
Buffer buf = File::GetBuffer(path_name);
|
||
if (buf.empty())
|
||
return nullptr;
|
||
|
||
WebPDecoderConfig config;
|
||
if (WebPInitDecoderConfig(&config) == 0)
|
||
{
|
||
log_err("webp:InitDecoderConfig失败:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
if (WebPGetFeatures((uint8_t*)buf.data(), buf.size(), &config.input) != VP8_STATUS_OK)
|
||
{
|
||
log_err("webp:GetFeatures失败:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
if (config.input.width == 0 || config.input.height == 0)
|
||
{
|
||
log_err("webp:宽高某值为0:{}", path_name);
|
||
return nullptr;
|
||
}
|
||
|
||
config.output.colorspace = config.input.has_alpha ? MODE_rgbA : MODE_RGB;
|
||
//_renderFormat = config.input.has_alpha ? Texture2D::PixelFormat::RGBA8888 : Texture2D::PixelFormat::RGB888;
|
||
int w = config.input.width;
|
||
int h = config.input.height;
|
||
|
||
//we ask webp to give data with premultiplied alpha
|
||
//_hasPremultipliedAlpha = (config.input.has_alpha != 0);
|
||
|
||
//_dataLen = _width * _height * (config.input.has_alpha ? 4 : 3);
|
||
//_data = static_cast<unsigned char*>(malloc(_dataLen * sizeof(unsigned char)));
|
||
|
||
Image* ret = new Image(toSize(w, h));
|
||
|
||
config.output.u.RGBA.rgba = (uint8_t*)ret->GetData();
|
||
config.output.u.RGBA.stride = w * (config.input.has_alpha ? 4 : 3);
|
||
config.output.u.RGBA.size = ret->GetSizeByte();
|
||
config.output.is_external_memory = 1;
|
||
|
||
if (WebPDecode((uint8_t*)buf.data(), buf.size(), &config) != VP8_STATUS_OK)
|
||
{
|
||
delete ret;
|
||
return nullptr;
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
bool saveImage(const Image* img, std::string_view path_name)
|
||
{
|
||
return stbi_write_png(std::string{ path_name }.c_str(),
|
||
img->GetWidth(), img->GetHeight(),
|
||
4, img->GetBuffer(), 0);
|
||
}
|
||
|
||
|
||
|
||
|
||
namespace Img
|
||
{
|
||
void CalcLeastRect(const Image* img, RectU& rect)
|
||
{
|
||
using namespace Align;
|
||
assert(img);
|
||
//如果此行、或此列全是透明像素则移动
|
||
//移动p1.y
|
||
while (GetTop(rect) < GetButtom(rect))
|
||
{
|
||
bool all_zero = true;
|
||
for (unsigned x = GetLeft(rect); x < GetRight(rect); ++x)
|
||
{
|
||
if (ABGR::GetA(img->GetData()[x + GetTop(rect) * img->GetWidth()]) != 0)
|
||
{
|
||
all_zero = false;
|
||
break;
|
||
}
|
||
}
|
||
if (all_zero)
|
||
++GetTop(rect);
|
||
else
|
||
break;
|
||
}
|
||
|
||
//移动p2.y
|
||
while (GetButtom(rect) - 1 > GetTop(rect))
|
||
{
|
||
bool all_zero = true;
|
||
for (unsigned x = GetLeft(rect); x < GetRight(rect); ++x)
|
||
{
|
||
if (ABGR::GetA(img->GetData()[x + (GetButtom(rect) - 1) * img->GetWidth()]) != 0)
|
||
{
|
||
all_zero = false;
|
||
break;
|
||
}
|
||
}
|
||
if (all_zero)
|
||
--GetButtom(rect);
|
||
else
|
||
break;
|
||
}
|
||
|
||
//移动p1.x
|
||
while (GetLeft(rect) < GetRight(rect))
|
||
{
|
||
bool all_zero = true;
|
||
for (unsigned y = GetTop(rect); y < GetButtom(rect); ++y)
|
||
{
|
||
if (ABGR::GetA(img->GetData()[GetLeft(rect) + y * img->GetWidth()]) != 0)
|
||
{
|
||
all_zero = false;
|
||
break;
|
||
}
|
||
}
|
||
if (all_zero)
|
||
++GetLeft(rect);
|
||
else
|
||
break;
|
||
}
|
||
|
||
//移动p2.x
|
||
while (GetRight(rect) - 1 > GetLeft(rect))
|
||
{
|
||
bool all_zero = true;
|
||
for (unsigned y = GetTop(rect); y < GetButtom(rect); ++y)
|
||
{
|
||
if (ABGR::GetA(img->GetData()[GetRight(rect) - 1 + y * img->GetWidth()]) != 0)
|
||
{
|
||
all_zero = false;
|
||
break;
|
||
}
|
||
}
|
||
if (all_zero)
|
||
--GetRight(rect);
|
||
else
|
||
break;
|
||
}
|
||
}
|
||
|
||
bool IsHaveTranslucent(const Image* img, const RectU& rect)
|
||
{
|
||
if (!Math::IsRectInSize(rect, img->GetSize()))
|
||
{
|
||
log_err("判断区域越界:rect:{},size:{}", to_string(rect), to_string(img->GetSize()));
|
||
return false;
|
||
}
|
||
unsigned length = Accumulate::Mul(img->GetSize());
|
||
for (unsigned j = rect[1]; j < rect[3]; ++j)
|
||
{
|
||
for (unsigned i = rect[0]; i < rect[2]; ++i)
|
||
{
|
||
unsigned index = j * img->GetWidth() + i;
|
||
|
||
uint32_t A = ABGR::GetA(img->GetData()[index]);
|
||
if (A != 0xff000000 && A != 0)
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
void AddImageRect(Image* img_tar, const Image* img_src, const RectU& rect, const PointU& tar_xy)
|
||
{
|
||
assert(img_tar && img_src);
|
||
if (!img_src || img_src->IsEmpty())
|
||
{
|
||
log_err0("传入img无效!");
|
||
return;
|
||
}
|
||
|
||
if (!Math::IsRectInSize(rect, img_src->GetSize()))
|
||
{
|
||
log_err0("源图像区域无效!");
|
||
return;
|
||
}
|
||
|
||
Size img_size = img_src->GetSize();
|
||
Size s_size = Align::GetSize(rect);
|
||
|
||
if ((tar_xy[0] + s_size[0] > img_tar->GetWidth())
|
||
|| (tar_xy[1] + s_size[1] > img_tar->GetHeight()))
|
||
{
|
||
log_err0("目标图像过小!");
|
||
return;
|
||
}
|
||
|
||
uint32_t* buf_this = img_tar->GetData();
|
||
const uint32_t* buf_b = img_src->GetData();
|
||
//copy
|
||
for (size_t j = 0; j < s_size[1]; ++j)
|
||
{
|
||
memcpy(&(buf_this[(tar_xy[1] + j) * img_tar->GetWidth() + tar_xy[0]]),
|
||
&(buf_b[(rect[1] + j) * img_size[0] + rect[0]]),
|
||
size_t(s_size[0] * sizeof(uint32_t)));
|
||
}
|
||
}
|
||
|
||
void SetAlphaZero(Image* img)
|
||
{
|
||
size_t length = (size_t)img->GetSizeMul();
|
||
for (size_t i = 0; i != length; ++i)
|
||
{
|
||
if (ABGR::GetA(img->GetData()[i]) == 0)
|
||
{
|
||
img->GetData()[i] = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
} |