【问题标题】:What is happening in this pixel-rendering algorithm?这个像素渲染算法发生了什么?
【发布时间】:2014-10-16 20:19:55
【问题描述】:

我刚刚看到了一些关于如何在另一个像素数组上绘制像素数组的代码,如下所示:

public class Bitmap {

private int[] pixels;
private int w, h;

public void draw(Bitmap b, int xp, int yp) {
    int x0 = xp;
    int x1 = xp+b.w;
    int y0 = yp;
    int y1 = yp+b.h;

    if(x0 < 0) x0 = 0;
    if(x1 > w) x1 = w;
    if(y0 < 0) y0 = 0;
    if(y1 > h) y1 = h;

    for (int y = y0; y < y1; y++) {
        int sp = (y - yp) * b.w - xp;
        int dp = (y) * w;

        for (int x = x0; x < x1; x++) {
            int c = b.pixels[sp + x];
            if (c < 0) pixels[dp + x] = b.pixels[sp + x];
        }
    }
}

}

如您所见,可以在另一个位图之上的特定坐标上绘制位图对象。

我没有得到的是两个 for 循环。我知道,外部for循环是绘制的Bitmap的y轴,并启动内部for循环来绘制Bitmap的x轴。

现在我想到了这个:

int sp = (y - yp) * b.w - xp;
int dp = (y) * w;

sp 和 dp 究竟代表什么?后面的“c”是什么意思

int c = b.pixels[sp + x];
if (c < 0) pixels[dp + x] = b.pixels[sp + x];

?

提前致谢,致以最诚挚的问候

【问题讨论】:

    标签: java bitmap pixel


    【解决方案1】:

    给定算法,我们可以猜出原作者的想法:

    • sp 是“源位置”:源位图中行的开始
    • dp 是“目标位置”:目标位图中行的开头
    • c 是“颜色”:源像素值(负值是透明的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-14
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多