identiconpp is a library to generate identicons for C++ and C.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example.c 2.9KB

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