From c94fc018eb9b6ce1e4f09a1eee4d5779c1ef0702 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 25 Dec 2018 23:02:19 +0100 Subject: [PATCH] Prepared functions for simple and libravatar-style identicons. --- example.cpp | 3 ++- src/identiconpp.cpp | 40 ++++++++++++++++++++++++++++++++++ src/identiconpp.hpp | 53 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/example.cpp b/example.cpp index d7e6bd3..46ee738 100644 --- a/example.cpp +++ b/example.cpp @@ -7,6 +7,7 @@ #include "identiconpp.hpp" using std::cout; +using std::endl; using std::string; int main(int argc, char *argv[]) @@ -28,6 +29,6 @@ int main(int argc, char *argv[]) } Identiconpp identicon(5, 5, 0xffffffff, { 0x000000ff, 0x000000 }); - + identicon.generate("testtest", Identiconpp::identicon_type::simple); return 0; } diff --git a/src/identiconpp.cpp b/src/identiconpp.cpp index fcaf2de..88d74ec 100644 --- a/src/identiconpp.cpp +++ b/src/identiconpp.cpp @@ -29,3 +29,43 @@ Identiconpp::Identiconpp(const uint8_t rows, const uint8_t columns, { } +Identiconpp::Image Identiconpp::generate(const string &digest, + identicon_type type) +{ + switch (type) + { + case identicon_type::simple: + { + return generate_simple(digest); + } + case identicon_type::libravatar: + case identicon_type::sigil: + { + return generate_libravatar(digest); + } + } +} + +Identiconpp::Image Identiconpp::generate_simple(const string &digest) +{ + return { 1, Magick::Image() }; +} + +Identiconpp::Image Identiconpp::generate_libravatar(const string &digest) +{ + uint8_t entropy_provided = digest.length() / 2 * 8; + uint8_t entropy_required = (_columns / 2 + _columns % 2) * _rows + 8; + ttdebug << "entropy_provided=" << std::to_string(entropy_provided) + << ", entropy_required=" << std::to_string(entropy_required) << '\n'; + + if (entropy_provided < entropy_required) + { + throw std::invalid_argument( + "Passed digest \"" + digest + "\" is not capable of providing " + + std::to_string(entropy_required) + " bits of entropy."); + } + + // TODO: implement + + return { 1, Magick::Image() }; +} diff --git a/src/identiconpp.hpp b/src/identiconpp.hpp index 9e90fa6..65b2f64 100644 --- a/src/identiconpp.hpp +++ b/src/identiconpp.hpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include using std::uint8_t; using std::uint32_t; @@ -31,12 +33,32 @@ using std::vector; class Identiconpp { public: + /*! + * @brief Return type for images. + */ + struct Image + { + uint8_t error; + Magick::Image data; + }; + + /*! + * @brief List of identicon types + * + * libravatar and sigil are synonymous. + */ + enum class identicon_type + { + simple, + libravatar, + sigil + }; + /*! * @brief Initialises an instance of Identiconpp. * * The instance can be used for creating identicons with differing - * image formats and sizes. Will throw `std::invalid_argument` if - * an argument is invalid. + * image formats and sizes. * * @param rows Number of rows * @param columns Number of columns @@ -47,9 +69,36 @@ public: const uint32_t background = 0xffffffff, const vector &foreground = { 0x000000ff } ); + /*! + * @brief Generates identicon from digest. + * + * @param digest The pre-computed digest + * + * @return 0 and an image on success, 1 and an empty image on error. + */ + Image generate(const string &digest, identicon_type type); + private: const uint8_t _rows; const uint8_t _columns; const uint32_t _background; const vector _foreground; + + /*! + * @brief Generate simple identicon. + * + * @param digest The pre-computed digest + * + * @return 0 and an image on success, 1 and an empty image on error. + */ + Image generate_simple(const string &digest); + + /*! + * @brief Generate libravatar-style identicon. + * + * @param digest The pre-computed digest + * + * @return 0 and an image on success, 1 and an empty image on error. + */ + Image generate_libravatar(const string &digest); };