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>
|
2018-12-27 21:56:50 +01:00
|
|
|
#include <cmath>
|
2018-12-25 18:33:19 +01:00
|
|
|
#include "identiconpp.hpp"
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
2018-12-27 06:13:49 +01:00
|
|
|
Identiconpp::Identiconpp(const uint8_t columns, const uint8_t rows,
|
2018-12-26 23:06:51 +01:00
|
|
|
algorithm type,
|
2018-12-26 05:20:41 +01:00
|
|
|
const string &background,
|
2018-12-27 06:13:49 +01:00
|
|
|
const vector<string> &foreground,
|
|
|
|
const array<const uint8_t, 2> &padding)
|
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-27 06:13:49 +01:00
|
|
|
, _padding(padding)
|
2018-12-25 18:33:19 +01:00
|
|
|
{
|
2019-01-02 11:16:39 +01:00
|
|
|
check_color(_background);
|
2018-12-26 04:36:58 +01:00
|
|
|
|
2019-01-02 11:16:39 +01:00
|
|
|
for (const string &color : _foreground)
|
2018-12-26 04:36:58 +01:00
|
|
|
{
|
2018-12-26 04:55:18 +01:00
|
|
|
check_color(color);
|
2018-12-26 04:36:58 +01:00
|
|
|
}
|
2019-01-02 11:16:39 +01:00
|
|
|
|
|
|
|
if (_foreground.size() == 0)
|
|
|
|
{
|
|
|
|
throw std::invalid_argument
|
|
|
|
(
|
|
|
|
"You must specify at least 1 foreground color."
|
|
|
|
);
|
|
|
|
}
|
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-25 23:02:19 +01:00
|
|
|
{
|
2018-12-27 02:25:56 +01:00
|
|
|
ttdebug << "Using digest: " << digest << '\n';
|
2018-12-26 04:36:58 +01:00
|
|
|
check_entropy(digest, _type);
|
2018-12-27 23:18:06 +01:00
|
|
|
const std::int16_t imgwidth = width - _padding[0] * 2;
|
|
|
|
const std::int16_t imgheight =
|
2018-12-27 22:24:36 +01:00
|
|
|
std::round(static_cast<float>(imgwidth) / _columns * _rows);
|
2018-12-27 23:18:06 +01:00
|
|
|
ttdebug << "width: " << std::to_string(imgwidth)
|
|
|
|
<< "+" << std::to_string(_padding[0] * 2)
|
|
|
|
<< ", height: " << std::to_string(imgheight)
|
|
|
|
<< "+" << std::to_string(_padding[1] * 2)
|
2018-12-27 06:13:49 +01:00
|
|
|
<< "\n";
|
2018-12-27 23:18:06 +01:00
|
|
|
if (imgwidth <= 0 || imgheight <= 0)
|
|
|
|
{
|
|
|
|
throw std::invalid_argument("Width or height is zero or less.");
|
|
|
|
}
|
2018-12-27 06:13:49 +01:00
|
|
|
Magick::Image img;
|
2018-12-26 04:36:58 +01:00
|
|
|
|
|
|
|
switch (_type)
|
2018-12-25 23:02:19 +01:00
|
|
|
{
|
2018-12-26 23:06:51 +01:00
|
|
|
case algorithm::ltr_symmetric:
|
2018-12-25 23:02:19 +01:00
|
|
|
{
|
2018-12-27 06:13:49 +01:00
|
|
|
img = generate_ltr_symmetric(digest);
|
2018-12-27 06:18:14 +01:00
|
|
|
break;
|
2018-12-25 23:02:19 +01:00
|
|
|
}
|
2018-12-27 05:21:40 +01:00
|
|
|
case algorithm::ltr_asymmetric:
|
|
|
|
{
|
2018-12-27 06:13:49 +01:00
|
|
|
img = generate_ltr_asymmetric(digest);
|
2018-12-27 06:18:14 +01:00
|
|
|
break;
|
2018-12-27 05:21:40 +01:00
|
|
|
}
|
2018-12-26 23:06:51 +01:00
|
|
|
case algorithm::sigil:
|
2018-12-25 23:02:19 +01:00
|
|
|
{
|
2018-12-27 06:13:49 +01:00
|
|
|
img = generate_sigil(digest);
|
2018-12-27 06:18:14 +01:00
|
|
|
break;
|
2018-12-25 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-27 06:13:49 +01:00
|
|
|
|
|
|
|
img.scale(Magick::Geometry(imgwidth, imgheight));
|
2019-01-02 06:00:51 +01:00
|
|
|
// The CompositeOperator prevents the background color to be affected by the
|
|
|
|
// frame color. See https://github.com/ImageMagick/ImageMagick/issues/647
|
|
|
|
img.compose(Magick::CompositeOperator::CopyCompositeOp);
|
|
|
|
img.matteColor(Magick::Color('#' + _background));
|
|
|
|
img.frame(Magick::Geometry(_padding[0], _padding[1]));
|
2018-12-27 06:13:49 +01:00
|
|
|
return img;
|
2018-12-25 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-27 02:25:56 +01:00
|
|
|
unsigned short nibble;
|
|
|
|
ss >> nibble;
|
|
|
|
|
|
|
|
// Shift nibble to the right until the bit we want is on the right border.
|
|
|
|
// Then check if it is set.
|
|
|
|
if (nibble >> (3 - bit % 4) & 1)
|
2018-12-26 03:26:53 +01:00
|
|
|
{
|
2018-12-27 02:25:56 +01:00
|
|
|
ttdebug << "Bit " << std::to_string(bit + 1) << " is set.\n";
|
2018-12-26 03:26:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-25 23:02:19 +01:00
|
|
|
|
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
|
2018-12-27 22:24:36 +01:00
|
|
|
const uint16_t colorbits = std::floor(std::log2(_foreground.size())) + 1;
|
2018-12-26 03:26:53 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2018-12-27 02:25:56 +01:00
|
|
|
// std::stringstream does not support writing into uint16_t
|
2018-12-26 03:26:53 +01:00
|
|
|
unsigned short bits;
|
|
|
|
ss >> bits;
|
|
|
|
|
2018-12-27 02:25:56 +01:00
|
|
|
// Shift an one $colorbits times to the left, substract 1. This leaves us
|
|
|
|
// with $colorbits ones. Then AND bits and our ones to keep only as many
|
|
|
|
// bits as we need.
|
|
|
|
bits = bits & ((1 << colorbits) - 1);
|
2018-12-26 03:26:53 +01:00
|
|
|
|
2018-12-27 02:25:56 +01:00
|
|
|
// We may get a number that is slightly too big if _foreground.size() is not
|
|
|
|
// a power of 2.
|
2018-12-26 22:16:11 +01:00
|
|
|
if (bits > (_foreground.size() - 1))
|
2018-12-26 04:36:58 +01:00
|
|
|
{
|
2018-12-28 01:40:56 +01:00
|
|
|
bits -= _foreground.size();
|
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]);
|
2018-12-25 23:02:19 +01:00
|
|
|
}
|