Giter Site home page Giter Site logo

setTransparent about jsgif HOT 15 OPEN

antimatter15 avatar antimatter15 commented on July 24, 2024
setTransparent

from jsgif.

Comments (15)

g-regor avatar g-regor commented on July 24, 2024 1

The quantization algorithm puts multiple entries into the color table for the same color, so then there are different indices for the same color. Because of that, color that should be transparent, has different index than the transparency index.

I wrote a fix, that sets all the pixel indices, that should be transparent to transparency index.
GIFEncoder.js#L337

if (transparent != null) {
    transIndex = findClosest(transparent);

    var r = colorTab[transIndex*3];
    var g = colorTab[transIndex*3+1];
    var b = colorTab[transIndex*3+2];
    var trans_indices = [];
    for (var i=0; i<colorTab.length; i+=3)
    {
        var index = i / 3;
        if (!usedEntry[index]) continue;
        if (colorTab[i] == r && colorTab[i+1] == g && colorTab[i+2] == b)
            trans_indices.push(index);
    }
    for (var i=0; i<indexedPixels.length; i++)
        if (trans_indices.indexOf(indexedPixels[i]) >= 0)
            indexedPixels[i] = transIndex;
}

from jsgif.

Pl4n3 avatar Pl4n3 commented on July 24, 2024 1

Also, to run, line 363 must use floor function here:

var index/int/ = Math.floor(i / 3);

from jsgif.

memalign avatar memalign commented on July 24, 2024 1

Note that there are TWO places that need to be changed from:
var index = i / 3;
To
var index = Math.floor(i / 3);

  • In the code g-regor posted above
  • In the implementation of findClosest

Without these two fixes the wrong color is chosen as the transparency color.

(I discovered this when debugging why 0x000000 worked as a transparency color but 0xFFFFFF did not)

from jsgif.

forresto avatar forresto commented on July 24, 2024

Like... what kind of number is it expecting for the color? I'm passing 0xFFFFFF but white isn't transparent... sometimes a random other color is but I can't figure it out.

from jsgif.

forresto avatar forresto commented on July 24, 2024

Anybody want to hack on https://wiki.mozilla.org/APNG_Specification encoding with me?

from jsgif.

forresto avatar forresto commented on July 24, 2024

Here is one of the rare frames where transparency (0x00FF00) worked:

transparency worked

Most of the time it doesn't:

transparency didn't work

from jsgif.

forresto avatar forresto commented on July 24, 2024

This might be a bug: GIFEncoder.js#L351

        for (var i/*int*/ = 0; i < len;) {
          var dr/*int*/ = r - (colorTab[i++] & 0xff);
          var dg/*int*/ = g - (colorTab[i++] & 0xff);
          var db/*int*/ = b - (colorTab[i] & 0xff);
          var d/*int*/ = dr * dr + dg * dg + db * db;
          var index/*int*/ = i / 3;
          if (usedEntry[index] && (d < dmin)) {
            dmin = d;
            minpos = index;
          }
          i++;
        }

That var index/*int*/ = i / 3; will be 2/3 the first time. I changed the block to:

        for (var i/*int*/ = 0; i < len;) {
          var dr/*int*/ = r - (colorTab[i++] & 0xff);
          var dg/*int*/ = g - (colorTab[i++] & 0xff);
          var db/*int*/ = b - (colorTab[i++] & 0xff);
          var d/*int*/ = dr * dr + dg * dg + db * db;
          var index/*int*/ = (i / 3) - 1;
          if (usedEntry[index] && (d < dmin)) {
            dmin = d;
            minpos = index;
          }
        }

With this I'm getting better, but still random results:

better, but still random results

from jsgif.

forresto avatar forresto commented on July 24, 2024

Testing it today... colors were crap at 200x200 and fine at 300x300... maybe has something to do with this. Will look more.

from jsgif.

forresto avatar forresto commented on July 24, 2024

Thanks @grego87, transparent works now:

trans

There is still a huge difference in the color quality at 200x200 vs 300x300... any leads on that bug?
200x200 vs 300x300
(here is the meemoo app that I used to make this test)

from jsgif.

antimatter15 avatar antimatter15 commented on July 24, 2024

Have you tried out https://github.com/deanm/omggif

from jsgif.

forresto avatar forresto commented on July 24, 2024

It does look a little cleaner but you have to manually set the palette... I might port that part of yours to work with his...

from jsgif.

raducostea avatar raducostea commented on July 24, 2024

Hi,

Any idea when the quality depending on the canvas size may get fixed?

from jsgif.

forresto avatar forresto commented on July 24, 2024

I'm pretty sure the size/color bug #6 is in NeuQuant.js, because I got it working with OMGGIF and they bug is still there. I'm going to start a bounty on Stack Overflow.

from jsgif.

forresto avatar forresto commented on July 24, 2024

@raducostea This patch should fix the color/size issue: https://github.com/antimatter15/jsgif/pull/8/files

from jsgif.

flyskywhy avatar flyskywhy commented on July 24, 2024

You can try gifenc

Because my react-native-gifencoder is also some fork of gif.js

But in my react-native-runescape-text I replace react-native-gifencoder with gifenc for Fix when one color e.g. '#ff0000' only have 1 or 2 or 3 pixel in one frame, the generated gif pixel will be '#9f0000'; fix some frame not transparent but black; reduce gif size 10x

from jsgif.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.