Added darkrgba and lightrgba attributes

This commit is contained in:
Andrew Stephens
2023-08-30 22:27:12 -04:00
parent 88b75eec78
commit a48a658205
3 changed files with 58 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
onmessage = function (e) {
const result = dither(e.data.imageData, e.data.pixelSize, e.data.cutoff)
const result = dither(e.data.imageData, e.data.pixelSize, e.data.cutoff, e.data.blackRGBA, e.data.whiteRGBA)
const reply = {}
reply.imageData = result
reply.pixelSize = e.data.pixelSize
@@ -8,7 +8,17 @@ onmessage = function (e) {
postMessage(reply)
}
function dither(imageData, scaleFactor, cutoff) {
function getRGBAArrayBuffer(color) {
let buffer = new ArrayBuffer(4)
for (let i = 0; i < 4; ++i) {
buffer[i] = color[i]
}
return buffer
}
function dither(imageData, scaleFactor, cutoff, blackRGBA, whiteRGBA) {
const blackRGBABuffer = getRGBAArrayBuffer(blackRGBA)
const whiteRGBABuffer = getRGBAArrayBuffer(whiteRGBA)
let output = new ImageData(imageData.width * scaleFactor, imageData.height * scaleFactor)
for (let i = 0; i < imageData.data.length; i += 4) {
imageData.data[i] = imageData.data[i + 1] = imageData.data[i + 2] = Math.floor(imageData.data[i] * 0.3 + imageData.data[i + 1] * 0.59 + imageData.data[i + 2] * 0.11)
@@ -42,12 +52,15 @@ function dither(imageData, scaleFactor, cutoff) {
// this is stupid but we have to do the pixel scaling ourselves because safari insists on interpolating putImageData
// which gives us blurry pixels (and it doesn't support the createImageBitmap call with an ImageData instance which
// would make this easy)
let rgba = (monoValue == 0) ? blackRGBABuffer : whiteRGBABuffer
for (let scaleY = 0; scaleY < scaleFactor; ++scaleY) {
let pixelOffset = (((y * scaleFactor + scaleY) * output.width) + (x * scaleFactor)) * 4
for (let scaleX = 0; scaleX < scaleFactor; ++scaleX) {
output.data[pixelOffset] = output.data[pixelOffset + 1] = output.data[pixelOffset + 2] = monoValue
output.data[pixelOffset + 3] = 255
output.data[pixelOffset] = rgba[0]
output.data[pixelOffset + 1] = rgba[1]
output.data[pixelOffset + 2] = rgba[2]
output.data[pixelOffset + 3] = rgba[3]
pixelOffset += 4
}
}