1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #pragma once 10 11 #include <vector> 12 13 // C++ implementation of the python functions in torchtune: 14 // https://github.com/pytorch/torchtune/tree/main/torchtune/modules/transforms/vision_utils 15 16 // Calculate all factors of a given number. 17 std::vector<int> _get_factors(int n); 18 19 // Computes all combinations of resolutions, multiple of tile_size, 20 // that contain up to max_num_tiles. Useful for when dividing an image into 21 // tiles. For example, if we want at most 2 tiles per image, then we can support 22 // the following resolutions: (1x1, 1x2, 2x1) * tile_size Returns a vector of 23 // tuples of (height, width). 24 std::vector<std::vector<int>> find_supported_resolutions( 25 int max_num_tiles, 26 int tile_size); 27 28 // Determines the best canvas possible from a list of possible resolutions to 29 // resize an image to, without distortion. 30 std::vector<int> get_canvas_best_fit( 31 std::vector<int> image_size, 32 std::vector<std::vector<int>> possible_resolutions, 33 bool resize_to_max_canvas); 34 35 // Calculates the size of an image, if it was resized to be inscribed within the 36 // target_size. It is upscaled or downscaled such that one size is equal to the 37 // target_size, and the second size is less than or equal to the target_size. 38 std::vector<int> get_inscribed_size( 39 std::vector<int> image_size, 40 std::vector<int> canvas_size, 41 int max_size); 42