理解 Ruby 的 ChunkyPNG 并将其转换为 Javascript 等价物

Understanding and Converting Ruby's ChunkyPNG to Javascript Equivalent

我正在将一些 RUBY 代码转换为 Javascript。 此 RUBY 代码使用 ChunkyPNG Ruby 库。

这是Ruby代码:

def self.from_png(file)
  image = ChunkyPNG::Image.from_file(file)
  mask = Mask.new(image.height, image.width)

  mask.rows.times do |row|
    mask.columns.times do |col|
      if image[col, row] == ChunkyPNG::Color::BLACK
        mask[row, col] = false
      else
        mask[row, col] = true
      end
    end
  end

  mask
end

如何将上面的代码转换成Javascript? 我面临转换挑战的 2 行是:

image = ChunkyPNG::Image.from_file(file)

if image[col, row] == ChunkyPNG::Color::BLACK

我可以使用什么 Javascript PNG 库来做同样的事情?

什么

image[col, row]

指的是?是RGB中的RG吗??或者它是什么?理解这一点可以帮助我在 Javascript PNG 库中找到等效的 Javascript 方法 ...

image[col, row]指的是图像中特定位置(列,行)的像素。 图像可以描述为二维颜色数组 - 在这种情况下,颜色是 ChunkyPNG::Color 的一个实例或只是一个数字。在您的代码示例中,掩码中所有黑色像素标记为 false,所有非黑色像素标记为 true.