Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6623239
  • 博文数量: 227
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6678
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-11 10:33
个人简介

网上的蜘蛛

文章分类

全部博文(227)

文章存档

2010年(19)

2009年(29)

2008年(179)

分类: C/C++

2008-10-22 19:47:25

最近对BMP格式比较感兴趣,刚好正在学OpenGL,就想这两者结合起来,写一个独立的
计算机生成BMP格式图像程序,在屏幕中显示和保存为bmp格式文件。
  我的程序产生图像如下:
   bmp

#include "gl/glut.h"
#include "stdlib.h"
#include "stdio.h"


#define BMPWIDTH 256
#define BMPHEIGHT 256
#define BI_RGB 0

unsigned char *imgdata;

#pragma pack(1)

typedef struct /**** BMP file header structure ****/
{
unsigned short bfType; /* Magic number for file */
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} BITMAPFILEHEADER;


typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} BITMAPINFOHEADER;


/*²úÉúbmpÊý¾Ý*/
void makeBMP(unsigned char *imgdat)
{

int i,j,r,g,b,t=0;
for(i=0;i<BMPWIDTH;i++)
for(j=0;j<BMPHEIGHT;j++)
{
r=(4*j)%255;
g=(4*i)%255;
b=(i*j)%255;

imgdata[t]=b;
imgdata[t+1]=g;
imgdata[t+2]=r;
t+=3;
}
}


void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}


void key(unsigned char ch,int x,int y)
{
if(ch=='0')
exit(0);
}

void display()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);

glRasterPos2i(0,0);
glDrawPixels( BMPWIDTH,BMPHEIGHT,GL_BGR_EXT, GL_UNSIGNED_BYTE,imgdata);
glFlush();
}


/* ±£´æΪbmp¸ñʽµÄÎļþ */
void saveBMP(char* name,unsigned char *imgdat)
{
FILE *fp;
fp=fopen(name,"wb");

BITMAPFILEHEADER bfh;
bfh.bfType=0X4D42;
bfh.bfSize=54+(BMPWIDTH*24+7)/8*BMPHEIGHT;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=54;
fwrite(&bfh,14,1,fp);

BITMAPINFOHEADER bih;
bih.biSize=40;
bih.biWidth=BMPWIDTH;
bih.biHeight=BMPHEIGHT;
bih.biPlanes=1;
bih.biBitCount=24;
bih.biCompression=BI_RGB;
bih.biSizeImage=0;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;

printf("%d\n",sizeof(bfh));
fwrite(&bih,40,1,fp);
fwrite(imgdat,BMPWIDTH*BMPHEIGHT*3,1,fp);
fclose(fp);
}


void main(int agc,char **agv)
{
imgdata=(unsigned char*)malloc(BMPWIDTH*BMPHEIGHT*3);
makeBMP(imgdata); /* ²úÉúbmp¸ñʽÊý¾Ý²¿·Ö */
saveBMP("outbmp.bmp",imgdata); /*½«Í¼ÏñÊä³öbmp¸ñʽ±£´æ*/

glutInit(&agc,agv);
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE);
glutInitWindowSize(BMPWIDTH,BMPHEIGHT);
glutInitWindowPosition(100,100);

glutCreateWindow("test_bmp");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);

glutMainLoop();
}

阅读(3507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~