2018-12-25 18:33:19 +01:00
|
|
|
/* This file is part of identiconpp.
|
2019-01-03 08:37:00 +01:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
2018-12-25 18:33:19 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-01-03 08:37:00 +01:00
|
|
|
#ifndef IDENTICONPP_HPP
|
|
|
|
#define IDENTICONPP_HPP
|
|
|
|
|
2018-12-25 18:33:19 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-12-27 06:13:49 +01:00
|
|
|
#include <array>
|
2018-12-25 23:02:19 +01:00
|
|
|
#include <Magick++/Image.h>
|
2018-12-25 18:33:19 +01:00
|
|
|
|
|
|
|
using std::uint8_t;
|
2018-12-26 03:26:53 +01:00
|
|
|
using std::uint16_t;
|
2018-12-25 18:33:19 +01:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2018-12-27 06:13:49 +01:00
|
|
|
using std::array;
|
2018-12-25 18:33:19 +01:00
|
|
|
|
2018-12-28 02:38:25 +01:00
|
|
|
/*!
|
|
|
|
* @example example.cpp
|
|
|
|
*/
|
|
|
|
|
2018-12-25 18:33:19 +01:00
|
|
|
/*!
|
|
|
|
* @brief Base class for identiconpp.
|
|
|
|
*
|
2018-12-26 04:36:58 +01:00
|
|
|
* Use this class for all your identicons. Exceptions will be thrown on
|
|
|
|
* error.
|
2018-12-25 18:33:19 +01:00
|
|
|
*/
|
|
|
|
class Identiconpp
|
|
|
|
{
|
|
|
|
public:
|
2019-01-04 20:07:11 +01:00
|
|
|
friend class Testiconpp;
|
2018-12-25 23:02:19 +01:00
|
|
|
/*!
|
2018-12-26 23:06:51 +01:00
|
|
|
* @brief List of identicon algorithms
|
2018-12-25 23:02:19 +01:00
|
|
|
*/
|
2018-12-26 23:06:51 +01:00
|
|
|
enum class algorithm
|
2018-12-25 23:02:19 +01:00
|
|
|
{
|
2018-12-28 01:57:08 +01:00
|
|
|
//! Generates symmetric (vertically mirrored) identicons.
|
2018-12-26 23:06:51 +01:00
|
|
|
ltr_symmetric,
|
2018-12-28 01:57:08 +01:00
|
|
|
//! Generates asymmetric identicons.
|
2018-12-27 05:21:40 +01:00
|
|
|
ltr_asymmetric,
|
2018-12-28 01:57:08 +01:00
|
|
|
/*!
|
|
|
|
* Generates the same results as
|
|
|
|
* [sigil](https://github.com/cupcake/sigil/) and
|
|
|
|
* [pydenticon](https://github.com/azaghal/pydenticon/).
|
|
|
|
*/
|
2018-12-25 23:02:19 +01:00
|
|
|
sigil
|
|
|
|
};
|
|
|
|
|
2018-12-25 18:33:19 +01:00
|
|
|
/*!
|
|
|
|
* @brief Initialises an instance of Identiconpp.
|
|
|
|
*
|
|
|
|
* The instance can be used for creating identicons with differing
|
2018-12-26 04:36:58 +01:00
|
|
|
* image formats and sizes. The colors must consist of exactly 8
|
|
|
|
* digits.
|
2018-12-25 18:33:19 +01:00
|
|
|
*
|
|
|
|
* @param columns Number of columns
|
2018-12-27 06:33:19 +01:00
|
|
|
* @param rows Number of rows
|
|
|
|
* @param type The algorithm to use
|
2018-12-25 18:33:19 +01:00
|
|
|
* @param background Background color, hexadecimal, rrggbbaa
|
|
|
|
* @param foreground vector of foreground colors
|
2018-12-27 06:33:19 +01:00
|
|
|
* @param padding Padding in pixels { left & right, top & down }
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-25 18:33:19 +01:00
|
|
|
*/
|
2018-12-27 06:13:49 +01:00
|
|
|
explicit Identiconpp(const uint8_t columns, const uint8_t rows,
|
2018-12-26 23:06:51 +01:00
|
|
|
algorithm type = algorithm::ltr_symmetric,
|
2018-12-26 05:20:41 +01:00
|
|
|
const string &background = "ffffffff",
|
2018-12-27 06:13:49 +01:00
|
|
|
const vector<string> &foreground = { "000000ff" },
|
2019-02-20 23:02:09 +01:00
|
|
|
const array<uint8_t, 2> &padding = { 0, 0 });
|
2018-12-25 18:33:19 +01:00
|
|
|
|
2018-12-25 23:02:19 +01:00
|
|
|
/*!
|
|
|
|
* @brief Generates identicon from digest.
|
|
|
|
*
|
|
|
|
* @param digest The pre-computed digest
|
2018-12-27 06:33:19 +01:00
|
|
|
* @param width The width of the image in pixels
|
2018-12-25 23:02:19 +01:00
|
|
|
*
|
2018-12-27 06:49:37 +01:00
|
|
|
* @return The image as Magick::Image
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-25 23:02:19 +01:00
|
|
|
*/
|
2018-12-26 04:36:58 +01:00
|
|
|
Magick::Image generate(const string &digest, const uint16_t width = 100);
|
2018-12-25 23:02:19 +01:00
|
|
|
|
2019-01-17 20:02:47 +01:00
|
|
|
/*!
|
|
|
|
* @brief Generates identicon from digest.
|
|
|
|
*
|
|
|
|
* @param magick See http://imagemagick.org/script/formats.php
|
|
|
|
* @param digest The pre-computed digest
|
|
|
|
* @param width The width of the image in pixels
|
|
|
|
*
|
|
|
|
* @return The image as base64-string
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since 0.5.0
|
2019-01-17 20:02:47 +01:00
|
|
|
*/
|
|
|
|
const string generate_base64(const string &magick, const string &digest,
|
|
|
|
const uint16_t width = 100);
|
|
|
|
|
2019-01-04 20:07:11 +01:00
|
|
|
private:
|
2018-12-25 18:33:19 +01:00
|
|
|
const uint8_t _rows;
|
|
|
|
const uint8_t _columns;
|
2018-12-26 23:06:51 +01:00
|
|
|
const algorithm _type;
|
2018-12-26 04:36:58 +01:00
|
|
|
const string _background;
|
|
|
|
const vector<string> _foreground;
|
2019-02-20 23:02:09 +01:00
|
|
|
const array<uint8_t, 2> _padding;
|
2018-12-25 23:02:19 +01:00
|
|
|
|
|
|
|
/*!
|
2018-12-27 05:21:40 +01:00
|
|
|
* @brief Generate ltr_symmetric identicon.
|
2018-12-25 23:02:19 +01:00
|
|
|
*
|
2018-12-26 23:51:32 +01:00
|
|
|
* Use bits 0 to (_columns / 2 + _columns % 2) * _rows, use the
|
2018-12-26 22:55:32 +01:00
|
|
|
* following bits to determine foreground color. Squares are drawn
|
|
|
|
* from left to right, top to bottom.
|
2018-12-26 22:16:11 +01:00
|
|
|
*
|
2018-12-25 23:02:19 +01:00
|
|
|
* @param digest The pre-computed digest
|
|
|
|
*
|
2018-12-26 04:36:58 +01:00
|
|
|
* @return The image
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-25 23:02:19 +01:00
|
|
|
*/
|
2018-12-27 06:13:49 +01:00
|
|
|
Magick::Image generate_ltr_symmetric(const string &digest);
|
2018-12-25 23:02:19 +01:00
|
|
|
|
2018-12-27 05:21:40 +01:00
|
|
|
/*!
|
|
|
|
* @brief Generate ltr_asymmetric identicon.
|
|
|
|
*
|
2018-12-27 06:13:49 +01:00
|
|
|
* Use bits 0 to _columns * _rows, use the following bits to
|
|
|
|
* determine foreground color. Squares are drawn from left to
|
|
|
|
* right, top to bottom.
|
2018-12-27 05:21:40 +01:00
|
|
|
*
|
|
|
|
* @param digest The pre-computed digest
|
|
|
|
*
|
|
|
|
* @return The image
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-27 05:21:40 +01:00
|
|
|
*/
|
2018-12-27 06:13:49 +01:00
|
|
|
Magick::Image generate_ltr_asymmetric(const string &digest);
|
2018-12-27 05:21:40 +01:00
|
|
|
|
2018-12-25 23:02:19 +01:00
|
|
|
/*!
|
2018-12-26 23:06:51 +01:00
|
|
|
* @brief Generate sigil identicon.
|
2018-12-25 23:02:19 +01:00
|
|
|
*
|
2018-12-27 06:13:49 +01:00
|
|
|
* 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.
|
2018-12-26 22:16:11 +01:00
|
|
|
*
|
2018-12-25 23:02:19 +01:00
|
|
|
* @param digest The pre-computed digest
|
|
|
|
*
|
2018-12-26 04:36:58 +01:00
|
|
|
* @return The image
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-25 23:02:19 +01:00
|
|
|
*/
|
2018-12-27 06:13:49 +01:00
|
|
|
Magick::Image generate_sigil(const string &digest);
|
2018-12-26 03:26:53 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Check if the digest contains enough entropy.
|
|
|
|
*
|
|
|
|
* Throws `std::invalid_argument` if not.
|
|
|
|
*
|
|
|
|
* @param digest The pre-computed digest
|
|
|
|
* @param type The type of identicon
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-26 03:26:53 +01:00
|
|
|
*/
|
2018-12-26 23:06:51 +01:00
|
|
|
void check_entropy(const string &digest, algorithm type);
|
2018-12-26 03:26:53 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Determines if the n-th bit of passed digest is 1 or 0.
|
|
|
|
*
|
|
|
|
* @param bit Bit to get
|
|
|
|
* @param digest The digest
|
|
|
|
*
|
|
|
|
* @return The bit.
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-26 03:26:53 +01:00
|
|
|
*/
|
|
|
|
bool get_bit(const uint16_t bit, const string &digest);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Chooses a foreground color.
|
|
|
|
*
|
|
|
|
* Extracts the right bits from the digest and returns a color.
|
|
|
|
*
|
|
|
|
* @param firstbit The first bit of the digest to choose a color
|
|
|
|
* @param digest The digest
|
|
|
|
*
|
|
|
|
* @return A foreground color.
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-26 03:26:53 +01:00
|
|
|
*/
|
|
|
|
Magick::Color get_color(const uint16_t firstbit, const string &digest);
|
2018-12-26 04:55:18 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Checks if character is not hexadecimal.
|
|
|
|
*
|
|
|
|
* @param c Character to check
|
|
|
|
*
|
|
|
|
* @return true if not hex.
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-26 04:55:18 +01:00
|
|
|
*/
|
|
|
|
static bool not_hex(const char c);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Performs checks on a color definition in a string.
|
|
|
|
*
|
|
|
|
* @param color The color as string
|
2019-02-20 23:02:09 +01:00
|
|
|
*
|
|
|
|
* @since before 0.5.0
|
2018-12-26 04:55:18 +01:00
|
|
|
*/
|
|
|
|
void check_color(const string &color);
|
2018-12-25 18:33:19 +01:00
|
|
|
};
|
2019-01-03 08:37:00 +01:00
|
|
|
|
|
|
|
#endif // IDENTICONPP_HPP
|