@pixzle/browser 0.0.19 → 0.0.21

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 ADDED
@@ -0,0 +1,31 @@
1
+ # @pixzle/browser
2
+
3
+ Browser implementation of Pixzle image restoration.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i @pixzle/browser
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { BrowserImageRestorer } from '@pixzle/browser';
15
+
16
+ const restorer = new BrowserImageRestorer();
17
+
18
+ const restoredBitmap = await restorer.restoreImage(
19
+ 'path/to/shuffled.png', // or URL, Blob, HTMLImageElement, ImageBitmap
20
+ 8, // blockSize
21
+ 72411, // seed
22
+ { w: 500, h: 500 } // imageInfo
23
+ );
24
+
25
+ // Draw to canvas
26
+ const canvas = document.createElement('canvas');
27
+ canvas.width = restoredBitmap.width;
28
+ canvas.height = restoredBitmap.height;
29
+ const ctx = canvas.getContext('2d');
30
+ ctx.drawImage(restoredBitmap, 0, 0);
31
+ ```
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Split an image (HTMLImageElement or ImageBitmap) into blocks
3
+ */
4
+ export declare function splitImageToBlocks(image: HTMLImageElement | ImageBitmap, blockSize: number): Uint8Array[];
5
+ /**
6
+ * Create an ImageBitmap from blocks
7
+ */
8
+ export declare function blocksToImage(blocks: Uint8Array[], width: number, height: number, blockSize: number): Promise<ImageBitmap>;
package/dist/block.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitImageToBlocks = splitImageToBlocks;
4
+ exports.blocksToImage = blocksToImage;
5
+ const core_1 = require("@pixzle/core");
6
+ /**
7
+ * Split an image (HTMLImageElement or ImageBitmap) into blocks
8
+ */
9
+ function splitImageToBlocks(image, blockSize) {
10
+ const canvas = document.createElement("canvas");
11
+ canvas.width = image.width;
12
+ canvas.height = image.height;
13
+ const ctx = canvas.getContext("2d");
14
+ if (!ctx)
15
+ throw new Error("Could not get 2D context");
16
+ ctx.drawImage(image, 0, 0);
17
+ const imageData = ctx.getImageData(0, 0, image.width, image.height);
18
+ // imageData.data is Uint8ClampedArray, we need Uint8Array
19
+ // We create a copy to avoid issues if the canvas is reused or garbage collected in weird ways,
20
+ // though here it's local.
21
+ // coreSplitImageToBlocks expects Uint8Array.
22
+ const buffer = new Uint8Array(imageData.data.buffer);
23
+ return (0, core_1.splitImageToBlocks)(buffer, image.width, image.height, blockSize);
24
+ }
25
+ /**
26
+ * Create an ImageBitmap from blocks
27
+ */
28
+ async function blocksToImage(blocks, width, height, blockSize) {
29
+ const buffer = (0, core_1.blocksToImageBuffer)(blocks, width, height, blockSize);
30
+ // Create ImageData
31
+ const imageData = new ImageData(new Uint8ClampedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength), width, height);
32
+ return createImageBitmap(imageData);
33
+ }
34
+ //# sourceMappingURL=block.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block.js","sourceRoot":"","sources":["../src/block.ts"],"names":[],"mappings":";;AAQA,gDAoBC;AAKD,sCAoBC;AArDD,uCAGsB;AAEtB;;GAEG;AACH,SAAgB,kBAAkB,CAChC,KAAqC,EACrC,SAAiB;IAEjB,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;IAC3B,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;IAC7B,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAEtD,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC3B,MAAM,SAAS,GAAG,GAAG,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAEpE,0DAA0D;IAC1D,+FAA+F;IAC/F,0BAA0B;IAC1B,6CAA6C;IAC7C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAErD,OAAO,IAAA,yBAAsB,EAAC,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAC9E,CAAC;AAED;;GAEG;AACI,KAAK,UAAU,aAAa,CACjC,MAAoB,EACpB,KAAa,EACb,MAAc,EACd,SAAiB;IAEjB,MAAM,MAAM,GAAG,IAAA,0BAAuB,EAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;IAEzE,mBAAmB;IACnB,MAAM,SAAS,GAAG,IAAI,SAAS,CAC7B,IAAI,iBAAiB,CACnB,MAAM,CAAC,MAAqB,EAC5B,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,UAAU,CAClB,EACD,KAAK,EACL,MAAM,CACP,CAAC;IAEF,OAAO,iBAAiB,CAAC,SAAS,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./restorer";
2
+ export * from "./block";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./restorer"), exports);
18
+ __exportStar(require("./block"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB"}
@@ -0,0 +1,13 @@
1
+ import type { ImageInfo } from "@pixzle/core";
2
+ export declare class BrowserImageRestorer {
3
+ /**
4
+ * Restore a single image from a shuffled source
5
+ * @param imageSource The shuffled image (URL, Blob, HTMLImageElement, or ImageBitmap)
6
+ * @param blockSize The block size used for fragmentation
7
+ * @param seed The seed used for shuffling
8
+ * @param imageInfo Information about the original image (dimensions)
9
+ * @returns Promise resolving to the restored ImageBitmap
10
+ */
11
+ restoreImage(imageSource: string | URL | Blob | HTMLImageElement | ImageBitmap, blockSize: number, seed: number | string, imageInfo: ImageInfo): Promise<ImageBitmap>;
12
+ private _loadImage;
13
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BrowserImageRestorer = void 0;
4
+ const seeded_shuffle_1 = require("@tuki0918/seeded-shuffle");
5
+ const block_1 = require("./block");
6
+ class BrowserImageRestorer {
7
+ /**
8
+ * Restore a single image from a shuffled source
9
+ * @param imageSource The shuffled image (URL, Blob, HTMLImageElement, or ImageBitmap)
10
+ * @param blockSize The block size used for fragmentation
11
+ * @param seed The seed used for shuffling
12
+ * @param imageInfo Information about the original image (dimensions)
13
+ * @returns Promise resolving to the restored ImageBitmap
14
+ */
15
+ async restoreImage(imageSource, blockSize, seed, imageInfo) {
16
+ const image = await this._loadImage(imageSource);
17
+ const blocks = (0, block_1.splitImageToBlocks)(image, blockSize);
18
+ const restoredBlocks = (0, seeded_shuffle_1.unshuffle)(blocks, seed);
19
+ return (0, block_1.blocksToImage)(restoredBlocks, imageInfo.w, imageInfo.h, blockSize);
20
+ }
21
+ async _loadImage(sourceInput) {
22
+ let source = sourceInput;
23
+ if (typeof URL !== "undefined" && source instanceof URL) {
24
+ source = source.toString();
25
+ }
26
+ if (typeof HTMLImageElement !== "undefined" &&
27
+ source instanceof HTMLImageElement) {
28
+ if (source.complete)
29
+ return source;
30
+ await new Promise((resolve, reject) => {
31
+ source.onload = resolve;
32
+ source.onerror = reject;
33
+ });
34
+ return source;
35
+ }
36
+ if (typeof ImageBitmap !== "undefined" && source instanceof ImageBitmap) {
37
+ return source;
38
+ }
39
+ if (typeof Blob !== "undefined" && source instanceof Blob) {
40
+ return createImageBitmap(source);
41
+ }
42
+ if (typeof source === "string") {
43
+ const img = new Image();
44
+ img.crossOrigin = "Anonymous";
45
+ img.src = source;
46
+ await new Promise((resolve, reject) => {
47
+ img.onload = resolve;
48
+ img.onerror = reject;
49
+ });
50
+ return img;
51
+ }
52
+ throw new Error("Unsupported image source");
53
+ }
54
+ }
55
+ exports.BrowserImageRestorer = BrowserImageRestorer;
56
+ //# sourceMappingURL=restorer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restorer.js","sourceRoot":"","sources":["../src/restorer.ts"],"names":[],"mappings":";;;AACA,6DAAqD;AACrD,mCAA4D;AAE5D,MAAa,oBAAoB;IAC/B;;;;;;;OAOG;IACH,KAAK,CAAC,YAAY,CAChB,WAAiE,EACjE,SAAiB,EACjB,IAAqB,EACrB,SAAoB;QAEpB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QAEjD,MAAM,MAAM,GAAG,IAAA,0BAAkB,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAEpD,MAAM,cAAc,GAAG,IAAA,0BAAS,EAAC,MAAM,EAAE,IAAI,CAAiB,CAAC;QAE/D,OAAO,IAAA,qBAAa,EAAC,cAAc,EAAE,SAAS,CAAC,CAAC,EAA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,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC9C,CAAC;CACF;AAjED,oDAiEC"}
package/package.json CHANGED
@@ -1,22 +1,43 @@
1
1
  {
2
2
  "name": "@pixzle/browser",
3
- "version": "0.0.19",
4
- "description": "Browser implementation of image fragmentation and restoration (RESERVED)",
3
+ "version": "0.0.21",
4
+ "description": "Browser implementation of image fragmentation and restoration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "sideEffects": false,
7
14
  "files": [
8
15
  "dist"
9
16
  ],
17
+ "keywords": [],
18
+ "author": "tuki0918",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/tuki0918/pixzle.git",
22
+ "directory": "packages/browser"
23
+ },
24
+ "license": "MIT",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
10
28
  "dependencies": {
11
- "@pixzle/core": "0.0.19"
29
+ "@tuki0918/seeded-shuffle": "^1.0.0",
30
+ "@pixzle/core": "0.0.21"
12
31
  },
13
32
  "devDependencies": {
33
+ "happy-dom": "^20.0.11",
14
34
  "typescript": "^5.7.2",
15
35
  "vitest": "^3.1.4"
16
36
  },
17
37
  "scripts": {
18
- "build": "echo 'Browser implementation not yet available'",
19
- "build:check": "echo 'Browser implementation not yet available'",
20
- "test": "echo 'Browser implementation not yet available'"
38
+ "build": "tsc -p tsconfig.build.json",
39
+ "build:check": "tsc -p tsconfig.build.json --noEmit",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest"
21
42
  }
22
43
  }