UNPKG

imagerot

Version:

A lightweight, cross-environment image library for applying unique effects via raw image buffers.

47 lines (46 loc) 2.75 kB
"use strict"; 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.chimera = void 0; const math_1 = require("../../constants/math"); const helpers_1 = require("../../helpers"); const weight = [0.25, 0.5]; const chimera = (_a) => __awaiter(void 0, [_a], void 0, function* ({ data, width, height, effects }) { const algorithm = (_a) => __awaiter(void 0, [_a], void 0, function* ({ data, width, height }) { for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { let index = (y * width + x) * 4; let r = data[index], g = data[index + 1], b = data[index + 2]; data[index + 0] = (r + g * weight[1] + b * weight[0]); data[index + 1] = (r * weight[1] + g + b * weight[0]); data[index + 2] = (r * weight[0] + g * weight[1] + b); } } return data; }); const direction = (0, math_1.random)() >= 0.5 ? 'horizontal' : 'vertical'; const intensity = (0, helpers_1.randomize)(5, 10); data = (yield effects.blur.method({ data, width, height }, { direction, intensity })) || data; data = yield algorithm({ data, width, height }); for (let index = 0; index < data.length; index += 4) { const useNoise = (0, math_1.random)() < 0.2; const useGrain = (0, math_1.random)() < 0.4 ? (0, math_1.floor)((0, math_1.random)() * 50) : 0; for (let i = 0; i < 3; i++) { data[index + i] = useNoise ? (0, math_1.min)(data[index + i] + (0, helpers_1.randomize)(1, i === 0 ? 15 : 10), 255) : data[index + i]; data[index + i] = (0, math_1.min)(255, (0, math_1.max)(0, data[index + i] + ((0, math_1.floor)((0, math_1.random)() * 20) - 30))); data[index + i] = useGrain ? (0, math_1.min)(255, (0, math_1.max)(0, data[index + i] + useGrain)) : data[index + i]; } ; } data = (yield effects.rectangles.method({ data, width, height }, { offset: 10, intensity: 15, sizeModifier: 1.25 })) || data; return data; }); exports.chimera = chimera;