byte-codec 0.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.
Files changed (38) hide show
  1. package/README.md +43 -0
  2. package/dist/bytes/crc32-BVCBBYa3.d.cts +4 -0
  3. package/dist/bytes/crc32-BVCBBYa3.d.ts +4 -0
  4. package/dist/bytes/crc32.cjs +21 -0
  5. package/dist/bytes/crc32.js +20 -0
  6. package/dist/bytes/flate-CbSD2OU9.d.cts +12 -0
  7. package/dist/bytes/flate-CbSD2OU9.d.ts +12 -0
  8. package/dist/bytes/flate.cjs +53 -0
  9. package/dist/bytes/flate.js +49 -0
  10. package/dist/bytes/reader-CDx_NmAQ.d.cts +20 -0
  11. package/dist/bytes/reader-CDx_NmAQ.d.ts +20 -0
  12. package/dist/bytes/reader.cjs +60 -0
  13. package/dist/bytes/reader.js +58 -0
  14. package/dist/bytes/writer-B9uaqo-B.d.cts +13 -0
  15. package/dist/bytes/writer-B9uaqo-B.d.ts +13 -0
  16. package/dist/bytes/writer.cjs +37 -0
  17. package/dist/bytes/writer.js +35 -0
  18. package/dist/image/jpeg-info-Dh6D8PRh.d.cts +12 -0
  19. package/dist/image/jpeg-info-Dh6D8PRh.d.ts +12 -0
  20. package/dist/image/jpeg-info.cjs +71 -0
  21. package/dist/image/jpeg-info.js +70 -0
  22. package/dist/image/png-decode-Dvizumot.d.cts +14 -0
  23. package/dist/image/png-decode-Dvizumot.d.ts +14 -0
  24. package/dist/image/png-decode.cjs +181 -0
  25. package/dist/image/png-decode.js +180 -0
  26. package/dist/image/png-encode-C5Jj9gBX.d.ts +9 -0
  27. package/dist/image/png-encode-CpwBOmkg.d.cts +9 -0
  28. package/dist/image/png-encode.cjs +64 -0
  29. package/dist/image/png-encode.js +63 -0
  30. package/dist/image/png-filter-5L1Q0PDF.d.cts +6 -0
  31. package/dist/image/png-filter-5L1Q0PDF.d.ts +6 -0
  32. package/dist/image/png-filter.cjs +95 -0
  33. package/dist/image/png-filter.js +93 -0
  34. package/dist/index-VKR9nd94.d.ts +9 -0
  35. package/dist/index-v-4chATX.d.cts +9 -0
  36. package/dist/index.cjs +23 -0
  37. package/dist/index.js +9 -0
  38. package/package.json +67 -2
