identiconpp/src/identiconpp.hpp

190 lines
5.6 KiB
C++
Raw Normal View History

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>
#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:
friend class Testiconpp;
/*!
* @brief List of identicon algorithms
*/
enum class algorithm
{
2018-12-28 01:57:08 +01:00
//! Generates symmetric (vertically mirrored) identicons.
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/).
*/
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 }
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,
algorithm type = algorithm::ltr_symmetric,
const string &background = "ffffffff",
2018-12-27 06:13:49 +01:00
const vector<string> &foreground = { "000000ff" },
const array<const uint8_t, 2> &padding = { 0, 0 });
2018-12-25 18:33: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
*
* @return The image as Magick::Image
*/
2018-12-26 04:36:58 +01:00
Magick::Image generate(const string &digest, const uint16_t width = 100);
private:
2018-12-25 18:33:19 +01:00
const uint8_t _rows;
const uint8_t _columns;
const algorithm _type;
2018-12-26 04:36:58 +01:00
const string _background;
const vector<string> _foreground;
2018-12-27 06:13:49 +01:00
const array<const uint8_t, 2> _padding;
/*!
2018-12-27 05:21:40 +01:00
* @brief Generate ltr_symmetric identicon.
*
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
*
* @param digest The pre-computed digest
*
2018-12-26 04:36:58 +01:00
* @return The image
*/
2018-12-27 06:13:49 +01:00
Magick::Image generate_ltr_symmetric(const string &digest);
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
*/
2018-12-27 06:13:49 +01:00
Magick::Image generate_ltr_asymmetric(const string &digest);
2018-12-27 05:21:40 +01:00
/*!
* @brief Generate sigil identicon.
*
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
*
* @param digest The pre-computed digest
*
2018-12-26 04:36:58 +01:00
* @return The image
*/
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
*/
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.
*/
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.
*/
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.
*/
static bool not_hex(const char c);
/*!
* @brief Performs checks on a color definition in a string.
*
* @param color The color as string
*/
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