GithubHelp home page GithubHelp logo

daciansky / image-wasm-for-war3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ilimei/image-wasm-for-war3

0.0 0.0 0.0 1.81 MB

image-wasm-for-war3

JavaScript 35.98% C 16.34% Rust 7.62% TypeScript 40.07%

image-wasm-for-war3's Introduction

简介

image-wasm-for-war3 是一个使用 WebAssembly 技术实现的图像处理库,可以在浏览器和 Node.js 环境中使用。该库提供了一组 API,用于对 RGBA 的图像数据进行解码和编码

安装

您可以使用 npm 包管理器来安装 image-wasm-for-war3:

npm install image-wasm-for-war3

使用

encode

const fs = require("fs");
const { ImageType, encode } = require("image-wasm-for-war3");

const data = new Uint8Array(
  new Array(100 * 100 * 4).fill(0).map((_, i) => i % 255)
);
const image = {
    width: 100,
    height: 100,
    buffer: data.buffer,
}

encode(image, ImageType.Tga).then((tgaContent) => {
  fs.writeFileSync("test.tga", Buffer.from(tgaContent));
});

encode(image, ImageType.Blp1).then((blp1Content) => {
  fs.writeFileSync("test.blp", Buffer.from(blp1Content));
});

decode

const fs = require("fs");
const { ImageType, decode } = require("image-wasm-for-war3");

decode(fs.readFileSync('test.tga'), ImageType.Tga).then((rgbaData) => {
    console.info(rgbaData.width);
    console.info(rgbaData.height);
    console.info(rgbaData.buffer);
});

decode(fs.readFileSync('test.blp'), ImageType.Blp1).then((rgbaData) => {
    console.info(rgbaData.width);
    console.info(rgbaData.height);
    console.info(rgbaData.buffer);
});

resize

const fs = require("fs");
const { ImageType, encode, decode, resize } = require("image-wasm-for-war3");

decode(fs.readFileSync('big.tga'), ImageType.Tga)
  .then((rgbaData) => resize(rgbaData, 50, 50))
  .then(resizeImage => encode(resizeImage, ImageType.Tga))
  .then((tgaContent) => {
    fs.writeFileSync("small.tga", Buffer.from(tgaContent));
  });

blp1的处理

const fs = require("fs");
const { Blp1File, encode, decode, ImageType } = require("image-wasm-for-war3");

const resizeImage = await decode(fs.readFileSync('some jpeg path'), ImageType.Jpeg);
// 生成一个 mimap是一个 质量是80 的blp1文件
const blpContent = await Blp1File.encode(resizeImage).encode(80, 1);
// 保存文件
fs.writeFileSync('some blp path', Buffer.from(blpContent));

// 获取blp的第一张mimap数据
const blpImageData = await Blp1File.decode(fs.readFileSync('some blp path')).getMimapData(0);
// 编码成png
const pngContent = await encode(blpImageData, ImageType.Png);
// 保存成png
fs.writeFileSync('some png path', Buffer.from(pngContent));

构建

在构建之前,您需要安装以下工具:

  • emcc
  • rust
  • cmake

rust 需要安装 target wasm32-unknown-emscripten

rustup target add wasm32-unknown-emscripten

开始构建

npm i
npm run build

如果一切顺利,构建完成后您将会在 dist 目录下找到编译好的文件

image-wasm-for-war3's People

Contributors

ilimei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.