@@ -0,0 +1,93 @@
1
+ //#region src/image/png-filter.ts
2
+ function paethPredictor(a, b, c) {
3
+ const p = a + b - c;
4
+ const pa = Math.abs(p - a);
5
+ const pb = Math.abs(p - b);
6
+ const pc = Math.abs(p - c);
7
+ if (pa <= pb && pa <= pc) return a;
8
+ if (pb <= pc) return b;
9
+ return c;
10
+ }
11
+ function isPngFilterType(value) {
12
+ return value === 0 || value === 1 || value === 2 || value === 3 || value === 4;
13
+ }
14
+ function predictorValue(filterType, a, b, c) {
15
+ if (filterType === 1) return a;
16
+ if (filterType === 2) return b;
17
+ if (filterType === 3) return Math.floor((a + b) / 2);
18
+ if (filterType === 4) return paethPredictor(a, b, c);
19
+ return 0;
20
+ }
21
+ function unfilterScanlines(data, height, bytesPerRow, bpp) {
22
+ const stride = bytesPerRow + 1;
23
+ if (data.length < height * stride) throw new Error(`PNG scanline data too short: expected at least ${height * stride} bytes, got ${data.length}`);
24
+ const out = new Uint8Array(height * bytesPerRow);
25
+ for (let y = 0; y < height; y++) {
26
+ const filterByte = data[y * stride];
27
+ if (filterByte === void 0 || !isPngFilterType(filterByte)) throw new Error(`unknown PNG filter type: ${String(filterByte)}`);
28
+ const rowStart = y * stride + 1;
29
+ const outRowStart = y * bytesPerRow;
30
+ const prevOutRowStart = y > 0 ? outRowStart - bytesPerRow : void 0;
31
+ for (let x = 0; x < bytesPerRow; x++) {
32
+ const raw = data[rowStart + x];
33
+ const a = x >= bpp ? out[outRowStart + x - bpp] : 0;
34
+ const b = prevOutRowStart === void 0 ? 0 : out[prevOutRowStart + x];
35
+ const c = x >= bpp && prevOutRowStart !== void 0 ? out[prevOutRowStart + x - bpp] : 0;
36
+ out[outRowStart + x] = raw + predictorValue(filterByte, a, b, c) & 255;
37
+ }
38
+ }
39
+ return out;
40
+ }
41
+ function sumOfAbsSigned(bytes) {
42
+ let sum = 0;
43
+ for (const byte of bytes) sum += byte < 128 ? byte : 256 - byte;
44
+ return sum;
45
+ }
46
+ function filterRowInto(raw, rowStart, prevRowStart, bytesPerRow, bpp, filterType, out, outOffset) {
47
+ for (let x = 0; x < bytesPerRow; x++) {
48
+ const rawByte = raw[rowStart + x];
49
+ const a = x >= bpp ? raw[rowStart + x - bpp] : 0;
50
+ const b = prevRowStart === void 0 ? 0 : raw[prevRowStart + x];
51
+ const c = x >= bpp && prevRowStart !== void 0 ? raw[prevRowStart + x - bpp] : 0;
52
+ out[outOffset + x] = rawByte - predictorValue(filterType, a, b, c) & 255;
53
+ }
54
+ }
55
+ const ALL_FILTER_TYPES = [
56
+ 0,
57
+ 1,
58
+ 2,
59
+ 3,
60
+ 4
61
+ ];
62
+ function filterScanlines(raw, height, bytesPerRow, bpp, strategy = "adaptive") {
63
+ const stride = bytesPerRow + 1;
64
+ const out = new Uint8Array(height * stride);
65
+ const candidate = new Uint8Array(bytesPerRow);
66
+ for (let y = 0; y < height; y++) {
67
+ const rowStart = y * bytesPerRow;
68
+ const prevRowStart = y > 0 ? rowStart - bytesPerRow : void 0;
69
+ const outRowStart = y * stride;
70
+ if (strategy === "none") {
71
+ out[outRowStart] = 0;
72
+ filterRowInto(raw, rowStart, prevRowStart, bytesPerRow, bpp, 0, out, outRowStart + 1);
73
+ continue;
74
+ }
75
+ let bestType = 0;
76
+ let bestSum = Number.POSITIVE_INFINITY;
77
+ let best;
78
+ for (const filterType of ALL_FILTER_TYPES) {
79
+ filterRowInto(raw, rowStart, prevRowStart, bytesPerRow, bpp, filterType, candidate, 0);
80
+ const sum = sumOfAbsSigned(candidate);
81
+ if (sum < bestSum) {
82
+ bestSum = sum;
83
+ bestType = filterType;
84
+ best = candidate.slice();
85
+ }
86
+ }
87
+ out[outRowStart] = bestType;
88
+ if (best !== void 0) out.set(best, outRowStart + 1);
89
+ }
90
+ return out;
91
+ }
92
+ //#endregion
93
+ export { filterScanlines, unfilterScanlines };
@@ -0,0 +1,9 @@
1
+ import { crc32 } from "./bytes/crc32-BVCBBYa3.js";
2
+ import { DeflateLevel, InflateResult, MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant } from "./bytes/flate-CbSD2OU9.js";
3
+ import { ByteReader, isAsciiWhitespace } from "./bytes/reader-CDx_NmAQ.js";
4
+ import { ByteWriter, concatBytes } from "./bytes/writer-B9uaqo-B.js";
5
+ import { JpegInfo, readJpegInfo } from "./image/jpeg-info-Dh6D8PRh.js";
6
+ import { PngDecodeOptions, RawImage, decodePng } from "./image/png-decode-Dvizumot.js";
7
+ import { PngEncodeOptions, encodePng } from "./image/png-encode-C5Jj9gBX.js";
8
+ import { PngFilterType, filterScanlines, unfilterScanlines } from "./image/png-filter-5L1Q0PDF.js";
9
+ export { ByteReader, ByteWriter, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, concatBytes, crc32, decodePng, deflate, encodePng, filterScanlines, inflate, inflateTolerant, isAsciiWhitespace, readJpegInfo, unfilterScanlines };
@@ -0,0 +1,9 @@
1
+ import { crc32 } from "./bytes/crc32-BVCBBYa3.cjs";
2
+ import { DeflateLevel, InflateResult, MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant } from "./bytes/flate-CbSD2OU9.cjs";
3
+ import { ByteReader, isAsciiWhitespace } from "./bytes/reader-CDx_NmAQ.cjs";
4
+ import { ByteWriter, concatBytes } from "./bytes/writer-B9uaqo-B.cjs";
5
+ import { JpegInfo, readJpegInfo } from "./image/jpeg-info-Dh6D8PRh.cjs";
6
+ import { PngDecodeOptions, RawImage, decodePng } from "./image/png-decode-Dvizumot.cjs";
7
+ import { PngEncodeOptions, encodePng } from "./image/png-encode-CpwBOmkg.cjs";
8
+ import { PngFilterType, filterScanlines, unfilterScanlines } from "./image/png-filter-5L1Q0PDF.cjs";
9
+ export { ByteReader, ByteWriter, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, concatBytes, crc32, decodePng, deflate, encodePng, filterScanlines, inflate, inflateTolerant, isAsciiWhitespace, readJpegInfo, unfilterScanlines };
package/dist/index.cjs ADDED
@@ -0,0 +1,23 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_bytes_writer = require("./bytes/writer.cjs");
3
+ const require_bytes_reader = require("./bytes/reader.cjs");
4
+ const require_bytes_crc32 = require("./bytes/crc32.cjs");
5
+ const require_bytes_flate = require("./bytes/flate.cjs");
6
+ const require_image_png_filter = require("./image/png-filter.cjs");
7
+ const require_image_png_encode = require("./image/png-encode.cjs");
8
+ const require_image_png_decode = require("./image/png-decode.cjs");
9
+ const require_image_jpeg_info = require("./image/jpeg-info.cjs");
10
+ exports.ByteReader = require_bytes_reader.ByteReader;
11
+ exports.ByteWriter = require_bytes_writer.ByteWriter;
12
+ exports.MAX_INFLATE_OUTPUT_BYTES = require_bytes_flate.MAX_INFLATE_OUTPUT_BYTES;
13
+ exports.concatBytes = require_bytes_writer.concatBytes;
14
+ exports.crc32 = require_bytes_crc32.crc32;
15
+ exports.decodePng = require_image_png_decode.decodePng;
16
+ exports.deflate = require_bytes_flate.deflate;
17
+ exports.encodePng = require_image_png_encode.encodePng;
18
+ exports.filterScanlines = require_image_png_filter.filterScanlines;
19
+ exports.inflate = require_bytes_flate.inflate;
20
+ exports.inflateTolerant = require_bytes_flate.inflateTolerant;
21
+ exports.isAsciiWhitespace = require_bytes_reader.isAsciiWhitespace;
22
+ exports.readJpegInfo = require_image_jpeg_info.readJpegInfo;
23
+ exports.unfilterScanlines = require_image_png_filter.unfilterScanlines;
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { ByteWriter, concatBytes } from "./bytes/writer.js";
2
+ import { ByteReader, isAsciiWhitespace } from "./bytes/reader.js";
3
+ import { crc32 } from "./bytes/crc32.js";
4
+ import { MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant } from "./bytes/flate.js";
5
+ import { filterScanlines, unfilterScanlines } from "./image/png-filter.js";
6
+ import { encodePng } from "./image/png-encode.js";
7
+ import { decodePng } from "./image/png-decode.js";
8
+ import { readJpegInfo } from "./image/jpeg-info.js";
9
+ export { ByteReader, ByteWriter, MAX_INFLATE_OUTPUT_BYTES, concatBytes, crc32, decodePng, deflate, encodePng, filterScanlines, inflate, inflateTolerant, isAsciiWhitespace, readJpegInfo, unfilterScanlines };
package/package.json CHANGED
@@ -1,5 +1,70 @@
1
1
  {
2
2
  "name": "byte-codec",
3
- "version": "0.0.0",
4
- "private": false
3
+ "version": "1.0.1",
4
+ "description": "Generic byte-level primitives (ByteWriter, ByteReader, CRC-32, deflate/inflate) and PNG/JPEG image encoding/decoding with zero PDF knowledge — the shared utility package for the documents.js family.",
5
+ "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/ExaDev/byte-codec.git"
9
+ },
10
+ "homepage": "https://github.com/ExaDev/byte-codec#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/ExaDev/byte-codec/issues"
13
+ },
14
+ "license": "MIT",
15
+ "exports": {
16
+ ".": {
17
+ "types": {
18
+ "import": "./dist/index.d.ts",
19
+ "require": "./dist/index.d.cts"
20
+ },
21
+ "import": "./dist/index.js",
22
+ "require": "./dist/index.cjs"
23
+ },
24
+ "./*": {
25
+ "types": {
26
+ "import": "./dist/*.d.ts",
27
+ "require": "./dist/*.d.cts"
28
+ },
29
+ "import": "./dist/*.js",
30
+ "require": "./dist/*.cjs"
31
+ }
32
+ },
33
+ "scripts": {
34
+ "build": "tsdown",
35
+ "typecheck": "tsc --noEmit",
36
+ "lint": "eslint . --fix --cache --max-warnings 0",
37
+ "test": "vitest run",
38
+ "test:watch": "vitest",
39
+ "prepare": "husky",
40
+ "prepublishOnly": "tsdown",
41
+ "lint:check": "eslint . --max-warnings 0"
42
+ },
43
+ "files": [
44
+ "dist"
45
+ ],
46
+ "packageManager": "pnpm@11.6.0",
47
+ "dependencies": {
48
+ "fflate": "^0.8.2"
49
+ },
50
+ "devDependencies": {
51
+ "@commitlint/cli": "^21.2.1",
52
+ "@commitlint/config-conventional": "^21.2.0",
53
+ "@eslint/js": "^10.0.1",
54
+ "@semantic-release/changelog": "^7.0.0",
55
+ "@semantic-release/git": "^11.0.1",
56
+ "@types/node": "^26.1.2",
57
+ "eslint": "^10.8.0",
58
+ "globals": "^17.8.0",
59
+ "husky": "^9.1.7",
60
+ "lint-staged": "^17.2.0",
61
+ "semantic-release": "^25.0.8",
62
+ "tsdown": "^0.12.5",
63
+ "typescript": "^5.9.3",
64
+ "typescript-eslint": "^8.65.0",
65
+ "vitest": "^4.1.10"
66
+ },
67
+ "lint-staged": {
68
+ "*.ts": "eslint --fix"
69
+ }
5
70
  }