forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gdal_pixelfn.cpp
249 lines (206 loc) · 7.97 KB
/
test_gdal_pixelfn.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
///////////////////////////////////////////////////////////////////////////////
//
// Project: C++ Test Suite for GDAL/OGR
// Purpose: Test constant and builtin arguments for C++ pixel functions
// Author: Momtchil Momtchev <[email protected]>
//
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2022, Momtchil Momtchev <[email protected]>
/*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdal_unit_test.h"
#include "cpl_string.h"
#include "gdal_alg.h"
#include "gdal_priv.h"
#include "gdal.h"
#include "../../frmts/vrt/vrtdataset.h"
#include <vector>
#include "gtest_include.h"
CPLErr CustomPixelFuncWithMetadata(void **papoSources, int nSources,
void *pData, int nXSize, int nYSize,
GDALDataType eSrcType, GDALDataType eBufType,
int nPixelSpace, int nLineSpace,
CSLConstList papszArgs);
CPLErr CustomPixelFunc(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize, GDALDataType eSrcType,
GDALDataType eBufType, int nPixelSpace, int nLineSpace,
CSLConstList papszArgs);
CPLErr CustomPixelFuncNoArgs(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize, GDALDataType eSrcType,
GDALDataType eBufType, int nPixelSpace,
int nLineSpace);
CPLErr CustomPixelFuncWithMetadata(void **papoSources, int nSources,
void *pData, int nXSize, int nYSize,
GDALDataType eSrcType, GDALDataType eBufType,
int nPixelSpace, int nLineSpace,
CSLConstList papszArgs)
{
/* ---- Init ---- */
if (nSources != 1)
return CE_Failure;
const char *pszConstant = CSLFetchNameValue(papszArgs, "customConstant");
if (pszConstant == nullptr)
return CE_Failure;
if (strncmp(pszConstant, "something", strlen("something")))
return CE_Failure;
const char *pszScale = CSLFetchNameValue(papszArgs, "scale");
if (pszScale == nullptr)
return CE_Failure;
/* ---- Set pixels ---- */
size_t ii = 0;
for (int iLine = 0; iLine < nYSize; ++iLine)
{
for (int iCol = 0; iCol < nXSize; ++iCol, ++ii)
{
const double dfPixVal = SRCVAL(papoSources[0], eSrcType, ii) * 2;
GDALCopyWords(&dfPixVal, GDT_Float64, 0,
static_cast<GByte *>(pData) +
static_cast<GSpacing>(nLineSpace) * iLine +
iCol * nPixelSpace,
eBufType, nPixelSpace, 1);
}
}
/* ---- Return success ---- */
return CE_None;
}
CPLErr CustomPixelFunc(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize, GDALDataType eSrcType,
GDALDataType eBufType, int nPixelSpace, int nLineSpace,
CSLConstList papszArgs)
{
/* ---- Init ---- */
if (nSources != 1)
return CE_Failure;
(void)papszArgs;
/* ---- Set pixels ---- */
size_t ii = 0;
for (int iLine = 0; iLine < nYSize; ++iLine)
{
for (int iCol = 0; iCol < nXSize; ++iCol, ++ii)
{
const double dfPixVal = SRCVAL(papoSources[0], eSrcType, ii) * 3;
GDALCopyWords(&dfPixVal, GDT_Float64, 0,
static_cast<GByte *>(pData) +
static_cast<GSpacing>(nLineSpace) * iLine +
iCol * nPixelSpace,
eBufType, nPixelSpace, 1);
}
}
/* ---- Return success ---- */
return CE_None;
}
CPLErr CustomPixelFuncNoArgs(void **papoSources, int nSources, void *pData,
int nXSize, int nYSize, GDALDataType eSrcType,
GDALDataType eBufType, int nPixelSpace,
int nLineSpace)
{
/* ---- Init ---- */
if (nSources != 1)
return CE_Failure;
/* ---- Set pixels ---- */
size_t ii = 0;
for (int iLine = 0; iLine < nYSize; ++iLine)
{
for (int iCol = 0; iCol < nXSize; ++iCol, ++ii)
{
const double dfPixVal = SRCVAL(papoSources[0], eSrcType, ii) * 4;
GDALCopyWords(&dfPixVal, GDT_Float64, 0,
static_cast<GByte *>(pData) +
static_cast<GSpacing>(nLineSpace) * iLine +
iCol * nPixelSpace,
eBufType, nPixelSpace, 1);
}
}
/* ---- Return success ---- */
return CE_None;
}
namespace
{
const char pszFuncMetadata[] =
"<PixelFunctionArgumentsList>"
" <Argument name='customConstant' type='constant' value='something'>"
" </Argument>"
" <Argument type='builtin' value='scale'>"
" </Argument>"
"</PixelFunctionArgumentsList>";
struct test_gdal_pixelfn : public ::testing::Test
{
std::string src_;
test_gdal_pixelfn()
{
src_ = tut::common::data_basedir;
src_ += SEP;
src_ += "pixelfn.vrt";
}
void SetUp() override
{
if (GDALGetMetadataItem(GDALGetDriverByName("VRT"),
GDAL_DMD_OPENOPTIONLIST, nullptr) == nullptr)
{
GTEST_SKIP() << "VRT driver Open() missing";
}
}
};
// Test constant parameters in a custom pixel function
TEST_F(test_gdal_pixelfn, custom_pixel_fn_constant_parameters)
{
if (!GDALGetDriverByName("GTiff"))
{
GTEST_SKIP() << "GTiff driver missing";
}
GDALAddDerivedBandPixelFuncWithArgs("custom", CustomPixelFuncWithMetadata,
pszFuncMetadata);
GDALDatasetH ds = GDALOpen(src_.c_str(), GA_ReadOnly);
ASSERT_TRUE(nullptr != ds);
GDALRasterBandH band = GDALGetRasterBand(ds, 1);
ASSERT_TRUE(nullptr != band);
float buf[20 * 20];
CPL_IGNORE_RET_VAL(GDALRasterIO(band, GF_Read, 0, 0, 20, 20, buf, 20, 20,
GDT_Float32, 0, 0));
EXPECT_EQ(buf[0], 107 * 2);
GDALClose(ds);
}
// Test registering of a custom pixel function without metadata
TEST_F(test_gdal_pixelfn, custom_pixel_fn_without_metadata)
{
if (!GDALGetDriverByName("GTiff"))
{
GTEST_SKIP() << "GTiff driver missing";
}
GDALAddDerivedBandPixelFuncWithArgs("custom2", CustomPixelFunc, nullptr);
GDALDatasetH ds = GDALOpen(src_.c_str(), GA_ReadOnly);
ASSERT_TRUE(nullptr != ds);
GDALRasterBandH band = GDALGetRasterBand(ds, 1);
ASSERT_TRUE(nullptr != band);
VRTDerivedRasterBand *derived = reinterpret_cast<VRTDerivedRasterBand *>(
GDALRasterBand::FromHandle(band));
derived->SetPixelFunctionName("custom2");
float buf[20 * 20];
CPL_IGNORE_RET_VAL(GDALRasterIO(band, GF_Read, 0, 0, 20, 20, buf, 20, 20,
GDT_Float32, 0, 0));
EXPECT_EQ(buf[0], 107 * 3);
GDALClose(ds);
}
// Test the registering of a custom pixel function without args
TEST_F(test_gdal_pixelfn, custom_pixel_fn_without_args)
{
if (!GDALGetDriverByName("GTiff"))
{
GTEST_SKIP() << "GTiff driver missing";
}
GDALAddDerivedBandPixelFunc("custom3", CustomPixelFuncNoArgs);
GDALDatasetH ds = GDALOpen(src_.c_str(), GA_ReadOnly);
ASSERT_TRUE(nullptr != ds);
GDALRasterBandH band = GDALGetRasterBand(ds, 1);
ASSERT_TRUE(nullptr != band);
VRTDerivedRasterBand *derived = reinterpret_cast<VRTDerivedRasterBand *>(
GDALRasterBand::FromHandle(band));
derived->SetPixelFunctionName("custom3");
float buf[20 * 20];
CPL_IGNORE_RET_VAL(GDALRasterIO(band, GF_Read, 0, 0, 20, 20, buf, 20, 20,
GDT_Float32, 0, 0));
EXPECT_EQ(buf[0], 107 * 4);
GDALClose(ds);
}
} // namespace