identiconpp/src/identiconpp.cpp

114 lines
3.1 KiB
C++
Raw Normal View History

2018-12-25 18:33:19 +01:00
/* This file is part of identiconpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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/>.
*/
#include <exception>
#include <stdexcept>
2018-12-26 03:26:53 +01:00
#include <sstream>
#include <bitset>
2018-12-25 18:33:19 +01:00
#include "identiconpp.hpp"
#include "debug.hpp"
Identiconpp::Identiconpp(const uint8_t rows, const uint8_t columns,
2018-12-26 04:36:58 +01:00
identicon_type type,
const string &background,
2018-12-26 04:36:58 +01:00
const vector<string> &foreground)
2018-12-25 18:33:19 +01:00
: _rows(rows)
, _columns(columns)
2018-12-26 04:36:58 +01:00
, _type(type)
2018-12-25 18:33:19 +01:00
, _background(background)
, _foreground(foreground)
{
2018-12-26 04:55:18 +01:00
check_color(background);
2018-12-26 04:36:58 +01:00
for (const string &color : foreground)
{
2018-12-26 04:55:18 +01:00
check_color(color);
2018-12-26 04:36:58 +01:00
}
2018-12-25 18:33:19 +01:00
}
2018-12-26 04:36:58 +01:00
Magick::Image Identiconpp::generate(const string &digest, const uint16_t width)
{
2018-12-26 04:36:58 +01:00
check_entropy(digest, _type);
const uint16_t height = width / _columns * _rows;
ttdebug << "width: " << std::to_string(width)
<< ", height: " << std::to_string(height) << "\n";
switch (_type)
{
case identicon_type::simple:
{
2018-12-26 03:26:53 +01:00
return generate_simple(digest, width, height);
}
case identicon_type::libravatar:
case identicon_type::sigil:
{
2018-12-26 03:26:53 +01:00
return generate_libravatar(digest, width, height);
}
}
}
2018-12-26 03:26:53 +01:00
bool Identiconpp::get_bit(const uint16_t bit, const string &digest)
{
std::stringstream ss;
ss << std::hex << digest[bit / 4];
// std::stringstream does not support writing into uint8_t
unsigned short buf;
ss >> buf;
std::bitset<4> nibble(buf);
if (nibble[3 - bit % 4])
2018-12-26 03:26:53 +01:00
{
ttdebug << "Bit " << std::to_string(bit + 1) << " is set in "
2018-12-26 18:52:27 +01:00
<< digest << ".\n";
ttdebug << nibble << " (" << bit % 4 << ")\n";
2018-12-26 03:26:53 +01:00
return true;
}
2018-12-26 03:26:53 +01:00
return false;
}
Magick::Color Identiconpp::get_color(const uint16_t firstbit,
const string &digest)
{
// Number of bits to use
const uint16_t colorbits = _foreground.size() / 2 + _foreground.size() % 2;
// Extract approximation
std::stringstream ss;
if (colorbits % 4 == 0)
{
ss << std::hex << digest.substr(firstbit / 4, colorbits / 4);
}
else
{
ss << std::hex << digest.substr(firstbit / 4, colorbits / 4 + 1);
}
unsigned short bits;
ss >> bits;
// Get rid of excess bits
bits = bits & (1 << colorbits) - 1;
2018-12-26 22:16:11 +01:00
if (bits > (_foreground.size() - 1))
2018-12-26 04:36:58 +01:00
{
2018-12-26 22:16:11 +01:00
bits -= (_foreground.size() - 1);
2018-12-26 04:36:58 +01:00
}
2018-12-26 03:26:53 +01:00
// Lookup und set color
2018-12-26 22:16:11 +01:00
ttdebug << "Color: #" << _foreground[bits] << '\n';
return Magick::Color("#" + _foreground[bits]);
}