2022-02-12 00:28:54 +01:00
|
|
|
#include "pack.h"
|
|
|
|
|
2022-02-14 00:13:09 +01:00
|
|
|
#include <algorithm>
|
2022-02-15 13:17:14 +01:00
|
|
|
#include <cassert>
|
2022-02-14 00:13:09 +01:00
|
|
|
#include <cmath>
|
2022-02-15 13:17:14 +01:00
|
|
|
#include <cstring>
|
2022-02-14 00:13:09 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2022-02-15 13:17:14 +01:00
|
|
|
namespace freling {
|
|
|
|
|
|
|
|
void blit(const Frame& in_frame,
|
|
|
|
const BoundingBox& in_box,
|
|
|
|
Frame& out_frame,
|
|
|
|
const BoundingBox& out_box) {
|
|
|
|
assert(in_box.width() == out_box.width());
|
|
|
|
assert(in_box.height() == out_box.height());
|
|
|
|
|
|
|
|
const int data_width = in_box.width() * sizeof(Pixel);
|
|
|
|
const int in_row_size = in_frame.width * sizeof(Pixel);
|
|
|
|
const int out_row_size = out_frame.width * sizeof(Pixel);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < in_box.height(); ++i) {
|
|
|
|
const int in_offset =
|
|
|
|
in_box.left * sizeof(Pixel) + (i + in_box.top) * in_row_size;
|
|
|
|
const int out_offset =
|
|
|
|
out_box.left * sizeof(Pixel) + (i + out_box.top) * out_row_size;
|
|
|
|
memcpy(out_frame.data + out_offset, in_frame.data + in_offset,
|
|
|
|
data_width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Frame> pack(const Frame& in_frame,
|
|
|
|
const std::vector<BoundingBox>& bboxes) {
|
|
|
|
if (bboxes.size() == 0) {
|
|
|
|
std::cerr << "No bounding box, cannot pack.\n";
|
|
|
|
return {};
|
|
|
|
}
|
2022-02-14 00:13:09 +01:00
|
|
|
// We sort the bounding boxes by maximum area
|
|
|
|
std::vector<BoundingBox> sorted_bboxes = bboxes;
|
|
|
|
std::sort(sorted_bboxes.begin(), sorted_bboxes.end(),
|
|
|
|
[](const auto& a, const auto& b) { return a.area() > b.area(); });
|
|
|
|
|
|
|
|
// We keep a mapping between the sorted bounding boxes and the original
|
|
|
|
// order
|
|
|
|
std::vector<int> mapping(bboxes.size());
|
|
|
|
for (int i = 0; i < sorted_bboxes.size(); ++i) {
|
|
|
|
const auto& s_box = sorted_bboxes[i];
|
|
|
|
for (int j = 0; j < bboxes.size(); ++j) {
|
|
|
|
const auto& box = bboxes[j];
|
|
|
|
if (box == s_box) {
|
|
|
|
mapping[i] = j;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int max_area = 0;
|
|
|
|
for (const auto& box : sorted_bboxes) {
|
|
|
|
int area = box.area();
|
|
|
|
std::cout << "bounding box area: " << area << "\n";
|
|
|
|
max_area += area;
|
|
|
|
}
|
|
|
|
std::cout << "max area: " << max_area << "\n";
|
|
|
|
int min_dim = int(ceil(std::sqrt(max_area)));
|
|
|
|
std::cout << "optimal image dimention: " << min_dim << " x " << min_dim
|
|
|
|
<< "\n";
|
|
|
|
const int in_min_dim = std::min(in_frame.width, in_frame.height);
|
|
|
|
std::cout << "maximum image dimention: " << in_min_dim << " x "
|
|
|
|
<< in_min_dim << "\n";
|
|
|
|
|
2022-02-15 13:17:14 +01:00
|
|
|
const auto& largest = sorted_bboxes[0];
|
|
|
|
BoundingBox out_largest = {0, 0, static_cast<uint32_t>(largest.width()),
|
|
|
|
static_cast<uint32_t>(largest.height())};
|
|
|
|
Frame packed_frame(largest.width(), largest.height());
|
|
|
|
blit(in_frame, largest, packed_frame, out_largest);
|
2022-02-13 23:41:08 +01:00
|
|
|
return Frame(in_frame);
|
2022-02-12 00:28:54 +01:00
|
|
|
}
|
2022-02-15 13:17:14 +01:00
|
|
|
|
|
|
|
} // namespace freling
|