From 36dd2b7e5a92c272adfcb4ba59209ce5cfc36914 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 27 Dec 2018 21:56:50 +0100 Subject: [PATCH] Corrected the "how many bits do i need for n colors"-algorithm. --- CMakeLists.txt | 2 +- README.md | 4 +++- src/checks.cpp | 5 +++-- src/identiconpp.cpp | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bcabf7..cea54e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(identiconpp - VERSION 0.3.1 + VERSION 0.3.2 LANGUAGES CXX ) diff --git a/README.md b/README.md index dc4db88..393eb57 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -**identiconpp** is a library to generate identicons. Written in C++. +**identiconpp** is a library to generate identicons for C++. You get the images as `Magick::Image`. This allows you to make all kinds of modifications. @@ -119,6 +119,7 @@ and you are welcome to redistribute it under certain conditions. * Use bits from digest to determine if a pixel is painted(1) or not(0). * Mirror the pixels vertically. * Use the following bits to pick the foreground color. + * You need `log2(n_colors) + 1` bits. * Scale image proportionally to requested width. ```PLAIN @@ -139,6 +140,7 @@ Implemented in [ltr_symmetric.cpp] * 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. + * * You need `log2(n_colors) + 1` bits. * Scale image proportionally to requested width. ```PLAIN diff --git a/src/checks.cpp b/src/checks.cpp index 29ef100..68a3588 100644 --- a/src/checks.cpp +++ b/src/checks.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "identiconpp.hpp" #include "debug.hpp" @@ -41,14 +42,14 @@ void Identiconpp::check_entropy(const string &digest, algorithm type) // We need bits for each field in half of the columns, +1 column if // they are uneven. Then we need enough bits to pick a color. entropy_required = (_columns / 2 + _columns % 2) * _rows - + (_foreground.size() / 2 + _foreground.size() % 2); + + std::log2(_foreground.size()) + 1; break; } case algorithm::ltr_asymmetric: { entropy_provided = digest.length() * 4; entropy_required = _columns * _rows - + (_foreground.size() / 2 + _foreground.size() % 2); + + std::log2(_foreground.size()) + 1; break; } case algorithm::sigil: diff --git a/src/identiconpp.cpp b/src/identiconpp.cpp index 2e2b426..b692ec9 100644 --- a/src/identiconpp.cpp +++ b/src/identiconpp.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "identiconpp.hpp" #include "debug.hpp" @@ -100,7 +101,7 @@ 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; + const uint16_t colorbits = std::log2(_foreground.size()) + 1; // Extract approximation std::stringstream ss;