Added padding

This commit is contained in:
tastytea 2018-12-27 06:13:49 +01:00
parent f14554f38f
commit c5aa9da086
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 44 additions and 52 deletions

View File

@ -8,7 +8,7 @@ modifications.
* [x] Symmetric identicons * [x] Symmetric identicons
* [x] sigil identicons * [x] sigil identicons
* [x] Asymmetric identicons * [x] Asymmetric identicons
* [ ] Padding * [x] Padding
## Usage ## Usage

View File

@ -29,7 +29,7 @@ int main(int argc, char *argv[])
"00ff00ff", "00ff00ff",
"00ffffff", "00ffffff",
"0000ffff" "0000ffff"
}); }, { 10, 10 });
img = identicon.generate(digest, 500); img = identicon.generate(digest, 500);
img.write("identicon_example_ltr_symmetric.png"); img.write("identicon_example_ltr_symmetric.png");
} }

View File

@ -20,15 +20,17 @@
#include "identiconpp.hpp" #include "identiconpp.hpp"
#include "debug.hpp" #include "debug.hpp"
Identiconpp::Identiconpp(const uint8_t rows, const uint8_t columns, Identiconpp::Identiconpp(const uint8_t columns, const uint8_t rows,
algorithm type, algorithm type,
const string &background, const string &background,
const vector<string> &foreground) const vector<string> &foreground,
const array<const uint8_t, 2> &padding)
: _rows(rows) : _rows(rows)
, _columns(columns) , _columns(columns)
, _type(type) , _type(type)
, _background(background) , _background(background)
, _foreground(foreground) , _foreground(foreground)
, _padding(padding)
{ {
check_color(background); check_color(background);
@ -42,25 +44,35 @@ Magick::Image Identiconpp::generate(const string &digest, const uint16_t width)
{ {
ttdebug << "Using digest: " << digest << '\n'; ttdebug << "Using digest: " << digest << '\n';
check_entropy(digest, _type); check_entropy(digest, _type);
const uint16_t height = width / _columns * _rows; const uint16_t imgwidth = width - _padding[0] * 2;
ttdebug << "width: " << std::to_string(width) const uint16_t imgheight = imgwidth / _columns * _rows;
<< ", height: " << std::to_string(height) << "\n"; ttdebug << "width: " << std::to_string(imgwidth + _padding[0] * 2)
<< ", height: " << std::to_string(imgheight + _padding[1] * 2)
<< "\n";
Magick::Image img;
switch (_type) switch (_type)
{ {
case algorithm::ltr_symmetric: case algorithm::ltr_symmetric:
{ {
return generate_ltr_symmetric(digest, width, height); img = generate_ltr_symmetric(digest);
} }
case algorithm::ltr_asymmetric: case algorithm::ltr_asymmetric:
{ {
return generate_ltr_asymmetric(digest, width, height); img = generate_ltr_asymmetric(digest);
} }
case algorithm::sigil: case algorithm::sigil:
{ {
return generate_sigil(digest, width, height); img = generate_sigil(digest);
} }
} }
img.backgroundColor(Magick::Color('#' + _background));
img.scale(Magick::Geometry(imgwidth, imgheight));
img.borderColor(Magick::Color('#' + _background));
img.border(Magick::Geometry(_padding[0], _padding[1]));
img.magick("png");
return img;
} }
bool Identiconpp::get_bit(const uint16_t bit, const string &digest) bool Identiconpp::get_bit(const uint16_t bit, const string &digest)

View File

@ -17,12 +17,14 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#include <array>
#include <Magick++/Image.h> #include <Magick++/Image.h>
using std::uint8_t; using std::uint8_t;
using std::uint16_t; using std::uint16_t;
using std::string; using std::string;
using std::vector; using std::vector;
using std::array;
/*! /*!
* @brief Base class for identiconpp. * @brief Base class for identiconpp.
@ -55,11 +57,13 @@ public:
* @param type The type of identicon * @param type The type of identicon
* @param background Background color, hexadecimal, rrggbbaa * @param background Background color, hexadecimal, rrggbbaa
* @param foreground vector of foreground colors * @param foreground vector of foreground colors
* @param padding Padding { left & right, top & down }
*/ */
explicit Identiconpp(const uint8_t rows, const uint8_t columns, explicit Identiconpp(const uint8_t columns, const uint8_t rows,
algorithm type = algorithm::ltr_symmetric, algorithm type = algorithm::ltr_symmetric,
const string &background = "ffffffff", const string &background = "ffffffff",
const vector<string> &foreground = { "000000ff" } ); const vector<string> &foreground = { "000000ff" },
const array<const uint8_t, 2> &padding = { 0, 0 });
/*! /*!
* @brief Generates identicon from digest. * @brief Generates identicon from digest.
@ -77,6 +81,7 @@ private:
const algorithm _type; const algorithm _type;
const string _background; const string _background;
const vector<string> _foreground; const vector<string> _foreground;
const array<const uint8_t, 2> _padding;
/*! /*!
* @brief Generate ltr_symmetric identicon. * @brief Generate ltr_symmetric identicon.
@ -86,48 +91,36 @@ private:
* from left to right, top to bottom. * from left to right, top to bottom.
* *
* @param digest The pre-computed digest * @param digest The pre-computed digest
* @param width The width of the image
* @param height The height of the image
* *
* @return The image * @return The image
*/ */
Magick::Image generate_ltr_symmetric(const string &digest, Magick::Image generate_ltr_symmetric(const string &digest);
const uint16_t width,
const uint16_t height);
/*! /*!
* @brief Generate ltr_asymmetric identicon. * @brief Generate ltr_asymmetric identicon.
* *
* Use bits 0 to _columns * _rows, use the * Use bits 0 to _columns * _rows, use the following bits to
* following bits to determine foreground color. Squares are drawn * determine foreground color. Squares are drawn from left to
* from left to right, top to bottom. * right, top to bottom.
* *
* @param digest The pre-computed digest * @param digest The pre-computed digest
* @param width The width of the image
* @param height The height of the image
* *
* @return The image * @return The image
*/ */
Magick::Image generate_ltr_asymmetric(const string &digest, Magick::Image generate_ltr_asymmetric(const string &digest);
const uint16_t width,
const uint16_t height);
/*! /*!
* @brief Generate sigil identicon. * @brief Generate sigil identicon.
* *
* Use bits 9 to (_columns / 2 + _columns % 2) * _rows, use the first * Use bits 9 to (_columns / 2 + _columns % 2) * _rows, use the
* 8 bits to determine foreground color. Squares are drawn from top * first 8 bits to determine foreground color. Squares are drawn
* to bottom, left to right. * from top to bottom, left to right.
* *
* @param digest The pre-computed digest * @param digest The pre-computed digest
* @param width The width of the image
* @param height The height of the image
* *
* @return The image * @return The image
*/ */
Magick::Image generate_sigil(const string &digest, Magick::Image generate_sigil(const string &digest);
const uint16_t width,
const uint16_t height);
/*! /*!
* @brief Check if the digest contains enough entropy. * @brief Check if the digest contains enough entropy.

View File

@ -17,12 +17,10 @@
#include "identiconpp.hpp" #include "identiconpp.hpp"
#include "debug.hpp" #include "debug.hpp"
Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest, Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest)
const uint16_t width,
const uint16_t height)
{ {
Magick::Image img(Magick::Geometry(_columns, _rows), Magick::Image img(Magick::Geometry(_columns, _rows),
Magick::Color("#" + _background)); Magick::Color("#00000000"));
Magick::Color dotcolor = get_color(_columns * _rows + 1, digest); Magick::Color dotcolor = get_color(_columns * _rows + 1, digest);
for (uint8_t row = 0; row < _rows; ++row) for (uint8_t row = 0; row < _rows; ++row)
@ -38,7 +36,5 @@ Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest,
} }
} }
img.scale(Magick::Geometry(width, height));
img.magick("png");
return img; return img;
} }

View File

@ -17,12 +17,10 @@
#include "identiconpp.hpp" #include "identiconpp.hpp"
#include "debug.hpp" #include "debug.hpp"
Magick::Image Identiconpp::generate_ltr_symmetric(const string &digest, Magick::Image Identiconpp::generate_ltr_symmetric(const string &digest)
const uint16_t width,
const uint16_t height)
{ {
Magick::Image img(Magick::Geometry(_columns, _rows), Magick::Image img(Magick::Geometry(_columns, _rows),
Magick::Color("#" + _background)); Magick::Color("#00000000"));
uint8_t used_columns = _columns / 2 + _columns % 2; uint8_t used_columns = _columns / 2 + _columns % 2;
Magick::Color dotcolor = get_color(used_columns * _rows + 1, digest); Magick::Color dotcolor = get_color(used_columns * _rows + 1, digest);
@ -40,7 +38,5 @@ Magick::Image Identiconpp::generate_ltr_symmetric(const string &digest,
} }
} }
img.scale(Magick::Geometry(width, height));
img.magick("png");
return img; return img;
} }

View File

@ -17,13 +17,10 @@
#include "identiconpp.hpp" #include "identiconpp.hpp"
#include "debug.hpp" #include "debug.hpp"
Magick::Image Identiconpp::generate_sigil(const string &digest, Magick::Image Identiconpp::generate_sigil(const string &digest)
const uint16_t width,
const uint16_t height)
{ {
// throw "Not implemented.";
Magick::Image img(Magick::Geometry(_columns, _rows), Magick::Image img(Magick::Geometry(_columns, _rows),
Magick::Color("#" + _background)); Magick::Color("#00000000"));
Magick::Color dotcolor = get_color(0, digest); Magick::Color dotcolor = get_color(0, digest);
uint8_t used_columns = _columns / 2 + _columns % 2; uint8_t used_columns = _columns / 2 + _columns % 2;
@ -41,7 +38,5 @@ Magick::Image Identiconpp::generate_sigil(const string &digest,
} }
} }
img.scale(Magick::Geometry(width, height));
img.magick("png");
return img; return img;
} }