@@ -37,6 +37,7 @@ struct pad_descriptor {
37
37
class pixel_exporter_cpu_crop {
38
38
std::unique_ptr<float []> buffer{};
39
39
std::unique_ptr<float []> buffer_alpha{};
40
+ shape_t <3 > current_buffer_shape;
40
41
size_t max_size;
41
42
42
43
public:
@@ -82,21 +83,23 @@ std::string pixel_importer_cpu::import_color(md_view<float, 3> dst,
82
83
md_uview<const U, 3 > src,
83
84
cudaStream_t stream,
84
85
float quant) {
85
- if (dst.shape .slice <1 , 2 >() != src.shape .template slice <0 , 2 >()) {
86
- return " dimension mismatch" ;
87
- }
88
-
89
86
auto [h, w, c] = src.shape ;
87
+ auto [dc, dh, dw] = dst.shape ;
90
88
91
- if (h * w > max_size) {
89
+ if (dh * dw > max_size) {
92
90
return " dimension too big" ;
93
91
}
94
92
93
+ if (h > dh || w > dw) {
94
+ return " incompatible dimension" ;
95
+ }
96
+
95
97
if (quant == 0.0 ) {
96
98
quant = 1.0 / float (std::numeric_limits<U>::max ());
97
99
}
98
100
99
101
md_view<float , 3 > tmp{buffer.get (), dst.shape };
102
+ md_view<float , 2 > tmp_alpha{buffer_alpha.get (), {h, w}};
100
103
101
104
if (c == 3 ) {
102
105
for (size_t y = 0 ; y < h; ++y) {
@@ -118,7 +121,6 @@ std::string pixel_importer_cpu::import_color(md_view<float, 3> dst,
118
121
}
119
122
}
120
123
else {
121
- md_view<float , 2 > tmp_alpha{buffer_alpha.get (), {h, w}};
122
124
for (size_t y = 0 ; y < h; ++y) {
123
125
for (size_t x = 0 ; x < w; ++x) {
124
126
tmp.at (0 , y, x) = static_cast <float >(src.at (y, x, 2 )) * quant;
@@ -133,6 +135,39 @@ std::string pixel_importer_cpu::import_color(md_view<float, 3> dst,
133
135
assert (false );
134
136
}
135
137
138
+ for (size_t y = h; y < dh; ++y) {
139
+ for (size_t x = 0 ; x < w; ++x) {
140
+ tmp.at (0 , y, x) = tmp.at (0 , h - 1 , x);
141
+ tmp.at (1 , y, x) = tmp.at (1 , h - 1 , x);
142
+ tmp.at (2 , y, x) = tmp.at (2 , h - 1 , x);
143
+ if (c == 4 && buffer_alpha) {
144
+ tmp_alpha.at (y, x) = tmp_alpha.at (h - 1 , x);
145
+ }
146
+ }
147
+ }
148
+
149
+ for (size_t y = 0 ; y < h; ++y) {
150
+ for (size_t x = w; x < dw; ++x) {
151
+ tmp.at (0 , y, x) = tmp.at (0 , y, w - 1 );
152
+ tmp.at (1 , y, x) = tmp.at (1 , y, w - 1 );
153
+ tmp.at (2 , y, x) = tmp.at (2 , y, w - 1 );
154
+ if (c == 4 && buffer_alpha) {
155
+ tmp_alpha.at (y, x) = tmp_alpha.at (y, w - 1 );
156
+ }
157
+ }
158
+ }
159
+
160
+ for (size_t y = h; y < dh; ++y) {
161
+ for (size_t x = w; x < dw; ++x) {
162
+ tmp.at (0 , y, x) = tmp.at (0 , h - 1 , w - 1 );
163
+ tmp.at (1 , y, x) = tmp.at (1 , h - 1 , w - 1 );
164
+ tmp.at (2 , y, x) = tmp.at (2 , h - 1 , w - 1 );
165
+ if (c == 4 && buffer_alpha) {
166
+ tmp_alpha.at (y, x) = tmp_alpha.at (h - 1 , w - 1 );
167
+ }
168
+ }
169
+ }
170
+
136
171
auto err = cudaMemcpyAsync (dst.data , tmp.data , dst.size () * 4 , cudaMemcpyHostToDevice, stream);
137
172
if (err != cudaSuccess) {
138
173
return std::string (" CUDA error: " ) + cudaGetErrorName (err);
@@ -148,8 +183,13 @@ std::string pixel_exporter_cpu_crop::export_data(md_uview<U, 3> dst, pad_descrip
148
183
}
149
184
150
185
auto [he, we, c] = dst.shape ;
151
- md_uview<float , 3 > tmp = md_view<float , 3 >{buffer.get (), {c, he, we}};
152
- md_uview<float , 2 > tmp_alpha = md_view<float , 2 >{buffer_alpha.get (), {he, we}};
186
+ auto [_, hs, ws] = current_buffer_shape;
187
+ if (he > hs || we > ws) {
188
+ return " incompatible dimension" ;
189
+ }
190
+
191
+ md_uview<float , 3 > tmp = md_view<float , 3 >{buffer.get (), current_buffer_shape};
192
+ md_uview<float , 2 > tmp_alpha = md_view<float , 2 >{buffer_alpha.get (), current_buffer_shape.slice <1 , 2 >()};
153
193
154
194
offset_t shrink = pad.pad / 2 ;
155
195
offset_t hb = pad.top ? 0 : shrink;
0 commit comments