imagerot
Version:
A lightweight, cross-environment image library for applying unique effects via raw image buffers.
42 lines (41 loc) • 2.04 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.dither = void 0;
const defaultIntensity = 0.5;
const global = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = null) {
const { intensity = defaultIntensity } = (options || {});
for (let i = 0; i < data.length; i += 4) {
const grayscale = (data[i] + data[i + 1] + data[i + 2]) / 3;
data[i] = data[i + 1] = data[i + 2] = grayscale;
}
for (let i = 0; i < data.length; i += 4) {
const nPixel = data[i] > (intensity * 255) ? 255 : 0;
const quantError = data[i] - nPixel;
data[i] = nPixel;
if (i + 4 < data.length)
data[i + 4] = data[i + 4] + quantError * 7 / 16;
if (i + (width * 4) < data.length)
data[i + (width * 4)] = data[i + (width * 4)] + quantError * 5 / 16;
if (i + (width * 4) + 4 < data.length)
data[i + (width * 4) + 4] = data[i + (width * 4) + 4] + quantError * 1 / 16;
if (i + (width * 4) - 4 >= 0)
data[i + (width * 4) - 4] = data[i + (width * 4) - 4] + quantError * 3 / 16;
data[i + 1] = data[i + 2] = data[i];
}
return new Uint8Array(data);
});
const dither = {
name: 'dither',
browser: global,
node: global
};
exports.dither = dither;