@carbonenginejs/runtime-resource 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +29 -29
  3. package/README.md +67 -548
  4. package/dist/CjsMotherLode.js +1271 -1271
  5. package/dist/CjsResMan.js +3005 -3005
  6. package/dist/CjsResManQueue.js +214 -214
  7. package/dist/CjsResource.js +566 -566
  8. package/dist/CjsResourceSource.js +59 -59
  9. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  10. package/dist/format/CjsBlueReader.js +269 -269
  11. package/dist/format/CjsFormat.js +192 -192
  12. package/dist/format/CjsReader.js +18 -18
  13. package/dist/format/CjsResourceProbe.js +277 -277
  14. package/dist/format/payloadContract.js +173 -173
  15. package/dist/formats/black/CjsBlackFormat.js +278 -278
  16. package/dist/formats/black/core/CjsBlackBinaryReader.js +153 -153
  17. package/dist/formats/black/core/CjsBlackPropertyReaders.js +382 -382
  18. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  19. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +433 -433
  20. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +4 -4
  21. package/dist/formats/black/core/blackConstants.js +7 -7
  22. package/dist/formats/black/core/blackDefinitions.js +10 -10
  23. package/dist/formats/black/core/blackEnums.js +6 -6
  24. package/dist/formats/black/core/blackSchema.js +3 -3
  25. package/dist/formats/black/core/blackVersion.js +22 -22
  26. package/dist/formats/black/core/helpers.js +182 -182
  27. package/dist/formats/black/core/schema.js +4 -4
  28. package/dist/formats/black/index.js +2 -2
  29. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  30. package/dist/formats/bnk/core/graph.js +140 -140
  31. package/dist/formats/bnk/core/helpers.js +409 -409
  32. package/dist/formats/bnk/core/musicNodes.js +489 -489
  33. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  34. package/dist/formats/bnk/index.js +2 -2
  35. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  36. package/dist/formats/cmf/core/binary.js +118 -118
  37. package/dist/formats/cmf/core/buffers.js +233 -233
  38. package/dist/formats/cmf/core/constants.js +47 -47
  39. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  40. package/dist/formats/cmf/core/helpers.js +287 -287
  41. package/dist/formats/cmf/core/pack.js +276 -276
  42. package/dist/formats/cmf/core/schema.js +364 -364
  43. package/dist/formats/cmf/core/shared.js +268 -268
  44. package/dist/formats/cmf/core/writer.js +517 -517
  45. package/dist/formats/cmf/index.js +2 -2
  46. package/dist/formats/dds/CjsDdsFormat.js +193 -193
  47. package/dist/formats/dds/core/bc6h.js +288 -288
  48. package/dist/formats/dds/core/bc7.js +251 -251
  49. package/dist/formats/dds/core/helpers.js +815 -815
  50. package/dist/formats/dds/index.js +2 -2
  51. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  52. package/dist/formats/fbx/core/helpers.js +3901 -3901
  53. package/dist/formats/fbx/index.js +2 -2
  54. package/dist/formats/flac/CjsFlacFormat.js +87 -87
  55. package/dist/formats/flac/core/helpers.js +295 -295
  56. package/dist/formats/flac/index.js +2 -2
  57. package/dist/formats/gif/CjsGifFormat.js +87 -87
  58. package/dist/formats/gif/core/helpers.js +360 -360
  59. package/dist/formats/gif/index.js +2 -2
  60. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  61. package/dist/formats/gltf/core/helpers.js +292 -292
  62. package/dist/formats/gltf/core/json.js +79 -79
  63. package/dist/formats/gltf/core/parser.js +666 -666
  64. package/dist/formats/gltf/core/targets.js +163 -163
  65. package/dist/formats/gltf/index.js +2 -2
  66. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  67. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  68. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  69. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  70. package/dist/formats/gr2/core/bitknit2.js +280 -0
  71. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  72. package/dist/formats/gr2/core/curves.js +1047 -0
  73. package/dist/formats/gr2/core/curves.js.map +1 -0
  74. package/dist/formats/gr2/core/gsf.js +72 -0
  75. package/dist/formats/gr2/core/gsf.js.map +1 -0
  76. package/dist/formats/gr2/core/helpers.js +332 -0
  77. package/dist/formats/gr2/core/helpers.js.map +1 -0
  78. package/dist/formats/gr2/core/json.js +622 -0
  79. package/dist/formats/gr2/core/json.js.map +1 -0
  80. package/dist/formats/gr2/core/oodle1.js +388 -0
  81. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  82. package/dist/formats/gr2/core/reader.js +617 -0
  83. package/dist/formats/gr2/core/reader.js.map +1 -0
  84. package/dist/formats/gr2/core/tangents.js +48 -0
  85. package/dist/formats/gr2/core/tangents.js.map +1 -0
  86. package/dist/formats/gr2/core/targets.js +351 -0
  87. package/dist/formats/gr2/core/targets.js.map +1 -0
  88. package/dist/formats/gr2/index.js +3 -0
  89. package/dist/formats/gr2/index.js.map +1 -0
  90. package/dist/formats/index.js +30 -23
  91. package/dist/formats/index.js.map +1 -1
  92. package/dist/formats/jpeg/CjsJpegFormat.js +206 -206
  93. package/dist/formats/jpeg/core/helpers.js +350 -350
  94. package/dist/formats/jpeg/core/jpeg.js +377 -377
  95. package/dist/formats/jpeg/index.js +2 -2
  96. package/dist/formats/mp3/CjsMp3Format.js +192 -192
  97. package/dist/formats/mp3/core/helpers.js +338 -338
  98. package/dist/formats/mp3/index.js +2 -2
  99. package/dist/formats/mp4/CjsMp4Format.js +191 -191
  100. package/dist/formats/mp4/core/helpers.js +449 -449
  101. package/dist/formats/mp4/index.js +2 -2
  102. package/dist/formats/obj/CjsObjFormat.js +253 -253
  103. package/dist/formats/obj/core/helpers.js +573 -573
  104. package/dist/formats/obj/core/json.js +64 -64
  105. package/dist/formats/obj/core/parser.js +321 -321
  106. package/dist/formats/obj/index.js +2 -2
  107. package/dist/formats/ogg/CjsOggFormat.js +88 -88
  108. package/dist/formats/ogg/core/helpers.js +387 -387
  109. package/dist/formats/ogg/core/imdct.js +178 -178
  110. package/dist/formats/ogg/core/vorbis.js +999 -999
  111. package/dist/formats/ogg/index.js +2 -2
  112. package/dist/formats/png/CjsPngFormat.js +195 -195
  113. package/dist/formats/png/core/helpers.js +583 -583
  114. package/dist/formats/png/index.js +2 -2
  115. package/dist/formats/red/CjsRedFormat.js +261 -261
  116. package/dist/formats/red/core/CjsRedReader.js +194 -194
  117. package/dist/formats/red/core/blackDefinitions.js +3 -3
  118. package/dist/formats/red/core/helpers.js +139 -139
  119. package/dist/formats/red/core/redGraph.js +68 -68
  120. package/dist/formats/red/core/schema.js +4 -4
  121. package/dist/formats/red/index.js +2 -2
  122. package/dist/formats/stl/CjsStlFormat.js +365 -365
  123. package/dist/formats/stl/core/helpers.js +261 -261
  124. package/dist/formats/stl/core/json.js +51 -51
  125. package/dist/formats/stl/core/stl.js +629 -629
  126. package/dist/formats/stl/core/targets.js +163 -163
  127. package/dist/formats/stl/index.js +2 -2
  128. package/dist/formats/tga/CjsTgaFormat.js +192 -192
  129. package/dist/formats/tga/core/helpers.js +446 -446
  130. package/dist/formats/tga/index.js +2 -2
  131. package/dist/formats/wav/CjsWavFormat.js +192 -192
  132. package/dist/formats/wav/core/helpers.js +328 -328
  133. package/dist/formats/wav/index.js +2 -2
  134. package/dist/formats/webm/CjsWebmFormat.js +191 -191
  135. package/dist/formats/webm/core/helpers.js +537 -537
  136. package/dist/formats/webm/index.js +2 -2
  137. package/dist/formats/webp/CjsWebpFormat.js +86 -86
  138. package/dist/formats/webp/core/helpers.js +214 -214
  139. package/dist/formats/webp/index.js +2 -2
  140. package/dist/formats/wem/CjsWemFormat.js +241 -241
  141. package/dist/formats/wem/core/bitStream.js +259 -259
  142. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  143. package/dist/formats/wem/core/helpers.js +417 -417
  144. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  145. package/dist/formats/wem/core/ptadpcm.js +77 -77
  146. package/dist/formats/wem/core/resolve.js +121 -121
  147. package/dist/formats/wem/core/wemToOgg.js +485 -485
  148. package/dist/formats/wem/index.js +2 -2
  149. package/dist/formats/yaml/CjsYamlFormat.js +83 -83
  150. package/dist/formats/yaml/core/CjsYamlReader.js +305 -305
  151. package/dist/formats/yaml/core/helpers.js +160 -160
  152. package/dist/formats/yaml/index.js +2 -2
  153. package/dist/index.js +49 -48
  154. package/dist/index.js.map +1 -1
  155. package/dist/resourcePath.js +18 -18
  156. package/dist/resourceStates.js +9 -9
  157. package/dist/resources/AudioGeometryResData.js +47 -47
  158. package/dist/resources/GStateBindingCallbackData.js +31 -31
  159. package/dist/resources/MeshDecalData.js +37 -37
  160. package/dist/resources/MeshDecalLodData.js +34 -34
  161. package/dist/resources/Tr2EffectRes.js +71 -71
  162. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  163. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  164. package/dist/resources/Tr2ImageRes.js +114 -114
  165. package/dist/resources/Tr2LightProfileRes.js +40 -40
  166. package/dist/resources/Tr2MaterialArea.js +34 -34
  167. package/dist/resources/Tr2MaterialMesh.js +31 -31
  168. package/dist/resources/Tr2MaterialRes.js +34 -34
  169. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  170. package/dist/resources/Tr2TextureLodManager.js +80 -80
  171. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  172. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  173. package/dist/resources/Tr2TexturePipeline.js +52 -52
  174. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  175. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  176. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  177. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  178. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  179. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  180. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  181. package/dist/resources/TriGeometryRes.js +239 -239
  182. package/dist/resources/TriGeometryResAreaData.js +59 -59
  183. package/dist/resources/TriGeometryResJointData.js +38 -38
  184. package/dist/resources/TriGeometryResLodData.js +88 -88
  185. package/dist/resources/TriGeometryResMeshData.js +63 -63
  186. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  187. package/dist/resources/TriGrannyRes.js +43 -43
  188. package/dist/resources/TriJointBinding.js +38 -38
  189. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  190. package/dist/resources/TriRtGeometryConstants.js +88 -88
  191. package/dist/resources/TriTextureRes.js +304 -304
  192. package/dist/resources/enums.js +18 -18
  193. package/dist/resources/resourceBoundary.js +47 -47
  194. package/dist/resources/texturePipelineBehavior.js +308 -308
  195. package/dist/texture/CjsTextureArrayRes.js +406 -406
  196. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  197. package/docs/README.md +69 -0
  198. package/docs/architecture.md +86 -0
  199. package/docs/concepts/resource-lifecycle.md +217 -0
  200. package/docs/formats/README.md +104 -0
  201. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  202. package/docs/formats/stl.md +37 -0
  203. package/docs/formats/wwise.md +44 -0
  204. package/docs/reference/events.md +92 -0
  205. package/docs/reference/motherlode-cache.md +244 -0
  206. package/docs/reference/queues.md +102 -0
  207. package/docs/reference/reload.md +107 -0
  208. package/docs/reference/texture-arrays.md +113 -0
  209. package/docs/reference/texture-pipeline.md +53 -0
  210. package/docs/roadmap.md +104 -0
  211. package/format-notices/black/LICENSE +21 -21
  212. package/format-notices/black/NOTICE +47 -47
  213. package/format-notices/bnk/LICENSE +21 -21
  214. package/format-notices/bnk/NOTICE +20 -20
  215. package/format-notices/cmf/LICENSE +21 -21
  216. package/format-notices/cmf/NOTICE +36 -36
  217. package/format-notices/dds/LICENSE +21 -21
  218. package/format-notices/dds/NOTICE +14 -14
  219. package/format-notices/fbx/LICENSE +21 -21
  220. package/format-notices/fbx/NOTICE +14 -14
  221. package/format-notices/flac/LICENSE +21 -21
  222. package/format-notices/flac/NOTICE +14 -14
  223. package/format-notices/gif/LICENSE +21 -21
  224. package/format-notices/gif/NOTICE +14 -14
  225. package/format-notices/gltf/LICENSE +21 -21
  226. package/format-notices/gltf/NOTICE +27 -27
  227. package/format-notices/gr2/LICENSE +21 -0
  228. package/format-notices/gr2/NOTICE +60 -0
  229. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  230. package/format-notices/jpeg/LICENSE +21 -21
  231. package/format-notices/jpeg/NOTICE +14 -14
  232. package/format-notices/mp3/LICENSE +21 -21
  233. package/format-notices/mp3/NOTICE +14 -14
  234. package/format-notices/mp4/LICENSE +21 -21
  235. package/format-notices/mp4/NOTICE +14 -14
  236. package/format-notices/obj/LICENSE +21 -21
  237. package/format-notices/obj/NOTICE +26 -26
  238. package/format-notices/ogg/LICENSE +21 -21
  239. package/format-notices/ogg/NOTICE +28 -28
  240. package/format-notices/png/LICENSE +21 -21
  241. package/format-notices/png/NOTICE +14 -14
  242. package/format-notices/red/LICENSE +21 -21
  243. package/format-notices/red/NOTICE +31 -31
  244. package/format-notices/stl/LICENSE +21 -21
  245. package/format-notices/stl/NOTICE +21 -21
  246. package/format-notices/tga/LICENSE +21 -21
  247. package/format-notices/tga/NOTICE +14 -14
  248. package/format-notices/wav/LICENSE +21 -21
  249. package/format-notices/wav/NOTICE +14 -14
  250. package/format-notices/webm/LICENSE +21 -21
  251. package/format-notices/webm/NOTICE +14 -14
  252. package/format-notices/webp/LICENSE +21 -21
  253. package/format-notices/webp/NOTICE +14 -14
  254. package/format-notices/wem/LICENSE +57 -57
  255. package/format-notices/wem/NOTICE +33 -33
  256. package/format-notices/yaml/LICENSE +21 -21
  257. package/format-notices/yaml/NOTICE +44 -44
  258. package/package.json +52 -51
  259. package/resource-lifecycle.md +0 -679
