foliojs / png.js

A (animated) PNG decoder in JavaScript for the HTML5 canvas element and Node.js
http://devongovett.github.com/png.js
MIT License
489 stars 92 forks source link

From browser Worker #30

Open Noitidart opened 7 years ago

Noitidart commented 7 years ago

I am trying to use this from a Web Worker in a browser.

I get the image data by doing xhr with responseType arraybuffer. I then do var p = new PNG(arrbuf);

However this gets stuck into an infinite loop. I see this is because:

      while (true) {
        chunkSize = this.readUInt32();
        section = ((function() {
          var _i, _results;
          _results = [];
          for (i = _i = 0; _i < 4; i = ++_i) {
            _results.push(String.fromCharCode(this.data[this.pos++]));
          }
          return _results;
        }).call(this)).join('');

Can you please help me modify to use in Web Worker scope.

Thanks!

Noitidart commented 7 years ago

Oh I got passed the infinite loop by doing:

var a = new PNG(new Uint8Array(xhr.response));

I need to now get the rgba pixel data. Working on that now.

Noitidart commented 7 years ago

Hm ok so I was doing this now:

PNG.load('file:///C:/Users/Mercurius/Documents/GitHub/Firefox-PNG-Icon-Collections/_nightly/64.png', function(c) { C = c })

So C is set to an object but i dont see pixels (rgba array). Can you please advise?

Noitidart commented 7 years ago

Oh got it had to do this:

    PNG.load('...../64.png', function(pnginfo) {
        var imagedata = new ImageData(pnginfo.width, pnginfo.height);
        pnginfo.copyToImageData(imagedata, pnginfo.decodePixels());
        pnginfo.pixels = imagedata.data;
        deferred_loaded.resolve(pnginfo);
    });

May I submit a PR, i had to add an if statement, if its a worker, not to load that makeImage stuff as it uses document and then to set global instead of window.PNG we have to set DedicatedWorkerGlobalScope.PNG =

guest271314 commented 4 years ago

@Noitidart

I am trying to use this from a Web Worker in a browser.

FWIW https://github.com/guest271314/decodePixelsFromOffscreenCanvas