1 Star 0 Fork 4

AlongsCode/eapi

forked from 精易科技/eapi 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
eapi_92_GetPictureFormat.cpp 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
AlongsCode 提交于 2023-02-11 03:01 +08:00 . 重构完成,反馈的问题全部修复完了
#include "..\include_eapi_header.h"
#include<map>
#include<fstream>
#include <iomanip>
#include <sstream>
#include <algorithm>
inline std::string byte2hex(const std::vector<unsigned char>& data) {
std::stringstream result;
result << std::hex << std::setfill('0');
for (unsigned char b : data) {
result << std::setw(2) << static_cast<int>(b);
}
std::string str = result.str();
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
// 调用格式: SDT_TEXT 取图片格式, 命令说明: "获取指定图片格式,失败返回空"
// 参数<1>: 图片路径或数据 _SDT_ALL, 参数说明: "可以选择图片路径,或者内存图片字节集。"
EAPI_EXTERN_C void eapi_GetPictureFormat_92_eapi(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
static std::map<std::string, const char*> pictureFormats = {
{"FFD8FF", "JPEG"},
{"89504E470D0A1A0A0000000D49484452", "PNG"},
{"47494638", "GIF"},
{"474946383761", "GIF"},
{"474946383961", "GIF"},
{"49492A00", "TIFF"},
{"4D4D002A", "TIFF"},
{"424D", "BMP"},
{"0A", "PCX"},
{"3C", "SVG"},
{"38425053000100000000000000", "PSD"},
{"465753", "SWF"},
{"D7CDC69A0000", "WMF"},
{"01000000", "EMF"},
{"C5D0D3C6", "EPS"},
{"0000020000", "TGA"},
{"0000100000", "TGA"},
{"00000100", "ICO"},
{"00000200", "CUR"},
{"52494646", "ANI"},
{"464F524D", "IFF"}
};
if (pArgInf[0].m_dtDataType == SDT_TEXT && pArgInf->m_pText)
{
auto GetPictureFormat = [](const std::string_view& path)->char*
{
std::ifstream file(path.data(), std::ios::binary);
if (!file) {
return nullptr;
}
std::vector<unsigned char> buffer(16);
file.read(reinterpret_cast<char*>(buffer.data()), 16);
file.close();
std::string hex = byte2hex(buffer);
for (const auto& [formatHex, formatName] : pictureFormats) {
if (hex.substr(0, formatHex.size()) == formatHex) {
return CloneTextData(formatName);
}
}
return nullptr;
};
pRetData->m_pText = GetPictureFormat(pArgInf[0].m_pText);
}
else if (pArgInf[0].m_dtDataType == SDT_BIN)
{
auto GetPictureFormat = [](unsigned char* pBuffer, size_t lenth)->char* {
if (!pBuffer || lenth < 16) {
return nullptr;
}
std::vector<unsigned char> buffer = std::vector<unsigned char>(pBuffer, pBuffer + 16);
std::string hex = byte2hex(buffer);
for (const auto& [formatHex, formatName] : pictureFormats) {
if (hex.substr(0, formatHex.size()) == formatHex) {
return CloneTextData(formatName);
}
}
return nullptr;
};
pRetData->m_pText = GetPictureFormat(get_ebin_data(pArgInf[0].m_pBin), get_ebin_lenth(pArgInf[0].m_pBin));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/alongscode/eapi.git
git@gitee.com:alongscode/eapi.git
alongscode
eapi
eapi
master

搜索帮助