Skip to content

Commit

Permalink
Adds option in imagetext to invert dots in braille algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Zailer43 committed Jun 18, 2024
1 parent 670781e commit d9aec6e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fzmm.zailer.me.client.gui.imagetext.algorithms;

import fzmm.zailer.me.client.gui.components.BooleanButton;
import fzmm.zailer.me.client.gui.components.SliderWidget;
import fzmm.zailer.me.client.gui.components.row.BooleanRow;
import fzmm.zailer.me.client.gui.components.row.SliderRow;
import fzmm.zailer.me.client.gui.utils.memento.IMementoObject;
import fzmm.zailer.me.client.logic.imagetext.ImagetextData;
Expand All @@ -17,10 +19,12 @@ public class ImagetextBrailleAlgorithm implements IImagetextAlgorithm {
private static final String[] BRAILLE_CHARACTERS;
private static final String EDGE_THRESHOLD_ID = "edgeThreshold";
private static final String EDGE_DISTANCE_ID = "edgeDistance";
private static final String INVERT_ID = "invert";
private static final byte BRAILLE_CHARACTER_WIDTH = 2;
private static final byte BRAILLE_CHARACTER_HEIGHT = 4;
private SliderWidget edgeThresholdSlider;
private SliderWidget edgeDistanceSlider;
private BooleanButton invertBooleanButton;

@Override
public String getId() {
Expand Down Expand Up @@ -51,6 +55,7 @@ public List<MutableText> get(ImagetextLogic logic, ImagetextData data, int lineS
public void setupComponents(FlowLayout rootComponent) {
this.edgeThresholdSlider = SliderRow.setup(rootComponent, EDGE_THRESHOLD_ID, 30, 1, 255, Integer.class, 0, 1, null);
this.edgeDistanceSlider = SliderRow.setup(rootComponent, EDGE_DISTANCE_ID, 2, 1, 5, Integer.class, 0, 1, null);
this.invertBooleanButton = BooleanRow.setup(rootComponent, INVERT_ID, false, null);
}

@Override
Expand Down Expand Up @@ -81,40 +86,33 @@ public List<String> getBrailleCharacters(BufferedImage grayScaleImage, int width
* and height multiply of {@link ImagetextBrailleAlgorithm#BRAILLE_CHARACTER_HEIGHT}
*/
public String getBrailleCharacter(BufferedImage grayScaleImage, int x, int y, int edgeThreshold, int edgeDistance) {
int index = 0B11111111;
int index = BRAILLE_CHARACTERS.length - 1;
int yOffset = y;

if (this.isEdge(grayScaleImage, x, yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(0);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(1);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(2);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(3);
index -= this.getBrailleCharacterIndex(0, grayScaleImage, x, yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(1, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(2, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(3, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);

yOffset = y;

if (this.isEdge(grayScaleImage, ++x, yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(4);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(5);
index -= this.getBrailleCharacterIndex(4, grayScaleImage, ++x, yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(5, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(6, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);
index -= this.getBrailleCharacterIndex(7, grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(6);

if (this.isEdge(grayScaleImage, x, ++yOffset, edgeThreshold, edgeDistance))
index -= this.getBrailleCharacterIndex(7);
if (this.invertBooleanButton.enabled()) {
index = BRAILLE_CHARACTERS.length - 1 - index;
}

return BRAILLE_CHARACTERS[index];
}

private int getBrailleCharacterIndex(int index) {
return 1 << index;
private int getBrailleCharacterIndex(int index, BufferedImage grayScaleImage, int x, int y, int edgeThreshold, int edgeDistance) {
if (this.isEdge(grayScaleImage, x, y, edgeThreshold, edgeDistance))
return 1 << index;

return 0;
}

// pain
Expand Down Expand Up @@ -162,17 +160,22 @@ public BufferedImage toGrayScale(BufferedImage image) {

@Override
public IMementoObject createMemento() {
return new BrailleAlgorithmMementoTab((int) this.edgeThresholdSlider.discreteValue(), (int) this.edgeDistanceSlider.discreteValue());
return new BrailleAlgorithmMementoTab(
(int) this.edgeThresholdSlider.discreteValue(),
(int) this.edgeDistanceSlider.discreteValue(),
this.invertBooleanButton.enabled()
);
}

@Override
public void restoreMemento(IMementoObject mementoObject) {
BrailleAlgorithmMementoTab memento = (BrailleAlgorithmMementoTab) mementoObject;
this.edgeThresholdSlider.setFromDiscreteValue(memento.edgeThreshold);
this.edgeDistanceSlider.setFromDiscreteValue(memento.edgeDistance);
this.invertBooleanButton.enabled(memento.invert);
}

private record BrailleAlgorithmMementoTab(int edgeThreshold, int edgeDistance) implements IMementoObject {
private record BrailleAlgorithmMementoTab(int edgeThreshold, int edgeDistance, boolean invert) implements IMementoObject {
}

static {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/fzmm/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@
"fzmm.gui.imagetext.option.bookTooltipMode.tooltip": "Choose how the imagetext will be added to the book.\n\nAdd page: add a page at the end with a message that has the imagetext.\nCreate book: create a book with only the message that has the imagetext.",
"fzmm.gui.imagetext.option.characters": "Characters",
"fzmm.gui.imagetext.option.characters.tooltip": "Characters that will be used in the imagetext,\nthis message will be repeated each time it ends until the line is completed\nand it will have the color of the pixel where it is located.\n\nNote: Leaving the default character will replace\nall transparent pixels with spaces to simulate transparency.",
"fzmm.gui.imagetext.option.invert": "Invert",
"fzmm.gui.imagetext.option.invert.tooltip": "Invert dots of the braille characters.",
"fzmm.gui.imagetext.option.edgeDistance": "Edge distance",
"fzmm.gui.imagetext.option.edgeDistance.tooltip": "Distance at which the pixel to which the Edge threshold\nis compared is located.",
"fzmm.gui.imagetext.option.edgeThreshold": "Edge threshold",
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/fzmm/owo_ui/imagetext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<fzmm.slider-row>
<id>edgeDistance</id>
</fzmm.slider-row>
<fzmm.boolean-row>
<id>invert</id>
</fzmm.boolean-row>
</children>
</fzmm.screen-tab>

Expand Down

0 comments on commit d9aec6e

Please sign in to comment.