Question

I am trying to re-size an image by using the bilinear technique I found here but I don't see anything but a black image. So, in first place I have my image decoded with LodePNG and the pixels go into a vector<unsigned char> variable. It says that they are stored as RGBARGBA but when I tried to apply the image to a X11 window I realized they were stored as BGRABGRA. I don't know if is the X11 API which changes the order or the LodePNG decoder. Anyway, before anything, I convert the BGR to RGB:

// Here is where I have the pixels stored
vector<unsigned char> Image;

// Converting BGRA to RGBA, or vice-versa, I don't know, but it's how it is shown
// correctly on the window
unsigned char red, blue;
unsigned int i;
for(i=0; i<Image.size(); i+=4)
{
    red  = Image[i + 2];
    blue = Image[i];
    Image[i] = red;
    Image[i + 2] = blue;
}

So, now I am trying to change the size of the image, before applying it to the window. The size would be the size of the window (stretch it). I firstly try to convert the RGBA to int values, like this:

vector<int> IntImage;
for(unsigned i=0; i<Image.size(); i+=4)
{
    IData.push_back(256*256*this->Data[i+2] + 256*this->Data[i+1] + this->Data[i]);
}

Now I have this function from the link I specified above, which is supposed to do the interpolation:

vector<int> resizeBilinear(vector<int> pixels, int w, int h, int w2, int h2) {
    vector<int> temp(w2 * h2);
    int a, b, c, d, x, y, index ;
    float x_ratio = ((float)(w-1))/w2 ;
    float y_ratio = ((float)(h-1))/h2 ;
    float x_diff, y_diff, blue, red, green ;

    for (int i=0;i<h2;i++) {
        for (int j=0;j<w2;j++) {
            x = (int)(x_ratio * j) ;
            y = (int)(y_ratio * i) ;
            x_diff = (x_ratio * j) - x ;
            y_diff = (y_ratio * i) - y ;
            index = (y*w+x) ;                
            a = pixels[index] ;
            b = pixels[index+1] ;
            c = pixels[index+w] ;
            d = pixels[index+w+1] ;

            // blue element
            // Yb = Ab(1-w)(1-h) + Bb(w)(1-h) + Cb(h)(1-w) + Db(wh)
            blue = (a&0xff)*(1-x_diff)*(1-y_diff) + (b&0xff)*(x_diff)*(1-y_diff) +
                   (c&0xff)*(y_diff)*(1-x_diff)   + (d&0xff)*(x_diff*y_diff);

            // green element
            // Yg = Ag(1-w)(1-h) + Bg(w)(1-h) + Cg(h)(1-w) + Dg(wh)
            green = ((a>>8)&0xff)*(1-x_diff)*(1-y_diff) + ((b>>8)&0xff)*(x_diff)*(1-y_diff) +
                    ((c>>8)&0xff)*(y_diff)*(1-x_diff)   + ((d>>8)&0xff)*(x_diff*y_diff);

            // red element
            // Yr = Ar(1-w)(1-h) + Br(w)(1-h) + Cr(h)(1-w) + Dr(wh)
            red = ((a>>16)&0xff)*(1-x_diff)*(1-y_diff) + ((b>>16)&0xff)*(x_diff)*(1-y_diff) +
                  ((c>>16)&0xff)*(y_diff)*(1-x_diff)   + ((d>>16)&0xff)*(x_diff*y_diff);

            temp.push_back( 
                    ((((int)red)<<16)&0xff0000) |
                    ((((int)green)<<8)&0xff00) |
                    ((int)blue) |
                    0xff); // hardcode alpha ;
        }
    }
    return temp;
}

and I use it like this:

vector<int> NewImage = resizeBilinear(IntData, image_width, image_height, window_width, window_height);

which is supposed to return me the RGBA vector of the re-sized image. Now I am changing back to RGBA (from int)

Image.clear();

for(unsigned i=0; i<NewImage.size(); i++)
{
    Image.push_back(NewImage[i] & 255);
    Image.push_back((NewImage[i] >> 8) & 255);
    Image.push_back((NewImage[i] >> 16) & 255);
    Image.push_back(0xff);
}

and what I get is a black window (the default background color), so I don't know what am I missing. If I comment out the line where I get the new image and just convert back to RGBA the IntImage I get the correct values so I don't know if it is the messed up RGBA/int <> int/RGBA. I'm just lost now. I know this can be optimized/simplified but for now I just want to make it work.

Was it helpful?

Solution

The array access in your code is incorrect:

vector<int> temp(w2 * h2); // initializes the array to contain zeros
...
temp.push_back(...); // appends to the array, leaving the zeros unchanged

You should overwrite instead of appending; for that, calculate the array position:

temp[i * w2 + j] = ...;

Alternatively, initialize the array to an empty state, and append your stuff:

vector<int> temp;
temp.reserve(w2 * h2); // reserves some memory; array is still empty
...
temp.push_back(...); // appends to the array
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top