diff --git a/build.gradle.kts b/build.gradle.kts index e9682f7..120ded7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,10 +3,6 @@ plugins { id("maven-publish") id("signing") } - -val currentGroup = "io.github.seujorgenochurras" -val currentVersion = "0.0.2" - repositories { mavenCentral() } @@ -14,6 +10,7 @@ dependencies { testImplementation(platform("org.junit:junit-bom:5.9.1")) testImplementation("org.junit.jupiter:junit-jupiter") implementation("io.metaloom.video:video4j:1.3.0") + implementation("org.openpnp:opencv:4.6.0-0") } tasks.test { useJUnitPlatform() @@ -21,14 +18,15 @@ tasks.test { tasks.withType(Jar::class) { manifest { attributes["Manifest-Version"] = "1.0" + attributes["Main-Class"] = "io.github.seujorgenochurras.ScreenParser" } } publishing { publications { create("mavenJava") { - groupId = currentGroup + groupId = "io.github.seujorgenochurras" artifactId = "image-to-ascii" - version = currentVersion + version = "0.0.2" from(components["java"]) pom { diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..033e24c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 874fcb1..0564365 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1 +1 @@ -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip diff --git a/src/main/java/io/github/seujorgenochurras/Main.java b/src/main/java/io/github/seujorgenochurras/Main.java index 79f0ca1..b0bfbfe 100644 --- a/src/main/java/io/github/seujorgenochurras/Main.java +++ b/src/main/java/io/github/seujorgenochurras/Main.java @@ -1,6 +1,5 @@ package io.github.seujorgenochurras; -import java.io.File; import java.io.FileWriter; import java.io.IOException; diff --git a/src/main/java/io/github/seujorgenochurras/ScreenParser.java b/src/main/java/io/github/seujorgenochurras/ScreenParser.java new file mode 100644 index 0000000..a65813e --- /dev/null +++ b/src/main/java/io/github/seujorgenochurras/ScreenParser.java @@ -0,0 +1,76 @@ +package io.github.seujorgenochurras; + +import io.github.seujorgenochurras.color.BestSymbolPatternFinder; +import io.github.seujorgenochurras.image.BetterImage; +import io.github.seujorgenochurras.image.ascii.AsciiParser; +import io.github.seujorgenochurras.image.ascii.ParserBuilder; +import io.github.seujorgenochurras.image.ascii.ParserConfig; +import io.github.seujorgenochurras.image.ascii.algorithm.pixel.bright.Algorithms; +import io.github.seujorgenochurras.image.ascii.algorithm.pixel.color.DefaultColorType; + +import java.awt.*; +import java.util.Timer; +import java.util.TimerTask; + +import static io.github.seujorgenochurras.util.StringUtils.getUTFChars; + +public class ScreenParser { + public static final String[] symbols = BestSymbolPatternFinder. + findBestPattern(2, 55, getUTFChars(32, 126)).toArray(); + + + public static final ParserConfig defaultParserConfig = ParserBuilder.startBuild() + .symbols(symbols) + .scaled() + .height(80) + .width(400) + .getScale() + .parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM.getAlgorithm()) + .colorAlgorithm(DefaultColorType.ANSI) + .build(); + + + public static void main(String[] args) throws AWTException { + if (args[0] == null || args[1] == null) { + args[0] = "80"; + args[1] = "300"; + } + + final ParserConfig defaultParserConfig = ParserBuilder.startBuild() + .symbols(symbols) + .scaled() + .height(Integer.parseInt(args[1])) + .width(Integer.parseInt(args[0])) + .getScale() + .parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM.getAlgorithm()) + // .colorAlgorithm(DefaultColorType.ANSI) + .build(); + + + Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize(); + Rectangle screenRect = new Rectangle(screenDimension); + Robot robot = new Robot(); + System.out.print("\u001b[2J"); + final double[] fps = {1}; + final int[] framesThisSecond = {0}; + TimerTask task = new TimerTask() { + @Override + public void run() { + fps[0] = framesThisSecond[0]; + framesThisSecond[0] = 0; + + } + }; + Timer timer = new Timer("Timer"); + int delay = 1000; + timer.scheduleAtFixedRate(task, 0, delay); + + while (true) { + framesThisSecond[0]++; + System.out.print("\u001B[1;1H"); + System.out.print(AsciiParser.parse(new BetterImage(robot.createScreenCapture(screenRect)), defaultParserConfig)); + System.out.println("\u001B[0;1000H Current fps: " + fps[0]); + } + } + +} diff --git a/src/main/java/io/github/seujorgenochurras/color/BestSymbolPatternFinder.java b/src/main/java/io/github/seujorgenochurras/color/BestSymbolPatternFinder.java index f1272dc..619bded 100644 --- a/src/main/java/io/github/seujorgenochurras/color/BestSymbolPatternFinder.java +++ b/src/main/java/io/github/seujorgenochurras/color/BestSymbolPatternFinder.java @@ -17,6 +17,7 @@ public static SymbolList findBestPattern(String... chars) { } public static SymbolList findBestPattern(int maxSymbols, String... chars) { + return findBestPattern(10, maxSymbols, chars); } @@ -33,6 +34,8 @@ public static SymbolList findBestPattern(int symbolAccuracy, int maxSymbols, Str SymbolList symbolList = new SymbolList(maxSymbols, symbolAccuracy); for (String symbol : chars) { + if (symbolList.size() > maxSymbols) break; + g2d = image.getGraphics(); Font font = new Font("Fira Mono", Font.BOLD, 40); @@ -67,7 +70,9 @@ private static double getAvgPixelBrightness(BufferedImage image) { int red = color.getRed().getColorValue(); int green = color.getGreen().getColorValue(); int blue = color.getBlue().getColorValue(); - avgPixelBrightness.addAndGet(Algorithms.BRIGHTEST_PIXEL.getAlgorithm().getPixelRepresentation(red, green, blue)); + + long pixelBrightness = Algorithms.BRIGHTEST_PIXEL.getAlgorithm().getPixelRepresentation(red, green, blue); + avgPixelBrightness.addAndGet(pixelBrightness); }); return (double) avgPixelBrightness.get() / betterImage.getPixels().size(); diff --git a/src/main/java/io/github/seujorgenochurras/color/symbol/SymbolList.java b/src/main/java/io/github/seujorgenochurras/color/symbol/SymbolList.java index 1885ed9..6e78de2 100644 --- a/src/main/java/io/github/seujorgenochurras/color/symbol/SymbolList.java +++ b/src/main/java/io/github/seujorgenochurras/color/symbol/SymbolList.java @@ -13,9 +13,10 @@ public SymbolList(int listSize) { this.sortedSymbols = new Symbol[listSize - 1]; } - public SymbolList(int listSize, int acceptableCharAccurace) { + public SymbolList(int listSize, int acceptableCharAccuracy) { this.sortedSymbols = new Symbol[listSize - 1]; - this.acceptableSymbolAccuracy = acceptableCharAccurace; + System.out.println(sortedSymbols.length); + this.acceptableSymbolAccuracy = acceptableCharAccuracy; } @@ -23,7 +24,7 @@ public SymbolList(int listSize, int acceptableCharAccurace) { public void addSymbol(Symbol symbol) { double newSymbolBrightness = symbol.getBrightness(); - int symbolShade = (int) Math.round(newSymbolBrightness / ((double) 256 / sortedSymbols.length)); + int symbolShade = (int) Math.round(newSymbolBrightness / ((double) 255 / sortedSymbols.length)); if (sortedSymbols.length - 1 < symbolShade) return; @@ -58,11 +59,11 @@ public Symbol[] getSymbolsAsArray() { public String[] toArray() { List stringList = new ArrayList<>(); - for (int i = 0; i < sortedSymbols.length - 1; i++) { + for (int i = 0; i <= sortedSymbols.length - 1; i++) { if (sortedSymbols[i] == null) continue; stringList.add(sortedSymbols[i].getData()); } - return stringList.toArray(new String[stringList.size() - 1]); + return stringList.toArray(new String[stringList.size()]); } public int size() { diff --git a/src/main/java/io/github/seujorgenochurras/demo/ImageToAscii.java b/src/main/java/io/github/seujorgenochurras/demo/ImageToAscii.java index 6c1e3d6..615d88a 100644 --- a/src/main/java/io/github/seujorgenochurras/demo/ImageToAscii.java +++ b/src/main/java/io/github/seujorgenochurras/demo/ImageToAscii.java @@ -19,14 +19,14 @@ public class ImageToAscii { private static final String[] unorderedSymbols = getUTFChars(32, 123); - private static final String[] symbols = BestSymbolPatternFinder.findBestPattern(1, unorderedSymbols).toArray(); + private static final String[] symbols = BestSymbolPatternFinder.findBestPattern(100, unorderedSymbols).toArray(); private static final ParserConfig parserConfig = ParserBuilder.startBuild() .parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM) .scaled() - .height(50) - .width(100) + .height(50) + .width(100) .getScale() .symbols(symbols) .colorAlgorithm(new AnsiColorAlgorithm()) @@ -34,7 +34,7 @@ public class ImageToAscii { private static PixelColor[] tones; public static void main(String[] args) throws IOException { - asciifyFile("src/main/resources/image/jorge.png"); + asciifyFile("/home/thiagoelias/IdeaProjects/image-to-ascii/src/main/resources/image/car.png"); } public static void asciifyFile(String fileName) throws IOException { @@ -43,7 +43,7 @@ public static void asciifyFile(String fileName) throws IOException { String asciiArt = AsciiParser.parse(betterImage, parserConfig); - File newFile = new File("/home/thiago/Desktop/projects/image-to-ascii/src/main/resources/" + image.getName().replaceAll("png|jpg|jpeg", "txt")); + File newFile = new File("myAscii.txt"); FileWriter fileWriter = new FileWriter(newFile); fileWriter.write(asciiArt); fileWriter.flush(); diff --git a/src/main/java/io/github/seujorgenochurras/demo/VideoToAsciiAnimation.java b/src/main/java/io/github/seujorgenochurras/demo/VideoToAsciiAnimation.java index 978b1ad..70581d9 100644 --- a/src/main/java/io/github/seujorgenochurras/demo/VideoToAsciiAnimation.java +++ b/src/main/java/io/github/seujorgenochurras/demo/VideoToAsciiAnimation.java @@ -7,11 +7,9 @@ import io.github.seujorgenochurras.image.ascii.ParserConfig; import io.github.seujorgenochurras.image.ascii.algorithm.pixel.bright.Algorithms; import io.github.seujorgenochurras.image.ascii.algorithm.pixel.color.DefaultColorType; -import io.github.seujorgenochurras.video.buffer.BufferedAsciiVideoParser; -import io.github.seujorgenochurras.video.buffer.PixelAnalyzerBuffer; import io.metaloom.video4j.Video4j; -import io.metaloom.video4j.VideoFile; -import io.metaloom.video4j.Videos; +import nu.pattern.OpenCV; +import org.opencv.videoio.VideoCapture; import static io.github.seujorgenochurras.util.StringUtils.getUTFChars; @@ -35,33 +33,42 @@ public class VideoToAsciiAnimation { .build(); public static void main(String[] args) throws InterruptedException { - Video4j.init(); - VideoFile videoFile = Videos.open(VIDEO_PATH); - - PixelAnalyzerBuffer pixelAnalyzerBuffer = new PixelAnalyzerBuffer(videoFile, 25); + System.loadLibrary("ffmepg"); - pixelAnalyzerBuffer.startQueue(); - - var bufferedAsciiVideoParser = new BufferedAsciiVideoParser(parserConfig, pixelAnalyzerBuffer, 10); + OpenCV.loadShared(); + OpenCV.loadLocally(); + Video4j.init(); + VideoCapture a = new VideoCapture(VIDEO_PATH); + Thread.sleep(4000); + System.out.println(a.isOpened()); - bufferedAsciiVideoParser.startQueue(); - System.out.print("\u001B[0J"); - Thread.sleep(5000); - long videoLength = pixelAnalyzerBuffer.getVideoFileLength(); - new Thread(() -> { - for (int i = 0; i < videoLength; i++) { - try { - Thread.sleep(1000 / DESIRED_FPS); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - String asciiArt = bufferedAsciiVideoParser.getAsciiArtQueue().poll(); - if (asciiArt == null) continue; - System.out.print("\u001B[1;1H"); - System.out.print(asciiArt); - } - }).start(); +// VideoFile videoFile = Videos.get(VIDEO_PATH); +// +// +// PixelAnalyzerBuffer pixelAnalyzerBuffer = new PixelAnalyzerBuffer(videoFile, 25); +// +// pixelAnalyzerBuffer.startQueue(); +// +// var bufferedAsciiVideoParser = new BufferedAsciiVideoParser(parserConfig, pixelAnalyzerBuffer, 10); +// +// bufferedAsciiVideoParser.startQueue(); +// +// System.out.print("\u001B[0J"); +// long videoLength = pixelAnalyzerBuffer.getVideoFileLength(); +// new Thread(() -> { +// for (int i = 0; i < videoLength; i++) { +// try { +// Thread.sleep(1000 / DESIRED_FPS); +// } catch (InterruptedException e) { +// throw new RuntimeException(e); +// } +// String asciiArt = bufferedAsciiVideoParser.getAsciiArtQueue().poll(); +// if (asciiArt == null) continue; +// System.out.print("\u001B[1;1H"); +// System.out.print(asciiArt); +// } +// }).start(); } diff --git a/src/main/java/io/github/seujorgenochurras/image/BetterImage.java b/src/main/java/io/github/seujorgenochurras/image/BetterImage.java index efb57a0..72eb7d1 100644 --- a/src/main/java/io/github/seujorgenochurras/image/BetterImage.java +++ b/src/main/java/io/github/seujorgenochurras/image/BetterImage.java @@ -1,8 +1,6 @@ package io.github.seujorgenochurras.image; -import io.github.seujorgenochurras.image.pixel.ImagePixel; import io.github.seujorgenochurras.image.pixel.ImagePixelGroup; -import io.github.seujorgenochurras.image.pixel.PixelBuilder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; @@ -16,17 +14,16 @@ public class BetterImage { public BetterImage(BufferedImage bufferedImage) { this.bufferedImage = bufferedImage; - pixels = PixelBuilder.build(this); + this.pixels = new ImagePixelGroup(bufferedImage); } - public BetterImage(String imagePath){ - this.bufferedImage = tryGenerateImageFromPath(imagePath); - pixels = PixelBuilder.build(this); + public BetterImage(String imagePath) { + this.bufferedImage = tryGenerateImageFromPath(imagePath); + this.pixels = new ImagePixelGroup(bufferedImage); } public BufferedImage tryGenerateImageFromPath(String imagePath) { File imageFile = new File(imagePath); - try { return ImageIO.read(imageFile); } catch (IOException e) { @@ -39,15 +36,6 @@ public BufferedImage getBufferedImage() { return bufferedImage; } - public void updateCurrentPixels() { - this.setPixels(pixels); - } - - public void setPixel(ImagePixel imagePixel) { - bufferedImage.setRGB(imagePixel.x, imagePixel.y, imagePixel.color.getRGB()); - - } - public int getWidth() { return bufferedImage.getWidth(); } @@ -60,7 +48,5 @@ public ImagePixelGroup getPixels() { return pixels; } - public void setPixels(ImagePixelGroup pixels) { - pixels.forEach(this::setPixel); - } + } diff --git a/src/main/java/io/github/seujorgenochurras/image/ascii/AsciiParser.java b/src/main/java/io/github/seujorgenochurras/image/ascii/AsciiParser.java index 2aa90c1..2b4da03 100644 --- a/src/main/java/io/github/seujorgenochurras/image/ascii/AsciiParser.java +++ b/src/main/java/io/github/seujorgenochurras/image/ascii/AsciiParser.java @@ -8,9 +8,12 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.util.logging.Logger; public class AsciiParser { + private static final Logger logger = Logger.getLogger("AsciiParser"); + private AsciiParser() { } @@ -18,12 +21,12 @@ private AsciiParser() { /** * Overloaded method of {@link AsciiParser#parse(BetterImage, ParserConfig)} */ - public static String parse(String imagePath, ParserConfig parserConfig){ + public static String parse(String imagePath, ParserConfig parserConfig) { File imageFile = new File(imagePath); - try{ + try { BetterImage betterImage = new BetterImage(ImageIO.read(imageFile)); return parse(betterImage, parserConfig); - } catch (IOException e){ + } catch (IOException e) { e.printStackTrace(); System.err.print(e.getMessage()); return "AN ERROR HAPPEN WHEN CREATING THE ASCII ART"; @@ -33,7 +36,7 @@ public static String parse(String imagePath, ParserConfig parserConfig){ /** * Generates ASCII art from your image by going through each pixel and * calculating its representation according to the algorithm defined in {@link ParserConfig}.

- * + *

* If your ParserConfig have less than 256 symbols, then the symbol representation will be defined in a symbolGap.
* A symbolGap is how many brightness values one character might represent:
* For instance, if you have a list with 128 symbols, then you have a symbol gap of 256 / 128 which is 2 @@ -41,12 +44,18 @@ public static String parse(String imagePath, ParserConfig parserConfig){ * A brightness unit is defined by the shades of white to black, * the maximum brightness a pixel/symbol can reach is 255, and the minimum 0. * - * @param betterImage image instance + * @param betterImage image instance * @param parserConfig AsciiParserConfig, must be built with {@link ParserBuilder} * @return ASCII art */ public static String parse(BetterImage betterImage, ParserConfig parserConfig) { var pixelLightSymbols = parserConfig.getSymbols(); + + if (pixelLightSymbols.length == 0) { + logger.warning("No symbols provided"); + return ""; + } + int symbolsGap = 255 / pixelLightSymbols.length; BetterImage scaledImage = scaleImage(betterImage, parserConfig.getScale()); @@ -54,7 +63,7 @@ public static String parse(BetterImage betterImage, ParserConfig parserConfig) { StringBuilder builder = new StringBuilder(); scaledImage.getPixels().forEach(pixel -> { - var color = pixel.color; + var color = pixel.getColor(); int red = color.getRed().getColorValue(); int green = color.getGreen().getColorValue(); @@ -75,8 +84,9 @@ public static String parse(BetterImage betterImage, ParserConfig parserConfig) { /** * Calculates and returns an element according to a brightness value on an element array. - * @param brightness element brightness value - * @param symbolsGap how many brightness units a symbol can represents, see {@link AsciiParser#parse} + * + * @param brightness element brightness value + * @param symbolsGap how many brightness units a symbol can represents, see {@link AsciiParser#parse} * @param pixelLightSymbols Array containing all the possible symbols * @return The element according to the given brightness value */ diff --git a/src/main/java/io/github/seujorgenochurras/image/ascii/ParserBuilder.java b/src/main/java/io/github/seujorgenochurras/image/ascii/ParserBuilder.java index 39a620c..fce5a7f 100644 --- a/src/main/java/io/github/seujorgenochurras/image/ascii/ParserBuilder.java +++ b/src/main/java/io/github/seujorgenochurras/image/ascii/ParserBuilder.java @@ -24,8 +24,8 @@ public static ParserBuilder startBuild() { /** * Sets the symbols to be used in the ascii art - * @param symbols from darkest to brightest * + * @param symbols from darkest to brightest */ public ParserBuilder symbols(String... symbols) { this.brightnessSymbols = symbols; @@ -34,6 +34,7 @@ public ParserBuilder symbols(String... symbols) { /** * Starts the image scale builder + * * @return PixelScaleBuilder */ public PixelScaleConfig scaled() { @@ -44,10 +45,12 @@ public ParserBuilder parserAlgorithm(ParserAlgorithm algorithm) { this.algorithm = algorithm; return this; } + public ParserBuilder parserAlgorithm(Algorithms algorithm) { this.algorithm = algorithm.getAlgorithm(); return this; } + public ParserBuilder reversed(boolean isSymbolWhiteToBlack) { this.isSymbolReverted = isSymbolWhiteToBlack; return this; @@ -60,6 +63,7 @@ public ParserBuilder colorAlgorithm(DefaultColorType defaultColorType) { /** * Sets the pixel to be colored + * * @param colorAlgorithm the algorithm to colorize your characters * @return builder */ @@ -69,7 +73,6 @@ public ParserBuilder colorAlgorithm(ColorAlgorithm colorAlgorithm) { } /** - * * @return your pixelParserConfig ready to be used */ public ParserConfig build() { @@ -107,6 +110,7 @@ public PixelScaleConfig height(int height) { /** * Sets the image scaling algorithm, this doesn't change much + * * @param algorithm * @return */ @@ -116,7 +120,6 @@ public PixelScaleConfig algorithm(PixelScaleAlgorithm algorithm) { } /** - * * @return the built pixelScaleConfig */ public ParserBuilder getScale() { diff --git a/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixel.java b/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixel.java index 3c862c5..a36f4fe 100644 --- a/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixel.java +++ b/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixel.java @@ -15,4 +15,16 @@ public ImagePixel(Color color, int x, int y) { this.x = x; this.y = y; } + + public int x() { + return x; + } + + public int y() { + return y; + } + + public PixelColor getColor() { + return color; + } } \ No newline at end of file diff --git a/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixelGroup.java b/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixelGroup.java index c14ddd6..029b35c 100644 --- a/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixelGroup.java +++ b/src/main/java/io/github/seujorgenochurras/image/pixel/ImagePixelGroup.java @@ -1,7 +1,39 @@ package io.github.seujorgenochurras.image.pixel; +import java.awt.*; +import java.awt.image.BufferedImage; import java.util.ArrayList; +import java.util.function.Consumer; public class ImagePixelGroup extends ArrayList { - //bruh + private final BufferedImage bufferedImage; + + public ImagePixelGroup(int initialCapacity, BufferedImage bufferedImage) { + super(initialCapacity); + this.bufferedImage = bufferedImage; + } + + public ImagePixelGroup(BufferedImage bufferedImage) { + this.bufferedImage = bufferedImage; + } + + @Override + public void forEach(Consumer imagePixelConsumer) { + int imageWidth = bufferedImage.getWidth(); + int imageHeight = bufferedImage.getHeight(); + for (int y = 0; y < imageHeight; y++) { + for (int x = 0; x < imageWidth; x++) { + + Color pixelColor = new Color(bufferedImage.getRGB(x, y)); + ImagePixel pixel = new ImagePixel(pixelColor, x, y); + imagePixelConsumer.accept(pixel); + } + } + + } + + @Override + public int size() { + return bufferedImage.getHeight() * bufferedImage.getWidth(); + } } diff --git a/src/main/java/io/github/seujorgenochurras/image/pixel/PixelBuilder.java b/src/main/java/io/github/seujorgenochurras/image/pixel/PixelBuilder.java index b9ecfe8..30f8a71 100644 --- a/src/main/java/io/github/seujorgenochurras/image/pixel/PixelBuilder.java +++ b/src/main/java/io/github/seujorgenochurras/image/pixel/PixelBuilder.java @@ -2,7 +2,6 @@ import io.github.seujorgenochurras.image.BetterImage; -import java.awt.*; import java.awt.image.BufferedImage; public class PixelBuilder { @@ -10,19 +9,7 @@ private PixelBuilder() { } public static ImagePixelGroup build(BufferedImage image) { - ImagePixelGroup pixels = new ImagePixelGroup(); - - final int imageWidth = image.getWidth(); - final int imageHeight = image.getHeight(); - - //TODO Profile it and fix this massive ram drain - for (int i = 0; i < imageHeight; i++) { - for (int j = 0; j < imageWidth; j++) { - Color pixelColor = new Color(image.getRGB(j, i)); - pixels.add(new ImagePixel(pixelColor, j, i)); - } - } - return pixels; + return new ImagePixelGroup(image); } public static ImagePixelGroup build(BetterImage betterImage) { diff --git a/src/main/java/io/github/seujorgenochurras/image/pixel/color/PixelColor.java b/src/main/java/io/github/seujorgenochurras/image/pixel/color/PixelColor.java index e8cc66c..357e3aa 100644 --- a/src/main/java/io/github/seujorgenochurras/image/pixel/color/PixelColor.java +++ b/src/main/java/io/github/seujorgenochurras/image/pixel/color/PixelColor.java @@ -21,18 +21,6 @@ public PixelColor(Color color) { } - public int getRGB() { - return instantiateCurrentColor().getRGB(); - } - - /*** - this method is needed to update current color, because the class Color is immutable - * @return a new color - */ - public Color instantiateCurrentColor() { - return new Color(getRed().colorValue, getGreen().colorValue, getBlue().colorValue, getAlpha().colorValue); - } - public Red getRed() { return red; } diff --git a/src/test/java/io/github/seujorgenochurras/color/FindBestSymbolPatternTest.java b/src/test/java/io/github/seujorgenochurras/color/FindBestSymbolPatternTest.java index 2b90290..1185720 100644 --- a/src/test/java/io/github/seujorgenochurras/color/FindBestSymbolPatternTest.java +++ b/src/test/java/io/github/seujorgenochurras/color/FindBestSymbolPatternTest.java @@ -6,10 +6,5 @@ import static org.junit.jupiter.api.Assertions.*; class FindBestSymbolPatternTest { - @Test - void isFindingBestPattern(){ - var symbols = BestSymbolPatternFinder.findBestPattern(10, 110, getUTFChars(32, 126)); - String[] expectedSymbols = {" ", "`", "'", ",", ":", "^", "*", "+", "-", ".", "r", ")", "/", "a", "E", "p", "G", "0", "1", "2", "3", "9", "@", "A", "B", "D", "H", "M", "W"}; - assertEquals(expectedSymbols, symbols.toArray()); - } + }