Simplified get_bit() and added comments for non-obvios things.
the build was successful Details

get_bit() is probably slightly faster too.
This commit is contained in:
tastytea 2018-12-27 02:25:56 +01:00
parent 491aa98697
commit a162965afe
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 16 additions and 12 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2) cmake_minimum_required (VERSION 3.2)
project(identiconpp project(identiconpp
VERSION 0.2.0 VERSION 0.2.1
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -17,7 +17,6 @@
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include <bitset>
#include "identiconpp.hpp" #include "identiconpp.hpp"
#include "debug.hpp" #include "debug.hpp"
@ -41,6 +40,7 @@ Identiconpp::Identiconpp(const uint8_t rows, const uint8_t columns,
Magick::Image Identiconpp::generate(const string &digest, const uint16_t width) Magick::Image Identiconpp::generate(const string &digest, const uint16_t width)
{ {
ttdebug << "Using digest: " << digest << '\n';
check_entropy(digest, _type); check_entropy(digest, _type);
const uint16_t height = width / _columns * _rows; const uint16_t height = width / _columns * _rows;
ttdebug << "width: " << std::to_string(width) ttdebug << "width: " << std::to_string(width)
@ -64,15 +64,14 @@ bool Identiconpp::get_bit(const uint16_t bit, const string &digest)
std::stringstream ss; std::stringstream ss;
ss << std::hex << digest[bit / 4]; ss << std::hex << digest[bit / 4];
// std::stringstream does not support writing into uint8_t // std::stringstream does not support writing into uint8_t
unsigned short buf; unsigned short nibble;
ss >> buf; ss >> nibble;
std::bitset<4> nibble(buf);
// Shift nibble to the right until the bit we want is on the right border.
if (nibble[3 - bit % 4]) // Then check if it is set.
if (nibble >> (3 - bit % 4) & 1)
{ {
ttdebug << "Bit " << std::to_string(bit + 1) << " is set in " ttdebug << "Bit " << std::to_string(bit + 1) << " is set.\n";
<< digest << ".\n";
ttdebug << nibble << " (" << bit % 4 << ")\n";
return true; return true;
} }
@ -95,12 +94,17 @@ Magick::Color Identiconpp::get_color(const uint16_t firstbit,
{ {
ss << std::hex << digest.substr(firstbit / 4, colorbits / 4 + 1); ss << std::hex << digest.substr(firstbit / 4, colorbits / 4 + 1);
} }
// std::stringstream does not support writing into uint16_t
unsigned short bits; unsigned short bits;
ss >> bits; ss >> bits;
// Get rid of excess bits // Shift an one $colorbits times to the left, substract 1. This leaves us
bits = bits & (1 << colorbits) - 1; // with $colorbits ones. Then AND bits and our ones to keep only as many
// bits as we need.
bits = bits & ((1 << colorbits) - 1);
// We may get a number that is slightly too big if _foreground.size() is not
// a power of 2.
if (bits > (_foreground.size() - 1)) if (bits > (_foreground.size() - 1))
{ {
bits -= (_foreground.size() - 1); bits -= (_foreground.size() - 1);