【问题标题】:Julia set c++ coloursJulia 设置 c++ 颜色
【发布时间】: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 转换等系统来选择颜色。

标签: c++ colors fractals


【解决方案1】:

除了使用查找表的绝妙想法外,您还可以在表中的值之间进行插值,而不仅仅是进行取模运算来选择一个。因此,您可以有一个 5 色查找表,但通过在 5 种颜色之间进行线性插值,将其应用于数百或数千次迭代。例如,如果您的最大迭代次数为 256,而您当前的计算需要 168 次迭代才能逃逸到无穷大,并且您有一个 5 色查找表,您可以这样做以获得颜色:

float lookupVal =  static_cast<float>((colors.size - 1) * i) / MAX_ITERATIONS;
int   lookupIndex  = static_cast<int>(floor(lookupValue));
float fraction  =  lookupVal - floor(lookupVal);
float colorF =  static_cast<float>(colors [ lookupIndex ]) + fraction * static_cast<float>(colors [ lookupIndex + 1 ] - colors [ lookupIndex ]);
uint8_t color = static_cast<uint8_t>(colorF);

如果您的查找表包含 RGB 值而不仅仅是灰度值,您需要为每个颜色通道(红色、绿色和蓝色)计算 colorFcolor

【讨论】:

    【解决方案2】:

    据我所知,90 年代初的分形生成器(例如:Fractint)使用迭代救助索引作为 256 种红-绿-蓝颜色表的索引(这是一个常见的限制,如无论如何,当时的大多数显示器都仅限于这种尺寸的调色板。)

    所以也许你可以定义一个 RGB 颜色表,然后在这些上查找你现在如何执行colors[i % 5];,除了它会输出一个 RGB 三元组colours[i % TABLE_SIZE].red.green.blue .我认为最好从单独的文件中加载您的调色板。

    我一直想知道带有 1000 种颜色调色板的分形会是什么样子。我觉得挺漂亮的。

    编辑:IIRC Fractint 有调色板编辑模式,可以将它们保存到文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 2013-08-27
      • 2020-08-09
      • 2023-03-03
      相关资源
      最近更新 更多