forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageio.cpp
More file actions
170 lines (130 loc) · 4.35 KB
/
imageio.cpp
File metadata and controls
170 lines (130 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::cfloat;
using af::cdouble;
template<typename T>
class ImageIO : public ::testing::Test
{
public:
virtual void SetUp() {
}
};
typedef ::testing::Types<float> TestTypes;
// register the type list
TYPED_TEST_CASE(ImageIO, TestTypes);
// Disable tests if FreeImage is not found
#if defined(WITH_FREEIMAGE)
void loadImageTest(string pTestFile, string pImageFile, const bool isColor)
{
if (noDoubleTests<float>()) return;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float, float, float>(pTestFile,numDims,in,tests);
af::dim4 dims = numDims[0];
af_array imgArray = 0;
ASSERT_EQ(AF_SUCCESS, af_load_image(&imgArray, pImageFile.c_str(), isColor));
// Get result
float *imgData = new float[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*) imgData, imgArray));
// Compare result
size_t nElems = in[0].size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(in[0][elIter], imgData[elIter]) << "at: " << elIter << std::endl;
}
// Delete
delete[] imgData;
if(imgArray != 0) af_release_array(imgArray);
}
TYPED_TEST(ImageIO, ColorSmall)
{
loadImageTest(string(TEST_DIR"/imageio/color_small.test"), string(TEST_DIR"/imageio/color_small.png"), true);
}
TYPED_TEST(ImageIO, GraySmall)
{
loadImageTest(string(TEST_DIR"/imageio/gray_small.test"), string(TEST_DIR"/imageio/gray_small.jpg"), false);
}
TYPED_TEST(ImageIO, GraySeq)
{
loadImageTest(string(TEST_DIR"/imageio/gray_seq.test"), string(TEST_DIR"/imageio/gray_seq.png"), false);
}
TYPED_TEST(ImageIO, ColorSeq)
{
loadImageTest(string(TEST_DIR"/imageio/color_seq.test"), string(TEST_DIR"/imageio/color_seq.png"), true);
}
void loadimageArgsTest(string pImageFile, const bool isColor, af_err err)
{
af_array imgArray = 0;
ASSERT_EQ(err, af_load_image(&imgArray, pImageFile.c_str(), isColor));
if(imgArray != 0) af_release_array(imgArray);
}
TYPED_TEST(ImageIO,InvalidArgsMissingFile)
{
loadimageArgsTest(string(TEST_DIR"/imageio/nofile.png"), false, AF_ERR_RUNTIME);
}
TYPED_TEST(ImageIO,InvalidArgsWrongExt)
{
loadimageArgsTest(string(TEST_DIR"/imageio/image.wrongext"), true, AF_ERR_NOT_SUPPORTED);
}
////////////////////////////////// CPP //////////////////////////////////////
TEST(ImageIO, CPP)
{
if (noDoubleTests<float>()) return;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float, float, float>(string(TEST_DIR"/imageio/color_small.test"),numDims,in,tests);
af::dim4 dims = numDims[0];
af::array img = af::loadImage(string(TEST_DIR"/imageio/color_small.png").c_str(), true);
// Get result
float *imgData = new float[dims.elements()];
img.host((void*)imgData);
// Compare result
size_t nElems = in[0].size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(in[0][elIter], imgData[elIter]) << "at: " << elIter << std::endl;
}
// Delete
delete[] imgData;
}
TEST(ImageIO, SavePNGCPP) {
af::array input(10, 10, 3, f32);
input(af::span, af::span, af::span) = 0;
input(0, 0, 0) = 255;
input(0, 9, 1) = 255;
input(9, 0, 2) = 255;
input(9, 9, af::span) = 255;
saveImage("SaveCPP.png", input);
af::array out = af::loadImage("SaveCPP.png", true);
ASSERT_FALSE(af::anyTrue<bool>(out - input));
}
TEST(ImageIO, SaveBMPCPP) {
af::array input(10, 10, 3, f32);
input(af::span, af::span, af::span) = 0;
input(0, 0, 0) = 255;
input(0, 9, 1) = 255;
input(9, 0, 2) = 255;
input(9, 9, af::span) = 255;
saveImage("SaveCPP.bmp", input);
af::array out = af::loadImage("SaveCPP.bmp", true);
ASSERT_FALSE(af::anyTrue<bool>(out - input));
}
#endif // WITH_FREEIMAGE