Hello, I have a question. I’m trying to re-create the typical ASCII art effect, where I divide an input image into a grid, and for each cell check the containing pixels brightness value and map that to a selection of chars.
I have that sketch for Processing, where I make use of functions that are not available in OpenRNDR (by convention I guess):
pgr.imageMode(CENTER);
pgr.textAlign(CENTER, CENTER);
However, my problem is the aligment of the grid relative to the window, the cells are misaligned in my implementation, and I cannot figure out why, or how I should proceed:
{
configure {
width = 1024
height = 1024
}
oliveProgram {
var img = loadImage("data/images/123.png")
val pixels = img.shadow
pixels.download()
val chars = listOf(
"%/*+-. ",
"╕╒╛╘╪╞╡╧╤",
"Ñs@#W$9876543210?!abc;:+=-,._",
"+"
)
val fonts = listOf(
"data/fonts/default.otf",
)
val fontSize = 64.0
val font = loadFont(fonts[0], fontSize)
val charSet = chars[3] // for debugging purposes
val tilesX = 16
val tilesY = 16
val tileW = img.width.toDouble() / tilesX
val tileH = img.height.toDouble() / tilesY
val pgr = renderTarget(img.width, img.height) {
colorBuffer()
}
extend {
drawer.isolated {
drawer.fontMap = font
drawer.withTarget(pgr) {
clear(ColorRGBa.BLACK)
fill = ColorRGBa.WHITE
translate(tileW / 2, tileH / 2)
for (x in 0 until tilesX) {
for (y in 0 until tilesY) {
val px = (x * tileW).toInt()
val py = (y * tileH).toInt()
val color = pixels[px, py]
val brightness = color.luminance
val selector = map(0.0, 1.0, 0.0, charSet.length.toDouble(), brightness)
isolated {
drawStyle.smooth = false
translate(x * tileW, y * tileH)
text(charSet[selector.toInt()].toString(), Vector2.ZERO, Vector2(0.0, 0.0))
}
}
}
}
drawer.image(pgr.colorBuffer(0), 0.0, 0.0, width.toDouble(), height.toDouble())
}
}
}
}
fun Drawer.text(txt: String, pos: Vector2, align: Vector2) {
fontMap?.let { fm ->
val writer = Writer(this)
val off = Vector2(
-writer.textWidth(txt) * align.x,
fm.height * (1 - align.y)
)
text(txt, pos + off)
}
}
The last function is from a different thread about text alignment.
Any hints would be appreciated.
Edit: for testing purposes, the sketch will display the problem when using a 1024x1024 image as input:
Ideally I want to have even spacing at the margins.