Skip to content

Commit

Permalink
kotlin: rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalourdio committed May 6, 2024
1 parent 9281b0f commit 1cf0115
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 153 deletions.
Binary file modified bin/flhacker.zip
Binary file not shown.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<properties>
<java.version>21</java.version>
<kotlin.version>1.9.23</kotlin.version>

<commons-cli.version>1.7.0</commons-cli.version>
<commons-io.version>2.16.1</commons-io.version>
Expand Down Expand Up @@ -52,9 +53,18 @@
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
Expand All @@ -72,6 +82,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.mpalourdio.projects.flhacker

import com.mpalourdio.projects.flhacker.utils.AudiofileHandler
import com.mpalourdio.projects.flhacker.utils.CliHandler
import org.apache.commons.cli.CommandLine
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean

@SpringBootApplication
class FlhackerApplication {

@Bean
fun run(): CommandLineRunner {
return CommandLineRunner { args ->
AudiofileHandler.setUp()
val cmd: CommandLine? = CliHandler.run(args)
try {
AudiofileHandler.extractResizeSaveArtwork(cmd?.getOptionValue(CliHandler.FILE_CMD_LONG_OPTION))
AudiofileHandler.generateAsciiArt()
} finally {
AudiofileHandler.tearDown()
}
}
}
}

fun main(args: Array<String>) {
runApplication<FlhackerApplication>(*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.mpalourdio.projects.flhacker.utils

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.io.FileUtils
import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.audio.exceptions.CannotReadException
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException
import org.jaudiotagger.tag.TagException
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import javax.imageio.ImageIO

object AudiofileHandler {
private val TMP_DIR: String = System.getProperty("java.io.tmpdir")
private val TMP_RESIZED_ARTWORK = "$TMP_DIR/resized.jpg"
private const val TARGET_SIZE = 80
private val TMP_RESIZED_FILE = File(TMP_RESIZED_ARTWORK)

@JvmStatic
fun setUp() {
FileUtils.deleteQuietly(TMP_RESIZED_FILE)
}

@JvmStatic
fun tearDown() {
setUp()
}

@JvmStatic
@Throws(
CannotReadException::class,
TagException::class,
InvalidAudioFrameException::class,
ReadOnlyFileException::class,
IOException::class
)
fun extractResizeSaveArtwork(audioFilePath: String?) {
val audioFile = File(audioFilePath)
val extractedArtwork = AudioFileIO.read(audioFile)
.tag
.firstArtwork
.image

val scaledArtwork =
(extractedArtwork as BufferedImage).getScaledInstance(TARGET_SIZE, TARGET_SIZE, Image.SCALE_DEFAULT)
val resized = BufferedImage(TARGET_SIZE, TARGET_SIZE, BufferedImage.TYPE_INT_RGB)
resized.graphics.drawImage(scaledArtwork, 0, 0, null)

ImageIO.write(resized, "png", TMP_RESIZED_FILE)
}

@JvmStatic
fun generateAsciiArt() {
val symbols = arrayOf(" ", ".", "-", "I", "W", "@")
val parserConfig = ParserBuilder.startBuild()
.parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM)
.scaled()
.height(TARGET_SIZE)
.width(TARGET_SIZE)
.scale
.symbols(*symbols)
.colorAlgorithm(DefaultColorType.ANSI)
.build()

val asciiArt = AsciiParser.parse(TMP_RESIZED_ARTWORK, parserConfig)

println(asciiArt)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.mpalourdio.projects.flhacker.utils

import org.apache.commons.cli.*
import kotlin.system.exitProcess

object CliHandler {
const val FILE_CMD_LONG_OPTION: String = "file"

fun run(args: Array<String?>?): CommandLine? {
val options = Options()
val input =
Option("f", FILE_CMD_LONG_OPTION, true, "audio file path which contains the artwork to print")

input.isRequired = true
options.addOption(input)

val parser: CommandLineParser = DefaultParser()
val formatter = HelpFormatter()
val cmd: CommandLine?

try {
cmd = parser.parse(options, args)
} catch (e: ParseException) {
println(e.message)
formatter.printHelp("utility-name", options)
exitProcess(1)
}

return cmd
}
}

0 comments on commit 1cf0115

Please sign in to comment.