Browse Source

C-interface: Added example

tastytea 2 months ago
parent
commit
cef436faf5
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: CFC39497F1B26E07
1 changed files with 104 additions and 0 deletions
  1. 104
    0
      example.c

+ 104
- 0
example.c View File

@@ -0,0 +1,104 @@
1
+/*  This file is part of identiconpp.
2
+ *  Public Domain / CC-0
3
+ *
4
+ *   Compile with gcc --std=c99 $(pkg-config --cflags --libs MagickWand) \
5
+ *   $(pkg-config --cflags --libs openssl) -lidenticonpp
6
+ */
7
+
8
+#include <stdint.h>
9
+#include <string.h>
10
+#include <stdio.h>
11
+#include "identiconpp_c.h"
12
+#include <MagickWand/MagickWand.h>
13
+#include <openssl/evp.h>
14
+#include <openssl/bio.h>
15
+
16
+char *b64decode(char *input, int length)
17
+{
18
+    BIO *b64, *bmem;
19
+
20
+    char *buffer = (char *)malloc(length);
21
+    memset(buffer, 0, length);
22
+
23
+    b64 = BIO_new(BIO_f_base64());
24
+    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
25
+    bmem = BIO_new_mem_buf(input, length);
26
+    bmem = BIO_push(b64, bmem);
27
+
28
+    BIO_read(bmem, buffer, length);
29
+
30
+    BIO_free_all(bmem);
31
+
32
+    return buffer;
33
+}
34
+
35
+int main(int argc, char *argv[])
36
+{
37
+    char digest[65] = "973dfe463ec85785f5f95af5ba3906ee"
38
+                      "db2d931c24e69824a89ea65dba4e813b";
39
+    const char colors[6][9] =
40
+    {
41
+        "800000ff",
42
+        "008000ff",
43
+        "000080ff",
44
+        "808000ff",
45
+        "008080ff",
46
+        "800080ff"
47
+    };
48
+
49
+    if (argc > 1)
50
+    {
51
+        strncpy(digest, argv[1], 64);
52
+    }
53
+
54
+    MagickWand *mw = NULL;
55
+    MagickWandGenesis();
56
+    mw = NewMagickWand();
57
+
58
+    {
59
+        const uint8_t padding[2] = { 20, 20 };
60
+        identiconpp_setup(4, 4, identiconpp_ltr_symmetric,
61
+                          "ffffffff", colors, 6, padding);
62
+        uint64_t len = identiconpp_generate("png", digest, 200);
63
+        char base64[len];
64
+        strcpy(base64, identiconpp_base64());
65
+        MagickReadImageBlob(mw, b64decode(base64, len), len);
66
+        MagickWriteImage(mw, "identicon1.png");
67
+    }
68
+
69
+    {
70
+        const uint8_t padding[2] = { 0, 0 };
71
+        identiconpp_setup(5, 5, identiconpp_sigil,
72
+                          "00000080", colors, 6, padding);
73
+        uint64_t len = identiconpp_generate("png", digest, 200);
74
+        char base64[len];
75
+        strcpy(base64, identiconpp_base64());
76
+        MagickReadImageBlob(mw, b64decode(base64, len), len);
77
+        MagickWriteImage(mw, "identicon2.png");
78
+    }
79
+
80
+    {
81
+        const uint8_t padding[2] = { 0, 0 };
82
+        identiconpp_setup(5, 5, identiconpp_ltr_asymmetric,
83
+                          "000000ff", colors, 6, padding);
84
+        uint64_t len = identiconpp_generate("png", digest, 200);
85
+        char base64[len];
86
+        strcpy(base64, identiconpp_base64());
87
+        MagickReadImageBlob(mw, b64decode(base64, len), len);
88
+        MagickWriteImage(mw, "identicon3.png");
89
+    }
90
+
91
+    {
92
+        const uint8_t padding[2] = { 10, 10 };
93
+        identiconpp_setup(6, 4, identiconpp_ltr_symmetric,
94
+                          "000000c0", colors, 6, padding);
95
+        uint64_t len = identiconpp_generate("png", digest, 200);
96
+        char base64[len];
97
+        strcpy(base64, identiconpp_base64());
98
+        MagickReadImageBlob(mw, b64decode(base64, len), len);
99
+        MagickWriteImage(mw, "identicon4.png");
100
+    }
101
+
102
+    DestroyMagickWand(mw);
103
+    MagickWandTerminus();
104
+}

Loading…
Cancel
Save