Files
dl_old/dl/render/dl_image.h
T
2023-09-06 18:42:16 +08:00

117 lines
3.0 KiB
C++

/**
* @file dl_image.h
* @brief 图像对象
*
*
* @version 1.1
* @author lveyou
* @date 21-08-09
*
* @note 基本的图像对象,使用stb_image库加载
* @note 基于4字节的n32颜色,内部通道为ABGR,接口使用Color.h规定的
* @note 注意转为char访问时,需要考虑字节序
*
* @note svg格式解析基于lunasvg
*/
#pragma once
#include <string>
#include "base/dl_vector2.h"
#include "dl_type.h"
#include "base/dl_range_any.h"
#include "memory/dl_buffer.h"
namespace dl
{
using Image = Vector2<uint32_t>;
/**
* @brief 创建图像
* @param[in] size 大小
* @param[in] color 颜色
* @retval 用户需要使用delete释放获得的指针,除非失败返回了nullptr
*/
Image* createImage(const Size& size, const Color& col);
/**
* @brief 创建图像
* @param[in] path_name 路径
* @retval 用户需要使用delete释放获得的指针,除非失败返回了nullptr
*/
Image* createImage(std::string_view path_name);
/**
* @brief 创建图像 从svg
* @param[in] path_name 路径
* @param[in] size 大小
* @retval 用户需要使用delete释放获得的指针,除非失败返回了nullptr
*/
Image* createImageSvg(std::string_view path_name, const Size& size);
/**
* @brief 创建图像 从webp
* @param[in] path_name 路径
* @retval 用户需要使用delete释放获得的指针,除非失败返回了nullptr
*/
Image* createImageWebp(std::string_view path_name);
Image* createImageWebp(BufferView buf);
/**
* @brief 保存到图像
*/
bool saveImage(const Image* img, std::string_view path_name);
namespace Img
{
/**
* @brief 是否支持的格式
* @param[in] str 格式,例如png
* @param[in] read 为真时检查可读取,反之为可保存
* @note 依赖stb_image库,注意必须全小写
*/
inline bool IsSupportFormat(std::string_view str, bool read)
{
constexpr std::array<std::string_view, 8> ARRAY_READ = {
"jpg", "png", "tga", "bmp", "psd", "gif", "hdr", "pic"
};
constexpr std::array<std::string_view, 3> ARRAY_WRITE = {
"png", "tag", "bmp"
};
return read ? Any::Equal(ARRAY_READ, str) : Any::Equal(ARRAY_WRITE, str);
}
/**
* @brief 计算返回最小包围盒(去除边缘的全透明像素)
* @param[in,out] rect 传入包围盒,返回缩小后的
*/
void CalcLeastRect(const Image* img, RectU& rect);
/**
* @brief 返回是否包含非透明颜色
* @note 只判断alpha通道
*/
bool IsHaveTranslucent(const Image* img, const RectU& rect);
/**
* @brief 添加另一个图像的部分到自己的位置上
* @param[in] img 另一个图像
* @param[in] rect 另一图像选择的区域
* @param[in] tar_xy 自己的位置
*
* @note 如果有超出源图像区域,或者超出目标图像区域,则什么都不做,只发出一条警告
*/
void AddImageRect(Image* img_tar, const Image* img_src, const RectU& rect, const PointU& tar_xy);
/**
* @brief 设置透明色颜色为0,针对于某些颜色透明,但是其他通道非0
*/
void SetAlphaZero(const Image* img);
}
}