@bcts/lifehash-cli 1.0.0-alpha.17
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 +48 -0
- package/README.md +109 -0
- package/dist/index.cjs +317 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +191 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +191 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +308 -0
- package/dist/index.mjs.map +1 -0
- package/dist/main.mjs +209 -0
- package/dist/main.mjs.map +1 -0
- package/package.json +92 -0
- package/src/index.ts +104 -0
- package/src/main.ts +150 -0
- package/src/png-writer.ts +101 -0
- package/src/utils.ts +75 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Image, Image as Image$1, Version, Version as Version$1, makeFromUtf8 } from "@bcts/lifehash";
|
|
2
|
+
|
|
3
|
+
//#region src/png-writer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Writes a LifeHash image to a PNG file.
|
|
6
|
+
*
|
|
7
|
+
* Port of `write_image()` from lifehash.cpp lines 174-186 and
|
|
8
|
+
* PNGWriter class from png-writer.hpp.
|
|
9
|
+
*
|
|
10
|
+
* @param image - The LifeHash image to write
|
|
11
|
+
* @param filename - The output filename
|
|
12
|
+
* @category PNG Encoding
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
17
|
+
*
|
|
18
|
+
* const image = makeFromUtf8("Hello");
|
|
19
|
+
* writeImage(image, "Hello.png");
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function writeImage(image: Image$1, filename: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Generates a PNG buffer from a LifeHash image without writing to disk.
|
|
25
|
+
*
|
|
26
|
+
* @param image - The LifeHash image to encode
|
|
27
|
+
* @returns A Buffer containing the PNG data
|
|
28
|
+
* @category PNG Encoding
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
33
|
+
*
|
|
34
|
+
* const image = makeFromUtf8("Hello");
|
|
35
|
+
* const pngBuffer = generatePNG(image);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function generatePNG(image: Image$1): Buffer;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/main.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* CLI options interface.
|
|
43
|
+
*
|
|
44
|
+
* Port of Parameters struct from lifehash.cpp lines 86-91.
|
|
45
|
+
*
|
|
46
|
+
* @category CLI
|
|
47
|
+
*/
|
|
48
|
+
interface CliOptions {
|
|
49
|
+
/** LifeHash version to generate */
|
|
50
|
+
version: string;
|
|
51
|
+
/** Size of each module ("pixel") */
|
|
52
|
+
module: string;
|
|
53
|
+
/** Output directory path */
|
|
54
|
+
path: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Parses a version string to the Version enum.
|
|
58
|
+
*
|
|
59
|
+
* Port of version parsing logic from lifehash.cpp lines 130-145.
|
|
60
|
+
*
|
|
61
|
+
* @param versionString - The version string from CLI
|
|
62
|
+
* @returns The corresponding Version enum value
|
|
63
|
+
* @throws Error if the version string is invalid
|
|
64
|
+
* @category CLI
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* parseVersion("version2") // => Version.version2
|
|
69
|
+
* parseVersion("detailed") // => Version.detailed
|
|
70
|
+
* parseVersion("invalid") // throws Error
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function parseVersion(versionString: string): Version$1;
|
|
74
|
+
/**
|
|
75
|
+
* Main execution function that generates a LifeHash image.
|
|
76
|
+
*
|
|
77
|
+
* Port of `run()` from lifehash.cpp lines 188-192.
|
|
78
|
+
*
|
|
79
|
+
* @param input - Input string to hash (or empty for random)
|
|
80
|
+
* @param options - CLI options
|
|
81
|
+
* @category CLI
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* run("Hello", { version: "version2", module: "1", path: "." });
|
|
86
|
+
* // Generates Hello.png in current directory
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function run(input: string, options: CliOptions): void;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/utils.d.ts
|
|
92
|
+
/**
|
|
93
|
+
* Utility functions for the LifeHash CLI.
|
|
94
|
+
*
|
|
95
|
+
* Ported from bc-lifehash-cli C++ implementation.
|
|
96
|
+
*
|
|
97
|
+
* @module
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* Appends a path component to a path, handling trailing slashes correctly.
|
|
101
|
+
*
|
|
102
|
+
* Port of `appending_path_component()` from lifehash.cpp lines 18-24.
|
|
103
|
+
*
|
|
104
|
+
* @param path - The base path
|
|
105
|
+
* @param component - The component to append
|
|
106
|
+
* @returns The combined path
|
|
107
|
+
* @category Utilities
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* appendingPathComponent("", "file.png") // => "file.png"
|
|
112
|
+
* appendingPathComponent("/tmp/", "file.png") // => "/tmp/file.png"
|
|
113
|
+
* appendingPathComponent("/tmp", "file.png") // => "/tmp/file.png"
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function appendingPathComponent(path: string, component: string): string;
|
|
117
|
+
/**
|
|
118
|
+
* Selects a random element from an array.
|
|
119
|
+
*
|
|
120
|
+
* Port of `random_element()` template from lifehash.cpp lines 38-50.
|
|
121
|
+
*
|
|
122
|
+
* @param array - The array to select from
|
|
123
|
+
* @returns A random element from the array
|
|
124
|
+
* @category Utilities
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* randomElement(["A", "B", "C"]) // => "A" or "B" or "C"
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare function randomElement<T>(array: T[]): T;
|
|
132
|
+
/**
|
|
133
|
+
* Generates a random input string in "XXX-XXX" format where X is a random uppercase letter.
|
|
134
|
+
*
|
|
135
|
+
* Port of `make_random_input()` from lifehash.cpp lines 52-57.
|
|
136
|
+
*
|
|
137
|
+
* @returns A random string in "XXX-XXX" format
|
|
138
|
+
* @category Utilities
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* makeRandomInput() // => "ABC-DEF" (random letters)
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
declare function makeRandomInput(): string;
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/index.d.ts
|
|
148
|
+
/**
|
|
149
|
+
* Options for generating a LifeHash image.
|
|
150
|
+
*
|
|
151
|
+
* @category Image Generation
|
|
152
|
+
*/
|
|
153
|
+
interface GenerateOptions {
|
|
154
|
+
/**
|
|
155
|
+
* LifeHash version to generate.
|
|
156
|
+
* @default "version2"
|
|
157
|
+
*/
|
|
158
|
+
version?: "version1" | "version2" | "detailed" | "fiducial" | "grayscaleFiducial";
|
|
159
|
+
/**
|
|
160
|
+
* Size of each module ("pixel").
|
|
161
|
+
* @default 1
|
|
162
|
+
*/
|
|
163
|
+
moduleSize?: number;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Generates a LifeHash PNG buffer from an input string.
|
|
167
|
+
*
|
|
168
|
+
* This is the main programmatic API for generating LifeHash images.
|
|
169
|
+
*
|
|
170
|
+
* @param input - The input string to hash
|
|
171
|
+
* @param options - Generation options
|
|
172
|
+
* @returns A Buffer containing the PNG data
|
|
173
|
+
* @category Image Generation
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* import { generateLifeHash } from "@bcts/lifehash-cli";
|
|
178
|
+
* import { writeFileSync } from "fs";
|
|
179
|
+
*
|
|
180
|
+
* const pngBuffer = generateLifeHash("Hello", {
|
|
181
|
+
* version: "version2",
|
|
182
|
+
* moduleSize: 1,
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* writeFileSync("Hello.png", pngBuffer);
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
declare function generateLifeHash(input: string, options?: GenerateOptions): Buffer;
|
|
189
|
+
//#endregion
|
|
190
|
+
export { type CliOptions, GenerateOptions, type Image, Version, appendingPathComponent, generateLifeHash, generatePNG, makeFromUtf8, makeRandomInput, parseVersion, randomElement, run, writeImage };
|
|
191
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/png-writer.ts","../src/main.ts","../src/utils.ts","../src/index.ts"],"mappings":";;;;;;AA4EA;;;;;;;;;;;;ACxDA;;;iBDWgB,UAAA,CAAW,KAAA,EAAO,OAAA,EAAO,QAAA;;;;;;ACezC;;;;;AAgCA;;;;;iBDFgB,WAAA,CAAY,KAAA,EAAO,OAAA,GAAQ,MAAA;;;;;;;AAA3C;;;UCxDiB,UAAA;EDwDkB;ECtDjC,OAAA;EDsDyC;ECpDzC,MAAA;EDoD+C;EClD/C,IAAA;AAAA;;AANF;;;;;;;;;AA0BA;;;;;AAgCA;;iBAhCgB,YAAA,CAAa,aAAA,WAAwB,SAAA;;;;;;;;;;ACrBrD;;;;;AAwBA;iBD6BgB,GAAA,CAAI,KAAA,UAAe,OAAA,EAAS,UAAA;;;;;;AD/C5C;;;;;;;;;AA6CA;;;;;;;;;;;;iBEnDgB,sBAAA,CAAuB,IAAA,UAAc,SAAA;;;;;;;;;ADqBrD;;;;;AAgCA;iBC7BgB,aAAA,GAAA,CAAiB,KAAA,EAAO,CAAA,KAAM,CAAA;;;;;;;;;;;AAxB9C;;;iBA0CgB,eAAA,CAAA;;;;;;;;UCNC,eAAA;EDpCD;;;;ECyCd,OAAA;EDjBc;;;;ECsBd,UAAA;AAAA;;;;;ADJF;;;;;;;;ACNA;;;;;AAoCA;;;;;;iBAAgB,gBAAA,CAAiB,KAAA,UAAe,OAAA,GAAS,eAAA,GAAuB,MAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Image, Image as Image$1, Version, Version as Version$1, makeFromUtf8 } from "@bcts/lifehash";
|
|
2
|
+
|
|
3
|
+
//#region src/png-writer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Writes a LifeHash image to a PNG file.
|
|
6
|
+
*
|
|
7
|
+
* Port of `write_image()` from lifehash.cpp lines 174-186 and
|
|
8
|
+
* PNGWriter class from png-writer.hpp.
|
|
9
|
+
*
|
|
10
|
+
* @param image - The LifeHash image to write
|
|
11
|
+
* @param filename - The output filename
|
|
12
|
+
* @category PNG Encoding
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
17
|
+
*
|
|
18
|
+
* const image = makeFromUtf8("Hello");
|
|
19
|
+
* writeImage(image, "Hello.png");
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function writeImage(image: Image$1, filename: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Generates a PNG buffer from a LifeHash image without writing to disk.
|
|
25
|
+
*
|
|
26
|
+
* @param image - The LifeHash image to encode
|
|
27
|
+
* @returns A Buffer containing the PNG data
|
|
28
|
+
* @category PNG Encoding
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
33
|
+
*
|
|
34
|
+
* const image = makeFromUtf8("Hello");
|
|
35
|
+
* const pngBuffer = generatePNG(image);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function generatePNG(image: Image$1): Buffer;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/main.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* CLI options interface.
|
|
43
|
+
*
|
|
44
|
+
* Port of Parameters struct from lifehash.cpp lines 86-91.
|
|
45
|
+
*
|
|
46
|
+
* @category CLI
|
|
47
|
+
*/
|
|
48
|
+
interface CliOptions {
|
|
49
|
+
/** LifeHash version to generate */
|
|
50
|
+
version: string;
|
|
51
|
+
/** Size of each module ("pixel") */
|
|
52
|
+
module: string;
|
|
53
|
+
/** Output directory path */
|
|
54
|
+
path: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Parses a version string to the Version enum.
|
|
58
|
+
*
|
|
59
|
+
* Port of version parsing logic from lifehash.cpp lines 130-145.
|
|
60
|
+
*
|
|
61
|
+
* @param versionString - The version string from CLI
|
|
62
|
+
* @returns The corresponding Version enum value
|
|
63
|
+
* @throws Error if the version string is invalid
|
|
64
|
+
* @category CLI
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* parseVersion("version2") // => Version.version2
|
|
69
|
+
* parseVersion("detailed") // => Version.detailed
|
|
70
|
+
* parseVersion("invalid") // throws Error
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function parseVersion(versionString: string): Version$1;
|
|
74
|
+
/**
|
|
75
|
+
* Main execution function that generates a LifeHash image.
|
|
76
|
+
*
|
|
77
|
+
* Port of `run()` from lifehash.cpp lines 188-192.
|
|
78
|
+
*
|
|
79
|
+
* @param input - Input string to hash (or empty for random)
|
|
80
|
+
* @param options - CLI options
|
|
81
|
+
* @category CLI
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* run("Hello", { version: "version2", module: "1", path: "." });
|
|
86
|
+
* // Generates Hello.png in current directory
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
declare function run(input: string, options: CliOptions): void;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/utils.d.ts
|
|
92
|
+
/**
|
|
93
|
+
* Utility functions for the LifeHash CLI.
|
|
94
|
+
*
|
|
95
|
+
* Ported from bc-lifehash-cli C++ implementation.
|
|
96
|
+
*
|
|
97
|
+
* @module
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* Appends a path component to a path, handling trailing slashes correctly.
|
|
101
|
+
*
|
|
102
|
+
* Port of `appending_path_component()` from lifehash.cpp lines 18-24.
|
|
103
|
+
*
|
|
104
|
+
* @param path - The base path
|
|
105
|
+
* @param component - The component to append
|
|
106
|
+
* @returns The combined path
|
|
107
|
+
* @category Utilities
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* appendingPathComponent("", "file.png") // => "file.png"
|
|
112
|
+
* appendingPathComponent("/tmp/", "file.png") // => "/tmp/file.png"
|
|
113
|
+
* appendingPathComponent("/tmp", "file.png") // => "/tmp/file.png"
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function appendingPathComponent(path: string, component: string): string;
|
|
117
|
+
/**
|
|
118
|
+
* Selects a random element from an array.
|
|
119
|
+
*
|
|
120
|
+
* Port of `random_element()` template from lifehash.cpp lines 38-50.
|
|
121
|
+
*
|
|
122
|
+
* @param array - The array to select from
|
|
123
|
+
* @returns A random element from the array
|
|
124
|
+
* @category Utilities
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* randomElement(["A", "B", "C"]) // => "A" or "B" or "C"
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare function randomElement<T>(array: T[]): T;
|
|
132
|
+
/**
|
|
133
|
+
* Generates a random input string in "XXX-XXX" format where X is a random uppercase letter.
|
|
134
|
+
*
|
|
135
|
+
* Port of `make_random_input()` from lifehash.cpp lines 52-57.
|
|
136
|
+
*
|
|
137
|
+
* @returns A random string in "XXX-XXX" format
|
|
138
|
+
* @category Utilities
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* makeRandomInput() // => "ABC-DEF" (random letters)
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
declare function makeRandomInput(): string;
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/index.d.ts
|
|
148
|
+
/**
|
|
149
|
+
* Options for generating a LifeHash image.
|
|
150
|
+
*
|
|
151
|
+
* @category Image Generation
|
|
152
|
+
*/
|
|
153
|
+
interface GenerateOptions {
|
|
154
|
+
/**
|
|
155
|
+
* LifeHash version to generate.
|
|
156
|
+
* @default "version2"
|
|
157
|
+
*/
|
|
158
|
+
version?: "version1" | "version2" | "detailed" | "fiducial" | "grayscaleFiducial";
|
|
159
|
+
/**
|
|
160
|
+
* Size of each module ("pixel").
|
|
161
|
+
* @default 1
|
|
162
|
+
*/
|
|
163
|
+
moduleSize?: number;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Generates a LifeHash PNG buffer from an input string.
|
|
167
|
+
*
|
|
168
|
+
* This is the main programmatic API for generating LifeHash images.
|
|
169
|
+
*
|
|
170
|
+
* @param input - The input string to hash
|
|
171
|
+
* @param options - Generation options
|
|
172
|
+
* @returns A Buffer containing the PNG data
|
|
173
|
+
* @category Image Generation
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* import { generateLifeHash } from "@bcts/lifehash-cli";
|
|
178
|
+
* import { writeFileSync } from "fs";
|
|
179
|
+
*
|
|
180
|
+
* const pngBuffer = generateLifeHash("Hello", {
|
|
181
|
+
* version: "version2",
|
|
182
|
+
* moduleSize: 1,
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* writeFileSync("Hello.png", pngBuffer);
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
declare function generateLifeHash(input: string, options?: GenerateOptions): Buffer;
|
|
189
|
+
//#endregion
|
|
190
|
+
export { type CliOptions, GenerateOptions, type Image, Version, appendingPathComponent, generateLifeHash, generatePNG, makeFromUtf8, makeRandomInput, parseVersion, randomElement, run, writeImage };
|
|
191
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/png-writer.ts","../src/main.ts","../src/utils.ts","../src/index.ts"],"mappings":";;;;;;AA4EA;;;;;;;;;;;;ACxDA;;;iBDWgB,UAAA,CAAW,KAAA,EAAO,OAAA,EAAO,QAAA;;;;;;ACezC;;;;;AAgCA;;;;;iBDFgB,WAAA,CAAY,KAAA,EAAO,OAAA,GAAQ,MAAA;;;;;;;AAA3C;;;UCxDiB,UAAA;EDwDkB;ECtDjC,OAAA;EDsDyC;ECpDzC,MAAA;EDoD+C;EClD/C,IAAA;AAAA;;AANF;;;;;;;;;AA0BA;;;;;AAgCA;;iBAhCgB,YAAA,CAAa,aAAA,WAAwB,SAAA;;;;;;;;;;ACrBrD;;;;;AAwBA;iBD6BgB,GAAA,CAAI,KAAA,UAAe,OAAA,EAAS,UAAA;;;;;;AD/C5C;;;;;;;;;AA6CA;;;;;;;;;;;;iBEnDgB,sBAAA,CAAuB,IAAA,UAAc,SAAA;;;;;;;;;ADqBrD;;;;;AAgCA;iBC7BgB,aAAA,GAAA,CAAiB,KAAA,EAAO,CAAA,KAAM,CAAA;;;;;;;;;;;AAxB9C;;;iBA0CgB,eAAA,CAAA;;;;;;;;UCNC,eAAA;EDpCD;;;;ECyCd,OAAA;EDjBc;;;;ECsBd,UAAA;AAAA;;;;;ADJF;;;;;;;;ACNA;;;;;AAoCA;;;;;;iBAAgB,gBAAA,CAAiB,KAAA,UAAe,OAAA,GAAS,eAAA,GAAuB,MAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { writeFileSync } from "fs";
|
|
2
|
+
import { PNG } from "pngjs";
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { Version, Version as Version$1, makeFromUtf8, makeFromUtf8 as makeFromUtf8$1 } from "@bcts/lifehash";
|
|
5
|
+
|
|
6
|
+
//#region src/png-writer.ts
|
|
7
|
+
/**
|
|
8
|
+
* PNG writer for LifeHash images.
|
|
9
|
+
*
|
|
10
|
+
* Ported from bc-lifehash-cli C++ implementation (png-writer.hpp).
|
|
11
|
+
* Uses pngjs instead of libpng.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Writes a LifeHash image to a PNG file.
|
|
17
|
+
*
|
|
18
|
+
* Port of `write_image()` from lifehash.cpp lines 174-186 and
|
|
19
|
+
* PNGWriter class from png-writer.hpp.
|
|
20
|
+
*
|
|
21
|
+
* @param image - The LifeHash image to write
|
|
22
|
+
* @param filename - The output filename
|
|
23
|
+
* @category PNG Encoding
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
28
|
+
*
|
|
29
|
+
* const image = makeFromUtf8("Hello");
|
|
30
|
+
* writeImage(image, "Hello.png");
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function writeImage(image, filename) {
|
|
34
|
+
const png = new PNG({
|
|
35
|
+
width: image.width,
|
|
36
|
+
height: image.height,
|
|
37
|
+
colorType: 2,
|
|
38
|
+
bitDepth: 8,
|
|
39
|
+
inputColorType: 2,
|
|
40
|
+
inputHasAlpha: false
|
|
41
|
+
});
|
|
42
|
+
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
|
|
43
|
+
const srcOffset = (y * image.width + x) * 3;
|
|
44
|
+
const dstOffset = (y * image.width + x) * 4;
|
|
45
|
+
png.data[dstOffset] = image.colors[srcOffset];
|
|
46
|
+
png.data[dstOffset + 1] = image.colors[srcOffset + 1];
|
|
47
|
+
png.data[dstOffset + 2] = image.colors[srcOffset + 2];
|
|
48
|
+
png.data[dstOffset + 3] = 255;
|
|
49
|
+
}
|
|
50
|
+
writeFileSync(filename, PNG.sync.write(png));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Generates a PNG buffer from a LifeHash image without writing to disk.
|
|
54
|
+
*
|
|
55
|
+
* @param image - The LifeHash image to encode
|
|
56
|
+
* @returns A Buffer containing the PNG data
|
|
57
|
+
* @category PNG Encoding
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { makeFromUtf8 } from "@bcts/lifehash";
|
|
62
|
+
*
|
|
63
|
+
* const image = makeFromUtf8("Hello");
|
|
64
|
+
* const pngBuffer = generatePNG(image);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function generatePNG(image) {
|
|
68
|
+
const png = new PNG({
|
|
69
|
+
width: image.width,
|
|
70
|
+
height: image.height,
|
|
71
|
+
colorType: 2,
|
|
72
|
+
bitDepth: 8,
|
|
73
|
+
inputColorType: 2,
|
|
74
|
+
inputHasAlpha: false
|
|
75
|
+
});
|
|
76
|
+
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
|
|
77
|
+
const srcOffset = (y * image.width + x) * 3;
|
|
78
|
+
const dstOffset = (y * image.width + x) * 4;
|
|
79
|
+
png.data[dstOffset] = image.colors[srcOffset];
|
|
80
|
+
png.data[dstOffset + 1] = image.colors[srcOffset + 1];
|
|
81
|
+
png.data[dstOffset + 2] = image.colors[srcOffset + 2];
|
|
82
|
+
png.data[dstOffset + 3] = 255;
|
|
83
|
+
}
|
|
84
|
+
return PNG.sync.write(png);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/utils.ts
|
|
89
|
+
/**
|
|
90
|
+
* Utility functions for the LifeHash CLI.
|
|
91
|
+
*
|
|
92
|
+
* Ported from bc-lifehash-cli C++ implementation.
|
|
93
|
+
*
|
|
94
|
+
* @module
|
|
95
|
+
*/
|
|
96
|
+
/**
|
|
97
|
+
* Appends a path component to a path, handling trailing slashes correctly.
|
|
98
|
+
*
|
|
99
|
+
* Port of `appending_path_component()` from lifehash.cpp lines 18-24.
|
|
100
|
+
*
|
|
101
|
+
* @param path - The base path
|
|
102
|
+
* @param component - The component to append
|
|
103
|
+
* @returns The combined path
|
|
104
|
+
* @category Utilities
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* appendingPathComponent("", "file.png") // => "file.png"
|
|
109
|
+
* appendingPathComponent("/tmp/", "file.png") // => "/tmp/file.png"
|
|
110
|
+
* appendingPathComponent("/tmp", "file.png") // => "/tmp/file.png"
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
function appendingPathComponent(path, component) {
|
|
114
|
+
if (path === "") return component;
|
|
115
|
+
if (path.endsWith("/")) return `${path}${component}`;
|
|
116
|
+
return `${path}/${component}`;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Selects a random element from an array.
|
|
120
|
+
*
|
|
121
|
+
* Port of `random_element()` template from lifehash.cpp lines 38-50.
|
|
122
|
+
*
|
|
123
|
+
* @param array - The array to select from
|
|
124
|
+
* @returns A random element from the array
|
|
125
|
+
* @category Utilities
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* randomElement(["A", "B", "C"]) // => "A" or "B" or "C"
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
function randomElement(array) {
|
|
133
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Generates a random input string in "XXX-XXX" format where X is a random uppercase letter.
|
|
137
|
+
*
|
|
138
|
+
* Port of `make_random_input()` from lifehash.cpp lines 52-57.
|
|
139
|
+
*
|
|
140
|
+
* @returns A random string in "XXX-XXX" format
|
|
141
|
+
* @category Utilities
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* makeRandomInput() // => "ABC-DEF" (random letters)
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
function makeRandomInput() {
|
|
149
|
+
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
|
150
|
+
const letter = () => randomElement(letters);
|
|
151
|
+
const cluster = () => `${letter()}${letter()}${letter()}`;
|
|
152
|
+
return `${cluster()}-${cluster()}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/main.ts
|
|
157
|
+
/**
|
|
158
|
+
* LifeHash CLI - Command line tool for generating LifeHash PNG images.
|
|
159
|
+
*
|
|
160
|
+
* Ported from bc-lifehash-cli C++ implementation (lifehash.cpp).
|
|
161
|
+
*
|
|
162
|
+
* @module
|
|
163
|
+
*/
|
|
164
|
+
/**
|
|
165
|
+
* Parses a version string to the Version enum.
|
|
166
|
+
*
|
|
167
|
+
* Port of version parsing logic from lifehash.cpp lines 130-145.
|
|
168
|
+
*
|
|
169
|
+
* @param versionString - The version string from CLI
|
|
170
|
+
* @returns The corresponding Version enum value
|
|
171
|
+
* @throws Error if the version string is invalid
|
|
172
|
+
* @category CLI
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* parseVersion("version2") // => Version.version2
|
|
177
|
+
* parseVersion("detailed") // => Version.detailed
|
|
178
|
+
* parseVersion("invalid") // throws Error
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
function parseVersion(versionString) {
|
|
182
|
+
switch (versionString) {
|
|
183
|
+
case "version1": return Version$1.version1;
|
|
184
|
+
case "version2": return Version$1.version2;
|
|
185
|
+
case "detailed": return Version$1.detailed;
|
|
186
|
+
case "fiducial": return Version$1.fiducial;
|
|
187
|
+
case "grayscaleFiducial": return Version$1.grayscale_fiducial;
|
|
188
|
+
default: throw new Error("Invalid version.");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Main execution function that generates a LifeHash image.
|
|
193
|
+
*
|
|
194
|
+
* Port of `run()` from lifehash.cpp lines 188-192.
|
|
195
|
+
*
|
|
196
|
+
* @param input - Input string to hash (or empty for random)
|
|
197
|
+
* @param options - CLI options
|
|
198
|
+
* @category CLI
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* run("Hello", { version: "version2", module: "1", path: "." });
|
|
203
|
+
* // Generates Hello.png in current directory
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
function run(input, options) {
|
|
207
|
+
const version = parseVersion(options.version);
|
|
208
|
+
const moduleSize = parseInt(options.module, 10);
|
|
209
|
+
if (moduleSize < 1 || isNaN(moduleSize)) throw new Error("Illegal value.");
|
|
210
|
+
const actualInput = input !== "" ? input : makeRandomInput();
|
|
211
|
+
const outputFilename = `${actualInput}.png`;
|
|
212
|
+
const outputFile = appendingPathComponent(options.path, outputFilename);
|
|
213
|
+
writeImage(makeFromUtf8$1(actualInput, version, moduleSize), outputFile);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* CLI entry point.
|
|
217
|
+
*
|
|
218
|
+
* Port of `main()` from lifehash.cpp lines 194-206.
|
|
219
|
+
*
|
|
220
|
+
* @category CLI
|
|
221
|
+
*/
|
|
222
|
+
function main() {
|
|
223
|
+
const program = new Command();
|
|
224
|
+
program.name("lifehash").description("Generate LifeHash PNG images from input strings").argument("[input]", "Input string to hash (default: random XXX-XXX)").option("-v, --version <version>", "LifeHash version: version1, version2, detailed, fiducial, grayscaleFiducial", "version2").option("-m, --module <size>", "Size of each module (\"pixel\")", "1").option("-p, --path <path>", "Output directory path", "").action((input, options) => {
|
|
225
|
+
try {
|
|
226
|
+
run(input ?? "", options);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (error instanceof Error) {
|
|
229
|
+
console.log(`\u{1F928} ${error.message}`);
|
|
230
|
+
console.log();
|
|
231
|
+
program.help();
|
|
232
|
+
}
|
|
233
|
+
process.exit(1);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
program.parse();
|
|
237
|
+
}
|
|
238
|
+
if (typeof process !== "undefined" && typeof process.argv[1] === "string" && (process.argv[1].endsWith("/main.mjs") || process.argv[1].endsWith("/main.js") || process.argv[1].includes("lifehash-cli"))) main();
|
|
239
|
+
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/index.ts
|
|
242
|
+
/**
|
|
243
|
+
* @bcts/lifehash-cli - Command line tool for generating LifeHash PNG images.
|
|
244
|
+
*
|
|
245
|
+
* This package provides both a CLI tool and a programmatic API for generating
|
|
246
|
+
* LifeHash visual hash images as PNG files.
|
|
247
|
+
*
|
|
248
|
+
* @packageDocumentation
|
|
249
|
+
* @module @bcts/lifehash-cli
|
|
250
|
+
*
|
|
251
|
+
* @example CLI Usage
|
|
252
|
+
* ```bash
|
|
253
|
+
* # Generate a LifeHash from a string
|
|
254
|
+
* lifehash Hello
|
|
255
|
+
*
|
|
256
|
+
* # Generate with specific version and module size
|
|
257
|
+
* lifehash -v detailed -m 8 Hello
|
|
258
|
+
*
|
|
259
|
+
* # Generate with random input
|
|
260
|
+
* lifehash
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* @example Programmatic Usage
|
|
264
|
+
* ```typescript
|
|
265
|
+
* import { generateLifeHash } from "@bcts/lifehash-cli";
|
|
266
|
+
* import { writeFileSync } from "fs";
|
|
267
|
+
*
|
|
268
|
+
* // Generate a PNG buffer
|
|
269
|
+
* const pngBuffer = generateLifeHash("Hello", {
|
|
270
|
+
* version: "version2",
|
|
271
|
+
* moduleSize: 1,
|
|
272
|
+
* });
|
|
273
|
+
*
|
|
274
|
+
* // Write to file
|
|
275
|
+
* writeFileSync("Hello.png", pngBuffer);
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
/**
|
|
279
|
+
* Generates a LifeHash PNG buffer from an input string.
|
|
280
|
+
*
|
|
281
|
+
* This is the main programmatic API for generating LifeHash images.
|
|
282
|
+
*
|
|
283
|
+
* @param input - The input string to hash
|
|
284
|
+
* @param options - Generation options
|
|
285
|
+
* @returns A Buffer containing the PNG data
|
|
286
|
+
* @category Image Generation
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```typescript
|
|
290
|
+
* import { generateLifeHash } from "@bcts/lifehash-cli";
|
|
291
|
+
* import { writeFileSync } from "fs";
|
|
292
|
+
*
|
|
293
|
+
* const pngBuffer = generateLifeHash("Hello", {
|
|
294
|
+
* version: "version2",
|
|
295
|
+
* moduleSize: 1,
|
|
296
|
+
* });
|
|
297
|
+
*
|
|
298
|
+
* writeFileSync("Hello.png", pngBuffer);
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
function generateLifeHash(input, options = {}) {
|
|
302
|
+
const { version = "version2", moduleSize = 1 } = options;
|
|
303
|
+
return generatePNG(makeFromUtf8(input, parseVersion(version), moduleSize));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
//#endregion
|
|
307
|
+
export { Version, appendingPathComponent, generateLifeHash, generatePNG, makeFromUtf8, makeRandomInput, parseVersion, randomElement, run, writeImage };
|
|
308
|
+
//# sourceMappingURL=index.mjs.map
|