FreeRDP
Loading...
Searching...
No Matches
img2bgra.c
1
20#include <winpr/image.h>
21#include <stdio.h>
22
23static void usage(const char* name)
24{
25 (void)printf("%s <source file> <destination file>\n\n", name);
26 (void)printf("\tRead any image format supported by WinPR::wImage\n");
27 (void)printf("\tand convert it to raw BGRA data.\n\n");
28 (void)printf("\toutput format is a C header with an array ready to be included\n");
29}
30
31static int dump_data_hex(FILE* fp, const uint8_t* data, size_t size)
32{
33 if (size > 0)
34 {
35 const int rc = fprintf(fp, "0x%02" PRIx8, data[0]);
36 if (rc != 4)
37 return -1;
38 }
39
40 for (size_t x = 1; x < size; x++)
41 {
42 if (x % 16 == 0)
43 {
44 const int rc = fprintf(fp, ",\n");
45 if (rc != 2)
46 return -2;
47 }
48 else
49 {
50 const int rc = fprintf(fp, ",");
51 if (rc != 1)
52 return -2;
53 }
54
55 const int rc = fprintf(fp, "0x%02" PRIx8, data[x]);
56 if (rc != 4)
57 return -2;
58 }
59 return 0;
60}
61
62static int dump_data(const wImage* img, const char* file)
63{
64 FILE* fp = fopen(file, "w");
65 if (!fp)
66 {
67 (void)fprintf(stderr, "Failed to open file '%s'\n", file);
68 return -1;
69 }
70
71 int rc = -2;
72 int count = fprintf(fp, "#pragma once\n");
73 if (count < 0)
74 goto fail;
75 count = fprintf(fp, "\n");
76 if (count < 0)
77 goto fail;
78 count = fprintf(fp, "#include <stdint.h>\n");
79 if (count < 0)
80 goto fail;
81 count = fprintf(fp, "\n");
82 if (count < 0)
83 goto fail;
84 count = fprintf(fp, "static const uint8_t img_data[] ={\n");
85 if (count < 0)
86 goto fail;
87 count = dump_data_hex(fp, img->data, 1ULL * img->height * img->scanline);
88 if (count < 0)
89 goto fail;
90 count = fprintf(fp, "};\n");
91 if (count < 0)
92 goto fail;
93 rc = 0;
94fail:
95 fclose(fp);
96 return rc;
97}
98
99int main(int argc, char* argv[])
100{
101 if (argc != 3)
102 {
103 usage(argv[0]);
104 return -1;
105 }
106
107 int rc = -1;
108 const char* src = argv[1];
109 const char* dst = argv[2];
110 wImage* img = winpr_image_new();
111 if (!img)
112 goto fail;
113
114 const int res = winpr_image_read(img, src);
115 if (res <= 0)
116 {
117 (void)fprintf(stderr, "Failed to read image file '%s'\n", src);
118 goto fail;
119 }
120
121 rc = dump_data(img, dst);
122
123 if (rc >= 0)
124 (void)printf("Converted '%s' to header '%s'\n", src, dst);
125fail:
126 if (rc != 0)
127 usage(argv[0]);
128 winpr_image_free(img, TRUE);
129 return rc;
130}