cross-image 0.1.2

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 (125) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +606 -0
  3. package/esm/mod.d.ts +33 -0
  4. package/esm/mod.d.ts.map +1 -0
  5. package/esm/mod.js +31 -0
  6. package/esm/package.json +3 -0
  7. package/esm/src/formats/ascii.d.ts +27 -0
  8. package/esm/src/formats/ascii.d.ts.map +1 -0
  9. package/esm/src/formats/ascii.js +172 -0
  10. package/esm/src/formats/bmp.d.ts +19 -0
  11. package/esm/src/formats/bmp.d.ts.map +1 -0
  12. package/esm/src/formats/bmp.js +174 -0
  13. package/esm/src/formats/gif.d.ts +40 -0
  14. package/esm/src/formats/gif.d.ts.map +1 -0
  15. package/esm/src/formats/gif.js +385 -0
  16. package/esm/src/formats/jpeg.d.ts +18 -0
  17. package/esm/src/formats/jpeg.d.ts.map +1 -0
  18. package/esm/src/formats/jpeg.js +414 -0
  19. package/esm/src/formats/png.d.ts +33 -0
  20. package/esm/src/formats/png.d.ts.map +1 -0
  21. package/esm/src/formats/png.js +544 -0
  22. package/esm/src/formats/raw.d.ts +23 -0
  23. package/esm/src/formats/raw.d.ts.map +1 -0
  24. package/esm/src/formats/raw.js +98 -0
  25. package/esm/src/formats/tiff.d.ts +58 -0
  26. package/esm/src/formats/tiff.d.ts.map +1 -0
  27. package/esm/src/formats/tiff.js +791 -0
  28. package/esm/src/formats/webp.d.ts +22 -0
  29. package/esm/src/formats/webp.d.ts.map +1 -0
  30. package/esm/src/formats/webp.js +403 -0
  31. package/esm/src/image.d.ts +124 -0
  32. package/esm/src/image.d.ts.map +1 -0
  33. package/esm/src/image.js +320 -0
  34. package/esm/src/types.d.ts +167 -0
  35. package/esm/src/types.d.ts.map +1 -0
  36. package/esm/src/types.js +1 -0
  37. package/esm/src/utils/gif_decoder.d.ts +42 -0
  38. package/esm/src/utils/gif_decoder.d.ts.map +1 -0
  39. package/esm/src/utils/gif_decoder.js +374 -0
  40. package/esm/src/utils/gif_encoder.d.ts +29 -0
  41. package/esm/src/utils/gif_encoder.d.ts.map +1 -0
  42. package/esm/src/utils/gif_encoder.js +226 -0
  43. package/esm/src/utils/jpeg_decoder.d.ts +39 -0
  44. package/esm/src/utils/jpeg_decoder.d.ts.map +1 -0
  45. package/esm/src/utils/jpeg_decoder.js +580 -0
  46. package/esm/src/utils/jpeg_encoder.d.ts +33 -0
  47. package/esm/src/utils/jpeg_encoder.d.ts.map +1 -0
  48. package/esm/src/utils/jpeg_encoder.js +1017 -0
  49. package/esm/src/utils/lzw.d.ts +43 -0
  50. package/esm/src/utils/lzw.d.ts.map +1 -0
  51. package/esm/src/utils/lzw.js +309 -0
  52. package/esm/src/utils/resize.d.ts +9 -0
  53. package/esm/src/utils/resize.d.ts.map +1 -0
  54. package/esm/src/utils/resize.js +52 -0
  55. package/esm/src/utils/tiff_lzw.d.ts +44 -0
  56. package/esm/src/utils/tiff_lzw.d.ts.map +1 -0
  57. package/esm/src/utils/tiff_lzw.js +306 -0
  58. package/esm/src/utils/webp_decoder.d.ts +39 -0
  59. package/esm/src/utils/webp_decoder.d.ts.map +1 -0
  60. package/esm/src/utils/webp_decoder.js +493 -0
  61. package/esm/src/utils/webp_encoder.d.ts +72 -0
  62. package/esm/src/utils/webp_encoder.d.ts.map +1 -0
  63. package/esm/src/utils/webp_encoder.js +627 -0
  64. package/package.json +41 -0
  65. package/script/mod.d.ts +33 -0
  66. package/script/mod.d.ts.map +1 -0
  67. package/script/mod.js +43 -0
  68. package/script/package.json +3 -0
  69. package/script/src/formats/ascii.d.ts +27 -0
  70. package/script/src/formats/ascii.d.ts.map +1 -0
  71. package/script/src/formats/ascii.js +176 -0
  72. package/script/src/formats/bmp.d.ts +19 -0
  73. package/script/src/formats/bmp.d.ts.map +1 -0
  74. package/script/src/formats/bmp.js +178 -0
  75. package/script/src/formats/gif.d.ts +40 -0
  76. package/script/src/formats/gif.d.ts.map +1 -0
  77. package/script/src/formats/gif.js +389 -0
  78. package/script/src/formats/jpeg.d.ts +18 -0
  79. package/script/src/formats/jpeg.d.ts.map +1 -0
  80. package/script/src/formats/jpeg.js +451 -0
  81. package/script/src/formats/png.d.ts +33 -0
  82. package/script/src/formats/png.d.ts.map +1 -0
  83. package/script/src/formats/png.js +548 -0
  84. package/script/src/formats/raw.d.ts +23 -0
  85. package/script/src/formats/raw.d.ts.map +1 -0
  86. package/script/src/formats/raw.js +102 -0
  87. package/script/src/formats/tiff.d.ts +58 -0
  88. package/script/src/formats/tiff.d.ts.map +1 -0
  89. package/script/src/formats/tiff.js +795 -0
  90. package/script/src/formats/webp.d.ts +22 -0
  91. package/script/src/formats/webp.d.ts.map +1 -0
  92. package/script/src/formats/webp.js +440 -0
  93. package/script/src/image.d.ts +124 -0
  94. package/script/src/image.d.ts.map +1 -0
  95. package/script/src/image.js +324 -0
  96. package/script/src/types.d.ts +167 -0
  97. package/script/src/types.d.ts.map +1 -0
  98. package/script/src/types.js +2 -0
  99. package/script/src/utils/gif_decoder.d.ts +42 -0
  100. package/script/src/utils/gif_decoder.d.ts.map +1 -0
  101. package/script/src/utils/gif_decoder.js +378 -0
  102. package/script/src/utils/gif_encoder.d.ts +29 -0
  103. package/script/src/utils/gif_encoder.d.ts.map +1 -0
  104. package/script/src/utils/gif_encoder.js +230 -0
  105. package/script/src/utils/jpeg_decoder.d.ts +39 -0
  106. package/script/src/utils/jpeg_decoder.d.ts.map +1 -0
  107. package/script/src/utils/jpeg_decoder.js +584 -0
  108. package/script/src/utils/jpeg_encoder.d.ts +33 -0
  109. package/script/src/utils/jpeg_encoder.d.ts.map +1 -0
  110. package/script/src/utils/jpeg_encoder.js +1021 -0
  111. package/script/src/utils/lzw.d.ts +43 -0
  112. package/script/src/utils/lzw.d.ts.map +1 -0
  113. package/script/src/utils/lzw.js +314 -0
  114. package/script/src/utils/resize.d.ts +9 -0
  115. package/script/src/utils/resize.d.ts.map +1 -0
  116. package/script/src/utils/resize.js +56 -0
  117. package/script/src/utils/tiff_lzw.d.ts +44 -0
  118. package/script/src/utils/tiff_lzw.d.ts.map +1 -0
  119. package/script/src/utils/tiff_lzw.js +311 -0
  120. package/script/src/utils/webp_decoder.d.ts +39 -0
  121. package/script/src/utils/webp_decoder.d.ts.map +1 -0
  122. package/script/src/utils/webp_decoder.js +497 -0
  123. package/script/src/utils/webp_encoder.d.ts +72 -0
  124. package/script/src/utils/webp_encoder.d.ts.map +1 -0
  125. package/script/src/utils/webp_encoder.js +631 -0
@@ -0,0 +1,306 @@
1
+ /**
2
+ * LZW compression and decompression for TIFF images
3
+ *
4
+ * TIFF LZW differs from GIF LZW in several ways:
5
+ * - Uses MSB-first bit ordering (big-endian bits)
6
+ * - Typically starts with 9-bit codes
7
+ * - Uses code 256 as clear code, code 257 as end-of-information (EOI)
8
+ * - Variable code size from 9 to 12 bits
9
+ */
10
+ /**
11
+ * LZW Decompressor for TIFF images
12
+ */
13
+ export class TIFFLZWDecoder {
14
+ constructor(data) {
15
+ Object.defineProperty(this, "data", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "pos", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "bitPos", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "codeSize", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "dict", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "clearCode", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: 256
50
+ });
51
+ Object.defineProperty(this, "eoiCode", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: 257
56
+ });
57
+ Object.defineProperty(this, "nextCode", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: 258
62
+ });
63
+ this.data = data;
64
+ this.pos = 0;
65
+ this.bitPos = 0;
66
+ this.codeSize = 9;
67
+ this.dict = [];
68
+ this.initDictionary();
69
+ }
70
+ initDictionary() {
71
+ this.dict = [];
72
+ // Initialize dictionary with single-byte entries (0-255)
73
+ for (let i = 0; i < 256; i++) {
74
+ this.dict[i] = new Uint8Array([i]);
75
+ }
76
+ this.nextCode = 258; // Next available code after clear and EOI
77
+ this.codeSize = 9;
78
+ }
79
+ readCode() {
80
+ if (this.pos >= this.data.length) {
81
+ return null;
82
+ }
83
+ let code = 0;
84
+ // Read bits MSB-first (TIFF bit ordering)
85
+ for (let i = 0; i < this.codeSize; i++) {
86
+ if (this.pos >= this.data.length) {
87
+ return null;
88
+ }
89
+ const byte = this.data[this.pos];
90
+ const bit = (byte >> (7 - this.bitPos)) & 1;
91
+ code = (code << 1) | bit;
92
+ this.bitPos++;
93
+ if (this.bitPos === 8) {
94
+ this.bitPos = 0;
95
+ this.pos++;
96
+ }
97
+ }
98
+ return code;
99
+ }
100
+ decompress() {
101
+ const output = [];
102
+ let prevCode = null;
103
+ while (true) {
104
+ const code = this.readCode();
105
+ if (code === null || code === this.eoiCode) {
106
+ break;
107
+ }
108
+ if (code === this.clearCode) {
109
+ this.initDictionary();
110
+ prevCode = null;
111
+ continue;
112
+ }
113
+ if (code < this.dict.length && this.dict[code]) {
114
+ const entry = this.dict[code];
115
+ output.push(...entry);
116
+ if (prevCode !== null && prevCode < this.dict.length &&
117
+ this.dict[prevCode]) {
118
+ const prevEntry = this.dict[prevCode];
119
+ const newEntry = new Uint8Array(prevEntry.length + 1);
120
+ newEntry.set(prevEntry);
121
+ newEntry[prevEntry.length] = entry[0];
122
+ if (this.nextCode < 4096) {
123
+ this.dict[this.nextCode] = newEntry;
124
+ this.nextCode++;
125
+ // Increase code size when dictionary reaches certain sizes
126
+ if (this.nextCode === 512 && this.codeSize === 9) {
127
+ this.codeSize = 10;
128
+ }
129
+ else if (this.nextCode === 1024 && this.codeSize === 10) {
130
+ this.codeSize = 11;
131
+ }
132
+ else if (this.nextCode === 2048 && this.codeSize === 11) {
133
+ this.codeSize = 12;
134
+ }
135
+ }
136
+ }
137
+ }
138
+ else if (prevCode !== null && prevCode < this.dict.length && this.dict[prevCode]) {
139
+ // Special case: code not in dictionary yet
140
+ const prevEntry = this.dict[prevCode];
141
+ const newEntry = new Uint8Array(prevEntry.length + 1);
142
+ newEntry.set(prevEntry);
143
+ newEntry[prevEntry.length] = prevEntry[0];
144
+ if (this.nextCode < 4096) {
145
+ this.dict[this.nextCode] = newEntry;
146
+ this.nextCode++;
147
+ // Increase code size when dictionary reaches certain sizes
148
+ if (this.nextCode === 512 && this.codeSize === 9) {
149
+ this.codeSize = 10;
150
+ }
151
+ else if (this.nextCode === 1024 && this.codeSize === 10) {
152
+ this.codeSize = 11;
153
+ }
154
+ else if (this.nextCode === 2048 && this.codeSize === 11) {
155
+ this.codeSize = 12;
156
+ }
157
+ }
158
+ output.push(...newEntry);
159
+ }
160
+ prevCode = code;
161
+ }
162
+ return new Uint8Array(output);
163
+ }
164
+ }
165
+ /**
166
+ * LZW Encoder for TIFF images
167
+ */
168
+ export class TIFFLZWEncoder {
169
+ constructor() {
170
+ Object.defineProperty(this, "codeSize", {
171
+ enumerable: true,
172
+ configurable: true,
173
+ writable: true,
174
+ value: void 0
175
+ });
176
+ Object.defineProperty(this, "dict", {
177
+ enumerable: true,
178
+ configurable: true,
179
+ writable: true,
180
+ value: void 0
181
+ });
182
+ Object.defineProperty(this, "output", {
183
+ enumerable: true,
184
+ configurable: true,
185
+ writable: true,
186
+ value: void 0
187
+ });
188
+ Object.defineProperty(this, "bitBuffer", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: void 0
193
+ });
194
+ Object.defineProperty(this, "bitCount", {
195
+ enumerable: true,
196
+ configurable: true,
197
+ writable: true,
198
+ value: void 0
199
+ });
200
+ Object.defineProperty(this, "clearCode", {
201
+ enumerable: true,
202
+ configurable: true,
203
+ writable: true,
204
+ value: 256
205
+ });
206
+ Object.defineProperty(this, "eoiCode", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: 257
211
+ });
212
+ Object.defineProperty(this, "nextCode", {
213
+ enumerable: true,
214
+ configurable: true,
215
+ writable: true,
216
+ value: 258
217
+ });
218
+ this.codeSize = 9;
219
+ this.dict = new Map();
220
+ this.output = [];
221
+ this.bitBuffer = 0;
222
+ this.bitCount = 0;
223
+ this.initDictionary();
224
+ }
225
+ initDictionary() {
226
+ this.dict.clear();
227
+ // Initialize dictionary with single-byte entries
228
+ for (let i = 0; i < 256; i++) {
229
+ this.dict.set(String.fromCharCode(i), i);
230
+ }
231
+ this.nextCode = 258;
232
+ this.codeSize = 9;
233
+ }
234
+ writeCode(code) {
235
+ // Write bits MSB-first (TIFF bit ordering)
236
+ for (let i = this.codeSize - 1; i >= 0; i--) {
237
+ const bit = (code >> i) & 1;
238
+ this.bitBuffer = (this.bitBuffer << 1) | bit;
239
+ this.bitCount++;
240
+ if (this.bitCount === 8) {
241
+ this.output.push(this.bitBuffer & 0xff);
242
+ this.bitBuffer = 0;
243
+ this.bitCount = 0;
244
+ }
245
+ }
246
+ }
247
+ compress(data) {
248
+ this.initDictionary();
249
+ this.output = [];
250
+ this.bitBuffer = 0;
251
+ this.bitCount = 0;
252
+ // Write clear code
253
+ this.writeCode(this.clearCode);
254
+ let buffer = "";
255
+ for (let i = 0; i < data.length; i++) {
256
+ const k = String.fromCharCode(data[i]);
257
+ const bufferK = buffer + k;
258
+ if (this.dict.has(bufferK)) {
259
+ buffer = bufferK;
260
+ }
261
+ else {
262
+ // Output code for buffer
263
+ const code = this.dict.get(buffer);
264
+ if (code !== undefined) {
265
+ this.writeCode(code);
266
+ }
267
+ // Add new entry to dictionary
268
+ if (this.nextCode < 4096) {
269
+ this.dict.set(bufferK, this.nextCode);
270
+ this.nextCode++;
271
+ // Increase code size when needed
272
+ if (this.nextCode === 512 && this.codeSize === 9) {
273
+ this.codeSize = 10;
274
+ }
275
+ else if (this.nextCode === 1024 && this.codeSize === 10) {
276
+ this.codeSize = 11;
277
+ }
278
+ else if (this.nextCode === 2048 && this.codeSize === 11) {
279
+ this.codeSize = 12;
280
+ }
281
+ // Reset when dictionary is full
282
+ if (this.nextCode === 4096) {
283
+ this.writeCode(this.clearCode);
284
+ this.initDictionary();
285
+ }
286
+ }
287
+ buffer = k;
288
+ }
289
+ }
290
+ // Output final code
291
+ if (buffer.length > 0) {
292
+ const code = this.dict.get(buffer);
293
+ if (code !== undefined) {
294
+ this.writeCode(code);
295
+ }
296
+ }
297
+ // Write end code
298
+ this.writeCode(this.eoiCode);
299
+ // Flush remaining bits
300
+ if (this.bitCount > 0) {
301
+ this.bitBuffer <<= 8 - this.bitCount;
302
+ this.output.push(this.bitBuffer & 0xff);
303
+ }
304
+ return new Uint8Array(this.output);
305
+ }
306
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * WebP VP8L (Lossless) decoder implementation
3
+ *
4
+ * This module implements a pure JavaScript decoder for WebP lossless (VP8L) format.
5
+ * It supports:
6
+ * - Huffman coding (canonical Huffman codes)
7
+ * - LZ77 backward references for compression
8
+ * - Color cache for repeated colors
9
+ * - Simple and complex Huffman code tables
10
+ *
11
+ * Current limitations:
12
+ * - Does not support transforms (predictor, color, subtract green, color indexing)
13
+ * - Does not support meta Huffman codes
14
+ * - Does not support lossy WebP (VP8) format
15
+ *
16
+ * For images with transforms or lossy compression, the decoder will fall back
17
+ * to the runtime's ImageDecoder API if available.
18
+ *
19
+ * @see https://developers.google.com/speed/webp/docs/riff_container
20
+ * @see https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
21
+ */
22
+ export declare class WebPDecoder {
23
+ private data;
24
+ constructor(data: Uint8Array);
25
+ decode(): {
26
+ width: number;
27
+ height: number;
28
+ data: Uint8Array;
29
+ };
30
+ private decodeVP8L;
31
+ private decodeImageData;
32
+ private readHuffmanCodes;
33
+ private readHuffmanCode;
34
+ private readCodeLengths;
35
+ private buildHuffmanTable;
36
+ private getLength;
37
+ private getDistance;
38
+ }
39
+ //# sourceMappingURL=webp_decoder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webp_decoder.d.ts","sourceRoot":"","sources":["../../../src/src/utils/webp_decoder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAsHH,qBAAa,WAAW;IACtB,OAAO,CAAC,IAAI,CAAa;gBAEb,IAAI,EAAE,UAAU;IAI5B,MAAM,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,UAAU,CAAA;KAAE;IAgE7D,OAAO,CAAC,UAAU;IA+BlB,OAAO,CAAC,eAAe;IAqHvB,OAAO,CAAC,gBAAgB;IAqDxB,OAAO,CAAC,eAAe;IA0CvB,OAAO,CAAC,eAAe;IAkEvB,OAAO,CAAC,iBAAiB;IA6BzB,OAAO,CAAC,SAAS;IAUjB,OAAO,CAAC,WAAW;CASpB"}