gpt4 book ai didi

c - 在 C 中将原始 24 位像素图缩放到大于 50% 的算法

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:29 24 4
gpt4 key购买 nike

将原始像素图的值减少到 50% 很容易。我只是在 map 上滑动一个 2x2 的正方形,然后对 4 个像素的 RGB 分量进行平均,如下所示:

 img = XGetImage(d_remote,RootWindow(d_remote,0),0,0,attr.width,attr.height,XAllPlanes(),ZPixmap);

int i;
int j;
for(i=0;i<attr.height;i=i+2){
for(j=0;j<attr.width;j=j+2) {
unsigned long p1 = XGetPixel(img, j, i);
unsigned long p1R = p1 & 0x00ff0000;
unsigned long p1G = p1 & 0x0000ff00;
unsigned long p1B = p1 & 0x000000ff;

unsigned long p2 = XGetPixel(img, j+1, i);
unsigned long p2R = p2 & 0x00ff0000;
unsigned long p2G = p2 & 0x0000ff00;
unsigned long p2B = p2 & 0x000000ff;

unsigned long p3 = XGetPixel(img, j, i+1);
unsigned long p3R = p3 & 0x00ff0000;
unsigned long p3G = p3 & 0x0000ff00;
unsigned long p3B = p3 & 0x000000ff;

unsigned long p4 = XGetPixel(img, j+1, i+1);
unsigned long p4R = p4 & 0x00ff0000;
unsigned long p4G = p4 & 0x0000ff00;
unsigned long p4B = p4 & 0x000000ff;

unsigned long averageR = (p1R+p2R+p3R+p4R)/4 & 0x00ff0000;
unsigned long averageG = (p1G+p2G+p3G+p4G)/4 & 0x0000ff00;
unsigned long averageB = (p1B+p2B+p3B+p4B)/4 & 0x000000ff;

int average = averageR | averageG | averageB;

XPutPixel(newImg, j/2, i/2, average);

}
}

这将使 500x500 的像素图变成 250x250 的像素图。这是 50% 的减少。如果我想将它缩放 20% 怎么办?例如,我希望我的 500x500 图片变成 400x400?我可以滑动的最小正方形是 2x2。我不明白我怎么能得到一个不是 2 的完美幂的减少。

解决方案:

努力怎么样?我修改了一个脚本,我发现它可以在 XImages 上进行双线性插值。它应该适用于任何通用像素图。我确实发现代码很难看,因为我将图像视为二维数组。我不明白为什么所有图像代码都映射到一维数组。更难想象。这适用于任何调整大小。

void resize(XImage* input, XImage* output, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight)
{
int a, b, c, d, x, y, index;
float x_ratio = ((float)(sourceWidth - 1)) / targetWidth;
float y_ratio = ((float)(sourceHeight - 1)) / targetHeight;
float x_diff, y_diff, blue, red, green ;
int offset = 0 ;

int i=0;
int j=0;

int* inputData = (int*)input->data;
int* outputData = (int*)output->data;
for (i = 0; i < targetHeight; i++)
{
for (j = 0; j < targetWidth; 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 * sourceWidth + x) ;
a = inputData[index] ;
b = inputData[index + 1] ;
c = inputData[index + sourceWidth] ;
d = inputData[index + sourceWidth + 1] ;

// blue element
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
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
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);


outputData[offset++] = (int)red << 16 | (int)green << 8 | (int)blue;
}
}
}

最佳答案

这里是一些用于缩减尺度的伪代码。 WS,HS 是目标图像尺寸 WB,HB 是源尺寸。 WS小于WB,HS小于HB。

double row[WB];
double Xratio= WB/WS;
double Yratio= HB/HS;

double curYratio= Yratio;
double remainY= Yratio - floor(Yratio);
double remainX= Xratio - floor(Xratio);
double curXratio;

double rfac, cfac;

int icol,irow, orow, ocol;

zero-out row

orow= 0;

for(irow=0..HB-1)
{
// we find out how much of this row we will add to the current sum
if (curYratio>=1.0) rfac= 1.0; else rfac= curYratio;

// we add it
for(icol=0..WB) row[icol] += rfac * input[irow][icol];

// we reduce the total weight
curYratio -= rfac;

// if the total weight is now zero, we have a complete row,
// otherwise we still need some of the next row
if (curYratio!=0.0) continue;

// we have a complete row, compute the weighted average
for(icol=0..WB-1) row[icol]/= Yratio;

// now we can scale the row in horizontal

curXratio= Xratio;
ocol= 0;
double pixel= 0.0;
for(icol=0..WB-1)
{
if (curXratio>=1.0) cfac= 1.0; else cfac= curXratio;
pixel+= row[icol]*cfac;
curXratio -= cfac;
if (curXratio!=0) continue;

// now we have a complete pixel
out[orow][ocol]= pixel / Xratio;
pixel= remainX * row[icol];
curXratio= Xratio - remainX;
ocol++;
}
orow++;

// let's put the remainder of the last input row into 'row'

for(icol=0..WB-1) row[i]= remainY*input[irow][icol];
curYratio= Yratio - remainY;
}

这比我想象的要花更长的时间,但就是这样。无论如何,直接在输入位图上运行它不是很明智。在进行任何运算之前,您应该将每个像素值转换为其 sRGB 值。普通位图中的像素值只是应该在计算中使用的实际值的名称。在维基百科上查找 sRGB,它有很好的信息。

如果您在不转换为 sRGB 并返回的情况下执行此操作,则当您缩小时图像会更暗。

关于c - 在 C 中将原始 24 位像素图缩放到大于 50% 的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17615740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com