|
8 | 8 | #include <sstream> |
9 | 9 |
|
10 | 10 | #include "image_loader.h" |
| 11 | +#include "string_utils.h" |
| 12 | +#include "assert.h" |
| 13 | + |
| 14 | +std::string ToMBString(std::wstring_view s) { |
| 15 | + if (s.size() >= static_cast<size_t>(std::numeric_limits<int>::max())) throw std::runtime_error("length overflow"); |
| 16 | + |
| 17 | + const int src_len = static_cast<int>(s.size() + 1); |
| 18 | + const int len = WideCharToMultiByte(CP_ACP, 0, s.data(), src_len, nullptr, 0, nullptr, nullptr); |
| 19 | + assert(len > 0); |
| 20 | + std::string ret(static_cast<size_t>(len) - 1, '\0'); |
| 21 | +#pragma warning(disable : 4189) |
| 22 | + const int r = WideCharToMultiByte(CP_ACP, 0, s.data(), src_len, (char*)ret.data(), len, nullptr, nullptr); |
| 23 | + assert(len == r); |
| 24 | +#pragma warning(default : 4189) |
| 25 | + return ret; |
| 26 | +} |
| 27 | + |
| 28 | +std::string ToUTF8String(std::wstring_view s) { |
| 29 | + if (s.size() >= static_cast<size_t>(std::numeric_limits<int>::max())) throw std::runtime_error("length overflow"); |
| 30 | + |
| 31 | + const int src_len = static_cast<int>(s.size() + 1); |
| 32 | + const int len = WideCharToMultiByte(CP_UTF8, 0, s.data(), src_len, nullptr, 0, nullptr, nullptr); |
| 33 | + assert(len > 0); |
| 34 | + std::string ret(static_cast<size_t>(len) - 1, '\0'); |
| 35 | +#pragma warning(disable : 4189) |
| 36 | + const int r = WideCharToMultiByte(CP_UTF8, 0, s.data(), src_len, (char*)ret.data(), len, nullptr, nullptr); |
| 37 | + assert(len == r); |
| 38 | +#pragma warning(default : 4189) |
| 39 | + return ret; |
| 40 | +} |
| 41 | + |
11 | 42 |
|
12 | 43 | bool CreateImageLoader(void** out) { |
13 | 44 | IWICImagingFactory* piFactory; |
@@ -84,24 +115,24 @@ OrtStatus* LoadImageFromFileAndCrop(void* loader, const ORTCHAR_T* filename, dou |
84 | 115 | rect.Width = bbox_w_size; |
85 | 116 |
|
86 | 117 | ATLENSURE_SUCCEEDED(ppIFormatConverter->CopyPixels(&rect, stride, static_cast<UINT>(data.size()), data.data())); |
87 | | - float* float_file_data = new float[data.size()]; |
| 118 | + std::unique_ptr<float[]> float_file_data(new float[data.size()]); |
88 | 119 | size_t len = data.size(); |
89 | 120 | for (size_t i = 0; i != len; ++i) { |
90 | | - float_file_data[i] = static_cast<float>(data[i]) / 255; |
| 121 | + float_file_data.get()[i] = static_cast<float>(data[i]) / 255; |
91 | 122 | } |
92 | 123 |
|
93 | | - *out = float_file_data; |
| 124 | + *out = float_file_data.release(); |
94 | 125 | *out_width = bbox_w_size; |
95 | 126 | *out_height = bbox_h_size; |
96 | 127 | return nullptr; |
97 | 128 | } catch (const std::exception& ex) { |
98 | | - std::ostringstream oss; |
| 129 | + std::basic_ostringstream<ORTCHAR_T> oss; |
99 | 130 | oss << "Load " << filename << " failed:" << ex.what(); |
100 | | - return Ort::GetApi().CreateStatus(ORT_FAIL, oss.str().c_str()); |
| 131 | + return Ort::GetApi().CreateStatus(ORT_FAIL, ToUTF8String(oss.str()).c_str()); |
101 | 132 | } catch (const CAtlException& ex) { |
102 | | - std::ostringstream oss; |
| 133 | + std::basic_ostringstream<ORTCHAR_T> oss; |
103 | 134 | oss << "Load " << filename << " failed:"; |
104 | 135 | PrintErrorDescription(ex.m_hr, oss); |
105 | | - return Ort::GetApi().CreateStatus(ORT_FAIL, oss.str().c_str()); |
| 136 | + return Ort::GetApi().CreateStatus(ORT_FAIL, ToUTF8String(oss.str()).c_str()); |
106 | 137 | } |
107 | 138 | } |
0 commit comments