7 #include <boost/core/ignore_unused.hpp> 8 #include <boost/format.hpp> 9 #include <boost/core/ignore_unused.hpp> 10 #include <boost/numeric/conversion/cast.hpp> 14 #define STB_IMAGE_IMPLEMENTATION 15 #include <stb/stb_image.h> 17 #define STB_IMAGE_RESIZE_IMPLEMENTATION 18 #include <stb/stb_image_resize.h> 20 #define STB_IMAGE_WRITE_IMPLEMENTATION 21 #include <stb/stb_image_write.h> 28 switch (channelLayout)
31 return static_cast<unsigned int>(channel);
33 return 2u -
static_cast<unsigned int>(channel);
36 % static_cast<int>(channelLayout)));
40 inline float Lerp(
float a,
float b,
float w)
42 return w * b + (1.f - w) * a;
45 inline void PutData(std::vector<float> & data,
46 const unsigned int width,
52 data[(3*((y*width)+x)) + c] = value;
56 const unsigned int outputWidth,
57 const unsigned int outputHeight,
59 const std::array<float, 3>& mean,
60 const std::array<float, 3>& stddev)
62 std::vector<float> out;
63 out.resize(outputWidth * outputHeight * 3);
69 const unsigned int inputWidth = image.
GetWidth();
70 const unsigned int inputHeight = image.
GetHeight();
74 const float scaleY = boost::numeric_cast<
float>(inputHeight) / boost::numeric_cast<float>(outputHeight);
75 const float scaleX = boost::numeric_cast<
float>(inputWidth) / boost::numeric_cast<float>(outputWidth);
82 for (
unsigned int y = 0; y < outputHeight; ++y)
85 const float iy = boost::numeric_cast<
float>(y) * scaleY;
88 const float fiy = floorf(iy);
89 const unsigned int y0 = boost::numeric_cast<
unsigned int>(fiy);
92 const float yw = iy - fiy;
94 for (
unsigned int x = 0; x < outputWidth; ++x)
97 const float ix = boost::numeric_cast<
float>(x) * scaleX;
98 const float fix = floorf(ix);
99 const unsigned int x0 = boost::numeric_cast<
unsigned int>(fix);
102 const float xw = ix - fix;
105 const unsigned int x1 = std::min(x0 + 1, inputWidth - 1u);
106 const unsigned int y1 = std::min(y0 + 1, inputHeight - 1u);
113 for (
unsigned c=0; c<3; ++c)
115 const float ly0 = Lerp(
float(rgb_x0y0[c]),
float(rgb_x1y0[c]), xw);
116 const float ly1 = Lerp(
float(rgb_x0y1[c]),
float(rgb_x1y1[c]), xw);
117 const float l = Lerp(ly0, ly1, yw);
118 PutData(out, outputWidth, x, y, c, ((l / scale) - mean[c]) / stddev[c]);
136 using StbImageDataPtr = std::unique_ptr<unsigned char, decltype(&stbi_image_free)>;
137 StbImageDataPtr stbData(stbi_load(filePath, &width, &height, &channels, 0), &stbi_image_free);
139 if (stbData ==
nullptr)
144 if (width == 0 || height == 0)
149 m_Width = boost::numeric_cast<
unsigned int>(width);
150 m_Height = boost::numeric_cast<
unsigned int>(height);
151 m_NumChannels = boost::numeric_cast<
unsigned int>(channels);
154 m_Data.resize(sizeInBytes);
155 memcpy(m_Data.data(), stbData.get(), sizeInBytes);
160 if (x >= m_Width || y >= m_Height)
163 "Requested (%1%, %2%). Maximum valid coordinates (%3%, %4%).") % x % y % (m_Width - 1) % (m_Height - 1)));
167 const uint8_t*
const pixelData = m_Data.data() + pixelOffset;
170 std::array<uint8_t, 3> outPixelData;
171 outPixelData.fill(0);
173 const unsigned int maxChannelsInPixel = std::min(
GetNumChannels(), static_cast<unsigned int>(outPixelData.size()));
174 for (
unsigned int c = 0; c < maxChannelsInPixel; ++c)
176 outPixelData[c] = pixelData[c];
179 return std::make_tuple(outPixelData[0], outPixelData[1], outPixelData[2]);
185 std::vector<uint8_t> newData;
186 newData.resize(newWidth * newHeight * im.
GetNumChannels() * im.GetSingleElementSizeInBytes());
191 const int nW = boost::numeric_cast<
int>(newWidth);
192 const int nH = boost::numeric_cast<
int>(newHeight);
194 const int w =
static_cast<int>(im.
GetWidth());
195 const int h =
static_cast<int>(im.
GetHeight());
198 const int res = stbir_resize_uint8(im.m_Data.data(), w, h, 0, newData.data(), nW, nH, 0, numChannels);
204 im.m_Data.swap(newData);
205 im.m_Width = newWidth;
206 im.m_Height = newHeight;
210 unsigned int newHeight,
213 const std::array<float, 3>& mean,
214 const std::array<float, 3>& stddev,
217 std::vector<float> out;
218 if (newWidth == 0 || newHeight == 0)
221 "operation can be zero. Requested width: %1%. Requested height: %2%.") % newWidth % newHeight));
232 out = ResizeBilinearAndNormalize(*
this, newWidth, newHeight, scale, mean, stddev);
237 boost::format(
"Unknown resizing method asked ArmNN only supports {STB, BilinearAndNormalized} %1%")
245 const int w =
static_cast<int>(
GetWidth());
246 const int h =
static_cast<int>(
GetHeight());
254 res = stbi_write_png(filePath, w, h, numChannels, m_Data.data(), 0);
259 res = stbi_write_bmp(filePath, w, h, numChannels, m_Data.data());
264 res = stbi_write_tga(filePath, w, h, numChannels, m_Data.data());
269 % static_cast<int>(format)));
279 template <
typename TProcessValueCallable>
282 TProcessValueCallable processValue)
284 const unsigned int h = image.
GetHeight();
285 const unsigned int w = image.
GetWidth();
287 std::vector<float> imageData;
288 imageData.resize(h * w * 3);
290 for (
unsigned int j = 0; j < h; ++j)
292 for (
unsigned int i = 0; i < w; ++i)
298 const unsigned int rDstIndex = GetImageChannelIndex(channelLayout,
ImageChannel::R) * h * w + j * w + i;
299 const unsigned int gDstIndex = GetImageChannelIndex(channelLayout,
ImageChannel::G) * h * w + j * w + i;
300 const unsigned int bDstIndex = GetImageChannelIndex(channelLayout,
ImageChannel::B) * h * w + j * w + i;
317 boost::ignore_unused(channel);
318 return value / 255.f;
324 const std::array<float, 3>& mean)
329 const unsigned int channelIndex = GetImageChannelIndex(layout, channel);
330 return value - mean[channelIndex];
337 std::vector<float> imageData;
338 const unsigned int h = image.
GetHeight();
339 const unsigned int w = image.
GetWidth();
341 const unsigned int rDstIndex = GetImageChannelIndex(layout,
ImageChannel::R);
342 const unsigned int gDstIndex = GetImageChannelIndex(layout,
ImageChannel::G);
343 const unsigned int bDstIndex = GetImageChannelIndex(layout,
ImageChannel::B);
345 imageData.resize(h * w * 3);
346 unsigned int offset = 0;
348 for (
unsigned int j = 0; j < h; ++j)
350 for (
unsigned int i = 0; i < w; ++i)
355 imageData[offset+rDstIndex] = float(r) / 255.0f;
356 imageData[offset+gDstIndex] = float(g) / 255.0f;
357 imageData[offset+bDstIndex] = float(b) / 255.0f;
std::vector< float > GetImageDataInArmNnLayoutAsFloats(ImageChannelLayout channelLayout, const InferenceTestImage &image, TProcessValueCallable processValue)
InferenceTestImage(const char *filePath)
unsigned int GetWidth() const
std::vector< float > GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout, const InferenceTestImage &image)
unsigned int GetNumChannels() const
std::vector< float > GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout, const InferenceTestImage &image, const std::array< float, 3 > &mean)
void Write(WriteFormat format, const char *filePath) const
std::vector< float > Resize(unsigned int newWidth, unsigned int newHeight, const armnn::CheckLocation &location, const ResizingMethods meth=ResizingMethods::STB, const std::array< float, 3 > &mean={{0.0, 0.0, 0.0}}, const std::array< float, 3 > &stddev={{1.0, 1.0, 1.0}}, const float scale=255.0f)
void StbResize(InferenceTestImage &im, const unsigned int newWidth, const unsigned int newHeight)
unsigned int GetSizeInBytes() const
unsigned int GetHeight() const
std::vector< float > GetImageDataAsNormalizedFloats(ImageChannelLayout layout, const InferenceTestImage &image)
std::string AsString() const
std::tuple< uint8_t, uint8_t, uint8_t > GetPixelAs3Channels(unsigned int x, unsigned int y) const