UNPKG

magica

Version:

ImageMagick for browser and Node.js, easy setup, high level API and Command Line Interface, including WASM binary for an easy setup.

106 lines 4.25 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 }); const misc_utils_of_mine_generic_1 = require("misc-utils-of-mine-generic"); const file_1 = require("../file/file"); const main_1 = require("../main/main"); /** * Returns given rgba image x,y pixel. If the image is not rgba the behavior is undefined. */ function getRgbaPixel(f, x, y) { // if (f.name.endsWith('rgba') && f.width && f.height) { const i = coordsToIndex(f.width, x, y); const c = f.content; return { r: c[i], g: c[i + 1], b: c[i + 2], a: c[i + 3] }; // return `rgba(${r},${g},${b},${a})` // } else { // throw new Error('Image must be .rgba and have own height and width properties') // } } exports.getRgbaPixel = getRgbaPixel; function coordsToIndex(width, x, y) { return (width * y + x) * 4; } exports.coordsToIndex = coordsToIndex; function getPixels(f) { return __awaiter(this, void 0, void 0, function* () { var data = yield f.asRGBAImageData(); var pixels = []; for (let y = 0; y < data.height; y++) { // var row: Rgba[] = [] // pixels.push(row) for (let x = 0; x < data.width; x++) { // const i = (f.width! * y + x) * 4 pixels.push(getRgbaPixel(f, x, y)); } } return pixels; }); } exports.getPixels = getPixels; function imagePixelColor(img, x, y) { return __awaiter(this, void 0, void 0, function* () { if (!img) { return; } var f = file_1.File.asFile(img); if (isRgbaImage(f)) { const c = getRgbaPixel(f, x, y); return rgbaToString(c); } else { const { outputFiles } = yield main_1.main({ inputFiles: [img], command: `convert ${img.name} -format %[pixel:p{${x},${y}}] info:info.txt` }); return (yield file_1.File.asString(outputFiles[0])) || undefined; } }); } exports.imagePixelColor = imagePixelColor; function rgbaToString(c) { return `rgba(${c.r},${c.g},${c.b},${c.a})`; } exports.rgbaToString = rgbaToString; function isRgbaImage(f) { return f.name.endsWith('rgba') && f.width && f.content instanceof Uint8ClampedArray; } exports.isRgbaImage = isRgbaImage; function colorCount(img) { return __awaiter(this, void 0, void 0, function* () { if (!img) { return; } const { outputFiles } = yield main_1.main({ inputFiles: [img], command: `convert ${img.name} -unique-colors -format %w info:info.txt` }); var s = yield file_1.File.asString(outputFiles[0]); return parseInt(s) || undefined; }); } exports.colorCount = colorCount; function parseConvertVerbose(stdout) { // logo:=>bbb.rgba GIF 640x480=>800x754 var r = /([^=]+)=>([^\s]+)\s+([a-zA-Z0-9]+)\s+([^=]+)=>([^\s]+)/; // logo:=>bbb.rgba GIF 384x288 384x288+0+0 var r2 = /([^=]+)=>([^\s]+)\s+([a-zA-Z0-9]+)\s+([^\s]+)/; return stdout.map(l => r.exec(l) || r2.exec(l)).filter(misc_utils_of_mine_generic_1.notFalsy).map(m => { var a = m[4].split('x'); var b = m[Math.min(m.length - 1, 6)].split('x'); return { inputName: m[1], outputName: m[2], inputFormat: m[3].toLowerCase(), inputSize: { width: parseInt(a[0]), height: parseInt(a[1]) }, outputSize: { width: parseInt(b[0]), height: parseInt(b[1]) } }; }); } exports.parseConvertVerbose = parseConvertVerbose; //# sourceMappingURL=imageUtil.js.map