@theresav/graphics 0.1.2 → 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 +58 -58
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
1
|
# TheresaV Graphics (`@theresav/graphics`)
|
|
2
2
|
|
|
3
|
-
Unified, ultra-high-performance, security-hardened 2D graphics engine written in Rust for **Node.js** and **Bun**.
|
|
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/
|
|
8
|
+
[](https://github.com/Blckrose08/theresav-graphics)
|
|
9
9
|
|
|
10
10
|
---
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Key Features
|
|
13
13
|
|
|
14
|
-
- ⚡ **
|
|
15
|
-
- 🛡️ **
|
|
16
|
-
- 🎨 **Canvas 2D Rendering**:
|
|
17
|
-
- 🖼️ **
|
|
18
|
-
- 📦 **
|
|
19
|
-
- 🎬 **
|
|
20
|
-
- 🔄 **Drop-in Sharp Compatibility**:
|
|
21
|
-
- 🌐 **Node.js & Bun Native**:
|
|
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
22
|
|
|
23
23
|
---
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Installation
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
|
-
#
|
|
28
|
+
# Using Bun
|
|
29
29
|
bun add @theresav/graphics
|
|
30
30
|
|
|
31
|
-
#
|
|
31
|
+
# Using npm
|
|
32
32
|
npm install @theresav/graphics
|
|
33
33
|
|
|
34
|
-
#
|
|
34
|
+
# Using pnpm or yarn
|
|
35
35
|
pnpm add @theresav/graphics
|
|
36
36
|
yarn add @theresav/graphics
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
---
|
|
40
40
|
|
|
41
|
-
##
|
|
41
|
+
## Quick Start & Code Examples
|
|
42
42
|
|
|
43
|
-
### 1. Image Processing Pipeline
|
|
43
|
+
### 1. Fluent Image Processing Pipeline
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
46
|
import { Graphics } from "@theresav/graphics";
|
|
47
47
|
|
|
48
|
-
//
|
|
48
|
+
// Open an image from file or Buffer
|
|
49
49
|
const image = await Graphics.open("./input.png");
|
|
50
|
-
//
|
|
50
|
+
// Or from memory: const image = await Graphics.fromBuffer(buffer);
|
|
51
51
|
|
|
52
|
-
//
|
|
52
|
+
// Fluent processing pipeline
|
|
53
53
|
const outputBuffer = await image
|
|
54
54
|
.resize(1200, 630)
|
|
55
55
|
.grayscale()
|
|
@@ -58,7 +58,7 @@ const outputBuffer = await image
|
|
|
58
58
|
.sharpen(1.0, 1.5)
|
|
59
59
|
.toBuffer("webp", { quality: 85 });
|
|
60
60
|
|
|
61
|
-
//
|
|
61
|
+
// Save directly to file
|
|
62
62
|
await image.toFile("./output.webp", "webp", { quality: 85 });
|
|
63
63
|
```
|
|
64
64
|
|
|
@@ -70,20 +70,20 @@ await image.toFile("./output.webp", "webp", { quality: 85 });
|
|
|
70
70
|
import { Graphics } from "@theresav/graphics";
|
|
71
71
|
import fs from "fs";
|
|
72
72
|
|
|
73
|
-
//
|
|
73
|
+
// Create an 800 x 600 Canvas
|
|
74
74
|
const canvas = Graphics.createCanvas(800, 600);
|
|
75
75
|
const ctx = canvas.getContext("2d");
|
|
76
76
|
|
|
77
|
-
//
|
|
77
|
+
// Draw solid background
|
|
78
78
|
ctx.fillStyle = "#1e1e2e";
|
|
79
79
|
ctx.fillRect(0, 0, 800, 600);
|
|
80
80
|
|
|
81
|
-
//
|
|
81
|
+
// Create Linear Gradient
|
|
82
82
|
const gradient = ctx.createLinearGradient(100, 100, 400, 400);
|
|
83
83
|
gradient.addColorStop(0, "#ff5555");
|
|
84
84
|
gradient.addColorStop(1, "#50fa7b");
|
|
85
85
|
|
|
86
|
-
//
|
|
86
|
+
// Draw closed vector paths & Bézier curves
|
|
87
87
|
ctx.beginPath();
|
|
88
88
|
ctx.moveTo(100, 100);
|
|
89
89
|
ctx.bezierCurveTo(200, 50, 300, 250, 400, 150);
|
|
@@ -92,24 +92,24 @@ ctx.closePath();
|
|
|
92
92
|
ctx.fillStyle = gradient;
|
|
93
93
|
ctx.fill();
|
|
94
94
|
|
|
95
|
-
//
|
|
95
|
+
// Draw circle (Arc)
|
|
96
96
|
ctx.beginPath();
|
|
97
97
|
ctx.arc(600, 300, 80, 0, Math.PI * 2);
|
|
98
98
|
ctx.fillStyle = "#8be9fd";
|
|
99
99
|
ctx.fill();
|
|
100
100
|
|
|
101
|
-
//
|
|
101
|
+
// Draw text with TrueType/OpenType typography
|
|
102
102
|
ctx.fillStyle = "#ffffff";
|
|
103
103
|
ctx.fillText("TheresaV Graphics Engine", 50, 520);
|
|
104
104
|
|
|
105
|
-
//
|
|
105
|
+
// Export to buffer
|
|
106
106
|
const pngBuffer = await canvas.toBuffer("png");
|
|
107
107
|
fs.writeFileSync("./canvas_output.png", pngBuffer);
|
|
108
108
|
```
|
|
109
109
|
|
|
110
110
|
---
|
|
111
111
|
|
|
112
|
-
### 3.
|
|
112
|
+
### 3. Image Compositing & Watermarking
|
|
113
113
|
|
|
114
114
|
```typescript
|
|
115
115
|
import { Graphics } from "@theresav/graphics";
|
|
@@ -117,12 +117,12 @@ import { Graphics } from "@theresav/graphics";
|
|
|
117
117
|
const baseImage = await Graphics.open("./background.jpg");
|
|
118
118
|
const watermark = await Graphics.open("./logo.png");
|
|
119
119
|
|
|
120
|
-
//
|
|
120
|
+
// Resize watermark and composite onto bottom right corner
|
|
121
121
|
watermark.resize(150, 150);
|
|
122
122
|
const posX = baseImage.width - watermark.width - 20;
|
|
123
123
|
const posY = baseImage.height - watermark.height - 20;
|
|
124
124
|
|
|
125
|
-
baseImage.composite(watermark, posX, posY, 0.85); //
|
|
125
|
+
baseImage.composite(watermark, posX, posY, 0.85); // 85% opacity
|
|
126
126
|
|
|
127
127
|
await baseImage.toFile("./watermarked.jpg", "jpeg", { quality: 90 });
|
|
128
128
|
```
|
|
@@ -131,7 +131,7 @@ await baseImage.toFile("./watermarked.jpg", "jpeg", { quality: 90 });
|
|
|
131
131
|
|
|
132
132
|
### 4. Drop-in Sharp Compatibility
|
|
133
133
|
|
|
134
|
-
|
|
134
|
+
Projects currently using `sharp` can import the `sharp` compatibility wrapper directly from `@theresav/graphics`:
|
|
135
135
|
|
|
136
136
|
```typescript
|
|
137
137
|
import { sharp } from "@theresav/graphics";
|
|
@@ -145,7 +145,7 @@ const output = await sharp(inputBuffer)
|
|
|
145
145
|
|
|
146
146
|
---
|
|
147
147
|
|
|
148
|
-
### 5.
|
|
148
|
+
### 5. Secure SVG Rendering
|
|
149
149
|
|
|
150
150
|
```typescript
|
|
151
151
|
import { Graphics } from "@theresav/graphics";
|
|
@@ -157,51 +157,51 @@ const svgString = `
|
|
|
157
157
|
</svg>
|
|
158
158
|
`;
|
|
159
159
|
|
|
160
|
-
//
|
|
160
|
+
// Strict mode: external entity resolution and scripts are rejected
|
|
161
161
|
const renderedBuffer = await Graphics.renderSvg(svgString);
|
|
162
162
|
```
|
|
163
163
|
|
|
164
164
|
---
|
|
165
165
|
|
|
166
|
-
### 6.
|
|
166
|
+
### 6. Animated GIF Creation
|
|
167
167
|
|
|
168
168
|
```typescript
|
|
169
169
|
import { Graphics } from "@theresav/graphics";
|
|
170
170
|
|
|
171
|
-
//
|
|
171
|
+
// Prepare frame buffers
|
|
172
172
|
const frame1 = await Graphics.open("./frame1.png").then(img => img.toBuffer("png"));
|
|
173
173
|
const frame2 = await Graphics.open("./frame2.png").then(img => img.toBuffer("png"));
|
|
174
174
|
|
|
175
175
|
const gifBuffer = await Graphics.createAnimatedGif(
|
|
176
176
|
[frame1, frame2],
|
|
177
|
-
200, // delay
|
|
178
|
-
500, //
|
|
179
|
-
500 //
|
|
177
|
+
200, // delay between frames in milliseconds (200ms)
|
|
178
|
+
500, // width
|
|
179
|
+
500 // height
|
|
180
180
|
);
|
|
181
181
|
```
|
|
182
182
|
|
|
183
183
|
---
|
|
184
184
|
|
|
185
|
-
### 7.
|
|
185
|
+
### 7. Security Profiles & Memory Quotas
|
|
186
186
|
|
|
187
|
-
TheresaV Graphics
|
|
187
|
+
TheresaV Graphics provides three built-in kernel-level security profiles:
|
|
188
188
|
|
|
189
189
|
```typescript
|
|
190
190
|
import { Graphics } from "@theresav/graphics";
|
|
191
191
|
|
|
192
|
-
// 1. Standard:
|
|
192
|
+
// 1. Standard: For local trusted applications
|
|
193
193
|
const standardEngine = new Graphics({ security: "standard" });
|
|
194
194
|
|
|
195
|
-
// 2. Strict:
|
|
195
|
+
// 2. Strict: For public APIs and user uploads
|
|
196
196
|
const strictEngine = new Graphics({
|
|
197
197
|
security: "strict",
|
|
198
|
-
memoryLimit: 128 * 1024 * 1024 //
|
|
198
|
+
memoryLimit: 128 * 1024 * 1024 // 128MB maximum allocation quota
|
|
199
199
|
});
|
|
200
200
|
|
|
201
|
-
// 3. Hardened:
|
|
201
|
+
// 3. Hardened: For untrusted files and multi-tenant environments
|
|
202
202
|
const hardenedEngine = new Graphics({
|
|
203
203
|
security: "hardened",
|
|
204
|
-
memoryLimit: 64 * 1024 * 1024
|
|
204
|
+
memoryLimit: 64 * 1024 * 1024 // 64MB tight limit
|
|
205
205
|
});
|
|
206
206
|
|
|
207
207
|
const safeImage = await strictEngine.fromBuffer(untrustedUploadBuffer);
|
|
@@ -209,21 +209,21 @@ const safeImage = await strictEngine.fromBuffer(untrustedUploadBuffer);
|
|
|
209
209
|
|
|
210
210
|
---
|
|
211
211
|
|
|
212
|
-
##
|
|
212
|
+
## Performance Benchmarks
|
|
213
213
|
|
|
214
|
-
*
|
|
214
|
+
*Benchmarked on Linux x86_64, Node.js v26.13.0, 500x500 RGBA8 test image, 800x600 Canvas (50 iterations).*
|
|
215
215
|
|
|
216
|
-
|
|
|
216
|
+
| Operation | TheresaV Graphics (Rust) | Sharp (libvips C/C++) | Jimp (Pure JavaScript) | TheresaV Advantage |
|
|
217
217
|
|:---|:---|:---|:---|:---|
|
|
218
|
-
| **Canvas 2D Rendering** | **1,076.4 op/s** (0.93 ms) | N/A | N/A | **14.6x
|
|
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
|
|
220
|
-
| **Sharpen** | **180.5 op/s** (5.54 ms) | 95.1 op/s (10.52 ms) | N/A | **1.9x
|
|
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
|
|
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
|
|
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
|
|
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
224
|
|
|
225
225
|
---
|
|
226
226
|
|
|
227
|
-
##
|
|
227
|
+
## License
|
|
228
228
|
|
|
229
|
-
|
|
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",
|