qq_44882950 2019-04-19 21:54 采纳率: 0%
浏览 239

求助大佬 将程序数据写入bmp图片并保存到任意盘

#include
#include
#include
#include
#include
#include

typedef long BOOL;
typedef long LONG;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
typedef unsigned short WORD;

typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMPFILEHEADER_T;

struct BMPFILEHEADER_S
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
};

typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BMPINFOHEADER_T;

typedef struct XET
{
float x;
float dx, ymax;
XET* next;
}AET, NET;

struct point
{
float x;
float y;
};

void generateBmp(BYTE * pData, int width, int height, char * filename) //创建bmp文件
{
int size = width * height * 3;
BMPFILEHEADER_T bfh;
bfh.bfType = 0X4d42;
bfh.bfSize = size
+ sizeof(BMPFILEHEADER_T)
+ sizeof(BMPINFOHEADER_T)
;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = bfh.bfSize - size;

BMPINFOHEADER_T bih;
bih.biSize = sizeof(BMPINFOHEADER_T);
bih.biWidth = width;
bih.biHeight = height;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = size;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;

FILE * fp = fopen(filename, "wb");
if (!fp) return;
fwrite(&bfh, 1, sizeof(BMPFILEHEADER_T), fp);
fwrite(&bih, 1, sizeof(BMPINFOHEADER_T), fp);
fwrite(pData, 1, size, fp);
fclose(fp);

}

int main()
{

struct
{
    BYTE b;
    BYTE g;
    BYTE r;
} pRGB[240][320];
memset(pRGB, 0, sizeof(pRGB));


const int POINTNUM = 6;
point polypoint[POINTNUM] = { 20,30,70,10,130,50,130,110,70,70,20,90, };

void PolyScan();
{
    int MaxY = 0;
    int i;
    for (i = 0; i < POINTNUM; i++)
    {
        if (polypoint[i].y > MaxY)
            MaxY = polypoint[i].y;
    }


    AET *pAET = new AET;
    pAET->next = NULL;

    NET *pNET[1024];
    for (i = 0; i <= MaxY; i++)
    {
        pNET[i] = new NET;
        pNET[i]->next = NULL;
    }
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 1);
    glBegin(GL_POINTS);


    for (i = 0; i < MaxY; i++)              //扫描建立NET表
    {
        for (int j = 0; j < POINTNUM; j++)
        {
            if (polypoint[j].y == i)
            {
                if (polypoint[(j - 1 + POINTNUM) % POINTNUM].y > polypoint[j].y)
                {
                    NET *p = new NET;
                    p->x = polypoint[j].x;
                    p->ymax = polypoint[(j - 1 + POINTNUM) % POINTNUM].y;
                    p->dx = (polypoint[(j - 1 + POINTNUM) % POINTNUM].x - polypoint[j].x) / (polypoint[(j - 1 + POINTNUM) % POINTNUM].y - polypoint[j].y);
                    p->next = pNET[i]->next;
                    pNET[i]->next = p;
                }
                if (polypoint[(j + 1 + POINTNUM) % POINTNUM].y > polypoint[j].y)
                {
                    NET *p = new NET;
                    p->x = polypoint[j].x;
                    p->ymax = polypoint[(j + 1 + POINTNUM) % POINTNUM].y;
                    p->dx = (polypoint[(j + 1 + POINTNUM) % POINTNUM].x - polypoint[j].x) / (polypoint[(j + 1 + POINTNUM) % POINTNUM].y - polypoint[j].y);
                    p->next = pNET[i]->next;
                    pNET[i]->next = p;
                }
            }
        }
    }
    for (i = 0; i <= MaxY; i++)    //建立更新活性边表
    {
        NET *p = pAET->next;
        while (p)
        {
            p->x = p->x + p->dx;
            p = p->next;
        }


        AET *tq = pAET;             //AET按照X值从小到大排序
        p = pAET->next;
        tq->next = NULL;
        while (p)
        {
            while (tq->next && p->x >= tq->next->x)
                tq = tq->next;
            NET *s = p->next;
            p->next = tq->next;
            tq->next = p;
            p = s;
            tq = pAET;
        }


        AET *q = pAET;              //从AET中删除ymax == i的结点
        p = q->next;
        while (p)
        {
            if (p->ymax == i)
            {
                q->next = p->next;
                delete p;
                p = q->next;
            }
            else
            {
                q = q->next;
                p = q->next;
            }
        }


        p = pNET[i]->next;          //将NET中的新点加入AET,并用插入法按X值递增排序
        q = pAET;
        while (p)
        {
            while (q->next && p->x >= q->next->x)
                q = q->next;
            NET *s = p->next;
            p->next = q->next;
            q->next = p;
            p = s;
            q = pAET;
        }


        p = pAET->next;               //颜色填充         
        while (p && p->next)
        {
            for (float j = p->x; j <= p->next->x; j++)
            {
                glVertex2i(static_cast<int>(j), i);
            }
            p = p->next->next;
        }
    }
    glEnd();
    glFlush();
}

void init();
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0, 200, 0, 200);

}

int main(int argc, char* argv);
{
    glutInit(&argc, &argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(800, 800);
    glutCreateWindow(" OpenGL ");

    init();
    glutDisplayFunc(PolyScan);
    glutMainLoop();
}

generateBmp((BYTE*)pRGB, 320, 240, "f:\\rgb.bmp");

}

  • 写回答

1条回答

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 18:41
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