您的位置:首页 > 编程语言 > C语言/C++

24位bmp转化为 565或者888C语言数组文件

2012-05-18 22:31 309 查看
/*这是我写的可以把24位bmp转化为 565或者888C语言数组文件的代码,
可以在嵌入式图片资源显示直接调用。
整个工程 在http://download.csdn.net/detail/liujia2100/4312894,
没有资源分哟!*/ 
#include "stdafx.h"
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h> 
#include "string.h"
//#include "24.h"
//#include "16.h"
//#include <graphics.h>

typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef float               FLOAT;
typedef unsigned char       byte;
//#define MAX(a,b,c) a>(b>c?b:c)?a:
struct BMP_img
{
    WORD  bfType;
    DWORD size;
	DWORD reser;
	DWORD header_length;
    DWORD infoheader_length;
	DWORD width;
	DWORD height;
	WORD  biplanes;
	WORD  bmp_type;        /* 8bit 24bit; */
	DWORD compres;
	DWORD datasize;
    DWORD bixpm;
	DWORD biypm;
	DWORD clrused;
	DWORD  relclrused;
    BYTE *image;
    byte *header_info;
	DWORD lineBytes;
	
};
struct RGB
{
	byte bitb;
	byte bitg;
	byte bitr;
	byte  re;
};

void read_img(char *path, struct BMP_img *img);
void make_src(char *outpath, struct BMP_img *img);
void display(byte *temp,int width,int height);
void make_src16(char *outpath, struct BMP_img *img);
void display16(unsigned short *temp,int width,int height);

#define	RGB2565(x) ((((x >> 16) & 0xff) >> 3) << 11) | ((((x >> 8) & 0xff) >> 2) << 5) | ((x & 0xff) >> 3)
#define RGB2_565(r,g,b)  (unsigned short)((((unsigned)(r) << 8) & 0xF800)|(((unsigned)(g) << 3) & 0x7E0)|(((unsigned)(b) >> 3)))
#define RGB565_2(x) (unsigned int)(((unsigned int)(x&0x1f))|((unsigned int)((x&0x7e0)<<5))|((unsigned int)((x&0xF800)<<8)))
#define RGB565_24(rgb,r,g,b)	do{r=(unsigned)(rgb>>8)&0xff;g=(unsigned)(rgb>>3)&0xff;b=(unsigned)(rgb<<3)&0xff;}while(0)
#define bigrvs(x) (unsigned int)((x&0xff)<<16)|((unsigned int)(x&0x00ff00))|((unsigned int)(x&0xff0000)>>16)

int main()
{

//	int i,j,n,flag;
//	unsigned char *temp=(unsigned char *)bsp_img;
//	unsigned short *temp1=(unsigned short *)bsp_img16;
	struct BMP_img img;

//	initgraph(480,480);
//	read_img("2.bmp", &img);
//	make_src("24.h",&img);

//	make_src16("16.h",&img);

//	display(temp,480,272);
//	display16(temp1,480,272);
int flag;
char srcbmp[100]={0};
char dstfile[100]={0};

printf("Please input 24bit bmp path name\nC file path name\n16 or 24bit\n");
scanf("%s %s %d",srcbmp,dstfile,&flag);
read_img(srcbmp, &img);
if(flag==16)
make_src16(dstfile,&img);
if(flag==24)
make_src(dstfile,&img);
printf("\nConvert Over\nPlease Check\n");

	return 0;
}

