[英]How to scale width of a RGB image in a byte array
My brain is not working properly today and I can't seem to figure this out.我的大脑今天不能正常工作,我似乎无法弄清楚这一点。
I have a RGBA image data stored in an Uint8Array()
and need to scale the width only.我有一个 RGBA 图像数据存储在
Uint8Array()
,只需要缩放宽度。
var w = 160;
var h = 200;
var depth=4;
var pixels = new Uint8Array(w*h*depth);
I need to scale the pixels array
to 320x200
and every attempt I did ended up with a garbled image.我需要将像素
array
缩放到320x200
,每次尝试都以乱码图像告终。
Thanks to Yves Daoust I revisited some of my old attempts at solving this by duplicating every chunk of 4 to the destination, and now I got it working.感谢Yves Daoust ,我通过将 4 的每一块复制到目的地,重新审视了我解决这个问题的一些旧尝试,现在我让它工作了。 So thank you Yves :) I do not know what I did wrong earlier.
所以谢谢你 Yves :) 我不知道我之前做错了什么。
This is the working code that I ended up with.这是我最终得到的工作代码。 I'm 100% sure it can be done differently and better, but at this point I am satisfied :)
我 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.