Files
dl_old/dl/render/dl_image.cpp
T
2023-07-23 00:27:01 +08:00

44 lines
939 B
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 "base/dl_color.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);
}
}