terminal-pixel-animation 0.3.5

Render pixel images as Unicode characters (Braille / Half-block) in the terminal with True Color support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# terminal-pixel-animation

[![crates.io](https://img.shields.io/crates/v/terminal-pixel-animation.svg)](https://crates.io/crates/terminal-pixel-animation)
[![npm (core)](https://img.shields.io/npm/v/terminal-pixel-animation.svg)](https://www.npmjs.com/package/terminal-pixel-animation)
[![npm (react)](https://img.shields.io/npm/v/terminal-pixel-animation-react.svg)](https://www.npmjs.com/package/terminal-pixel-animation-react)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

Render pixel images as Unicode characters in the terminal with True Color support.
Also available as a WebAssembly module for browser use with React hooks.

RGBピクセル画像をUnicode文字に変換し、ターミナルやブラウザにTrue Colorで表示するライブラリ。React Hooks対応。

## Features

| Renderer | Language | Cells per pixel | Resolution | Best for |
|---|---|---|---|---|
| **Braille** | Odin | 2x4 (8 px/cell) | High | Detailed still images, thumbnails |
| **Half-block** | Zig | 1x2 (2 px/cell) | Medium | High frame-rate video playback |

- Aspect-ratio-preserving resize with letterboxing
- ANSI True Color (24-bit) output helpers
- Luminance-weighted color averaging with saturation boost (Braille mode)
- **WebAssembly** target for browser use via `wasm-pack` / `wasm-bindgen`
- **React Hooks** (`WasmProvider`, `useBraille`, `useHalfBlock`)

アスペクト比を保ったリサイズ、ANSI True Color出力、WebAssembly対応、React Hooks対応。

---

## Installation

### Rust (crates.io)

```toml
[dependencies]
terminal-pixel-animation = "0.2"
```

### npm

```bash
# Core WASM module / WASMコアモジュール
npm install terminal-pixel-animation

# React hooks (optional) / React Hooks(オプション)
npm install terminal-pixel-animation-react
```

> **Build dependencies:** Requires `odin`, `zig`, `objcopy`, and `wasm-pack` in your `PATH`.
>
> **ビルド時の依存関係:** Rust, Odin, Zig, objcopy (binutils), wasm-pack が必要です。

---

## Quick Start

### Native (Terminal)

```rust
use terminal_pixel_animation::{render_braille, print_braille_to_terminal};

// RGB8 pixel buffer (width * height * 3 bytes)
let pixels: Vec<u8> = load_your_image();
let (width, height) = (320u32, 240u32);

// Render into Braille cells (80 columns x 30 rows)
let cells = render_braille(&pixels, width, height, 80, 30).unwrap();

// Print to terminal
print_braille_to_terminal(&cells, 80, 30);
```

### WASM (Browser)

```js
import init, { render_braille, render_half_block } from "terminal-pixel-animation";

await init();

// RGB8 pixel buffer as Uint8Array
const cells = render_braille(rgb, videoWidth, videoHeight, cols, rows);

// Decode Braille cells
for (let i = 0; i < cells.length; i += 8) {
    const cp = cells[i] | (cells[i+1] << 8) | (cells[i+2] << 16) | (cells[i+3] << 24);
    const ch = String.fromCodePoint(cp);
    const r = cells[i+4], g = cells[i+5], b = cells[i+6];
    // Draw ch with color rgb(r, g, b) on a canvas...
}
```

### React

```tsx
import { useState, useEffect } from "react";
import { WasmProvider, useBraille } from "terminal-pixel-animation-react";

function App() {
  return (
    <WasmProvider>
      <PixelCanvas />
    </WasmProvider>
  );
}

function PixelCanvas() {
  const [pixels, setPixels] = useState<Uint8Array | null>(null);
  const { decoded, loading } = useBraille(pixels, 320, 240, 80, 30);

  if (loading) return <p>Loading WASM...</p>;
  if (!decoded) return null;

  return (
    <pre style={{ fontFamily: "monospace", lineHeight: "1", fontSize: "10px" }}>
      {decoded.map((cell, i) => (
        <span key={i} style={{ color: `rgb(${cell.r},${cell.g},${cell.b})` }}>
          {cell.char}
        </span>
      ))}
    </pre>
  );
}
```

---

## WASM Guide

### Setup

```bash
npm init -y
npm install terminal-pixel-animation
```

### Browser Usage

```js
import init, { render_braille, render_half_block } from "terminal-pixel-animation";

// Initialize WASM (call once)
await init();

// Get RGB buffer from webcam
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.drawImage(video, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

// Convert RGBA to RGB
const rgb = new Uint8Array(canvas.width * canvas.height * 3);
for (let i = 0, j = 0; i < imageData.data.length; i += 4, j += 3) {
    rgb[j] = imageData.data[i];
    rgb[j + 1] = imageData.data[i + 1];
    rgb[j + 2] = imageData.data[i + 2];
}

// Braille rendering
const cells = render_braille(rgb, canvas.width, canvas.height, 80, 30);

// Draw on canvas
const displayCanvas = document.getElementById("output");
const displayCtx = displayCanvas.getContext("2d");
displayCanvas.width = 80 * 8;
displayCanvas.height = 30 * 14;

for (let i = 0; i < cells.length; i += 8) {
    const cp = cells[i] | (cells[i+1] << 8) | (cells[i+2] << 16) | (cells[i+3] << 24);
    const ch = String.fromCodePoint(cp);
    const r = cells[i+4], g = cells[i+5], b = cells[i+6];

    if (ch !== "\0" && ch !== " ") {
        const col = (i / 8) % 80;
        const row = Math.floor((i / 8) / 80);
        displayCtx.fillStyle = `rgb(${r},${g},${b})`;
        displayCtx.font = "14px monospace";
        displayCtx.fillText(ch, col * 8, row * 14);
    }
}
```

### Half-block Rendering

```js
const cells = render_half_block(rgb, canvas.width, canvas.height, 80, 30);

// Cell is 6 bytes: [R_fg, G_fg, B_fg, R_bg, G_bg, B_bg]
for (let i = 0; i < cells.length; i += 6) {
    const rFg = cells[i], gFg = cells[i+1], bFg = cells[i+2];
    const rBg = cells[i+3], gBg = cells[i+4], bBg = cells[i+5];

    const col = (i / 6) % 80;
    const row = Math.floor((i / 6) / 80);

    // Upper half (foreground)
    displayCtx.fillStyle = `rgb(${rFg},${gFg},${bFg})`;
    displayCtx.fillRect(col * 8, row * 14, 8, 7);

    // Lower half (background)
    displayCtx.fillStyle = `rgb(${rBg},${gBg},${bBg})`;
    displayCtx.fillRect(col * 8, row * 14 + 7, 8, 7);
}
```

---

## React Guide

### Setup

```bash
npx create-vite@latest my-app --template react-ts
cd my-app
npm install terminal-pixel-animation terminal-pixel-animation-react
npm run dev
```

### WasmProvider

Wrap your app with `WasmProvider` at the root. The WASM module is loaded once.

```tsx
import { WasmProvider } from "terminal-pixel-animation-react";

function App() {
  return (
    <WasmProvider>
      <MyComponent />
    </WasmProvider>
  );
}
```

### useBraille Hook

```tsx
import { useBraille } from "terminal-pixel-animation-react";

function MyComponent() {
  const { cells, decoded, loading, error } = useBraille(pixels, 320, 240, 80, 30);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error: {error.message}</p>;
  if (!decoded) return null;

  // decoded: [{ char, r, g, b }, ...] — flat array, row-major order
  return (
    <pre style={{ fontFamily: "monospace", lineHeight: "1", fontSize: "10px" }}>
      {decoded.map((cell, i) => (
        <span key={i} style={{ color: `rgb(${cell.r},${cell.g},${cell.b})` }}>
          {cell.char}
        </span>
      ))}
    </pre>
  );
}
```

### useHalfBlock Hook

```tsx
import { useHalfBlock } from "terminal-pixel-animation-react";

function MyComponent() {
  const { decoded } = useHalfBlock(pixels, 320, 240, 80, 30);

  // decoded: [{ rFg, gFg, bFg, rBg, gBg, bBg }, ...]
  return (
    <div>
      {decoded?.map((cell, i) => (
        <div
          key={i}
          style={{
            display: "inline-block",
            width: 8,
            height: 14,
            background: `linear-gradient(to bottom, rgb(${cell.rFg},${cell.gFg},${cell.bFg}) 50%, rgb(${cell.rBg},${cell.gBg},${cell.bBg}) 50%)`,
          }}
        />
      ))}
    </div>
  );
}
```

### Real-time Webcam Rendering

```tsx
import { useState, useEffect, useRef } from "react";
import { WasmProvider, useBraille } from "terminal-pixel-animation-react";

function App() {
  return (
    <WasmProvider>
      <WebcamDemo />
    </WasmProvider>
  );
}

function WebcamDemo() {
  const videoRef = useRef<HTMLVideoElement>(null);
  const [pixels, setPixels] = useState<Uint8Array | null>(null);
  const [size, setSize] = useState({ w: 0, h: 0 });

  useEffect(() => {
    navigator.mediaDevices.getUserMedia({ video: true })
      .then(stream => {
        if (videoRef.current) {
          videoRef.current.srcObject = stream;
          videoRef.current.play();
        }
      });
  }, []);

  useEffect(() => {
    const canvas = document.createElement("canvas");
    const ctx = canvas.getContext("2d")!;

    const capture = () => {
      const video = videoRef.current;
      if (!video || video.readyState < 2) return;

      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      ctx.drawImage(video, 0, 0);
      const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

      const rgb = new Uint8Array(canvas.width * canvas.height * 3);
      for (let i = 0, j = 0; i < imageData.data.length; i += 4, j += 3) {
        rgb[j] = imageData.data[i];
        rgb[j + 1] = imageData.data[i + 1];
        rgb[j + 2] = imageData.data[i + 2];
      }

      setPixels(rgb);
      setSize({ w: canvas.width, h: canvas.height });
    };

    const id = requestAnimationFrame(function loop() {
      capture();
      requestAnimationFrame(loop);
    });
    return () => cancelAnimationFrame(id);
  }, []);

  return (
    <div>
      <video ref={videoRef} autoPlay playsInline style={{ display: "none" }} />
      <PixelDisplay pixels={pixels} width={size.w} height={size.h} />
    </div>
  );
}

function PixelDisplay({ pixels, width, height }: {
  pixels: Uint8Array | null; width: number; height: number;
}) {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const { decoded } = useBraille(pixels, width, height, 100, 45);

  useEffect(() => {
    if (!decoded || !canvasRef.current) return;
    const canvas = canvasRef.current;
    const ctx = canvas.getContext("2d")!;

    canvas.width = 100 * 8;
    canvas.height = 45 * 14;
    ctx.fillStyle = "#000";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.font = "14px monospace";
    ctx.textBaseline = "top";

    for (let i = 0; i < decoded.length; i++) {
      const cell = decoded[i];
      if (cell.char !== "\0" && cell.char !== " ") {
        const col = i % 100;
        const row = Math.floor(i / 100);
        ctx.fillStyle = `rgb(${cell.r},${cell.g},${cell.b})`;
        ctx.fillText(cell.char, col * 8, row * 14);
      }
    }
  }, [decoded]);

  return <canvas ref={canvasRef} style={{ background: "#000" }} />;
}
```

---

## API Reference

### Rust (Native)

```rust
// Braille: 8 bytes per cell [utf8;4, R, G, B, _]
let cells = render_braille(&pixels, w, h, cols, rows)?;

// Half-block: 6 bytes per cell [R_fg, G_fg, B_fg, R_bg, G_bg, B_bg]
let cells = render_half_block(&pixels, w, h, cols, rows)?;

// Terminal output
print_braille_to_terminal(&cells, cols, rows);
print_halfblock_to_terminal(&cells, cols, rows);
```

### WASM (JavaScript)

```js
// Braille: Uint8Array, 8 bytes per cell
const cells = render_braille(rgb, in_width, in_height, target_cols, target_rows);

// Half-block: Uint8Array, 6 bytes per cell
const cells = render_half_block(rgb, in_width, in_height, target_cols, target_rows);
```

### React Hooks

| Hook | Arguments | Returns |
|---|---|---|
| `useBraille(pixels, w, h, cols, rows)` | `Uint8Array \| null`, 4 numbers | `{ cells, decoded, loading, error }` |
| `useHalfBlock(pixels, w, h, cols, rows)` | `Uint8Array \| null`, 4 numbers | `{ cells, decoded, loading, error }` |

---

## Building from Source

### Dependencies

- [Rust]https://rustup.rs/ (edition 2024)
- [Odin compiler]https://odin-lang.org/
- [Zig compiler]https://ziglang.org/
- `objcopy` (binutils)
- [wasm-pack]https://rustwasm.github.io/wasm-pack/ (for WASM builds)

### Native

```bash
cargo build --release
```

### WASM

```bash
wasm-pack build --target bundler --out-dir pkg
```

### React hooks

```bash
cd react && npm install && npm run build
```

---

## Examples

### Native

```bash
cargo run --example demo -- path/to/image.png braille
cargo run --example demo -- path/to/image.png halfblock
```

### WASM

```bash
python3 -m http.server 8080
# Open http://localhost:8080/pkg/wasm-demo.html in your browser
```

Supports webcam live feed and video file playback.

---

## Publishing

### npm

```bash
npm login
cd pkg && npm publish
cd ../react && npm publish
```

### crates.io

```bash
cargo login <your-api-token>
cargo publish
```

---

## License

MIT