感谢我的 stackexchange 问题中出现的侧边栏中的一些措辞,我再次尝试使用一些不同的术语进行谷歌搜索,发现:
http://www.worldwidewhat.net/2012/07/how-to-draw-bitmaps-using-javascript/
这或多或少回答了我的问题。不过还是有兴趣的其他人有建议或答案!
更新:
这是我最终使用的代码。它是从上面的链接修改的,因此网格上的循环是从下到上从左到右完成的,因此不需要翻转,并且我已经删除了一些辅助功能。在我的循环中,我将我的数据调整为以平均值为中心并标准化为跨越 4 个标准差(两个正数,两个负数)。
var interpolation = this.interpolateGeoDataLayerToGrid();
var numFileBytes = this.getLittleEndianHex(interpolation.grid[0].length * interpolation.grid.length);
var w = this.getLittleEndianHex(interpolation.grid[0].length);
var h = this.getLittleEndianHex(interpolation.grid.length);
var header =
'BM' + // Signature
numFileBytes + // size of the file (bytes)*
'\x00\x00' + // reserved
'\x00\x00' + // reserved
'\x36\x00\x00\x00' + // offset of where BMP data lives (54 bytes)
'\x28\x00\x00\x00' + // number of remaining bytes in header from here (40 bytes)
w + // the width of the bitmap in pixels*
h + // the height of the bitmap in pixels*
'\x01\x00' + // the number of color planes (1)
'\x20\x00' + // 32 bits / pixel
'\x00\x00\x00\x00' + // No compression (0)
'\x00\x00\x00\x00' + // size of the BMP data (bytes)*
'\x13\x0B\x00\x00' + // 2835 pixels/meter - horizontal resolution
'\x13\x0B\x00\x00' + // 2835 pixels/meter - the vertical resolution
'\x00\x00\x00\x00' + // Number of colors in the palette (keep 0 for 32-bit)
'\x00\x00\x00\x00'; // 0 important colors (means all colors are important)
var imgdata = "";
for (var row=interpolation.grid.length-1; row >= 0; row--) {
for (var col=0; col<interpolation.grid[row].length; col++) {
var value = Math.min(255,Math.max(0,Math.floor(128 + 64*(interpolation.grid[row][col]-interpolation.mean)/interpolation.stdev)));
imgdata += String.fromCharCode(255-value, 0, value, 128);
}
}
var datauri = 'data:image/bmp;base64,';
if(window.btoa != undefined) {
datauri += btoa(header + imgdata);
}
else {
datauri += $.base64.encode(header + imgdata);
}
newOverlay = new google.maps.GroundOverlay(datauri,
new google.maps.LatLngBounds(
new google.maps.LatLng(this.GridDataSettings.latmin, this.GridDataSettings.longmin),
new google.maps.LatLng(this.GridDataSettings.latmax, this.GridDataSettings.longmax)
));
newOverlay.setMap(map);
这里是 lttleEndianHex 辅助函数,取自上面的链接:
getLittleEndianHex: function(value) {
var result = [];
for (var bytes = 4; bytes > 0; bytes--) {
result.push(String.fromCharCode(value & 255));
value >>= 8;
}
return result.join('');
}