双线性用C和RGBA像素矢量重新调整大小

双线性用C和RGBA像素矢量重新调整大小,第1张

概述我试图通过使用我发现 here的双线性技术重新调整图像大小,但除了黑色图像我什么也看不到. 所以,首先我用 LodePNG解码我的图像,像素进入矢量< unsigned char>变量.它说它们存储为RGBARGBA但是当我尝试将图像应用到X11窗口时,我意识到它们存储为BGRABGRA.我不知道是否是更改顺序的X11 API或LodePNG解码器.无论如何,在任何事情之前,我将BGR转换为RGB 我试图通过使用我发现 here的双线性技术重新调整图像大小,但除了黑色图像我什么也看不到.
所以,首先我用 LodePNG解码我的图像,像素进入矢量< unsigned char>变量.它说它们存储为RGbarGBA但是当我尝试将图像应用到X11窗口时,我意识到它们存储为BGRABGRA.我不知道是否是更改顺序的X11 API或LodePNG解码器.无论如何,在任何事情之前,我将BGR转换为RGB:

// Here is where I have the pixels storedvector<unsigned char> Image;// Converting BGRA to RGBA,or vice-versa,I don't kNow,but it's how it is shown// correctly on the windowunsigned 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;}

所以,现在我尝试在将图像应用到窗口之前更改图像的大小.大小将是窗口的大小(拉伸它).
我首先尝试将RGBA转换为int值,如下所示:

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

现在我从上面指定的链接中获得了这个函数,它应该进行插值:

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

我这样使用它:

vector<int> NewImage = resizeBilinear(IntData,image_wIDth,image_height,window_wIDth,window_height);

应该返回重新调整大小的图像的RGBA矢量.现在我正在改回RGBA(来自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);}

我得到的是一个黑色的窗口(默认的背景颜色),所以我不知道我错过了什么.如果我注释掉我获得新图像的行并且只是转换回RGBA Intimage我得到正确的值,所以我不知道它是否是混乱的RGBA / int<> INT / RGBA.我现在迷失了.我知道这可以优化/简化,但现在我只想让它工作.

解决方法 您的代码中的数组访问不正确:

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

你应该覆盖而不是追加;为此,计算阵列位置:

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

或者,将数组初始化为空状态,并附加你的东西:

vector<int> temp;temp.reserve(w2 * h2); // reserves some memory; array is still empty...temp.push_back(...); // appends to the array
总结

以上是内存溢出为你收集整理的双线性用C和RGBA像素矢量重新调整大小全部内容,希望文章能够帮你解决双线性用C和RGBA像素矢量重新调整大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1227085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存