html - 如何在每次 Canvas 变换后实现 Lanczos 重采样而无需制作新 Canvas ?

标签 html image-processing canvas lanczos

更新:一旦我让这个演示开始工作...老天,它很慢,只有 2 级渲染(当图像大约为 1000x2000 像素时)大约需要 12-16 秒。这甚至不值得为此烦恼。

我在此处的最佳答案中发现了这段非常棒且充满希望的代码:Resizing an image in an HTML5 canvas

//returns a function that calculates lanczos weight
function lanczosCreate(lobes){
  return function(x){
    if (x > lobes) 
      return 0;
    x *= Math.PI;
    if (Math.abs(x) < 1e-16) 
      return 1
    var xx = x / lobes;
    return Math.sin(x) * Math.sin(xx) / x / xx;
  }
}

//elem: canvas element, img: image element, sx: scaled width, lobes: kernel radius
function thumbnailer(elem, img, sx, lobes){ 
    this.canvas = elem;
    elem.width = img.width;
    elem.height = img.height;
    elem.style.display = "none";
    this.ctx = elem.getContext("2d");
    this.ctx.drawImage(img, 0, 0);
    this.img = img;
    this.src = this.ctx.getImageData(0, 0, img.width, img.height);
    this.dest = {
        width: sx,
        height: Math.round(img.height * sx / img.width),
    };
    this.dest.data = new Array(this.dest.width * this.dest.height * 3);
    this.lanczos = lanczosCreate(lobes);
    this.ratio = img.width / sx;
    this.rcp_ratio = 2 / this.ratio;
    this.range2 = Math.ceil(this.ratio * lobes / 2);
    this.cacheLanc = {};
    this.center = {};
    this.icenter = {};
    setTimeout(this.process1, 0, this, 0);
}

thumbnailer.prototype.process1 = function(self, u){
    self.center.x = (u + 0.5) * self.ratio;
    self.icenter.x = Math.floor(self.center.x);
    for (var v = 0; v < self.dest.height; v++) {
        self.center.y = (v + 0.5) * self.ratio;
        self.icenter.y = Math.floor(self.center.y);
        var a, r, g, b;
        a = r = g = b = 0;
        for (var i = self.icenter.x - self.range2; i <= self.icenter.x + self.range2; i++) {
            if (i < 0 || i >= self.src.width) 
                continue;
            var f_x = Math.floor(1000 * Math.abs(i - self.center.x));
            if (!self.cacheLanc[f_x]) 
                self.cacheLanc[f_x] = {};
            for (var j = self.icenter.y - self.range2; j <= self.icenter.y + self.range2; j++) {
                if (j < 0 || j >= self.src.height) 
                    continue;
                var f_y = Math.floor(1000 * Math.abs(j - self.center.y));
                if (self.cacheLanc[f_x][f_y] == undefined) 
                    self.cacheLanc[f_x][f_y] = self.lanczos(Math.sqrt(Math.pow(f_x * self.rcp_ratio, 2) + Math.pow(f_y * self.rcp_ratio, 2)) / 1000);
                weight = self.cacheLanc[f_x][f_y];
                if (weight > 0) {
                    var idx = (j * self.src.width + i) * 4;
                    a += weight;
                    r += weight * self.src.data[idx];
                    g += weight * self.src.data[idx + 1];
                    b += weight * self.src.data[idx + 2];
                }
            }
        }
        var idx = (v * self.dest.width + u) * 3;
        self.dest.data[idx] = r / a;
        self.dest.data[idx + 1] = g / a;
        self.dest.data[idx + 2] = b / a;
    }

    if (++u < self.dest.width) 
        setTimeout(self.process1, 0, self, u);
    else 
        setTimeout(self.process2, 0, self);
};
thumbnailer.prototype.process2 = function(self){
    self.canvas.width = self.dest.width;
    self.canvas.height = self.dest.height;
    self.ctx.drawImage(self.img, 0, 0);
    self.src = self.ctx.getImageData(0, 0, self.dest.width, self.dest.height);
    var idx, idx2;
    for (var i = 0; i < self.dest.width; i++) {
        for (var j = 0; j < self.dest.height; j++) {
            idx = (j * self.dest.width + i) * 3;
            idx2 = (j * self.dest.width + i) * 4;
            self.src.data[idx2] = self.dest.data[idx];
            self.src.data[idx2 + 1] = self.dest.data[idx + 1];
            self.src.data[idx2 + 2] = self.dest.data[idx + 2];
        }
    }
    self.ctx.putImageData(self.src, 0, 0);
    self.canvas.style.display = "block";
}

...

img.onload = function() {
  var canvas = document.createElement("canvas");
  new thumbnailer(canvas, img, 188, 3); //this produces lanczos3
  //but feel free to raise it up to 8. Your client will appreciate
  //that the program makes full use of his machine.
  document.body.appendChild(canvas);
}

但是,此实现加载图像并渲染它,故事结束。

我一直在尝试重新实现此代码,以便它在每次缩放现有 Canvas (思考,放大和缩小图像或文档)时进行过滤,而无需加载新图像或创建新图像 Canvas 。

我怎样才能让它以这种方式工作?或者这甚至可能吗?

最佳答案

你想要做的是像 singleton 这样的东西来重用你的 Canvas 对象。这将使您节省每次创建新 Canvas 对象的成本,并且您将重复使用相同的对象

function getCanvas(){ 
  var canvas; 
  if (typeof canvas === "undefined"){ canvas = document.createElement("canvas");}
  return canvas;
}
img.onload = function() {
  var canvas = getCanvas("canvas");
  .... THE REST OF YOUR CODE .......
}

.

然而,这不会减慢您的代码,图像缩放算法是非常繁重的算法,密集使用 cpu“通常在非常低的水平上使用 gpu 加速”,并使用像 multiple buffer 这样的高级技术r 等等。这是关于图像缩放如何工作的 interesting tutorial in java.net,它在 Java 中,但您可以插入任何语言。

Javascript 还没有为这种技术做好准备,因此我建议您使用 canvas api 中可用的 transformations,因为在教程中您阅读的有效方法是使用 canvas2Dcontext。

var ctx = canvas.getContext("2d"); 
ctx.scale(2,2);

关于html - 如何在每次 Canvas 变换后实现 Lanczos 重采样而无需制作新 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537383/

相关文章:

javascript - Web 应用程序上的图像编辑

java - 电影绘制到 Canvas 时的 Android 错误信号 11

javascript - Canvas 上绘制的图形出现一小会儿,然后就消失了

html - 为什么 HTML/JavaScript/CSS 不是编译语言,而且它们永远是?

html - 如何让我的导航按钮保持事件状态

image-processing - 使用 ImageMagick 将图像转换为灰度非常暗

php显示所有不重复的图像

javascript - HTML5 样板与 HTML5 重置

html - 将最后一列显示为新行

c++ - 如何在opencv中读取具有透明像素的tif图像