Skip to content

Commit

Permalink
The rest of the loafing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed May 4, 2024
1 parent 79643dd commit 0e90fb6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/tommyettinger/anim8/AnimatedGif.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,14 @@ protected void analyzeLoaf() {
final byte[] paletteMapping = palette.paletteMapping;
boolean hasTransparent = paletteArray[0] == 0;

final int strength = (int) (11f * ditherStrength / (palette.populationBias * palette.populationBias) + 0.5f);
final float strength = ditherStrength * palette.populationBias;
for (int y = 0, i = 0; y < height && i < nPix; y++) {
for (int px = 0; px < width & i < nPix; px++) {
color = image.getPixel(px, flipped + flipDir * y);
if ((color & 0x80) == 0 && hasTransparent)
indexedPixels[i++] = 0;
else {
int adj = ((px & 1) + (y & 1) - 1) * strength * (2 + (((px ^ y) & 2) - 1));
int adj = (int)((((px + y & 1) << 5) - 16) * strength);
int rr = Math.min(Math.max(((color >>> 24) ) + adj, 0), 255);
int gg = Math.min(Math.max(((color >>> 16) & 0xFF) + adj, 0), 255);
int bb = Math.min(Math.max(((color >>> 8) & 0xFF) + adj, 0), 255);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/github/tommyettinger/anim8/PNG8.java
Original file line number Diff line number Diff line change
Expand Up @@ -1177,15 +1177,15 @@ public void writeLoafDithered(OutputStream output, Pixmap pixmap) {
}

int color;
final int strength = (int) (11f * ditherStrength / (palette.populationBias * palette.populationBias) + 0.5f);
final float strength = ditherStrength * palette.populationBias;
for (int y = 0; y < h; y++) {
int py = flipY ? (h - y - 1) : y;
for (int px = 0; px < w; px++) {
color = pixmap.getPixel(px, py);
if ((color & 0x80) == 0 && hasTransparent)
curLine[px] = 0;
else {
int adj = ((px & 1) + (y & 1) - 1) * strength * (2 + (((px ^ y) & 2) - 1));
int adj = (int)((((px + y & 1) << 5) - 16) * strength);
int rr = Math.min(Math.max(((color >>> 24) ) + adj, 0), 255);
int gg = Math.min(Math.max(((color >>> 16) & 0xFF) + adj, 0), 255);
int bb = Math.min(Math.max(((color >>> 8) & 0xFF) + adj, 0), 255);
Expand Down Expand Up @@ -3331,8 +3331,7 @@ public void writeLoafDithered(OutputStream output, Array<Pixmap> frames, int fps
byte[] curLine;
int color;

final float populationBias = palette.populationBias;
final int strength = (int) (11f * ditherStrength / (populationBias * populationBias) + 0.5f);
final float strength = ditherStrength * palette.populationBias;

int seq = 0;
for (int i = 0; i < frames.size; i++) {
Expand Down Expand Up @@ -3371,7 +3370,7 @@ public void writeLoafDithered(OutputStream output, Array<Pixmap> frames, int fps
if ((color & 0x80) == 0 && hasTransparent)
curLine[px] = 0;
else {
int adj = ((px & 1) + (y & 1) - 1) * strength * (2 + (((px ^ y) & 2) - 1));
int adj = (int)((((px + y & 1) << 5) - 16) * strength);
int rr = Math.min(Math.max(((color >>> 24) ) + adj, 0), 255);
int gg = Math.min(Math.max(((color >>> 16) & 0xFF) + adj, 0), 255);
int bb = Math.min(Math.max(((color >>> 8) & 0xFF) + adj, 0), 255);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/github/tommyettinger/anim8/PaletteReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,24 @@ public void setDitherStrength(float ditherStrength) {
this.ditherStrength = Math.max(0f, ditherStrength);
}

public float getPopulationBias() {
return populationBias;
}

/**
* Sets the population bias; rarely needed externally.
* Typically, the population bias is between 0.5 and 1, closer to 1 with larger palette sizes, and closer to 0.5
* with smaller palettes.
* <br>
* Within anim8-gdx, this is generally calculated with {@code (float)Math.exp(-1.375 / colorCount)}, where
* {@link #colorCount} is already known and between 2 and 256, inclusive.
*
* @param populationBias a population bias value, which is almost always between 0.5 and 1.0
*/
public void setPopulationBias(float populationBias) {
this.populationBias = populationBias;
}

/**
* Modifies the given Pixmap so that it only uses colors present in this PaletteReducer, dithering when it can by
* using Wren dithering (this merely delegates to {@link #reduceWren(Pixmap)}).
Expand Down

0 comments on commit 0e90fb6

Please sign in to comment.