0

I am referring to this Link and trying the same in my local.

However, the code works without any problem, if I change the source of image of any width*height, but it fails to load (shows a black image) if I prepare a Uint8Array and then render it.

Here's my Code:

function renderImage (u8a) {
 if(dataTypedArray != null) {
     gl.clear(gl.COLOR_BUFFER_BIT || gl.DEPTH_BUFFER_BIT);
     u8a = dataTypedArray;
     image.width = tempImg.width;
     image.height = tempImg.height;
     gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, u8a);
        //Draw the rectangle.
        gl.drawArrays(gl.TRIANGLES, 0, 6);
      renderTexture();
 }

}

Where dataTypedArray is the Uint8Array created by myself.

Mainly when I am adding image data to a canvas, it works fine, but when I add canvas to texture, it fails.

 var renderData = function (imageAttr) {
 var data = imageAttr.data;
  var LINES_PER_CHUNK = imageAttr.lines;
 var alpha = 4;
 if(imageAttr.newBag) {
    newBuffer = new ArrayBuffer(imageAttr.width * imageAttr.height * alpha);
    dataTypedArray = new Uint8Array(newBuffer);
} else {
    for (var z = imageAttr.index; z > 0; z--) {
        for (i = 0 ; i < LINES_PER_CHUNK; i++) {
            for (j = 0 ; j < imageAttr.height; j++) {
                dataTypedArray[i * alpha + imageAttr.width*alpha * j + 3 + LINES_PER_CHUNK  * alpha * z] = dataTypedArray[i * alpha + imageAttr.width*alpha * j +  3 + LINES_PER_CHUNK  * alpha * (z-1)];
            }
        }
    }
}
for (i = 0, k = imageAttr.height*LINES_PER_CHUNK; i < LINES_PER_CHUNK; i++) {
    for (j = 0 ; j < imageAttr.height; j++) {
        dataTypedArray[i * alpha + imageAttr.width*4 * j + 3] = data[k - imageAttr.height + j];
    }
    k = k - imageAttr.height;
}
imageAttrTemp = imageAttr;
var can1 = document.getElementById('canvas1');
  can1.width = imageAttr.width; can1.height = imageAttr.height;
  var ctx = can1.getContext('2d');
  var imageData = ctx.getImageData(0, 0, imageAttr.width, imageAttr.height);
  for (i = 3 ; i < dataTypedArray.byteLength; i=i+4) {
      imageData.data[i] = dataTypedArray[i];
  }
  ctx.putImageData(imageData, 0,0);
  image.width=can1.width;
  image.height = can1.height;
  setupGLSL();
  renderImageLineByLine(can1);
};
var proto = 'ws://';
if(location.protocol === 'https:'){
    proto = 'wss://';
}
var ws = new WebSocket(proto + location.host + '/dashboard');
ws.binaryType = 'arraybuffer';
ws.onmessage = function(event) {
    try {
        var imageAttr = JSON.parse(event.data);
       renderData(imageAttr);
    } catch(E) {
       console.log(E);
    }
};
ws.onopen = function(event) {
   console.log('Con opened');
};
ws.onerror = function(event){
    console.log('Con error');
};
Peter O.
  • 32,158
  • 14
  • 82
  • 96
graphics123
  • 1,191
  • 3
  • 20
  • 57
  • Do you get any errors/warnings in the JavaScript console? – gman Mar 03 '16 at 08:51
  • that's the saddest and frustrating part ,no errors in console. – graphics123 Mar 03 '16 at 08:58
  • But I get a strong feeling that .the errors are coming from some incorrect height and width chosen by me . If I directly use a image ,it works , but If I take a Uint8Array and give that width and height , it shows a black image – graphics123 Mar 03 '16 at 09:04
  • Things to try. Set the entire Uint8Array to some solid color. Make sure the width and height are powers of 2 or if not set `TEXTURE_WRAP_S/T` to `CLAMP_TO_EDGE` and set `TEXTURE_MIN_FILTER` to `LINEAR` – gman Mar 03 '16 at 09:07
  • I am adding my code , so that you can have a look on to what I am doing wrong – graphics123 Mar 03 '16 at 09:51
  • Unable to add code (There are lot of formattting issues coming) .Could you please open https://jsfiddle.net/Subhasish2015/zsxuhx5u/ and see my code . In the bottom area ,you will find websocket part and how am I sending the image data for rendering – graphics123 Mar 03 '16 at 10:37

0 Answers0