Dither
Quick (as in hacked together) Floyd Steinberg dither.
import coracle.Drawing
import coracle.Image
class ImageDither: Drawing() {
var testImage: Image? = null
var threshold = 128
override fun setup() {
(550, 414)
size("salts_mill.png"){ image ->
loadImage("Image loaded: ${image?.path}")
print= image
testImage }
}
override fun draw() {
?.let{ image ->
testImage(image, 0, 0)
image()
loadPixels
val errors = Array(width) { IntArray(height) }
for (y in 0 until height - 1) {
for (x in 1 until width - 1) {
val gray = (pixel(x, y)?.shr(16) ?: 0) and 0xFF
var error: Int
when {
+ errors[x][y] < threshold -> {
gray = gray + errors[x][y]
error (BLACK)
stroke(x, y)
point}
else -> {
= gray + errors[x][y] - 255
error (WHITE)
stroke(x, y)
point}
}
[x + 1][y] += 7 * error / 16
errors[x - 1][y + 1] += 3 * error / 16
errors[x][y + 1] += 5 * error / 16
errors[x + 1][y + 1] += 1 * error / 16
errors}
}
()
noLoop}
}
}