【问题标题】:How to scale width of a RGB image in a byte array如何在字节数组中缩放 RGB 图像的宽度
【发布时间】:2020-06-09 13:52:22
【问题描述】:

今天我的大脑工作不正常,我似乎无法弄清楚这一点。

我有一个 RGBA 图像数据存储在 Uint8Array() 中,只需要缩放宽度。

var w = 160;
var h = 200;
var depth=4;
var pixels = new Uint8Array(w*h*depth);

我需要将像素 array 缩放到 320x200 并且我所做的每一次尝试都以乱码告终。

【问题讨论】:

  • 扫描每一行并将每个 4 字节的块复制两次到目标。

标签: javascript arrays image-processing scale


【解决方案1】:

感谢Yves Daoust,我重新尝试通过将 4 的每一块复制到目的地来解决这个问题,现在我得到了它的工作。所以谢谢 Yves :) 我不知道我之前做错了什么。

这是我最终得到的工作代码。我 100% 确信它可以做得更好,但在这一点上我很满意 :)

Utils.prototype.scalePixelsInWidth = function(pixels) {

  var w = 320;
  var h = 200;
  var scanlineWidth = w*4;
  var scaledPixels = new Uint8Array(w*h*4);

  var a = 0;
  for(let row=0;row<h;row++) {
    var col2 = 0;
    for(let col=0;col<w;col++) {
      var srcIndex = col2*4 + (row*(w/2)*4);
      var destIndex = col*4 + (row * scanlineWidth);
      scaledPixels[destIndex+0] = pixels[srcIndex+0];
      scaledPixels[destIndex+1] = pixels[srcIndex+1];
      scaledPixels[destIndex+2] = pixels[srcIndex+2];
      scaledPixels[destIndex+3] = pixels[srcIndex+3];
      a++;
      if (a > 1) {
        a = 0;
        col2++;
      }
    }
  }

  return scaledPixels;

}

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2012-08-04
    • 2020-02-08
    • 2013-11-05
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多