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] sigil identicons
* [x] Asymmetric identicons
* [ ] Padding
* [x] Padding
## Usage

View File

@ -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");
}

View File

@ -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<string> &foreground)
const vector<string> &foreground,
const array<const uint8_t, 2> &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)

View File

@ -17,12 +17,14 @@
#include <cstdint>
#include <string>
#include <vector>
#include <array>
#include <Magick++/Image.h>
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<string> &foreground = { "000000ff" } );
const vector<string> &foreground = { "000000ff" },
const array<const uint8_t, 2> &padding = { 0, 0 });
/*!
* @brief Generates identicon from digest.
@ -77,6 +81,7 @@ private:
const algorithm _type;
const string _background;
const vector<string> _foreground;
const array<const uint8_t, 2> _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.

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}