@levischuck/tiny-png 0.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/LICENSE.txt +21 -0
- package/README.md +48 -0
- package/dist/bytes.d.ts +1 -0
- package/dist/compress.d.ts +1 -0
- package/dist/crc.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +79 -0
- package/dist/pngBytes.d.ts +1 -0
- package/package.json +41 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Levi Schuck
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# tiny-png
|
|
2
|
+
|
|
3
|
+
A minimal, dependency-free TypeScript PNG encoder for indexed color images.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { indexedPng } from '@levischuck/tiny-png';
|
|
9
|
+
|
|
10
|
+
// Create an indexed PNG from pixel data
|
|
11
|
+
// Input is a Uint8Array where each byte is a palette index
|
|
12
|
+
const width = 100;
|
|
13
|
+
const height = 100;
|
|
14
|
+
const pixels = new Uint8Array(width * height);
|
|
15
|
+
// ... fill pixels with palette indices (0, 1, 2, etc.)
|
|
16
|
+
|
|
17
|
+
// Define color palette: [R, G, B] tuples
|
|
18
|
+
const colors: [number, number, number][] = [
|
|
19
|
+
[255, 255, 255], // Index 0: white
|
|
20
|
+
[0, 0, 0], // Index 1: black
|
|
21
|
+
[255, 0, 0], // Index 2: red
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const pngBytes = await indexedPng(pixels, width, height, colors);
|
|
25
|
+
|
|
26
|
+
// Write to file
|
|
27
|
+
await Bun.write('output.png', pngBytes);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### `indexedPng(input: Uint8Array, width: number, height: number, colors: [number, number, number][]): Promise<Uint8Array>`
|
|
33
|
+
|
|
34
|
+
Generates an indexed-color PNG image.
|
|
35
|
+
|
|
36
|
+
**Parameters:**
|
|
37
|
+
- `input` - Pixel data as a Uint8Array where each byte is a palette index
|
|
38
|
+
- `width` - Image width in pixels
|
|
39
|
+
- `height` - Image height in pixels
|
|
40
|
+
- `colors` - Color palette as an array of `[R, G, B]` tuples (0-255)
|
|
41
|
+
|
|
42
|
+
**Returns:** Promise resolving to PNG file bytes as a Uint8Array
|
|
43
|
+
|
|
44
|
+
**Throws:** Error if the color palette doesn't have enough colors for the highest index in the input data.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT Licensed.
|
package/dist/bytes.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function concat(...bytes: Uint8Array[]): Uint8Array;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deflate(uncompressed: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
|
package/dist/crc.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function crc32(data: Uint8Array): number;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function indexedPng(input: Uint8Array, width: number, height: number, colors: [number, number, number][]): Promise<Uint8Array>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
function x(...r) {
|
|
2
|
+
const e = new Uint8Array(
|
|
3
|
+
r.reduceRight((o, i) => o + i.length, 0)
|
|
4
|
+
);
|
|
5
|
+
let t = 0;
|
|
6
|
+
for (const o of r)
|
|
7
|
+
e.set(o, t), t += o.length;
|
|
8
|
+
return e;
|
|
9
|
+
}
|
|
10
|
+
async function E(r) {
|
|
11
|
+
const e = new CompressionStream("deflate"), t = e.writable.getWriter();
|
|
12
|
+
t.write(r), t.close();
|
|
13
|
+
const o = e.readable.getReader(), i = [];
|
|
14
|
+
let a = 0;
|
|
15
|
+
for (; ; ) {
|
|
16
|
+
const { done: s, value: l } = await o.read();
|
|
17
|
+
if (s) break;
|
|
18
|
+
i.push(l), a += l.length;
|
|
19
|
+
}
|
|
20
|
+
const c = new Uint8Array(a);
|
|
21
|
+
let u = 0;
|
|
22
|
+
for (const s of i)
|
|
23
|
+
c.set(s, u), u += s.length;
|
|
24
|
+
return c;
|
|
25
|
+
}
|
|
26
|
+
const h = new Uint32Array(256);
|
|
27
|
+
for (let r = 0; r < 256; r++) {
|
|
28
|
+
let e = r;
|
|
29
|
+
for (let t = 0; t < 8; t++)
|
|
30
|
+
e = e & 1 ? 3988292384 ^ e >>> 1 : e >>> 1;
|
|
31
|
+
h[r] = e;
|
|
32
|
+
}
|
|
33
|
+
function V(r) {
|
|
34
|
+
let e = 4294967295;
|
|
35
|
+
for (let t = 0; t < r.length; t++)
|
|
36
|
+
e = h[(e ^ r[t]) & 255] ^ e >>> 8;
|
|
37
|
+
return (e ^ 4294967295) >>> 0;
|
|
38
|
+
}
|
|
39
|
+
const k = new TextEncoder();
|
|
40
|
+
function w(r, e) {
|
|
41
|
+
const t = new Uint8Array(8 + e.length + 4), o = new DataView(t.buffer);
|
|
42
|
+
o.setUint32(0, e.length), t.set(k.encode(r), 4), t.set(e, 8);
|
|
43
|
+
const i = V(t.subarray(4, t.length - 4));
|
|
44
|
+
return o.setUint32(t.length - 4, i), t;
|
|
45
|
+
}
|
|
46
|
+
async function v(r, e, t, o) {
|
|
47
|
+
const i = new DataView(r.buffer), a = i.byteLength;
|
|
48
|
+
let c = 0;
|
|
49
|
+
for (let n = 0; n < a; n++) {
|
|
50
|
+
const f = i.getUint8(n);
|
|
51
|
+
f > c && (c = f);
|
|
52
|
+
}
|
|
53
|
+
if (o.length <= c)
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Color palette does not have enough colors (${c + 1}). Only ${o.length} were given!`
|
|
56
|
+
);
|
|
57
|
+
if (a != e * t)
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Input does not match dimensions ${e}x${t}. Only ${a} bytes were given when ${e * t} are expected!`
|
|
60
|
+
);
|
|
61
|
+
if (a == 0)
|
|
62
|
+
throw new Error("Received empty input");
|
|
63
|
+
const u = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]), s = new Uint8Array(13), l = new DataView(s.buffer);
|
|
64
|
+
l.setUint32(0, e), l.setUint32(4, t), s[8] = 8, s[9] = 3, s[10] = 0, s[11] = 0, s[12] = 0;
|
|
65
|
+
const y = w("IHDR", s), p = new Uint8Array(o.length * 3), g = new DataView(p.buffer);
|
|
66
|
+
for (let n = 0; n <= c; n++)
|
|
67
|
+
g.setUint8(n * 3, o[n][0]), g.setUint8(n * 3 + 1, o[n][1]), g.setUint8(n * 3 + 2, o[n][2]);
|
|
68
|
+
const D = w("PLTE", p), d = e + 1, U = new Uint8Array(d * t), b = new DataView(U.buffer);
|
|
69
|
+
for (let n = 0; n < t; n++) {
|
|
70
|
+
U[n * d] = 0;
|
|
71
|
+
for (let f = 0; f < e; f++)
|
|
72
|
+
b.setUint8(n * d + 1 + f, i.getUint8(n * e + f));
|
|
73
|
+
}
|
|
74
|
+
const A = w("IDAT", await E(U)), m = w("IEND", new Uint8Array(0));
|
|
75
|
+
return x(u, y, D, A, m);
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
v as indexedPng
|
|
79
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pngChunk(type: string, data: Uint8Array): Uint8Array;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@levischuck/tiny-png",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/levischuck/tiny-packages.git"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE.txt"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"png",
|
|
29
|
+
"encoder"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "vite build",
|
|
33
|
+
"prepublishOnly": "bun run build"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/bun": "latest"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"typescript": "^5"
|
|
40
|
+
}
|
|
41
|
+
}
|