【问题标题】:Generate gradient with CImg and dynamic array使用 CImg 和动态数组生成梯度
【发布时间】:2020-03-02 14:29:54
【问题描述】:

我正在尝试生成具有不同分辨率的 png。但是如果我使用动态数组,它只会生成灰色区域。这是我的代码的来源 (C++ 16 bit grayscale gradient image from 2D array)

void generate_horizontal_gradient(char fileName[], int width, int height, int offset, bool direction)
{
    unsigned short** buffer = new unsigned short* [height];
    for (int i = 0; i < height; i++)
    {
    buffer[i] = new unsigned short[width];
    }

    for (int i = 0; i < height; i++)
    {
        unsigned short temp_data = 65535;
        if (direction == true) {
            for (int j = width; j > 0; j--)
            {
                buffer[i][j] = temp_data;
                if (j < width - offset)
                {
                    temp_data -= 65535 / (width - offset);
                }
            }
        }
        else
        {
            for (int j = 0; j < width; j++)
            {
                buffer[i][j] = temp_data;
                if (j > offset)
                {
                    temp_data -= 65535 / (width - offset);
                }
            }
        }
    }
    auto hold_arr = (unsigned short*) &buffer[0][0];
    cimg_library::CImg<unsigned short> img(hold_arr, width, height);
    img.save_png(fileName);
}

【问题讨论】:

    标签: c++ arrays png gradient cimg


    【解决方案1】:

    显然我还不了解二维数组中的某些内容。通过一维数组解决问题:

    void generate_horizontal_gradient(char fileName[], int width, int height, int offset, bool direction)
    {   
    unsigned short* buffer = new unsigned short[height * width];
    
    //Add values to array.
    for (int i = 0; i < height; i++)
    {
        unsigned short temp_data = 65535;
        if (direction == true) {
            for (int j = width; j > 0; j--)
            {
                buffer[i* width +j] = temp_data;
                if (j < width - offset) temp_data -= 65535 / (width - offset);          
            }
        }
        else
        {
            for (int j = 0; j < width; j++)
            {
                buffer[i * width + j] = temp_data;
                if (j > offset) temp_data -= 65535 / (width - offset);
            }
        }
    }
    
    unsigned short* hold_arr = (unsigned short*)& buffer[0*0];
    cimg_library::CImg<unsigned short> img(buffer, width, height);
    img.save_png(fileName);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2021-11-22
      • 2023-02-10
      相关资源
      最近更新 更多