@moonbase-machines/files 0.5.0 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moonbase-machines/files",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "The formats the machines' files come in and go out as, in Node: PNG written and read, and zip read.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/png.js CHANGED
@@ -2,7 +2,8 @@
2
2
  // [r, g, b, a] (with its alpha), or from RGBA bytes as a canvas or the machines' screens give them.
3
3
  //
4
4
  // png(rows) a PNG's bytes from rows of [r, g, b] or [r, g, b, a]
5
- // pngOfRGBA(rgba, width, height) a PNG's bytes from RGBA bytes, four a pixel, row by row
5
+ // pngOfRGBA(rgba, width, height, { alpha }) a PNG's bytes from RGBA bytes, four a pixel, row by row; alpha
6
+ // false, written as RGB, its alpha left out (a picture with none to show)
6
7
  // sheet(pictures, { across, gap, scale }) pictures (rows) on one sheet, `gap` gray dots between
7
8
  // padded(pictures, fill) pictures of different sizes padded to the largest, top left
8
9
  import { deflateSync } from 'node:zlib';
@@ -30,10 +31,10 @@ export function png(pixels) {
30
31
  return encode(raw, w, h, n);
31
32
  }
32
33
 
33
- export function pngOfRGBA(rgba, w, h) {
34
- const raw = Buffer.alloc(h * (w * 4 + 1));
35
- for (let y = 0; y < h; y++) raw.set(rgba.subarray(y * w * 4, (y + 1) * w * 4), y * (w * 4 + 1) + 1);
36
- return encode(raw, w, h, 4);
34
+ export function pngOfRGBA(rgba, w, h, { alpha = true } = {}) {
35
+ const n = alpha ? 4 : 3, raw = Buffer.alloc(h * (w * n + 1));
36
+ for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) raw.set(rgba.subarray((y * w + x) * 4, (y * w + x) * 4 + n), y * (w * n + 1) + 1 + x * n);
37
+ return encode(raw, w, h, n);
37
38
  }
38
39
 
39
40
  // Pictures scaled by `scale`, with `gap` gray dots between them, `across` to a row.
@@ -22,6 +22,8 @@ test('a PNG of RGBA bytes reads back as the same picture as its rows', () => {
22
22
  const rgba = new Uint8Array([10, 20, 30, 255, 40, 50, 60, 255, 70, 80, 90, 255, 1, 2, 3, 255]);
23
23
  const p = readPng(pngOfRGBA(rgba, 2, 2));
24
24
  assert.deepEqual([p.at(0, 0), p.at(1, 0), p.at(0, 1), p.at(1, 1)], ['0a141e', '28323c', '46505a', '010203']);
25
+ const rows = [[[10, 20, 30], [40, 50, 60]], [[70, 80, 90], [1, 2, 3]]];
26
+ assert.deepEqual(pngOfRGBA(rgba, 2, 2, { alpha: false }), png(rows), 'without its alpha, the same bytes as the rows\' PNG');
25
27
  });
26
28
 
27
29
  test('a sheet lays pictures out with gray between, the smaller padded to the largest', () => {