Skip to content

Commit

Permalink
Size/unit/resolution handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregersn committed Aug 31, 2024
1 parent f10aaf9 commit 77fb402
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/pyro/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <filesystem>
#include <cstdint>
#include "constants.h"
#include "utils.h"

namespace Pyro
{
Expand Down Expand Up @@ -54,6 +55,8 @@ namespace Pyro
unsigned int width() { return this->_pixelwidth; };
unsigned int height() { return this->_pixelheight; };
unsigned int channels();
float real_width() { return pixels2unit(this->_pixelwidth, this->unit, this->dpi); };
float real_height() { return pixels2unit(this->_pixelwidth, this->unit, this->dpi); };
Unit unit{Unit::PX};

Image(const Image &in);
Expand Down
6 changes: 5 additions & 1 deletion include/pyro/pyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ namespace Pyro
extern unsigned int height;
extern uint32_t *pixels;

float real_width();
float real_height();

// Initialize the library with this.
void size(unsigned int width, unsigned int height, Unit unit = Unit::PX, unsigned int dpi = 72);
void size(unsigned int width, unsigned int height);
void size(float width, float height, Unit unit, unsigned int dpi = 72);

inline void save(const char *file) { pg->save(file); };
inline void save(const char *file, unsigned int dpi) { pg->save(file, dpi); };
Expand Down
1 change: 1 addition & 0 deletions screenshot-tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test_sources = [
'test-resolution.cpp',
'test-colors.cpp',
'test-units.cpp',
'test-pyro.cpp',
'screenshotmatcher.cpp']

catch2 = subproject('catch2', default_options: 'tests=false')
Expand Down
35 changes: 35 additions & 0 deletions screenshot-tests/test-pyro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <catch2/catch_all.hpp>
#include "test-settings.h"
#include "pyro/pyro.h"

TEST_CASE("Using differnt resolutions can give the same picture")
{
SECTION("Using inches and pixels")
{
unsigned int inwidth = 15;
unsigned int inheight = 10;
unsigned int dpi = 10;

unsigned int pwidth = inwidth * dpi;
unsigned int pheight = inheight * dpi;

Pyro::size(inwidth, inheight, Pyro::Unit::IN, dpi);
Pyro::strokeweight(1.0 / dpi);
Pyro::rect(0, 0, 1, 1);
Pyro::translate(2, 2);
Pyro::rect(0, 0, 1, 1);
Pyro::translate(Pyro::real_width() / 2.0, Pyro::real_height() / 2.0);
Pyro::ellipse(0, 0, 2, 2);
Pyro::save("/tmp/pyro-test-pyro-inches.png");

Pyro::size(pwidth, pheight, Pyro::Unit::PX, dpi);
Pyro::strokeweight(1.0);
Pyro::rect(0, 0, 10, 10);
Pyro::translate(20, 20);
Pyro::rect(0, 0, 10, 10);
Pyro::translate(Pyro::real_width() / 2.0, Pyro::real_height() / 2.0);
Pyro::ellipse(0, 0, 2 * dpi, 2 * dpi);
Pyro::save("/tmp/pyro-test-pyro-pixels.png");
CHECK_THAT("/tmp/pyro-test-pyro-inches.png", LooksLike("/tmp/pyro-test-pyro-pixels.png"));
}
}
3 changes: 0 additions & 3 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ namespace Pyro
// Coordinates
void Graphics::translate(float x, float y)
{
if (unit == Unit::CURRENT)
unit = this->unit;

x = x * pixel_multiplier;
y = y * pixel_multiplier;

Expand Down
18 changes: 13 additions & 5 deletions src/pyro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Pyro
unsigned int height{480};
uint32_t *pixels{nullptr};

float real_width() { return pg->real_width(); }
float real_height() { return pg->real_height(); }

void exit()
{
if (pg != nullptr)
Expand Down Expand Up @@ -36,10 +39,13 @@ namespace Pyro
}
};

void size(unsigned int width, unsigned int height, Unit unit, unsigned int dpi)
void size(unsigned int width, unsigned int height)
{
float multiplier{size_multiplier(unit, dpi)};
size(width, height, Unit::PX, 72);
}

void size(float width, float height, Unit unit, unsigned int dpi)
{
if (pg == nullptr)
{
std::atexit(exit);
Expand All @@ -49,10 +55,12 @@ namespace Pyro
{
delete pg;
}
Pyro::width = (unsigned int)width * multiplier;
Pyro::height = (unsigned int)height * multiplier;
pg = creategraphics(width, height, GraphicsMode::CAIRO);
// , 100, Pyro::Unit::MM
pg->set_dpi(dpi);
pg->set_unit(unit);
pg->init();
Pyro::width = pg->width();
Pyro::height = pg->height();
}

void loadpixels()
Expand Down

0 comments on commit 77fb402

Please sign in to comment.