blob: 45dd8bf4e2a3870b29608066eac35ae254867fae [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5#pragma once
6
7#include <armnn/Exceptions.hpp>
8
9#include <array>
10#include <cstdint>
11#include <vector>
12#include <utility>
13
14class InferenceTestImageException : public armnn::Exception
15{
16public:
17 using Exception::Exception;
18};
19
20class InferenceTestImageLoadFailed : public InferenceTestImageException
21{
22public:
23 using InferenceTestImageException::InferenceTestImageException;
24};
25
26class InferenceTestImageOutOfBoundsAccess : public InferenceTestImageException
27{
28public:
29 using InferenceTestImageException::InferenceTestImageException;
30};
31
32class InferenceTestImageResizeFailed : public InferenceTestImageException
33{
34public:
35 using InferenceTestImageException::InferenceTestImageException;
36};
37
38class InferenceTestImageWriteFailed : public InferenceTestImageException
39{
40public:
41 using InferenceTestImageException::InferenceTestImageException;
42};
43
44class UnknownImageChannelLayout : public InferenceTestImageException
45{
46public:
47 using InferenceTestImageException::InferenceTestImageException;
48};
49
50class InferenceTestImage
51{
52public:
53 enum class WriteFormat
54 {
55 Png,
56 Bmp,
57 Tga
58 };
59
60 explicit InferenceTestImage(const char* filePath);
61
62 InferenceTestImage(InferenceTestImage&&) = delete;
63 InferenceTestImage(const InferenceTestImage&) = delete;
64 InferenceTestImage& operator=(const InferenceTestImage&) = delete;
65 InferenceTestImage& operator=(InferenceTestImage&&) = delete;
66
67 unsigned int GetWidth() const { return m_Width; }
68 unsigned int GetHeight() const { return m_Height; }
69 unsigned int GetNumChannels() const { return m_NumChannels; }
70 unsigned int GetNumElements() const { return GetWidth() * GetHeight() * GetNumChannels(); }
71 unsigned int GetSizeInBytes() const { return GetNumElements() * GetSingleElementSizeInBytes(); }
72
73 // Returns the pixel identified by the given coordinates as a 3-channel value.
74 // Channels beyond the third are dropped. If the image provides less than 3 channels, the non-existent
75 // channels of the pixel will be filled with 0. Channels are returned in RGB order (that is, the first element
76 // of the tuple corresponds to the Red channel, whereas the last element is the Blue channel).
77 std::tuple<uint8_t, uint8_t, uint8_t> GetPixelAs3Channels(unsigned int x, unsigned int y) const;
78
79 void Resize(unsigned int newWidth, unsigned int newHeight);
80 void Write(WriteFormat format, const char* filePath) const;
81
82private:
83 static unsigned int GetSingleElementSizeInBytes()
84 {
85 return sizeof(decltype(std::declval<InferenceTestImage>().m_Data[0]));
86 }
87
88 std::vector<uint8_t> m_Data;
89 unsigned int m_Width;
90 unsigned int m_Height;
91 unsigned int m_NumChannels;
92};
93
94// Common names used to identify a channel in a pixel
95enum class ImageChannel
96{
97 R,
98 G,
99 B
100};
101
102// Channel layouts handled by the test framework
103enum class ImageChannelLayout
104{
105 Rgb,
106 Bgr
107};
108
109// Reads the contents of an inference test image as 3-channel pixels whose channel values have been normalized (scaled)
110// and now lie in the range [0,1]. Channel data is stored according to the ArmNN layout (CHW). The order in which
111// channels appear in the resulting vector is defined by the provided layout.
112std::vector<float> GetImageDataInArmNnLayoutAsNormalizedFloats(ImageChannelLayout layout,
113 const InferenceTestImage& image);
114
115// Reads the contents of an inference test image as 3-channel pixels whose value is the result of subtracting the mean
116// from the values in the original image. Channel data is stored according to the ArmNN layout (CHW). The order in
117// which channels appear in the resulting vector is defined by the provided layout. The order of the channels of the
118// provided mean should also match the given layout.
119std::vector<float> GetImageDataInArmNnLayoutAsFloatsSubtractingMean(ImageChannelLayout layout,
120 const InferenceTestImage& image,
121 const std::array<float, 3>& mean);