File: bmp2vbm.cpp

package info (click to toggle)
v1 1.20-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,240 kB
  • ctags: 9,439
  • sloc: cpp: 48,033; ansic: 8,939; makefile: 1,369; sh: 30
file content (367 lines) | stat: -rw-r--r-- 9,209 bytes parent folder | download | duplicates (4)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <stdio.h>

#define BI_RGB  0
#define BI_RLE8 1
#define BI_RLE4 2

#define WIN 40
#define OS2 64

  int bmp2vbm(FILE* fileIn, FILE* fileOut, char* vbmName);
  static unsigned int rdInt16(FILE *fileIn);
  static unsigned int rdInt32(FILE *fileIn);
  static int rdBMP1(FILE* fileIn, unsigned char* rev,
		    unsigned int w, unsigned int h);
  static int rdBMP4(FILE* fileIn, unsigned char* rev,
		    unsigned int w, unsigned int h, unsigned int comp);
  static int rdBMP8(FILE* fileIn, unsigned char* rev,
		    unsigned int w, unsigned int h, unsigned comp);
  static int rdBMP24(FILE* fileIn, unsigned char* rev,
		     unsigned int w, unsigned int h);
  static unsigned int rdInt16(FILE *fileIn);
  static unsigned int rdInt32(FILE *fileIn);

// ==========================>>> main <<<==========================
  int main(int argc, char** argv)
  {
    // simple driver for bmp2vbm

    FILE* fin;
    FILE* fout;

    if (argc != 4)
      {
	fprintf(stderr,"Usage: b2v in.bmp out.vbm iconname\n");
        return 0;
      }
    if (!(fin = fopen(argv[1],"rb")))
      {
	fprintf(stderr,"Input .bmp file %s not found.\n",argv[1]);
        return 0;
      }
    if (!(fout = fopen(argv[2],"w")))
      {
	fprintf(stderr,"Can't create output .vbm file %s.\n",argv[2]);
	fclose(fin);
        return 0;
      }


    int res = bmp2vbm(fin, fout, argv[3]);
    if (!res)
	fprintf(stderr,"Conversion failed\n");
    fclose(fin);
    fclose(fout);

    return 0;
  }

