Skip to content

Commit

Permalink
feat: use image-to-ascii lib. Better API and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalourdio committed May 5, 2024
1 parent 435bce4 commit 85fc0cb
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 94 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Who knows ?

- Uses `Spring Boot` as base, because life is too short.
- Uses [Jaudiotagger](https://www.jthink.net/jaudiotagger/).
- Uses [image-to-ascii](https://github.com/seujorgenochurras/image-to-ascii)

# Requirements for hacking the codebase

Expand All @@ -34,7 +35,7 @@ The needed `reachabilty metadata` are in the [src/main/resources/META-INF/native

# JUST MAKE A FUCKING .EXE FILE AND GIVE IT TO ME

The running executable available in the [bin](https://github.com/mpalourdio/flhacker/raw/main/bin/flhacker) is linux x64 only. I may forget to update the bin because this is not automated for now. It may also not target your OS/CPU architecture.
The running executable available in the [zip](https://github.com/mpalourdio/flhacker/raw/main/bin/flhacker.zip) is linux x64 only. I may forget to update the bin because this is not automated for now. It may also not target your OS/CPU architecture.

The best way to be up-to-date is to clone the repo and compile the sources if you know what you are doing (or open an issue).

Expand Down
Binary file added bin/flacker.zip
Binary file not shown.
Binary file removed bin/flhacker
Binary file not shown.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>io.github.seujorgenochurras</groupId>
<artifactId>image-to-ascii</artifactId>
<version>0.0.4</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

package com.mpalourdio.projects.flhacker;

import io.github.seujorgenochurras.image.ascii.AsciiParser;
import io.github.seujorgenochurras.image.ascii.ParserBuilder;
import io.github.seujorgenochurras.image.ascii.algorithm.pixel.bright.Algorithms;
import io.github.seujorgenochurras.image.ascii.algorithm.pixel.color.DefaultColorType;
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
import org.jaudiotagger.audio.AudioFileIO;
Expand All @@ -24,14 +28,13 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@SpringBootApplication
public class FlhackerApplication {

static final String TMP_DIR = System.getProperty("java.io.tmpdir");
private static final String TMP_RESIZED_ARTWORK = TMP_DIR + "/resized.jpg";

private static final int TARGET_SIZE = 80;
public static final String FILE_CMD_LONG_OPTION = "file";

Expand All @@ -40,10 +43,9 @@ public static void main(String[] args) throws IOException, CannotReadException,

// init cleanup if files already exist
var tmpResizedFile = new File(TMP_RESIZED_ARTWORK);
FileUtils.deleteQuietly(new File(Img2Ascii.TMP_ASCIIART_TXT));
FileUtils.deleteQuietly(tmpResizedFile);

// CLI handgling
// CLI handling
var options = new Options();
var input = new Option("f", FILE_CMD_LONG_OPTION, true, "input file path which containe the artwork to print");
input.setRequired(true);
Expand All @@ -61,22 +63,38 @@ public static void main(String[] args) throws IOException, CannotReadException,
System.exit(1);
}

// main process
var audioFile = new File(cmd.getOptionValue(FILE_CMD_LONG_OPTION));
var extractedArtwork = AudioFileIO.read(audioFile)
.getTag()
.getFirstArtwork()
.getImage();
try {
// extract artwork
var audioFile = new File(cmd.getOptionValue(FILE_CMD_LONG_OPTION));
var extractedArtwork = AudioFileIO.read(audioFile)
.getTag()
.getFirstArtwork()
.getImage();

// resize and save tmp artwork
var scaledArtwork = ((BufferedImage) extractedArtwork).getScaledInstance(TARGET_SIZE, TARGET_SIZE, Image.SCALE_DEFAULT);
var resized = new BufferedImage(TARGET_SIZE, TARGET_SIZE, BufferedImage.TYPE_INT_RGB);
resized.getGraphics().drawImage(scaledArtwork, 0, 0, null);

var scaledArtwork = ((BufferedImage) extractedArtwork).getScaledInstance(TARGET_SIZE, TARGET_SIZE, Image.SCALE_DEFAULT);
var resized = new BufferedImage(TARGET_SIZE, TARGET_SIZE, BufferedImage.TYPE_INT_RGB);
resized.getGraphics().drawImage(scaledArtwork, 0, 0, null);
ImageIO.write(resized, "png", tmpResizedFile);

ImageIO.write(resized, "png", tmpResizedFile);
// tmp artwork to ascii art
var symbols = new String[]{" ", ".", "-", "I", "W", "@"};
var parserConfig = ParserBuilder.startBuild()
.parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM)
.scaled()
.height(TARGET_SIZE)
.width(TARGET_SIZE)
.getScale()
.symbols(symbols)
.colorAlgorithm(DefaultColorType.ANSI)
.build();

var img2Ascii = new Img2Ascii();
img2Ascii.convertToAscii(TMP_RESIZED_ARTWORK);
var asciiArt = AsciiParser.parse(TMP_RESIZED_ARTWORK, parserConfig);

System.out.println(Files.readString(Path.of(Img2Ascii.TMP_ASCIIART_TXT)));
System.out.println(asciiArt);
} finally {
FileUtils.deleteQuietly(tmpResizedFile);
}
}
}
76 changes: 0 additions & 76 deletions src/main/java/com/mpalourdio/projects/flhacker/Img2Ascii.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/main/resources/META-INF/native-image/reflect-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
{
"name":"jakarta.validation.Validator"
},
{
"name":"java.awt.image.ColorConvertOp"
},
{
"name":"java.io.FilePermission"
},
Expand Down Expand Up @@ -124,6 +127,9 @@
"name":"java.net.URLPermission",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","java.lang.String"] }]
},
{
"name":"java.nio.ByteBuffer"
},
{
"name":"java.security.AllPermission"
},
Expand Down Expand Up @@ -243,6 +249,10 @@
"name":"org.jaudiotagger.tag.datatype.PartOfSet",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.datatype.PartOfSet"] }]
},
{
"name":"org.jaudiotagger.tag.datatype.StringHashMap",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.datatype.StringHashMap"] }]
},
{
"name":"org.jaudiotagger.tag.datatype.StringNullTerminated",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.datatype.StringNullTerminated"] }]
Expand All @@ -263,6 +273,18 @@
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC"] }, {"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyPIC"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC"] }, {"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyPIC"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyCOMM"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyMCDI",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyMCDI"] }]
Expand All @@ -271,6 +293,10 @@
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTALB",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTALB"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTCOM",
"methods":[{"name":"<init>","parameterTypes":["java.nio.ByteBuffer","int"] }, {"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTCOM"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTCON",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTCON"] }]
Expand All @@ -283,6 +309,10 @@
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTDRC",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTYER"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTENC",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTENC"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTIT2",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTIT2"] }]
Expand All @@ -307,6 +337,14 @@
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTPOS",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTPOS"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTRCK",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTRCK"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTYER",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTYER"] }]
},
{
"name":"org.jaudiotagger.tag.id3.framebody.FrameBodyTPUB",
"methods":[{"name":"<init>","parameterTypes":["org.jaudiotagger.tag.id3.framebody.FrameBodyTPUB"] }]
Expand Down

0 comments on commit 85fc0cb

Please sign in to comment.