diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c6e49a..f9365e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(identiconpp - VERSION 0.2.1 + VERSION 0.3.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 10511d7..0306e51 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ modifications. * [x] Symmetric identicons * [x] sigil identicons -* [ ] Asymmetric identicons +* [x] Asymmetric identicons * [ ] Padding ## Usage @@ -132,6 +132,26 @@ and you are welcome to redistribute it under certain conditions. Implemented in [ltr_symmetric.cpp] (https://schlomp.space/tastytea/identiconpp/src/branch/master/src/ltr_symmetric.cpp) +### ltr_asymmetric + +* Create image with width=columns, height=rows. +* Set background color. +* Pixels are drawn from left to right, top to bottom. +* Use bits from digest to determine if a pixel is painted(1) or not(0). +* Use the following bits to pick the foreground color. +* Scale image proportionally to requested width. + +```PLAIN + 0111 0011 1101 1100 […] 1111 0111 0101 0111 +^ ^ ++----------------------------+--------------> + | | + pixel matrix foreground color +``` + +Implemented in [ltr_asymmetric.cpp] +(https://schlomp.space/tastytea/identiconpp/src/branch/master/src/ltr_asymmetric.cpp) + ### sigil * Create image with width=columns, height=rows. diff --git a/example.cpp b/example.cpp index 517550a..e8354a5 100644 --- a/example.cpp +++ b/example.cpp @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) { Identiconpp identicon(5, 5, Identiconpp::algorithm::ltr_symmetric, - "ffffff88", + "ffffff80", { "000000ff", "ff0000ff", @@ -34,6 +34,21 @@ int main(int argc, char *argv[]) img.write("identicon_example_ltr_symmetric.png"); } + { + Identiconpp identicon(5, 5, Identiconpp::algorithm::ltr_asymmetric, + "00000080", + { + "ffffffc0", + "ff0000c0", + "ffff00c0", + "00ff00c0", + "00ffffc0", + "0000ffc0" + }); + img = identicon.generate(digest, 500); + img.write("identicon_example_ltr_asymmetric.png"); + } + { Identiconpp identicon(5, 5, Identiconpp::algorithm::sigil, "ffffffff", diff --git a/src/checks.cpp b/src/checks.cpp index 2bd44d1..29ef100 100644 --- a/src/checks.cpp +++ b/src/checks.cpp @@ -44,6 +44,13 @@ void Identiconpp::check_entropy(const string &digest, algorithm type) + (_foreground.size() / 2 + _foreground.size() % 2); break; } + case algorithm::ltr_asymmetric: + { + entropy_provided = digest.length() * 4; + entropy_required = _columns * _rows + + (_foreground.size() / 2 + _foreground.size() % 2); + break; + } case algorithm::sigil: { entropy_provided = digest.length() / 2 * 8; diff --git a/src/identiconpp.cpp b/src/identiconpp.cpp index dd347d1..5dadcb6 100644 --- a/src/identiconpp.cpp +++ b/src/identiconpp.cpp @@ -52,6 +52,10 @@ Magick::Image Identiconpp::generate(const string &digest, const uint16_t width) { return generate_ltr_symmetric(digest, width, height); } + case algorithm::ltr_asymmetric: + { + return generate_ltr_asymmetric(digest, width, height); + } case algorithm::sigil: { return generate_sigil(digest, width, height); diff --git a/src/identiconpp.hpp b/src/identiconpp.hpp index 5b32967..ed40687 100644 --- a/src/identiconpp.hpp +++ b/src/identiconpp.hpp @@ -39,6 +39,7 @@ public: enum class algorithm { ltr_symmetric, + ltr_asymmetric, sigil }; @@ -78,7 +79,7 @@ private: const vector _foreground; /*! - * @brief Generate simple identicon. + * @brief Generate ltr_symmetric identicon. * * Use bits 0 to (_columns / 2 + _columns % 2) * _rows, use the * following bits to determine foreground color. Squares are drawn @@ -94,6 +95,23 @@ private: const uint16_t width, const uint16_t height); + /*! + * @brief Generate ltr_asymmetric identicon. + * + * Use bits 0 to _columns * _rows, use the + * following bits to determine foreground color. Squares are drawn + * from left to right, top to bottom. + * + * @param digest The pre-computed digest + * @param width The width of the image + * @param height The height of the image + * + * @return The image + */ + Magick::Image generate_ltr_asymmetric(const string &digest, + const uint16_t width, + const uint16_t height); + /*! * @brief Generate sigil identicon. * diff --git a/src/ltr_asymmetric.cpp b/src/ltr_asymmetric.cpp new file mode 100644 index 0000000..8320971 --- /dev/null +++ b/src/ltr_asymmetric.cpp @@ -0,0 +1,44 @@ +/* This file is part of identiconpp. + * Copyright © 2018 tastytea + * + * 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 . + */ + +#include "identiconpp.hpp" +#include "debug.hpp" + +Magick::Image Identiconpp::generate_ltr_asymmetric(const string &digest, + const uint16_t width, + const uint16_t height) +{ + Magick::Image img(Magick::Geometry(_columns, _rows), + Magick::Color("#" + _background)); + Magick::Color dotcolor = get_color(_columns * _rows + 1, digest); + + for (uint8_t row = 0; row < _rows; ++row) + { + for (uint8_t column = 0; column < _columns; ++column) + { + if (get_bit(row * _columns + column, digest)) + { + ttdebug << "col=" << std::to_string(column) + << ", row=" << std::to_string(row) << '\n'; + img.pixelColor(column, row, dotcolor); + } + } + } + + img.scale(Magick::Geometry(width, height)); + img.magick("png"); + return img; +}