diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a438551..aad3edd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,7 +8,10 @@ - + + + + @@ -24,6 +27,9 @@ + + + @@ -31,18 +37,28 @@ - - + + - + - - + + + + + + + + + + + + @@ -51,10 +67,11 @@ - - + + - + + @@ -63,10 +80,10 @@ - - + + - + @@ -79,52 +96,20 @@ - - + + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -144,10 +129,6 @@ - vi - code by - ImageToAASubGly - ==\nleft empty\n== algs.model.kdtree algs.model \[[A-Za-z]+\] @@ -174,6 +155,10 @@ fun find buffer void render + image + imageBuffer + getLuminance + println( kdtree @@ -214,15 +199,15 @@ @@ -248,8 +233,6 @@ - - @@ -302,32 +285,6 @@ - - - - - - - - - - - - - - + + @@ -574,6 +533,15 @@ + + + + + + + + + @@ -583,13 +551,15 @@ + - + + @@ -602,9 +572,7 @@ - - @@ -625,7 +593,7 @@ - + @@ -643,7 +611,8 @@ - + + @@ -706,13 +675,7 @@ - - - - - - - + @@ -766,16 +729,6 @@ - - - - - - - - - - @@ -793,99 +746,108 @@ - + + - - - + + - + - - - + + - + - - - - - + + + - + - + - - + - - + + + + + + - + - - + + - + - - + + - + - + - - + + + - + - - - - - + + + - + - - + + - + - - + + + + + + + + + + + + diff --git a/config.properties b/config.properties index 844af21..5de8030 100644 --- a/config.properties +++ b/config.properties @@ -116,7 +116,7 @@ bDemoCredit=true # Forces the renderer to make exact calculation # Will drop the performance significantly # default: false -bNoApproximate=false +bNoApproximate=true # Explicitly set the limitation of search depth # Lower value: less accurate, faster diff --git a/src/net/torvald/aa/ImageToAA.kt b/src/net/torvald/aa/ImageToAA.kt index c1c089d..8220bed 100644 --- a/src/net/torvald/aa/ImageToAA.kt +++ b/src/net/torvald/aa/ImageToAA.kt @@ -414,3 +414,21 @@ fun Color.getLuminance(colourAlgo: Int, gamma: Double): Double = Math.pow((3 * r + b + 4 * g) / 8.0, gamma) else throw IllegalArgumentException("Unknown luminance algorithm: $colourAlgo") + +fun getLuminance(redByte: Int, greenByte: Int, blueByte: Int, colourAlgo: Int, gamma: Double): Double { + val r = redByte / 255f + val g = greenByte / 255f + val b = blueByte / 255f + + return if (redByte > 0xF8 && greenByte >= 0xF8 && blueByte >= 0xF8) // mask irregularity in white colour + 1.0 + else if (redByte < 0x8 && greenByte < 0x8 && blueByte < 0x8) + 0.0 + else + if (colourAlgo == 1) + (0.299 * r.sqr() + 0.587 * g.sqr() + 0.114 * b.sqr()).powerOf(0.5 * gamma) + else if (colourAlgo == 0) + Math.pow((3 * r + b + 4 * g) / 8.0, gamma) + else + throw IllegalArgumentException("Unknown luminance algorithm: $colourAlgo") +} \ No newline at end of file diff --git a/src/net/torvald/aa/ImageToAASubGlyph4.kt b/src/net/torvald/aa/ImageToAASubGlyph4.kt index ea20f31..fc75946 100644 --- a/src/net/torvald/aa/ImageToAASubGlyph4.kt +++ b/src/net/torvald/aa/ImageToAASubGlyph4.kt @@ -19,7 +19,11 @@ import java.util.* * * FIXME: lots of 'q's artefact */ -class ImageToAASubGlyph4 : AsciiAlgo { +@Deprecated( + "I won't hack with this one; use ImageToAASubGlyphArb.", + ReplaceWith("net.torvald.aa.ImageToAASubGlyphArb"), + DeprecationLevel.WARNING +) class ImageToAASubGlyph4 : AsciiAlgo { private var w = 0 private var h = 0 diff --git a/src/net/torvald/aa/ImageToAASubGlyphArb.kt b/src/net/torvald/aa/ImageToAASubGlyphArb.kt index 5699cec..48fa35d 100644 --- a/src/net/torvald/aa/ImageToAASubGlyphArb.kt +++ b/src/net/torvald/aa/ImageToAASubGlyphArb.kt @@ -1,6 +1,7 @@ package net.torvald.aa import net.torvald.aa.demoplayer.BaAA +import net.torvald.aa.demoplayer.toUint import org.newdawn.slick.* import java.util.* @@ -53,7 +54,7 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { else fontRange = 32..126 - imageBuffer = Image(w, h) + pixelDataBufferFrameBuffer = Image(w, h) // re-invert colourmap so that the calculation stay normal when inverted colourMap = Array(BaAA.colors.size, { 0.0 }) @@ -97,7 +98,8 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { private lateinit var brightnessKDTree: KDHeapifiedTree - internal lateinit var imageBuffer: Image + internal lateinit var pixelDataBufferFrameBuffer: Image + internal lateinit var pixelDataBuffer: ByteArray private var precalcDone = false @@ -116,6 +118,9 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { private var totalGlyphCount = 0 override fun precalcFont() { + // NOTE: diabolical "getColor()" is used, but it's a loading stage and it's not that long + // so I'll just leave it. + if (!precalcDone) { val fontSheetW = fontSheet.width / fontW @@ -241,7 +246,9 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { override fun toAscii(rawImage: Image, aaframe: AAFrame) { // draw scale-flagged (getScaledCopy) image to the buffer - imageBuffer.graphics.drawImage(rawImage.getScaledCopy(w, h), 0f, 0f) + // so that the image is actually resized + pixelDataBufferFrameBuffer.graphics.drawImage(rawImage.getScaledCopy(w, h), 0f, 0f) + pixelDataBuffer = pixelDataBufferFrameBuffer.texture.textureData // yes, we're going into the texture for (y in 0..h - 1 step divH) { for (x in 0..w - 1 step divW) { @@ -320,7 +327,7 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { } // clear buffer - imageBuffer.graphics.flush() + pixelDataBufferFrameBuffer.graphics.flush() } fun Array.set(x: Int, y: Int, value: Int) { @@ -335,13 +342,29 @@ class ImageToAASubGlyphArb(val divW: Int, val divH: Int) : AsciiAlgo { } fun bufferPixelFontLum(x: Int, y: Int): Int { + val pixel = getPixelFromTexture(x, y) val lum = // [0.0 - 1.0] - imageBuffer.getColor(x, y).getLuminance(colourAlgo, gamma) + net.torvald.aa.getLuminance( + pixel[0].toUint(), pixel[1].toUint(), pixel[2].toUint(), + colourAlgo, gamma + ) val delta = lumMax - lumMin return (lumMin + delta * lum).roundInt() } + fun getPixelFromTexture(x: Int, y: Int): ByteArray { + val textureWidth = nextPowerOfTwo(w) // slick2d's behaviour + + val oneDimPos = 4 * (textureWidth * y + x) // 4: # of channels (RGBA) + + return byteArrayOf( + pixelDataBuffer[oneDimPos], + pixelDataBuffer[oneDimPos + 1], + pixelDataBuffer[oneDimPos + 2] + ) + } + /** * @param fontLum : int ranged 0..fontLumMax * @return indexed lum 0..fontLumMax