2

将原始像素图减少到 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 上进行双线性插值。它应该适用于任何通用像素图。我确实发现代码很难看,因为我将图像视为二维数组。我不明白为什么所有图像代码都映射到一维数组。更难想象。这适用于任何调整大小。

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

1 回答 1

0

这是一些缩小比例的伪代码。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 并返回的情况下执行此操作,则按比例缩小时您将获得较暗的图像。

于 2013-08-02T15:36:52.923 回答