【发布时间】:2019-04-15 15:20:42
【问题描述】:
我目前正在执行一项任务,我必须在 C++ 中以顺序、并行和 OpenCL 的方式生成一个 Julia 集。我已经设法制作了一张图像,但是我使用颜色的方式非常无效,关于目前如何改进代码的颜色部分有什么想法吗?下面是我的代码的顺序部分,任何有助于改进我设置颜色的方式将不胜感激
void sequentialJulia(const complex<float> C, const UINT size = 1000,
const UINT MAX_ITERATIONS = 100, const float limit = 1.7f) {
int start_s = clock();// starts the timer
// Setup output image
fipImage outputImage;
outputImage = fipImage(FIT_BITMAP, size, size, 24);
UINT bytesPerElement = 3;
BYTE* outputBuffer = outputImage.accessPixels();
vector<int> colors{ 100, 140, 180, 220, 225 };// this sets the intsity of the image, if i was to remove 225 the image would be darker
//vector<int> colors{9, 19, 29, 39, 49 }; //THIS DOESNT WORK DO NOT UNCOMMENT
//RGBQUAD color;
complex<float> Z;
std::cout << "Processing...\n";
for (UINT y = 0; y < size; y++) {
//tracking progress;
cout << y * 100 / size << "%\r";
cout.flush();
for (UINT x = 0; x < size; x++) {
Z = complex<float>(-limit + 2.0f * limit / size * x, -limit + 2.0f * limit / size * y);
UINT i;
for (i = 0; i < MAX_ITERATIONS; i++) {
Z = Z * Z + C;
if (abs(Z) > 2.0f) break;
}
if (i < MAX_ITERATIONS ) { //only changing red byte
// bytes per element 9 = blue
// bytes per element 2 = red
// bytes per element 7 = green
outputBuffer[( y * size + x) * bytesPerElement + 9] = colors[i % 5];
}
}
}
cout << "Saving image...\n";
ostringstream name;
name << "..\\Images\\" << C << " size=" << size << " mIterations=" << MAX_ITERATIONS << " sequential19.png" ;
cout << "saving in: " << name.str().c_str() << "\n";
outputImage.save(name.str().c_str());
cout << "...done\n\n";
int stop_s = clock();
cout << "time: " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << endl;// stops the timer once code has executed
}
【问题讨论】:
-
这个问题需要做一些工作。一方面,这根本不是问题。您正在寻找代码审查,即codereview.stackexchange.com 您也将从stackoverflow.com/help/mcve 中受益。之后,尝试向我们解释我们不熟悉的术语。不是每个 C++ 开发人员都知道 Julia 集是什么,包括我自己。
-
我不知道该怎么说。我的代码生成了一个 Julia 集,我可以将其更改为生成一个红色绿色或蓝色的。但是我希望通过添加更多颜色来改进它,这是否更有意义?
-
固定格式
-
不清楚这里发生了什么。据我了解,这里的颜色是 24 位 RGB,但您设置它们的方式简直超出了我的想象。您是否尝试将颜色直接设置到位图缓冲区而不是使用正确的 API?你现在,喜欢
setPixelColor()。您还可以使用 HSV 到 RGB 转换等系统来选择颜色。