Skip to content

Commit

Permalink
gif anim palette closest color remap
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed Apr 7, 2024
1 parent bf3b302 commit 7e8a1f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
27 changes: 22 additions & 5 deletions lib/src/formats/gif/gif_color_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,32 @@ class GifColorMap {
}

int findColor(num r, num g, num b, num a) {
num closestDistance = -1;
var closestIndex = -1;
for (var i = 0; i < numColors; ++i) {
if (_palette.getRed(i) == r &&
_palette.getGreen(i) == g &&
_palette.getBlue(i) == b &&
_palette.getAlpha(i) == a) {
final pr = _palette.getRed(i);
final pg = _palette.getGreen(i);
final pb = _palette.getBlue(i);
final pa = _palette.getAlpha(i);
if (pr == r && pg == g && pb == b && pa == a) {
return i;
}
final dr = r - pr;
final dg = g - pg;
final db = b - pb;
final da = a - pa;
final d2 = (dr * dr) + (dg * dg) + (db * db) + (da * da);
if (closestIndex == -1) {
closestIndex = i;
closestDistance = d2;
} else {
if (d2 < closestDistance) {
closestIndex = i;
closestDistance = d2;
}
}
}
return -1;
return closestIndex;
}

int red(int color) => _palette.getRed(color) as int;
Expand Down
10 changes: 6 additions & 4 deletions lib/src/formats/gif_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,16 @@ class GifDecoder extends Decoder {
if (frame.disposal == 2) {
nextImage.clear(colorMap.color(info!.backgroundColor!.r as int));
} else if (frame.disposal != 3) {
if (frame.x != 0 || frame.y != 0 || frame.width != lastImage.width ||
if (frame.x != 0 ||
frame.y != 0 ||
frame.width != lastImage.width ||
frame.height != lastImage.height) {
if (frame.colorMap != null) {
final lp = lastImage.palette!;
final remapColors = <int,int>{};
final remapColors = <int, int>{};
for (var ci = 0; ci < colorMap.numColors; ++ci) {
final nc = colorMap.findColor(
lp.getRed(ci), lp.getGreen(ci), lp.getBlue(ci), lp.getAlpha(ci));
final nc = colorMap.findColor(lp.getRed(ci), lp.getGreen(ci),
lp.getBlue(ci), lp.getAlpha(ci));
remapColors[ci] = nc;
}

Expand Down

0 comments on commit 7e8a1f6

Please sign in to comment.