canvas-image-toolkit 1.0.0 → 1.0.1
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/decoder.js +9 -4
- package/encoder.js +13 -4
- package/filter/blur.js +53 -23
- package/filter/generate_ASCII.js +6 -3
- package/package.json +4 -1
- package/tsconfig.json +43 -0
- package/types/canvas.image.toolkit.d.ts +1 -0
package/decoder.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Decode a 2D RGBA matrix into pixel data
|
|
3
|
+
* @param {number[][][]} binary - 2D array of RGBA pixels
|
|
4
|
+
* @returns {{ data: Uint8ClampedArray, width: number, height: number }}
|
|
5
|
+
*/
|
|
2
6
|
export function decodeImageData(binary){
|
|
3
7
|
const height = binary.length
|
|
4
8
|
const width = binary[0].length
|
|
5
9
|
const imageData = new Uint8ClampedArray(width*height*4)
|
|
6
|
-
for(let y=0; y; y++){
|
|
10
|
+
for(let y=0; y<height; y++){
|
|
7
11
|
if (binary[y].length !== width) {
|
|
8
12
|
throw new Error("Invalid binary matrix: inconsistent row width");
|
|
9
13
|
}
|
|
10
|
-
for(let x=0; x<
|
|
14
|
+
for(let x=0; x<width; x++){
|
|
11
15
|
const i = (y*width+x)*4
|
|
12
16
|
const [r, g, b, alpha] = binary[y][x]
|
|
13
17
|
imageData[i] = r
|
|
@@ -20,5 +24,6 @@ export function decodeImageData(binary){
|
|
|
20
24
|
data:imageData,
|
|
21
25
|
width,
|
|
22
26
|
height
|
|
23
|
-
})
|
|
27
|
+
})
|
|
28
|
+
|
|
24
29
|
}
|
package/encoder.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @param {Uint8ClampedArray} imageData
|
|
4
|
+
* @param {number} width
|
|
5
|
+
* @param {number} height
|
|
6
|
+
* @returns {number[][][]}
|
|
7
|
+
*/
|
|
8
|
+
|
|
1
9
|
export function encodeImageData(imageData, width, height){
|
|
2
10
|
const Binary = []
|
|
11
|
+
const data = imageData
|
|
3
12
|
for(let y=0; y<height; y++){
|
|
4
13
|
let row = []
|
|
5
14
|
for(let x =0; x<width; x++){
|
|
6
|
-
const i = (y *
|
|
7
|
-
const r =
|
|
8
|
-
const g =
|
|
9
|
-
const b =
|
|
15
|
+
const i = (y * width + x) * 4;
|
|
16
|
+
const r = data[i];
|
|
17
|
+
const g = data[i + 1];
|
|
18
|
+
const b = data[i + 2];
|
|
10
19
|
const alpha = data[i+3]
|
|
11
20
|
if(alpha<50){
|
|
12
21
|
row.push([0,0,0,0]);
|
package/filter/blur.js
CHANGED
|
@@ -1,28 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Gaussian blur (separable, high quality)
|
|
3
3
|
* @param {Uint8ClampedArray} pixels
|
|
4
|
+
* @param {number} width
|
|
5
|
+
* @param {number} height
|
|
6
|
+
* @param {number} radius
|
|
4
7
|
*/
|
|
5
|
-
export function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let i = (y * canvas.width + x)*4
|
|
23
|
-
pixels[i] = r/count
|
|
24
|
-
pixels[i+1] = g/count
|
|
25
|
-
pixels[i+2] = b/count
|
|
8
|
+
export function gaussianBlur(pixels, width, height, radius = 5) {
|
|
9
|
+
const kernel = createGaussianKernel(radius);
|
|
10
|
+
const temp = new Float32Array(pixels.length);
|
|
11
|
+
for (let y = 0; y < height; y++) {
|
|
12
|
+
for (let x = 0; x < width; x++) {
|
|
13
|
+
|
|
14
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
15
|
+
|
|
16
|
+
for (let k = -radius; k <= radius; k++) {
|
|
17
|
+
const px = Math.min(width - 1, Math.max(0, x + k));
|
|
18
|
+
const i = (y * width + px) * 4;
|
|
19
|
+
const w = kernel[k + radius];
|
|
20
|
+
|
|
21
|
+
r += pixels[i] * w;
|
|
22
|
+
g += pixels[i + 1] * w;
|
|
23
|
+
b += pixels[i + 2] * w;
|
|
24
|
+
a += pixels[i + 3] * w;
|
|
26
25
|
}
|
|
26
|
+
|
|
27
|
+
const idx = (y * width + x) * 4;
|
|
28
|
+
temp[idx] = r;
|
|
29
|
+
temp[idx + 1] = g;
|
|
30
|
+
temp[idx + 2] = b;
|
|
31
|
+
temp[idx + 3] = a;
|
|
27
32
|
}
|
|
28
|
-
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (let y = 0; y < height; y++) {
|
|
36
|
+
for (let x = 0; x < width; x++) {
|
|
37
|
+
|
|
38
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
39
|
+
|
|
40
|
+
for (let k = -radius; k <= radius; k++) {
|
|
41
|
+
const py = Math.min(height - 1, Math.max(0, y + k));
|
|
42
|
+
const i = (py * width + x) * 4;
|
|
43
|
+
const w = kernel[k + radius];
|
|
44
|
+
|
|
45
|
+
r += temp[i] * w;
|
|
46
|
+
g += temp[i + 1] * w;
|
|
47
|
+
b += temp[i + 2] * w;
|
|
48
|
+
a += temp[i + 3] * w;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const idx = (y * width + x) * 4;
|
|
52
|
+
pixels[idx] = r;
|
|
53
|
+
pixels[idx + 1] = g;
|
|
54
|
+
pixels[idx + 2] = b;
|
|
55
|
+
pixels[idx + 3] = a;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
package/filter/generate_ASCII.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Convert image pixels to grayscale (in-place)
|
|
3
3
|
* @param {Uint8ClampedArray} pixels
|
|
4
|
+
* @param {number} width
|
|
5
|
+
* @param {Object} options
|
|
6
|
+
* @returns {string}
|
|
4
7
|
*/
|
|
5
8
|
export function ASCII_generator(pixels, width, option={}){
|
|
6
9
|
const ASCII_CHARS = option.char||["." , ",", ":", "-", "=", "+","*","#","%", "@"]
|
|
@@ -10,9 +13,9 @@
|
|
|
10
13
|
for(let y = 0; y<height; y++){
|
|
11
14
|
for(x =0; x<width; x++){
|
|
12
15
|
let i = (y*width+x)*4
|
|
13
|
-
const r =
|
|
14
|
-
const g =
|
|
15
|
-
const b =
|
|
16
|
+
const r = pixels[i]
|
|
17
|
+
const g = pixels[i+1]
|
|
18
|
+
const b = pixels[i+2]
|
|
16
19
|
|
|
17
20
|
const brightness = 0.299 * r + 0.587 * g + 0.114 * b;
|
|
18
21
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "canvas-image-toolkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Image encode/decode, filters, ASCII generator using raw ImageData",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"image",
|
|
@@ -17,5 +17,8 @@
|
|
|
17
17
|
"main": "index.js",
|
|
18
18
|
"scripts": {
|
|
19
19
|
"test": "echo \\\"No tests specified\\\"\""
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"typescript": "^5.9.3"
|
|
20
23
|
}
|
|
21
24
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
// "rootDir": "./src",
|
|
6
|
+
// "outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
// For nodejs:
|
|
13
|
+
// "lib": ["esnext"],
|
|
14
|
+
// "types": ["node"],
|
|
15
|
+
// and npm install -D @types/node
|
|
16
|
+
|
|
17
|
+
// Other Outputs
|
|
18
|
+
"sourceMap": true,
|
|
19
|
+
"declaration": true,
|
|
20
|
+
"declarationMap": true,
|
|
21
|
+
|
|
22
|
+
// Stricter Typechecking Options
|
|
23
|
+
"noUncheckedIndexedAccess": true,
|
|
24
|
+
"exactOptionalPropertyTypes": true,
|
|
25
|
+
|
|
26
|
+
// Style Options
|
|
27
|
+
// "noImplicitReturns": true,
|
|
28
|
+
// "noImplicitOverride": true,
|
|
29
|
+
// "noUnusedLocals": true,
|
|
30
|
+
// "noUnusedParameters": true,
|
|
31
|
+
// "noFallthroughCasesInSwitch": true,
|
|
32
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
33
|
+
|
|
34
|
+
// Recommended Options
|
|
35
|
+
"strict": true,
|
|
36
|
+
"jsx": "react-jsx",
|
|
37
|
+
"verbatimModuleSyntax": true,
|
|
38
|
+
"isolatedModules": true,
|
|
39
|
+
"noUncheckedSideEffectImports": true,
|
|
40
|
+
"moduleDetection": "force",
|
|
41
|
+
"skipLibCheck": true,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module 'canvas-image-toolkit';
|