我有这个代码有一个Bresenham()函数绘制一个简单的行,然后当用户点击右键时,bresenham行被保存为bmp图像。但我遇到的问题是我不知道如何计算bmpInfoHeader结构的宽度和高度。我也不知道如何加载图像以便保存它。
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<gl/glut.h>
typedef struct
{
uint32_t Size;
uint16_t field;
uint32_t offset;
}bmpFileHeader;
typedef struct
{
uint32_t headerSize;
uint32_t width;
uint32_t hight;
uint16_t planes;
uint16_t dcm;
uint32_t compression;
uint32_t imageSize;
uint32_t bmpx;
uint32_t bmpy;
uint32_t colors;
uint32_t impColors;
}bmpInfoHeader;
float a[90000];
int x0=0,y0=0,xf=0,yf=0;
int print=0;
FILE *bmp = NULL;
bmpInfoHeader *infoHeader;
bmpFileHeader *fileHeader;
void init(void);
void putpixel(int x,int y);
void Bresenham(int x0,int y0,int x1,int y1);
void display(void);
void onMotion(int x,int y);
void onMouse(int button, int e, int x, int y);
void Save();
void onPassive(int x,int y);
void createFileHeader(bmpFileHeader *fileHeader);
void createInfoHeader(bmpInfoHeader *infoHeader);
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 300.0, 0.0,300.0);
}
void putpixel(int x,int y)
{
glColor3f(0.0, 0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if(print==1)
glDrawPixels(300,300,GL_RGB,GL_UNSIGNED_BYTE,a);
Bresenham(x0,y0,xf,yf);
glFlush();
}
void Bresenham(int x0,int y0,int x1,int y1)
{
int dx,dy,p,x,y,px = 1,py = 1,twoDy_Dx,twoDy,i;
glColor3f(0.0,0.0,1.0);
dx = x1-x0;
dy = y1-y0;
if(dx < 0)
dx = dx*-1;
if(dy < 0)
dy = dy*-1;
if(x1 < x0)
px = -1;
if(y1 < y0)
py = -1;
x = x0;
y = y0;
if( dx > dy )
{
putpixel(x,y);
p = 2 * dy - dx;
twoDy_Dx = 2 * ( dy - dx );
twoDy = 2 * dy;
for( i = 0; i < dx; i++ )
{
if( p >= 0 )
{
y += py;
p += twoDy_Dx;
}
else
p += twoDy;
x += px;
putpixel(x,y);
}
}
else
{
putpixel(x,y);
p = 2*dx - dy;
twoDy_Dx = 2 * ( dx - dy );
twoDy = 2*dx;
for( i = 0; i < dy; i++ )
{
if( p >= 0 )
{
x += px;
p += twoDy_Dx;
}
else
p += twoDy;
y += py;
putpixel(x,y);
}
}
glFlush();
}
void onMotion(int x,int y)
{
xf = x;
yf = 300-y;
glutPostRedisplay();
}
void onMouse(int button, int e, int x, int y)
{
if((button == GLUT_LEFT_BUTTON) && (e == GLUT_DOWN))
{
print = 1;
x0 = xf = x;
y0 = yf = abs(300-y);
}
else if((button == GLUT_LEFT_BUTTON) && (e == GLUT_UP))
print = 0;
else if((button == GLUT_RIGHT_BUTTON) && (e == GLUT_UP))
{
createFileHeader(fileHeader);
createInfoHeader(infoHeader);
Save();
}
}
void onPassive(int x,int y)
{
glReadPixels(0.0,0.0,300.0,300.0,GL_RGB,GL_UNSIGNED_BYTE,a);
Bresenham(x0,y0,xf,yf);
}
void createInfoHeader(bmpInfoHeader *infoHeader)
{
infoHeader = (bmpInfoHeader*)malloc(sizeof(bmpInfoHeader));
infoHeader->headerSize = sizeof(bmpInfoHeader);
infoHeader->width = ???;
infoHeader->hight = ???;
infoHeader->planes = 1;
infoHeader->dcm = 24;
infoHeader->compression = BI_RGB;
infoHeader->imageSize =;
infoHeader->bmpx = 0;
infoHeader->bmpy = 0;
infoHeader->colors = 0;
infoHeader->impColors = 0;
}
void createFileHeader(bmpFileHeader *fileHeader)
{
fileHeader = (bmpFileHeader*)malloc(sizeof(bmpFileHeader));
fileHeader->Size = 0;
fileHeader->field = 0;
fileHeader->offset = sizeof(bmpFileHeader)+sizeof(bmpInfoHeader);
}
void Save()
{
uint16_t type;
if((bmp = fopen("practica no. 7.bmp","wt"))!= NULL)
{
type = 0x4D42;
fwrite(&type,sizeof(type),1,bmp);
fwrite(&fileHeader,sizeof(bmpFileHeader),1,bmp);
fwrite(&infoHeader,sizeof(bmpInfoHeader),1,bmp);
}
else
printf("No se pudo crear fichero");
}
int main()
{
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow();
init();
glutDisplayFunc(display);
glutMotionFunc(onMotion);
glutMouseFunc(onMouse);
glutPassiveMotionFunc(onPassive);
glutMainLoop();
}
答案 0 :(得分:0)
从您的代码中可以看出,图像是 300 x 300。这意味着,如果您保持比例,您的 bmpInfoHeader.width
和 bmpInfoHeader.height
应该是 300。否则,将窗口宽度乘以比例。
您问题的另一部分在 How to use GLUT to render to a file
中有答案