diff --git a/README.md b/README.md index 0306e51..8a38b24 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ modifications. * [x] Symmetric identicons * [x] sigil identicons * [x] Asymmetric identicons -* [ ] Padding +* [x] Padding ## Usage diff --git a/example.cpp b/example.cpp index e8354a5..9d9892c 100644 --- a/example.cpp +++ b/example.cpp @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) "00ff00ff", "00ffffff", "0000ffff" - }); + }, { 10, 10 }); img = identicon.generate(digest, 500); img.write("identicon_example_ltr_symmetric.png"); } diff --git a/src/identiconpp.cpp b/src/identiconpp.cpp index 5dadcb6..1d925cf 100644 --- a/src/identiconpp.cpp +++ b/src/identiconpp.cpp @@ -20,15 +20,17 @@ #include "identiconpp.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, const string &background, - const vector &foreground) + const vector &foreground, + const array &padding) : _rows(rows) , _columns(columns) , _type(type) , _background(background) , _foreground(foreground) +, _padding(padding) { check_color(background); @@ -42,25 +44,35 @@ Magick::Image Identiconpp::generate(const string &digest, const uint16_t width) { ttdebug << "Using digest: " << digest << '\n'; check_entropy(digest, _type); - const uint16_t height = width / _columns * _rows; - ttdebug << "width: " << std::to_string(width) - << ", height: " << std::to_string(height) << "\n"; + const uint16_t imgwidth = width - _padding[0] * 2; + const uint16_t imgheight = imgwidth / _columns * _rows; + ttdebug << "width: " << std::to_string(imgwidth + _padding[0] * 2) + << ", height: " << std::to_string(imgheight + _padding[1] * 2) + << "\n"; + Magick::Image img; switch (_type) { case algorithm::ltr_symmetric: { - return generate_ltr_symmetric(digest, width, height); + img = generate_ltr_symmetric(digest); } case algorithm::ltr_asymmetric: { - return generate_ltr_asymmetric(digest, width, height); + img = generate_ltr_asymmetric(digest); } 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) diff --git a/src/identiconpp.hpp b/src/identiconpp.hpp index ed40687..559a9c8 100644 --- a/src/identiconpp.hpp +++ b/src/identiconpp.hpp @@ -17,12 +17,14 @@ #include #include #include +#include #include using std::uint8_t; using std::uint16_t; using std::string; using std::vector; +using std::array; /*! * @brief Base class for identiconpp. @@ -55,11 +57,13 @@ public: * @param type The type of identicon * @param background Background color, hexadecimal, rrggbbaa * @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, const string &background = "ffffffff", - const vector &foreground = { "000000ff" } ); + const vector &foreground = { "000000ff" }, + const array &padding = { 0, 0 }); /*! * @brief Generates identicon from digest. @@ -77,6 +81,7 @@ private: const algorithm _type; const string _background; const vector _foreground; + const array _padding; /*! * @brief Generate ltr_symmetric identicon. @@ -86,48 +91,36 @@ private: * from left to right, top to bottom. * * @param digest The pre-computed digest - * @param width The width of the image - * @param height The height of the image * * @return The image */ - Magick::Image generate_ltr_symmetric(const string &digest, - const uint16_t width, - const uint16_t height); + Magick::Image generate_ltr_symmetric(const string &digest); /*! * @brief Generate ltr_asymmetric identicon. * - * Use bits 0 to _columns * _rows, use the - * following bits to determine foreground color. Squares are drawn - * from left to right, top to bottom. + * Use bits 0 to _columns * _rows, use the following bits to + * determine foreground color. Squares are drawn from left to + * right, top to bottom. * * @param digest The pre-computed digest - * @param width The width of the image - * @param height The height of the image * * @return The image */ - Magick::Image generate_ltr_asymmetric(const string &digest, - const uint16_t width, - const uint16_t height); + Magick::Image generate_ltr_asymmetric(const string &digest); /*! * @brief Generate sigil identicon. * - * Use bits 9 to (_columns / 2 + _columns % 2) * _rows, use the first - * 8 bits to determine foreground color. Squares are drawn from top - * to bottom, left to right. + * Use bits 9 to (_columns / 2 + _columns % 2) * _rows, use the + * first 8 bits to determine foreground color. Squares are drawn + * from top to bottom, left to right. * * @param digest The pre-computed digest - * @param width The width of the image - * @param height The height of the image * * @return The image */ - Magick::Image generate_sigil(const string &digest, - const uint16_t width, - const uint16_t height); + Magick::Image generate_sigil(const string &digest); /*! * @brief Check if the digest contains enough entropy. diff --git a/src/ltr_asymmetric.cpp b/src/ltr_asymmetric.cpp index 8320971..c693491 100644 --- a/src/ltr_asymmetric.cpp +++ b/src/ltr_asymmetric.cpp @@ -17,12 +17,10 @@ #include "identiconpp.hpp" #include "debug.hpp" -Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest, - const uint16_t width, - const uint16_t height) +Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest) { Magick::Image img(Magick::Geometry(_columns, _rows), - Magick::Color("#" + _background)); + Magick::Color("#00000000")); Magick::Color dotcolor = get_color(_columns * _rows + 1, digest); 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; } diff --git a/src/ltr_symmetric.cpp b/src/ltr_symmetric.cpp index ccb2cd9..a5aad60 100644 --- a/src/ltr_symmetric.cpp +++ b/src/ltr_symmetric.cpp @@ -17,12 +17,10 @@ #include "identiconpp.hpp" #include "debug.hpp" -Magick::Image Identiconpp::generate_ltr_symmetric(const string &digest, - const uint16_t width, - const uint16_t height) +Magick::Image Identiconpp::generate_ltr_symmetric(const string &digest) { Magick::Image img(Magick::Geometry(_columns, _rows), - Magick::Color("#" + _background)); + Magick::Color("#00000000")); uint8_t used_columns = _columns / 2 + _columns % 2; 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; } diff --git a/src/sigil.cpp b/src/sigil.cpp index 45b18ae..8945979 100644 --- a/src/sigil.cpp +++ b/src/sigil.cpp @@ -17,13 +17,10 @@ #include "identiconpp.hpp" #include "debug.hpp" -Magick::Image Identiconpp::generate_sigil(const string &digest, - const uint16_t width, - const uint16_t height) +Magick::Image Identiconpp::generate_sigil(const string &digest) { - // throw "Not implemented."; Magick::Image img(Magick::Geometry(_columns, _rows), - Magick::Color("#" + _background)); + Magick::Color("#00000000")); Magick::Color dotcolor = get_color(0, digest); 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; }