免费图像写入像素

freeimage write to pixels

本文关键字:像素 图像 免费      更新时间:2023-10-16

我正在尝试使用freeimage在文件中写入一些顶点(但我也对使用stb_image的解决方案持开放态度(。

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

我不知道如何处理免费图片库。

即使我设置了一个红色的背景色,我收到的却是黑色。

此外,即使我使用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;
}

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