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



将原始像素图的值降低到50%很容易。我只需在地图上滑动一个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。它应该适用于任何通用的像素映射。我确实觉得代码很难看,因为我把图像看作是2d数组。我不明白为什么所有的图像代码都映射到1d数组。这很难想象。这适用于任何调整大小的操作。

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并返回,当你缩小比例时,你会有一个更暗的图像。

最新更新