-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmp2c.c
289 lines (250 loc) · 6.78 KB
/
bmp2c.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define XPARENT (0xf7) /* palette index representing transparent in BMP */
typedef struct stPicData
{
short pwid;
short phgt;
short pxoff;
short pyoff;
long *coloffs;
unsigned char *posts;
long postlen;
} PicData;
#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L
typedef unsigned short UINT;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned char UBYTE;
typedef struct tagBITMAPFILEHEADER
{
UINT bfType; // 2
DWORD bfSize; // 4
UINT bfReserved1; // 2
UINT bfReserved2; // 2
DWORD bfOffBits; // 4
} __attribute__ ((packed)) BITMAPFILEHEADER; // 14
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize; // 4
LONG biWidth; // 4
LONG biHeight; // 4
WORD biPlanes; // 2
WORD biBitCount; // 2
DWORD biCompression; // 4
DWORD biSizeImage; // 4
LONG biXPelsPerMeter; // 4
LONG biYPelsPerMeter; // 4
DWORD biClrUsed; // 4
DWORD biClrImportant; // 4
} __attribute__ ((packed)) BITMAPINFOHEADER; // 40
typedef struct tagRGBQUAD
{
UBYTE rgbBlue;
UBYTE rgbGreen;
UBYTE rgbRed;
UBYTE rgbReserved;
} __attribute__ ((packed)) RGBQUAD;
// Convert a rectangular array of numbers to a DOOM format picture
// bytes 0-255 represent palette colors as usual, XPARENT is transparency.
//
// memory is allocated on p->coloffs and p->posts
// max: 256k+rwid actual: postlen+4*(1+rwid)
void ConvertToPicture(BYTE *bytes,int logw,int rwid,int logh,PicData *p,int pf)
{
int i,j,n;
int pixcnt;
int postlen;
p->pwid = rwid;
p->phgt = logh;
p->pxoff = pf? rwid/2 - 1 : 0;
p->pyoff = pf? logh - 5 : 0;
p->coloffs = (long *)malloc(logw*sizeof(int));
p->posts = (unsigned char *)malloc(1024*256*sizeof(unsigned char));
n=postlen=0;
for (j=0;j<rwid;j++) // do each column
{
p->coloffs[j] = 4*sizeof(short) + p->pwid*sizeof(int) + postlen;
pixcnt = 0;
for (i=0;i<logh;i++)
{
if (bytes[logw*i+j]==XPARENT)
{
if (pixcnt>0)
{
p->posts[postlen++]=0; // add fill byte at end
pixcnt=0;
}
}
else
{
if (pixcnt==0) // start a post
{
p->posts[postlen++] = i;
n = postlen++;
p->posts[n] = 1;
p->posts[postlen++] = 0; // add fill byte at start
}
else p->posts[n]++;
// add pixel to current post
p->posts[postlen++] = bytes[logw*i+j];
pixcnt++;
}
}
if (pixcnt>0) // terminate last post
p->posts[postlen++]=0;
p->posts[postlen++]=255; // terminate column
}
p->posts = (unsigned char *)realloc(p->posts,postlen);
p->postlen = postlen;
}
// Reads BMP file
// Header is read for real width, and logical height
// Logical width is next multiple of 4 greater than or equal to real width
// Bitmap is written to short array *out, allocated to fit
// *out is allocated and read as logical width by logical height
//
// memory is allocated on *out
// max: logw*logh*sizeof(short)
void ReadBMP(BYTE **out,int *logw,int *realw,int *logh,char *bmpname)
{
int i,n;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
FILE *st;
char *buffer=NULL;
char *p=NULL;
RGBQUAD pal[256];
*logw = *logh = *realw = 0;
st = fopen(bmpname,"rb");
if (st!=NULL)
{
fread(&bmfh.bfType,sizeof(bmfh.bfType),1,st);
fread(&bmfh.bfSize,sizeof(bmfh.bfSize),1,st);
fread(&bmfh.bfReserved1,sizeof(bmfh.bfReserved1),1,st);
fread(&bmfh.bfReserved2,sizeof(bmfh.bfReserved2),1,st);
fread(&bmfh.bfOffBits,sizeof(bmfh.bfOffBits),1,st);
fread(&bmih.biSize,sizeof(bmih.biSize),1,st);
fread(&bmih.biWidth,sizeof(bmih.biWidth),1,st);
fread(&bmih.biHeight,sizeof(bmih.biHeight),1,st);
fread(&bmih.biPlanes,sizeof(bmih.biPlanes),1,st);
fread(&bmih.biBitCount,sizeof(bmih.biBitCount),1,st);
fread(&bmih.biCompression,sizeof(bmih.biCompression),1,st);
fread(&bmih.biSizeImage,sizeof(bmih.biSizeImage),1,st);
fread(&bmih.biXPelsPerMeter,sizeof(bmih.biXPelsPerMeter),1,st);
fread(&bmih.biYPelsPerMeter,sizeof(bmih.biYPelsPerMeter),1,st);
fread(&bmih.biClrUsed,sizeof(bmih.biClrUsed),1,st);
fread(&bmih.biClrImportant,sizeof(bmih.biClrImportant),1,st);
fread(pal,sizeof(RGBQUAD),256,st);
*realw = bmih.biWidth;
*logw = 4*((bmih.biWidth+3)/4);
*logh = bmih.biHeight;
n = (*logw)*(*logh);
buffer = malloc(n);
fseek(st,bmfh.bfOffBits,SEEK_SET);
fread(buffer,n,1,st);
*out = (BYTE *)malloc(n);
for (i=(*logh)-1;i>=0;i--)
memmove((*out)+((*logh)-1-i)*(*logw),buffer+(*logw)*i,*logw);
fclose(st);
free(buffer);
}
else fprintf(stderr,"ERROR: bitmap file %s not found\n",bmpname);
}
// Read a .BMP file and embed in a wad as a DOOM picture
// memory balanced except directory extension
void WritePic(char *path,FILE *st,int pf)
{
int n,t;
BYTE *out = NULL;
int logw=0,logh=0,rwid=0;
PicData pic;
char name[256],*p;
BYTE *q;
if (path && *path)
{
ReadBMP(&out,&logw,&rwid,&logh,path); // allocs out
if (logw>0)
{
ConvertToPicture(out,logw,rwid,logh,&pic,pf);
strcpy(name,path[1]==':'? path+2 : path); // strip drive, if any
p = strrchr(name,'\\'); // strip dirs, if any
if (p) strcpy(name,p+1);
p = strrchr(name,'.'); // strip ext, if any
if (p) *p='\0';
// code to write pic as C data here
fprintf(st,"static const char %s[]=\n{\n\t",name);
q = (BYTE *)&pic.pwid;
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,",*q++);
fprintf(st,"%3d,\n\t",*q++);
q = (BYTE *)pic.coloffs;
t = pic.pwid*sizeof(long);
for (n=0;n<t;n++)
{
fprintf(st,"%3d,",*q++);
if ((n&15)==15)
fprintf(st,"\n\t");
}
q = pic.posts;
for (;n<pic.postlen+t;n++)
{
fprintf(st,"%3d,",*q++);
if (n==t+pic.postlen-1)
fprintf(st,"\n};\n\n");
else if ((n&15)==15)
fprintf(st,"\n\t");
}
free(pic.coloffs);
free(pic.posts);
}
else
{
fprintf(stderr,"ERROR: Can't open file: %s\n",path);
return;
}
free(out);
}
}
// write all .BMP files specified as input (possibly wildcarded)
// as a collection of C arrays
int main(int argc,char **argv)
{
int i;
char name[256];
FILE *st;
int pf;
if (argc<2)
{
printf("Usage: BMP2C [/p] name1.bmp...\n");
printf("name may contain wildcards\n");
printf("/p will insert patch style offsets, else 0\n");
exit(1);
}
st = fopen("cdata.c","w");
if (st)
{
pf = 0;
if (argv[1][0] == '-' || argv[1][0]=='/' && tolower(argv[1][1])=='p')
pf = 1;
for (i=1+pf;i<argc;i++)
{
strcpy(name,argv[i]);
WritePic(name,st,pf);
}
}
else
fprintf(stderr,"Cannot open CDATA.C for output\n");
return 0;
}