show packed frame
This commit is contained in:
parent
d7a54361f5
commit
a2f07920c7
2
justfile
2
justfile
|
@ -20,7 +20,7 @@ run-cli: build-cli
|
|||
{{exe_cli}} lenna.png 0 0 0 0
|
||||
|
||||
run-gui: build-gui
|
||||
nixGL {{exe_gui}} lenna.png
|
||||
nixGL {{exe_gui}} lenna.png 0 0 64 64 100 100 200 164 80 200 150 420
|
||||
|
||||
generate-build:
|
||||
git clean -xf src/
|
||||
|
|
|
@ -7,7 +7,7 @@ Frame::Frame(uint32_t width, uint32_t height) : width(width), height(height) {
|
|||
}
|
||||
|
||||
Frame::Frame(const Frame& other) : Frame(other.width, other.height) {
|
||||
std::memcpy(data, other.data, width * height);
|
||||
std::memcpy(data, other.data, width * height * sizeof(Pixel));
|
||||
}
|
||||
|
||||
Frame& Frame::operator=(const Frame& other) {
|
||||
|
@ -15,7 +15,7 @@ Frame& Frame::operator=(const Frame& other) {
|
|||
height = other.height;
|
||||
delete[] data;
|
||||
data = new Pixel[width * height];
|
||||
std::memcpy(data, other.data, width * height);
|
||||
std::memcpy(data, other.data, width * height * sizeof(Pixel));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include <cstdint>
|
||||
|
||||
struct Pixel {
|
||||
uint32_t r;
|
||||
uint32_t g;
|
||||
uint32_t b;
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
};
|
||||
|
||||
struct Frame {
|
||||
|
|
146
src/main_gui.cpp
146
src/main_gui.cpp
|
@ -1,6 +1,8 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace raylib {
|
||||
|
@ -8,6 +10,9 @@ namespace raylib {
|
|||
}
|
||||
|
||||
#include "bounding_box.h"
|
||||
#include "frame.h"
|
||||
#include "pack.h"
|
||||
#include "png.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
@ -34,11 +39,39 @@ struct Button {
|
|||
};
|
||||
};
|
||||
|
||||
void draw(const BoundingBox& box) {}
|
||||
void draw(const BoundingBox& box,
|
||||
const raylib::Vector2& offset,
|
||||
const raylib::Color& color) {
|
||||
const raylib::Rectangle rect = {offset.x + box.left, offset.y + box.top,
|
||||
static_cast<float>(box.right - box.left),
|
||||
static_cast<float>(box.bottom - box.top)};
|
||||
raylib::DrawRectangleRec(rect, raylib::ColorAlpha(color, 0.3));
|
||||
raylib::DrawRectangleLinesEx(rect, 3, color);
|
||||
}
|
||||
|
||||
raylib::Image image_from_frame(const Frame& frame) {
|
||||
raylib::Image image = {0};
|
||||
|
||||
const int format = raylib::PIXELFORMAT_UNCOMPRESSED_R8G8B8;
|
||||
const unsigned int size =
|
||||
raylib::GetPixelDataSize(frame.width, frame.height, format);
|
||||
|
||||
image.data = RL_MALLOC(size);
|
||||
memcpy(image.data, frame.data, size);
|
||||
image.width = frame.width;
|
||||
image.height = frame.height;
|
||||
image.mipmaps = 1;
|
||||
image.format = format;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " path/to/image\n";
|
||||
if (argc < 2 or (argc - 2) % 4 != 0) {
|
||||
std::cerr
|
||||
<< "Usage: " << argv[0] << " path/to/image"
|
||||
<< " [x1 y1 x2 y2 ...]\n"
|
||||
<< "x/y points must be grouped by 4 to define bounding boxes\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -47,45 +80,124 @@ int main(int argc, const char* argv[]) {
|
|||
std::cerr << "Input file " << input << " does not exist.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
const std::optional<Frame> in_frame = load_png(input);
|
||||
if (!in_frame) {
|
||||
std::cerr << "Cannot load file " << input << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<BoundingBox> bboxes;
|
||||
bboxes.reserve((argc - 2) / 4);
|
||||
int i = 2;
|
||||
while (i < argc) {
|
||||
const uint32_t x1 = atoi(argv[i]);
|
||||
const uint32_t y1 = atoi(argv[i + 1]);
|
||||
const uint32_t x2 = atoi(argv[i + 2]);
|
||||
const uint32_t y2 = atoi(argv[i + 3]);
|
||||
bboxes.push_back(BoundingBox({x1, y1, x2, y2}));
|
||||
i += 4;
|
||||
}
|
||||
const std::vector<raylib::Color> bbox_colors = {raylib::RED, raylib::GREEN,
|
||||
raylib::BLUE};
|
||||
|
||||
raylib::Vector2 win_size = {800, 450};
|
||||
const int padding = 5;
|
||||
raylib::Vector2 in_offset = {padding, 40};
|
||||
const raylib::Vector2 in_offset = {padding, 40};
|
||||
|
||||
raylib::InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
|
||||
raylib::SetTargetFPS(60);
|
||||
|
||||
raylib::Image imOrigin = raylib::LoadImage(input.c_str());
|
||||
raylib::ImageFormat(&imOrigin, raylib::PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
raylib::Texture2D texture = raylib::LoadTextureFromImage(imOrigin);
|
||||
raylib::Image in_img = image_from_frame(*in_frame);
|
||||
raylib::Texture2D in_texture = raylib::LoadTextureFromImage(in_img);
|
||||
|
||||
win_size.x = std::max(win_size.x, in_offset.x + texture.width + padding);
|
||||
win_size.y = std::max(win_size.y, in_offset.y + texture.height + padding);
|
||||
raylib::Image pack_img;
|
||||
raylib::Texture2D pack_texture;
|
||||
bool packed = false;
|
||||
|
||||
win_size.x = std::max(win_size.x, in_offset.x + in_texture.width + padding);
|
||||
win_size.y =
|
||||
std::max(win_size.y, in_offset.y + in_texture.height + padding);
|
||||
|
||||
raylib::SetWindowSize(win_size.x, win_size.y);
|
||||
|
||||
Button b_add_box = {{padding, padding, 150, 30}, "Add box"};
|
||||
std::vector<BoundingBox> bboxes;
|
||||
const raylib::Vector2 b_size = {150, 30};
|
||||
Button b_add_box = {{padding, padding, b_size.x, b_size.y}, "Add box"};
|
||||
Button b_pack = {{padding * 2 + b_size.x, padding, b_size.x, b_size.y},
|
||||
"Pack rects"};
|
||||
|
||||
while (!raylib::WindowShouldClose()) {
|
||||
//
|
||||
// Update
|
||||
//
|
||||
if (b_add_box.pressed()) {
|
||||
bboxes.push_back(BoundingBox({0, 0, 20, 20}));
|
||||
bboxes.push_back(BoundingBox({0, 0, 64, 64}));
|
||||
}
|
||||
if (b_pack.pressed()) {
|
||||
Frame f_packed = pack(*in_frame, bboxes);
|
||||
|
||||
if (packed) {
|
||||
raylib::UnloadTexture(pack_texture);
|
||||
raylib::UnloadImage(pack_img);
|
||||
}
|
||||
|
||||
pack_img = image_from_frame(f_packed);
|
||||
pack_texture = raylib::LoadTextureFromImage(pack_img);
|
||||
packed = true;
|
||||
|
||||
win_size.x =
|
||||
std::max(win_size.x, in_offset.x + in_texture.width +
|
||||
padding * 2 + pack_texture.width);
|
||||
win_size.y = std::max(
|
||||
win_size.y,
|
||||
in_offset.y + std::max(in_texture.height, pack_texture.height) +
|
||||
padding);
|
||||
|
||||
raylib::SetWindowSize(win_size.x, win_size.y);
|
||||
}
|
||||
|
||||
//
|
||||
// Draw
|
||||
//
|
||||
raylib::BeginDrawing();
|
||||
raylib::ClearBackground(raylib::RAYWHITE);
|
||||
|
||||
b_add_box.draw();
|
||||
b_pack.draw();
|
||||
|
||||
raylib::DrawTexture(texture, in_offset.x, in_offset.y, raylib::WHITE);
|
||||
raylib::DrawRectangleLines(in_offset.x, in_offset.y, texture.width,
|
||||
texture.height, raylib::BLACK);
|
||||
// Input image
|
||||
raylib::DrawTexture(in_texture, in_offset.x, in_offset.y,
|
||||
raylib::WHITE);
|
||||
raylib::DrawRectangleLines(in_offset.x, in_offset.y, in_texture.width,
|
||||
in_texture.height, raylib::BLACK);
|
||||
|
||||
int c = 0;
|
||||
for (const auto& b : bboxes) {
|
||||
const auto& color = bbox_colors[c % bboxes.size()];
|
||||
draw(b, in_offset, color);
|
||||
++c;
|
||||
};
|
||||
|
||||
// Packed image
|
||||
if (packed) {
|
||||
const int offset_x = in_offset.x + in_texture.width + padding;
|
||||
raylib::DrawTexture(pack_texture, offset_x, in_offset.y,
|
||||
raylib::WHITE);
|
||||
raylib::DrawRectangleLines(offset_x, in_offset.y,
|
||||
pack_texture.width, pack_texture.height,
|
||||
raylib::BLACK);
|
||||
}
|
||||
|
||||
raylib::EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
raylib::UnloadTexture(texture);
|
||||
raylib::UnloadImage(imOrigin);
|
||||
raylib::UnloadTexture(in_texture);
|
||||
raylib::UnloadImage(in_img);
|
||||
if (packed) {
|
||||
raylib::UnloadTexture(pack_texture);
|
||||
raylib::UnloadImage(pack_img);
|
||||
}
|
||||
raylib::CloseWindow();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -2,5 +2,5 @@
|
|||
|
||||
Frame pack(const Frame& in_frame, const std::vector<BoundingBox>& bboxes) {
|
||||
// TODO
|
||||
return Frame(1, 1);
|
||||
return Frame(in_frame);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue