2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "InferenceTestImage.hpp"
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_image.h>
17 #define STB_IMAGE_RESIZE_IMPLEMENTATION
18 #include <stb_image_resize.h>
20 #define STB_IMAGE_WRITE_IMPLEMENTATION
21 #include <stb_image_write.h>
26 unsigned int GetImageChannelIndex(ImageChannelLayout channelLayout, ImageChannel channel)
28 switch (channelLayout)
30 case ImageChannelLayout::Rgb:
31 return static_cast<unsigned int>(channel);
32 case ImageChannelLayout::Bgr:
33 return 2u - static_cast<unsigned int>(channel);
35 throw UnknownImageChannelLayout(boost::str(boost::format("Unknown layout %1%")
36 % static_cast<int>(channelLayout)));
42 InferenceTestImage::InferenceTestImage(char const* filePath)
51 using StbImageDataPtr = std::unique_ptr<unsigned char, decltype(&stbi_image_free)>;
52 StbImageDataPtr stbData(stbi_load(filePath, &width, &height, &channels, 0), &stbi_image_free);
54 if (stbData == nullptr)
56 throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load the image at %1%") % filePath));
59 if (width == 0 || height == 0)
61 throw InferenceTestImageLoadFailed(boost::str(boost::format("Could not load empty image at %1%") % filePath));
64 m_Width = boost::numeric_cast<unsigned int>(width);
65 m_Height = boost::numeric_cast<unsigned int>(height);
66 m_NumChannels = boost::numeric_cast<unsigned int>(channels);
68 const unsigned int sizeInBytes = GetSizeInBytes();
69 m_Data.resize(sizeInBytes);
70 memcpy(m_Data.data(), stbData.get(), sizeInBytes);
73 std::tuple<uint8_t, uint8_t, uint8_t> InferenceTestImage::GetPixelAs3Channels(unsigned int x, unsigned int y) const
75 if (x >= m_Width || y >= m_Height)
77 throw InferenceTestImageOutOfBoundsAccess(boost::str(boost::format("Attempted out of bounds image access. "
78 "Requested (%1%, %2%). Maximum valid coordinates (%3%, %4%).") % x % y % (m_Width - 1) % (m_Height - 1)));
81 const unsigned int pixelOffset = x * GetNumChannels() + y * GetWidth() * GetNumChannels();
82 const uint8_t* const pixelData = m_Data.data() + pixelOffset;
83 BOOST_ASSERT(pixelData <= (m_Data.data() + GetSizeInBytes()));
85 std::array<uint8_t, 3> outPixelData;
88 const unsigned int maxChannelsInPixel = std::min(GetNumChannels(), static_cast<unsigned int>(outPixelData.size()));
89 for (unsigned int c = 0; c < maxChannelsInPixel; ++c)
91 outPixelData[c] = pixelData[c];
94 return std::make_tuple(outPixelData[0], outPixelData[1], outPixelData[2]);
97 void InferenceTestImage::Resize(unsigned int newWidth, unsigned int newHeight)
99 if (newWidth == 0 || newHeight == 0)
101 throw InferenceTestImageResizeFailed(boost::str(boost::format("None of the dimensions passed to a resize "
102 "operation can be zero. Requested width: %1%. Requested height: %2%.") % newWidth % newHeight));
105 if (newWidth == m_Width && newHeight == m_Height)
111 std::vector<uint8_t> newData;
112 newData.resize(newWidth * newHeight * GetNumChannels() * GetSingleElementSizeInBytes());
114 // boost::numeric_cast<>() is used for user-provided data (protecting about overflows).
115 // static_cast<> ok for internal data (assumes that, when internal data was originally provided by a user,
116 // a boost::numeric_cast<>() handled the conversion).
117 const int nW = boost::numeric_cast<int>(newWidth);
118 const int nH = boost::numeric_cast<int>(newHeight);
120 const int w = static_cast<int>(GetWidth());
121 const int h = static_cast<int>(GetHeight());
122 const int numChannels = static_cast<int>(GetNumChannels());
124 const int res = stbir_resize_uint8(m_Data.data(), w, h, 0, newData.data(), nW, nH, 0, numChannels);
127 throw InferenceTestImageResizeFailed("The resizing operation failed");
130 m_Data.swap(newData);
132 m_Height = newHeight;
135 void InferenceTestImage::Write(WriteFormat format, const char* filePath) const
137 const int w = static_cast<int>(GetWidth());
138 const int h = static_cast<int>(GetHeight());
139 const int numChannels = static_cast<int>(GetNumChannels());
144 case WriteFormat::Png:
146 res = stbi_write_png(filePath, w, h, numChannels, m_Data.data(), 0);
149 case WriteFormat::Bmp:
151 res = stbi_write_bmp(filePath, w, h, numChannels, m_Data.data());
154 case WriteFormat::Tga:
156 res = stbi_write_tga(filePath, w, h, numChannels, m_Data.data());
160 throw InferenceTestImageWriteFailed(boost::str(boost::format("Unknown format %1%")
161 % static_cast<int>(format)));
166 throw InferenceTestImageWriteFailed(boost::str(boost::format("An error occurred when writing to file %1%")
171 template <typename TProcessValueCallable>
172 std::vector<float> GetImageDataInArmNnLayoutAsFloats(ImageChannelLayout channelLayout,
173 const InferenceTestImage& image,
174 TProcessValueCallable processValue)
176 const unsigned int h = image.GetHeight();
177 const unsigned int w = image.GetWidth();
179 std::vector<float> imageData;
180 imageData.resize(h * w * 3);
182 for (unsigned int j = 0; j < h; ++j)
184 for (unsigned int i = 0; i < w; ++i)
187 std::tie(r, g, b) = image.GetPixelAs3Channels(i, j);
189 // ArmNN order: C, H, W
190 const unsigned int rDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::R) * h * w + j * w + i;
191 const unsigned int gDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::G) * h * w + j * w + i;
192 const unsigned int bDstIndex = GetImageChannelIndex(channelLayout, ImageChannel::B) * h * w + j * w + i;
194 imageData[rDstIndex] = processValue(ImageChannel::R, float(r));
195 imageData[gDstIndex] = processValue(ImageChannel::G, float(g));
196 imageData[bDstIndex] = processValue(ImageChannel::B, float(b));
203 std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
204 const InferenceTestImage& image)
206 return GetImageDataInArmNnLayoutAsFloats(layout, image,
207 [](ImageChannel channel, float value)
209 boost::ignore_unused(channel);
210 return value / 255.f;
214 std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
215 const InferenceTestImage& image,
216 const std::array<float, 3>& mean)
218 return GetImageDataInArmNnLayoutAsFloats(layout, image,
219 [layout, &mean](ImageChannel channel, float value)
221 const unsigned int channelIndex = GetImageChannelIndex(layout, channel);
222 return value - mean[channelIndex];
226 std::vector<float> GetImageDataAsNormalizedFloats(ImageChannelLayout layout,
227 const InferenceTestImage& image)
229 std::vector<float> imageData;
230 const unsigned int h = image.GetHeight();
231 const unsigned int w = image.GetWidth();
233 const unsigned int rDstIndex = GetImageChannelIndex(layout, ImageChannel::R);
234 const unsigned int gDstIndex = GetImageChannelIndex(layout, ImageChannel::G);
235 const unsigned int bDstIndex = GetImageChannelIndex(layout, ImageChannel::B);
237 imageData.resize(h * w * 3);
238 unsigned int offset = 0;
240 for (unsigned int j = 0; j < h; ++j)
242 for (unsigned int i = 0; i < w; ++i)
245 std::tie(r, g, b) = image.GetPixelAs3Channels(i, j);
247 imageData[offset+rDstIndex] = float(r) / 255.0f;
248 imageData[offset+gDstIndex] = float(g) / 255.0f;
249 imageData[offset+bDstIndex] = float(b) / 255.0f;