void read_img(char *path, struct BMP_img *img)
{
	FILE *infile;
	DWORD i,j,l,bitcolor;
	DWORD line24;
	DWORD line8;
	
    struct RGB *bitmap;

	if((infile=fopen(path,"rb"))==NULL)
	{
		printf( "\nCan not open the path: %s \n", path);
		exit(-1);
	}

	fread(&img->bfType,sizeof(WORD),1,infile);//printf("\n打开的图为 %d",img->bfType);
	fread(&img->size,sizeof(DWORD),1,infile);          printf("\n图片大小为:%d",img->size);
	fread(&img->reser,sizeof(DWORD),1,infile);//printf("\n保留位:");
	fread(&img->header_length,sizeof(DWORD),1,infile); printf("\n信息长度:%d",img->header_length);
	fread(&img->infoheader_length,sizeof(DWORD),1,infile);
	fread(&img->width, sizeof(DWORD), 1, infile);
	fread(&img->height, sizeof(DWORD), 1, infile);     printf( "\n宽度=%d  高度=%d ", img->width, img->height);
	fread(&img->biplanes, sizeof(WORD), 1, infile);
	fread(&img->bmp_type, sizeof(WORD), 1, infile);    printf("\n位图位数=%d ", img->bmp_type);
	fread(&img->compres, sizeof(DWORD), 1, infile);    if(img->compres==0) {printf("\nbmp图片为非压缩!");}
	fread(&img->datasize, sizeof(DWORD), 1, infile);   printf("\n位图数据大小=%d ",img->datasize);   
	fread(&img->bixpm, sizeof(DWORD), 1, infile);
	fread(&img->biypm, sizeof(DWORD), 1, infile);
	fread(&img->clrused, sizeof(DWORD), 1, infile);    printf("\n实际使用颜色数=%d ",img->clrused);  
	fread(&img->relclrused, sizeof(DWORD), 1, infile);      
	
	img->lineBytes=(img->width*img->bmp_type+31)/32*4;
	//printf("\nLineBytes            :%l\n",img->lineBytes);
	
	line24=(img->width*24+31)/32*4;
	
	   line8=(img->width*8+31)/32*4;
	   if(img->bmp_type==1){bitcolor=2;printf("不能读取退出");exit(-1);}
	   if(img->bmp_type==4){bitcolor=16;printf("不能读取退出");exit(-1);}
	   if(img->bmp_type==8)
	   {
		   byte *temp=(BYTE*)malloc(img->height*line8*sizeof(BYTE));
		   memset(temp,0x00,img->height*img->lineBytes*sizeof(BYTE));
		   
		   bitcolor=256;
		   bitmap=(struct RGB *)calloc(bitcolor,sizeof(struct RGB));
		   img->image=(unsigned char *)malloc(sizeof(unsigned char)*(line8*img->height));
		   memset(img->image,0x00,sizeof(byte)*line8*img->height);
		   
		   if(img->image==NULL) {fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");}
		   
		   fseek(infile,0x36, SEEK_SET);
		   fread(bitmap,sizeof(struct RGB),bitcolor,infile);
		   fseek(infile, img->header_length, SEEK_SET);
		   //fread(temp, sizeof(unsigned char),lineBytes*img->height, infile);
		   fread(temp, img->lineBytes*img->height,1, infile);
		   if(temp==NULL)printf("\n读取失败\n");
		   
		   for(i=0;i<img->height;i++)
		   {
			   for(j=0;j<img->width;j++)
			   {
				   img->image[i*img->width+j]=(byte)(0.299*bitmap[temp[i*line8+j]].bitb+0.578*bitmap[temp[i*line8+j]].bitg+0.114*bitmap[temp[i*line8+j]].bitr);
				   //	putpixel(j,img->height-i,RGB(img->image[i*img->width+j],img->image[i*img->width+j],img->image[i*img->width+j]));
				   
			   }
		   }
		   free(temp);
		   temp=NULL;
	   }
	   if(img->bmp_type==24)
	   {
		   byte *temp=(byte *)malloc(sizeof(byte)*img->height*img->lineBytes); if(temp==NULL)
			   exit(-1);
		   img->image=(unsigned char *)malloc(sizeof(unsigned char)*((line24)*img->height));
		   if(img->image==NULL) fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");
		   fseek(infile, img->header_length, SEEK_SET);
		   fread(temp, sizeof(unsigned char), (img->lineBytes)*img->height, infile);
		   // byte *temp=(byte *)malloc(sizeof(byte)*img->lineBytes*img->height)
	//	   memcpy(img->image,temp,(img->lineBytes)*img->height);
		   for(i=0;i<img->height;i++)
		   {
			   l=0;
			   for(j=0;j<img->width*3;j+=3)
			   {
				  
				   l=(img->height-i-1)*img->width*3+j;
				   img->image[l+2]=*(temp+i*img->lineBytes+j+2);
				   img->image[l+1]=*(temp+i*img->lineBytes+j+1);
				   img->image[l]=*(temp+i*img->lineBytes+j);
			   }
		   }
		   
		   free(temp);
		   temp=NULL;
	   }
}
void make_src(char *outpath, struct BMP_img *img)
{
	FILE *infile;
	int i,j,k,n;
	char pbuf[10]={0};
	if((infile=fopen(outpath,"wa+"))==NULL)
	{
		printf( "\nCan not open the path: %s \n", outpath);
		exit(-1);
	}
	k=0;
/*	for(i=0;i<img->height;i++)
	{
		for(n=0,j=0;n<img->lineBytes,j<img->width;n+=3,j++)
		{
			sprintf(pbuf,"0x%08x",img->image[i*img->lineBytes+n])

		}
	}*/
	fwrite("unsigned char bsp_img[]={\n",strlen("unsigned char bsp_img[]={\n"),1,infile);
	for(i=0;i<img->height;i++)
	{
		for(j=0;j<img->width*3;j++)
		{
			sprintf(pbuf,"0x%02x",img->image[i*img->width*3+j]);
			fwrite(pbuf,strlen(pbuf),1,infile);
			if((i==img->height-1)&&(j==img->width*3-1))
				break;
			fwrite(",",strlen(","),1,infile);
			k++;
			if(k==16)
			{
				k=0;
				fwrite("\n",strlen("\n"),1,infile);

			}
		}
	}
	fseek(infile,0,SEEK_END);
	fwrite("\n};",strlen("\n};"),1,infile);

	fclose(infile);
//	free()
	

}
void display(byte *temp,int width,int height)
{
	DWORD i,j,n;
   	for(i=0;i<height;i++)
    {
        for(j=0,n=0;n<width*3,j<width;n+=3,j++)
            
        {  
	//		putpixel(j,i,RGB(*(temp+i*width*3+n+2),*(temp+i*width*3+n+1),*(temp+i*width*3+n)));
        }
    }
}

void make_src16(char *outpath, struct BMP_img *img)
{
	FILE *infile;
	int i,j,k,n;
	char pbuf[10]={0};
	if((infile=fopen(outpath,"wa+"))==NULL)
	{
		printf( "\nCan not open the path: %s \n", outpath);
		exit(-1);
	}
	k=0;
/*	for(i=0;i<img->height;i++)
	{
		for(n=0,j=0;n<img->lineBytes,j<img->width;n+=3,j++)
		{
			sprintf(pbuf,"0x%08x",img->image[i*img->lineBytes+n])

		}
	}*/
//	fwrite("unsigned char bsp_img16[]={",strlen("unsigned char bsp_img[]={"),1,infile);
	fwrite("unsigned short bsp_img16[]={\r\n",strlen("unsigned short bsp_img[]={\r\n"),1,infile);
	for(i=0;i<img->height;i++)
	{
		for(j=0,n=0;n<img->width,j<img->width*3;n++,j=j+3)
		{
			sprintf(pbuf,"0x%04x", RGB2_565(img->image[i*img->width*3+j],img->image[i*img->width*3+j+1],img->image[i*img->width*3+j+2]));
			fwrite(pbuf,strlen(pbuf),1,infile);
			if((i==img->height-1)&&(j>=img->width*3-1))
				break;
			fwrite(",",strlen(","),1,infile);
			k++;
			if(k>=16)
			{
				k=0;
				fwrite("\n",strlen("\n"),1,infile);

			}
		}
	}
	fseek(infile,0,SEEK_END);
	fwrite("\n};",strlen("\n};"),1,infile);

	fclose(infile);
//	free()
	

}
void display16(unsigned short *temp,int width,int height)
{
	DWORD i,j,n;
	unsigned char r,g,b;

   	for(i=0;i<height;i++)
    {
        for(j=0;j<width;j++)
            
        {  
			RGB565_24(temp[i*width+j],b,g,r);
			
		//	putpixel(j,i,RGB(r,g,b));
        }
    }
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: