从C++;到C# 我将Dmitry V. Sokolov的TyyReNeRever项目复制到C++,而无法复制他编写的最基本的函数来生成一个梯度图像。我有一种感觉,这两种语言使用的字符编码的差异是其背后的原因
原始功能位于,相关部分如下所述从C++;到C# 我将Dmitry V. Sokolov的TyyReNeRever项目复制到C++,而无法复制他编写的最基本的函数来生成一个梯度图像。我有一种感觉,这两种语言使用的字符编码的差异是其背后的原因,c#,c++,gradient,porting,procedural-generation,C#,C++,Gradient,Porting,Procedural Generation,原始功能位于,相关部分如下所述 std::ofstream ofs; // save the framebuffer to file ofs.open("./out.ppm"); ofs << "P6\n" << width << " " << height << "\n255\n"; for (size_t i = 0; i < height*width; ++i) { for (size_t j = 0; j<3
std::ofstream ofs; // save the framebuffer to file
ofs.open("./out.ppm");
ofs << "P6\n" << width << " " << height << "\n255\n";
for (size_t i = 0; i < height*width; ++i) {
for (size_t j = 0; j<3; j++) {
ofs << (char)(255 * std::max(0.f, std::min(1.f, framebuffer[i][j])));
}
}
std::ofs流;//将帧缓冲区保存到文件
ofs.打开(“./out.ppm”);
ofs记录中间结果并进行比较您的C#翻译似乎是正确的,尽管有点低效。我猜frameBuffer
中的值是不同的。编辑:虽然您似乎已将标题中的最大值设置为65535而不是255。记录中间结果并进行比较您的C#翻译似乎是正确的,但效率有点低。我猜frameBuffer
中的值是不同的。编辑:尽管您似乎已将标题中的最大值设置为65535,而不是255。
using (var stream = new FileStream("out.ppm", FileMode.Create))
{
var header = $"P6\n{width} {height}\n65535";
var bytes = Encoding.ASCII.GetBytes(header);
stream.Write(bytes, 0, bytes.Length);
var pixels = new byte[3];
for (var index = 0; index < height * width; index++)
{
var point = frameBuffer[index];
pixels[0] = (byte)(255 * Math.Max(0.0f, Math.Min(1.0f, point.Item1)));
pixels[1] = (byte)(255 * Math.Max(0.0f, Math.Min(1.0f, point.Item2)));
pixels[2] = (byte)(255 * Math.Max(0.0f, Math.Min(1.0f, point.Item3)));
stream.Write(pixels, 0, pixels.Length);
}
}