1. main.c
#include <stdio.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned int LONG;
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biwidth;
LONG biheight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
int main( int argc, char *argv[])
{
FILE * fsrc, *fdst;
int pos;
RGBQUAD pixel;
long size, cnt, offset, width, height, mode;
BYTE alpha, red, green, blue, msb, lsb;
long Line = 0;
mode = 32;
if (argc !=3){
help:
printf("Usage: \tbmp2bin sourceFile destinationFile bpp[16,32]\n");
return 0;
}
if((fsrc = fopen(argv[1], "rb")) == NULL){
printf( "The file %s cannot be opened\n", argv[1]);
return 0;
}
if((fdst = fopen(argv[2], "wb")) == NULL){
printf( "The file %s cannot be created\n", argv[2] );
return 0;
}
if(argc >3 && (*(int*)argv[3] != 16 || *(int*)argv[3] != 32) ){
goto help;
}
pos = 0x0A;
if(fsetpos(fsrc, &pos) == 0){
offset = fgetc(fsrc) + (fgetc(fsrc) << 8) + (fgetc(fsrc) << 16) + (fgetc(fsrc) << 24);
}
pos = 0x12;
if(fsetpos(fsrc, &pos) == 0){
width = fgetc(fsrc) + (fgetc(fsrc) << 8) + (fgetc(fsrc) << 16) + (fgetc(fsrc) << 24);
}
pos = 0x16;
if(fsetpos(fsrc, &pos) == 0){
height = fgetc(fsrc) + (fgetc(fsrc) << 8) + (fgetc(fsrc) << 16) + (fgetc(fsrc) << 24);
}
size = ((width * 3 + 3) / 4) * 4;
pos = offset + size * (height - 1);
alpha = 0xFF;
while(pos >= 0)
{
if(fsetpos(fsrc, &pos) != 0)
break;
for(cnt = 0; cnt < width; cnt++){
blue = fgetc(fsrc);
green = fgetc(fsrc);
red = fgetc(fsrc);
if(mode == 32)
{
#if 0
fprintf(fdst, "0x%x, ", alpha);
fprintf(fdst, "0x%x, ", blue);
fprintf(fdst, "0x%x, ", green);
fprintf(fdst, "0x%x, ", red);
#else
fwrite(&blue,1,1,fdst);
fwrite(&green,1,1,fdst);
fwrite(&red,1,1,fdst);
fwrite(&alpha,1,1,fdst);
#endif
}
else if(mode == 16)
{
msb = (red & 0xF8) | (green >> 5);
lsb = ((green << 3) & 0xE0) | (blue >> 3);
#if 0
fprintf(fdst, "0x%02x, ", lsb);
fprintf(fdst, "0x%02x, ", msb);
#else
fwrite(&lsb,1,1,fdst);
fwrite(&msb,1,1,fdst);
#endif
}
#if 0
if(++Line%5 == 0)
fprintf(fdst,"\n");
#endif
}
pos -= size;
}
fclose(fsrc);
fclose(fdst);
return 0;
}
2.Makefile:
CC = gcc
obj = main.o
target = bmp2bin
all: $(obj)
$(CC) -o $(target) $(obj)
install:
cp -rf $(target) ../../Qte/arm-qtopia-2.2.0/pro/charge
clean:
rm -rf $(target) $(obj)