开发者

Pixel Replication bug

My Old image is being copied till the middle of the new image

PImage toPixelReplication(PImage p)
{
 PImage newImage = new PImage((p.width*2),(p.height*2));
 newImage.loadPixels();
 for(int i = 0; i < p.width; i++开发者_运维问答)
 {
  for(int j = 0; j < p.height; j++)
  {
   newImage.pixels[((2*i))*p.width + (j*2)]= p.pixels[(i)*p.width + j];
  }
 }
 newImage.updatePixels();
 return newImage;
}


You are missing a factor two since the new image's width is twice the old one:

newImage.pixels[((2*j))*(2*p.width) + (i*2)]= p.pixels[(j)*p.width + i];

I also exchange i and j because they should be the other way round in the pixel calculations (i denotes the column, j the row).

Note: this method fills only every second pixel in every second row. If you want the full image filled (each pixel doubled horizontally and vertically) you may use the following lines:

for(int i = 0; i < 2*p.width; i++) {
    for(int j = 0; j < 2*p.height; j++) {
        newImage.pixels[j*2*p.width + i]= p.pixels[(j/2)*p.width + (i/2)];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