UNPKG

imagerot

Version:

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

56 lines (55 loc) 2.43 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.blur = void 0; const defaultDirection = 'horizontal'; const defaultIntensity = 5; const global = (_a, ...args_1) => __awaiter(void 0, [_a, ...args_1], void 0, function* ({ data, width, height }, options = null) { const { direction = defaultDirection, intensity = defaultIntensity } = (options || {}); const buffer = new Uint8Array(data.length); buffer.set(data); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { let index = (y * width + x) * 4; let sumR = 0, sumG = 0, sumB = 0, count = 0; for (let i = 1; i <= intensity; i++) { if (direction === 'horizontal') { if (x + i < width) { let nextIndex = (y * width + x + i) * 4; sumR += data[nextIndex]; sumG += data[nextIndex + 1]; sumB += data[nextIndex + 2]; count++; } } else if (direction === 'vertical') { if (y + i < height) { let nextIndex = ((y + i) * width + x) * 4; sumR += data[nextIndex]; sumG += data[nextIndex + 1]; sumB += data[nextIndex + 2]; count++; } } } buffer[index] = sumR / count; buffer[index + 1] = sumG / count; buffer[index + 2] = sumB / count; } } return buffer; }); const blur = { name: 'blur', browser: global, node: global }; exports.blur = blur;