// ==========================>>> bmp2vbm <<<==========================
  int bmp2vbm(FILE* fileIn, FILE* fileOut, char* vbmName)
  {
    // Convert MSDOS, OS/2 bmp file to a V .vbm file

    int          i, c, c1, rv;
    unsigned int bfSize, bfOffBits, Size, Width, Height, Planes;
    unsigned int BitCount, Compression, SizeImage, XPelsPerMeter;
    unsigned int YPelsPerMeter, ClrUsed, ClrImportant;
    int bPad;

    // First two bytes must be "BM"

    c = getc(fileIn);  c1 = getc(fileIn);
    if (c !='B' || c1 != 'M')		// anything not BM fails
	return 0;

    bfSize = rdInt32(fileIn);
    rdInt16(fileIn);         		// reserved: ignore
    rdInt16(fileIn);
    bfOffBits = rdInt32(fileIn);

    Size = rdInt32(fileIn);

    if (Size == WIN || Size == OS2)  // read header info
      {
	Width         = rdInt32(fileIn);
	Height        = rdInt32(fileIn);
	Planes        = rdInt16(fileIn);
	BitCount      = rdInt16(fileIn);
	Compression   = rdInt32(fileIn);
	SizeImage     = rdInt32(fileIn);
	XPelsPerMeter = rdInt32(fileIn);
	YPelsPerMeter = rdInt32(fileIn);
	ClrUsed       = rdInt32(fileIn);
	ClrImportant  = rdInt32(fileIn);
      }
    else
	return 0;			// old format, we won't handle!

    // Check to see if things are as they should be

    if (ferror(fileIn) || feof(fileIn))	// something is amis
	return 0;			// so fail

    if ((BitCount!=1 && BitCount!=4 && BitCount!=8 && BitCount!=24) || 
      Planes!=1 || Compression > BI_RLE4)
	return 0;

    if (((BitCount==1 || BitCount==24) && Compression != BI_RGB) ||
	Compression != BI_RGB)	// only uncompressed for now
//         (BitCount==4 && Compression==BI_RLE8) ||
//         (BitCount==8 && Compression==BI_RLE4))
	return 0;

    // Skip to color map
    c = Size - 40;    // 40 bytes read from Size to ClrImportant
    for (i=0; i<c; i++)
	getc(fileIn);
    
    bPad = bfOffBits - (Size + 14);	// padding after color map

    // Now, write the header

    if (BitCount != 24)		// colormap for 1 or 4 or 8
      {
	int i, cmaplen;
	cmaplen = (ClrUsed) ? ClrUsed : 1 << BitCount;

        fprintf(fileOut,"//vbm8\n");
	fprintf(fileOut,"#define %s_height %d\n", vbmName,Height);
	fprintf(fileOut,"#define %s_width %d\n", vbmName,Width);
	fprintf(fileOut,"unsigned char %s_bits[] = {\n%d,\n",vbmName,cmaplen-1);

	int r,g,b;
  	int valsOut = 0;
	for (i=0; i<cmaplen; i++)
	  {
	    b = getc(fileIn);
	    g = getc(fileIn);
	    r = getc(fileIn);
	    (void) getc(fileIn);	// skip pad byte
	    bPad -= 4;			// we just read 4 bytes
	    fprintf(fileOut,"%d,%d,%d,",r,g,b);
	    valsOut += 3;
	    if (valsOut >= 20)
	      {
		valsOut = 0;
	 	fprintf(fileOut,"\n");
	      }
	  }
	fprintf(fileOut,"// begin bitmap\n");
      }
    else				// 24 bit bmp
      {
	fprintf(fileOut,"//vbm24\n");
	fprintf(fileOut,"#define %s_height %d\n", vbmName,Height);
	fprintf(fileOut,"#define %s_width %d\n", vbmName,Width);
	fprintf(fileOut,"unsigned char %s[] = {\n",vbmName);
      }

    if (ferror(fileIn) || feof(fileIn))	// something is amiss
	return 0;			// so fail

    // Now, skip over any unused bytes between the color map (if there
    // was one, and the start of the bitmap data.
    
    while (bPad > 0)
      {
	(void) getc(fileIn);
	bPad--;
      }

    // Now, read rest of file and write out an 8 or 24 bit v icon def

    long limit = (BitCount == 24) ? Width*Height*3 : Width*Height;

    unsigned char* rev = new unsigned char[limit];

    if (BitCount == 1)
	rv = rdBMP1(fileIn,rev,Width,Height);
    else if (BitCount == 4)
	rv = rdBMP4(fileIn,rev,Width,Height,Compression);
    else if (BitCount == 8)
	rv = rdBMP8(fileIn,rev,Width,Height, Compression);
    else
	rv = rdBMP24(fileIn,rev,Width,Height);

    int valsOut = 0;

    // have to reverse the bitmap's row order

    long rowLen = (BitCount == 24) ? Width*3 : Width;

    for (long ix = limit - rowLen ; ix >= 0 ; ix -= rowLen)
      {
	// output one row
	for (long ri = 0 ; ri < rowLen ; ++ri)
	  {
	    fprintf(fileOut,"%d,",rev[ix+ri]);
	    if (++valsOut >= 20)
	      {
		valsOut = 0; fprintf(fileOut,"\n");
	      }
	  }
      }

    fprintf(fileOut,"0};\n");
    if (!rv)
	return 0;

    return 1;

  }  

//=========================>> rdBMP1 <<=============================
  static int rdBMP1(FILE* fileIn, unsigned char* rev,
	unsigned int w, unsigned int h)
  {
    int i,j,c,bitnum,padw;

    c = 0;
    padw = ((w + 31)/32) * 32;  /* 'w', padded to be a multiple of 32 */

    for (i=h-1; i>=0; i--)
      {
	for (j=bitnum=0; j<padw; j++,bitnum++)
	  {
	    if ((bitnum&7) == 0)
	      { /* read the next byte */
		c = getc(fileIn);
		bitnum = 0;
	      }
      
	    if (j<w)
	      {
		*rev++ = (c & 0x80) ? 1 : 0;
		c <<= 1;
	      }
	  }

	if (ferror(fileIn) || feof(fileIn))	// something is amiss
	    return 0;			// so fail
      }

    if (ferror(fileIn) || feof(fileIn))	// something is amiss
	return 0;			// so fail
}  

