@@ -28,6 +28,44 @@ void DumpTensorToFile(const ortc::Tensor<T>& tensor, const char* name) {
28
28
#endif
29
29
}
30
30
31
+ template <typename T>
32
+ void SplitIntoTitles (const ortc::Tensor<T>& normalized_image, ortc::Tensor<T>& pixel_values,
33
+ int64_t tile_height, int64_t tile_width) {
34
+ auto & shape = normalized_image.Shape ();
35
+ int64_t image_height = shape[0 ];
36
+ int64_t image_width = shape[1 ];
37
+ int64_t num_channels = shape[2 ];
38
+
39
+ const int64_t image_1c_size = tile_height * tile_width;
40
+ assert (image_height % tile_height == 0 );
41
+ int64_t num_tiles_height = static_cast <int64_t >(image_height / tile_height);
42
+ assert (image_width % tile_width == 0 );
43
+ int64_t num_tiles_width = static_cast <int64_t >(image_width / tile_width);
44
+
45
+ auto p_normalized_image = normalized_image.Data ();
46
+ // shape (num_tiles_width * num_tiles_height, num_channels, tile_height, tile_width)
47
+ float * output_pixel =
48
+ pixel_values.Allocate ({num_tiles_height * num_tiles_width, num_channels, tile_height, tile_width});
49
+
50
+ // From (image_height, image_width, num_channels)
51
+ // Permute to (num_tiles_height, num_tiles_width, num_channels, tile_height, tile_width)
52
+ for (int64_t i = 0 ; i < num_tiles_height; ++i) {
53
+ for (int64_t j = 0 ; j < num_tiles_width; ++j) {
54
+ // convert to be channel first
55
+ for (int64_t k = 0 ; k < num_channels; ++k) {
56
+ auto sub_index = image_1c_size * (i * num_tiles_width + j) * num_channels + image_1c_size * k;
57
+ for (int64_t y = 0 ; y < tile_height; ++y) {
58
+ for (int64_t x = 0 ; x < tile_width; ++x) {
59
+ output_pixel[sub_index + y * tile_width + x] =
60
+ p_normalized_image[(i * tile_height + y) * image_width * num_channels +
61
+ (j * tile_width + x) * num_channels + k];
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+
31
69
inline OrtxStatus convert_to_rgb (const ortc::Tensor<uint8_t >& input, ortc::Tensor<uint8_t >& output) {
32
70
auto & dimensions = input.Shape ();
33
71
if (dimensions.size () != 3ULL || dimensions[2 ] != 3 ) {
@@ -106,7 +144,6 @@ struct Resize {
106
144
std::memcpy (p_output_image + c0_index, output_image->image [i] + j * 4 , c);
107
145
}
108
146
}
109
- // DumpTensor(output);
110
147
111
148
ImagingDelete (output_image);
112
149
return {};
0 commit comments