Geometrize 1.0
C++ library for geometrizing images into geometric primitives
Functions | Variables
geometrize::commonutil Namespace Reference

Functions

static thread_local std::mt19937 mt (std::random_device{}())
 
void seedRandomGenerator (std::uint32_t seed)
 seedRandomGenerator Seeds the (thread-local) random number generators. More...
 
std::int32_t randomRange (std::int32_t min, std::int32_t max)
 randomRange Returns a random integer in the range, inclusive. Uses thread-local random number generators under the hood. To ensure deterministic shape generation that can be repeated for different seeds, this should be used for shape mutation, but nothing else. More...
 
geometrize::rgba getAverageImageColor (const geometrize::Bitmap &image)
 getAverageImageColor Computes the average RGB color of the pixels in the bitmap. More...
 
bool scanlinesContainTransparentPixels (const std::vector< geometrize::Scanline > &scanlines, const geometrize::Bitmap &image, int minAlpha)
 scanlinesContainTransparentPixels Returns true if the scanlines contain transparent pixels in the given image More...
 
std::tuple< std::int32_t, std::int32_t, std::int32_t, std::int32_t > mapShapeBoundsToImage (const geometrize::ImageRunnerShapeBoundsOptions &options, const geometrize::Bitmap &image)
 mapShapeBoundsToImage Maps the given shape bound percentages to the given image, returning a bounding rectangle, or the whole image if the bounds were invalid More...
 
template<typename T >
clamp (const T &value, const T &lower, const T &upper)
 clamp Clamps a value within a range. More...
 

Variables

static thread_local std::uniform_int_distribution< std::int32_t > pick
 

Function Documentation

◆ clamp()

template<typename T >
T geometrize::commonutil::clamp ( const T &  value,
const T &  lower,
const T &  upper 
)

clamp Clamps a value within a range.

Parameters
valueThe value to clamp.
lowerThe lower bound of the range.
upperThe upper bound of the range.
Returns
The clamped value.
46{
47 return (std::max)(lower, (std::min)(value, upper));
48}
Here is the caller graph for this function:

◆ getAverageImageColor()

geometrize::rgba geometrize::commonutil::getAverageImageColor ( const geometrize::Bitmap image)

getAverageImageColor Computes the average RGB color of the pixels in the bitmap.

Parameters
imageThe image whose average color will be calculated.
Returns
The average RGB color of the image, RGBA8888 format. Alpha is set to opaque (255).
36{
37 const std::vector<std::uint8_t>& data{image.getDataRef()};
38 const std::size_t size{data.size()};
39 if(size == 0) {
40 return geometrize::rgba{0, 0, 0, 0};
41 }
42
43 const std::size_t numPixels{size / 4U};
44
45 std::uint32_t totalRed{0};
46 std::uint32_t totalGreen{0};
47 std::uint32_t totalBlue{0};
48 for(std::size_t i = 0; i < size; i += 4U) {
49 totalRed += data[i];
50 totalGreen += data[i + 1U];
51 totalBlue += data[i + 2U];
52 }
53
54 return geometrize::rgba{
55 static_cast<std::uint8_t>(totalRed / numPixels),
56 static_cast<std::uint8_t>(totalGreen / numPixels),
57 static_cast<std::uint8_t>(totalBlue / numPixels),
58 static_cast<std::uint8_t>(UINT8_MAX)
59 };
60}
const std::vector< std::uint8_t > & getDataRef() const
getDataRef Gets a reference to the raw bitmap data.
Definition: bitmap.cpp:35
The rgba struct is a helper for manipulating RGBA8888 color data.
Definition: rgba.h:13
Here is the call graph for this function:

◆ mapShapeBoundsToImage()

std::tuple< std::int32_t, std::int32_t, std::int32_t, std::int32_t > geometrize::commonutil::mapShapeBoundsToImage ( const geometrize::ImageRunnerShapeBoundsOptions options,
const geometrize::Bitmap image 
)

mapShapeBoundsToImage Maps the given shape bound percentages to the given image, returning a bounding rectangle, or the whole image if the bounds were invalid