//=========================>> rdBMP4 <<=============================
  static int rdBMP4(FILE* fileIn, unsigned char*rev,
		    unsigned int w, unsigned int h, unsigned int comp)
  {
    int   i,j,c,c1,x,y,nybnum,padw,rv;

    rv = 1;
    c = c1 = 0;

    if (comp == BI_RGB)			// read uncompressed data
      {
	padw = ((w + 7)/8) * 8; 	// 'w' padded to a multiple of 8pix (32 bits)

	for (i=h-1; i>=0; i--)
	  {
	    for (j=nybnum=0; j<padw; j++,nybnum++)
	      {
		if ((nybnum & 1) == 0)	// read next byte
		  { /* read next byte */
		    c = getc(fileIn);
		    nybnum = 0;
		  }

		if (j<w)
		  {
		    *rev++ = (c & 0xf0) >> 4;
		    c <<= 4;
		  }
		if (ferror(fileIn) || feof(fileIn))	// something is amiss
		    return 0;			// so fail
	      }
	  }
      }
    else
	return 0;
  
    if (ferror(fileIn) || feof(fileIn))	// something is amiss
	return 0;			// so fail
    return rv;
  }  

//=========================>> rdBMP8 <<=============================
  static int rdBMP8(FILE* fileIn, unsigned char* rev,
		    unsigned int w, unsigned int h, unsigned int comp)
  {
    int   i,j,c,c1,padw,x,y,rv;
 
    rv = 1;

    if (comp == BI_RGB)		// read uncompressed data
      {
	padw = ((w + 3)/4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */

	for (i=h-1; i>=0; i--) 
	  {
	    for (j=0; j<padw; j++) 
	      {
		c = getc(fileIn);
		if (c==EOF) 
		    rv = 1;
		if (j<w)
		  {
		    *rev++ = c;
		  }
		if (ferror(fileIn) || feof(fileIn))	// something is amiss
		    return 0;			// so fail
	      }
	  }
      }
    else 
	return 0;

    if (ferror(fileIn) || feof(fileIn))	// something is amiss
	return 0;			// so fail
    return rv;
  }  

//=========================>> rdBMP24 <<=============================
  static int rdBMP24(FILE* fileIn, unsigned char* rev,
		     unsigned int w, unsigned int h)
  {
    int   i,j,padb,rv;
    unsigned int r,g,b;

    padb = (4 - ((w*3) % 4)) & 0x03;  // # of pad bytes to read at EOscanline

    for (i=h-1; i>=0; i--)
      {
	for (j=0; j<w; j++) 
	  {
	    b = getc(fileIn);   // blue
	    g = getc(fileIn);   // green
	    r = getc(fileIn);   // red
	    *rev++ = r; *rev++ = g; *rev++ = b;
	  }

	for (j=0; j<padb; j++) 
	    getc(fileIn);
      }
    return 1;
  }  

//=============================>>> rdInt16 <<<=============================
  static unsigned int rdInt16(FILE *fileIn)
  {
    int c, c1;
    c = getc(fileIn);  c1 = getc(fileIn);
    return ((unsigned int) c) + (((unsigned int) c1) << 8);
  }

//=============================>>> rdInt32 <<<=============================
  static unsigned int rdInt32(FILE *fileIn)
  {
    int c, c1, c2, c3;
    c = getc(fileIn);  c1 = getc(fileIn);  c2 = getc(fileIn);  c3 = getc(fileIn);
    return ((unsigned int) c) +
         (((unsigned int) c1) << 8) + 
	 (((unsigned int) c2) << 16) +
	 (((unsigned int) c3) << 24);
  }