0

Now I want to scale a background image, stretch it until it fits the while size of a X11 window. Is there something the XLib API offers for that or do I have to do it "manually"?
I have the data of the image stored in a vector of unsigned chars and I can play with that before I set up the background but I don't know how would you do that. The image could be smaller or bigger than the window. Some hints or examples?

NEW CODE

I found some code here

I tried to adapt it. My image data is stored in a vector <unsigned char> variable

vector<unsigned char> image;
int width = 600;
int height = 400;
int new_width = 820;
int new_height = 420;
lanczos_size = 3.0;

double col_rat = static_cast<double>(width) / static_cast<double>(new_width);
double row_rat = static_cast<double>(height) / static_cast<double>(new_height);

vector<unsigned char> himage(new_width * height);

for (unsigned int r=0; r<height; r++)
{
    for (unsigned int c=0; c<new_width; c++)
    {
        double x = static_cast<double>(c) * col_rat;
        int floor_X = static_cast<int>(x);
        himage[r * c] = 0.0;
        double weight = 0.0;
        // Add up terms across the filter
        for (unsigned i=floor_X-lanczos_size + 1; i<floor_X+lanczos_size; i++) {
            if (i >= 0 && i < width) {
                double lanc_term = LanczosFilter(X - i);
                himage[r * c] += image[r * i] * lanc_term;
                Weight += lanc_term;
            }
        }
        // Normalize the filter
        himage[r * c] /= Weight;
        // Strap the pixel values to valid values
        himage[r * c] = (himage[r * c] > 1.0) ? 1.0 : himage[r * c];
        himage[r * c] = (himage[r * c] < 0.0) ? 0.0 : himage[r * c];
        }
    }
    // Now apply a vertical filter to the horiz image
    vector<unsigned char> newimage(new_width * new_height);
    for (unsigned int r=0; r<new_height; r++)
    {
        double x = static_cast<double>(r) * row_rat;
        int floor_X = static_cast<int>(x);
        for (unsigned int c = 0; c < new_width; c++)
        {
            newimage[r * c] = 0.0;
            double weight = 0.0;
            for (unsigned int i = floor_X-lanczos_size+1; i<FloorX+lanczos_size; i++)
            {
                if (i >= 0 && i < height) {
                    double lanc_term = LanczosFilter(X - i);
                    newimage[r * c] += himage[i * c] * lanc_term;
                    weight += lanc_term;
                }
            }
            newimage[r * c] /= weight;
            newimage[r * c] = (newimage[r * c] > 1.0) ? 1.0 : newimage[r * c];
            newimage[r * c] = (newimage[r * c] < 0.0) ? 0.0 : newimage[r * c];
        }
    }
    // Now we store the new data
    image = newimage;
    // Save the new size
    width = new_width;
    height = new_height;

The Lanczos algorithm was also taken from here but I don't think it has a problem. Now, when I use this to re-size my image I get a very weird result so now I just wonder where is the problem. Maybe some faster eye will see it before me.

The data of my images is stored as ARGBARGBARGB

Community
  • 1
  • 1
ali
  • 10,927
  • 20
  • 89
  • 138

0 Answers0