bmp2u64.c
6.83 KB
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
/*===========================================================
bmp2u64 : BMP to U64 converter
BMP->16bitRGBA Converter (for PC)
version 1.0
Programmed by M.Kawamura 1997.07.11
Copyright (C) 1997 Nintendo.Co.,Ltd.
===========================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
char *progName = "bmp2u64";
char *usage =
"=== bmp2u64 (version 1.0) ===\n"
"usage: bmp2u64 <bmpFilename> <u64Filename>\n";
void write1bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height);
void write4bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height);
void write8bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height);
void write24bitdata16(FILE *infp, FILE *outfp, long width, long height);
unsigned short chendian16(unsigned short *num);
unsigned long chendian32(unsigned long *num);
main(int argc, char *argv[])
{
int i;
FILE *infile, *outfile;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD aColors[256];
unsigned short rgba16[256];
if(argc != 3){
fprintf(stderr, "%s\n", usage);
exit(1);
}
if((infile = fopen(argv[1], "rb")) == NULL){
fprintf(stderr, "%s: cannot open input file [%s].\n", progName, argv[1]);
exit(1);
}
fread(&bmfh, sizeof(BITMAPFILEHEADER), 1, infile);
/* Check Header */
if(bmfh.bfType != 19778){
fprintf(stderr, "%s: [%s] is not a BMP file.\n", progName, argv[1]);
fclose(infile);
exit(1);
}
fread(&bmih, sizeof(BITMAPINFOHEADER), 1, infile);
printf("===== BMP Image Information =====\n");
printf(" Width = %ld\n", bmih.biWidth);
printf(" Height = %ld\n", bmih.biHeight);
printf(" Colors = ");
switch(bmih.biBitCount){
case 1:
printf("1-bit monochrome\n");
break;
case 4:
printf("4-bit 16 colors\n");
break;
case 8:
printf("8-bit 256 colors\n");
break;
case 24:
printf("24-bit colors\n");
break;
default:
printf("Unknown color format\n");
break;
}
printf(" Compression = ");
switch(bmih.biCompression){
case BI_RGB:
printf("No Compression\n");
break;
case BI_RLE8:
printf("RLE8\n");
break;
case BI_RLE4:
printf("RLE4\n");
break;
default:
printf("Unknown compression format\n");
break;
}
printf("---------------------------------\n");
if(bmih.biCompression != BI_RGB){
fprintf(stderr, "%s: Sorry, RLE bitmap is not supported.\n", progName);
fclose(infile);
exit(1);
}
if(bmih.biBitCount != 24){
if(!bmih.biClrUsed)
bmih.biClrUsed = pow(2, bmih.biBitCount);
fread(aColors, sizeof(RGBQUAD), bmih.biClrUsed, infile);
for(i = 0; i < bmih.biClrUsed; i++){
rgba16[i] = ((unsigned short)(aColors[i].rgbRed >> 3) << 11);
rgba16[i] |= ((unsigned short)(aColors[i].rgbGreen >> 3) << 6);
rgba16[i] |= ((unsigned short)(aColors[i].rgbBlue >> 3) << 1);
rgba16[i] |= 0x0001;
chendian16(&rgba16[i]);
}
}
fseek(infile, bmfh.bfOffBits, 0);
if((outfile = fopen(argv[2], "wb")) == NULL){
fprintf(stderr, "%s: cannot open output file [%s].\n", progName, argv[2]);
fclose(infile);
exit(1);
}
/* RGBAf[^̏o */
switch(bmih.biBitCount){
case 1:
write1bitdata16(infile, outfile, rgba16, bmih.biWidth, bmih.biHeight);
break;
case 4:
write4bitdata16(infile, outfile, rgba16, bmih.biWidth, bmih.biHeight);
break;
case 8:
write8bitdata16(infile, outfile, rgba16, bmih.biWidth, bmih.biHeight);
break;
case 24:
write24bitdata16(infile, outfile, bmih.biWidth, bmih.biHeight);
break;
default:
fprintf(stderr, "%s: [%s] is not a BMP file.\n", progName, argv[1]);
fclose(infile);
fclose(outfile);
unlink(argv[2]);
break;
}
fclose(outfile);
fclose(infile);
return EXIT_SUCCESS;
}
void write1bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height)
{
long ptrWd = 0, ptrHt = 0;
int i;
unsigned long data;
unsigned int c;
long ptr = width * (height - 1) * 2;
fseek(outfp, ptr, 0);
while(fread(&data, 4, 1, infp) == 1){
chendian32(&data);
for(i = 31; i >= 0; i--){
if(ptrWd < width){
c = (unsigned int)(data >> i) & 0x0001;
fwrite(palette + c, 2, 1, outfp);
}
ptrWd++;
}
if(ptrWd >= width){
ptrWd = 0;
ptrHt++;
if(ptrHt >= height)
return;
ptr -= width * 2;
fseek(outfp, ptr, 0);
}
}
}
void write4bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height)
{
long ptrWd = 0, ptrHt = 0;
int i;
unsigned long data;
unsigned int c;
long ptr = width * (height - 1) * 2;
fseek(outfp, ptr, 0);
while(fread(&data, 4, 1, infp) == 1){
chendian32(&data);
for(i = 7; i >= 0; i--){
if(ptrWd < width){
c = (unsigned int)(data >> (4 * i)) & 0x000f;
fwrite(palette + c, 2, 1, outfp);
}
ptrWd++;
}
if(ptrWd >= width){
ptrWd = 0;
ptrHt++;
if(ptrHt >= height)
return;
ptr -= width * 2;
fseek(outfp, ptr, 0);
}
}
}
void write8bitdata16(FILE *infp, FILE *outfp, unsigned short *palette, long width, long height)
{
long ptrWd = 0, ptrHt = 0;
int i;
unsigned long data;
unsigned int c;
long ptr = width * (height - 1) * 2;
fseek(outfp, ptr, 0);
while(fread(&data, 4, 1, infp) == 1){
chendian32(&data);
for(i = 3; i >= 0; i--){
if(ptrWd < width){
c = (unsigned int)(data >> (8 * i)) & 0x00ff;
fwrite(palette + c, 2, 1, outfp);
}
ptrWd++;
}
if(ptrWd >= width){
ptrWd = 0;
ptrHt++;
if(ptrHt >= height)
return;
ptr -= width * 2;
fseek(outfp, ptr, 0);
}
}
}
void write24bitdata16(FILE *infp, FILE *outfp, long width, long height)
{
long ptrWd = 0, ptrHt = 0;
unsigned char data[3];
unsigned short outdata;
unsigned int c;
long ptr = width * (height - 1) * 2;
fseek(outfp, ptr, 0);
while(fread(data, 1, 3, infp) == 3){
outdata = ((unsigned short)(data[2] >> 3) << 11);
outdata |= ((unsigned short)(data[1] >> 3) << 6);
outdata |= ((unsigned short)(data[0] >> 3) << 1);
outdata |= 0x0001;
/* to convert Endian */
chendian16(&outdata);
fwrite(&outdata, 2, 1, outfp);
ptrWd++;
if(ptrWd >= width){
fread(data, 1, (4 - (ptrWd % 4)) % 4, infp);
ptrWd = 0;
ptrHt++;
if(ptrHt >= height)
return;
ptr -= width * 2;
fseek(outfp, ptr, 0);
}
}
}
/*
* chendian16 : Change Endian (16bit)
*/
unsigned short chendian16(unsigned short *num)
{
unsigned char high, low;
unsigned short out;
high = (*num >> 8) & 0x00FF;
low = *num & 0x00FF;
out = ((unsigned int)low << 8) | (unsigned int)high;
*num = out;
return out;
}
/*
* chendian32 : Change Endian (32bit)
*/
unsigned long chendian32(unsigned long *num)
{
unsigned short high, low;
unsigned long out;
high = (*num >> 16) & 0x0000FFFF;
low = *num & 0x0000FFFF;
high = chendian16(&high);
low = chendian16(&low);
out = ((unsigned long)low << 16) | (unsigned long)high;
*num = out;
return out;
}