Parameters
Theoptions to map to the image
Theimage to map the options around
Returns
The mapped shape bounds (xMin, yMin, xMax, yMax)
76{
77 if(!options.enabled) {
78 return { 0, 0, image.getWidth() - 1, image.getHeight() - 1 };
79 }
80
81 const double xMinPx = options.xMinPercent / 100.0 * static_cast<double>(image.getWidth() - 1);
82 const double yMinPx = options.yMinPercent / 100.0 * static_cast<double>(image.getHeight() - 1);
83 const double xMaxPx = options.xMaxPercent / 100.0 * static_cast<double>(image.getWidth() - 1);
84 const double yMaxPx = options.yMaxPercent / 100.0 * static_cast<double>(image.getHeight() - 1);
85
86 std::int32_t xMin = static_cast<std::int32_t>(std::round(std::min(std::min(xMinPx, xMaxPx), image.getWidth() - 1.0)));
87 std::int32_t yMin = static_cast<std::int32_t>(std::round(std::min(std::min(yMinPx, yMaxPx), image.getHeight() - 1.0)));
88 std::int32_t xMax = static_cast<std::int32_t>(std::round(std::min(std::max(xMinPx, xMaxPx), image.getWidth() - 1.0)));
89 std::int32_t yMax = static_cast<std::int32_t>(std::round(std::min(std::max(yMinPx, yMaxPx), image.getHeight() - 1.0)));
90
91 // If we have a bad width or height, which is bound to cause problems - use the whole image
92 if(xMax - xMin <= 1) {
93 xMin = 0;
94 xMax = image.getWidth() - 1;
95 }
96 if(yMax - yMin <= 1) {
97 yMin = 0;
98 yMax = image.getHeight() - 1;
99 }
100
101 return std::make_tuple(xMin, yMin, xMax, yMax);
102}
std::uint32_t getHeight() const
getHeight Gets the height of the bitmap.
Definition: bitmap.cpp:25
std::uint32_t getWidth() const
getWidth Gets the width of the bitmap.
Definition: bitmap.cpp:20
double xMinPercent
Definition: imagerunneroptions.h:17
double yMinPercent
Definition: imagerunneroptions.h:18
double xMaxPercent
Definition: imagerunneroptions.h:19
bool enabled
Definition: imagerunneroptions.h:16
double yMaxPercent
Definition: imagerunneroptions.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt()

static thread_local std::mt19937 geometrize::commonutil::mt ( std::random_device{}  ())
static
Here is the caller graph for this function:

◆ randomRange()

std::int32_t geometrize::commonutil::randomRange ( std::int32_t  min,
std::int32_t  max 
)

randomRange Returns a random integer in the range, inclusive. Uses thread-local random number generators under the hood. To ensure deterministic shape generation that can be repeated for different seeds, this should be used for shape mutation, but nothing else.

Parameters
minThe lower bound.
maxThe upper bound.
Returns
The random integer in the range.
30{
31 assert(min <= max);
32 return pick(mt, std::uniform_int_distribution<std::int32_t>::param_type{min, max});
33}
static thread_local std::mt19937 mt(std::random_device{}())
static thread_local std::uniform_int_distribution< std::int32_t > pick
Definition: commonutil.cpp:22
Here is the call graph for this function:
Here is the caller graph for this function:

◆ scanlinesContainTransparentPixels()

bool geometrize::commonutil::scanlinesContainTransparentPixels ( const std::vector< geometrize::Scanline > &  scanlines,
const geometrize::Bitmap image,
int  minAlpha 
)

scanlinesContainTransparentPixels Returns true if the scanlines contain transparent pixels in the given image

Parameters
scanlinesThe scanlines to check
imageThe image whose pixels to check
minAlphaThe minimum alpha level (0-255) to consider transparent
Returns
True if the scanlines contains any transparent pixels
63{
64 const auto& trimmedScanlines = geometrize::trimScanlines(scanlines, 0, 0, image.getWidth(), image.getHeight());
65 for(const geometrize::Scanline& scanline : trimmedScanlines) {
66 for(int x = scanline.x1; x < scanline.x2; x++) {
67 if(image.getPixel(x, scanline.y).a < minAlpha) {
68 return true;
69 }
70 }
71 }
72 return false;
73}
geometrize::rgba getPixel(std::uint32_t x, std::uint32_t y) const
getPixel Gets a pixel color value.
Definition: bitmap.cpp:40
The Scanline class represents a scanline, a row of pixels running across a bitmap.
Definition: scanline.h:14
std::vector< geometrize::Scanline > trimScanlines(const std::vector< geometrize::Scanline > &scanlines, std::int32_t minX, std::int32_t minY, std::int32_t maxX, std::int32_t maxY)
trimScanlines Crops the scanning width of an array of scanlines so they do not scan outside of the gi...
Definition: scanline.cpp:23
std::uint8_t a
‍The blue component (0-255).
Definition: rgba.h:17
Here is the call graph for this function:

◆ seedRandomGenerator()

void geometrize::commonutil::seedRandomGenerator ( std::uint32_t  seed)

seedRandomGenerator Seeds the (thread-local) random number generators.

Parameters
seedThe random seed.
25{
26 mt.seed(seed);
27}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ pick

thread_local std::uniform_int_distribution<std::int32_t> geometrize::commonutil::pick
static