Skip to content

Commit 0a35363

Browse files
authored
Fix WarpPerspective::GetFillValue (#5575)
- Fix copying fill_value - Add more error checks. - Minor style fixes and improved structure packing. Signed-off-by: Michal Zientkiewicz <[email protected]>
1 parent fc77109 commit 0a35363

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

dali/operators/image/remap/cvcuda/warp_perspective.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ analog to the ``WARP_INVERSE_MAP`` flag.
7272
.AddOptionalArg("pixel_origin", R"doc(Pixel origin. Possible values: "corner", "center".
7373
7474
Determines the meaning of (0, 0) coordinates - "corner" places the origin at the top-left corner of
75-
the top-left pixel (like in OpenGL); "center" places (0, 0) in the center of
75+
the top-left pixel (like in OpenGL); "center" places (0, 0) in the center of
7676
the top-left pixel (like in OpenCV).))doc", "corner")
7777
.AddOptionalArg<float>("fill_value",
7878
"Value used to fill areas that are outside the source image when the "
@@ -91,8 +91,8 @@ class WarpPerspective : public nvcvop::NVCVSequenceOperator<StatelessOperator> {
9191
: nvcvop::NVCVSequenceOperator<StatelessOperator>(spec),
9292
border_mode_(nvcvop::GetBorderMode(spec.GetArgument<std::string>("border_mode"))),
9393
interp_type_(nvcvop::GetInterpolationType(spec.GetArgument<DALIInterpType>("interp_type"))),
94-
inverse_map_(spec.GetArgument<bool>("inverse_map")),
9594
fill_value_arg_(spec.GetArgument<std::vector<float>>("fill_value")),
95+
inverse_map_(spec.GetArgument<bool>("inverse_map")),
9696
ocv_pixel_(OCVCompatArg(spec.GetArgument<std::string>("pixel_origin"))) {
9797
matrix_data_.SetContiguity(BatchContiguity::Contiguous);
9898
}
@@ -108,16 +108,16 @@ class WarpPerspective : public nvcvop::NVCVSequenceOperator<StatelessOperator> {
108108
float4 GetFillValue(int channels) const {
109109
if (fill_value_arg_.size() > 1) {
110110
if (channels > 0) {
111-
if (channels == static_cast<int>(fill_value_arg_.size())) {
112-
float4 fill_value{0, 0, 0, 0};
113-
memcpy(&fill_value, fill_value_arg_.data(), sizeof(decltype(fill_value)));
114-
return fill_value;
115-
} else {
111+
if (channels != static_cast<int>(fill_value_arg_.size())) {
116112
DALI_FAIL(make_string(
117113
"Number of values provided as a fill_value should match the number of channels.\n"
118114
"Number of channels: ",
119115
channels, ". Number of values provided: ", fill_value_arg_.size(), "."));
120116
}
117+
assert(channels <= 4);
118+
float4 fill_value{0, 0, 0, 0};
119+
memcpy(&fill_value, fill_value_arg_.data(), channels * sizeof(float));
120+
return fill_value;
121121
} else {
122122
DALI_FAIL("Only scalar fill_value can be provided when processing data in planar layout.");
123123
}
@@ -162,6 +162,8 @@ class WarpPerspective : public nvcvop::NVCVSequenceOperator<StatelessOperator> {
162162

163163
auto output_shape = input_shape;
164164
int channels = (input_layout.find('C') != -1) ? input_shape[0][input_layout.find('C')] : -1;
165+
if (channels > 4)
166+
DALI_FAIL("Images with more than 4 channels are not supported.");
165167
fill_value_ = GetFillValue(channels);
166168
if (size_arg_.HasExplicitValue()) {
167169
size_arg_.Acquire(spec_, ws, input_shape.size(), TensorShape<1>(2));
@@ -226,14 +228,14 @@ class WarpPerspective : public nvcvop::NVCVSequenceOperator<StatelessOperator> {
226228
ArgValue<float, 2> matrix_arg_{"matrix", spec_};
227229
ArgValue<float, 1> size_arg_{"size", spec_};
228230
int op_batch_size_ = 0;
229-
NVCVBorderType border_mode_{NVCV_BORDER_CONSTANT};
230-
NVCVInterpolationType interp_type_{NVCV_INTERP_NEAREST};
231-
bool inverse_map_{false};
231+
NVCVBorderType border_mode_ = NVCV_BORDER_CONSTANT;
232+
NVCVInterpolationType interp_type_ = NVCV_INTERP_NEAREST;
232233
std::vector<float> fill_value_arg_{0, 0, 0, 0};
233-
float4 fill_value_{0, 0, 0, 0};
234+
float4 fill_value_;
235+
bool inverse_map_ = false;
234236
bool ocv_pixel_ = true;
235-
std::optional<cvcuda::WarpPerspective> warp_perspective_{};
236-
TensorList<GPUBackend> matrix_data_{};
237+
std::optional<cvcuda::WarpPerspective> warp_perspective_;
238+
TensorList<GPUBackend> matrix_data_;
237239
};
238240

239241
DALI_REGISTER_OPERATOR(experimental__WarpPerspective, WarpPerspective, GPU);

0 commit comments

Comments
 (0)