From 559eb9633e1f97d70112f02538ae5e9a0e46f4d5 Mon Sep 17 00:00:00 2001 From: Leo Howell Date: Sun, 27 Sep 2009 16:31:03 +0900 Subject: bitmap handling routines --- lpg/libqr/qr-bitmap-pbm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lpg/libqr/qr-bitmap-pbm.c (limited to 'lpg/libqr/qr-bitmap-pbm.c') diff --git a/lpg/libqr/qr-bitmap-pbm.c b/lpg/libqr/qr-bitmap-pbm.c new file mode 100644 index 0000000..3bcddb2 --- /dev/null +++ b/lpg/libqr/qr-bitmap-pbm.c @@ -0,0 +1,47 @@ +#include +#include +#include "qr-bitmap.h" + +int qr_bitmap_write_pbm(const char * path, + const char * comment, + const struct qr_bitmap * bmp) +{ + FILE * out; + size_t count, x, y; + + out = fopen(path, "w"); + if (!out) + return -1; + + count = 0; + + count += fputs("P1\n", out); + + if (comment) + count += fprintf(out, "# %s\n", comment); + + count += fprintf(out, "%u %u\n", + (unsigned)bmp->width, + (unsigned)bmp->height); + + for (y = 0; y < bmp->height; ++y) { + unsigned char * row = bmp->bits + y * bmp->stride; + + for (x = 0; x < bmp->width; ++x) { + int bit = row[x / CHAR_BIT] & (1 << x % CHAR_BIT); + + if (x > 0) + fputc(' ', out); + fputc(bit ? '1' : '0', out); + } + count += fputc('\n', out); + } + + if (ferror(out)) + count = -1; + + fclose(out); + + return count; +} + -- cgit v1.2.3-70-g09d2