Debian Codegraph @

support/bmp2c/bmp2c.cpp

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