libravatar/sigil algorithm, first try

This commit is contained in:
tastytea 2018-12-26 22:16:11 +01:00
parent 77741a19df
commit eaffd0b005
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 44 additions and 21 deletions

View File

@ -34,20 +34,20 @@ int main(int argc, char *argv[])
img.write("identicon_example_simple.png");
}
// {
// Identiconpp identicon(10, 10, Identiconpp::identicon_type::libravatar,
// "ffffffff",
// {
// "000000ff",
// "ff0000ff",
// "ffff00ff",
// "00ff00ff",
// "00ffffff",
// "0000ffff"
// });
// img = identicon.generate(digest, 500);
// img.write("identicon_example_libravatar.png");
// }
{
Identiconpp identicon(5, 5, Identiconpp::identicon_type::libravatar,
"ffffffff",
{
"000000ff",
"ff0000ff",
"ffff00ff",
"00ff00ff",
"00ffffff",
"0000ffff"
});
img = identicon.generate("5a105e8b9d40e1329780d62ea2265d8a", 500);
img.write("identicon_example_libravatar.png");
}
return 0;
}

View File

@ -101,12 +101,12 @@ Magick::Color Identiconpp::get_color(const uint16_t firstbit,
// Get rid of excess bits
bits = bits & (1 << colorbits) - 1;
if (bits > _foreground.size())
if (bits > (_foreground.size() - 1))
{
bits -= _foreground.size();
bits -= (_foreground.size() - 1);
}
// Lookup und set color
ttdebug << "Color: #" << _foreground[bits - 1] << '\n';
return Magick::Color("#" + _foreground[bits - 1]);
ttdebug << "Color: #" << _foreground[bits] << '\n';
return Magick::Color("#" + _foreground[bits]);
}

View File

@ -83,6 +83,9 @@ private:
/*!
* @brief Generate simple identicon.
*
* Use bits 0 to (columns / 2 + columns % 2) * _rows, use the
* following bits to determine foreground color.
*
* @param digest The pre-computed digest
* @param width The width of the image
* @param height The height of the image
@ -96,6 +99,9 @@ private:
/*!
* @brief Generate libravatar-style / sigil identicon.
*
* Use bits 9 to (columns / 2 + columns % 2) * _rows, use the first
* 8 bits to determine foreground color.
*
* @param digest The pre-computed digest
* @param width The width of the image
* @param height The height of the image

View File

@ -21,8 +21,27 @@ Magick::Image Identiconpp::generate_libravatar(const string &digest,
const uint16_t width,
const uint16_t height)
{
throw "Not implemented.";
// throw "Not implemented.";
Magick::Image img(Magick::Geometry(_columns, _rows),
Magick::Color("#" + _background));
Magick::Color dotcolor = get_color(0, digest);
uint8_t used_columns = _columns / 2 + _columns % 2;
uint8_t cells = _rows * used_columns;
for (uint8_t cur_cell = 0; cur_cell < cells; ++cur_cell)
{
if (get_bit(8 + cur_cell, digest))
{
const uint8_t column = cur_cell / _columns;
const uint8_t row = cur_cell % _rows;
ttdebug << "col=" << std::to_string(column)
<< ", row=" << std::to_string(row) << '\n';
img.pixelColor(column, row, dotcolor);
img.pixelColor(_columns - column - 1, row, dotcolor);
}
}
img.scale(Magick::Geometry(width, height));
img.magick("png");
return img;
}

View File

@ -34,8 +34,6 @@ Magick::Image Identiconpp::generate_simple(const string &digest,
{
ttdebug << "col=" << std::to_string(column)
<< ", row=" << std::to_string(row) << '\n';
ttdebug << "col=" << std::to_string(used_columns - 1 + column)
<< ", row=" << std::to_string(_rows - 1 - row) << '\n';
img.pixelColor(column, row, dotcolor);
img.pixelColor(_columns - 1 - column, row, dotcolor);
}