#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();
}
|