Files
dl_old/dl/render/dl_image.cpp
T
2023-08-06 03:06:14 +08:00

200 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "render/dl_image.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "base/dl_color.h"
#include "dl_align.h"
#include "math/dl_math.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);
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;
}
}
}
}
}