aboutsummaryrefslogtreecommitdiff
path: root/lpg/libqr/qr-bitmap-pbm.c
blob: 3bcddb2f312f0b7c96e2c57b5b69c028560dfcc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <limits.h>
#include <stdio.h>
#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;
}