现在我想缩放背景图像,将其拉伸直到它适合X11窗口的大小。是否有XLib API为此提供的功能,还是必须“手动”执行此操作? 我有一个存储在无符号字符向量中的图像数据,我可以在设置背景之前使用它,但我不知道你会怎么做。图像可能比窗口更小或更大。一些提示或例子?
新代码
我找到了一些代码here
我试图改编它。我的图片数据存储在vector <unsigned char>
变量
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;
Lanczos算法也取自here,但我不认为它有问题。现在,当我使用它来重新调整我的图像大小时,我得到一个非常奇怪的结果,所以现在我只是想知道问题出在哪里。也许一些更快的眼睛会在我面前看到它。
我的图片数据存储为ARGBARGBARGB