Looking to access 16-bit image data in Javascript/WebGL

后端 未结 3 2244
迷失自我
迷失自我 2021-02-14 23:49

I\'m trying to download 16-bit image data from a server and push it into a WebGL texture without browser plug-ins. texImage2d will work with: ImageData, HTMLImageElement, HTMLCa

3条回答
  •  长情又很酷
    2021-02-15 00:15

    PNGToy is a pretty featured library for extracting PNG chunks of almost all depths and channel modes with javascript (really client-side / without node.js, just Promise.js dependencies). The decode method will return the desired buffer. Here is an example for 16 bits grayscale PNG (16 bits RGB should work as well) :

    var dataObj;
    var img = new PngImage();
    var buffer;
    
    img.onload = function() {
    
        var pngtoy = this.pngtoy;
    
        dataObj = pngtoy.decode().then(function(results) {
    
            buffer = new Uint16Array(results.bitmap);
    
            for(var i = 0, j; i < buffer.length; i++) {
    
              j = buffer[i];
              buffer[i] = ((j & 0xff) << 8) | ((j & 0xff00) >>> 8); // needed to swap bytes for correct unsigned integer values  
            }
    
            console.log(buffer);
        });     
    };
    
    img.onerror = function(e) {
        console.log(e.message);
    };
    
    img.src = "image.png";
    

提交回复
热议问题