Debian Codegraph @

support/bmp2c/bmp2c.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // bmp2c.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
•5 #include <string.h>
6
7 #include <iostream>
8 #include <fstream>
9
10 using namespace std;
11
12 #pragma pack(push, 1)
13
14 struct BMPHEAD
15 {
16 char id[2];
17 int size;
18 int reserved;
19 int headersize;
20 int infosize;
21 int width;
22 int depth;
23 short biplane;
24 short bits;
25 int comp;
26 int imagesize;
27 int xpelperm;
28 int ypelperm;
29 int clrused;
30 int clrimport;
31 };
32
33 #pragma pack(pop)
34
35 static short
36 gfx_readportableshort(istream &is)
37 {
38 unsigned char c[2];
39 short result;
40
41 // This is one of my top-ten stupid conversions.
42 // If MSVC weren't such crap, I could just use char * everywhere.
43 is.read((char *) c, 2);
44
45 // By reading a byte at a time we can re-assemble the short
46 // without worrying about the native format as arithimatic is the
47 // same.
48 result = c[0];
49 result |= c[1] << 8;
50
51 return result;
52 }
53
54 static int
55 gfx_readportableint(istream &is)
56 {
57 unsigned short s1, s2;
58 int result;
59
60 s1 = (unsigned short) gfx_readportableshort(is);
61 s2 = (unsigned short) gfx_readportableshort(is);
62
63 result = s1;
64 result |= s2 << 16;
65
66 return result;
67 }
68
69 unsigned short *
70 loadbitmap(const char *name, int &size)
71 {
72 ifstream is;
73 BMPHEAD head;
74 int i, x, y;
75
76 is.open(name, ios_base::in | ios_base::binary);
77
78 if (!is)
79 {
80 cerr << "Failure to open: " << name << endl;
81 return 0;
82 }
83
84 is.read(head.id, 2);
85
86 head.size = gfx_readportableint(is);
87 head.reserved = gfx_readportableint(is);
88 head.headersize = gfx_readportableint(is);
89 head.infosize = gfx_readportableint(is);
90 head.width = gfx_readportableint(is);
91 head.depth = gfx_readportableint(is);
92 head.biplane = gfx_readportableshort(is);
93 head.bits = gfx_readportableshort(is);
94 head.comp = gfx_readportableint(is);
95 head.imagesize = gfx_readportableint(is);
96 head.xpelperm = gfx_readportableint(is);
97 head.ypelperm = gfx_readportableint(is);
98 head.clrused = gfx_readportableint(is);
99 head.clrimport = gfx_readportableint(is);
100
101 if (head.bits != 24)
102 {
103 cerr << "Head.bits is " << head.bits << endl;
104 cerr << "Can only support 24 bit colour bitmaps." << endl;
105 cerr << "If this is 6144, maybe endianness problem?" << endl;
106 return 0;
107 }
108
109 unsigned short *result;
110
111 size = head.imagesize / 3;
112 result = new unsigned short [size];
113
114 // BMPs are stored bottom up, so we reverse it here.
115 for (y = head.depth - 1; y >= 0; y--)
116 {
117 for (x = 0; x < head.width; x++)
118 {
119 unsigned char red, blue, green;
120
121 // BMPs are stored BGR.
122 is.read((char *) &blue, 1);
123 is.read((char *) &green, 1);
124 is.read((char *) &red, 1);
125
126 // Convert to 15 bit BGR.
127
128 // Convert all to 5 bits from 8 bits.
129 red >>= 3;
130 green >>= 3;
131 blue >>= 3;
132
133 // Account for endian issues & repack
134 i = x + y * head.width;
135 //result[i] = (((blue << 2) | (green >> 3)) << 8) |
136 // (((green << 5) | red));
137 result[i] = blue;
138 result[i] <<= 5;
139 result[i] |= green;
140 result[i] <<= 5;
141 result[i] |= red;
142
143 // Swap endianness.
144 // Because we save this as 16 bit text we don't
145 // have to correct for the machine's endianess. I hope.
146 //result[i] = (result[i] << 8) | (result[i] >> 8);
147 }
148 }
149
150 return result;
151 }
152
153 int main(int argc, char *argv[])
154 {
155 cout << "bmp2c: Version 001" << endl;
156
157 if (argc < 2)
158 {
159 cerr << "No given source file, aborting." << endl;
160 return -1;
161 }
162
163 unsigned short *result;
164 int size;
165 int i;
166
167 result = loadbitmap(argv[1], size);
168
169 if (!result)
170 return -1;
171
172 char outputname[500];
173 char varname[500];
174
175 sprintf(outputname, "%s.c", argv[1]);
176
177 ofstream os(outputname);
178
179 if (!os)
180 {
181 cerr << "Unable to write to " << outputname << ", aborting." << endl;
182 return -1;
183 }
184
185 strcpy(varname, argv[1]);
186 // Terminate at .
187 if (strchr(varname, '.'))
188 *strchr(varname, '.') = '\0';
189
190 os << "// Auto-generated .bmp file" << endl;
191 os << "// DO NOT HAND EDIT" << endl;
192 os << "// Generated from " << argv[1] << endl;
193 os << endl;
194 os << "const unsigned short bmp_" << varname << "[" << size << "] = {" << endl;
195
196 for (i = 0; i < size; i++)
197 {
198 char buf[100];
199
200 sprintf(buf, "0x%04x", (unsigned int) result[i]);
201 if (i < size-1)
202 strcat(buf, ", ");
203 os << buf;
204 if ((i & 7) == 7)
205 os << endl;
206 }
207 if (i & 7)
208 os << endl;
209
210 os << "};" << endl;
211
212 return 0;
213 }