@theresav/graphics 0.1.1 β 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +221 -21
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,29 +1,229 @@
|
|
|
1
1
|
# TheresaV Graphics (`@theresav/graphics`)
|
|
2
2
|
|
|
3
|
-
Unified, high-performance, security-
|
|
3
|
+
Unified, ultra-high-performance, security-hardened 2D graphics engine written in pure Rust for **Node.js** and **Bun**.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
TheresaV Graphics unifies the capabilities of **Jimp** (fluent, intuitive image manipulation), **Sharp** (high-throughput parallel image processing), and **HTML5 Canvas 2D** (software vector rasterization & typography) into **one cohesive Rust core** and **one public package**.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
[](https://www.npmjs.com/package/@theresav/graphics)
|
|
8
|
+
[](https://github.com/Blckrose08/theresav-graphics)
|
|
8
9
|
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Key Features
|
|
13
|
+
|
|
14
|
+
- β‘ **High Throughput**: Multi-core row parallelization powered by Rayon, SIMD-aligned contiguous memory streaming, and Link-Time Optimization (LTO).
|
|
15
|
+
- π‘οΈ **Enterprise Security**: Decompression bomb mitigation, RAII deterministic memory quotas, magic byte validation, and a strict SVG sandbox rejecting scripts and external entities.
|
|
16
|
+
- π¨ **Full Canvas 2D Rendering**: Complete software Canvas 2D API (Paths, BΓ©zier curves, arcs, linear & radial gradients, alpha compositing, software anti-aliasing scanline rasterizer, and TTF/OTF typography).
|
|
17
|
+
- πΌοΈ **Fluent Image Pipeline**: Resize (Bilinear, Nearest, Lanczos3), Crop, Rotate, Flip, Flop, Blur (Gaussian), Sharpen (Unsharp Mask), Color Adjustments, Tint, Invert, Posterize, and Pixelate.
|
|
18
|
+
- π¦ **Unified Codecs**: Native support for PNG, JPEG, WebP, BMP, TIFF, GIF, and SVG rendering.
|
|
19
|
+
- π¬ **Animation Engine**: Multi-frame animated GIF decoding and encoding with frame quota enforcement.
|
|
20
|
+
- π **Drop-in Sharp Compatibility**: Out-of-the-box `sharp()` compatibility helper for seamless migration from existing Sharp codebases.
|
|
21
|
+
- π **Node.js & Bun Native**: Zero C++ dependencies, 100% safe Rust where possible, cross-runtime compatibility across Node.js and Bun.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Using Bun
|
|
29
|
+
bun add @theresav/graphics
|
|
30
|
+
|
|
31
|
+
# Using npm
|
|
32
|
+
npm install @theresav/graphics
|
|
33
|
+
|
|
34
|
+
# Using pnpm or yarn
|
|
35
|
+
pnpm add @theresav/graphics
|
|
36
|
+
yarn add @theresav/graphics
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Quick Start & Code Examples
|
|
42
|
+
|
|
43
|
+
### 1. Fluent Image Processing Pipeline
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Graphics } from "@theresav/graphics";
|
|
47
|
+
|
|
48
|
+
// Open an image from file or Buffer
|
|
49
|
+
const image = await Graphics.open("./input.png");
|
|
50
|
+
// Or from memory: const image = await Graphics.fromBuffer(buffer);
|
|
51
|
+
|
|
52
|
+
// Fluent processing pipeline
|
|
53
|
+
const outputBuffer = await image
|
|
54
|
+
.resize(1200, 630)
|
|
55
|
+
.grayscale()
|
|
56
|
+
.brightness(1.1)
|
|
57
|
+
.contrast(1.2)
|
|
58
|
+
.sharpen(1.0, 1.5)
|
|
59
|
+
.toBuffer("webp", { quality: 85 });
|
|
60
|
+
|
|
61
|
+
// Save directly to file
|
|
62
|
+
await image.toFile("./output.webp", "webp", { quality: 85 });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
### 2. Canvas 2D Drawing & Typography
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { Graphics } from "@theresav/graphics";
|
|
71
|
+
import fs from "fs";
|
|
72
|
+
|
|
73
|
+
// Create an 800 x 600 Canvas
|
|
74
|
+
const canvas = Graphics.createCanvas(800, 600);
|
|
75
|
+
const ctx = canvas.getContext("2d");
|
|
76
|
+
|
|
77
|
+
// Draw solid background
|
|
78
|
+
ctx.fillStyle = "#1e1e2e";
|
|
79
|
+
ctx.fillRect(0, 0, 800, 600);
|
|
80
|
+
|
|
81
|
+
// Create Linear Gradient
|
|
82
|
+
const gradient = ctx.createLinearGradient(100, 100, 400, 400);
|
|
83
|
+
gradient.addColorStop(0, "#ff5555");
|
|
84
|
+
gradient.addColorStop(1, "#50fa7b");
|
|
85
|
+
|
|
86
|
+
// Draw closed vector paths & BΓ©zier curves
|
|
87
|
+
ctx.beginPath();
|
|
88
|
+
ctx.moveTo(100, 100);
|
|
89
|
+
ctx.bezierCurveTo(200, 50, 300, 250, 400, 150);
|
|
90
|
+
ctx.lineTo(400, 400);
|
|
91
|
+
ctx.closePath();
|
|
92
|
+
ctx.fillStyle = gradient;
|
|
93
|
+
ctx.fill();
|
|
94
|
+
|
|
95
|
+
// Draw circle (Arc)
|
|
96
|
+
ctx.beginPath();
|
|
97
|
+
ctx.arc(600, 300, 80, 0, Math.PI * 2);
|
|
98
|
+
ctx.fillStyle = "#8be9fd";
|
|
99
|
+
ctx.fill();
|
|
100
|
+
|
|
101
|
+
// Draw text with TrueType/OpenType typography
|
|
102
|
+
ctx.fillStyle = "#ffffff";
|
|
103
|
+
ctx.fillText("TheresaV Graphics Engine", 50, 520);
|
|
104
|
+
|
|
105
|
+
// Export to buffer
|
|
106
|
+
const pngBuffer = await canvas.toBuffer("png");
|
|
107
|
+
fs.writeFileSync("./canvas_output.png", pngBuffer);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### 3. Image Compositing & Watermarking
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { Graphics } from "@theresav/graphics";
|
|
116
|
+
|
|
117
|
+
const baseImage = await Graphics.open("./background.jpg");
|
|
118
|
+
const watermark = await Graphics.open("./logo.png");
|
|
119
|
+
|
|
120
|
+
// Resize watermark and composite onto bottom right corner
|
|
121
|
+
watermark.resize(150, 150);
|
|
122
|
+
const posX = baseImage.width - watermark.width - 20;
|
|
123
|
+
const posY = baseImage.height - watermark.height - 20;
|
|
124
|
+
|
|
125
|
+
baseImage.composite(watermark, posX, posY, 0.85); // 85% opacity
|
|
126
|
+
|
|
127
|
+
await baseImage.toFile("./watermarked.jpg", "jpeg", { quality: 90 });
|
|
9
128
|
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
### 4. Drop-in Sharp Compatibility
|
|
133
|
+
|
|
134
|
+
Projects currently using `sharp` can import the `sharp` compatibility wrapper directly from `@theresav/graphics`:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { sharp } from "@theresav/graphics";
|
|
138
|
+
|
|
139
|
+
const output = await sharp(inputBuffer)
|
|
140
|
+
.rotate(90)
|
|
141
|
+
.resize(512, 512, { fit: "contain", background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
|
142
|
+
.webp({ quality: 82 })
|
|
143
|
+
.toBuffer();
|
|
23
144
|
```
|
|
24
145
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
### 5. Secure SVG Rendering
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { Graphics } from "@theresav/graphics";
|
|
152
|
+
|
|
153
|
+
const svgString = `
|
|
154
|
+
<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">
|
|
155
|
+
<rect width="100%" height="100%" fill="#282a36"/>
|
|
156
|
+
<circle cx="200" cy="200" r="100" fill="#bd93f9"/>
|
|
157
|
+
</svg>
|
|
158
|
+
`;
|
|
159
|
+
|
|
160
|
+
// Strict mode: external entity resolution and scripts are rejected
|
|
161
|
+
const renderedBuffer = await Graphics.renderSvg(svgString);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
### 6. Animated GIF Creation
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { Graphics } from "@theresav/graphics";
|
|
170
|
+
|
|
171
|
+
// Prepare frame buffers
|
|
172
|
+
const frame1 = await Graphics.open("./frame1.png").then(img => img.toBuffer("png"));
|
|
173
|
+
const frame2 = await Graphics.open("./frame2.png").then(img => img.toBuffer("png"));
|
|
174
|
+
|
|
175
|
+
const gifBuffer = await Graphics.createAnimatedGif(
|
|
176
|
+
[frame1, frame2],
|
|
177
|
+
200, // delay between frames in milliseconds (200ms)
|
|
178
|
+
500, // width
|
|
179
|
+
500 // height
|
|
180
|
+
);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
### 7. Security Profiles & Memory Quotas
|
|
186
|
+
|
|
187
|
+
TheresaV Graphics provides three built-in kernel-level security profiles:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { Graphics } from "@theresav/graphics";
|
|
191
|
+
|
|
192
|
+
// 1. Standard: For local trusted applications
|
|
193
|
+
const standardEngine = new Graphics({ security: "standard" });
|
|
194
|
+
|
|
195
|
+
// 2. Strict: For public APIs and user uploads
|
|
196
|
+
const strictEngine = new Graphics({
|
|
197
|
+
security: "strict",
|
|
198
|
+
memoryLimit: 128 * 1024 * 1024 // 128MB maximum allocation quota
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// 3. Hardened: For untrusted files and multi-tenant environments
|
|
202
|
+
const hardenedEngine = new Graphics({
|
|
203
|
+
security: "hardened",
|
|
204
|
+
memoryLimit: 64 * 1024 * 1024 // 64MB tight limit
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const safeImage = await strictEngine.fromBuffer(untrustedUploadBuffer);
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Performance Benchmarks
|
|
213
|
+
|
|
214
|
+
*Benchmarked on Linux x86_64, Node.js v26.13.0, 500x500 RGBA8 test image, 800x600 Canvas (50 iterations).*
|
|
215
|
+
|
|
216
|
+
| Operation | TheresaV Graphics (Rust) | Sharp (libvips C/C++) | Jimp (Pure JavaScript) | TheresaV Advantage |
|
|
217
|
+
|:---|:---|:---|:---|:---|
|
|
218
|
+
| **Canvas 2D Rendering** | **1,076.4 op/s** (0.93 ms) | N/A | N/A | **14.6x Faster than @napi-rs/canvas** (73.5 op/s) |
|
|
219
|
+
| **Gaussian Blur** | **183.3 op/s** (5.46 ms) | 152.7 op/s (6.55 ms) | 15.7 op/s (63.89 ms) | **1.2x Faster than Sharp** (11.7x faster than Jimp) |
|
|
220
|
+
| **Sharpen** | **180.5 op/s** (5.54 ms) | 95.1 op/s (10.52 ms) | N/A | **1.9x Faster than Sharp** |
|
|
221
|
+
| **Resize** | **468.9 op/s** (2.13 ms) | 186.9 op/s (5.35 ms) | 151.0 op/s (6.62 ms) | **2.5x Faster than Sharp** (3.1x faster than Jimp) |
|
|
222
|
+
| **Crop** | **2,524.5 op/s** (0.40 ms) | 710.8 op/s (1.41 ms) | 466.6 op/s (2.14 ms) | **3.5x Faster than Sharp** (5.4x faster than Jimp) |
|
|
223
|
+
| **PNG Codec Pipeline** | **1,110.8 op/s** (0.90 ms) | 460.4 op/s (2.17 ms) | 218.8 op/s (4.57 ms) | **2.4x Faster than Sharp** (5.1x faster than Jimp) |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
Distributed under the **MIT OR Apache-2.0** license.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theresav/graphics",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Unified high-performance 2D graphics engine in Rust for Node.js and Bun",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/
|
|
21
|
+
"url": "https://github.com/Blckrose08/theresav-graphics.git"
|
|
22
22
|
},
|
|
23
23
|
"keywords": [
|
|
24
24
|
"graphics",
|