| 28 | Resize::Resize(int target_h, int target_w) : size_hw_(std::make_pair(target_h, target_w)) {} |
| 29 | |
| 30 | Image Resize::apply(const Image& input) const { |
| 31 | Image src = input; // mutable copy |
| 32 | if (size_hw_.has_value()) { |
| 33 | // Explicit resize to (H, W) |
| 34 | const int new_h = size_hw_->first; |
| 35 | const int new_w = size_hw_->second; |
| 36 | return src.resize(new_w, new_h); // Image::resize expects (w, h) |
| 37 | } |
| 38 | |
| 39 | // Shorter-side resize with aspect ratio preserved |
| 40 | MLLM_RT_ASSERT(size_shorter_.has_value()); |
| 41 | const int target_shorter = *size_shorter_; |
| 42 | const int h = src.h(); |
| 43 | const int w = src.w(); |
| 44 | |
| 45 | const int shorter = std::min(h, w); |
| 46 | const double scale = static_cast<double>(target_shorter) / static_cast<double>(shorter); |
| 47 | const int new_h = static_cast<int>(std::round(h * scale)); |
| 48 | const int new_w = static_cast<int>(std::round(w * scale)); |
| 49 | return src.resize(new_w, new_h); |
| 50 | } |
| 51 | |
| 52 | // ========================= CenterCrop ========================= |
| 53 | CenterCrop::CenterCrop(int crop_size) : crop_h_(crop_size), crop_w_(crop_size) {} |
no test coverage detected