UNPKG

@ibaraki-douji/waifu2x

Version:

Upscale image with waifu2x-ncnn-vulkan.

71 lines (70 loc) 2.73 kB
"use strict"; const path = require("path"); const os_1 = require("os"); const fs_1 = require("fs"); const child_process_1 = require("child_process"); const generate = (length) => { const alphabet = "AZERTYUIOPQSDFGHJKLMWXCVBNazertyuiopqsdfghjklmwxcvbn123456789"; let end = ""; for (let i = 0; i < length; i++) end += alphabet[Math.floor(Math.random() * alphabet.length)]; return end; }; module.exports = class Waifu2X { inputPath; outputPath; process; endBuffer; constructor(inp, out, options = {}) { if (typeof inp == "string") this.inputPath = path.resolve(inp); else { this.inputPath = path.resolve(os_1.tmpdir(), generate(50) + ".png"); fs_1.writeFileSync(this.inputPath, inp); } if (options.outputAsBuffer) { this.outputPath = path.resolve(os_1.tmpdir(), generate(50) + ".png"); } else { this.outputPath = path.resolve(out); } this.process = child_process_1.spawn(__dirname + "/../waifu2x/waifu2x-ncnn-vulkan", [ "-i", this.inputPath, "-o", this.outputPath, ...(options.gpu ? ["-g", "" + options.gpu] : []), ...(options.noise ? ['-n', '' + options.noise] : []), ...(options.ramLimit ? ['-t', '' + options.ramLimit] : []), ...(options.scale ? ['-s', '' + options.scale] : []) ]); this.process.on('close', () => { try { if (options.outputAsBuffer) this.endBuffer = fs_1.readFileSync(this.outputPath); } catch (e) { } }); } static upscale(inp, out, options) { return new Waifu2X(inp, out, options); } static async listGPUs() { const process = child_process_1.spawn(__dirname + "/../waifu2x/waifu2x-ncnn-vulkan", [ "-i", "/" + generate(50) + ".png", "-o", "/" + generate(50) + ".png", "-s", "1", ]); const out = []; process.on('error', console.log); process.stdout.on('data', d => out.push(d.toString())); process.stderr.on('data', d => out.push(d.toString())); await new Promise(res => (process.on('close', res))); const GPUs = []; for (let std of out) { if (std.startsWith("[")) { const str = std.split(']')[0].split("[")[1]; GPUs[+str.split(" ")[0]] = str.replace(str.split(" ")[0] + " ", ""); } } return GPUs; } finishedPromise() { return new Promise(res => { process.on('exit', res); }); } };