npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-ccitt-g4-encoder

v1.0.0

Published

Encode 2-bit image buffer to CCITT G4 Fax Compression

Readme

ts-ccitt-g4-encoder

ts-ccitt-g4-encoder は、ピクセルバッファを CCITT G4 Fax 圧縮形式にエンコードするシンプルな TypeScript ライブラリです。


特徴

  • バイナライズ グレースケール画像を指定した閾値で 2 値化し、ビットパック形式(MSB 先頭)に変換します。
  • CCITT G4 圧縮 ビットパック化された 2 値画像を CCITT G4 Fax Compression 形式で効率的に圧縮します。
  • 高速かつ高圧縮 文章や図面のような一般的な白黒2値画像では PNG エンコードよりも CCITT G4 圧縮の方が処理が高速で、ファイルサイズも小さくなります。
  • Canvas ImageData 対応 CanvasRenderingContext2D#getImageData で取得した ImageData オブジェクトをそのまま受け取れます。

使用例

import { binarizeToBitPacked, encode, imageDataToEncoded } from "ts-ccitt-g4-encoder";

// Canvas のコンテキストから ImageData を取得
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Canvas context not available");
const imageData = ctx.getImageData(x, y, width, height);

// 閾値(0-255)を指定してビンナライズ + G4 エンコード
const threshold = 128;

// 方法1: 二段階処理
const bitPacked = binarizeToBitPacked(imageData, threshold);
const encoded = encode(bitPacked, imageData.width, imageData.height);

// 方法2: ワンステップ
const encodedDirect = imageDataToEncoded(imageData, threshold);

console.log(encoded, encodedDirect);

API

binarizeToBitPacked(imageData: ImageData, threshold: number): Uint8Array

  • グレースケールを閾値で判定し、2 値化したピクセルを MSB-ファーストのビットパック Uint8Array に変換します。

encode(imageBuffer: Uint8Array, width: number, height: number): Uint8Array

  • ビットパック形式の 2 値画像(MSB-ファースト)を CCITT G4 Fax Compression 形式でエンコードします。

imageDataToEncoded(imageData: ImageData, threshold: number): Uint8Array

  • binarizeToBitPackedencode を組み合わせ、ImageData から直接 CCITT G4 圧縮データを生成します。
ビットパック形式について

MSB-ファーストのビットパック形式では、画像の各ピクセルが左上から右方向に順に格納され、例えば「白白黒白黒白黒黒白」の並びは 0b00101011, 0b0... のように表現されます。行の途中で次の行へ続く場合も同様にビット単位でシームレスに連結されます。


ライセンス

MIT ライセンスのもとで公開されています。詳しくは LICENSE ファイルを参照してください。