Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayBuffer to blob conversion

I have a project where I need to display djvu schemas in browser.

I found this old library on Github which, as far as I understood, converts djvu files to bmp and then puts them into canvas element.

As I said, library is old(last commit was 5 years ago), so I need to make some corrections. Main problem is that lib uses obsolete BlobBuilder.

Steps I made to solve this problem:

  1. Decompress this library via Chrome DevTools
  2. Initial error is at line 3774 var c = "undefined" != typeof MozBlobBuilder ? MozBlobBuilder : "undefined" != typeof WebKitBlobBuilder ? WebKitBlobBuilder : console.log("warning: cannot build blobs")
  3. I commented out this line
  4. Next, I commented out line c = new c; and some of following lines too.

So, now it looks this way(variable I is array buffer, and ololo1 and ololo2 are some kind of offset and limit)

var c = new Blob(new Uint8Array(new Uint8Array(I,ololo1,ololo2)))               , b = b.createObjectURL(c)               , c = document.getElementById(kb)               , f = c.getContext("2d")               , h = new Image               , g = a[Ea >> 2]               , i = a[Fa >> 2]               , j = c.width               , k = Math.round(i * j / g);              h.onload = function()             {                 var a = g / j;                 4 < a && (a = 4);                 1 > a && (a = 1);                 f.globalAlpha = 1;                  for (N = 0; N < a; N++)                     f.drawImage(h, N, N, g - a + N, i - a + N, 0, 0, j, k),                     f.globalAlpha *= 1 - 1 / a;                 R(h.complete, "Image /bmp.bmp could not be decoded")             }             ;             h.onerror = function(errorMsg, url, lineNumber, column, errorObj) {                 console.log(errorMsg, url, lineNumber, column, errorObj);                 console.log("Image /bmp.bmp could not be decoded!")             }                        ; 

And now I stuck at error "Image /bmp.bmp could not be decoded!"(throwed in h.onerror handler).

So, my question is: what I am doing wrong?

like image 499
Rulisp Avatar asked May 24 '17 02:05

Rulisp


People also ask

Is ArrayBuffer a Blob?

An ArrayBuffer can be manipulated by using TypedArrays and DataView, whereas Blob is immutable. An ArrayBuffer is in memory, available for manipulation. A Blob can be on disk, in cache memory, and other places that are not readily available. Blob can be passed directly into other functions like window.

What is an ArrayBuffer?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".

Is Uint8Array an ArrayBuffer?

For instance: Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only that much). Such value is called a “8-bit unsigned integer”. Uint16Array – treats every 2 bytes as an integer, with possible values from 0 to 65535.

What is ArrayBuffer in typescript?

Interface ArrayBufferRepresents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.


2 Answers

I don't know why the author did wrap his Uint8Array in an new one... note that I don't really know either the deprecated BlobBuilder API, but one typo I can see in your code is that you need to wrap your TypedArray in a normal Array:

new Blob([new Uint8Array(buffer, byteOffset, length)]); 

The Blob constructor takes a blobParts sequence as first parameter, and then searches for BufferSource, USVStrings and Blob elements in this sequence. So when you pass a TypedArray, it will actually iterate over all the entries of this TypedArray and treat these as USVString (and thus convert their numerical value to UTF-8 strings in the Blob). That's rarely what you want, so better always pass an Array in this constructor.

like image 183
Kaiido Avatar answered Sep 19 '22 01:09

Kaiido


var buffer = new ArrayBuffer(32);  new Blob([buffer]); 

so the Uint8Array should be

new Blob([new Uint8Array([1, 2, 3, 4]).buffer]); 
like image 41
xsilen T Avatar answered Sep 21 '22 01:09

xsilen T