summaryrefslogtreecommitdiffstats
path: root/tools/bitmap_to_c.c
blob: 01b925ea9b99270dee7c12b218f17f4016a6fc2e (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <spice/macros.h>


#define TAB "    "

#define ERROR(str) printf("%s: error: %s\n", prog_name, str); exit(-1);

static char *prog_name = NULL;

static size_t read_input(const char *file_name, uint8_t** out_buf)
{
    int fd = open(file_name, O_RDONLY);
    if (fd == -1) {
        ERROR("open source file failed");
        return 0;
    }
    struct stat file_stat;
    if (fstat(fd, &file_stat) == -1) {
        ERROR("fstat on source file failed");
        return 0;
    }

    uint8_t *buf = malloc(file_stat.st_size);

    if (!buf) {
        close(fd);
        ERROR("alloc mem failed");
        return 0;
    }

    size_t to_read = file_stat.st_size;
    uint8_t *buf_pos = buf;
    while (to_read) {
        int n = read(fd, buf_pos, to_read);
        if (n <= 0) {
            ERROR("read from source file failed");
            close(fd);
            free(buf);
            return 0;
        }
        to_read -= n;
        buf_pos += n;
    }
    close(fd);
    *out_buf = buf;
    return file_stat.st_size;
}

typedef struct __attribute__ ((__packed__)) BitmpaHeader {
    uint32_t header_size;
    uint32_t width;
    uint32_t height;
    uint16_t plans;
    uint16_t bpp;
    uint32_t compression;
    uint32_t image_size;
    uint32_t horizontal_resolution;
    uint32_t vertical_resolution;
    uint32_t num_colors;
    uint32_t important_colors;
} BitmpaHeader;

typedef struct __attribute__ ((__packed__)) BMPFileHeader {
    uint16_t magic;
    uint32_t file_size;
    uint32_t reserved;
    uint32_t data_offset;
    BitmpaHeader header;
} BMPFileHeader;

#define BI_RGB 0

typedef struct Pixmap {
    uint32_t width;
    uint32_t height;
    uint32_t stride;
    uint32_t bpp;
    uint8_t* data;
} Pixmap;

static Pixmap *init_bitmap(size_t input_size, uint8_t *buf)
{
    BMPFileHeader *file_header;
    uint8_t *pixels;
    Pixmap *pixmap;
    uint32_t stride;


    if (input_size < sizeof(BMPFileHeader)) {
        ERROR("invalid source file");
        return NULL;
    }

    file_header = (BMPFileHeader *)buf;

    if (file_header->magic != 0x4d42) {
        ERROR("bad bitmap magic");
        return NULL;
    }

    if (file_header->file_size != input_size) {
        ERROR("invalid source file");
        return NULL;
    }

    if (file_header->header.header_size != 40 || file_header->header.plans != 1 ||
                                                        file_header->header.compression != BI_RGB ||
                                                        !file_header->header.width ||
                                                        !file_header->header.height) {
        ERROR("invalid bitmap header");
        return NULL;
    }

    if (file_header->header.bpp == 32) {
        stride = file_header->header.width * sizeof(uint32_t);
    } else if (file_header->header.bpp == 24) {
        stride = SPICE_ALIGN(file_header->header.width * 3, 4);
    } else {
        ERROR("unsupported bpp");
        return NULL;
    }

    if (file_header->header.height * stride > file_header->header.image_size) {
        ERROR("image size is to small");
        return NULL;
    }
    pixels = buf + file_header->data_offset;
    if (pixels < (uint8_t *)(file_header + 1) ||
                                       pixels + file_header->header.image_size > buf + input_size) {
        ERROR("bad data offset");
        return NULL;
    }

    if (!(pixmap = (Pixmap *)malloc(sizeof(*pixmap)))) {
        ERROR("alloc mem failed");
        return NULL;
    }

    pixmap->width = file_header->header.width;
    pixmap->height = file_header->header.height;
    pixmap->stride = stride;
    pixmap->bpp = file_header->header.bpp;
    pixmap->data = pixels;
    return pixmap;
}

static inline void put_char(FILE* f, uint8_t val)
{
    fprintf(f, "0x%.2x,", val);
}

static void do_dump_with_alpha_conversion(const Pixmap *pixmap, FILE *f)
{
    uint8_t *line = (uint8_t *)(pixmap->data + ((pixmap->height - 1) * pixmap->stride));
    int line_size = 0;
    int i, j;

    for (i = 0; i < pixmap->height; i++) {
        uint8_t *now = line;
        for (j = 0; j < pixmap->width; j++, now += 4) {
            if ((line_size++ % 4) == 0) {
                fprintf(f, "\n" TAB);
            }
            double alpha = (double)now[3] / 0xff;
            put_char(f, alpha * now[0]);
            put_char(f, alpha * now[1]);
            put_char(f, alpha * now[2]);
            put_char(f, now[3]);
        }
        line -= pixmap->stride;
    }
}

static void do_dump_32bpp(const Pixmap *pixmap, FILE *f)
{
    uint8_t *line = (uint8_t *)(pixmap->data + ((pixmap->height - 1) * pixmap->stride));
    int line_size = 0;
    int i, j;

    for (i = 0; i < pixmap->height; i++) {
        uint8_t *now = line;
        for (j = 0; j < pixmap->width; j++, now += 4) {
            if ((line_size++ % 4) == 0) {
                fprintf(f, "\n" TAB);
            }
            put_char(f, now[0]);
            put_char(f, now[1]);
            put_char(f, now[2]);
            put_char(f, now[3]);
        }
        line -= pixmap->stride;
    }
}

static void do_dump_24bpp(const Pixmap *pixmap, FILE *f)
{
    uint8_t *line = (uint8_t *)(pixmap->data + ((pixmap->height - 1) * pixmap->stride));
    int line_size = 0;
    int i, j;

    for (i = 0; i < pixmap->height; i++) {
        uint8_t *now = line;
        for (j = 0; j < pixmap->width; j++, now += 3) {
            if ((line_size++ % 4) == 0) {
                fprintf(f, "\n" TAB);
            }
            put_char(f, now[0]);
            put_char(f, now[1]);
            put_char(f, now[2]);
            put_char(f, 0);
        }
        line -= pixmap->stride;
    }
}

static int pixmap_to_c_struct(const Pixmap *pixmap, const char *dest_file, const char *image_name,
                              int alpha_convertion)
{
    int fd;
    FILE *f;

    if ((fd = open(dest_file, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
        ERROR("open dest file failed");
        return -1;
    }

    if (!(f = fdopen(fd, "w"))) {
        ERROR("fdopen failed");
        close(fd);
        return -1;
    }

    uint32_t data_size = pixmap->width * sizeof(uint32_t) * pixmap->height;
    fprintf(f, "static const struct {\n"
               TAB "uint32_t width;\n"
               TAB "uint32_t height;\n"
               TAB "uint8_t pixel_data[%u];\n"
               "} %s = { %u, %u, {",
            data_size, image_name, pixmap->width, pixmap->height);

    if (alpha_convertion) {
        if (pixmap->bpp != 32) {
            ERROR("32 bpp is requred for alpha option")
        }
        do_dump_with_alpha_conversion(pixmap, f);
    } else if (pixmap->bpp == 32) {
        do_dump_32bpp(pixmap, f);
    } else {
        do_dump_24bpp(pixmap, f);
    }

    fseek(f, -1, SEEK_CUR);
    fprintf(f, "}\n};\n");
    fclose(f);
    close(fd);
    return 0;
}

enum {
    ALPHA_OPT = 'a',
    HELP_OPT = 'h',
    NAME_OPT = 'n',
};

static void usage()
{
    printf("usage: %s [--alpha] [--name <struct name>] SOURCE [DEST]\n", prog_name);
    printf("       %s --help\n", prog_name);
}

const struct option longopts[] = {
    {"help", no_argument, NULL, HELP_OPT},
    {"alpha", no_argument, NULL, ALPHA_OPT},
    {"name", required_argument, NULL, NAME_OPT},
    {NULL, 0, NULL, 0},
};

int main(int argc, char **argv)
{
    size_t input_size;
    uint8_t* buf;
    Pixmap *pixmap;
    int opt;
    int alpha_convertion = FALSE;
    char *struct_name = NULL;
    char *src = NULL;
    char *dest = NULL;


    if (!(prog_name = strrchr(argv[0], '/'))) {
        prog_name = argv[0];
    } else {
        prog_name++;
    }


    while ((opt = getopt_long(argc, argv, "ah", longopts, NULL)) != -1) {
        switch (opt) {
        case 0:
        case '?':
            usage();
            exit(-1);
        case ALPHA_OPT:
            alpha_convertion = TRUE;
            break;
        case HELP_OPT:
            usage();
            exit(0);
        case NAME_OPT:
            struct_name = optarg;
            break;
        }
    }

    int more = argc - optind;
    switch (more) {
    case 1: {
        char *slash;
        char *dot;

        dest = malloc(strlen(argv[optind]) + 3);
        strcpy(dest, argv[optind]);
        dot = strrchr(dest, '.');
        slash = strrchr(dest, '/');
        if (!dot || (slash && slash > dot)) {
            strcat(dest, ".c");
        } else {
            strcpy(dot, ".c");
        }
        break;
    }
    case 2:
        dest = argv[optind + 1];
        //todo: if dir strcat src name
        break;
    default:
        usage();
        exit(-1);
    }

    src = argv[optind];

    if (!struct_name) {
        char *struct_name_src;
        char *dot;

        struct_name_src = strrchr(dest, '/');
        if (!struct_name_src) {
            struct_name_src = dest;
        } else {
            ++struct_name_src;
        }
        struct_name = malloc(strlen(struct_name_src) + 1);
        strcpy(struct_name, struct_name_src);
        if ((dot = strchr(struct_name, '.'))) {
            *dot = 0;
        }
    }

    if (!(input_size = read_input(src, &buf))) {
        return -1;
    }

    if (!(pixmap = init_bitmap(input_size, buf))) {
        return -1;
    }
    return pixmap_to_c_struct(pixmap, dest, struct_name, alpha_convertion);
}