Merge branch 'c-interface'
This commit is contained in:
commit
da2b64b159
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.2)
|
cmake_minimum_required (VERSION 3.2)
|
||||||
project(identiconpp
|
project(identiconpp
|
||||||
VERSION 0.3.7
|
VERSION 0.4.0
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ target_link_libraries(example ${CMAKE_PROJECT_NAME})
|
||||||
|
|
||||||
install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
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}.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})
|
install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
||||||
|
|
||||||
include(packages.CMakeLists.txt)
|
include(packages.CMakeLists.txt)
|
||||||
|
|
|
@ -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 <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "identiconpp_c.h"
|
||||||
|
#include <MagickWand/MagickWand.h>
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
#include <openssl/bio.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
|
@ -0,0 +1,97 @@
|
||||||
|
/* This file is part of identiconpp.
|
||||||
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include <exception>
|
||||||
|
#include <Magick++/Image.h>
|
||||||
|
#include <Magick++/Blob.h>
|
||||||
|
#include "identiconpp.hpp"
|
||||||
|
#include "debug.hpp"
|
||||||
|
#include "identiconpp_c.h"
|
||||||
|
|
||||||
|
static std::unique_ptr<Identiconpp> identicon;
|
||||||
|
static string base64;
|
||||||
|
|
||||||
|
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<string> vforeground;
|
||||||
|
for (uint8_t i = 0; i < foreground_len; ++i)
|
||||||
|
{
|
||||||
|
vforeground.push_back(foreground[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
identicon = std::make_unique<Identiconpp>(
|
||||||
|
Identiconpp(columns, rows, algo, background, vforeground,
|
||||||
|
{ padding[0], padding[1] }));
|
||||||
|
}
|
||||||
|
catch (const std::exception &e)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t identiconpp_generate(const char magick[],
|
||||||
|
const char digest[], const uint16_t width)
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
return base64.c_str();
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
/* This file is part of identiconpp.
|
||||||
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef IDENTICONPP_H
|
||||||
|
#define IDENTICONPP_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @example example.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief C interface for identiconpp.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief List of identicon algorithms
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
identiconpp_ltr_symmetric,
|
||||||
|
identiconpp_ltr_asymmetric,
|
||||||
|
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 false on error.
|
||||||
|
*/
|
||||||
|
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]);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @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, or 0 on error.
|
||||||
|
*/
|
||||||
|
uint64_t identiconpp_generate(const char magick[],
|
||||||
|
const char digest[], const uint16_t width);
|
||||||
|
const char *identiconpp_base64();
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // IDENTICONPP_H
|
Loading…
Reference in New Issue