1 /**
2 * File name: rkpng2c.c
3 * Project: Redkite (A small GUI toolkit)
4 *
5 * Copyright (C) 2019 Iurie Nistor <http://geontime.com>
6 *
7 * This file is part of Redkite.
8 *
9 * Redkite is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /**
25 * rkpng2c is a part of Redkite GUI toolkit.
26 * It converts a PNG image to C array encoded in ARGB32 format.
27 */
28
29 #define RK_VERSION_STR "1.0.0"
30
31 #include <stdio.h>
32 #ifdef __DragonFly__
33 #include <stdlib.h>
34 #include <string.h>
35 #endif
36 #include <libgen.h>
37 #include <cairo/cairo.h>
38
main(int argc,char ** argv)39 int main(int argc , char **argv)
40 {
41 if (argc != 4) {
42 printf("Redkite GUI toolkit\n");
43 printf("rkpng2c version %s\n", RK_VERSION_STR);
44 printf("Converts a PNG to C array encoded in ARGB32\n");
45 printf("Copyright (C) 2019 Iurie Nistor <iur.nistor@gmail.com>\n");
46 printf("License GPLv2\n");
47 printf("Usage: rkpng2c <PNG file> <C or C++ file> <array name>\n");
48 return 0;
49 }
50
51 cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]);
52 int w = cairo_image_surface_get_width(image);
53 int h = cairo_image_surface_get_height(image);
54 printf("image size: %dx%d\n", w, h);
55
56 FILE *fptr;
57 fptr = fopen(argv[2], "wb");
58 if (fptr == NULL) {
59 printf("can't open file %s\n", argv[2]);
60 return 1;
61 }
62
63 const unsigned char *buff = cairo_image_surface_get_data(image);
64 #ifdef __DragonFly__
65 char *b1 = strdup(basename(argv[1]));
66 char *b3 = strdup(basename(argv[3]));
67 #endif
68 fprintf(fptr, "/**\n"
69 " * Generated with rkpng2c version %s, part of Redkite GUI toolkit.\n"
70 " * File name: %s\n"
71 " * Image size: %dx%d\n"
72 " */\n"
73 "\n"
74 #ifdef __DragonFly__
75 "const unsigned char %s[] = {\n", RK_VERSION_STR, b1, w, h, b3);
76 free(b1);
77 free(b3);
78 #else
79 "const unsigned char %s[] = {\n", RK_VERSION_STR, basename(argv[1]), w, h, basename(argv[3]));
80 #endif
81 for (int i = 0; i < w * h * 4; i++) {
82 if ((i + 1) == 12 || (i + 1) % 12 == 0)
83 fprintf(fptr, "0x%02x,\n", buff[i]);
84 else
85 fprintf(fptr, "0x%02x, ", buff[i]);
86 }
87 fprintf(fptr, "};\n");
88
89 fclose(fptr);
90 cairo_surface_destroy(image);
91 return 0;
92 }
93