Prepared functions for simple and libravatar-style identicons.

This commit is contained in:
tastytea 2018-12-25 23:02:19 +01:00
parent aaece0eabc
commit c94fc018eb
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 93 additions and 3 deletions

View File

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

View File

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

View File

@ -17,6 +17,8 @@
#include <cstdint>
#include <string>
#include <vector>
#include <tuple>
#include <Magick++/Image.h>
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<uint32_t> &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<uint32_t> _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);
};