@@ -1,815 +1,815 @@
1
- import { decodeBc6h } from './bc6h.js';
2
- import { decodeBc7 } from './bc7.js';
3
-
4
- const OUTPUT_IMAGE = "image";
5
- const OUTPUT_TEXTURE = "texture";
6
- const OUTPUT_RGBA = "rgba";
7
- const OUTPUT_RAW = "raw";
8
- const OUTPUT_JSON = "json";
9
- const DEFAULT_VALUES = Object.freeze({
10
- emit: OUTPUT_RAW,
11
- inputType: "",
12
- source: ""
13
- });
14
- const DDS_HEADER_SIZE = 124;
15
- const DDS_PIXELFORMAT_OFFSET = 76;
16
- const DDS_FOURCC = 0x00000004;
17
- const DDS_RGB = 0x00000040;
18
- const DDS_ALPHAPIXELS = 0x00000001;
19
- const DDSCAPS2_CUBEMAP = 0x00000200;
20
- const DDSCAPS2_VOLUME = 0x00200000;
21
- const DDS_CUBE_FACE_FLAGS = Object.freeze([["positive-x", 0x00000400], ["negative-x", 0x00000800], ["positive-y", 0x00001000], ["negative-y", 0x00002000], ["positive-z", 0x00004000], ["negative-z", 0x00008000]]);
22
- const FOURCC_PIXEL_FORMATS = Object.freeze({
23
- DXT1: "bc1-rgba-unorm",
24
- DXT3: "bc2-rgba-unorm",
25
- DXT5: "bc3-rgba-unorm",
26
- ATI1: "bc4-r-unorm",
27
- BC4U: "bc4-r-unorm",
28
- BC4S: "bc4-r-snorm",
29
- ATI2: "bc5-rg-unorm",
30
- BC5U: "bc5-rg-unorm",
31
- BC5S: "bc5-rg-snorm"
32
- });
33
- const DXGI_PIXEL_FORMATS = Object.freeze({
34
- 2: "rgba32float",
35
- 6: "rgb32float",
36
- 10: "rgba16float",
37
- 16: "rg32float",
38
- 41: "r32float",
39
- 54: "r16float",
40
- 28: "rgba8unorm",
41
- 29: "rgba8unorm-srgb",
42
- 49: "rg8unorm",
43
- 61: "r8unorm",
44
- 71: "bc1-rgba-unorm",
45
- 72: "bc1-rgba-unorm-srgb",
46
- 74: "bc2-rgba-unorm",
47
- 75: "bc2-rgba-unorm-srgb",
48
- 77: "bc3-rgba-unorm",
49
- 78: "bc3-rgba-unorm-srgb",
50
- 80: "bc4-r-unorm",
51
- 81: "bc4-r-snorm",
52
- 83: "bc5-rg-unorm",
53
- 84: "bc5-rg-snorm",
54
- 87: "bgra8unorm",
55
- 88: "bgrx8unorm",
56
- 91: "bgra8unorm-srgb",
57
- 93: "bgrx8unorm-srgb",
58
- 95: "bc6h-rgb-ufloat",
59
- 96: "bc6h-rgb-float",
60
- 98: "bc7-rgba-unorm",
61
- 99: "bc7-rgba-unorm-srgb"
62
- });
63
- const COMPRESSED_PIXEL_FORMATS = new Set(["bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"]);
64
- const DEBUG_OUTPUTS = Object.freeze({
65
- png: "pngJson",
66
- jpeg: "jpegJson",
67
- jpg: "jpegJson",
68
- tga: "tgaJson",
69
- dds: "ddsJson"
70
- });
71
- function normalizeValues(base = DEFAULT_VALUES, options = {}, readerName = "CjsImageFormat") {
72
- const values = {
73
- ...DEFAULT_VALUES,
74
- ...(base || {}),
75
- ...(options || {})
76
- };
77
- values.inputType = normalizeInputType(values.inputType);
78
- values.emit = normalizeEmit(values.emit, values.inputType, readerName);
79
- return values;
80
- }
81
- function normalizeInputType(inputType) {
82
- if (!inputType) return "";
83
- const value = String(inputType).replace(/^\./u, "").toLowerCase();
84
- return value === "jpg" ? "jpeg" : value;
85
- }
86
- function normalizeEmit(emit, inputType, readerName) {
87
- if (emit === undefined || emit === null) return OUTPUT_RAW;
88
- if (emit === OUTPUT_JSON && inputType) return DEBUG_OUTPUTS[inputType] || OUTPUT_JSON;
89
- if ([OUTPUT_IMAGE, OUTPUT_TEXTURE, OUTPUT_RGBA, OUTPUT_RAW, OUTPUT_JSON].includes(emit)) return emit;
90
- if (Object.values(DEBUG_OUTPUTS).includes(emit)) return emit;
91
- throw new TypeError(`${readerName}: unknown emit value ${JSON.stringify(emit)}`);
92
- }
93
- function toBytes(input) {
94
- if (input instanceof Uint8Array) return input;
95
- if (input instanceof ArrayBuffer) return new Uint8Array(input);
96
- if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
97
- throw new TypeError("Image input must be Uint8Array, ArrayBuffer, or DataView");
98
- }
99
- function inspectWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
100
- const bytes = toBytes(input);
101
- const detected = inspectBytes(bytes);
102
- const sourceFormat = expectedType || values.inputType || detected.sourceFormat;
103
- if (expectedType && detected.sourceFormat && detected.sourceFormat !== expectedType) {
104
- throw new TypeError(`CjsFormat${capitalize(expectedType)}: expected ${expectedType}, got ${detected.sourceFormat}`);
105
- }
106
- return {
107
- payloadType: sourceFormat === "dds" ? "texture" : "image",
108
- mediaTypes: sourceFormat === "dds" ? ["texture", "image"] : ["image"],
109
- sourceFormat,
110
- byteLength: bytes.byteLength,
111
- ...detected
112
- };
113
- }
114
- function isSupportedWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
115
- try {
116
- const metadata = inspectWithValues(input, values, expectedType);
117
- const hasCompleteTextureData = metadata.isDataComplete !== false;
118
- const canEmitTexture = metadata.sourceFormat === "dds" && metadata.dataOffset > 0 && !!metadata.pixelFormat && hasCompleteTextureData;
119
- const canEmitRgba = canDecodeDdsToRgba(metadata) && hasCompleteTextureData;
120
- const textureUnsupportedReason = !metadata.pixelFormat ? "DDS texture format is not recognized yet." : !hasCompleteTextureData ? `DDS texture data is truncated: expected ${metadata.expectedDataBytes} bytes, got ${metadata.dataBytes}.` : "";
121
- const variants = metadata.sourceFormat === "dds" ? [{
122
- kind: "compressed",
123
- payloadType: "texture",
124
- codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
125
- supported: canEmitTexture && metadata.isCompressed,
126
- nativeOnly: !!metadata.nativeTextureOnly,
127
- reason: metadata.isCompressed ? textureUnsupportedReason : "DDS is not block-compressed."
128
- }, {
129
- kind: "texture",
130
- payloadType: "texture",
131
- codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
132
- supported: canEmitTexture,
133
- reason: canEmitTexture ? "" : textureUnsupportedReason
134
- }, {
135
- kind: "rgba",
136
- payloadType: "rgba",
137
- codec: canEmitRgba ? isFloatPixelFormat(metadata.pixelFormat) ? "rgba32float" : "rgba8unorm" : "rgba8unorm",
138
- supported: canEmitRgba,
139
- reason: canEmitRgba ? "" : textureUnsupportedReason || (metadata.nativeTextureOnly ? `${metadata.pixelFormat} is available as a native compressed texture only; register a software decoder or alternate representation for RGBA fallback.` : "DDS RGBA decode is not implemented for this pixel format.")
140
- }, {
141
- kind: "raw",
142
- payloadType: "raw",
143
- codec: "dds",
144
- supported: true
145
- }] : [{
146
- kind: "rgba",
147
- codec: "rgba8",
148
- supported: false,
149
- reason: `${metadata.sourceFormat.toUpperCase()} RGBA decode is not implemented yet.`
150
- }];
151
- const preferredVariant = variants.find(v => v.supported && v.kind !== "raw") || variants.find(v => v.supported);
152
- return {
153
- format: metadata.sourceFormat,
154
- source: values.source || "buffer",
155
- supported: metadata.sourceFormat ? variants.some(v => v.supported && v.kind !== "raw") ? "full" : variants.some(v => v.supported) ? "partial" : "none" : "none",
156
- confidence: metadata.sourceFormat ? 1 : 0,
157
- preferred: preferredVariant?.codec || "",
158
- reason: metadata.sourceFormat ? "Container/header recognized." : "Unrecognized image format.",
159
- metadata,
160
- variants,
161
- warnings: [],
162
- errors: []
163
- };
164
- } catch (error) {
165
- return {
166
- format: expectedType || values.inputType || "",
167
- source: values.source || "buffer",
168
- supported: "none",
169
- confidence: 0,
170
- preferred: "",
171
- reason: error.message,
172
- metadata: null,
173
- variants: [],
174
- warnings: [],
175
- errors: [error.message]
176
- };
177
- }
178
- }
179
- function readWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
180
- const bytes = toBytes(input);
181
- const metadata = inspectWithValues(bytes, values, expectedType);
182
- if (values.emit === OUTPUT_RAW) {
183
- return {
184
- payloadType: "raw",
185
- sourceFormat: metadata.sourceFormat,
186
- metadata,
187
- bytes
188
- };
189
- }
190
- if (values.emit === OUTPUT_JSON || values.emit === DEBUG_OUTPUTS[metadata.sourceFormat]) {
191
- return metadata;
192
- }
193
- if (values.emit === OUTPUT_TEXTURE && metadata.sourceFormat === "dds") {
194
- return readDdsTexture(bytes, metadata);
195
- }
196
- if ((values.emit === OUTPUT_RGBA || values.emit === OUTPUT_IMAGE) && metadata.sourceFormat === "dds") {
197
- return readDdsToRgba(bytes, metadata);
198
- }
199
- const error = new Error(`${metadata.sourceFormat}: emit "${values.emit}" is not implemented yet`);
200
- error.code = "CJS_FORMAT_OUTPUT_NOT_IMPLEMENTED";
201
- error.sourceFormat = metadata.sourceFormat;
202
- error.emit = values.emit;
203
- throw error;
204
- }
205
- function toJsonValue(value) {
206
- if (value instanceof Uint8Array) {
207
- return {
208
- byteLength: value.byteLength
209
- };
210
- }
211
- if (Array.isArray(value)) return value.map(toJsonValue);
212
- if (value && typeof value === "object") {
213
- const output = {};
214
- for (const [key, entry] of Object.entries(value)) output[key] = toJsonValue(entry);
215
- return output;
216
- }
217
- return value;
218
- }
219
- function inspectBytes(bytes) {
220
- if (isPNG(bytes)) return inspectPNG(bytes);
221
- if (isJPEG(bytes)) return inspectJPEG(bytes);
222
- if (isDDS(bytes)) return inspectDDS(bytes);
223
- if (isTGA(bytes)) return inspectTGA(bytes);
224
- return {
225
- sourceFormat: "",
226
- width: 0,
227
- height: 0
228
- };
229
- }
230
- function isPNG(bytes) {
231
- return bytes.byteLength >= 24 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a;
232
- }
233
- function isJPEG(bytes) {
234
- return bytes.byteLength >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8;
235
- }
236
- function isDDS(bytes) {
237
- return bytes.byteLength >= DDS_HEADER_SIZE + 4 && bytes[0] === 0x44 && bytes[1] === 0x44 && bytes[2] === 0x53 && bytes[3] === 0x20 && readU32LE(bytes, 4) === DDS_HEADER_SIZE;
238
- }
239
- function isTGA(bytes) {
240
- if (bytes.byteLength < 18) return false;
241
- const imageType = bytes[2],
242
- width = readU16LE(bytes, 12),
243
- height = readU16LE(bytes, 14),
244
- bpp = bytes[16];
245
- return width > 0 && height > 0 && [1, 2, 3, 9, 10, 11].includes(imageType) && [8, 16, 24, 32].includes(bpp);
246
- }
247
- function inspectPNG(bytes) {
248
- const colorType = bytes[25],
249
- bitDepth = bytes[24];
250
- return {
251
- sourceFormat: "png",
252
- width: readU32BE(bytes, 16),
253
- height: readU32BE(bytes, 20),
254
- bitDepth,
255
- colorType,
256
- channels: pngChannels(colorType),
257
- pixelFormat: `png-${bitDepth}-${colorType}`,
258
- isCompressed: false
259
- };
260
- }
261
- function inspectJPEG(bytes) {
262
- let offset = 2;
263
- while (offset + 9 < bytes.byteLength) {
264
- if (bytes[offset] !== 0xff) {
265
- offset++;
266
- continue;
267
- }
268
- const marker = bytes[offset + 1],
269
- length = readU16BE(bytes, offset + 2);
270
- if ([0xc0, 0xc1, 0xc2, 0xc3].includes(marker)) {
271
- return {
272
- sourceFormat: "jpeg",
273
- width: readU16BE(bytes, offset + 7),
274
- height: readU16BE(bytes, offset + 5),
275
- channels: bytes[offset + 9],
276
- marker,
277
- pixelFormat: "jpeg-ycbcr",
278
- isCompressed: true
279
- };
280
- }
281
- offset += Math.max(length + 2, 2);
282
- }
283
- return {
284
- sourceFormat: "jpeg",
285
- width: 0,
286
- height: 0,
287
- channels: 0,
288
- pixelFormat: "jpeg",
289
- isCompressed: true
290
- };
291
- }
292
- function inspectDDS(bytes) {
293
- const headerSize = readU32LE(bytes, 4);
294
- const pfOffset = DDS_PIXELFORMAT_OFFSET;
295
- const pfFlags = readU32LE(bytes, pfOffset + 4);
296
- const fourCcCode = readU32LE(bytes, pfOffset + 8);
297
- const fourCc = String.fromCharCode(bytes[84], bytes[85], bytes[86], bytes[87]).replace(/\0+$/u, "");
298
- const rgbBitCount = readU32LE(bytes, pfOffset + 12);
299
- const rBitMask = readU32LE(bytes, pfOffset + 16);
300
- const gBitMask = readU32LE(bytes, pfOffset + 20);
301
- const bBitMask = readU32LE(bytes, pfOffset + 24);
302
- const aBitMask = readU32LE(bytes, pfOffset + 28);
303
- const caps2 = readU32LE(bytes, 112);
304
- const isCube = !!(caps2 & DDSCAPS2_CUBEMAP);
305
- const isVolume = !!(caps2 & DDSCAPS2_VOLUME);
306
- const cubeFaces = getDdsCubeFaces(caps2);
307
- const hasDx10 = fourCc === "DX10" && bytes.byteLength >= 148;
308
- const dxgiFormat = hasDx10 ? readU32LE(bytes, 128) : 0;
309
- const resourceDimension = hasDx10 ? readU32LE(bytes, 132) : 0;
310
- const arraySize = hasDx10 ? readU32LE(bytes, 140) : 1;
311
- const dataOffset = hasDx10 ? 148 : 128;
312
- const pixelFormat = getDdsPixelFormat({
313
- pfFlags,
314
- fourCc,
315
- fourCcCode,
316
- rgbBitCount,
317
- rBitMask,
318
- gBitMask,
319
- bBitMask,
320
- aBitMask,
321
- dxgiFormat
322
- });
323
- const width = readU32LE(bytes, 16);
324
- const height = readU32LE(bytes, 12);
325
- const depth = readU32LE(bytes, 24);
326
- const mipCount = Math.max(readU32LE(bytes, 28), 1);
327
- const dataBytes = Math.max(bytes.byteLength - dataOffset, 0);
328
- const expectedDataBytes = pixelFormat ? getDdsExpectedDataBytes({
329
- pixelFormat,
330
- width,
331
- height,
332
- depth,
333
- mipCount,
334
- arraySize,
335
- isCube,
336
- isVolume
337
- }) : null;
338
- return {
339
- sourceFormat: "dds",
340
- headerSize,
341
- width,
342
- height,
343
- depth,
344
- mipCount,
345
- pitchOrLinearSize: readU32LE(bytes, 20),
346
- pfFlags,
347
- fourCc,
348
- fourCcCode,
349
- rgbBitCount,
350
- rBitMask,
351
- gBitMask,
352
- bBitMask,
353
- aBitMask,
354
- caps2,
355
- hasDx10,
356
- dxgiFormat,
357
- resourceDimension,
358
- arraySize,
359
- dataOffset,
360
- dataBytes,
361
- expectedDataBytes,
362
- isDataComplete: expectedDataBytes !== null && dataBytes >= expectedDataBytes,
363
- missingDataBytes: expectedDataBytes === null ? null : Math.max(expectedDataBytes - dataBytes, 0),
364
- extraDataBytes: expectedDataBytes === null ? null : Math.max(dataBytes - expectedDataBytes, 0),
365
- faces: isCube ? 6 : 1,
366
- cubeFaces,
367
- isCubeComplete: isCube && cubeFaces.length === 6,
368
- isCube,
369
- isVolume,
370
- dimension: isVolume ? "3d" : isCube ? "cube" : "2d",
371
- pixelFormat,
372
- textureFormat: pixelFormat || fourCc || "dds-legacy",
373
- isCompressed: COMPRESSED_PIXEL_FORMATS.has(pixelFormat),
374
- nativeTextureOnly: COMPRESSED_PIXEL_FORMATS.has(pixelFormat) && !canDecodeDdsToRgba({
375
- pixelFormat
376
- }),
377
- hasAlpha: !!(pfFlags & DDS_ALPHAPIXELS) || /rgba|bgra/u.test(pixelFormat),
378
- hasMipMaps: readU32LE(bytes, 28) > 1
379
- };
380
- }
381
- function readDdsTexture(bytes, metadata) {
382
- if (!metadata.pixelFormat) {
383
- const error = new Error("dds: texture format is not recognized yet");
384
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
385
- error.sourceFormat = metadata.sourceFormat;
386
- throw error;
387
- }
388
- const subresources = buildDdsSubresources(bytes, metadata);
389
- return {
390
- payloadType: OUTPUT_TEXTURE,
391
- sourceFormat: "dds",
392
- width: metadata.width,
393
- height: metadata.height,
394
- depth: metadata.depth,
395
- faces: metadata.faces,
396
- cubeFaces: metadata.cubeFaces,
397
- isCubeComplete: metadata.isCubeComplete,
398
- dimension: metadata.dimension,
399
- mipCount: metadata.mipCount,
400
- arraySize: metadata.arraySize,
401
- pixelFormat: metadata.pixelFormat,
402
- isCompressed: metadata.isCompressed,
403
- dataBytes: metadata.dataBytes,
404
- expectedDataBytes: metadata.expectedDataBytes,
405
- isDataComplete: metadata.isDataComplete,
406
- missingDataBytes: metadata.missingDataBytes,
407
- extraDataBytes: metadata.extraDataBytes,
408
- subresources,
409
- metadata,
410
- data: bytes.subarray(metadata.dataOffset, metadata.dataOffset + subresources.reduce((sum, entry) => sum + entry.byteLength, 0))
411
- };
412
- }
413
- function canDecodeDdsToRgba(metadata) {
414
- return ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float", "rgba8unorm", "rgba8unorm-srgb", "bgra8unorm", "bgra8unorm-srgb", "bgrx8unorm", "bgrx8unorm-srgb", "bgr8unorm", "rg8unorm", "r8unorm", "bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"].includes(metadata.pixelFormat);
415
- }
416
- function readDdsToRgba(bytes, metadata) {
417
- if (!canDecodeDdsToRgba(metadata)) {
418
- const error = new Error(`dds: RGBA decode is not implemented for ${metadata.pixelFormat || "unknown"}`);
419
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
420
- error.sourceFormat = metadata.sourceFormat;
421
- throw error;
422
- }
423
- const subresource = buildDdsSubresources(bytes, metadata)[0];
424
- const source = bytes.subarray(metadata.dataOffset + subresource.offset, metadata.dataOffset + subresource.offset + subresource.byteLength);
425
- const rgba = metadata.pixelFormat.startsWith("bc1-") ? decodeBc1(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc2-") ? decodeBc2(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc3-") ? decodeBc3(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc4-") ? decodeBc4(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc5-") ? decodeBc5(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc6h-") ? decodeBc6h(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat === "bc6h-rgb-float") : metadata.pixelFormat.startsWith("bc7-") ? decodeBc7(source, metadata.width, metadata.height, subresource.rowPitch) : isFloatPixelFormat(metadata.pixelFormat) ? decodeFloatUncompressed(source, metadata) : decodeUncompressed(source, metadata);
426
- const isFloat = isFloatPixelFormat(metadata.pixelFormat);
427
- return {
428
- payloadType: OUTPUT_RGBA,
429
- sourceFormat: "dds",
430
- width: metadata.width,
431
- height: metadata.height,
432
- pixelFormat: isFloat ? "rgba32float" : "rgba8unorm",
433
- strideBytes: metadata.width * (isFloat ? 16 : 4),
434
- origin: "top-left",
435
- colorSpace: isFloat ? "linear" : metadata.pixelFormat.endsWith("-srgb") ? "srgb" : "unknown",
436
- alphaMode: metadata.hasAlpha ? "straight" : "opaque",
437
- metadata,
438
- data: rgba
439
- };
440
- }
441
- function buildDdsSubresources(bytes, metadata) {
442
- const subresources = [];
443
- let offset = 0;
444
- const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
445
- for (let layer = 0; layer < layers; layer++) {
446
- const arrayIndex = metadata.isCube ? Math.floor(layer / 6) : layer;
447
- const face = metadata.isCube ? layer % 6 : 0;
448
- for (let mip = 0; mip < metadata.mipCount; mip++) {
449
- const width = Math.max(metadata.width >> mip, 1);
450
- const height = Math.max(metadata.height >> mip, 1);
451
- const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
452
- const layout = getDdsLevelLayout(metadata.pixelFormat, width, height, depth);
453
- if (offset + layout.byteLength > bytes.byteLength - metadata.dataOffset) {
454
- const error = new Error("dds: texture data is truncated");
455
- error.code = "CJS_FORMAT_TRUNCATED";
456
- error.sourceFormat = metadata.sourceFormat;
457
- error.expectedDataBytes = metadata.expectedDataBytes;
458
- error.dataBytes = metadata.dataBytes;
459
- error.missingDataBytes = metadata.missingDataBytes;
460
- throw error;
461
- }
462
- subresources.push({
463
- mip,
464
- layer,
465
- arrayIndex,
466
- face,
467
- offset,
468
- byteLength: layout.byteLength,
469
- rowPitch: layout.rowPitch,
470
- slicePitch: layout.slicePitch,
471
- width,
472
- height,
473
- depth
474
- });
475
- offset += layout.byteLength;
476
- }
477
- }
478
- return subresources;
479
- }
480
- function getDdsExpectedDataBytes(metadata) {
481
- let total = 0;
482
- const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
483
- for (let layer = 0; layer < layers; layer++) {
484
- for (let mip = 0; mip < metadata.mipCount; mip++) {
485
- const width = Math.max(metadata.width >> mip, 1);
486
- const height = Math.max(metadata.height >> mip, 1);
487
- const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
488
- total += getDdsLevelLayout(metadata.pixelFormat, width, height, depth).byteLength;
489
- }
490
- }
491
- return total;
492
- }
493
- function getDdsCubeFaces(caps2) {
494
- if (!(caps2 & DDSCAPS2_CUBEMAP)) {
495
- return [];
496
- }
497
- return DDS_CUBE_FACE_FLAGS.filter(entry => !!(caps2 & entry[1])).map(entry => entry[0]);
498
- }
499
- function getDdsLevelLayout(pixelFormat, width, height, depth) {
500
- const blockBytes = pixelFormat.startsWith("bc1-") || pixelFormat.startsWith("bc4-") ? 8 : pixelFormat.startsWith("bc") ? 16 : 0;
501
- if (blockBytes) {
502
- const blocksWide = Math.max(Math.ceil(width / 4), 1);
503
- const blocksHigh = Math.max(Math.ceil(height / 4), 1);
504
- const rowPitch = blocksWide * blockBytes;
505
- const slicePitch = rowPitch * blocksHigh;
506
- return {
507
- rowPitch,
508
- slicePitch,
509
- byteLength: slicePitch * depth
510
- };
511
- }
512
- const bytesPerPixel = {
513
- "rgba32float": 16,
514
- "rgb32float": 12,
515
- "rgba16float": 8,
516
- "rg32float": 8,
517
- "r32float": 4,
518
- "r16float": 2,
519
- "rgba8unorm": 4,
520
- "rgba8unorm-srgb": 4,
521
- "bgra8unorm": 4,
522
- "bgra8unorm-srgb": 4,
523
- "bgrx8unorm": 4,
524
- "bgrx8unorm-srgb": 4,
525
- "bgr8unorm": 3,
526
- "rg8unorm": 2,
527
- "r8unorm": 1
528
- }[pixelFormat];
529
- if (!bytesPerPixel) {
530
- const error = new Error(`dds: cannot calculate subresource layout for ${pixelFormat || "unknown"}`);
531
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
532
- throw error;
533
- }
534
- const rowPitch = width * bytesPerPixel;
535
- const slicePitch = rowPitch * height;
536
- return {
537
- rowPitch,
538
- slicePitch,
539
- byteLength: slicePitch * depth
540
- };
541
- }
542
- function decodeUncompressed(source, metadata) {
543
- const rgba = new Uint8Array(metadata.width * metadata.height * 4);
544
- const bytesPerPixel = metadata.pixelFormat === "bgr8unorm" ? 3 : metadata.pixelFormat === "rg8unorm" ? 2 : metadata.pixelFormat === "r8unorm" ? 1 : 4;
545
- for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
546
- const sourceOffset = pixel * bytesPerPixel;
547
- const outputOffset = pixel * 4;
548
- if (metadata.pixelFormat.startsWith("bgra") || metadata.pixelFormat.startsWith("bgrx")) {
549
- rgba[outputOffset] = source[sourceOffset + 2];
550
- rgba[outputOffset + 1] = source[sourceOffset + 1];
551
- rgba[outputOffset + 2] = source[sourceOffset];
552
- rgba[outputOffset + 3] = metadata.pixelFormat.startsWith("bgrx") ? 255 : source[sourceOffset + 3];
553
- } else if (metadata.pixelFormat === "bgr8unorm") {
554
- rgba[outputOffset] = source[sourceOffset + 2];
555
- rgba[outputOffset + 1] = source[sourceOffset + 1];
556
- rgba[outputOffset + 2] = source[sourceOffset];
557
- rgba[outputOffset + 3] = 255;
558
- } else if (metadata.pixelFormat === "rg8unorm") {
559
- rgba[outputOffset] = source[sourceOffset];
560
- rgba[outputOffset + 1] = source[sourceOffset + 1];
561
- rgba[outputOffset + 2] = 0;
562
- rgba[outputOffset + 3] = 255;
563
- } else {
564
- rgba[outputOffset] = source[sourceOffset];
565
- rgba[outputOffset + 1] = bytesPerPixel > 1 ? source[sourceOffset + 1] : source[sourceOffset];
566
- rgba[outputOffset + 2] = bytesPerPixel > 2 ? source[sourceOffset + 2] : source[sourceOffset];
567
- rgba[outputOffset + 3] = bytesPerPixel > 3 ? source[sourceOffset + 3] : 255;
568
- }
569
- }
570
- return rgba;
571
- }
572
- function decodeFloatUncompressed(source, metadata) {
573
- const rgba = new Float32Array(metadata.width * metadata.height * 4);
574
- const view = new DataView(source.buffer, source.byteOffset, source.byteLength);
575
- const bytesPerPixel = {
576
- "rgba32float": 16,
577
- "rgb32float": 12,
578
- "rgba16float": 8,
579
- "rg32float": 8,
580
- "r32float": 4,
581
- "r16float": 2
582
- }[metadata.pixelFormat];
583
- for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
584
- const sourceOffset = pixel * bytesPerPixel;
585
- const outputOffset = pixel * 4;
586
- let red, green, blue, alpha;
587
- if (metadata.pixelFormat === "rgba32float") {
588
- red = view.getFloat32(sourceOffset, true);
589
- green = view.getFloat32(sourceOffset + 4, true);
590
- blue = view.getFloat32(sourceOffset + 8, true);
591
- alpha = view.getFloat32(sourceOffset + 12, true);
592
- } else if (metadata.pixelFormat === "rgb32float") {
593
- red = view.getFloat32(sourceOffset, true);
594
- green = view.getFloat32(sourceOffset + 4, true);
595
- blue = view.getFloat32(sourceOffset + 8, true);
596
- alpha = 1;
597
- } else if (metadata.pixelFormat === "rgba16float") {
598
- red = halfToFloat(view.getUint16(sourceOffset, true));
599
- green = halfToFloat(view.getUint16(sourceOffset + 2, true));
600
- blue = halfToFloat(view.getUint16(sourceOffset + 4, true));
601
- alpha = halfToFloat(view.getUint16(sourceOffset + 6, true));
602
- } else if (metadata.pixelFormat === "rg32float") {
603
- red = view.getFloat32(sourceOffset, true);
604
- green = view.getFloat32(sourceOffset + 4, true);
605
- blue = 0;
606
- alpha = 1;
607
- } else {
608
- red = metadata.pixelFormat === "r16float" ? halfToFloat(view.getUint16(sourceOffset, true)) : view.getFloat32(sourceOffset, true);
609
- green = red;
610
- blue = red;
611
- alpha = 1;
612
- }
613
- rgba[outputOffset] = red;
614
- rgba[outputOffset + 1] = green;
615
- rgba[outputOffset + 2] = blue;
616
- rgba[outputOffset + 3] = alpha;
617
- }
618
- return rgba;
619
- }
620
- function isFloatPixelFormat(pixelFormat) {
621
- return typeof pixelFormat === "string" && (pixelFormat.startsWith("bc6h-") || ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float"].includes(pixelFormat));
622
- }
623
- function halfToFloat(value) {
624
- const sign = value & 0x8000 ? -1 : 1;
625
- const exponent = value >>> 10 & 0x1f;
626
- const mantissa = value & 0x03ff;
627
- if (exponent === 0) {
628
- return sign * Math.pow(2, -14) * (mantissa / 1024);
629
- }
630
- if (exponent === 0x1f) {
631
- return mantissa === 0 ? sign * Infinity : NaN;
632
- }
633
- return sign * Math.pow(2, exponent - 15) * (1 + mantissa / 1024);
634
- }
635
- function decodeBc1(source, width, height, rowPitch) {
636
- const rgba = new Uint8Array(width * height * 4);
637
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
638
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
639
- const blockOffset = blockY * rowPitch + blockX * 8;
640
- const c0 = source[blockOffset] | source[blockOffset + 1] << 8;
641
- const c1 = source[blockOffset + 2] | source[blockOffset + 3] << 8;
642
- const colors = decodeBc1Colors(c0, c1);
643
- const indices = source[blockOffset + 4] | source[blockOffset + 5] << 8 | source[blockOffset + 6] << 16 | source[blockOffset + 7] << 24;
644
- for (let y = 0; y < 4; y++) {
645
- for (let x = 0; x < 4; x++) {
646
- const outputX = blockX * 4 + x;
647
- const outputY = blockY * 4 + y;
648
- if (outputX >= width || outputY >= height) continue;
649
- const color = colors[indices >>> 2 * (y * 4 + x) & 3];
650
- rgba.set(color, (outputY * width + outputX) * 4);
651
- }
652
- }
653
- }
654
- }
655
- return rgba;
656
- }
657
- function decodeBc2(source, width, height, rowPitch) {
658
- return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
659
- const alphaNibble = source[blockOffset + y * 2 + Math.floor(x / 2)] >>> x % 2 * 4 & 0x0f;
660
- return alphaNibble * 17;
661
- }, 8);
662
- }
663
- function decodeBc3(source, width, height, rowPitch) {
664
- return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
665
- const alpha0 = source[blockOffset],
666
- alpha1 = source[blockOffset + 1];
667
- const alphaValues = alpha0 > alpha1 ? [alpha0, alpha1, Math.round((6 * alpha0 + alpha1) / 7), Math.round((5 * alpha0 + 2 * alpha1) / 7), Math.round((4 * alpha0 + 3 * alpha1) / 7), Math.round((3 * alpha0 + 4 * alpha1) / 7), Math.round((2 * alpha0 + 5 * alpha1) / 7), Math.round((alpha0 + 6 * alpha1) / 7)] : [alpha0, alpha1, Math.round((4 * alpha0 + alpha1) / 5), Math.round((3 * alpha0 + 2 * alpha1) / 5), Math.round((2 * alpha0 + 3 * alpha1) / 5), Math.round((alpha0 + 4 * alpha1) / 5), 0, 255];
668
- let alphaBits = 0n;
669
- for (let i = 0; i < 6; i++) alphaBits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
670
- return alphaValues[Number(alphaBits >> BigInt((y * 4 + x) * 3) & 7n)];
671
- }, 8);
672
- }
673
- function decodeBc4(source, width, height, rowPitch, signed) {
674
- const rgba = new Uint8Array(width * height * 4);
675
- forEachBcPixel(width, height, rowPitch, 8, (blockOffset, x, y, outputOffset) => {
676
- const value = decodeBc4Value(source, blockOffset, x, y, signed);
677
- rgba[outputOffset] = value;
678
- rgba[outputOffset + 1] = value;
679
- rgba[outputOffset + 2] = value;
680
- rgba[outputOffset + 3] = 255;
681
- });
682
- return rgba;
683
- }
684
- function decodeBc5(source, width, height, rowPitch, signed) {
685
- const rgba = new Uint8Array(width * height * 4);
686
- forEachBcPixel(width, height, rowPitch, 16, (blockOffset, x, y, outputOffset) => {
687
- rgba[outputOffset] = decodeBc4Value(source, blockOffset, x, y, signed);
688
- rgba[outputOffset + 1] = decodeBc4Value(source, blockOffset + 8, x, y, signed);
689
- rgba[outputOffset + 2] = 0;
690
- rgba[outputOffset + 3] = 255;
691
- });
692
- return rgba;
693
- }
694
- function decodeBc4Value(source, blockOffset, x, y, signed) {
695
- const endpoint0 = signed ? decodeSnormByte(source[blockOffset]) : source[blockOffset];
696
- const endpoint1 = signed ? decodeSnormByte(source[blockOffset + 1]) : source[blockOffset + 1];
697
- const values = endpoint0 > endpoint1 ? [endpoint0, endpoint1, (6 * endpoint0 + endpoint1) / 7, (5 * endpoint0 + 2 * endpoint1) / 7, (4 * endpoint0 + 3 * endpoint1) / 7, (3 * endpoint0 + 4 * endpoint1) / 7, (2 * endpoint0 + 5 * endpoint1) / 7, (endpoint0 + 6 * endpoint1) / 7] : [endpoint0, endpoint1, (4 * endpoint0 + endpoint1) / 5, (3 * endpoint0 + 2 * endpoint1) / 5, (2 * endpoint0 + 3 * endpoint1) / 5, (endpoint0 + 4 * endpoint1) / 5, signed ? -1 : 0, signed ? 1 : 255];
698
- let bits = 0n;
699
- for (let i = 0; i < 6; i++) bits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
700
- const value = values[Number(bits >> BigInt((y * 4 + x) * 3) & 7n)];
701
- return signed ? Math.max(0, Math.min(255, Math.round((value + 1) * 127.5))) : Math.round(value);
702
- }
703
- function decodeSnormByte(value) {
704
- const signed = value > 127 ? value - 256 : value;
705
- return Math.max(-1, signed / 127);
706
- }
707
- function forEachBcPixel(width, height, rowPitch, blockBytes, callback) {
708
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
709
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
710
- const blockOffset = blockY * rowPitch + blockX * blockBytes;
711
- for (let y = 0; y < 4; y++) {
712
- for (let x = 0; x < 4; x++) {
713
- const outputX = blockX * 4 + x;
714
- const outputY = blockY * 4 + y;
715
- if (outputX < width && outputY < height) {
716
- callback(blockOffset, x, y, (outputY * width + outputX) * 4);
717
- }
718
- }
719
- }
720
- }
721
- }
722
- }
723
- function decodeBcAlphaColor(source, width, height, rowPitch, alphaAt, colorOffset = 0) {
724
- const rgba = new Uint8Array(width * height * 4);
725
- for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
726
- for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
727
- const blockOffset = blockY * rowPitch + blockX * 16;
728
- const colorOffsetInBlock = blockOffset + colorOffset;
729
- const c0 = source[colorOffsetInBlock] | source[colorOffsetInBlock + 1] << 8;
730
- const c1 = source[colorOffsetInBlock + 2] | source[colorOffsetInBlock + 3] << 8;
731
- const colors = decodeBc1Colors(c0, c1, true);
732
- const indices = source[colorOffsetInBlock + 4] | source[colorOffsetInBlock + 5] << 8 | source[colorOffsetInBlock + 6] << 16 | source[colorOffsetInBlock + 7] << 24;
733
- for (let y = 0; y < 4; y++) {
734
- for (let x = 0; x < 4; x++) {
735
- const outputX = blockX * 4 + x;
736
- const outputY = blockY * 4 + y;
737
- if (outputX >= width || outputY >= height) continue;
738
- const color = colors[indices >>> 2 * (y * 4 + x) & 3];
739
- const outputOffset = (outputY * width + outputX) * 4;
740
- rgba[outputOffset] = color[0];
741
- rgba[outputOffset + 1] = color[1];
742
- rgba[outputOffset + 2] = color[2];
743
- rgba[outputOffset + 3] = alphaAt(blockOffset, x, y);
744
- }
745
- }
746
- }
747
- }
748
- return rgba;
749
- }
750
- function decodeBc1Colors(c0, c1, forceFourColor = false) {
751
- const first = decode565(c0);
752
- const second = decode565(c1);
753
- const colors = [first, second];
754
- if (c0 > c1 || forceFourColor) {
755
- colors.push([Math.round((2 * first[0] + second[0]) / 3), Math.round((2 * first[1] + second[1]) / 3), Math.round((2 * first[2] + second[2]) / 3), 255], [Math.round((first[0] + 2 * second[0]) / 3), Math.round((first[1] + 2 * second[1]) / 3), Math.round((first[2] + 2 * second[2]) / 3), 255]);
756
- } else {
757
- colors.push([Math.round((first[0] + second[0]) / 2), Math.round((first[1] + second[1]) / 2), Math.round((first[2] + second[2]) / 2), 255], [0, 0, 0, 0]);
758
- }
759
- return colors;
760
- }
761
- function decode565(value) {
762
- return [Math.round((value >>> 11 & 0x1f) * 255 / 31), Math.round((value >>> 5 & 0x3f) * 255 / 63), Math.round((value & 0x1f) * 255 / 31), 255];
763
- }
764
- function getDdsPixelFormat(format) {
765
- if (format.dxgiFormat) return DXGI_PIXEL_FORMATS[format.dxgiFormat] || "";
766
- if (format.pfFlags & DDS_FOURCC && format.fourCc) {
767
- return FOURCC_PIXEL_FORMATS[format.fourCc] || {
768
- 113: "rgba16float",
769
- 116: "rgba32float"
770
- }[format.fourCcCode] || "";
771
- }
772
- if (format.pfFlags & DDS_RGB) {
773
- if (format.rgbBitCount === 32 && format.rBitMask === 0x000000ff && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x00ff0000 && format.aBitMask === 0xff000000) return "rgba8unorm";
774
- if (format.rgbBitCount === 32 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff && format.aBitMask === 0xff000000) return "bgra8unorm";
775
- if (format.rgbBitCount === 24 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff) return "bgr8unorm";
776
- }
777
- return "";
778
- }
779
- function inspectTGA(bytes) {
780
- return {
781
- sourceFormat: "tga",
782
- width: readU16LE(bytes, 12),
783
- height: readU16LE(bytes, 14),
784
- channels: Math.max(bytes[16] / 8, 1),
785
- imageType: bytes[2],
786
- pixelFormat: `tga-${bytes[16]}`,
787
- isCompressed: [9, 10, 11].includes(bytes[2])
788
- };
789
- }
790
- function pngChannels(colorType) {
791
- if (colorType === 0) return 1;
792
- if (colorType === 2) return 3;
793
- if (colorType === 3) return 1;
794
- if (colorType === 4) return 2;
795
- if (colorType === 6) return 4;
796
- return 0;
797
- }
798
- function readU16BE(bytes, offset) {
799
- return bytes[offset] << 8 | bytes[offset + 1];
800
- }
801
- function readU16LE(bytes, offset) {
802
- return bytes[offset] | bytes[offset + 1] << 8;
803
- }
804
- function readU32BE(bytes, offset) {
805
- return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
806
- }
807
- function readU32LE(bytes, offset) {
808
- return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
809
- }
810
- function capitalize(value) {
811
- return value ? value[0].toUpperCase() + value.slice(1) : "Image";
812
- }
813
-
814
- export { DEFAULT_VALUES, OUTPUT_IMAGE, OUTPUT_JSON, OUTPUT_RAW, OUTPUT_RGBA, OUTPUT_TEXTURE, inspectBytes, inspectWithValues, isDDS, isJPEG, isPNG, isSupportedWithValues, isTGA, normalizeEmit, normalizeInputType, normalizeValues, readWithValues, toBytes, toJsonValue };
815
- //# sourceMappingURL=helpers.js.map
1
+ import { decodeBc6h } from './bc6h.js';
2
+ import { decodeBc7 } from './bc7.js';
3
+
4
+ const OUTPUT_IMAGE = "image";
5
+ const OUTPUT_TEXTURE = "texture";
6
+ const OUTPUT_RGBA = "rgba";
7
+ const OUTPUT_RAW = "raw";
8
+ const OUTPUT_JSON = "json";
9
+ const DEFAULT_VALUES = Object.freeze({
10
+ emit: OUTPUT_RAW,
11
+ inputType: "",
12
+ source: ""
13
+ });
14
+ const DDS_HEADER_SIZE = 124;
15
+ const DDS_PIXELFORMAT_OFFSET = 76;
16
+ const DDS_FOURCC = 0x00000004;
17
+ const DDS_RGB = 0x00000040;
18
+ const DDS_ALPHAPIXELS = 0x00000001;
19
+ const DDSCAPS2_CUBEMAP = 0x00000200;
20
+ const DDSCAPS2_VOLUME = 0x00200000;
21
+ const DDS_CUBE_FACE_FLAGS = Object.freeze([["positive-x", 0x00000400], ["negative-x", 0x00000800], ["positive-y", 0x00001000], ["negative-y", 0x00002000], ["positive-z", 0x00004000], ["negative-z", 0x00008000]]);
22
+ const FOURCC_PIXEL_FORMATS = Object.freeze({
23
+ DXT1: "bc1-rgba-unorm",
24
+ DXT3: "bc2-rgba-unorm",
25
+ DXT5: "bc3-rgba-unorm",
26
+ ATI1: "bc4-r-unorm",
27
+ BC4U: "bc4-r-unorm",
28
+ BC4S: "bc4-r-snorm",
29
+ ATI2: "bc5-rg-unorm",
30
+ BC5U: "bc5-rg-unorm",
31
+ BC5S: "bc5-rg-snorm"
32
+ });
33
+ const DXGI_PIXEL_FORMATS = Object.freeze({
34
+ 2: "rgba32float",
35
+ 6: "rgb32float",
36
+ 10: "rgba16float",
37
+ 16: "rg32float",
38
+ 41: "r32float",
39
+ 54: "r16float",
40
+ 28: "rgba8unorm",
41
+ 29: "rgba8unorm-srgb",
42
+ 49: "rg8unorm",
43
+ 61: "r8unorm",
44
+ 71: "bc1-rgba-unorm",
45
+ 72: "bc1-rgba-unorm-srgb",
46
+ 74: "bc2-rgba-unorm",
47
+ 75: "bc2-rgba-unorm-srgb",
48
+ 77: "bc3-rgba-unorm",
49
+ 78: "bc3-rgba-unorm-srgb",
50
+ 80: "bc4-r-unorm",
51
+ 81: "bc4-r-snorm",
52
+ 83: "bc5-rg-unorm",
53
+ 84: "bc5-rg-snorm",
54
+ 87: "bgra8unorm",
55
+ 88: "bgrx8unorm",
56
+ 91: "bgra8unorm-srgb",
57
+ 93: "bgrx8unorm-srgb",
58
+ 95: "bc6h-rgb-ufloat",
59
+ 96: "bc6h-rgb-float",
60
+ 98: "bc7-rgba-unorm",
61
+ 99: "bc7-rgba-unorm-srgb"
62
+ });
63
+ const COMPRESSED_PIXEL_FORMATS = new Set(["bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"]);
64
+ const DEBUG_OUTPUTS = Object.freeze({
65
+ png: "pngJson",
66
+ jpeg: "jpegJson",
67
+ jpg: "jpegJson",
68
+ tga: "tgaJson",
69
+ dds: "ddsJson"
70
+ });
71
+ function normalizeValues(base = DEFAULT_VALUES, options = {}, readerName = "CjsImageFormat") {
72
+ const values = {
73
+ ...DEFAULT_VALUES,
74
+ ...(base || {}),
75
+ ...(options || {})
76
+ };
77
+ values.inputType = normalizeInputType(values.inputType);
78
+ values.emit = normalizeEmit(values.emit, values.inputType, readerName);
79
+ return values;
80
+ }
81
+ function normalizeInputType(inputType) {
82
+ if (!inputType) return "";
83
+ const value = String(inputType).replace(/^\./u, "").toLowerCase();
84
+ return value === "jpg" ? "jpeg" : value;
85
+ }
86
+ function normalizeEmit(emit, inputType, readerName) {
87
+ if (emit === undefined || emit === null) return OUTPUT_RAW;
88
+ if (emit === OUTPUT_JSON && inputType) return DEBUG_OUTPUTS[inputType] || OUTPUT_JSON;
89
+ if ([OUTPUT_IMAGE, OUTPUT_TEXTURE, OUTPUT_RGBA, OUTPUT_RAW, OUTPUT_JSON].includes(emit)) return emit;
90
+ if (Object.values(DEBUG_OUTPUTS).includes(emit)) return emit;
91
+ throw new TypeError(`${readerName}: unknown emit value ${JSON.stringify(emit)}`);
92
+ }
93
+ function toBytes(input) {
94
+ if (input instanceof Uint8Array) return input;
95
+ if (input instanceof ArrayBuffer) return new Uint8Array(input);
96
+ if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
97
+ throw new TypeError("Image input must be Uint8Array, ArrayBuffer, or DataView");
98
+ }
99
+ function inspectWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
100
+ const bytes = toBytes(input);
101
+ const detected = inspectBytes(bytes);
102
+ const sourceFormat = expectedType || values.inputType || detected.sourceFormat;
103
+ if (expectedType && detected.sourceFormat && detected.sourceFormat !== expectedType) {
104
+ throw new TypeError(`CjsFormat${capitalize(expectedType)}: expected ${expectedType}, got ${detected.sourceFormat}`);
105
+ }
106
+ return {
107
+ payloadType: sourceFormat === "dds" ? "texture" : "image",
108
+ mediaTypes: sourceFormat === "dds" ? ["texture", "image"] : ["image"],
109
+ sourceFormat,
110
+ byteLength: bytes.byteLength,
111
+ ...detected
112
+ };
113
+ }
114
+ function isSupportedWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
115
+ try {
116
+ const metadata = inspectWithValues(input, values, expectedType);
117
+ const hasCompleteTextureData = metadata.isDataComplete !== false;
118
+ const canEmitTexture = metadata.sourceFormat === "dds" && metadata.dataOffset > 0 && !!metadata.pixelFormat && hasCompleteTextureData;
119
+ const canEmitRgba = canDecodeDdsToRgba(metadata) && hasCompleteTextureData;
120
+ const textureUnsupportedReason = !metadata.pixelFormat ? "DDS texture format is not recognized yet." : !hasCompleteTextureData ? `DDS texture data is truncated: expected ${metadata.expectedDataBytes} bytes, got ${metadata.dataBytes}.` : "";
121
+ const variants = metadata.sourceFormat === "dds" ? [{
122
+ kind: "compressed",
123
+ payloadType: "texture",
124
+ codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
125
+ supported: canEmitTexture && metadata.isCompressed,
126
+ nativeOnly: !!metadata.nativeTextureOnly,
127
+ reason: metadata.isCompressed ? textureUnsupportedReason : "DDS is not block-compressed."
128
+ }, {
129
+ kind: "texture",
130
+ payloadType: "texture",
131
+ codec: metadata.pixelFormat || metadata.textureFormat || "unknown",
132
+ supported: canEmitTexture,
133
+ reason: canEmitTexture ? "" : textureUnsupportedReason
134
+ }, {
135
+ kind: "rgba",
136
+ payloadType: "rgba",
137
+ codec: canEmitRgba ? isFloatPixelFormat(metadata.pixelFormat) ? "rgba32float" : "rgba8unorm" : "rgba8unorm",
138
+ supported: canEmitRgba,
139
+ reason: canEmitRgba ? "" : textureUnsupportedReason || (metadata.nativeTextureOnly ? `${metadata.pixelFormat} is available as a native compressed texture only; register a software decoder or alternate representation for RGBA fallback.` : "DDS RGBA decode is not implemented for this pixel format.")
140
+ }, {
141
+ kind: "raw",
142
+ payloadType: "raw",
143
+ codec: "dds",
144
+ supported: true
145
+ }] : [{
146
+ kind: "rgba",
147
+ codec: "rgba8",
148
+ supported: false,
149
+ reason: `${metadata.sourceFormat.toUpperCase()} RGBA decode is not implemented yet.`
150
+ }];
151
+ const preferredVariant = variants.find(v => v.supported && v.kind !== "raw") || variants.find(v => v.supported);
152
+ return {
153
+ format: metadata.sourceFormat,
154
+ source: values.source || "buffer",
155
+ supported: metadata.sourceFormat ? variants.some(v => v.supported && v.kind !== "raw") ? "full" : variants.some(v => v.supported) ? "partial" : "none" : "none",
156
+ confidence: metadata.sourceFormat ? 1 : 0,
157
+ preferred: preferredVariant?.codec || "",
158
+ reason: metadata.sourceFormat ? "Container/header recognized." : "Unrecognized image format.",
159
+ metadata,
160
+ variants,
161
+ warnings: [],
162
+ errors: []
163
+ };
164
+ } catch (error) {
165
+ return {
166
+ format: expectedType || values.inputType || "",
167
+ source: values.source || "buffer",
168
+ supported: "none",
169
+ confidence: 0,
170
+ preferred: "",
171
+ reason: error.message,
172
+ metadata: null,
173
+ variants: [],
174
+ warnings: [],
175
+ errors: [error.message]
176
+ };
177
+ }
178
+ }
179
+ function readWithValues(input, values = DEFAULT_VALUES, expectedType = "") {
180
+ const bytes = toBytes(input);
181
+ const metadata = inspectWithValues(bytes, values, expectedType);
182
+ if (values.emit === OUTPUT_RAW) {
183
+ return {
184
+ payloadType: "raw",
185
+ sourceFormat: metadata.sourceFormat,
186
+ metadata,
187
+ bytes
188
+ };
189
+ }
190
+ if (values.emit === OUTPUT_JSON || values.emit === DEBUG_OUTPUTS[metadata.sourceFormat]) {
191
+ return metadata;
192
+ }
193
+ if (values.emit === OUTPUT_TEXTURE && metadata.sourceFormat === "dds") {
194
+ return readDdsTexture(bytes, metadata);
195
+ }
196
+ if ((values.emit === OUTPUT_RGBA || values.emit === OUTPUT_IMAGE) && metadata.sourceFormat === "dds") {
197
+ return readDdsToRgba(bytes, metadata);
198
+ }
199
+ const error = new Error(`${metadata.sourceFormat}: emit "${values.emit}" is not implemented yet`);
200
+ error.code = "CJS_FORMAT_OUTPUT_NOT_IMPLEMENTED";
201
+ error.sourceFormat = metadata.sourceFormat;
202
+ error.emit = values.emit;
203
+ throw error;
204
+ }
205
+ function toJsonValue(value) {
206
+ if (value instanceof Uint8Array) {
207
+ return {
208
+ byteLength: value.byteLength
209
+ };
210
+ }
211
+ if (Array.isArray(value)) return value.map(toJsonValue);
212
+ if (value && typeof value === "object") {
213
+ const output = {};
214
+ for (const [key, entry] of Object.entries(value)) output[key] = toJsonValue(entry);
215
+ return output;
216
+ }
217
+ return value;
218
+ }
219
+ function inspectBytes(bytes) {
220
+ if (isPNG(bytes)) return inspectPNG(bytes);
221
+ if (isJPEG(bytes)) return inspectJPEG(bytes);
222
+ if (isDDS(bytes)) return inspectDDS(bytes);
223
+ if (isTGA(bytes)) return inspectTGA(bytes);
224
+ return {
225
+ sourceFormat: "",
226
+ width: 0,
227
+ height: 0
228
+ };
229
+ }
230
+ function isPNG(bytes) {
231
+ return bytes.byteLength >= 24 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 && bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a;
232
+ }
233
+ function isJPEG(bytes) {
234
+ return bytes.byteLength >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8;
235
+ }
236
+ function isDDS(bytes) {
237
+ return bytes.byteLength >= DDS_HEADER_SIZE + 4 && bytes[0] === 0x44 && bytes[1] === 0x44 && bytes[2] === 0x53 && bytes[3] === 0x20 && readU32LE(bytes, 4) === DDS_HEADER_SIZE;
238
+ }
239
+ function isTGA(bytes) {
240
+ if (bytes.byteLength < 18) return false;
241
+ const imageType = bytes[2],
242
+ width = readU16LE(bytes, 12),
243
+ height = readU16LE(bytes, 14),
244
+ bpp = bytes[16];
245
+ return width > 0 && height > 0 && [1, 2, 3, 9, 10, 11].includes(imageType) && [8, 16, 24, 32].includes(bpp);
246
+ }
247
+ function inspectPNG(bytes) {
248
+ const colorType = bytes[25],
249
+ bitDepth = bytes[24];
250
+ return {
251
+ sourceFormat: "png",
252
+ width: readU32BE(bytes, 16),
253
+ height: readU32BE(bytes, 20),
254
+ bitDepth,
255
+ colorType,
256
+ channels: pngChannels(colorType),
257
+ pixelFormat: `png-${bitDepth}-${colorType}`,
258
+ isCompressed: false
259
+ };
260
+ }
261
+ function inspectJPEG(bytes) {
262
+ let offset = 2;
263
+ while (offset + 9 < bytes.byteLength) {
264
+ if (bytes[offset] !== 0xff) {
265
+ offset++;
266
+ continue;
267
+ }
268
+ const marker = bytes[offset + 1],
269
+ length = readU16BE(bytes, offset + 2);
270
+ if ([0xc0, 0xc1, 0xc2, 0xc3].includes(marker)) {
271
+ return {
272
+ sourceFormat: "jpeg",
273
+ width: readU16BE(bytes, offset + 7),
274
+ height: readU16BE(bytes, offset + 5),
275
+ channels: bytes[offset + 9],
276
+ marker,
277
+ pixelFormat: "jpeg-ycbcr",
278
+ isCompressed: true
279
+ };
280
+ }
281
+ offset += Math.max(length + 2, 2);
282
+ }
283
+ return {
284
+ sourceFormat: "jpeg",
285
+ width: 0,
286
+ height: 0,
287
+ channels: 0,
288
+ pixelFormat: "jpeg",
289
+ isCompressed: true
290
+ };
291
+ }
292
+ function inspectDDS(bytes) {
293
+ const headerSize = readU32LE(bytes, 4);
294
+ const pfOffset = DDS_PIXELFORMAT_OFFSET;
295
+ const pfFlags = readU32LE(bytes, pfOffset + 4);
296
+ const fourCcCode = readU32LE(bytes, pfOffset + 8);
297
+ const fourCc = String.fromCharCode(bytes[84], bytes[85], bytes[86], bytes[87]).replace(/\0+$/u, "");
298
+ const rgbBitCount = readU32LE(bytes, pfOffset + 12);
299
+ const rBitMask = readU32LE(bytes, pfOffset + 16);
300
+ const gBitMask = readU32LE(bytes, pfOffset + 20);
301
+ const bBitMask = readU32LE(bytes, pfOffset + 24);
302
+ const aBitMask = readU32LE(bytes, pfOffset + 28);
303
+ const caps2 = readU32LE(bytes, 112);
304
+ const isCube = !!(caps2 & DDSCAPS2_CUBEMAP);
305
+ const isVolume = !!(caps2 & DDSCAPS2_VOLUME);
306
+ const cubeFaces = getDdsCubeFaces(caps2);
307
+ const hasDx10 = fourCc === "DX10" && bytes.byteLength >= 148;
308
+ const dxgiFormat = hasDx10 ? readU32LE(bytes, 128) : 0;
309
+ const resourceDimension = hasDx10 ? readU32LE(bytes, 132) : 0;
310
+ const arraySize = hasDx10 ? readU32LE(bytes, 140) : 1;
311
+ const dataOffset = hasDx10 ? 148 : 128;
312
+ const pixelFormat = getDdsPixelFormat({
313
+ pfFlags,
314
+ fourCc,
315
+ fourCcCode,
316
+ rgbBitCount,
317
+ rBitMask,
318
+ gBitMask,
319
+ bBitMask,
320
+ aBitMask,
321
+ dxgiFormat
322
+ });
323
+ const width = readU32LE(bytes, 16);
324
+ const height = readU32LE(bytes, 12);
325
+ const depth = readU32LE(bytes, 24);
326
+ const mipCount = Math.max(readU32LE(bytes, 28), 1);
327
+ const dataBytes = Math.max(bytes.byteLength - dataOffset, 0);
328
+ const expectedDataBytes = pixelFormat ? getDdsExpectedDataBytes({
329
+ pixelFormat,
330
+ width,
331
+ height,
332
+ depth,
333
+ mipCount,
334
+ arraySize,
335
+ isCube,
336
+ isVolume
337
+ }) : null;
338
+ return {
339
+ sourceFormat: "dds",
340
+ headerSize,
341
+ width,
342
+ height,
343
+ depth,
344
+ mipCount,
345
+ pitchOrLinearSize: readU32LE(bytes, 20),
346
+ pfFlags,
347
+ fourCc,
348
+ fourCcCode,
349
+ rgbBitCount,
350
+ rBitMask,
351
+ gBitMask,
352
+ bBitMask,
353
+ aBitMask,
354
+ caps2,
355
+ hasDx10,
356
+ dxgiFormat,
357
+ resourceDimension,
358
+ arraySize,
359
+ dataOffset,
360
+ dataBytes,
361
+ expectedDataBytes,
362
+ isDataComplete: expectedDataBytes !== null && dataBytes >= expectedDataBytes,
363
+ missingDataBytes: expectedDataBytes === null ? null : Math.max(expectedDataBytes - dataBytes, 0),
364
+ extraDataBytes: expectedDataBytes === null ? null : Math.max(dataBytes - expectedDataBytes, 0),
365
+ faces: isCube ? 6 : 1,
366
+ cubeFaces,
367
+ isCubeComplete: isCube && cubeFaces.length === 6,
368
+ isCube,
369
+ isVolume,
370
+ dimension: isVolume ? "3d" : isCube ? "cube" : "2d",
371
+ pixelFormat,
372
+ textureFormat: pixelFormat || fourCc || "dds-legacy",
373
+ isCompressed: COMPRESSED_PIXEL_FORMATS.has(pixelFormat),
374
+ nativeTextureOnly: COMPRESSED_PIXEL_FORMATS.has(pixelFormat) && !canDecodeDdsToRgba({
375
+ pixelFormat
376
+ }),
377
+ hasAlpha: !!(pfFlags & DDS_ALPHAPIXELS) || /rgba|bgra/u.test(pixelFormat),
378
+ hasMipMaps: readU32LE(bytes, 28) > 1
379
+ };
380
+ }
381
+ function readDdsTexture(bytes, metadata) {
382
+ if (!metadata.pixelFormat) {
383
+ const error = new Error("dds: texture format is not recognized yet");
384
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
385
+ error.sourceFormat = metadata.sourceFormat;
386
+ throw error;
387
+ }
388
+ const subresources = buildDdsSubresources(bytes, metadata);
389
+ return {
390
+ payloadType: OUTPUT_TEXTURE,
391
+ sourceFormat: "dds",
392
+ width: metadata.width,
393
+ height: metadata.height,
394
+ depth: metadata.depth,
395
+ faces: metadata.faces,
396
+ cubeFaces: metadata.cubeFaces,
397
+ isCubeComplete: metadata.isCubeComplete,
398
+ dimension: metadata.dimension,
399
+ mipCount: metadata.mipCount,
400
+ arraySize: metadata.arraySize,
401
+ pixelFormat: metadata.pixelFormat,
402
+ isCompressed: metadata.isCompressed,
403
+ dataBytes: metadata.dataBytes,
404
+ expectedDataBytes: metadata.expectedDataBytes,
405
+ isDataComplete: metadata.isDataComplete,
406
+ missingDataBytes: metadata.missingDataBytes,
407
+ extraDataBytes: metadata.extraDataBytes,
408
+ subresources,
409
+ metadata,
410
+ data: bytes.subarray(metadata.dataOffset, metadata.dataOffset + subresources.reduce((sum, entry) => sum + entry.byteLength, 0))
411
+ };
412
+ }
413
+ function canDecodeDdsToRgba(metadata) {
414
+ return ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float", "rgba8unorm", "rgba8unorm-srgb", "bgra8unorm", "bgra8unorm-srgb", "bgrx8unorm", "bgrx8unorm-srgb", "bgr8unorm", "rg8unorm", "r8unorm", "bc1-rgba-unorm", "bc1-rgba-unorm-srgb", "bc2-rgba-unorm", "bc2-rgba-unorm-srgb", "bc3-rgba-unorm", "bc3-rgba-unorm-srgb", "bc4-r-unorm", "bc4-r-snorm", "bc5-rg-unorm", "bc5-rg-snorm", "bc6h-rgb-ufloat", "bc6h-rgb-float", "bc7-rgba-unorm", "bc7-rgba-unorm-srgb"].includes(metadata.pixelFormat);
415
+ }
416
+ function readDdsToRgba(bytes, metadata) {
417
+ if (!canDecodeDdsToRgba(metadata)) {
418
+ const error = new Error(`dds: RGBA decode is not implemented for ${metadata.pixelFormat || "unknown"}`);
419
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
420
+ error.sourceFormat = metadata.sourceFormat;
421
+ throw error;
422
+ }
423
+ const subresource = buildDdsSubresources(bytes, metadata)[0];
424
+ const source = bytes.subarray(metadata.dataOffset + subresource.offset, metadata.dataOffset + subresource.offset + subresource.byteLength);
425
+ const rgba = metadata.pixelFormat.startsWith("bc1-") ? decodeBc1(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc2-") ? decodeBc2(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc3-") ? decodeBc3(source, metadata.width, metadata.height, subresource.rowPitch) : metadata.pixelFormat.startsWith("bc4-") ? decodeBc4(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc5-") ? decodeBc5(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat.endsWith("-snorm")) : metadata.pixelFormat.startsWith("bc6h-") ? decodeBc6h(source, metadata.width, metadata.height, subresource.rowPitch, metadata.pixelFormat === "bc6h-rgb-float") : metadata.pixelFormat.startsWith("bc7-") ? decodeBc7(source, metadata.width, metadata.height, subresource.rowPitch) : isFloatPixelFormat(metadata.pixelFormat) ? decodeFloatUncompressed(source, metadata) : decodeUncompressed(source, metadata);
426
+ const isFloat = isFloatPixelFormat(metadata.pixelFormat);
427
+ return {
428
+ payloadType: OUTPUT_RGBA,
429
+ sourceFormat: "dds",
430
+ width: metadata.width,
431
+ height: metadata.height,
432
+ pixelFormat: isFloat ? "rgba32float" : "rgba8unorm",
433
+ strideBytes: metadata.width * (isFloat ? 16 : 4),
434
+ origin: "top-left",
435
+ colorSpace: isFloat ? "linear" : metadata.pixelFormat.endsWith("-srgb") ? "srgb" : "unknown",
436
+ alphaMode: metadata.hasAlpha ? "straight" : "opaque",
437
+ metadata,
438
+ data: rgba
439
+ };
440
+ }
441
+ function buildDdsSubresources(bytes, metadata) {
442
+ const subresources = [];
443
+ let offset = 0;
444
+ const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
445
+ for (let layer = 0; layer < layers; layer++) {
446
+ const arrayIndex = metadata.isCube ? Math.floor(layer / 6) : layer;
447
+ const face = metadata.isCube ? layer % 6 : 0;
448
+ for (let mip = 0; mip < metadata.mipCount; mip++) {
449
+ const width = Math.max(metadata.width >> mip, 1);
450
+ const height = Math.max(metadata.height >> mip, 1);
451
+ const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
452
+ const layout = getDdsLevelLayout(metadata.pixelFormat, width, height, depth);
453
+ if (offset + layout.byteLength > bytes.byteLength - metadata.dataOffset) {
454
+ const error = new Error("dds: texture data is truncated");
455
+ error.code = "CJS_FORMAT_TRUNCATED";
456
+ error.sourceFormat = metadata.sourceFormat;
457
+ error.expectedDataBytes = metadata.expectedDataBytes;
458
+ error.dataBytes = metadata.dataBytes;
459
+ error.missingDataBytes = metadata.missingDataBytes;
460
+ throw error;
461
+ }
462
+ subresources.push({
463
+ mip,
464
+ layer,
465
+ arrayIndex,
466
+ face,
467
+ offset,
468
+ byteLength: layout.byteLength,
469
+ rowPitch: layout.rowPitch,
470
+ slicePitch: layout.slicePitch,
471
+ width,
472
+ height,
473
+ depth
474
+ });
475
+ offset += layout.byteLength;
476
+ }
477
+ }
478
+ return subresources;
479
+ }
480
+ function getDdsExpectedDataBytes(metadata) {
481
+ let total = 0;
482
+ const layers = Math.max(metadata.arraySize, 1) * (metadata.isCube ? 6 : 1);
483
+ for (let layer = 0; layer < layers; layer++) {
484
+ for (let mip = 0; mip < metadata.mipCount; mip++) {
485
+ const width = Math.max(metadata.width >> mip, 1);
486
+ const height = Math.max(metadata.height >> mip, 1);
487
+ const depth = metadata.isVolume ? Math.max(metadata.depth >> mip, 1) : 1;
488
+ total += getDdsLevelLayout(metadata.pixelFormat, width, height, depth).byteLength;
489
+ }
490
+ }
491
+ return total;
492
+ }
493
+ function getDdsCubeFaces(caps2) {
494
+ if (!(caps2 & DDSCAPS2_CUBEMAP)) {
495
+ return [];
496
+ }
497
+ return DDS_CUBE_FACE_FLAGS.filter(entry => !!(caps2 & entry[1])).map(entry => entry[0]);
498
+ }
499
+ function getDdsLevelLayout(pixelFormat, width, height, depth) {
500
+ const blockBytes = pixelFormat.startsWith("bc1-") || pixelFormat.startsWith("bc4-") ? 8 : pixelFormat.startsWith("bc") ? 16 : 0;
501
+ if (blockBytes) {
502
+ const blocksWide = Math.max(Math.ceil(width / 4), 1);
503
+ const blocksHigh = Math.max(Math.ceil(height / 4), 1);
504
+ const rowPitch = blocksWide * blockBytes;
505
+ const slicePitch = rowPitch * blocksHigh;
506
+ return {
507
+ rowPitch,
508
+ slicePitch,
509
+ byteLength: slicePitch * depth
510
+ };
511
+ }
512
+ const bytesPerPixel = {
513
+ "rgba32float": 16,
514
+ "rgb32float": 12,
515
+ "rgba16float": 8,
516
+ "rg32float": 8,
517
+ "r32float": 4,
518
+ "r16float": 2,
519
+ "rgba8unorm": 4,
520
+ "rgba8unorm-srgb": 4,
521
+ "bgra8unorm": 4,
522
+ "bgra8unorm-srgb": 4,
523
+ "bgrx8unorm": 4,
524
+ "bgrx8unorm-srgb": 4,
525
+ "bgr8unorm": 3,
526
+ "rg8unorm": 2,
527
+ "r8unorm": 1
528
+ }[pixelFormat];
529
+ if (!bytesPerPixel) {
530
+ const error = new Error(`dds: cannot calculate subresource layout for ${pixelFormat || "unknown"}`);
531
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
532
+ throw error;
533
+ }
534
+ const rowPitch = width * bytesPerPixel;
535
+ const slicePitch = rowPitch * height;
536
+ return {
537
+ rowPitch,
538
+ slicePitch,
539
+ byteLength: slicePitch * depth
540
+ };
541
+ }
542
+ function decodeUncompressed(source, metadata) {
543
+ const rgba = new Uint8Array(metadata.width * metadata.height * 4);
544
+ const bytesPerPixel = metadata.pixelFormat === "bgr8unorm" ? 3 : metadata.pixelFormat === "rg8unorm" ? 2 : metadata.pixelFormat === "r8unorm" ? 1 : 4;
545
+ for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
546
+ const sourceOffset = pixel * bytesPerPixel;
547
+ const outputOffset = pixel * 4;
548
+ if (metadata.pixelFormat.startsWith("bgra") || metadata.pixelFormat.startsWith("bgrx")) {
549
+ rgba[outputOffset] = source[sourceOffset + 2];
550
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
551
+ rgba[outputOffset + 2] = source[sourceOffset];
552
+ rgba[outputOffset + 3] = metadata.pixelFormat.startsWith("bgrx") ? 255 : source[sourceOffset + 3];
553
+ } else if (metadata.pixelFormat === "bgr8unorm") {
554
+ rgba[outputOffset] = source[sourceOffset + 2];
555
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
556
+ rgba[outputOffset + 2] = source[sourceOffset];
557
+ rgba[outputOffset + 3] = 255;
558
+ } else if (metadata.pixelFormat === "rg8unorm") {
559
+ rgba[outputOffset] = source[sourceOffset];
560
+ rgba[outputOffset + 1] = source[sourceOffset + 1];
561
+ rgba[outputOffset + 2] = 0;
562
+ rgba[outputOffset + 3] = 255;
563
+ } else {
564
+ rgba[outputOffset] = source[sourceOffset];
565
+ rgba[outputOffset + 1] = bytesPerPixel > 1 ? source[sourceOffset + 1] : source[sourceOffset];
566
+ rgba[outputOffset + 2] = bytesPerPixel > 2 ? source[sourceOffset + 2] : source[sourceOffset];
567
+ rgba[outputOffset + 3] = bytesPerPixel > 3 ? source[sourceOffset + 3] : 255;
568
+ }
569
+ }
570
+ return rgba;
571
+ }
572
+ function decodeFloatUncompressed(source, metadata) {
573
+ const rgba = new Float32Array(metadata.width * metadata.height * 4);
574
+ const view = new DataView(source.buffer, source.byteOffset, source.byteLength);
575
+ const bytesPerPixel = {
576
+ "rgba32float": 16,
577
+ "rgb32float": 12,
578
+ "rgba16float": 8,
579
+ "rg32float": 8,
580
+ "r32float": 4,
581
+ "r16float": 2
582
+ }[metadata.pixelFormat];
583
+ for (let pixel = 0; pixel < metadata.width * metadata.height; pixel++) {
584
+ const sourceOffset = pixel * bytesPerPixel;
585
+ const outputOffset = pixel * 4;
586
+ let red, green, blue, alpha;
587
+ if (metadata.pixelFormat === "rgba32float") {
588
+ red = view.getFloat32(sourceOffset, true);
589
+ green = view.getFloat32(sourceOffset + 4, true);
590
+ blue = view.getFloat32(sourceOffset + 8, true);
591
+ alpha = view.getFloat32(sourceOffset + 12, true);
592
+ } else if (metadata.pixelFormat === "rgb32float") {
593
+ red = view.getFloat32(sourceOffset, true);
594
+ green = view.getFloat32(sourceOffset + 4, true);
595
+ blue = view.getFloat32(sourceOffset + 8, true);
596
+ alpha = 1;
597
+ } else if (metadata.pixelFormat === "rgba16float") {
598
+ red = halfToFloat(view.getUint16(sourceOffset, true));
599
+ green = halfToFloat(view.getUint16(sourceOffset + 2, true));
600
+ blue = halfToFloat(view.getUint16(sourceOffset + 4, true));
601
+ alpha = halfToFloat(view.getUint16(sourceOffset + 6, true));
602
+ } else if (metadata.pixelFormat === "rg32float") {
603
+ red = view.getFloat32(sourceOffset, true);
604
+ green = view.getFloat32(sourceOffset + 4, true);
605
+ blue = 0;
606
+ alpha = 1;
607
+ } else {
608
+ red = metadata.pixelFormat === "r16float" ? halfToFloat(view.getUint16(sourceOffset, true)) : view.getFloat32(sourceOffset, true);
609
+ green = red;
610
+ blue = red;
611
+ alpha = 1;
612
+ }
613
+ rgba[outputOffset] = red;
614
+ rgba[outputOffset + 1] = green;
615
+ rgba[outputOffset + 2] = blue;
616
+ rgba[outputOffset + 3] = alpha;
617
+ }
618
+ return rgba;
619
+ }
620
+ function isFloatPixelFormat(pixelFormat) {
621
+ return typeof pixelFormat === "string" && (pixelFormat.startsWith("bc6h-") || ["rgba32float", "rgb32float", "rgba16float", "rg32float", "r32float", "r16float"].includes(pixelFormat));
622
+ }
623
+ function halfToFloat(value) {
624
+ const sign = value & 0x8000 ? -1 : 1;
625
+ const exponent = value >>> 10 & 0x1f;
626
+ const mantissa = value & 0x03ff;
627
+ if (exponent === 0) {
628
+ return sign * Math.pow(2, -14) * (mantissa / 1024);
629
+ }
630
+ if (exponent === 0x1f) {
631
+ return mantissa === 0 ? sign * Infinity : NaN;
632
+ }
633
+ return sign * Math.pow(2, exponent - 15) * (1 + mantissa / 1024);
634
+ }
635
+ function decodeBc1(source, width, height, rowPitch) {
636
+ const rgba = new Uint8Array(width * height * 4);
637
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
638
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
639
+ const blockOffset = blockY * rowPitch + blockX * 8;
640
+ const c0 = source[blockOffset] | source[blockOffset + 1] << 8;
641
+ const c1 = source[blockOffset + 2] | source[blockOffset + 3] << 8;
642
+ const colors = decodeBc1Colors(c0, c1);
643
+ const indices = source[blockOffset + 4] | source[blockOffset + 5] << 8 | source[blockOffset + 6] << 16 | source[blockOffset + 7] << 24;
644
+ for (let y = 0; y < 4; y++) {
645
+ for (let x = 0; x < 4; x++) {
646
+ const outputX = blockX * 4 + x;
647
+ const outputY = blockY * 4 + y;
648
+ if (outputX >= width || outputY >= height) continue;
649
+ const color = colors[indices >>> 2 * (y * 4 + x) & 3];
650
+ rgba.set(color, (outputY * width + outputX) * 4);
651
+ }
652
+ }
653
+ }
654
+ }
655
+ return rgba;
656
+ }
657
+ function decodeBc2(source, width, height, rowPitch) {
658
+ return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
659
+ const alphaNibble = source[blockOffset + y * 2 + Math.floor(x / 2)] >>> x % 2 * 4 & 0x0f;
660
+ return alphaNibble * 17;
661
+ }, 8);
662
+ }
663
+ function decodeBc3(source, width, height, rowPitch) {
664
+ return decodeBcAlphaColor(source, width, height, rowPitch, (blockOffset, x, y) => {
665
+ const alpha0 = source[blockOffset],
666
+ alpha1 = source[blockOffset + 1];
667
+ const alphaValues = alpha0 > alpha1 ? [alpha0, alpha1, Math.round((6 * alpha0 + alpha1) / 7), Math.round((5 * alpha0 + 2 * alpha1) / 7), Math.round((4 * alpha0 + 3 * alpha1) / 7), Math.round((3 * alpha0 + 4 * alpha1) / 7), Math.round((2 * alpha0 + 5 * alpha1) / 7), Math.round((alpha0 + 6 * alpha1) / 7)] : [alpha0, alpha1, Math.round((4 * alpha0 + alpha1) / 5), Math.round((3 * alpha0 + 2 * alpha1) / 5), Math.round((2 * alpha0 + 3 * alpha1) / 5), Math.round((alpha0 + 4 * alpha1) / 5), 0, 255];
668
+ let alphaBits = 0n;
669
+ for (let i = 0; i < 6; i++) alphaBits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
670
+ return alphaValues[Number(alphaBits >> BigInt((y * 4 + x) * 3) & 7n)];
671
+ }, 8);
672
+ }
673
+ function decodeBc4(source, width, height, rowPitch, signed) {
674
+ const rgba = new Uint8Array(width * height * 4);
675
+ forEachBcPixel(width, height, rowPitch, 8, (blockOffset, x, y, outputOffset) => {
676
+ const value = decodeBc4Value(source, blockOffset, x, y, signed);
677
+ rgba[outputOffset] = value;
678
+ rgba[outputOffset + 1] = value;
679
+ rgba[outputOffset + 2] = value;
680
+ rgba[outputOffset + 3] = 255;
681
+ });
682
+ return rgba;
683
+ }
684
+ function decodeBc5(source, width, height, rowPitch, signed) {
685
+ const rgba = new Uint8Array(width * height * 4);
686
+ forEachBcPixel(width, height, rowPitch, 16, (blockOffset, x, y, outputOffset) => {
687
+ rgba[outputOffset] = decodeBc4Value(source, blockOffset, x, y, signed);
688
+ rgba[outputOffset + 1] = decodeBc4Value(source, blockOffset + 8, x, y, signed);
689
+ rgba[outputOffset + 2] = 0;
690
+ rgba[outputOffset + 3] = 255;
691
+ });
692
+ return rgba;
693
+ }
694
+ function decodeBc4Value(source, blockOffset, x, y, signed) {
695
+ const endpoint0 = signed ? decodeSnormByte(source[blockOffset]) : source[blockOffset];
696
+ const endpoint1 = signed ? decodeSnormByte(source[blockOffset + 1]) : source[blockOffset + 1];
697
+ const values = endpoint0 > endpoint1 ? [endpoint0, endpoint1, (6 * endpoint0 + endpoint1) / 7, (5 * endpoint0 + 2 * endpoint1) / 7, (4 * endpoint0 + 3 * endpoint1) / 7, (3 * endpoint0 + 4 * endpoint1) / 7, (2 * endpoint0 + 5 * endpoint1) / 7, (endpoint0 + 6 * endpoint1) / 7] : [endpoint0, endpoint1, (4 * endpoint0 + endpoint1) / 5, (3 * endpoint0 + 2 * endpoint1) / 5, (2 * endpoint0 + 3 * endpoint1) / 5, (endpoint0 + 4 * endpoint1) / 5, signed ? -1 : 0, signed ? 1 : 255];
698
+ let bits = 0n;
699
+ for (let i = 0; i < 6; i++) bits |= BigInt(source[blockOffset + 2 + i]) << BigInt(i * 8);
700
+ const value = values[Number(bits >> BigInt((y * 4 + x) * 3) & 7n)];
701
+ return signed ? Math.max(0, Math.min(255, Math.round((value + 1) * 127.5))) : Math.round(value);
702
+ }
703
+ function decodeSnormByte(value) {
704
+ const signed = value > 127 ? value - 256 : value;
705
+ return Math.max(-1, signed / 127);
706
+ }
707
+ function forEachBcPixel(width, height, rowPitch, blockBytes, callback) {
708
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
709
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
710
+ const blockOffset = blockY * rowPitch + blockX * blockBytes;
711
+ for (let y = 0; y < 4; y++) {
712
+ for (let x = 0; x < 4; x++) {
713
+ const outputX = blockX * 4 + x;
714
+ const outputY = blockY * 4 + y;
715
+ if (outputX < width && outputY < height) {
716
+ callback(blockOffset, x, y, (outputY * width + outputX) * 4);
717
+ }
718
+ }
719
+ }
720
+ }
721
+ }
722
+ }
723
+ function decodeBcAlphaColor(source, width, height, rowPitch, alphaAt, colorOffset = 0) {
724
+ const rgba = new Uint8Array(width * height * 4);
725
+ for (let blockY = 0; blockY < Math.ceil(height / 4); blockY++) {
726
+ for (let blockX = 0; blockX < Math.ceil(width / 4); blockX++) {
727
+ const blockOffset = blockY * rowPitch + blockX * 16;
728
+ const colorOffsetInBlock = blockOffset + colorOffset;
729
+ const c0 = source[colorOffsetInBlock] | source[colorOffsetInBlock + 1] << 8;
730
+ const c1 = source[colorOffsetInBlock + 2] | source[colorOffsetInBlock + 3] << 8;
731
+ const colors = decodeBc1Colors(c0, c1, true);
732
+ const indices = source[colorOffsetInBlock + 4] | source[colorOffsetInBlock + 5] << 8 | source[colorOffsetInBlock + 6] << 16 | source[colorOffsetInBlock + 7] << 24;
733
+ for (let y = 0; y < 4; y++) {
734
+ for (let x = 0; x < 4; x++) {
735
+ const outputX = blockX * 4 + x;
736
+ const outputY = blockY * 4 + y;
737
+ if (outputX >= width || outputY >= height) continue;
738
+ const color = colors[indices >>> 2 * (y * 4 + x) & 3];
739
+ const outputOffset = (outputY * width + outputX) * 4;
740
+ rgba[outputOffset] = color[0];
741
+ rgba[outputOffset + 1] = color[1];
742
+ rgba[outputOffset + 2] = color[2];
743
+ rgba[outputOffset + 3] = alphaAt(blockOffset, x, y);
744
+ }
745
+ }
746
+ }
747
+ }
748
+ return rgba;
749
+ }
750
+ function decodeBc1Colors(c0, c1, forceFourColor = false) {
751
+ const first = decode565(c0);
752
+ const second = decode565(c1);
753
+ const colors = [first, second];
754
+ if (c0 > c1 || forceFourColor) {
755
+ colors.push([Math.round((2 * first[0] + second[0]) / 3), Math.round((2 * first[1] + second[1]) / 3), Math.round((2 * first[2] + second[2]) / 3), 255], [Math.round((first[0] + 2 * second[0]) / 3), Math.round((first[1] + 2 * second[1]) / 3), Math.round((first[2] + 2 * second[2]) / 3), 255]);
756
+ } else {
757
+ colors.push([Math.round((first[0] + second[0]) / 2), Math.round((first[1] + second[1]) / 2), Math.round((first[2] + second[2]) / 2), 255], [0, 0, 0, 0]);
758
+ }
759
+ return colors;
760
+ }
761
+ function decode565(value) {
762
+ return [Math.round((value >>> 11 & 0x1f) * 255 / 31), Math.round((value >>> 5 & 0x3f) * 255 / 63), Math.round((value & 0x1f) * 255 / 31), 255];
763
+ }
764
+ function getDdsPixelFormat(format) {
765
+ if (format.dxgiFormat) return DXGI_PIXEL_FORMATS[format.dxgiFormat] || "";
766
+ if (format.pfFlags & DDS_FOURCC && format.fourCc) {
767
+ return FOURCC_PIXEL_FORMATS[format.fourCc] || {
768
+ 113: "rgba16float",
769
+ 116: "rgba32float"
770
+ }[format.fourCcCode] || "";
771
+ }
772
+ if (format.pfFlags & DDS_RGB) {
773
+ if (format.rgbBitCount === 32 && format.rBitMask === 0x000000ff && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x00ff0000 && format.aBitMask === 0xff000000) return "rgba8unorm";
774
+ if (format.rgbBitCount === 32 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff && format.aBitMask === 0xff000000) return "bgra8unorm";
775
+ if (format.rgbBitCount === 24 && format.rBitMask === 0x00ff0000 && format.gBitMask === 0x0000ff00 && format.bBitMask === 0x000000ff) return "bgr8unorm";
776
+ }
777
+ return "";
778
+ }
779
+ function inspectTGA(bytes) {
780
+ return {
781
+ sourceFormat: "tga",
782
+ width: readU16LE(bytes, 12),
783
+ height: readU16LE(bytes, 14),
784
+ channels: Math.max(bytes[16] / 8, 1),
785
+ imageType: bytes[2],
786
+ pixelFormat: `tga-${bytes[16]}`,
787
+ isCompressed: [9, 10, 11].includes(bytes[2])
788
+ };
789
+ }
790
+ function pngChannels(colorType) {
791
+ if (colorType === 0) return 1;
792
+ if (colorType === 2) return 3;
793
+ if (colorType === 3) return 1;
794
+ if (colorType === 4) return 2;
795
+ if (colorType === 6) return 4;
796
+ return 0;
797
+ }
798
+ function readU16BE(bytes, offset) {
799
+ return bytes[offset] << 8 | bytes[offset + 1];
800
+ }
801
+ function readU16LE(bytes, offset) {
802
+ return bytes[offset] | bytes[offset + 1] << 8;
803
+ }
804
+ function readU32BE(bytes, offset) {
805
+ return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16 | bytes[offset + 2] << 8 | bytes[offset + 3]) >>> 0;
806
+ }
807
+ function readU32LE(bytes, offset) {
808
+ return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
809
+ }
810
+ function capitalize(value) {
811
+ return value ? value[0].toUpperCase() + value.slice(1) : "Image";
812
+ }
813
+
814
+ export { DEFAULT_VALUES, OUTPUT_IMAGE, OUTPUT_JSON, OUTPUT_RAW, OUTPUT_RGBA, OUTPUT_TEXTURE, inspectBytes, inspectWithValues, isDDS, isJPEG, isPNG, isSupportedWithValues, isTGA, normalizeEmit, normalizeInputType, normalizeValues, readWithValues, toBytes, toJsonValue };
815
+ //# sourceMappingURL=helpers.js.map