0

I'm trying to generate png with different resolution. But if i use dynamic array its generate only gray area. This is source of my code (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);
}

1 Answers1

0

Apparently I don’t understand something yet in two-dimensional arrays. Solved the problem through a one-dimensional array:

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);
}