@pixzle/node 0.0.13 → 0.0.15
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/dist/cjs/block.d.ts +71 -0
- package/dist/cjs/block.d.ts.map +1 -0
- package/dist/cjs/block.js +158 -0
- package/dist/cjs/block.js.map +1 -0
- package/dist/cjs/constants.d.ts +2 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +6 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/file.d.ts +33 -0
- package/dist/cjs/file.d.ts.map +1 -0
- package/dist/cjs/file.js +57 -0
- package/dist/cjs/file.js.map +1 -0
- package/dist/cjs/fragmenter.d.ts +14 -0
- package/dist/cjs/fragmenter.d.ts.map +1 -0
- package/dist/cjs/fragmenter.js +95 -0
- package/dist/cjs/fragmenter.js.map +1 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/restorer.d.ts +10 -0
- package/dist/cjs/restorer.d.ts.map +1 -0
- package/dist/cjs/restorer.js +54 -0
- package/dist/cjs/restorer.js.map +1 -0
- package/dist/cjs/utils.d.ts +6 -0
- package/dist/cjs/utils.d.ts.map +1 -0
- package/dist/cjs/utils.js +15 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/block.d.ts +71 -0
- package/dist/esm/block.d.ts.map +1 -0
- package/dist/esm/block.js +149 -0
- package/dist/esm/block.js.map +1 -0
- package/dist/esm/constants.d.ts +2 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +3 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/file.d.ts +33 -0
- package/dist/esm/file.d.ts.map +1 -0
- package/dist/esm/file.js +47 -0
- package/dist/esm/file.js.map +1 -0
- package/dist/esm/fragmenter.d.ts +14 -0
- package/dist/esm/fragmenter.d.ts.map +1 -0
- package/dist/esm/fragmenter.js +91 -0
- package/dist/esm/fragmenter.js.map +1 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +55 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +53 -0
- package/dist/esm/restorer.d.ts +10 -0
- package/dist/esm/restorer.d.ts.map +1 -0
- package/dist/esm/restorer.js +50 -0
- package/dist/esm/restorer.js.map +1 -0
- package/dist/esm/utils.d.ts +6 -0
- package/dist/esm/utils.d.ts.map +1 -0
- package/dist/esm/utils.js +9 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/package.json +54 -0
- package/package.json +21 -5
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
interface ImageFileToBlocksResult {
|
|
2
|
+
blocks: Buffer[];
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
channels: number;
|
|
6
|
+
blockCountX: number;
|
|
7
|
+
blockCountY: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Split an RGBA image buffer into an array of blocks (Node.js Buffer wrapper)
|
|
11
|
+
* @param buffer Source image buffer (RGBA format)
|
|
12
|
+
* @param width Image width in pixels
|
|
13
|
+
* @param height Image height in pixels
|
|
14
|
+
* @param blockSize Block size in pixels
|
|
15
|
+
* @returns Array of block buffers
|
|
16
|
+
*/
|
|
17
|
+
export declare function splitImageToBlocks(buffer: Buffer, width: number, height: number, blockSize: number): Buffer[];
|
|
18
|
+
/**
|
|
19
|
+
* Reconstruct an RGBA image buffer from an array of blocks (Node.js Buffer wrapper)
|
|
20
|
+
* @param blocks Array of block buffers
|
|
21
|
+
* @param width Target image width in pixels
|
|
22
|
+
* @param height Target image height in pixels
|
|
23
|
+
* @param blockSize Block size in pixels
|
|
24
|
+
* @returns Reconstructed image buffer
|
|
25
|
+
*/
|
|
26
|
+
export declare function blocksToImageBuffer(blocks: Buffer[], width: number, height: number, blockSize: number): Buffer;
|
|
27
|
+
/**
|
|
28
|
+
* Load an image from file or buffer and split into blocks
|
|
29
|
+
* @param input Path to the image file or Buffer containing image data
|
|
30
|
+
* @param blockSize Block size in pixels
|
|
31
|
+
* @returns Promise resolving to block data and image metadata
|
|
32
|
+
*/
|
|
33
|
+
export declare function imageFileToBlocks(input: string | Buffer, blockSize: number): Promise<ImageFileToBlocksResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Reconstruct a PNG image from blocks
|
|
36
|
+
* @param blocks Array of block buffers
|
|
37
|
+
* @param width Target image width in pixels
|
|
38
|
+
* @param height Target image height in pixels
|
|
39
|
+
* @param blockSize Block size in pixels
|
|
40
|
+
* @returns Promise resolving to PNG buffer
|
|
41
|
+
*/
|
|
42
|
+
export declare function blocksToPngImage(blocks: Buffer[], width: number, height: number, blockSize: number): Promise<Buffer>;
|
|
43
|
+
/**
|
|
44
|
+
* Extract raw RGBA image buffer from a PNG buffer using Jimp
|
|
45
|
+
* @param pngBuffer PNG image buffer
|
|
46
|
+
* @returns Promise resolving to image buffer and image dimensions
|
|
47
|
+
*/
|
|
48
|
+
export declare function extractImageBufferFromPng(pngBuffer: Buffer): Promise<{
|
|
49
|
+
imageBuffer: Buffer;
|
|
50
|
+
width: number;
|
|
51
|
+
height: number;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* Create a PNG buffer from raw RGBA image buffer using Jimp
|
|
55
|
+
* @param imageBuffer Raw RGBA image buffer
|
|
56
|
+
* @param width Image width in pixels
|
|
57
|
+
* @param height Image height in pixels
|
|
58
|
+
* @returns Promise resolving to PNG buffer
|
|
59
|
+
*/
|
|
60
|
+
export declare function createPngFromImageBuffer(imageBuffer: Buffer, width: number, height: number): Promise<Buffer>;
|
|
61
|
+
/**
|
|
62
|
+
* Apply a function to blocks per image
|
|
63
|
+
* @param allBlocks - All blocks to process
|
|
64
|
+
* @param fragmentBlocksCount - Number of blocks per fragment
|
|
65
|
+
* @param seed - Seed for the processing function
|
|
66
|
+
* @param processFunc - Function to apply to blocks (shuffle or unshuffle)
|
|
67
|
+
* @returns Processed blocks
|
|
68
|
+
*/
|
|
69
|
+
export declare function blocksPerImage(allBlocks: Buffer[], fragmentBlocksCount: number[], seed: number | string, processFunc: (blocks: Buffer[], seed: number | string) => Buffer[]): Buffer[];
|
|
70
|
+
export {};
|
|
71
|
+
//# sourceMappingURL=block.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":"AAQA,UAAU,uBAAuB;IAC/B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAgCD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,MAAM,EAAE,CAGV;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EAAE,EAChB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,MAAM,CASR;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,uBAAuB,CAAC,CAyBlC;AAED;;;;;;;GAOG;AACH,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EAAE,EAChB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CASjB;AAED;;;;GAIG;AACH,wBAAsB,yBAAyB,CAC7C,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAYjE;AAED;;;;;;GAMG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CASjB;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,SAAS,EAAE,MAAM,EAAE,EACnB,mBAAmB,EAAE,MAAM,EAAE,EAC7B,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,KAAK,MAAM,EAAE,GACjE,MAAM,EAAE,CAYV"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitImageToBlocks = splitImageToBlocks;
|
|
4
|
+
exports.blocksToImageBuffer = blocksToImageBuffer;
|
|
5
|
+
exports.imageFileToBlocks = imageFileToBlocks;
|
|
6
|
+
exports.blocksToPngImage = blocksToPngImage;
|
|
7
|
+
exports.extractImageBufferFromPng = extractImageBufferFromPng;
|
|
8
|
+
exports.createPngFromImageBuffer = createPngFromImageBuffer;
|
|
9
|
+
exports.blocksPerImage = blocksPerImage;
|
|
10
|
+
const core_1 = require("@pixzle/core");
|
|
11
|
+
const jimp_1 = require("jimp");
|
|
12
|
+
/**
|
|
13
|
+
* Format error message consistently
|
|
14
|
+
* @param operation Description of the operation that failed
|
|
15
|
+
* @param error The error that occurred
|
|
16
|
+
* @returns Formatted error message
|
|
17
|
+
*/
|
|
18
|
+
function formatErrorMessage(operation, error) {
|
|
19
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
20
|
+
return `${operation}: ${errorMessage}`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create a Jimp image from raw RGBA image buffer
|
|
24
|
+
* @param imageBuffer Raw RGBA image buffer
|
|
25
|
+
* @param width Image width in pixels
|
|
26
|
+
* @param height Image height in pixels
|
|
27
|
+
* @returns Jimp image instance
|
|
28
|
+
*/
|
|
29
|
+
function createJimpFromImageBuffer(imageBuffer, width, height) {
|
|
30
|
+
return new jimp_1.Jimp({
|
|
31
|
+
data: imageBuffer,
|
|
32
|
+
width,
|
|
33
|
+
height,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Split an RGBA image buffer into an array of blocks (Node.js Buffer wrapper)
|
|
38
|
+
* @param buffer Source image buffer (RGBA format)
|
|
39
|
+
* @param width Image width in pixels
|
|
40
|
+
* @param height Image height in pixels
|
|
41
|
+
* @param blockSize Block size in pixels
|
|
42
|
+
* @returns Array of block buffers
|
|
43
|
+
*/
|
|
44
|
+
function splitImageToBlocks(buffer, width, height, blockSize) {
|
|
45
|
+
const blocks = (0, core_1.splitImageToBlocks)(buffer, width, height, blockSize);
|
|
46
|
+
return blocks.map((block) => Buffer.from(block));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Reconstruct an RGBA image buffer from an array of blocks (Node.js Buffer wrapper)
|
|
50
|
+
* @param blocks Array of block buffers
|
|
51
|
+
* @param width Target image width in pixels
|
|
52
|
+
* @param height Target image height in pixels
|
|
53
|
+
* @param blockSize Block size in pixels
|
|
54
|
+
* @returns Reconstructed image buffer
|
|
55
|
+
*/
|
|
56
|
+
function blocksToImageBuffer(blocks, width, height, blockSize) {
|
|
57
|
+
const uint8Blocks = blocks.map((block) => new Uint8Array(block));
|
|
58
|
+
const imageBuffer = (0, core_1.blocksToImageBuffer)(uint8Blocks, width, height, blockSize);
|
|
59
|
+
return Buffer.from(imageBuffer);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Load an image from file or buffer and split into blocks
|
|
63
|
+
* @param input Path to the image file or Buffer containing image data
|
|
64
|
+
* @param blockSize Block size in pixels
|
|
65
|
+
* @returns Promise resolving to block data and image metadata
|
|
66
|
+
*/
|
|
67
|
+
async function imageFileToBlocks(input, blockSize) {
|
|
68
|
+
try {
|
|
69
|
+
// Load and process image with Jimp (automatically converts to RGBA)
|
|
70
|
+
const image = await jimp_1.Jimp.read(input);
|
|
71
|
+
const { width, height } = image.bitmap;
|
|
72
|
+
const channels = core_1.RGBA_CHANNELS;
|
|
73
|
+
const imageBuffer = image.bitmap.data;
|
|
74
|
+
// Split image into blocks
|
|
75
|
+
const blocks = splitImageToBlocks(imageBuffer, width, height, blockSize);
|
|
76
|
+
const blockCounts = (0, core_1.calculateBlockCounts)(width, height, blockSize);
|
|
77
|
+
return {
|
|
78
|
+
blocks,
|
|
79
|
+
width,
|
|
80
|
+
height,
|
|
81
|
+
channels,
|
|
82
|
+
blockCountX: blockCounts.blockCountX,
|
|
83
|
+
blockCountY: blockCounts.blockCountY,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new Error(`${formatErrorMessage("Failed to process image file", error)}. The manifest file may not match the image data.`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Reconstruct a PNG image from blocks
|
|
92
|
+
* @param blocks Array of block buffers
|
|
93
|
+
* @param width Target image width in pixels
|
|
94
|
+
* @param height Target image height in pixels
|
|
95
|
+
* @param blockSize Block size in pixels
|
|
96
|
+
* @returns Promise resolving to PNG buffer
|
|
97
|
+
*/
|
|
98
|
+
async function blocksToPngImage(blocks, width, height, blockSize) {
|
|
99
|
+
try {
|
|
100
|
+
const imageBuffer = blocksToImageBuffer(blocks, width, height, blockSize);
|
|
101
|
+
return await createPngFromImageBuffer(imageBuffer, width, height);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new Error(formatErrorMessage("Failed to reconstruct PNG image from blocks", error));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Extract raw RGBA image buffer from a PNG buffer using Jimp
|
|
109
|
+
* @param pngBuffer PNG image buffer
|
|
110
|
+
* @returns Promise resolving to image buffer and image dimensions
|
|
111
|
+
*/
|
|
112
|
+
async function extractImageBufferFromPng(pngBuffer) {
|
|
113
|
+
try {
|
|
114
|
+
const image = await jimp_1.Jimp.read(pngBuffer);
|
|
115
|
+
const { width, height } = image.bitmap;
|
|
116
|
+
const imageBuffer = Buffer.from(image.bitmap.data);
|
|
117
|
+
return { imageBuffer, width, height };
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
throw new Error(formatErrorMessage("Failed to extract image buffer from PNG", error));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Create a PNG buffer from raw RGBA image buffer using Jimp
|
|
125
|
+
* @param imageBuffer Raw RGBA image buffer
|
|
126
|
+
* @param width Image width in pixels
|
|
127
|
+
* @param height Image height in pixels
|
|
128
|
+
* @returns Promise resolving to PNG buffer
|
|
129
|
+
*/
|
|
130
|
+
async function createPngFromImageBuffer(imageBuffer, width, height) {
|
|
131
|
+
try {
|
|
132
|
+
const image = createJimpFromImageBuffer(imageBuffer, width, height);
|
|
133
|
+
return await image.getBuffer(jimp_1.JimpMime.png);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
throw new Error(formatErrorMessage("Failed to create PNG from image buffer", error));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Apply a function to blocks per image
|
|
141
|
+
* @param allBlocks - All blocks to process
|
|
142
|
+
* @param fragmentBlocksCount - Number of blocks per fragment
|
|
143
|
+
* @param seed - Seed for the processing function
|
|
144
|
+
* @param processFunc - Function to apply to blocks (shuffle or unshuffle)
|
|
145
|
+
* @returns Processed blocks
|
|
146
|
+
*/
|
|
147
|
+
function blocksPerImage(allBlocks, fragmentBlocksCount, seed, processFunc) {
|
|
148
|
+
const processedBlocks = [];
|
|
149
|
+
let offset = 0;
|
|
150
|
+
for (const blockCount of fragmentBlocksCount) {
|
|
151
|
+
const imageBlocks = allBlocks.slice(offset, offset + blockCount);
|
|
152
|
+
const processed = processFunc(imageBlocks, seed);
|
|
153
|
+
processedBlocks.push(...processed);
|
|
154
|
+
offset += blockCount;
|
|
155
|
+
}
|
|
156
|
+
return processedBlocks;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=block.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block.js","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":";;AAuDA,gDAQC;AAUD,kDAcC;AAQD,8CA4BC;AAUD,4CAcC;AAOD,8DAcC;AASD,4DAaC;AAUD,wCAiBC;AAzND,uCAKsB;AACtB,+BAAsC;AAWtC;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,SAAiB,EAAE,KAAc;IAC3D,MAAM,YAAY,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC;IAC9E,OAAO,GAAG,SAAS,KAAK,YAAY,EAAE,CAAC;AACzC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,yBAAyB,CAChC,WAAmB,EACnB,KAAa,EACb,MAAc;IAEd,OAAO,IAAI,WAAI,CAAC;QACd,IAAI,EAAE,WAAW;QACjB,KAAK;QACL,MAAM;KACP,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAChC,MAAc,EACd,KAAa,EACb,MAAc,EACd,SAAiB;IAEjB,MAAM,MAAM,GAAG,IAAA,yBAAsB,EAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;IACxE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,C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M,IAAI,KAAK,CACb,kBAAkB,CAAC,wCAAwC,EAAE,KAAK,CAAC,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,cAAc,CAC5B,SAAmB,EACnB,mBAA6B,EAC7B,IAAqB,EACrB,WAAkE;IAElE,MAAM,eAAe,GAAa,EAAE,CAAC;IACrC,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,KAAK,MAAM,UAAU,IAAI,mBAAmB,EAAE,CAAC;QAC7C,MAAM,WAAW,GAAG,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC;QACjE,MAAM,SAAS,GAAG,WAAW,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QACjD,eAAe,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC;QACnC,MAAM,IAAI,UAAU,CAAC;IACvB,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,QAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAA,+CAAuC;AAE1B,QAAA,OAAO,GAAG,sBAAO,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a directory
|
|
3
|
+
* @param dir Directory path
|
|
4
|
+
* @param recursive Create parent directories if they don't exist
|
|
5
|
+
*/
|
|
6
|
+
export declare function createDir(dir: string, recursive?: boolean): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Write a file
|
|
9
|
+
* @param dir Directory path
|
|
10
|
+
* @param filename Filename
|
|
11
|
+
* @param data Data to write
|
|
12
|
+
* @returns Path to the file
|
|
13
|
+
*/
|
|
14
|
+
export declare function writeFile(dir: string, filename: string, data: string | Buffer): Promise<string>;
|
|
15
|
+
/**
|
|
16
|
+
* Read a JSON file and return its content
|
|
17
|
+
* @param filePath Path to the JSON file
|
|
18
|
+
* @returns Content of the JSON file
|
|
19
|
+
*/
|
|
20
|
+
export declare function readJsonFile<T>(filePath: string): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* Read a file and return its content
|
|
23
|
+
* @param filePath Path to the file
|
|
24
|
+
* @returns Content of the file
|
|
25
|
+
*/
|
|
26
|
+
export declare function readFileBuffer(filePath: string): Promise<Buffer>;
|
|
27
|
+
/**
|
|
28
|
+
* Get the filename without the extension
|
|
29
|
+
* @param filePath Path to the file
|
|
30
|
+
* @returns Filename without the extension
|
|
31
|
+
*/
|
|
32
|
+
export declare function fileNameWithoutExtension(filePath: string): string;
|
|
33
|
+
//# sourceMappingURL=file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/file.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,UAAQ,iBAE7D;AAED;;;;;;GAMG;AACH,wBAAsB,SAAS,CAC7B,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GAAG,MAAM,mBAKtB;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAElE;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEtE;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEjE"}
|
package/dist/cjs/file.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDir = createDir;
|
|
7
|
+
exports.writeFile = writeFile;
|
|
8
|
+
exports.readJsonFile = readJsonFile;
|
|
9
|
+
exports.readFileBuffer = readFileBuffer;
|
|
10
|
+
exports.fileNameWithoutExtension = fileNameWithoutExtension;
|
|
11
|
+
const node_fs_1 = require("node:fs");
|
|
12
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
13
|
+
/**
|
|
14
|
+
* Create a directory
|
|
15
|
+
* @param dir Directory path
|
|
16
|
+
* @param recursive Create parent directories if they don't exist
|
|
17
|
+
*/
|
|
18
|
+
async function createDir(dir, recursive = false) {
|
|
19
|
+
await node_fs_1.promises.mkdir(dir, { recursive });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Write a file
|
|
23
|
+
* @param dir Directory path
|
|
24
|
+
* @param filename Filename
|
|
25
|
+
* @param data Data to write
|
|
26
|
+
* @returns Path to the file
|
|
27
|
+
*/
|
|
28
|
+
async function writeFile(dir, filename, data) {
|
|
29
|
+
const filePath = node_path_1.default.join(dir, filename);
|
|
30
|
+
await node_fs_1.promises.writeFile(filePath, data);
|
|
31
|
+
return filePath;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Read a JSON file and return its content
|
|
35
|
+
* @param filePath Path to the JSON file
|
|
36
|
+
* @returns Content of the JSON file
|
|
37
|
+
*/
|
|
38
|
+
async function readJsonFile(filePath) {
|
|
39
|
+
return JSON.parse(await node_fs_1.promises.readFile(filePath, "utf8"));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Read a file and return its content
|
|
43
|
+
* @param filePath Path to the file
|
|
44
|
+
* @returns Content of the file
|
|
45
|
+
*/
|
|
46
|
+
async function readFileBuffer(filePath) {
|
|
47
|
+
return await node_fs_1.promises.readFile(filePath);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get the filename without the extension
|
|
51
|
+
* @param filePath Path to the file
|
|
52
|
+
* @returns Filename without the extension
|
|
53
|
+
*/
|
|
54
|
+
function fileNameWithoutExtension(filePath) {
|
|
55
|
+
return node_path_1.default.basename(filePath, node_path_1.default.extname(filePath));
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../src/file.ts"],"names":[],"mappings":";;;;;AAQA,8BAEC;AASD,8BAQC;AAOD,oCAEC;AAOD,wCAEC;AAOD,4DAEC;AAtDD,qCAAyC;AACzC,0DAA6B;AAE7B;;;;GAIG;AACI,KAAK,UAAU,SAAS,CAAC,GAAW,EAAE,SAAS,GAAG,KAAK;IAC5D,MAAM,kBAAE,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;AACrC,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,SAAS,CAC7B,GAAW,EACX,QAAgB,EAChB,IAAqB;IAErB,MAAM,QAAQ,GAAG,mBAAI,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC1C,MAAM,kBAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACnC,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,YAAY,CAAI,QAAgB;IACpD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,kBAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACzD,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,cAAc,CAAC,QAAgB;IACnD,OAAO,MAAM,kBAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACrC,CAAC;AAED;;;;GAIG;AACH,SAAgB,wBAAwB,CAAC,QAAgB;IACvD,OAAO,mBAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,mBAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type FragmentationConfig, type FragmentationResult } from "@pixzle/core";
|
|
2
|
+
export declare class ImageFragmenter {
|
|
3
|
+
private config;
|
|
4
|
+
constructor(config: FragmentationConfig);
|
|
5
|
+
private _initializeConfig;
|
|
6
|
+
fragmentImages(paths: string[]): Promise<FragmentationResult>;
|
|
7
|
+
private _createImages;
|
|
8
|
+
private _createManifest;
|
|
9
|
+
private _prepareData;
|
|
10
|
+
private _processImages;
|
|
11
|
+
private _processImage;
|
|
12
|
+
private _createImage;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=fragmenter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragmenter.d.ts","sourceRoot":"","sources":["../../src/fragmenter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EAQzB,MAAM,cAAc,CAAC;AAOtB,qBAAa,eAAe;IAC1B,OAAO,CAAC,MAAM,CAAgC;gBAElC,MAAM,EAAE,mBAAmB;IAIvC,OAAO,CAAC,iBAAiB;IAenB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,mBAAmB,CAAC;YA6BrD,aAAa;IAc3B,OAAO,CAAC,eAAe;YAaT,YAAY;YAyBZ,cAAc;YAcd,aAAa;YAuBb,YAAY;CAW3B"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageFragmenter = void 0;
|
|
4
|
+
const core_1 = require("@pixzle/core");
|
|
5
|
+
const seeded_shuffle_1 = require("@tuki0918/seeded-shuffle");
|
|
6
|
+
const block_1 = require("./block");
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
const file_1 = require("./file");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
class ImageFragmenter {
|
|
11
|
+
config;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.config = this._initializeConfig(config);
|
|
14
|
+
}
|
|
15
|
+
_initializeConfig(config) {
|
|
16
|
+
return {
|
|
17
|
+
blockSize: config.blockSize ?? core_1.DEFAULT_FRAGMENTATION_CONFIG.BLOCK_SIZE,
|
|
18
|
+
prefix: config.prefix ?? core_1.DEFAULT_FRAGMENTATION_CONFIG.PREFIX,
|
|
19
|
+
seed: config.seed || seeded_shuffle_1.SeededRandom.generateSeed(),
|
|
20
|
+
preserveName: config.preserveName ?? core_1.DEFAULT_FRAGMENTATION_CONFIG.PRESERVE_NAME,
|
|
21
|
+
crossImageShuffle: config.crossImageShuffle ??
|
|
22
|
+
core_1.DEFAULT_FRAGMENTATION_CONFIG.CROSS_IMAGE_SHUFFLE,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
async fragmentImages(paths) {
|
|
26
|
+
const { manifest, blocks, blockCountsForCrossImages, blockCountsPerImage } = await this._prepareData(paths);
|
|
27
|
+
const shuffled = this.config.crossImageShuffle
|
|
28
|
+
? (0, seeded_shuffle_1.shuffle)(blocks, manifest.config.seed)
|
|
29
|
+
: (0, block_1.blocksPerImage)(blocks, blockCountsPerImage, manifest.config.seed, seeded_shuffle_1.shuffle);
|
|
30
|
+
const blockCounts = this.config.crossImageShuffle
|
|
31
|
+
? blockCountsForCrossImages
|
|
32
|
+
: blockCountsPerImage;
|
|
33
|
+
const fragmentedImages = await this._createImages(shuffled, blockCounts, manifest);
|
|
34
|
+
return {
|
|
35
|
+
manifest,
|
|
36
|
+
fragmentedImages,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async _createImages(blocks, blockCounts, manifest) {
|
|
40
|
+
return await Promise.all(manifest.images.map(async (_, index) => {
|
|
41
|
+
const { start, end } = (0, core_1.calculateBlockRange)(blockCounts, index);
|
|
42
|
+
const imageBlocks = blocks.slice(start, end);
|
|
43
|
+
return await this._createImage(imageBlocks, manifest.config.blockSize);
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
_createManifest(manifestId, imageInfos) {
|
|
47
|
+
return {
|
|
48
|
+
id: manifestId,
|
|
49
|
+
version: constants_1.VERSION,
|
|
50
|
+
timestamp: new Date().toISOString(),
|
|
51
|
+
config: this.config,
|
|
52
|
+
images: imageInfos,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async _prepareData(paths) {
|
|
56
|
+
const manifestId = (0, utils_1.generateManifestId)();
|
|
57
|
+
const { imageInfos, blocks } = await this._processImages(paths);
|
|
58
|
+
(0, core_1.validateFileNames)(imageInfos, this.config.preserveName);
|
|
59
|
+
const manifest = this._createManifest(manifestId, imageInfos);
|
|
60
|
+
const blockCountsForCrossImages = (0, core_1.calculateBlockCountsForCrossImages)(blocks.length, paths.length);
|
|
61
|
+
// Calculate actual block counts per image for per-image shuffle
|
|
62
|
+
const blockCountsPerImage = (0, core_1.calculateBlockCountsPerImage)(imageInfos);
|
|
63
|
+
return { manifest, blocks, blockCountsForCrossImages, blockCountsPerImage };
|
|
64
|
+
}
|
|
65
|
+
async _processImages(paths) {
|
|
66
|
+
const results = await Promise.all(paths.map((path) => this._processImage(path)));
|
|
67
|
+
const imageInfos = results.map((r) => r.imageInfo);
|
|
68
|
+
const blocks = results.flatMap((r) => r.blocks);
|
|
69
|
+
return { imageInfos, blocks };
|
|
70
|
+
}
|
|
71
|
+
async _processImage(path) {
|
|
72
|
+
const buffer = await (0, file_1.readFileBuffer)(path);
|
|
73
|
+
const { blocks, width, height, blockCountX, blockCountY } = await (0, block_1.imageFileToBlocks)(buffer, this.config.blockSize);
|
|
74
|
+
const imageInfo = {
|
|
75
|
+
w: width,
|
|
76
|
+
h: height,
|
|
77
|
+
c: 4, // Always use 4 channels (RGBA) for generated PNG
|
|
78
|
+
x: blockCountX,
|
|
79
|
+
y: blockCountY,
|
|
80
|
+
name: this.config.preserveName
|
|
81
|
+
? (0, core_1.encodeFileName)((0, file_1.fileNameWithoutExtension)(path))
|
|
82
|
+
: undefined,
|
|
83
|
+
};
|
|
84
|
+
return { imageInfo, blocks };
|
|
85
|
+
}
|
|
86
|
+
async _createImage(blocks, blockSize) {
|
|
87
|
+
const blockCount = blocks.length;
|
|
88
|
+
const blocksPerRow = Math.ceil(Math.sqrt(blockCount));
|
|
89
|
+
const imageWidth = blocksPerRow * blockSize;
|
|
90
|
+
const imageHeight = Math.ceil(blockCount / blocksPerRow) * blockSize;
|
|
91
|
+
return await (0, block_1.blocksToPngImage)(blocks, imageWidth, imageHeight, blockSize);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.ImageFragmenter = ImageFragmenter;
|
|
95
|
+
//# sourceMappingURL=fragmenter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fragmenter.js","sourceRoot":"","sources":["../../src/fragmenter.ts"],"names":[],"mappings":";;;AAAA,uCAWsB;AACtB,6DAAiE;AACjE,mCAA8E;AAC9E,2CAAsC;AACtC,iCAAkE;AAClE,mCAA6C;AAE7C,MAAa,eAAe;IAClB,MAAM,CAAgC;IAE9C,YAAY,MAA2B;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;IAEO,iBAAiB,CACvB,MAA2B;QAE3B,OAAO;YACL,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,mCAA4B,CAAC,UAAU;YACtE,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,mCAA4B,CAAC,MAAM;YAC5D,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,6BAAY,CAAC,YAAY,EAAE;YAChD,YAAY,EACV,MAAM,CAAC,YAAY,IAAI,mCAA4B,CAAC,aAAa;YACnE,iBAAiB,EACf,MAAM,CAAC,iBAAiB;gBAC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CAuJC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FragmentationConfig, type ManifestData, type RestoreOptions, type ShuffleOptions } from "@pixzle/core";
|
|
2
|
+
import { ImageFragmenter } from "./fragmenter";
|
|
3
|
+
import { ImageRestorer } from "./restorer";
|
|
4
|
+
export { ImageFragmenter, ImageRestorer, type FragmentationConfig, type ManifestData, };
|
|
5
|
+
declare function shuffle(options: ShuffleOptions): Promise<void>;
|
|
6
|
+
declare function restore(options: RestoreOptions): Promise<void>;
|
|
7
|
+
declare const pixzle: {
|
|
8
|
+
shuffle: typeof shuffle;
|
|
9
|
+
restore: typeof restore;
|
|
10
|
+
};
|
|
11
|
+
export default pixzle;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,mBAAmB,EAExB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,cAAc,EAKpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,EACL,eAAe,EACf,aAAa,EACb,KAAK,mBAAmB,EACxB,KAAK,YAAY,GAClB,CAAC;AAEF,iBAAe,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAoB7D;AAED,iBAAe,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAsB7D;AAED,QAAA,MAAM,MAAM;;;CAGX,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageRestorer = exports.ImageFragmenter = void 0;
|
|
4
|
+
const core_1 = require("@pixzle/core");
|
|
5
|
+
const file_1 = require("./file");
|
|
6
|
+
const fragmenter_1 = require("./fragmenter");
|
|
7
|
+
Object.defineProperty(exports, "ImageFragmenter", { enumerable: true, get: function () { return fragmenter_1.ImageFragmenter; } });
|
|
8
|
+
const restorer_1 = require("./restorer");
|
|
9
|
+
Object.defineProperty(exports, "ImageRestorer", { enumerable: true, get: function () { return restorer_1.ImageRestorer; } });
|
|
10
|
+
async function shuffle(options) {
|
|
11
|
+
const { imagePaths, config, outputDir } = validateShuffleOptions(options);
|
|
12
|
+
const fragmenter = new fragmenter_1.ImageFragmenter(config ?? {});
|
|
13
|
+
const { manifest, fragmentedImages } = await fragmenter.fragmentImages(imagePaths);
|
|
14
|
+
await (0, file_1.createDir)(outputDir, true);
|
|
15
|
+
await (0, file_1.writeFile)(outputDir, core_1.MANIFEST_FILE_NAME, JSON.stringify(manifest, null, 2));
|
|
16
|
+
await Promise.all(fragmentedImages.map((img, i) => {
|
|
17
|
+
const filename = (0, core_1.generateFragmentFileName)(manifest, i);
|
|
18
|
+
return (0, file_1.writeFile)(outputDir, filename, img);
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
async function restore(options) {
|
|
22
|
+
const { imagePaths, manifestPath, outputDir } = validateRestoreOptions(options);
|
|
23
|
+
const manifest = await (0, file_1.readJsonFile)(manifestPath);
|
|
24
|
+
(0, core_1.validateFragmentImageCount)(imagePaths, manifest);
|
|
25
|
+
const restorer = new restorer_1.ImageRestorer();
|
|
26
|
+
const restoredImages = await restorer.restoreImages(imagePaths, manifest);
|
|
27
|
+
await (0, file_1.createDir)(outputDir, true);
|
|
28
|
+
const imageInfos = manifest.images;
|
|
29
|
+
await Promise.all(restoredImages.map((img, i) => {
|
|
30
|
+
const filename = (0, core_1.generateRestoredOriginalFileName)(imageInfos[i]) ??
|
|
31
|
+
(0, core_1.generateRestoredFileName)(manifest, i);
|
|
32
|
+
return (0, file_1.writeFile)(outputDir, filename, img);
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
const pixzle = {
|
|
36
|
+
shuffle,
|
|
37
|
+
restore,
|
|
38
|
+
};
|
|
39
|
+
exports.default = pixzle;
|
|
40
|
+
function validateCommonOptions(options, context) {
|
|
41
|
+
if (!options)
|
|
42
|
+
throw new Error(`[${context}] Options object is required.`);
|
|
43
|
+
const { imagePaths, outputDir } = options;
|
|
44
|
+
if (!imagePaths || !Array.isArray(imagePaths) || imagePaths.length === 0)
|
|
45
|
+
throw new Error(`[${context}] imagePaths must be a non-empty array.`);
|
|
46
|
+
if (!outputDir || typeof outputDir !== "string")
|
|
47
|
+
throw new Error(`[${context}] outputDir is required and must be a string.`);
|
|
48
|
+
return options;
|
|
49
|
+
}
|
|
50
|
+
function validateShuffleOptions(options) {
|
|
51
|
+
return validateCommonOptions(options, "shuffle");
|
|
52
|
+
}
|
|
53
|
+
function validateRestoreOptions(options) {
|
|
54
|
+
const { manifestPath } = options;
|
|
55
|
+
if (!manifestPath || typeof manifestPath !== "string")
|
|
56
|
+
throw new Error("[restore] manifestPath is required and must be a string.");
|
|
57
|
+
return validateCommonOptions(options, "restore");
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAUsB;AACtB,iCAA4D;AAC5D,6CAA+C;AAI7C,gGAJO,4BAAe,OAIP;AAHjB,yCAA2C;AAIzC,8FAJO,wBAAa,OAIP;AAKf,KAAK,UAAU,OAAO,CAAC,OAAuB;IAC5C,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAE1E,MAAM,UAAU,GAAG,IAAI,4BAAe,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;IACrD,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,GAClC,MAAM,UAAU,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
export declare class ImageRestorer {
|
|
3
|
+
restoreImages(fragments: (string | Buffer)[], manifest: ManifestData): Promise<Buffer[]>;
|
|
4
|
+
private _reconstructImages;
|
|
5
|
+
private _prepareData;
|
|
6
|
+
private _readBlocksFromFragment;
|
|
7
|
+
private _readBlocks;
|
|
8
|
+
private _createImage;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=restorer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restorer.d.ts","sourceRoot":"","sources":["../../src/restorer.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EAKlB,MAAM,cAAc,CAAC;AAKtB,qBAAa,aAAa;IAClB,aAAa,CACjB,SAAS,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAC9B,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC,MAAM,EAAE,CAAC;YAkBN,kBAAkB;YAkBlB,YAAY;YA2BZ,uBAAuB;YAgBvB,WAAW;YAaX,YAAY;CAQ3B"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageRestorer = void 0;
|
|
4
|
+
const core_1 = require("@pixzle/core");
|
|
5
|
+
const seeded_shuffle_1 = require("@tuki0918/seeded-shuffle");
|
|
6
|
+
const block_1 = require("./block");
|
|
7
|
+
const file_1 = require("./file");
|
|
8
|
+
class ImageRestorer {
|
|
9
|
+
async restoreImages(fragments, manifest) {
|
|
10
|
+
const { blocks, blockCountsPerImage } = await this._prepareData(fragments, manifest);
|
|
11
|
+
const restored = manifest.config.crossImageShuffle
|
|
12
|
+
? (0, seeded_shuffle_1.unshuffle)(blocks, manifest.config.seed)
|
|
13
|
+
: (0, block_1.blocksPerImage)(blocks, blockCountsPerImage, manifest.config.seed, seeded_shuffle_1.unshuffle);
|
|
14
|
+
return await this._reconstructImages(restored, manifest);
|
|
15
|
+
}
|
|
16
|
+
async _reconstructImages(blocks, manifest) {
|
|
17
|
+
const blockCountsPerImage = (0, core_1.calculateBlockCountsPerImage)(manifest.images);
|
|
18
|
+
return await Promise.all(manifest.images.map(async (imageInfo, index) => {
|
|
19
|
+
const { start, end } = (0, core_1.calculateBlockRange)(blockCountsPerImage, index);
|
|
20
|
+
const imageBlocks = blocks.slice(start, end);
|
|
21
|
+
return await this._createImage(imageBlocks, manifest.config.blockSize, imageInfo);
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
async _prepareData(fragments, manifest) {
|
|
25
|
+
const totalBlocks = (0, core_1.calculateTotalBlocks)(manifest.images);
|
|
26
|
+
const blockCountsForCrossImages = (0, core_1.calculateBlockCountsForCrossImages)(totalBlocks, fragments.length);
|
|
27
|
+
// Calculate actual block counts per image for per-image unshuffle
|
|
28
|
+
const blockCountsPerImage = (0, core_1.calculateBlockCountsPerImage)(manifest.images);
|
|
29
|
+
// Use blockCountsPerImage when crossImageShuffle is false
|
|
30
|
+
const blockCounts = manifest.config.crossImageShuffle
|
|
31
|
+
? blockCountsForCrossImages
|
|
32
|
+
: blockCountsPerImage;
|
|
33
|
+
const blocks = await this._readBlocks(fragments, manifest, blockCounts);
|
|
34
|
+
return { blocks, blockCountsPerImage };
|
|
35
|
+
}
|
|
36
|
+
// Extract an array of blocks (Buffer) from a fragment image
|
|
37
|
+
async _readBlocksFromFragment(fragment, manifest, expectedCount) {
|
|
38
|
+
const buffer = Buffer.isBuffer(fragment)
|
|
39
|
+
? fragment
|
|
40
|
+
: await (0, file_1.readFileBuffer)(fragment);
|
|
41
|
+
const { blocks } = await (0, block_1.imageFileToBlocks)(buffer, manifest.config.blockSize);
|
|
42
|
+
return blocks.slice(0, expectedCount);
|
|
43
|
+
}
|
|
44
|
+
async _readBlocks(fragments, manifest, blockCounts) {
|
|
45
|
+
const blockGroups = await Promise.all(fragments.map((fragment, i) => this._readBlocksFromFragment(fragment, manifest, blockCounts[i])));
|
|
46
|
+
return blockGroups.flat();
|
|
47
|
+
}
|
|
48
|
+
async _createImage(blocks, blockSize, imageInfo) {
|
|
49
|
+
const { w, h } = imageInfo;
|
|
50
|
+
return await (0, block_1.blocksToPngImage)(blocks, w, h, blockSize);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ImageRestorer = ImageRestorer;
|
|
54
|
+
//# sourceMappingURL=restorer.js.map
|