Skip to content

MilthmLang/milimg.js

Repository files navigation

Milimg.js

Milimg is a internal image format used by Milthm which acts as a simple AV1 container. This is a reference milimg encoder and decoder based on WebCodecs API.

Note: This library only supports browser environment, requires WebCodecs API support For unknown reason, some jpeg image may not be encoded successfully

Install

npm install milimg
# or
yarn add milimg

Or use CDN

<script type="module">
  import {
    decodeMilimg,
    encodeMilimg
  } from 'https://cdn.jsdelivr.net/npm/milimg/lib/index.js';
</script>

Quick Start

Encode Image

import { encodeMilimg } from 'milimg';

const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const imageBuffer = Buffer.from(arrayBuffer);

const milimgBuffer = await encodeMilimg(imageBuffer, 0);

Decode Image

import { decodeMilimg } from 'milimg';

const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const milimgBuffer = Buffer.from(arrayBuffer);

const pngBuffer = await decodeMilimg(milimgBuffer);

API

encodeMilimg(imageBuffer: Buffer, quality?: number): Promise<Buffer>

Encode image to milimg format

Parameters:

  • imageBuffer - Image file buffer (support PNG/JPEG/WebP format)
  • quality - Encoding quality(qp) (0-63), the lower the quality, the higher the quality, default is 0

Returns:

  • Promise<Buffer> - milimg buffer

Example:

const milimgBuffer = await encodeMilimg(imageBuffer, 28);

decodeMilimg(buffer: Buffer): Promise<Buffer>

Decode milimg format to PNG

Parameters:

  • buffer - milimg buffer

Returns:

  • Promise<Buffer> - PNG buffer

Example:

const pngBuffer = await decodeMilimg(milimgBuffer);

parseMilimgContainer(buffer: Buffer): MilimgHeader

Parse milimg container header

Parameters:

  • buffer - milimg buffer

Returns:

  • MilimgHeader - Object containing version, size, and payload information

Example:

const header = parseMilimgContainer(milimgBuffer);
console.log(header.width, header.height, header.version);

Version Description

  • Version 0: Image without alpha channel
  • Version 1: Image with alpha channel (RGBA)

Credits

The library is originally created by Q78KG based on qaqFei's reverse engineering results, later the disclosure of technical details and specs of milimg format is authorized and it became a official project.

Copyright

milimg.js - reference encoder & decoder implementation of the milimg format
Copyright (C) 2024-present  Morizero, Hoshino Yumetsuki

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •