@agent-infra/media-utils 0.1.5 → 0.2.0-alpha.3
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 +201 -0
- package/README.md +15 -3
- package/dist/{base64/index.d.ts → image/base64.d.ts} +1 -1
- package/dist/image/base64.d.ts.map +1 -0
- package/dist/{base64/index.js → image/base64.js} +2 -2
- package/dist/image/base64.js.map +1 -0
- package/dist/{base64/index.mjs → image/base64.mjs} +3 -3
- package/dist/image/base64.mjs.map +1 -0
- package/dist/image/binary.d.ts +14 -0
- package/dist/image/binary.d.ts.map +1 -0
- package/dist/image/binary.js +106 -0
- package/dist/image/binary.js.map +1 -0
- package/dist/image/binary.mjs +72 -0
- package/dist/image/binary.mjs.map +1 -0
- package/dist/{base64 → image}/image-parse.d.ts +2 -1
- package/dist/image/image-parse.d.ts.map +1 -0
- package/dist/{base64 → image}/image-parse.js +53 -5
- package/dist/image/image-parse.js.map +1 -0
- package/dist/{base64 → image}/image-parse.mjs +48 -3
- package/dist/image/image-parse.mjs.map +1 -0
- package/dist/image/index.d.ts +3 -0
- package/dist/image/index.d.ts.map +1 -0
- package/dist/image/index.js +75 -0
- package/dist/image/index.js.map +1 -0
- package/dist/image/index.mjs +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.mjs +1 -1
- package/package.json +4 -4
- package/dist/base64/image-parse.d.ts.map +0 -1
- package/dist/base64/image-parse.js.map +0 -1
- package/dist/base64/image-parse.mjs.map +0 -1
- package/dist/base64/index.d.ts.map +0 -1
- package/dist/base64/index.js.map +0 -1
- package/dist/base64/index.mjs.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -31,7 +31,12 @@ Supports both Node.js and browsers.
|
|
|
31
31
|
import { Base64ImageParser } from '@agent-infra/media-utils';
|
|
32
32
|
|
|
33
33
|
// Initialize with a base64 image string
|
|
34
|
-
|
|
34
|
+
// You only need to ensure that the input base64 string is image data; the data URI prefix is not required.
|
|
35
|
+
const tool = new Base64ImageParser(
|
|
36
|
+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...',
|
|
37
|
+
);
|
|
38
|
+
// or
|
|
39
|
+
const tool = new Base64ImageParser('iVBORw0KGgoAAAANSUhEUgA...');
|
|
35
40
|
|
|
36
41
|
// Get image type
|
|
37
42
|
const imageType = tool.getImageType(); // 'png' | 'jpeg' | 'webp' | 'gif' | 'bmp' | null
|
|
@@ -54,7 +59,8 @@ const dataUri = tool.getDataUri(); // 'data:image/png;base64,...'
|
|
|
54
59
|
```typescript
|
|
55
60
|
import { Base64ImageParser } from '@agent-infra/media-utils';
|
|
56
61
|
|
|
57
|
-
const base64Image =
|
|
62
|
+
const base64Image =
|
|
63
|
+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';
|
|
58
64
|
|
|
59
65
|
const tool = new Base64ImageParser(base64Image);
|
|
60
66
|
|
|
@@ -65,4 +71,10 @@ console.log('Pure base64:', tool.getPureBase64Image()); // 'iVBORw0KGgo...'
|
|
|
65
71
|
|
|
66
72
|
## License
|
|
67
73
|
|
|
68
|
-
|
|
74
|
+
Apache License 2.0.
|
|
75
|
+
|
|
76
|
+
## Credits
|
|
77
|
+
|
|
78
|
+
Special thanks to the open source projects that inspired this toolkit:
|
|
79
|
+
|
|
80
|
+
- [puppeteer](https://github.com/puppeteer/puppeteer) - The underlying browser automation library
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/image/base64.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAE1D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAC,CAAa;IAE5B,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,UAAU,CAAgC;gBAEtC,WAAW,EAAE,MAAM;IAIxB,kBAAkB,IAAI,MAAM;IAI5B,SAAS,IAAI,UAAU;IAM9B;;OAEG;IACI,YAAY,IAAI,SAAS,GAAG,IAAI;IAiBhC,aAAa,IAAI,eAAe,GAAG,IAAI;IAqDvC,UAAU;IASjB;;;OAGG;IACH,OAAO,CAAC,eAAe;CAcxB"}
|
|
@@ -52,7 +52,7 @@ class Base64ImageParser {
|
|
|
52
52
|
getImageType() {
|
|
53
53
|
if (this.imageType) return this.imageType;
|
|
54
54
|
const prefix = this.pureBase64.substring(0, 8);
|
|
55
|
-
for (const [signature, type] of external_image_parse_js_namespaceObject.
|
|
55
|
+
for (const [signature, type] of external_image_parse_js_namespaceObject.IMAGE_MAGIC_STRINGS)if (prefix.startsWith(signature)) {
|
|
56
56
|
this.imageType = type;
|
|
57
57
|
break;
|
|
58
58
|
}
|
|
@@ -134,4 +134,4 @@ Object.defineProperty(exports, '__esModule', {
|
|
|
134
134
|
value: true
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
-
//# sourceMappingURL=
|
|
137
|
+
//# sourceMappingURL=base64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/base64.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object","webpack://@agent-infra/media-utils/./src/image/base64.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n base64String2Uint8Array,\n IMAGE_MAGIC_STRINGS,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\nexport class Base64ImageParser {\n private pureBase64: string;\n private buffer?: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(base64Image: string) {\n this.pureBase64 = base64Image.replace(/^data:image\\/\\w+;base64,/, '');\n }\n\n public getPureBase64Image(): string {\n return this.pureBase64;\n }\n\n public getBuffer(): Uint8Array {\n this.buffer = base64String2Uint8Array(this.pureBase64);\n\n return this.buffer;\n }\n\n /**\n * get image type form base64 magic number\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n const prefix = this.pureBase64.substring(0, 8);\n\n for (const [signature, type] of IMAGE_MAGIC_STRINGS) {\n if (prefix.startsWith(signature)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n const bytes = this.getHeaderBuffer(32); // 16-23\n this.dimensions = parsePngDimensions(bytes);\n break;\n }\n case 'jpeg': {\n try {\n const headerBytes = this.getHeaderBuffer(1024); // SOF marker\n this.dimensions = parseJpegDimensions(headerBytes);\n } catch (e) {\n const fullBuffer = this.getBuffer();\n this.dimensions = parseJpegDimensions(fullBuffer);\n }\n break;\n }\n case 'webp': {\n const bytes = this.getHeaderBuffer(32); // 23 - 29\n this.dimensions = parseWebpDimensions(bytes);\n break;\n }\n case 'gif': {\n const bytes = this.getHeaderBuffer(24); // 6 - 9\n this.dimensions = parseGifDimensions(bytes);\n break;\n }\n case 'bmp': {\n const bytes = this.getHeaderBuffer(40); // 18-25\n this.dimensions = parseBmpDimensions(bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n\n public getDataUri() {\n const imageType = this.getImageType();\n if (imageType) {\n return `data:image/${this.imageType};base64,${this.pureBase64}`;\n }\n\n return null;\n }\n\n /**\n * Get only the header bytes needed for dimension parsing\n * This is much more memory efficient than converting the entire image\n */\n private getHeaderBuffer(maxBytes: number): Uint8Array {\n if (this.buffer) {\n return this.buffer;\n }\n\n // Calculate how much of the base64 we need to decode\n // Base64 encoding: 4 characters represent 3 bytes\n const base64CharsNeeded = Math.ceil((maxBytes * 4) / 3);\n // Round up to nearest multiple of 4 to avoid padding issues\n const alignedChars = Math.ceil(base64CharsNeeded / 4) * 4;\n const headerBase64 = this.pureBase64.substring(0, alignedChars);\n\n return base64String2Uint8Array(headerBase64);\n }\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","Base64ImageParser","base64String2Uint8Array","prefix","signature","type","IMAGE_MAGIC_STRINGS","imageType","bytes","parsePngDimensions","headerBytes","parseJpegDimensions","e","fullBuffer","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","maxBytes","base64CharsNeeded","Math","alignedChars","headerBase64","base64Image"],"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,CAAC,QAAQ,EAAE,IAAI,CAAC,UAAU,EAAE;QAGjE,OAAO;IACT;IAMQ,gBAAgBY,QAAgB,EAAc;QACpD,IAAI,IAAI,CAAC,MAAM,EACb,OAAO,IAAI,CAAC,MAAM;QAKpB,MAAMC,oBAAoBC,KAAK,IAAI,CAAEF,AAAW,IAAXA,WAAgB;QAErD,MAAMG,eAAeD,AAAmC,IAAnCA,KAAK,IAAI,CAACD,oBAAoB;QACnD,MAAMG,eAAe,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,GAAGD;QAElD,OAAOpB,AAAAA,IAAAA,wCAAAA,uBAAAA,AAAAA,EAAwBqB;IACjC;IAjHA,YAAYC,WAAmB,CAAE;QANjC,uBAAQ,cAAR;QACA,uBAAQ,UAAR;QAEA,uBAAQ,aAA8B;QACtC,uBAAQ,cAAqC;QAG3C,IAAI,CAAC,UAAU,GAAGA,YAAY,OAAO,CAAC,4BAA4B;IACpE;AAgHF"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { IMAGE_MAGIC_STRINGS, base64String2Uint8Array, parseBmpDimensions, parseGifDimensions, parseJpegDimensions, parsePngDimensions, parseWebpDimensions } from "./image-parse.mjs";
|
|
6
6
|
function _define_property(obj, key, value) {
|
|
7
7
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
8
8
|
value: value,
|
|
@@ -24,7 +24,7 @@ class Base64ImageParser {
|
|
|
24
24
|
getImageType() {
|
|
25
25
|
if (this.imageType) return this.imageType;
|
|
26
26
|
const prefix = this.pureBase64.substring(0, 8);
|
|
27
|
-
for (const [signature, type] of
|
|
27
|
+
for (const [signature, type] of IMAGE_MAGIC_STRINGS)if (prefix.startsWith(signature)) {
|
|
28
28
|
this.imageType = type;
|
|
29
29
|
break;
|
|
30
30
|
}
|
|
@@ -100,4 +100,4 @@ class Base64ImageParser {
|
|
|
100
100
|
}
|
|
101
101
|
export { Base64ImageParser };
|
|
102
102
|
|
|
103
|
-
//# sourceMappingURL=
|
|
103
|
+
//# sourceMappingURL=base64.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/base64.mjs","sources":["webpack://@agent-infra/media-utils/./src/image/base64.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n base64String2Uint8Array,\n IMAGE_MAGIC_STRINGS,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\nexport class Base64ImageParser {\n private pureBase64: string;\n private buffer?: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(base64Image: string) {\n this.pureBase64 = base64Image.replace(/^data:image\\/\\w+;base64,/, '');\n }\n\n public getPureBase64Image(): string {\n return this.pureBase64;\n }\n\n public getBuffer(): Uint8Array {\n this.buffer = base64String2Uint8Array(this.pureBase64);\n\n return this.buffer;\n }\n\n /**\n * get image type form base64 magic number\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n const prefix = this.pureBase64.substring(0, 8);\n\n for (const [signature, type] of IMAGE_MAGIC_STRINGS) {\n if (prefix.startsWith(signature)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n const bytes = this.getHeaderBuffer(32); // 16-23\n this.dimensions = parsePngDimensions(bytes);\n break;\n }\n case 'jpeg': {\n try {\n const headerBytes = this.getHeaderBuffer(1024); // SOF marker\n this.dimensions = parseJpegDimensions(headerBytes);\n } catch (e) {\n const fullBuffer = this.getBuffer();\n this.dimensions = parseJpegDimensions(fullBuffer);\n }\n break;\n }\n case 'webp': {\n const bytes = this.getHeaderBuffer(32); // 23 - 29\n this.dimensions = parseWebpDimensions(bytes);\n break;\n }\n case 'gif': {\n const bytes = this.getHeaderBuffer(24); // 6 - 9\n this.dimensions = parseGifDimensions(bytes);\n break;\n }\n case 'bmp': {\n const bytes = this.getHeaderBuffer(40); // 18-25\n this.dimensions = parseBmpDimensions(bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n\n public getDataUri() {\n const imageType = this.getImageType();\n if (imageType) {\n return `data:image/${this.imageType};base64,${this.pureBase64}`;\n }\n\n return null;\n }\n\n /**\n * Get only the header bytes needed for dimension parsing\n * This is much more memory efficient than converting the entire image\n */\n private getHeaderBuffer(maxBytes: number): Uint8Array {\n if (this.buffer) {\n return this.buffer;\n }\n\n // Calculate how much of the base64 we need to decode\n // Base64 encoding: 4 characters represent 3 bytes\n const base64CharsNeeded = Math.ceil((maxBytes * 4) / 3);\n // Round up to nearest multiple of 4 to avoid padding issues\n const alignedChars = Math.ceil(base64CharsNeeded / 4) * 4;\n const headerBase64 = this.pureBase64.substring(0, alignedChars);\n\n return base64String2Uint8Array(headerBase64);\n }\n}\n"],"names":["Base64ImageParser","base64String2Uint8Array","prefix","signature","type","IMAGE_MAGIC_STRINGS","imageType","bytes","parsePngDimensions","headerBytes","parseJpegDimensions","e","fullBuffer","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","maxBytes","base64CharsNeeded","Math","alignedChars","headerBase64","base64Image"],"mappings":";;;;;AAGC;;;;;;;;;;AAaM,MAAMA;IAWJ,qBAA6B;QAClC,OAAO,IAAI,CAAC,UAAU;IACxB;IAEO,YAAwB;QAC7B,IAAI,CAAC,MAAM,GAAGC,wBAAwB,IAAI,CAAC,UAAU;QAErD,OAAO,IAAI,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AAC,UAAU;QACxB,EAAE,OAAOS,OAAO;YACdC,QAAQ,IAAI,CAAC,qCAAqCD;YAClD,OAAO;QACT;IACF;IAEO,aAAa;QAClB,MAAMV,YAAY,IAAI,CAAC,YAAY;QACnC,IAAIA,WACF,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,UAAU,EAAE;QAGjE,OAAO;IACT;IAMQ,gBAAgBY,QAAgB,EAAc;QACpD,IAAI,IAAI,CAAC,MAAM,EACb,OAAO,IAAI,CAAC,MAAM;QAKpB,MAAMC,oBAAoBC,KAAK,IAAI,CAAEF,AAAW,IAAXA,WAAgB;QAErD,MAAMG,eAAeD,AAAmC,IAAnCA,KAAK,IAAI,CAACD,oBAAoB;QACnD,MAAMG,eAAe,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,GAAGD;QAElD,OAAOpB,wBAAwBqB;IACjC;IAjHA,YAAYC,WAAmB,CAAE;QANjC,uBAAQ,cAAR;QACA,uBAAQ,UAAR;QAEA,uBAAQ,aAA8B;QACtC,uBAAQ,cAAqC;QAG3C,IAAI,CAAC,UAAU,GAAGA,YAAY,OAAO,CAAC,4BAA4B;IACpE;AAgHF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ImageDimensions, ImageType } from '../type';
|
|
2
|
+
export declare class BinaryImageParser {
|
|
3
|
+
private bytes;
|
|
4
|
+
private imageType;
|
|
5
|
+
private dimensions;
|
|
6
|
+
constructor(imageBytes: Uint8Array);
|
|
7
|
+
getBuffer(): Uint8Array;
|
|
8
|
+
/**
|
|
9
|
+
* Get image type from binary magic numbers
|
|
10
|
+
*/
|
|
11
|
+
getImageType(): ImageType | null;
|
|
12
|
+
getDimensions(): ImageDimensions | null;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../src/image/binary.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAmB1D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,KAAK,CAAa;IAE1B,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,UAAU,CAAgC;gBAEtC,UAAU,EAAE,UAAU;IAI3B,SAAS,IAAI,UAAU;IAI9B;;OAEG;IACI,YAAY,IAAI,SAAS,GAAG,IAAI;IAehC,aAAa,IAAI,eAAe,GAAG,IAAI;CA0C/C"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
var __webpack_require__ = {};
|
|
7
|
+
(()=>{
|
|
8
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
9
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: definition[key]
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
BinaryImageParser: ()=>BinaryImageParser
|
|
32
|
+
});
|
|
33
|
+
const external_image_parse_js_namespaceObject = require("./image-parse.js");
|
|
34
|
+
function _define_property(obj, key, value) {
|
|
35
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
36
|
+
value: value,
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true
|
|
40
|
+
});
|
|
41
|
+
else obj[key] = value;
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
function startsWith(bytes, pattern) {
|
|
45
|
+
if (bytes.length < pattern.length) return false;
|
|
46
|
+
for(let i = 0; i < pattern.length; i++)if (bytes[i] !== pattern[i]) return false;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
class BinaryImageParser {
|
|
50
|
+
getBuffer() {
|
|
51
|
+
return this.bytes;
|
|
52
|
+
}
|
|
53
|
+
getImageType() {
|
|
54
|
+
if (this.imageType) return this.imageType;
|
|
55
|
+
for (const [pattern, type] of external_image_parse_js_namespaceObject.IMAGE_MAGIC_NUMBERS)if (startsWith(this.bytes, pattern)) {
|
|
56
|
+
this.imageType = type;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return this.imageType;
|
|
60
|
+
}
|
|
61
|
+
getDimensions() {
|
|
62
|
+
if (this.dimensions) return this.dimensions;
|
|
63
|
+
const imageType = this.getImageType();
|
|
64
|
+
if (!imageType) return null;
|
|
65
|
+
try {
|
|
66
|
+
switch(imageType){
|
|
67
|
+
case 'png':
|
|
68
|
+
this.dimensions = (0, external_image_parse_js_namespaceObject.parsePngDimensions)(this.bytes);
|
|
69
|
+
break;
|
|
70
|
+
case 'jpeg':
|
|
71
|
+
this.dimensions = (0, external_image_parse_js_namespaceObject.parseJpegDimensions)(this.bytes);
|
|
72
|
+
break;
|
|
73
|
+
case 'webp':
|
|
74
|
+
this.dimensions = (0, external_image_parse_js_namespaceObject.parseWebpDimensions)(this.bytes);
|
|
75
|
+
break;
|
|
76
|
+
case 'gif':
|
|
77
|
+
this.dimensions = (0, external_image_parse_js_namespaceObject.parseGifDimensions)(this.bytes);
|
|
78
|
+
break;
|
|
79
|
+
case 'bmp':
|
|
80
|
+
this.dimensions = (0, external_image_parse_js_namespaceObject.parseBmpDimensions)(this.bytes);
|
|
81
|
+
break;
|
|
82
|
+
default:
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return this.dimensions;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.warn('Failed to parse image dimensions:', error);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
constructor(imageBytes){
|
|
92
|
+
_define_property(this, "bytes", void 0);
|
|
93
|
+
_define_property(this, "imageType", null);
|
|
94
|
+
_define_property(this, "dimensions", null);
|
|
95
|
+
this.bytes = imageBytes;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.BinaryImageParser = __webpack_exports__.BinaryImageParser;
|
|
99
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
100
|
+
"BinaryImageParser"
|
|
101
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
102
|
+
Object.defineProperty(exports, '__esModule', {
|
|
103
|
+
value: true
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/binary.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object","webpack://@agent-infra/media-utils/./src/image/binary.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n IMAGE_MAGIC_NUMBERS,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\n/**\n * Helper function to check if array starts with a specific pattern\n */\nfunction startsWith(bytes: Uint8Array, pattern: Uint8Array): boolean {\n if (bytes.length < pattern.length) {\n return false;\n }\n\n for (let i = 0; i < pattern.length; i++) {\n if (bytes[i] !== pattern[i]) {\n return false;\n }\n }\n\n return true;\n}\n\nexport class BinaryImageParser {\n private bytes: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(imageBytes: Uint8Array) {\n this.bytes = imageBytes;\n }\n\n public getBuffer(): Uint8Array {\n return this.bytes;\n }\n\n /**\n * Get image type from binary magic numbers\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n for (const [pattern, type] of IMAGE_MAGIC_NUMBERS) {\n if (startsWith(this.bytes, pattern)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n this.dimensions = parsePngDimensions(this.bytes);\n break;\n }\n case 'jpeg': {\n this.dimensions = parseJpegDimensions(this.bytes);\n break;\n }\n case 'webp': {\n this.dimensions = parseWebpDimensions(this.bytes);\n break;\n }\n case 'gif': {\n this.dimensions = parseGifDimensions(this.bytes);\n break;\n }\n case 'bmp': {\n this.dimensions = parseBmpDimensions(this.bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","startsWith","bytes","pattern","i","BinaryImageParser","type","IMAGE_MAGIC_NUMBERS","imageType","parsePngDimensions","parseJpegDimensions","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","imageBytes"],"mappings":";;;;;;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFL,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOM,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D;;;;;;;;ACHC;;;;;;;;;;AAeD,SAASI,WAAWC,KAAiB,EAAEC,OAAmB;IACxD,IAAID,MAAM,MAAM,GAAGC,QAAQ,MAAM,EAC/B,OAAO;IAGT,IAAK,IAAIC,IAAI,GAAGA,IAAID,QAAQ,MAAM,EAAEC,IAClC,IAAIF,KAAK,CAACE,EAAE,KAAKD,OAAO,CAACC,EAAE,EACzB,OAAO;IAIX,OAAO;AACT;AAEO,MAAMC;IAUJ,YAAwB;QAC7B,OAAO,IAAI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A8B;QACtC,uBAAQ,cAAqC;QAG3C,IAAI,CAAC,KAAK,GAAGA;IACf;AAkEF"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { IMAGE_MAGIC_NUMBERS, parseBmpDimensions, parseGifDimensions, parseJpegDimensions, parsePngDimensions, parseWebpDimensions } from "./image-parse.mjs";
|
|
6
|
+
function _define_property(obj, key, value) {
|
|
7
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
8
|
+
value: value,
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true
|
|
12
|
+
});
|
|
13
|
+
else obj[key] = value;
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
function startsWith(bytes, pattern) {
|
|
17
|
+
if (bytes.length < pattern.length) return false;
|
|
18
|
+
for(let i = 0; i < pattern.length; i++)if (bytes[i] !== pattern[i]) return false;
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
class BinaryImageParser {
|
|
22
|
+
getBuffer() {
|
|
23
|
+
return this.bytes;
|
|
24
|
+
}
|
|
25
|
+
getImageType() {
|
|
26
|
+
if (this.imageType) return this.imageType;
|
|
27
|
+
for (const [pattern, type] of IMAGE_MAGIC_NUMBERS)if (startsWith(this.bytes, pattern)) {
|
|
28
|
+
this.imageType = type;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
return this.imageType;
|
|
32
|
+
}
|
|
33
|
+
getDimensions() {
|
|
34
|
+
if (this.dimensions) return this.dimensions;
|
|
35
|
+
const imageType = this.getImageType();
|
|
36
|
+
if (!imageType) return null;
|
|
37
|
+
try {
|
|
38
|
+
switch(imageType){
|
|
39
|
+
case 'png':
|
|
40
|
+
this.dimensions = parsePngDimensions(this.bytes);
|
|
41
|
+
break;
|
|
42
|
+
case 'jpeg':
|
|
43
|
+
this.dimensions = parseJpegDimensions(this.bytes);
|
|
44
|
+
break;
|
|
45
|
+
case 'webp':
|
|
46
|
+
this.dimensions = parseWebpDimensions(this.bytes);
|
|
47
|
+
break;
|
|
48
|
+
case 'gif':
|
|
49
|
+
this.dimensions = parseGifDimensions(this.bytes);
|
|
50
|
+
break;
|
|
51
|
+
case 'bmp':
|
|
52
|
+
this.dimensions = parseBmpDimensions(this.bytes);
|
|
53
|
+
break;
|
|
54
|
+
default:
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return this.dimensions;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.warn('Failed to parse image dimensions:', error);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
constructor(imageBytes){
|
|
64
|
+
_define_property(this, "bytes", void 0);
|
|
65
|
+
_define_property(this, "imageType", null);
|
|
66
|
+
_define_property(this, "dimensions", null);
|
|
67
|
+
this.bytes = imageBytes;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export { BinaryImageParser };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=binary.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/binary.mjs","sources":["webpack://@agent-infra/media-utils/./src/image/binary.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n IMAGE_MAGIC_NUMBERS,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\n/**\n * Helper function to check if array starts with a specific pattern\n */\nfunction startsWith(bytes: Uint8Array, pattern: Uint8Array): boolean {\n if (bytes.length < pattern.length) {\n return false;\n }\n\n for (let i = 0; i < pattern.length; i++) {\n if (bytes[i] !== pattern[i]) {\n return false;\n }\n }\n\n return true;\n}\n\nexport class BinaryImageParser {\n private bytes: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(imageBytes: Uint8Array) {\n this.bytes = imageBytes;\n }\n\n public getBuffer(): Uint8Array {\n return this.bytes;\n }\n\n /**\n * Get image type from binary magic numbers\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n for (const [pattern, type] of IMAGE_MAGIC_NUMBERS) {\n if (startsWith(this.bytes, pattern)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n this.dimensions = parsePngDimensions(this.bytes);\n break;\n }\n case 'jpeg': {\n this.dimensions = parseJpegDimensions(this.bytes);\n break;\n }\n case 'webp': {\n this.dimensions = parseWebpDimensions(this.bytes);\n break;\n }\n case 'gif': {\n this.dimensions = parseGifDimensions(this.bytes);\n break;\n }\n case 'bmp': {\n this.dimensions = parseBmpDimensions(this.bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n}\n"],"names":["startsWith","bytes","pattern","i","BinaryImageParser","type","IMAGE_MAGIC_NUMBERS","imageType","parsePngDimensions","parseJpegDimensions","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","imageBytes"],"mappings":";;;;;AAGC;;;;;;;;;;AAeD,SAASA,WAAWC,KAAiB,EAAEC,OAAmB;IACxD,IAAID,MAAM,MAAM,GAAGC,QAAQ,MAAM,EAC/B,OAAO;IAGT,IAAK,IAAIC,IAAI,GAAGA,IAAID,QAAQ,MAAM,EAAEC,IAClC,IAAIF,KAAK,CAACE,EAAE,KAAKD,OAAO,CAACC,EAAE,EACzB,OAAO;IAIX,OAAO;AACT;AAEO,MAAMC;IAUJ,YAAw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GIC_STRINGS: Map<string, ImageType>;
|
|
3
|
+
export declare const IMAGE_MAGIC_NUMBERS: Map<Uint8Array<ArrayBufferLike>, ImageType>;
|
|
3
4
|
export declare function base64String2Uint8Array(base64: string): Uint8Array<ArrayBuffer>;
|
|
4
5
|
/**
|
|
5
6
|
* PNG: https://github.com/corkami/pics/blob/master/binary/PNG.png
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-parse.d.ts","sourceRoot":"","sources":["../../src/image/image-parse.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1D,eAAO,MAAM,mBAAmB,wBAM9B,CAAC;AAEH,eAAO,MAAM,mBAAmB,6CAM9B,CAAC;AAEH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,GACb,UAAU,CAAC,WAAW,CAAC,CAoBzB;AAED;;;;;;GAMG;AAEH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAOrE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CA0BtE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAKrE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAoEtE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAOrE"}
|
|
@@ -28,7 +28,8 @@ var __webpack_require__ = {};
|
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
|
|
31
|
+
IMAGE_MAGIC_NUMBERS: ()=>IMAGE_MAGIC_NUMBERS,
|
|
32
|
+
IMAGE_MAGIC_STRINGS: ()=>IMAGE_MAGIC_STRINGS,
|
|
32
33
|
base64String2Uint8Array: ()=>base64String2Uint8Array,
|
|
33
34
|
parseBmpDimensions: ()=>parseBmpDimensions,
|
|
34
35
|
parseGifDimensions: ()=>parseGifDimensions,
|
|
@@ -36,7 +37,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
36
37
|
parsePngDimensions: ()=>parsePngDimensions,
|
|
37
38
|
parseWebpDimensions: ()=>parseWebpDimensions
|
|
38
39
|
});
|
|
39
|
-
const
|
|
40
|
+
const IMAGE_MAGIC_STRINGS = new Map([
|
|
40
41
|
[
|
|
41
42
|
'/9j/',
|
|
42
43
|
'jpeg'
|
|
@@ -58,9 +59,54 @@ const IMAGE_TYPE_MAP = new Map([
|
|
|
58
59
|
'bmp'
|
|
59
60
|
]
|
|
60
61
|
]);
|
|
62
|
+
const IMAGE_MAGIC_NUMBERS = new Map([
|
|
63
|
+
[
|
|
64
|
+
new Uint8Array([
|
|
65
|
+
0xff,
|
|
66
|
+
0xd8,
|
|
67
|
+
0xff
|
|
68
|
+
]),
|
|
69
|
+
'jpeg'
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
new Uint8Array([
|
|
73
|
+
0x89,
|
|
74
|
+
0x50,
|
|
75
|
+
0x4e,
|
|
76
|
+
0x47
|
|
77
|
+
]),
|
|
78
|
+
'png'
|
|
79
|
+
],
|
|
80
|
+
[
|
|
81
|
+
new Uint8Array([
|
|
82
|
+
0x52,
|
|
83
|
+
0x49,
|
|
84
|
+
0x46,
|
|
85
|
+
0x46
|
|
86
|
+
]),
|
|
87
|
+
'webp'
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
new Uint8Array([
|
|
91
|
+
0x47,
|
|
92
|
+
0x49,
|
|
93
|
+
0x46,
|
|
94
|
+
0x38
|
|
95
|
+
]),
|
|
96
|
+
'gif'
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
new Uint8Array([
|
|
100
|
+
0x42,
|
|
101
|
+
0x4d
|
|
102
|
+
]),
|
|
103
|
+
'bmp'
|
|
104
|
+
]
|
|
105
|
+
]);
|
|
61
106
|
function base64String2Uint8Array(base64) {
|
|
62
|
-
if ('
|
|
107
|
+
if ('function' == typeof Uint8Array.fromBase64) return Uint8Array.fromBase64(base64);
|
|
63
108
|
{
|
|
109
|
+
if ('undefined' != typeof Buffer) return new Uint8Array(Buffer.from(base64, 'base64'));
|
|
64
110
|
const binaryString = atob(base64);
|
|
65
111
|
const bytes = new Uint8Array(binaryString.length);
|
|
66
112
|
for(let i = 0; i < binaryString.length; i++)bytes[i] = binaryString.charCodeAt(i);
|
|
@@ -144,7 +190,8 @@ function parseBmpDimensions(bytes) {
|
|
|
144
190
|
height
|
|
145
191
|
};
|
|
146
192
|
}
|
|
147
|
-
exports.
|
|
193
|
+
exports.IMAGE_MAGIC_NUMBERS = __webpack_exports__.IMAGE_MAGIC_NUMBERS;
|
|
194
|
+
exports.IMAGE_MAGIC_STRINGS = __webpack_exports__.IMAGE_MAGIC_STRINGS;
|
|
148
195
|
exports.base64String2Uint8Array = __webpack_exports__.base64String2Uint8Array;
|
|
149
196
|
exports.parseBmpDimensions = __webpack_exports__.parseBmpDimensions;
|
|
150
197
|
exports.parseGifDimensions = __webpack_exports__.parseGifDimensions;
|
|
@@ -152,7 +199,8 @@ exports.parseJpegDimensions = __webpack_exports__.parseJpegDimensions;
|
|
|
152
199
|
exports.parsePngDimensions = __webpack_exports__.parsePngDimensions;
|
|
153
200
|
exports.parseWebpDimensions = __webpack_exports__.parseWebpDimensions;
|
|
154
201
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
155
|
-
"
|
|
202
|
+
"IMAGE_MAGIC_NUMBERS",
|
|
203
|
+
"IMAGE_MAGIC_STRINGS",
|
|
156
204
|
"base64String2Uint8Array",
|
|
157
205
|
"parseBmpDimensions",
|
|
158
206
|
"parseGifDimensions",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/image-parse.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object","webpack://@agent-infra/media-utils/./src/image/image-parse.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport type { ImageType, ImageDimensions } from '../type';\n\nexport const IMAGE_MAGIC_STRINGS = new Map<string, ImageType>([\n ['/9j/', 'jpeg'], // JPEG: FF D8 FF\n ['iVBORw', 'png'], // PNG: 89 50 4E 47\n ['UklGR', 'webp'], // WebP: 52 49 46 46\n ['R0lGOD', 'gif'], // GIF: 47 49 46 38\n ['Qk', 'bmp'], // BMP: 42 4D\n]);\n\nexport const IMAGE_MAGIC_NUMBERS = new Map<Uint8Array, ImageType>([\n [new Uint8Array([0xff, 0xd8, 0xff]), 'jpeg'], // JPEG: FF D8 FF\n [new Uint8Array([0x89, 0x50, 0x4e, 0x47]), 'png'], // PNG: 89 50 4E 47\n [new Uint8Array([0x52, 0x49, 0x46, 0x46]), 'webp'], // WebP: 52 49 46 46 (RIFF)\n [new Uint8Array([0x47, 0x49, 0x46, 0x38]), 'gif'], // GIF: 47 49 46 38\n [new Uint8Array([0x42, 0x4d]), 'bmp'], // BMP: 42 4D\n]);\n\nexport function base64String2Uint8Array(\n base64: string,\n): Uint8Array<ArrayBuffer> {\n // @ts-ignore\n if (typeof Uint8Array.fromBase64 === 'function') {\n // New Uint8Array API\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64\n // @ts-ignore\n return Uint8Array.fromBase64(base64);\n } else if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return new Uint8Array(Buffer.from(base64, 'base64'));\n } else {\n // Browser environment (legacy browsers)\n const binaryString = atob(base64);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n\n return bytes;\n }\n}\n\n/**\n * PNG: https://github.com/corkami/pics/blob/master/binary/PNG.png\n * JPEG: https://github.com/corkami/pics/blob/master/binary/JPG.png\n * WEBP: https://datatracker.ietf.org/doc/rfc9649/\n * GIF: https://github.com/corkami/pics/blob/master/binary/GIF.png\n * BMP: https://github.com/corkami/pics/blob/master/binary/bmp3.png\n */\n\nexport function parsePngDimensions(bytes: Uint8Array): ImageDimensions {\n // PNG dimensions are at bytes 16-23 (big-endian)\n const width =\n (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19];\n const height =\n (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23];\n return { width, height };\n}\n\nexport function parseJpegDimensions(bytes: Uint8Array): ImageDimensions {\n // JPEG requires parsing through segments to find SOF marker\n let i = 2; // Skip FF D8\n while (i < bytes.length - 1) {\n if (bytes[i] === 0xff) {\n const marker = bytes[i + 1];\n // SOF0, SOF1, SOF2 markers contain dimensions\n if (marker >= 0xc0 && marker <= 0xc3) {\n if (i + 8 < bytes.length) {\n const height = (bytes[i + 5] << 8) | bytes[i + 6];\n const width = (bytes[i + 7] << 8) | bytes[i + 8];\n return { width, height };\n }\n }\n // Skip to next segment\n if (i + 3 < bytes.length) {\n const segmentLength = (bytes[i + 2] << 8) | bytes[i + 3];\n i += 2 + segmentLength;\n } else {\n break;\n }\n } else {\n i++;\n }\n }\n throw new Error('Unable to find JPEG dimensions');\n}\n\nexport function parseGifDimensions(bytes: Uint8Array): ImageDimensions {\n // GIF dimensions are at bytes 6-9 (little-endian)\n const width = bytes[6] | (bytes[7] << 8);\n const height = bytes[8] | (bytes[9] << 8);\n return { width, height };\n}\n\nexport function parseWebpDimensions(bytes: Uint8Array): ImageDimensions {\n /**\n * WebP format varies, check for VP8/VP8L/VP8X\n *\n * - VP8 : Simple File Format (Lossy)\n * - VP8L: Simple File Format (Lossless)\n * - VP8X: Extended File Format\n */\n const fourCC = String.fromCharCode(\n bytes[12],\n bytes[13],\n bytes[14],\n bytes[15],\n );\n\n if (fourCC === 'VP8 ') {\n // VP8 format - dimensions are in the frame header\n // Skip chunk size (4 bytes) and frame tag (3 bytes) + key frame info (1 byte)\n const startByte = 20 + 3; // Start after VP8 chunk header + frame tag\n\n // Read width and height from VP8 frame header\n const width = bytes[startByte + 3] | (bytes[startByte + 4] << 8);\n const height = bytes[startByte + 5] | (bytes[startByte + 6] << 8);\n\n return {\n width: width & 0x3fff,\n height: height & 0x3fff,\n };\n } else if (fourCC === 'VP8L') {\n // VP8L format - dimensions are right after the signature\n // Skip chunk size (4 bytes) and VP8L signature (1 byte)\n const startByte = 20 + 1;\n\n // Read 4 bytes containing width and height info\n const bits =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16) |\n (bytes[startByte + 3] << 24);\n\n const width = (bits & 0x3fff) + 1;\n const height = ((bits >> 14) & 0x3fff) + 1;\n\n return { width, height };\n } else if (fourCC === 'VP8X') {\n // VP8X format - extended format with dimensions in header\n // Skip chunk size (4 bytes) and flags (4 bytes)\n const startByte = 20 + 4;\n\n // Width is stored in 3 bytes (little endian) + 1\n const width =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16);\n\n // Height is stored in next 3 bytes (little endian) + 1\n const height =\n bytes[startByte + 3] |\n (bytes[startByte + 4] << 8) |\n (bytes[startByte + 5] << 16);\n\n return {\n width: width + 1,\n height: height + 1,\n };\n }\n\n throw new Error('Unsupported WebP format');\n}\n\nexport function parseBmpDimensions(bytes: Uint8Array): ImageDimensions {\n // BMP dimensions are at bytes 18-25 (little-endian)\n const width =\n bytes[18] | (bytes[19] << 8) | (bytes[20] << 16) | (bytes[21] << 24);\n const height =\n bytes[22] | (bytes[23] << 8) | (bytes[24] << 16) | (bytes[25] << 24);\n return { width, height };\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","IMAGE_MAGIC_STRINGS","Map","IMAGE_MAGIC_NUMBERS","Uint8Array","base64String2Uint8Array","base64","Buffer","binaryString","atob","bytes","i","parsePngDimensions","width","height","parseJpegDimensions","marker","segmentLength","Error","parseGifDimensions","parseWebpDimensions","fourCC","String","startByte","bits","parseBmpDimensions"],"mappings":";;;;;;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFL,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOM,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D;;;;;;;;;;;;;;ACAO,MAAMI,sBAAsB,IAAIC,IAAuB;IAC5D;QAAC;QAAQ;KAAO;IAChB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAS;KAAO;IACjB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAM;KAAM;CACd;AAEM,MAAMC,sBAAsB,IAAID,IAA2B;IAChE;Q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jpeg'
|
|
@@ -24,9 +24,54 @@ const IMAGE_TYPE_MAP = new Map([
|
|
|
24
24
|
'bmp'
|
|
25
25
|
]
|
|
26
26
|
]);
|
|
27
|
+
const IMAGE_MAGIC_NUMBERS = new Map([
|
|
28
|
+
[
|
|
29
|
+
new Uint8Array([
|
|
30
|
+
0xff,
|
|
31
|
+
0xd8,
|
|
32
|
+
0xff
|
|
33
|
+
]),
|
|
34
|
+
'jpeg'
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
new Uint8Array([
|
|
38
|
+
0x89,
|
|
39
|
+
0x50,
|
|
40
|
+
0x4e,
|
|
41
|
+
0x47
|
|
42
|
+
]),
|
|
43
|
+
'png'
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
new Uint8Array([
|
|
47
|
+
0x52,
|
|
48
|
+
0x49,
|
|
49
|
+
0x46,
|
|
50
|
+
0x46
|
|
51
|
+
]),
|
|
52
|
+
'webp'
|
|
53
|
+
],
|
|
54
|
+
[
|
|
55
|
+
new Uint8Array([
|
|
56
|
+
0x47,
|
|
57
|
+
0x49,
|
|
58
|
+
0x46,
|
|
59
|
+
0x38
|
|
60
|
+
]),
|
|
61
|
+
'gif'
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
new Uint8Array([
|
|
65
|
+
0x42,
|
|
66
|
+
0x4d
|
|
67
|
+
]),
|
|
68
|
+
'bmp'
|
|
69
|
+
]
|
|
70
|
+
]);
|
|
27
71
|
function base64String2Uint8Array(base64) {
|
|
28
|
-
if ('
|
|
72
|
+
if ('function' == typeof Uint8Array.fromBase64) return Uint8Array.fromBase64(base64);
|
|
29
73
|
{
|
|
74
|
+
if ('undefined' != typeof Buffer) return new Uint8Array(Buffer.from(base64, 'base64'));
|
|
30
75
|
const binaryString = atob(base64);
|
|
31
76
|
const bytes = new Uint8Array(binaryString.length);
|
|
32
77
|
for(let i = 0; i < binaryString.length; i++)bytes[i] = binaryString.charCodeAt(i);
|
|
@@ -110,6 +155,6 @@ function parseBmpDimensions(bytes) {
|
|
|
110
155
|
height
|
|
111
156
|
};
|
|
112
157
|
}
|
|
113
|
-
export {
|
|
158
|
+
export { IMAGE_MAGIC_NUMBERS, IMAGE_MAGIC_STRINGS, base64String2Uint8Array, parseBmpDimensions, parseGifDimensions, parseJpegDimensions, parsePngDimensions, parseWebpDimensions };
|
|
114
159
|
|
|
115
160
|
//# sourceMappingURL=image-parse.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/image-parse.mjs","sources":["webpack://@agent-infra/media-utils/./src/image/image-parse.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport type { ImageType, ImageDimensions } from '../type';\n\nexport const IMAGE_MAGIC_STRINGS = new Map<string, ImageType>([\n ['/9j/', 'jpeg'], // JPEG: FF D8 FF\n ['iVBORw', 'png'], // PNG: 89 50 4E 47\n ['UklGR', 'webp'], // WebP: 52 49 46 46\n ['R0lGOD', 'gif'], // GIF: 47 49 46 38\n ['Qk', 'bmp'], // BMP: 42 4D\n]);\n\nexport const IMAGE_MAGIC_NUMBERS = new Map<Uint8Array, ImageType>([\n [new Uint8Array([0xff, 0xd8, 0xff]), 'jpeg'], // JPEG: FF D8 FF\n [new Uint8Array([0x89, 0x50, 0x4e, 0x47]), 'png'], // PNG: 89 50 4E 47\n [new Uint8Array([0x52, 0x49, 0x46, 0x46]), 'webp'], // WebP: 52 49 46 46 (RIFF)\n [new Uint8Array([0x47, 0x49, 0x46, 0x38]), 'gif'], // GIF: 47 49 46 38\n [new Uint8Array([0x42, 0x4d]), 'bmp'], // BMP: 42 4D\n]);\n\nexport function base64String2Uint8Array(\n base64: string,\n): Uint8Array<ArrayBuffer> {\n // @ts-ignore\n if (typeof Uint8Array.fromBase64 === 'function') {\n // New Uint8Array API\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64\n // @ts-ignore\n return Uint8Array.fromBase64(base64);\n } else if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return new Uint8Array(Buffer.from(base64, 'base64'));\n } else {\n // Browser environment (legacy browsers)\n const binaryString = atob(base64);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n\n return bytes;\n }\n}\n\n/**\n * PNG: https://github.com/corkami/pics/blob/master/binary/PNG.png\n * JPEG: https://github.com/corkami/pics/blob/master/binary/JPG.png\n * WEBP: https://datatracker.ietf.org/doc/rfc9649/\n * GIF: https://github.com/corkami/pics/blob/master/binary/GIF.png\n * BMP: https://github.com/corkami/pics/blob/master/binary/bmp3.png\n */\n\nexport function parsePngDimensions(bytes: Uint8Array): ImageDimensions {\n // PNG dimensions are at bytes 16-23 (big-endian)\n const width =\n (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19];\n const height =\n (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23];\n return { width, height };\n}\n\nexport function parseJpegDimensions(bytes: Uint8Array): ImageDimensions {\n // JPEG requires parsing through segments to find SOF marker\n let i = 2; // Skip FF D8\n while (i < bytes.length - 1) {\n if (bytes[i] === 0xff) {\n const marker = bytes[i + 1];\n // SOF0, SOF1, SOF2 markers contain dimensions\n if (marker >= 0xc0 && marker <= 0xc3) {\n if (i + 8 < bytes.length) {\n const height = (bytes[i + 5] << 8) | bytes[i + 6];\n const width = (bytes[i + 7] << 8) | bytes[i + 8];\n return { width, height };\n }\n }\n // Skip to next segment\n if (i + 3 < bytes.length) {\n const segmentLength = (bytes[i + 2] << 8) | bytes[i + 3];\n i += 2 + segmentLength;\n } else {\n break;\n }\n } else {\n i++;\n }\n }\n throw new Error('Unable to find JPEG dimensions');\n}\n\nexport function parseGifDimensions(bytes: Uint8Array): ImageDimensions {\n // GIF dimensions are at bytes 6-9 (little-endian)\n const width = bytes[6] | (bytes[7] << 8);\n const height = bytes[8] | (bytes[9] << 8);\n return { width, height };\n}\n\nexport function parseWebpDimensions(bytes: Uint8Array): ImageDimensions {\n /**\n * WebP format varies, check for VP8/VP8L/VP8X\n *\n * - VP8 : Simple File Format (Lossy)\n * - VP8L: Simple File Format (Lossless)\n * - VP8X: Extended File Format\n */\n const fourCC = String.fromCharCode(\n bytes[12],\n bytes[13],\n bytes[14],\n bytes[15],\n );\n\n if (fourCC === 'VP8 ') {\n // VP8 format - dimensions are in the frame header\n // Skip chunk size (4 bytes) and frame tag (3 bytes) + key frame info (1 byte)\n const startByte = 20 + 3; // Start after VP8 chunk header + frame tag\n\n // Read width and height from VP8 frame header\n const width = bytes[startByte + 3] | (bytes[startByte + 4] << 8);\n const height = bytes[startByte + 5] | (bytes[startByte + 6] << 8);\n\n return {\n width: width & 0x3fff,\n height: height & 0x3fff,\n };\n } else if (fourCC === 'VP8L') {\n // VP8L format - dimensions are right after the signature\n // Skip chunk size (4 bytes) and VP8L signature (1 byte)\n const startByte = 20 + 1;\n\n // Read 4 bytes containing width and height info\n const bits =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16) |\n (bytes[startByte + 3] << 24);\n\n const width = (bits & 0x3fff) + 1;\n const height = ((bits >> 14) & 0x3fff) + 1;\n\n return { width, height };\n } else if (fourCC === 'VP8X') {\n // VP8X format - extended format with dimensions in header\n // Skip chunk size (4 bytes) and flags (4 bytes)\n const startByte = 20 + 4;\n\n // Width is stored in 3 bytes (little endian) + 1\n const width =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16);\n\n // Height is stored in next 3 bytes (little endian) + 1\n const height =\n bytes[startByte + 3] |\n (bytes[startByte + 4] << 8) |\n (bytes[startByte + 5] << 16);\n\n return {\n width: width + 1,\n height: height + 1,\n };\n }\n\n throw new Error('Unsupported WebP format');\n}\n\nexport function parseBmpDimensions(bytes: Uint8Array): ImageDimensions {\n // BMP dimensions are at bytes 18-25 (little-endian)\n const width =\n bytes[18] | (bytes[19] << 8) | (bytes[20] << 16) | (bytes[21] << 24);\n const height =\n bytes[22] | (bytes[23] << 8) | (bytes[24] << 16) | (bytes[25] << 24);\n return { width, height };\n}\n"],"names":["IMAGE_MAGIC_STRINGS","Map","IMAGE_MAGIC_NUMBERS","Uint8Array","base64String2Uint8Array","base64","Buffer","binaryString","atob","bytes","i","parsePngDimensions","width","height","parseJpegDimensions","marker","segmentLength","Error","parseGifDimensions","parseWebpDimensions","fourCC","String","startByte","bits","parseBmpDimensions"],"mappings":";;;;AAMO,MAAMA,sBAAsB,IAAIC,IAAuB;IAC5D;QAAC;QAAQ;KAAO;IAChB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAS;KAAO;IACjB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAM;KAAM;CACd;AAEM,MAAMC,sBAAsB,IAAID,IAA2B;IAChE;QAAC,IAAIE,WAAW;YAAC;YAAM;YAAM;SAAK;QAAG;KAAO;IAC5C;QAAC,IAAIA,WAAW;YAAC;YAAM;YAAM;YAAM;SAAK;QAAG;KAAM;IACjD;QAAC,IAAIA,WAAW;YAAC;YAAM;YAAM;YAAM;SAAK;QAAG;KAAO;IAClD;QAAC,IAAIA,WAAW;YAAC;YAAM;YAAM;YAAM;SAAK;QAAG;KAAM;IACjD;QAAC,IAAIA,WAAW;YAAC;YAAM;SAAK;QAAG;KAAM;CACtC;AAEM,SAASC,wBACdC,MAAc;IAGd,IAAI,AAAiC,cAAjC,OAAOF,WAAW,UAAU,EAI9B,OAAOA,WAAW,UAAU,CAACE;IAIxB;QAHA,IAAI,AAAkB,eAAlB,OAAOC,QAEhB,OAAO,IAAIH,WAAWG,OAAO,IAAI,CAACD,QAAQ;QAG1C,MAAME,eAAeC,KAAKH;QAC1B,MAAMI,QAAQ,IAAIN,WAAWI,aAAa,MAAM;QAChD,IAAK,IAAIG,IAAI,GAAGA,IAAIH,aAAa,MAAM,EAAEG,IACvCD,KAAK,CAACC,EAAE,GAA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mBAAmBf,KAAiB;IAElD,MAAMG,QACJH,KAAK,CAAC,GAAG,GAAIA,KAAK,CAAC,GAAG,IAAI,IAAMA,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI;IACnE,MAAMI,SACJJ,KAAK,CAAC,GAAG,GAAIA,KAAK,CAAC,GAAG,IAAI,IAAMA,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI;IACnE,OAAO;QAAEG;QAAOC;IAAO;AACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/image/index.ts"],"names":[],"mappings":"AAIA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
var __webpack_modules__ = {
|
|
7
|
+
"./base64": function(module) {
|
|
8
|
+
module.exports = require("./base64.js");
|
|
9
|
+
},
|
|
10
|
+
"./binary": function(module) {
|
|
11
|
+
module.exports = require("./binary.js");
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var __webpack_module_cache__ = {};
|
|
15
|
+
function __webpack_require__(moduleId) {
|
|
16
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
17
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
18
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
19
|
+
exports: {}
|
|
20
|
+
};
|
|
21
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
22
|
+
return module.exports;
|
|
23
|
+
}
|
|
24
|
+
(()=>{
|
|
25
|
+
__webpack_require__.n = (module)=>{
|
|
26
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
27
|
+
__webpack_require__.d(getter, {
|
|
28
|
+
a: getter
|
|
29
|
+
});
|
|
30
|
+
return getter;
|
|
31
|
+
};
|
|
32
|
+
})();
|
|
33
|
+
(()=>{
|
|
34
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
35
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: definition[key]
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
(()=>{
|
|
42
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
43
|
+
})();
|
|
44
|
+
(()=>{
|
|
45
|
+
__webpack_require__.r = (exports1)=>{
|
|
46
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
47
|
+
value: 'Module'
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
50
|
+
value: true
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
})();
|
|
54
|
+
var __webpack_exports__ = {};
|
|
55
|
+
(()=>{
|
|
56
|
+
__webpack_require__.r(__webpack_exports__);
|
|
57
|
+
var _base64__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./base64");
|
|
58
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
59
|
+
for(var __WEBPACK_IMPORT_KEY__ in _base64__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
60
|
+
return _base64__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
61
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
62
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
63
|
+
var _binary__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./binary");
|
|
64
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
65
|
+
for(var __WEBPACK_IMPORT_KEY__ in _binary__WEBPACK_IMPORTED_MODULE_1__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
66
|
+
return _binary__WEBPACK_IMPORTED_MODULE_1__[key];
|
|
67
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
68
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
69
|
+
})();
|
|
70
|
+
for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
71
|
+
Object.defineProperty(exports, '__esModule', {
|
|
72
|
+
value: true
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image/index.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/compat_get_default_export","webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object"],"sourcesContent":["// getDefaultExport function for compatibility with non-ESM modules\n__webpack_require__.n = (module) => {\n\tvar getter = module && module.__esModule ?\n\t\t() => (module['default']) :\n\t\t() => (module);\n\t__webpack_require__.d(getter, { a: getter });\n\treturn getter;\n};\n","__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};"],"names":["__webpack_require__","module","getter","definition","key","Object","obj","prop","Symbol"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;IACAA,oBAAoB,CAAC,GAAG,CAACC;QACxB,IAAIC,SAASD,UAAUA,OAAO,UAAU,GACvC,IAAOA,MAAM,CAAC,UAAU,GACxB,IAAOA;QACRD,oBAAoB,CAAC,CAACE,QAAQ;YAAE,GAAGA;QAAO;QAC1C,OAAOA;IACR;;;ICPAF,oBAAoB,CAAC,GAAG,CAAC,UAASG;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGH,oBAAoB,CAAC,CAACG,YAAYC,QAAQ,CAACJ,oBAAoB,CAAC,CAAC,UAASI,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAJ,oBAAoB,CAAC,GAAG,CAACM,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFP,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOQ,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
"use strict";
|
|
6
6
|
var __webpack_modules__ = {
|
|
7
|
-
"./
|
|
8
|
-
module.exports = require("./
|
|
7
|
+
"./image": function(module) {
|
|
8
|
+
module.exports = require("./image/index.js");
|
|
9
9
|
},
|
|
10
10
|
"./type": function(module) {
|
|
11
11
|
module.exports = require("./type.js");
|
|
@@ -54,10 +54,10 @@ function __webpack_require__(moduleId) {
|
|
|
54
54
|
var __webpack_exports__ = {};
|
|
55
55
|
(()=>{
|
|
56
56
|
__webpack_require__.r(__webpack_exports__);
|
|
57
|
-
var
|
|
57
|
+
var _image__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./image");
|
|
58
58
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
59
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
|
60
|
-
return
|
|
59
|
+
for(var __WEBPACK_IMPORT_KEY__ in _image__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
60
|
+
return _image__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
61
61
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
62
62
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
63
63
|
var _type__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./type");
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-infra/media-utils",
|
|
3
3
|
"description": "media-utils",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0-alpha.3",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"delay": "6.0.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@types/node": "24.1
|
|
31
|
-
"typescript": "5.
|
|
30
|
+
"@types/node": "24.7.1",
|
|
31
|
+
"typescript": "5.9.3",
|
|
32
32
|
"vitest": "3.2.4",
|
|
33
33
|
"@vitest/coverage-v8": "3.2.4",
|
|
34
|
-
"@rslib/core": "0.
|
|
34
|
+
"@rslib/core": "0.15.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"dev": "rslib build --watch",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"image-parse.d.ts","sourceRoot":"","sources":["../../src/base64/image-parse.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1D,eAAO,MAAM,cAAc,wBAMzB,CAAC;AAEH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,2BAcrD;AAED;;;;;;GAMG;AAEH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAOrE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CA0BtE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAKrE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAoEtE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,CAOrE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64/image-parse.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object","webpack://@agent-infra/media-utils/./src/base64/image-parse.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport type { ImageType, ImageDimensions } from '../type';\n\nexport const IMAGE_TYPE_MAP = new Map<string, ImageType>([\n ['/9j/', 'jpeg'], // JPEG: FF D8 FF\n ['iVBORw', 'png'], // PNG: 89 50 4E 47\n ['UklGR', 'webp'], // WebP: 52 49 46 46\n ['R0lGOD', 'gif'], // GIF: 47 49 46 38\n ['Qk', 'bmp'], // BMP: 42 4D\n]);\n\nexport function base64String2Uint8Array(base64: string) {\n if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return new Uint8Array(Buffer.from(base64, 'base64'));\n } else {\n // Browser environment\n const binaryString = atob(base64);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n\n return bytes;\n }\n}\n\n/**\n * PNG: https://github.com/corkami/pics/blob/master/binary/PNG.png\n * JPEG: https://github.com/corkami/pics/blob/master/binary/JPG.png\n * WEBP: https://datatracker.ietf.org/doc/rfc9649/\n * GIF: https://github.com/corkami/pics/blob/master/binary/GIF.png\n * BMP: https://github.com/corkami/pics/blob/master/binary/bmp3.png\n */\n\nexport function parsePngDimensions(bytes: Uint8Array): ImageDimensions {\n // PNG dimensions are at bytes 16-23 (big-endian)\n const width =\n (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19];\n const height =\n (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23];\n return { width, height };\n}\n\nexport function parseJpegDimensions(bytes: Uint8Array): ImageDimensions {\n // JPEG requires parsing through segments to find SOF marker\n let i = 2; // Skip FF D8\n while (i < bytes.length - 1) {\n if (bytes[i] === 0xff) {\n const marker = bytes[i + 1];\n // SOF0, SOF1, SOF2 markers contain dimensions\n if (marker >= 0xc0 && marker <= 0xc3) {\n if (i + 8 < bytes.length) {\n const height = (bytes[i + 5] << 8) | bytes[i + 6];\n const width = (bytes[i + 7] << 8) | bytes[i + 8];\n return { width, height };\n }\n }\n // Skip to next segment\n if (i + 3 < bytes.length) {\n const segmentLength = (bytes[i + 2] << 8) | bytes[i + 3];\n i += 2 + segmentLength;\n } else {\n break;\n }\n } else {\n i++;\n }\n }\n throw new Error('Unable to find JPEG dimensions');\n}\n\nexport function parseGifDimensions(bytes: Uint8Array): ImageDimensions {\n // GIF dimensions are at bytes 6-9 (little-endian)\n const width = bytes[6] | (bytes[7] << 8);\n const height = bytes[8] | (bytes[9] << 8);\n return { width, height };\n}\n\nexport function parseWebpDimensions(bytes: Uint8Array): ImageDimensions {\n /**\n * WebP format varies, check for VP8/VP8L/VP8X\n *\n * - VP8 : Simple File Format (Lossy)\n * - VP8L: Simple File Format (Lossless)\n * - VP8X: Extended File Format\n */\n const fourCC = String.fromCharCode(\n bytes[12],\n bytes[13],\n bytes[14],\n bytes[15],\n );\n\n if (fourCC === 'VP8 ') {\n // VP8 format - dimensions are in the frame header\n // Skip chunk size (4 bytes) and frame tag (3 bytes) + key frame info (1 byte)\n const startByte = 20 + 3; // Start after VP8 chunk header + frame tag\n\n // Read width and height from VP8 frame header\n const width = bytes[startByte + 3] | (bytes[startByte + 4] << 8);\n const height = bytes[startByte + 5] | (bytes[startByte + 6] << 8);\n\n return {\n width: width & 0x3fff,\n height: height & 0x3fff,\n };\n } else if (fourCC === 'VP8L') {\n // VP8L format - dimensions are right after the signature\n // Skip chunk size (4 bytes) and VP8L signature (1 byte)\n const startByte = 20 + 1;\n\n // Read 4 bytes containing width and height info\n const bits =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16) |\n (bytes[startByte + 3] << 24);\n\n const width = (bits & 0x3fff) + 1;\n const height = ((bits >> 14) & 0x3fff) + 1;\n\n return { width, height };\n } else if (fourCC === 'VP8X') {\n // VP8X format - extended format with dimensions in header\n // Skip chunk size (4 bytes) and flags (4 bytes)\n const startByte = 20 + 4;\n\n // Width is stored in 3 bytes (little endian) + 1\n const width =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16);\n\n // Height is stored in next 3 bytes (little endian) + 1\n const height =\n bytes[startByte + 3] |\n (bytes[startByte + 4] << 8) |\n (bytes[startByte + 5] << 16);\n\n return {\n width: width + 1,\n height: height + 1,\n };\n }\n\n throw new Error('Unsupported WebP format');\n}\n\nexport function parseBmpDimensions(bytes: Uint8Array): ImageDimensions {\n // BMP dimensions are at bytes 18-25 (little-endian)\n const width =\n bytes[18] | (bytes[19] << 8) | (bytes[20] << 16) | (bytes[21] << 24);\n const height =\n bytes[22] | (bytes[23] << 8) | (bytes[24] << 16) | (bytes[25] << 24);\n return { width, height };\n}"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","IMAGE_TYPE_MAP","Map","base64String2Uint8Array","base64","Buffer","Uint8Array","binaryString","atob","bytes","i","parsePngDimensions","width","height","parseJpegDimensions","marker","segmentLength","Error","parseGifDimensions","parseWebpDimensions","fourCC","String","startByte","bits","parseBmpDimensions"],"mappings":";;;;;;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO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XA,QAAmB;QAG5B,MAAME,YAAY;QAGlB,MAAMC,OACJd,KAAK,CAACa,UAAU,GACfb,KAAK,CAACa,YAAY,EAAE,IAAI,IACxBb,KAAK,CAACa,YAAY,EAAE,IAAI,KACxBb,KAAK,CAACa,YAAY,EAAE,IAAI;QAE3B,MAAMV,QAASW,AAAAA,CAAAA,AAAO,SAAPA,IAAY,IAAK;QAChC,MAAMV,SAAU,AAACU,CAAAA,QAAQ,KAAM,MAAK,IAAK;QAEzC,OAAO;YAAEX;YAAOC;QAAO;IACzB;IAAO,IAAIO,AAAW,WAAXA,QAAmB;QAG5B,MAAME,YAAY;QAGlB,MAAMV,QACJH,KAAK,CAACa,UAAU,GACfb,KAAK,CAACa,YAAY,EAAE,IAAI,IACxBb,KAAK,CAACa,YAAY,EAAE,IAAI;QAG3B,MAAMT,SACJJ,KAAK,CAACa,YAAY,EAAE,GACnBb,KAAK,CAACa,YAAY,EAAE,IAAI,IACxBb,KAAK,CAACa,YAAY,EAAE,IAAI;QAE3B,OAAO;YACL,OAAOV,QAAQ;YACf,QAAQC,SAAS;QACnB;IACF;IAEA,MAAM,IAAII,MAAM;AAClB;AAEO,SAASO,mBAAmBf,KAAiB;IAElD,MAAMG,QACJH,KAAK,CAAC,GAAG,GAAIA,KAAK,CAAC,GAAG,IAAI,IAAMA,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI;IACnE,MAAMI,SACJJ,KAAK,CAAC,GAAG,GAAIA,KAAK,CAAC,GAAG,IAAI,IAAMA,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI;IACnE,OAAO;QAAEG;QAAOC;IAAO;AACzB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64/image-parse.mjs","sources":["webpack://@agent-infra/media-utils/./src/base64/image-parse.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport type { ImageType, ImageDimensions } from '../type';\n\nexport const IMAGE_TYPE_MAP = new Map<string, ImageType>([\n ['/9j/', 'jpeg'], // JPEG: FF D8 FF\n ['iVBORw', 'png'], // PNG: 89 50 4E 47\n ['UklGR', 'webp'], // WebP: 52 49 46 46\n ['R0lGOD', 'gif'], // GIF: 47 49 46 38\n ['Qk', 'bmp'], // BMP: 42 4D\n]);\n\nexport function base64String2Uint8Array(base64: string) {\n if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return new Uint8Array(Buffer.from(base64, 'base64'));\n } else {\n // Browser environment\n const binaryString = atob(base64);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n\n return bytes;\n }\n}\n\n/**\n * PNG: https://github.com/corkami/pics/blob/master/binary/PNG.png\n * JPEG: https://github.com/corkami/pics/blob/master/binary/JPG.png\n * WEBP: https://datatracker.ietf.org/doc/rfc9649/\n * GIF: https://github.com/corkami/pics/blob/master/binary/GIF.png\n * BMP: https://github.com/corkami/pics/blob/master/binary/bmp3.png\n */\n\nexport function parsePngDimensions(bytes: Uint8Array): ImageDimensions {\n // PNG dimensions are at bytes 16-23 (big-endian)\n const width =\n (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19];\n const height =\n (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23];\n return { width, height };\n}\n\nexport function parseJpegDimensions(bytes: Uint8Array): ImageDimensions {\n // JPEG requires parsing through segments to find SOF marker\n let i = 2; // Skip FF D8\n while (i < bytes.length - 1) {\n if (bytes[i] === 0xff) {\n const marker = bytes[i + 1];\n // SOF0, SOF1, SOF2 markers contain dimensions\n if (marker >= 0xc0 && marker <= 0xc3) {\n if (i + 8 < bytes.length) {\n const height = (bytes[i + 5] << 8) | bytes[i + 6];\n const width = (bytes[i + 7] << 8) | bytes[i + 8];\n return { width, height };\n }\n }\n // Skip to next segment\n if (i + 3 < bytes.length) {\n const segmentLength = (bytes[i + 2] << 8) | bytes[i + 3];\n i += 2 + segmentLength;\n } else {\n break;\n }\n } else {\n i++;\n }\n }\n throw new Error('Unable to find JPEG dimensions');\n}\n\nexport function parseGifDimensions(bytes: Uint8Array): ImageDimensions {\n // GIF dimensions are at bytes 6-9 (little-endian)\n const width = bytes[6] | (bytes[7] << 8);\n const height = bytes[8] | (bytes[9] << 8);\n return { width, height };\n}\n\nexport function parseWebpDimensions(bytes: Uint8Array): ImageDimensions {\n /**\n * WebP format varies, check for VP8/VP8L/VP8X\n *\n * - VP8 : Simple File Format (Lossy)\n * - VP8L: Simple File Format (Lossless)\n * - VP8X: Extended File Format\n */\n const fourCC = String.fromCharCode(\n bytes[12],\n bytes[13],\n bytes[14],\n bytes[15],\n );\n\n if (fourCC === 'VP8 ') {\n // VP8 format - dimensions are in the frame header\n // Skip chunk size (4 bytes) and frame tag (3 bytes) + key frame info (1 byte)\n const startByte = 20 + 3; // Start after VP8 chunk header + frame tag\n\n // Read width and height from VP8 frame header\n const width = bytes[startByte + 3] | (bytes[startByte + 4] << 8);\n const height = bytes[startByte + 5] | (bytes[startByte + 6] << 8);\n\n return {\n width: width & 0x3fff,\n height: height & 0x3fff,\n };\n } else if (fourCC === 'VP8L') {\n // VP8L format - dimensions are right after the signature\n // Skip chunk size (4 bytes) and VP8L signature (1 byte)\n const startByte = 20 + 1;\n\n // Read 4 bytes containing width and height info\n const bits =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16) |\n (bytes[startByte + 3] << 24);\n\n const width = (bits & 0x3fff) + 1;\n const height = ((bits >> 14) & 0x3fff) + 1;\n\n return { width, height };\n } else if (fourCC === 'VP8X') {\n // VP8X format - extended format with dimensions in header\n // Skip chunk size (4 bytes) and flags (4 bytes)\n const startByte = 20 + 4;\n\n // Width is stored in 3 bytes (little endian) + 1\n const width =\n bytes[startByte] |\n (bytes[startByte + 1] << 8) |\n (bytes[startByte + 2] << 16);\n\n // Height is stored in next 3 bytes (little endian) + 1\n const height =\n bytes[startByte + 3] |\n (bytes[startByte + 4] << 8) |\n (bytes[startByte + 5] << 16);\n\n return {\n width: width + 1,\n height: height + 1,\n };\n }\n\n throw new Error('Unsupported WebP format');\n}\n\nexport function parseBmpDimensions(bytes: Uint8Array): ImageDimensions {\n // BMP dimensions are at bytes 18-25 (little-endian)\n const width =\n bytes[18] | (bytes[19] << 8) | (bytes[20] << 16) | (bytes[21] << 24);\n const height =\n bytes[22] | (bytes[23] << 8) | (bytes[24] << 16) | (bytes[25] << 24);\n return { width, height };\n}"],"names":["IMAGE_TYPE_MAP","Map","base64String2Uint8Array","base64","Buffer","Uint8Array","binaryString","atob","bytes","i","parsePngDimensions","width","height","parseJpegDimensions","marker","segmentLength","Error","parseGifDimensions","parseWebpDimensions","fourCC","String","startByte","bits","parseBmpDimensions"],"mappings":";;;;AAMO,MAAMA,iBAAiB,IAAIC,IAAuB;IACvD;QAAC;QAAQ;KAAO;IAChB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAS;KAAO;IACjB;QAAC;QAAU;KAAM;IACjB;QAAC;QAAM;KAAM;CACd;AAEM,SAASC,wBAAwBC,MAAc;IACpD,IAAI,AAAkB,eAAlB,OAAOC,QAET,OAAO,IAAIC,WAAWD,OAAO,IAAI,CAACD,QAAQ;IACrC;QAEL,MAAMG,eAAeC,KAAKJ;QAC1B,MAAMK,QAAQ,IAAIH,WAAWC,aAAa,MAAM;QAChD,IAAK,IAAIG,IAAI,GAAGA,IAAIH,aAAa,MAAM,EAAEG,IACvCD,KAAK,CAACC,EAAE,GAAGH,aAAa,UAAU,CAACG;QAGrC,OAAOD;IACT;AACF;AAUO,SAASE,mBAAmBF,KAAiB;IAElD,MAAMG,QACHH,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI,IAAKA,KAAK,CAAC,GAAG;IACtE,MAAMI,SACHJ,KAAK,CAAC,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AAG,IAAI,KAAOA,KAAK,CAAC,GAAG,IAAI;IACnE,OAAO;QAAEG;QAAOC;IAAO;AACzB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base64/index.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAE1D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,MAAM,CAAC,CAAa;IAE5B,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,UAAU,CAAgC;gBAEtC,WAAW,EAAE,MAAM;IAIxB,kBAAkB,IAAI,MAAM;IAI5B,SAAS,IAAI,UAAU;IAM9B;;OAEG;IACI,YAAY,IAAI,SAAS,GAAG,IAAI;IAiBhC,aAAa,IAAI,eAAe,GAAG,IAAI;IAqDvC,UAAU;IASjB;;;OAGG;IACH,OAAO,CAAC,eAAe;CAcxB"}
|
package/dist/base64/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64/index.js","sources":["webpack://@agent-infra/media-utils/webpack/runtime/define_property_getters","webpack://@agent-infra/media-utils/webpack/runtime/has_own_property","webpack://@agent-infra/media-utils/webpack/runtime/make_namespace_object","webpack://@agent-infra/media-utils/./src/base64/index.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n base64String2Uint8Array,\n IMAGE_TYPE_MAP,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\nexport class Base64ImageParser {\n private pureBase64: string;\n private buffer?: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(base64Image: string) {\n this.pureBase64 = base64Image.replace(/^data:image\\/\\w+;base64,/, '');\n }\n\n public getPureBase64Image(): string {\n return this.pureBase64;\n }\n\n public getBuffer(): Uint8Array {\n this.buffer = base64String2Uint8Array(this.pureBase64);\n\n return this.buffer;\n }\n\n /**\n * get image type form base64 magic number\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n const prefix = this.pureBase64.substring(0, 8);\n\n for (const [signature, type] of IMAGE_TYPE_MAP) {\n if (prefix.startsWith(signature)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n const bytes = this.getHeaderBuffer(32); // 16-23\n this.dimensions = parsePngDimensions(bytes);\n break;\n }\n case 'jpeg': {\n try {\n const headerBytes = this.getHeaderBuffer(1024); // SOF marker\n this.dimensions = parseJpegDimensions(headerBytes);\n } catch(e) {\n const fullBuffer = this.getBuffer();\n this.dimensions = parseJpegDimensions(fullBuffer);\n }\n break;\n }\n case 'webp': {\n const bytes = this.getHeaderBuffer(32); // 23 - 29\n this.dimensions = parseWebpDimensions(bytes);\n break;\n }\n case 'gif': {\n const bytes = this.getHeaderBuffer(24); // 6 - 9\n this.dimensions = parseGifDimensions(bytes);\n break;\n }\n case 'bmp': {\n const bytes = this.getHeaderBuffer(40); // 18-25\n this.dimensions = parseBmpDimensions(bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n\n public getDataUri() {\n const imageType = this.getImageType();\n if (imageType) {\n return `data:image/${this.imageType};base64,${this.pureBase64}`;\n }\n\n return null;\n }\n\n /**\n * Get only the header bytes needed for dimension parsing\n * This is much more memory efficient than converting the entire image\n */\n private getHeaderBuffer(maxBytes: number): Uint8Array {\n if (this.buffer) {\n return this.buffer;\n }\n\n // Calculate how much of the base64 we need to decode\n // Base64 encoding: 4 characters represent 3 bytes\n const base64CharsNeeded = Math.ceil((maxBytes * 4) / 3);\n // Round up to nearest multiple of 4 to avoid padding issues\n const alignedChars = Math.ceil(base64CharsNeeded / 4) * 4;\n const headerBase64 = this.pureBase64.substring(0, alignedChars);\n\n return base64String2Uint8Array(headerBase64);\n }\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","Base64ImageParser","base64String2Uint8Array","prefix","signature","type","IMAGE_TYPE_MAP","imageType","bytes","parsePngDimensions","headerBytes","parseJpegDimensions","e","fullBuffer","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","maxBytes","base64CharsNeeded","Math","alignedChars","headerBase64","base64Image"],"mappings":";;;;;;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFL,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAO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,WAAmB,CAAE;QANjC,uBAAQ,cAAR;QACA,uBAAQ,UAAR;QAEA,uBAAQ,aAA8B;QACtC,uBAAQ,cAAqC;QAG3C,IAAI,CAAC,UAAU,GAAGA,YAAY,OAAO,CAAC,4BAA4B;IACpE;AAgHF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64/index.mjs","sources":["webpack://@agent-infra/media-utils/./src/base64/index.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\nimport {\n base64String2Uint8Array,\n IMAGE_TYPE_MAP,\n parseBmpDimensions,\n parseGifDimensions,\n parseJpegDimensions,\n parsePngDimensions,\n parseWebpDimensions,\n} from './image-parse';\n\nimport type { ImageDimensions, ImageType } from '../type';\n\nexport class Base64ImageParser {\n private pureBase64: string;\n private buffer?: Uint8Array;\n\n private imageType: ImageType | null = null;\n private dimensions: ImageDimensions | null = null;\n\n constructor(base64Image: string) {\n this.pureBase64 = base64Image.replace(/^data:image\\/\\w+;base64,/, '');\n }\n\n public getPureBase64Image(): string {\n return this.pureBase64;\n }\n\n public getBuffer(): Uint8Array {\n this.buffer = base64String2Uint8Array(this.pureBase64);\n\n return this.buffer;\n }\n\n /**\n * get image type form base64 magic number\n */\n public getImageType(): ImageType | null {\n if (this.imageType) {\n return this.imageType;\n }\n\n const prefix = this.pureBase64.substring(0, 8);\n\n for (const [signature, type] of IMAGE_TYPE_MAP) {\n if (prefix.startsWith(signature)) {\n this.imageType = type;\n break;\n }\n }\n\n return this.imageType;\n }\n\n public getDimensions(): ImageDimensions | null {\n if (this.dimensions) {\n return this.dimensions;\n }\n\n const imageType = this.getImageType();\n if (!imageType) {\n return null;\n }\n\n try {\n switch (imageType) {\n case 'png': {\n const bytes = this.getHeaderBuffer(32); // 16-23\n this.dimensions = parsePngDimensions(bytes);\n break;\n }\n case 'jpeg': {\n try {\n const headerBytes = this.getHeaderBuffer(1024); // SOF marker\n this.dimensions = parseJpegDimensions(headerBytes);\n } catch(e) {\n const fullBuffer = this.getBuffer();\n this.dimensions = parseJpegDimensions(fullBuffer);\n }\n break;\n }\n case 'webp': {\n const bytes = this.getHeaderBuffer(32); // 23 - 29\n this.dimensions = parseWebpDimensions(bytes);\n break;\n }\n case 'gif': {\n const bytes = this.getHeaderBuffer(24); // 6 - 9\n this.dimensions = parseGifDimensions(bytes);\n break;\n }\n case 'bmp': {\n const bytes = this.getHeaderBuffer(40); // 18-25\n this.dimensions = parseBmpDimensions(bytes);\n break;\n }\n default:\n return null;\n }\n\n return this.dimensions;\n } catch (error) {\n console.warn('Failed to parse image dimensions:', error);\n return null;\n }\n }\n\n public getDataUri() {\n const imageType = this.getImageType();\n if (imageType) {\n return `data:image/${this.imageType};base64,${this.pureBase64}`;\n }\n\n return null;\n }\n\n /**\n * Get only the header bytes needed for dimension parsing\n * This is much more memory efficient than converting the entire image\n */\n private getHeaderBuffer(maxBytes: number): Uint8Array {\n if (this.buffer) {\n return this.buffer;\n }\n\n // Calculate how much of the base64 we need to decode\n // Base64 encoding: 4 characters represent 3 bytes\n const base64CharsNeeded = Math.ceil((maxBytes * 4) / 3);\n // Round up to nearest multiple of 4 to avoid padding issues\n const alignedChars = Math.ceil(base64CharsNeeded / 4) * 4;\n const headerBase64 = this.pureBase64.substring(0, alignedChars);\n\n return base64String2Uint8Array(headerBase64);\n }\n}\n"],"names":["Base64ImageParser","base64String2Uint8Array","prefix","signature","type","IMAGE_TYPE_MAP","imageType","bytes","parsePngDimensions","headerBytes","parseJpegDimensions","e","fullBuffer","parseWebpDimensions","parseGifDimensions","parseBmpDimensions","error","console","maxBytes","base64CharsNeeded","Math","alignedChars","headerBase64","base64Image"],"mappings":";;;;;AAGC;;;;;;;;;;AAaM,MAAMA;IAWJ,qBAA6B;QAClC,OAAO,IAAI,CAAC,UAAU;IACxB;IAEO,YAAwB;QAC7B,IAAI,CAAC,MAAM,GAAGC,wBAAwB,IAAI,CAAC,UAAU;QA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