diff options
author | Christian Hesse <mail@eworm.de> | 2013-08-12 10:55:39 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2013-08-12 10:55:39 +0200 |
commit | 5529f56ffd79c37b74e86c26127bdbc13bc1d005 (patch) | |
tree | 1aad8619460dc274a88d2b1b876a083338c56906 | |
parent | 5aa9d8c3da630755519abc7c98570f23a98737fc (diff) | |
download | cqrlogo-5529f56ffd79c37b74e86c26127bdbc13bc1d005.tar.gz cqrlogo-5529f56ffd79c37b74e86c26127bdbc13bc1d005.tar.zst |
rework handling of PNG file text information
-rw-r--r-- | config.def.h | 9 | ||||
-rw-r--r-- | cqrlogo.c | 56 |
2 files changed, 42 insertions, 23 deletions
diff --git a/config.def.h b/config.def.h index fa26683..cdd9b86 100644 --- a/config.def.h +++ b/config.def.h @@ -20,6 +20,9 @@ * a numeric value from 0 to 3 */ /* if you really, really, really want to save some bytes... - * it is possible to disable text information in PNG file, just uncomment - * below to undefine PNG_TEXT_SUPPORTED */ -/* #undef PNG_TEXT_SUPPORTED */ + * It is possible to disable text information in PNG file completly, though + * nobody will have an idea where you got this great software... + * So please do not. */ +#define PNG_ENABLE_TEXT 1 +/* do you want version information within the PNG file? */ +#define PNG_ENABLE_TEXT_VERSIONS 1 @@ -27,6 +27,20 @@ struct bitmap_t { uint8_t *pixel; }; +#if defined PNG_TEXT_SUPPORTED && PNG_ENABLE_TEXT +/*** add_png_text ***/ +png_text * add_png_text(png_text *pngtext, unsigned int *textcount, char *key, char *text) { + pngtext = realloc(pngtext, ((*textcount) + 1) * sizeof(png_text)); + + pngtext[*textcount].compression = PNG_TEXT_COMPRESSION_zTXt; + pngtext[*textcount].key = key; + pngtext[*textcount].text = text; + + (*textcount)++; + return pngtext; +} +#endif + /*** generate_png ***/ int generate_png (struct bitmap_t *bitmap, const char *uri) { png_structp png_ptr = NULL; @@ -49,28 +63,30 @@ int generate_png (struct bitmap_t *bitmap, const char *uri) { png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); -#ifdef PNG_TEXT_SUPPORTED -#define VERSIONSTR VERSION " (" __DATE__ ", " __TIME__ ", libpng %s, zlib %s)" - png_text text[3]; - char *version; - - version = malloc(sizeof(VERSIONSTR) + strlen(png_libpng_ver) + strlen(zlib_version)); - sprintf(version, VERSIONSTR, png_libpng_ver, zlib_version); - - text[0].compression = PNG_TEXT_COMPRESSION_zTXt; - text[0].key = "comment"; - text[0].text = "QR-Code created by cqrlogo - https://github.com/eworm-de/cqrlogo"; +#if defined PNG_TEXT_SUPPORTED && PNG_ENABLE_TEXT + unsigned int textcount = 0; + png_text *pngtext = NULL; - text[1].compression = PNG_TEXT_COMPRESSION_zTXt; - text[1].key = "version"; - text[1].text = version; + pngtext = add_png_text(pngtext, &textcount, "comment", "QR-Code created by cqrlogo - https://github.com/eworm-de/cqrlogo"); + pngtext = add_png_text(pngtext, &textcount, "referer", (char *)uri); - text[2].compression = PNG_TEXT_COMPRESSION_zTXt; - text[2].key = "referer"; - text[2].text = (char*)uri; - - png_set_text(png_ptr, info_ptr, text, 3); - free(version); +# if PNG_ENABLE_TEXT_VERSIONS +# define VERSIONSTR VERSION " (" __DATE__ ", " __TIME__ ")" +# define LIBSSTR "libqrencode %s, libpng %s, zlib %s" + char *libsstr, *qrver = QRcode_APIVersionString(); + + libsstr = malloc(sizeof(LIBSSTR) + strlen(qrver) + strlen(png_libpng_ver) + strlen(zlib_version)); + sprintf(libsstr, LIBSSTR, qrver, png_libpng_ver, zlib_version); + + pngtext = add_png_text(pngtext, &textcount, "version", VERSIONSTR); + pngtext = add_png_text(pngtext, &textcount, "libs", libsstr); +# endif + + png_set_text(png_ptr, info_ptr, pngtext, textcount); + free(pngtext); +# if PNG_ENABLE_TEXT_VERSIONS + free(libsstr); +# endif #endif row_pointers = png_malloc (png_ptr, bitmap->height * sizeof (png_byte *)); |