freeimage写入像素

时间:2018-07-12 11:09:17

标签: c++ image freeimage stb-image

我正在尝试使用freeimage在文件中写入一些顶点(但我也可以使用stb_image解决方案)。

我正在尝试使用《计算机图形学:原则与实践》第3版第3章中的代码,清单3.6。

我不确定如何处理freeimage库。

即使我将背景色设置为红色,我也会收到黑色。

此外,即使我正在使用setpixelcolor写入文件,我仍然会收到一个黑色的窗口。 顶点应显示为绿色点。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <FreeImage.h>


typedef struct Point
{
    float x, y, z;
}Point;

int main() 
{
    int width = 400;
    int height = 300; 
    int bpp = 24; 

    FreeImage_Initialise();
    FIBITMAP *img = FreeImage_Allocate(width, height, bpp);
    RGBQUAD color;

    if (!img)
        exit(1);

    // build a table of vertices
    int nbOfPoints = 8;
    int nbOfEdges = 12;
    float vertices[nbOfPoints][3] = 
    {
        {-0.5, -0.5, 2.5},
        {-0.5, 0.5, 2.5},
        {0.5, 0.5, 2.5},
        {0.5, -0.5, 2.5},
        {-0.5, -0.5, 3.5},
        {-0.5, 0.5, 3.5},
        {0.5, 0.5, 3.5},
        {0.5, -0.5, 3.5}
    };

    // build a table of edges
    int edges[nbOfEdges][2] = 
    {
        {0, 1},
        {1, 2},
        {2, 3},
        {3, 0},
        {0, 4},
        {1, 5},
        {2, 6},
        {3, 7},
        {4, 5},
        {5, 6},
        {6, 7},
        {7, 4}
    };

    float xmin = -0.5;
    float xmax = 0.5;
    float ymin = -0.5;
    float ymax = 0.5;

    Point *pictureVertices = new Point[nbOfPoints * width * height];

    float scale = 100;

    BYTE *pixelData = NULL;

    RGBQUAD backg_color;// = new RGBQUAD(Color.Red);
    backg_color.rgbRed = 1.0;
    backg_color.rgbGreen = 0;
    backg_color.rgbBlue = 0;
    FreeImage_SetBackgroundColor(img, &backg_color);
    for (int j = height-1; j >= 0; --j)
    {
        for (int i = 0; i < width; ++i)
        {
            for (int p = 0; p < nbOfPoints; ++p)
            {
                float x = vertices[p][0];
                float y = vertices[p][1];
                float z = vertices[p][2];

                float xprime = x / z;
                float yprime = y / z;

                pictureVertices[p].x = scale * (1.0 - (xprime - xmin) / (xmax - xmin));
                pictureVertices[p].y = scale * (yprime - ymin) / (ymax - ymin);

                color.rgbRed = 0;
                color.rgbGreen = 1.0;
                color.rgbBlue = 0;
                FreeImage_SetPixelColor(img, pictureVertices[p].x, pictureVertices[p].y, &color);
                //FreeImage_SetPixelIndex(img,  (int)pictureVertices[i].x,  (int)pictureVertices[i].y, pixelData);
            }
        }
    }
    FreeImage_Save(FIF_PNG, img, "test.png", 0);
    FreeImage_DeInitialise();


    std::cout << "\n";

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您应该使用范围0..255,而不是0..1:)