From 988eaf406c217f23f11837ca573e199eba1cc0f8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 3 Jan 2019 13:27:49 +0100 Subject: [PATCH 1/5] Started C interface. --- src/c_interface.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/identiconpp_c.h | 49 +++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/c_interface.cpp create mode 100644 src/identiconpp_c.h diff --git a/src/c_interface.cpp b/src/c_interface.cpp new file mode 100644 index 0000000..174782d --- /dev/null +++ b/src/c_interface.cpp @@ -0,0 +1,82 @@ +/* This file is part of identiconpp. + * Copyright © 2019 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 +#include +#include +#include +#include +#include "identiconpp.hpp" +#include "debug.hpp" +#include "identiconpp_c.h" + +std::unique_ptr identicon; + +bool identiconpp_setup(const uint8_t columns, const uint8_t rows, + identiconpp_algorithm type, + const char background[9], + const char foreground[][9], + const uint8_t foreground_len, + const uint8_t padding[2]) +{ + Identiconpp::algorithm algo; + switch (type) + { + case identiconpp_ltr_symmetric: + { + algo = Identiconpp::algorithm::ltr_symmetric; + break; + } + case identiconpp_ltr_asymmetric: + { + algo = Identiconpp::algorithm::ltr_asymmetric; + break; + } + case identiconpp_sigil: + { + algo = Identiconpp::algorithm::sigil; + break; + } + } + + std::vector vforeground; + for (uint8_t i = 0; i < foreground_len; ++i) + { + vforeground.push_back(foreground[i]); + } + + try + { + identicon = std::make_unique( + Identiconpp(columns, rows, algo, background, vforeground, + { padding[0], padding[1] })); + } + catch (const std::exception &e) + { + return false; + } + + return true; +} + +bool identiconpp_generate(MagickWand *wand, + const char digest[], const uint16_t width) +{ + Magick::Image img = identicon->generate(digest, width); + Magick::Blob blob; + img.write(&blob); + MagickReadImageBlob(wand, blob.data(), blob.length()); +} diff --git a/src/identiconpp_c.h b/src/identiconpp_c.h new file mode 100644 index 0000000..eef1e1f --- /dev/null +++ b/src/identiconpp_c.h @@ -0,0 +1,49 @@ +/* This file is part of identiconpp. + * Copyright © 2019 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 . + */ + +#ifndef IDENTICONPP_H +#define IDENTICONPP_H + +#ifdef __cplusplus +extern "C" +{ +#endif + + #include + #include + #include + #include + + typedef enum + { + identiconpp_ltr_symmetric, + identiconpp_ltr_asymmetric, + identiconpp_sigil + } identiconpp_algorithm; + + bool identiconpp_setup(const uint8_t columns, const uint8_t rows, + identiconpp_algorithm type, + const char background[9], + const char foreground[][9], + const uint8_t foreground_len, + const uint8_t padding[2]); + bool identiconpp_generate(MagickWand *wand, + const char digest[], const uint16_t width); + +#ifdef __cplusplus +} +#endif +#endif // IDENTICONPP_H From 7d89f416e889327f8fdc64ca8e9eb3301edb2924 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 4 Jan 2019 17:36:10 +0100 Subject: [PATCH 2/5] C-interface: return images as base64. I couldn't figure out how to return them as MagickWand, maybe I can't mix Magick++ with MagickWand. --- src/c_interface.cpp | 17 +++++++++++++---- src/identiconpp_c.h | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/c_interface.cpp b/src/c_interface.cpp index 174782d..e25a9f4 100644 --- a/src/c_interface.cpp +++ b/src/c_interface.cpp @@ -23,7 +23,8 @@ #include "debug.hpp" #include "identiconpp_c.h" -std::unique_ptr identicon; +static std::unique_ptr identicon; +static string base64; bool identiconpp_setup(const uint8_t columns, const uint8_t rows, identiconpp_algorithm type, @@ -72,11 +73,19 @@ bool identiconpp_setup(const uint8_t columns, const uint8_t rows, return true; } -bool identiconpp_generate(MagickWand *wand, - const char digest[], const uint16_t width) +uint64_t identiconpp_generate(const char magick[], + const char digest[], const uint16_t width) { Magick::Image img = identicon->generate(digest, width); Magick::Blob blob; + img.magick(magick); img.write(&blob); - MagickReadImageBlob(wand, blob.data(), blob.length()); + base64 = blob.base64(); + + return blob.base64().length(); +} + +const char *identiconpp_base64() +{ + return base64.c_str(); } diff --git a/src/identiconpp_c.h b/src/identiconpp_c.h index eef1e1f..dec0f5f 100644 --- a/src/identiconpp_c.h +++ b/src/identiconpp_c.h @@ -25,8 +25,18 @@ extern "C" #include #include #include - #include + /*! + * @example example.c + */ + + /*! + * @brief C interface for identiconpp. + */ + + /*! + * @brief List of identicon algorithms + */ typedef enum { identiconpp_ltr_symmetric, @@ -34,14 +44,38 @@ extern "C" identiconpp_sigil } identiconpp_algorithm; + /*! + * @brief Setup identicon parameters. + * + * @param columns Number of columns + * @param rows Number of rows + * @param type The algorithm to use + * @param background Background color, hexadecimal, rrggbbaa + * @param foreground Array of foreground colors + * @param foreground_len Length of the array of foreground colors + * @param padding Padding in pixels { left & right, top & down } + * + * @return { description_of_the_return_value } + */ bool identiconpp_setup(const uint8_t columns, const uint8_t rows, identiconpp_algorithm type, const char background[9], const char foreground[][9], const uint8_t foreground_len, const uint8_t padding[2]); - bool identiconpp_generate(MagickWand *wand, - const char digest[], const uint16_t width); + + /*! + * @brief Generates identicon from digest. + * + * @param magick See http://imagemagick.org/script/formats.php + * @param digest The pre-computed digest + * @param width The width of the identicon + * + * @return Length of the generated base64-string + */ + uint64_t identiconpp_generate(const char magick[], + const char digest[], const uint16_t width); + const char *identiconpp_base64(); #ifdef __cplusplus } From cef436faf54abe7c8817d137712ecec7fb6b7ba9 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 4 Jan 2019 19:00:16 +0100 Subject: [PATCH 3/5] C-interface: Added example --- example.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 example.c diff --git a/example.c b/example.c new file mode 100644 index 0000000..8eb7297 --- /dev/null +++ b/example.c @@ -0,0 +1,104 @@ +/* This file is part of identiconpp. + * Public Domain / CC-0 + * + * Compile with gcc --std=c99 $(pkg-config --cflags --libs MagickWand) \ + * $(pkg-config --cflags --libs openssl) -lidenticonpp + */ + +#include +#include +#include +#include "identiconpp_c.h" +#include +#include +#include + +char *b64decode(char *input, int length) +{ + BIO *b64, *bmem; + + char *buffer = (char *)malloc(length); + memset(buffer, 0, length); + + b64 = BIO_new(BIO_f_base64()); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + bmem = BIO_new_mem_buf(input, length); + bmem = BIO_push(b64, bmem); + + BIO_read(bmem, buffer, length); + + BIO_free_all(bmem); + + return buffer; +} + +int main(int argc, char *argv[]) +{ + char digest[65] = "973dfe463ec85785f5f95af5ba3906ee" + "db2d931c24e69824a89ea65dba4e813b"; + const char colors[6][9] = + { + "800000ff", + "008000ff", + "000080ff", + "808000ff", + "008080ff", + "800080ff" + }; + + if (argc > 1) + { + strncpy(digest, argv[1], 64); + } + + MagickWand *mw = NULL; + MagickWandGenesis(); + mw = NewMagickWand(); + + { + const uint8_t padding[2] = { 20, 20 }; + identiconpp_setup(4, 4, identiconpp_ltr_symmetric, + "ffffffff", colors, 6, padding); + uint64_t len = identiconpp_generate("png", digest, 200); + char base64[len]; + strcpy(base64, identiconpp_base64()); + MagickReadImageBlob(mw, b64decode(base64, len), len); + MagickWriteImage(mw, "identicon1.png"); + } + + { + const uint8_t padding[2] = { 0, 0 }; + identiconpp_setup(5, 5, identiconpp_sigil, + "00000080", colors, 6, padding); + uint64_t len = identiconpp_generate("png", digest, 200); + char base64[len]; + strcpy(base64, identiconpp_base64()); + MagickReadImageBlob(mw, b64decode(base64, len), len); + MagickWriteImage(mw, "identicon2.png"); + } + + { + const uint8_t padding[2] = { 0, 0 }; + identiconpp_setup(5, 5, identiconpp_ltr_asymmetric, + "000000ff", colors, 6, padding); + uint64_t len = identiconpp_generate("png", digest, 200); + char base64[len]; + strcpy(base64, identiconpp_base64()); + MagickReadImageBlob(mw, b64decode(base64, len), len); + MagickWriteImage(mw, "identicon3.png"); + } + + { + const uint8_t padding[2] = { 10, 10 }; + identiconpp_setup(6, 4, identiconpp_ltr_symmetric, + "000000c0", colors, 6, padding); + uint64_t len = identiconpp_generate("png", digest, 200); + char base64[len]; + strcpy(base64, identiconpp_base64()); + MagickReadImageBlob(mw, b64decode(base64, len), len); + MagickWriteImage(mw, "identicon4.png"); + } + + DestroyMagickWand(mw); + MagickWandTerminus(); +} From 28d08342d3f535113b701e61830ea93f30c7cf9d Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 4 Jan 2019 19:10:01 +0100 Subject: [PATCH 4/5] C-interface: Added more error checking. --- src/c_interface.cpp | 20 +++++++++++++------- src/identiconpp_c.h | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/c_interface.cpp b/src/c_interface.cpp index e25a9f4..d904ee8 100644 --- a/src/c_interface.cpp +++ b/src/c_interface.cpp @@ -76,13 +76,19 @@ bool identiconpp_setup(const uint8_t columns, const uint8_t rows, uint64_t identiconpp_generate(const char magick[], const char digest[], const uint16_t width) { - Magick::Image img = identicon->generate(digest, width); - Magick::Blob blob; - img.magick(magick); - img.write(&blob); - base64 = blob.base64(); - - return blob.base64().length(); + try + { + Magick::Image img = identicon->generate(digest, width); + Magick::Blob blob; + img.magick(magick); + img.write(&blob); + base64 = blob.base64(); + return blob.base64().length(); + } + catch (const std::exception &e) + { + return 0; + } } const char *identiconpp_base64() diff --git a/src/identiconpp_c.h b/src/identiconpp_c.h index dec0f5f..065d38b 100644 --- a/src/identiconpp_c.h +++ b/src/identiconpp_c.h @@ -55,7 +55,7 @@ extern "C" * @param foreground_len Length of the array of foreground colors * @param padding Padding in pixels { left & right, top & down } * - * @return { description_of_the_return_value } + * @return false on error. */ bool identiconpp_setup(const uint8_t columns, const uint8_t rows, identiconpp_algorithm type, @@ -71,7 +71,7 @@ extern "C" * @param digest The pre-computed digest * @param width The width of the identicon * - * @return Length of the generated base64-string + * @return Length of the generated base64-string, or 0 on error. */ uint64_t identiconpp_generate(const char magick[], const char digest[], const uint16_t width); From 7cd528bea1c183c7b19d85355fb26883ef55e371 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 4 Jan 2019 19:10:59 +0100 Subject: [PATCH 5/5] Added C-header to install recipe. --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 087e269..5ca3086 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project(identiconpp - VERSION 0.3.7 + VERSION 0.4.0 LANGUAGES CXX ) @@ -38,6 +38,7 @@ target_link_libraries(example ${CMAKE_PROJECT_NAME}) install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES src/${CMAKE_PROJECT_NAME}.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(FILES src/${CMAKE_PROJECT_NAME}_c.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) include(packages.CMakeLists.txt)