3 #include <range/v3/all.hpp>
17 const std::vector<Image>& images,
21 auto sizes = std::vector<size2i>{};
23 for(
const auto& img: images)
39 const std::vector<vec2i>& positions,
40 const std::vector<Image>& images,
42 const Rgbi& background_color
45 auto composed_image =
Image{};
51 clear(&composed_image, background_color);
65 return composed_image;
72 const size2i& default_size,
73 std::vector<vec2i>* positions,
74 const std::vector<Image>& images,
78 std::optional<Recti> bb = std::nullopt;
82 const auto image_width = img.
width;
83 const auto image_height = img.height;
86 vec2i(position.x, position.y),
87 image_width + padding,
88 image_height + padding
91 { bb->include(rect); }
95 if(!bb) {
return default_size; }
97 const auto size = bb->get_size();
98 const auto dx = -bb->left;
99 const auto dy = -bb->bottom;
101 for(
auto& position: *positions)
103 position +=
vec2i(dx, dy);
116 const std::vector<Image>& images,
117 const std::vector<std::string>& files,
122 const auto packed =
pack(image_size, image_sizes);
124 auto ret = std::vector<vec2i>{};
127 for(
const auto& rect: packed)
129 const auto& file = files[
i];
132 if(rect.has_value() ==
false)
134 std::cerr <<
"failed to pack " << file <<
"\n";
138 ret.emplace_back(rect->get_bottom_left());
149 const std::vector<Image>& images,
150 const std::vector<std::string>& files,
151 const size2i& requested_size,
153 Rgbi background_color,
154 bool should_pack_image
157 if( requested_size.
width < padding ||
158 requested_size.
height < padding)
165 requested_size.
width - padding,
166 requested_size.
height - padding
170 auto packed =
pack_image(image_size, images, files, padding);
178 const auto size = should_pack_image
179 ?
pack_tight(requested_size, &packed, images, padding)
189 for(
auto& position: packed)
191 position +=
vec2i(padding, padding);
195 auto composed_image =
draw_image(packed, images, size, background_color);
197 return composed_image;
204 std::pair<std::vector<vec2i>,
size2i>
207 const std::vector<Image>& images,
226 auto ret = std::vector<vec2i>{};
230 int current_height = 0;
236 max_x =
max(max_x,
x);
239 y += current_height + padding;
243 for(
const auto& src: images)
245 const auto width = src.width;
246 const auto height = src.height;
253 x += width + padding;
254 current_height =
max(current_height, height);
257 if(column >= images_per_row)
268 const auto image_width = max_x;
269 const auto image_height =
y;
276 const auto into_pointer = [](
vec2i&
v) ->
vec2i* {
return &
v; };
277 using namespace ranges::views;
280 const auto& [position, img]:
281 zip(ret|transform(into_pointer), images)
284 position->y = image_height - (position->y + img.height);
295 const std::vector<Image>& images,
297 Rgbi background_color,
302 const auto [image_grid, size] =
lay_out_in_a_grid(images, padding, top_to_bottom);
305 auto composed_image =
draw_image(image_grid, images, size, background_color);
307 return composed_image;
void clear(Image *image, const Rgbai &color)
Image draw_image(const std::vector< vec2i > &positions, const std::vector< Image > &images, const size2i &size, const Rgbi &background_color)
std::pair< std::vector< vec2i >, size2i > lay_out_in_a_grid(const std::vector< Image > &images, int padding, bool top_to_bottom)
std::vector< std::optional< Recti > > pack(const size2i &container, const std::vector< size2i > &to_pack)
std::vector< vec2i > pack_image(const size2i &image_size, const std::vector< Image > &images, const std::vector< std::string > &files, int padding)
std::vector< size2i > collect_sizes(const std::vector< Image > &images, int padding)
size2i pack_tight(const size2i &default_size, std::vector< vec2i > *positions, const std::vector< Image > &images, int padding)
void paste_image(Image *dest_image, const vec2i &position, const Image &source_image, BlendMode blend_mode, PixelsOutside clip)
constexpr float c_sizet_to_float(std::size_t f)
constexpr int c_float_to_int(float f)
std::vector< std::pair< A, B > > zip(const std::vector< A > &as, const std::vector< B > &bs)
size2f max(const size2f lhs, const size2f rhs)
static Recti from_bottom_left_width_height(const vec2i &bl, int width, int height)
void setup_with_alpha_support(int image_width, int image_height, int default_value=0)
if default value is negative, default value is ignored, otherwise its the default value for both R,...
static size2i create_from_width_height(int w, int h)