From 988eaf406c217f23f11837ca573e199eba1cc0f8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 3 Jan 2019 13:27:49 +0100 Subject: [PATCH] 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