Skip to content

Commit

Permalink
Default thresholds really needed work.
Browse files Browse the repository at this point in the history
The thresholds were way too low for analyze() and analyzeHueWise(), and a little low for analyzeReductive (but one place set them far too low as well...). This affected the quality of animations, mainly, if they didn't use the default palette (or any exact palette). The current thresholds should behave better, though we'll have to see.
  • Loading branch information
tommyettinger committed Jul 9, 2024
1 parent c3edd94 commit 9e65d57
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/github/tommyettinger/anim8/AnimatedGif.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void write(OutputStream output, Array<Pixmap> frames, int fps) {
if (clearPalette) {
if (fastAnalysis && frames.size > 1) {
palette = new PaletteReducer();
palette.analyzeFast(frames.first(), 100, 256);
palette.analyzeFast(frames.first(), 300, 256);
}
else
palette = new PaletteReducer(frames.first());
Expand Down Expand Up @@ -1844,9 +1844,9 @@ protected void analyzePixels() {
if(seq > 1 && clearPalette)
{
if(fastAnalysis)
palette.analyzeFast(image, 100, 256);
palette.analyzeFast(image, 300, 256);
else
palette.analyze(image, 100, 256);
palette.analyze(image, 300, 256);
}
final int[] paletteArray = palette.paletteArray;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/tommyettinger/anim8/FastGif.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void write(OutputStream output, Array<Pixmap> frames, int fps) {
if (clearPalette) {
if (fastAnalysis && frames.size > 1) {
palette = new FastPalette();
palette.analyzeFast(frames.first(), 100, 256);
palette.analyzeFast(frames.first(), 300, 256);
}
else
palette = new FastPalette(frames.first());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public FastPalette(int[] palette, byte[] preload)
* (see {@link #analyze(Pixmap, double)} for more info).
*
* @param pixmap a Pixmap to analyze in detail to produce a palette
* @param threshold the minimum difference between colors required to put them in the palette (default 100)
* @param threshold the minimum difference between colors required to put them in the palette (default 300)
*/
public FastPalette(Pixmap pixmap, double threshold) {
analyze(pixmap, threshold);
Expand Down
104 changes: 53 additions & 51 deletions src/main/java/com/github/tommyettinger/anim8/PaletteReducer.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public QualityPalette(int[] palette, byte[] preload)
* (see {@link #analyze(Pixmap, double)} for more info).
*
* @param pixmap a Pixmap to analyze in detail to produce a palette
* @param threshold the minimum difference between colors required to put them in the palette (default 100)
* @param threshold the minimum difference between colors required to put them in the palette (default 300)
*/
public QualityPalette(Pixmap pixmap, double threshold) {
analyze(pixmap, threshold);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void renderGif(String filename) {
String type = "";
{
for (int count : new int[]{16, 31, 255}) {
reducer.analyze(pixmaps, 100, count + 1);
reducer.analyze(pixmaps, 300, count + 1);

gif.setPalette(reducer);
for(Dithered.DitherAlgorithm d : Dithered.DitherAlgorithm.ALL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public boolean keyDown(int keycode) {
if(UIUtils.ctrl()) {
if (UIUtils.shift()) {
int kc = (keycode - 6) * keycode;
reducer.analyzeHueWise(p0, 100, kc);
reducer.analyzeHueWise(p0, 500, kc);
}
else {
int kc = keycode - 5;
reducer.analyzeHueWise(p0, 100, kc);
reducer.analyzeHueWise(p0, 500, kc);
}

// if (UIUtils.shift())
Expand All @@ -237,21 +237,21 @@ public boolean keyDown(int keycode) {
else {
if (UIUtils.shift()) {
int kc = (keycode - 6) * keycode;
reducer.analyze(p0, 100, kc);
reducer.analyze(p0, 300, kc);
}
else {
int kc = keycode - 5;
reducer.analyze(p0, 100, kc);
reducer.analyze(p0, 300, kc);
}
}
// System.out.println("Total for all color differences: " + reducer.totalDifference);
refresh();
break;
case Input.Keys.NUM_0:
if(UIUtils.ctrl())
reducer.analyzeHueWise(p0, 100, 256);
reducer.analyzeHueWise(p0, 500, 256);
else
reducer.analyze(p0, 100);
reducer.analyze(p0, 300);
refresh();
break;
case Input.Keys.B:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ public void renderPNG8(String[] names, int[][] palettes, long[] seeds, ShaderPro
pixmaps.add(ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
}
if (palettes[n].length == 0) {
png8.palette.analyzeReductive(pixmaps);
png8.palette.analyzeReductive(pixmaps, 400);
} else if (palettes[n].length == 1 && palettes[n][0] == 1) {
png8.palette.analyzeHueWise(pixmaps);
png8.palette.analyzeHueWise(pixmaps, 500);
} else if (palettes[n].length == 1 && palettes[n][0] == 2) {
png8.palette.analyze(pixmaps);
png8.palette.analyze(pixmaps, 300);
} else {
png8.palette.exact(palettes[n]);
}
Expand Down Expand Up @@ -399,15 +399,15 @@ public void renderGif(String[] names, int[][] palettes, long[] seeds, ShaderProg
if (!(gif.fastAnalysis = !gif.fastAnalysis)) --n;
prefix = "images/gif/animated" + (gif.fastAnalysis ? "Fast" : "Slow") + "/AnimatedGif-";
} else if (palettes[n].length == 0) {
pal.analyzeReductive(pixmaps);
pal.analyzeReductive(pixmaps, 400);
gif.palette = pal;
prefix = "images/gif/animatedReductive/AnimatedGif-";
} else if (palettes[n].length == 1 && palettes[n][0] == 1) {
pal.analyzeHueWise(pixmaps);
pal.analyzeHueWise(pixmaps, 500);
gif.palette = pal;
prefix = "images/gif/animatedHue/AnimatedGif-";
} else if (palettes[n].length == 1 && palettes[n][0] == 2) {
pal.analyze(pixmaps);
pal.analyze(pixmaps, 300);
gif.palette = pal;
prefix = "images/gif/animatedAnalyzed/AnimatedGif-";
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/github/tommyettinger/StillImageDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void renderPNG8(String filename) {
reducer = (type.isEmpty()) ? regular : quality;
for (int count : new int[]{16, 31, 255}) {
if ("H".equals(type)) {
reducer.analyzeHueWise(pixmap, 100, count + 1);
reducer.analyzeHueWise(pixmap, 500, count + 1);
} else if ("R".equals(type)) {
reducer.analyzeReductive(pixmap, 300, count + 1);
reducer.analyzeReductive(pixmap, 400, count + 1);
} else {
reducer.analyze(pixmap, 100, count + 1);
reducer.analyze(pixmap, 300, count + 1);
}

png8.setPalette(reducer);
Expand Down Expand Up @@ -149,11 +149,11 @@ public void renderGif(String filename) {
reducer = (type.isEmpty()) ? regular : quality;
for (int count : new int[]{16, 31, 255}) {
if ("H".equals(type)) {
reducer.analyzeHueWise(pixmaps, 100, count + 1);
reducer.analyzeHueWise(pixmaps, 500, count + 1);
} else if ("R".equals(type)) {
reducer.analyzeReductive(pixmaps, 300, count + 1);
reducer.analyzeReductive(pixmaps, 400, count + 1);
} else {
reducer.analyze(pixmaps, 100, count + 1);
reducer.analyze(pixmaps, 300, count + 1);
}
gif.setPalette(reducer);
for(Dithered.DitherAlgorithm d : ALGORITHMS) {
Expand Down

0 comments on commit 9e65d57

Please sign in to comment.