#include
#include
static int g_iExtraSize=0;
static void *g_pExtra=0;
static const int g_iHeaderSize=54;
static void *g_pHeader=0;
void *bmp32to24(unsigned char *fileName)
{
unsigned char *p=0;
unsigned char *q=0;
FILE *fr=0;
int t=0;
unsigned char bfType[2]={0};
int bfSize=0;
unsigned short bfReserved1=0;
unsigned short bfReserved2=0;
int bfOffbits=0;
int biSize=0;
int biWidth=0;
int biHeight=0;
unsigned short biPlanes=0;
unsigned short biBitCount=0;
int biCompression=0;
int biSizeImage=0;
int biXpelsPermeter=0;
int biYpelsPermeter=0;
int biClrUsed=0;
int biClrImportant=0;
int bMask=0;
int gMask=0;
int rMask=0;
fr=fopen(fileName, "rb");
if (fr)
{
g_pHeader=malloc(g_iHeaderSize);
if (g_pHeader)
{
t=fread(g_pHeader, 1, g_iHeaderSize, fr);
if (t==g_iHeaderSize)
{
q=g_pHeader;
memcpy(bfType, q, 2);
q+=2;
memcpy(&bfSize, q, 4);
q+=4;
memcpy(&bfReserved1, q, 2);
q+=2;
memcpy(&bfReserved2, q, 2);
q+=2;
memcpy(&bfOffbits, q, 4);
q+=4;
memcpy(&biSize, q, 4);
q+=4;
memcpy(&biWidth, q, 4);
q+=4;
memcpy(&biHeight, q, 4);
q+=4;
memcpy(&biPlanes, q, 2);
q+=2;
memcpy(&biBitCount, q, 2);
q+=2;
memcpy(&biCompression, q, 4);
q+=4;
memcpy(&biSizeImage, q, 4);
q+=4;
memcpy(&biXpelsPermeter, q, 4);
q+=4;
memcpy(&biYpelsPermeter, q, 4);
q+=4;
memcpy(&biClrUsed, q, 4);
q+=4;
memcpy(&biClrImportant, q, 4);
q+=4;
g_iExtraSize=bfOffbits-g_iHeaderSize;
g_pExtra=malloc(g_iHeaderSize);
t=fread(g_pExtra, 1, g_iExtraSize, fr);
if (t>11 && t==g_iExtraSize)
{
q=g_pExtra;
memcpy(&bMask, q, 4);
q+=4;
memcpy(&gMask, q, 4);
q+=4;
memcpy(&rMask, q, 4);
q+=4;
}
else
{
printf("error");
}
if (biBitCount==32)
{
void *buf32=0, *buf24=0;
buf32=malloc(biWidth*biHeight*4);
buf24=malloc(biWidth*biHeight*3);
if (buf32 && buf24)
{
t=fread(buf32, 1, biWidth*biHeight*4, fr);
if (t==biWidth*biHeight*4)
{
int i=0, j=0;
unsigned char headerBuffer[54]={0};
p=buf32;
q=buf24;
for (j=0; j
{
for (i=0; i
{
*q=*p;
q++;
p++;
*q=*p;
q++;
p++;
*q=*p;
q++;
p++;
p++;
}
}
memcpy(headerBuffer, g_pHeader, g_iHeaderSize);
p=headerBuffer;
p+=2;
t=biWidth*biHeight*3+54;
memcpy(p, &t, 4);
p=headerBuffer;
p+=10;
t=54;
memcpy(p, &t, 4);
p=headerBuffer;
p+=28;
t=24;
memcpy(p, &t, 2);
p=headerBuffer;
p+=30;
t=0;
memcpy(p, &t, 4);
p=headerBuffer;
p+=34;
t=biWidth*biHeight*3;
memcpy(p, &t, 4);
{
FILE *fw=0;
fw=fopen("d:\\我的文档\\图片收藏\\24bits.bmp.", "wb");
if (fw)
{
t=fwrite(headerBuffer, 1, g_iHeaderSize, fw);
t=fwrite(buf24, 1, biWidth*biHeight*3, fw);
fclose(fw);
}
}
}
else
{
printf("error");
}
free(buf32);
free(buf24);
}
else
{
printf("error");
}
}
else
{
printf("error");
}
}
else
{
printf("error");
}
}
fclose(fr);
}
return p;
}
void bmp32to24deinit()
{
g_iExtraSize=0;
if (g_pExtra)
{
free(g_pExtra);
g_pExtra=0;
}
if (g_pHeader)
{
free(g_pHeader);
g_pHeader=0;
}
}