Files
dl_old/dl/render/dl_image.cpp
T
2023-07-09 20:41:09 +08:00

42 lines
908 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>
namespace dl
{
dl::Image* CreateImage(const Size& size, const Color& col)
{
Image* ret = new Image(size);
ret->Fill(col);
return ret;
}
dl::Image* CreateImage(std::string_view path_name)
{
int texWidth, texHeight, texChannels;
stbi_uc* pixels = stbi_load(string{ path_name }.c_str(),
&texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
if (!pixels)
{
debug_err(format("¼ÓÔØÍ¼Ïñʧ°Ü£º{}", path_name));
return nullptr;
}
Image* ret = new Image;
ret->_data = (uint32_t*)pixels;
ret->_size = { (unsigned)texWidth, (unsigned)texHeight };
return ret;
}
bool SaveImage(const Image* img, std::string_view path_name)
{
return stbi_write_png(string{ path_name }.c_str(),
img->GetWidth(), img->GetSize(),
4, img->GetBuffer(), 0);
}
}