@carbonenginejs/runtime-resource 0.8.0 → 0.9.1

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 (297) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +31 -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 +68 -59
  9. package/dist/CjsResourceSource.js.map +1 -1
  10. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  11. package/dist/format/CjsBlueReader.js +269 -269
  12. package/dist/format/CjsFormat.js +197 -192
  13. package/dist/format/CjsFormat.js.map +1 -1
  14. package/dist/format/CjsReader.js +18 -18
  15. package/dist/format/CjsResourceProbe.js +277 -277
  16. package/dist/format/payloadContract.js +173 -173
  17. package/dist/formats/black/CjsBlackFormat.js +278 -278
  18. package/dist/formats/black/core/CjsBlackBinaryReader.js +157 -153
  19. package/dist/formats/black/core/CjsBlackBinaryReader.js.map +1 -1
  20. package/dist/formats/black/core/CjsBlackPropertyReaders.js +387 -382
  21. package/dist/formats/black/core/CjsBlackPropertyReaders.js.map +1 -1
  22. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  23. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +437 -433
  24. package/dist/formats/black/core/CjsBlackSchemaRegistry.js.map +1 -1
  25. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js +4 -0
  26. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js.map +1 -0
  27. package/dist/formats/black/core/blackConstants.js +7 -7
  28. package/dist/formats/black/core/blackDefinitions.js +10 -10
  29. package/dist/formats/black/core/blackDefinitions.js.map +1 -1
  30. package/dist/formats/black/core/blackEnums.js +6 -6
  31. package/dist/formats/black/core/blackSchema.js +3 -3
  32. package/dist/formats/black/core/blackVersion.js +22 -22
  33. package/dist/formats/black/core/helpers.js +182 -182
  34. package/dist/formats/black/core/schema.js +4 -4
  35. package/dist/formats/black/index.js +2 -2
  36. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  37. package/dist/formats/bnk/core/graph.js +140 -140
  38. package/dist/formats/bnk/core/helpers.js +409 -409
  39. package/dist/formats/bnk/core/musicNodes.js +493 -489
  40. package/dist/formats/bnk/core/musicNodes.js.map +1 -1
  41. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  42. package/dist/formats/bnk/index.js +2 -2
  43. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  44. package/dist/formats/cmf/core/binary.js +123 -118
  45. package/dist/formats/cmf/core/binary.js.map +1 -1
  46. package/dist/formats/cmf/core/buffers.js +233 -233
  47. package/dist/formats/cmf/core/constants.js +47 -47
  48. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  49. package/dist/formats/cmf/core/helpers.js +287 -287
  50. package/dist/formats/cmf/core/pack.js +276 -276
  51. package/dist/formats/cmf/core/schema.js +364 -364
  52. package/dist/formats/cmf/core/shared.js +268 -268
  53. package/dist/formats/cmf/core/writer.js +517 -517
  54. package/dist/formats/cmf/index.js +2 -2
  55. package/dist/formats/dds/CjsDdsFormat.js +200 -193
  56. package/dist/formats/dds/CjsDdsFormat.js.map +1 -1
  57. package/dist/formats/dds/core/bc6h.js +288 -288
  58. package/dist/formats/dds/core/bc7.js +256 -251
  59. package/dist/formats/dds/core/bc7.js.map +1 -1
  60. package/dist/formats/dds/core/helpers.js +815 -815
  61. package/dist/formats/dds/index.js +2 -2
  62. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  63. package/dist/formats/fbx/core/helpers.js +3911 -3901
  64. package/dist/formats/fbx/core/helpers.js.map +1 -1
  65. package/dist/formats/fbx/index.js +2 -2
  66. package/dist/formats/flac/CjsFlacFormat.js +93 -87
  67. package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
  68. package/dist/formats/flac/core/helpers.js +295 -295
  69. package/dist/formats/flac/index.js +2 -2
  70. package/dist/formats/gif/CjsGifFormat.js +92 -87
  71. package/dist/formats/gif/CjsGifFormat.js.map +1 -1
  72. package/dist/formats/gif/core/helpers.js +360 -360
  73. package/dist/formats/gif/index.js +2 -2
  74. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  75. package/dist/formats/gltf/core/helpers.js +292 -292
  76. package/dist/formats/gltf/core/json.js +79 -79
  77. package/dist/formats/gltf/core/parser.js +666 -666
  78. package/dist/formats/gltf/core/targets.js +163 -163
  79. package/dist/formats/gltf/index.js +2 -2
  80. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  81. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  82. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  83. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  84. package/dist/formats/gr2/core/bitknit2.js +280 -0
  85. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  86. package/dist/formats/gr2/core/curves.js +1047 -0
  87. package/dist/formats/gr2/core/curves.js.map +1 -0
  88. package/dist/formats/gr2/core/gsf.js +72 -0
  89. package/dist/formats/gr2/core/gsf.js.map +1 -0
  90. package/dist/formats/gr2/core/helpers.js +332 -0
  91. package/dist/formats/gr2/core/helpers.js.map +1 -0
  92. package/dist/formats/gr2/core/json.js +622 -0
  93. package/dist/formats/gr2/core/json.js.map +1 -0
  94. package/dist/formats/gr2/core/oodle1.js +388 -0
  95. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  96. package/dist/formats/gr2/core/reader.js +617 -0
  97. package/dist/formats/gr2/core/reader.js.map +1 -0
  98. package/dist/formats/gr2/core/tangents.js +48 -0
  99. package/dist/formats/gr2/core/tangents.js.map +1 -0
  100. package/dist/formats/gr2/core/targets.js +351 -0
  101. package/dist/formats/gr2/core/targets.js.map +1 -0
  102. package/dist/formats/gr2/index.js +3 -0
  103. package/dist/formats/gr2/index.js.map +1 -0
  104. package/dist/formats/index.js +30 -23
  105. package/dist/formats/index.js.map +1 -1
  106. package/dist/formats/jpeg/CjsJpegFormat.js +212 -206
  107. package/dist/formats/jpeg/CjsJpegFormat.js.map +1 -1
  108. package/dist/formats/jpeg/core/helpers.js +350 -350
  109. package/dist/formats/jpeg/core/jpeg.js +387 -377
  110. package/dist/formats/jpeg/core/jpeg.js.map +1 -1
  111. package/dist/formats/jpeg/index.js +2 -2
  112. package/dist/formats/mp3/CjsMp3Format.js +197 -192
  113. package/dist/formats/mp3/CjsMp3Format.js.map +1 -1
  114. package/dist/formats/mp3/core/helpers.js +338 -338
  115. package/dist/formats/mp3/index.js +2 -2
  116. package/dist/formats/mp4/CjsMp4Format.js +197 -191
  117. package/dist/formats/mp4/CjsMp4Format.js.map +1 -1
  118. package/dist/formats/mp4/core/helpers.js +449 -449
  119. package/dist/formats/mp4/index.js +2 -2
  120. package/dist/formats/obj/CjsObjFormat.js +253 -253
  121. package/dist/formats/obj/core/helpers.js +573 -573
  122. package/dist/formats/obj/core/json.js +64 -64
  123. package/dist/formats/obj/core/parser.js +321 -321
  124. package/dist/formats/obj/index.js +2 -2
  125. package/dist/formats/ogg/CjsOggFormat.js +94 -88
  126. package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
  127. package/dist/formats/ogg/core/helpers.js +387 -387
  128. package/dist/formats/ogg/core/imdct.js +178 -178
  129. package/dist/formats/ogg/core/vorbis.js +1004 -999
  130. package/dist/formats/ogg/core/vorbis.js.map +1 -1
  131. package/dist/formats/ogg/index.js +2 -2
  132. package/dist/formats/png/CjsPngFormat.js +201 -195
  133. package/dist/formats/png/CjsPngFormat.js.map +1 -1
  134. package/dist/formats/png/core/helpers.js +583 -583
  135. package/dist/formats/png/index.js +2 -2
  136. package/dist/formats/red/CjsRedFormat.js +261 -261
  137. package/dist/formats/red/core/CjsRedReader.js +194 -194
  138. package/dist/formats/red/core/blackDefinitions.js +3 -3
  139. package/dist/formats/red/core/helpers.js +139 -139
  140. package/dist/formats/red/core/redGraph.js +68 -68
  141. package/dist/formats/red/core/schema.js +4 -4
  142. package/dist/formats/red/index.js +2 -2
  143. package/dist/formats/stl/CjsStlFormat.js +365 -365
  144. package/dist/formats/stl/core/helpers.js +261 -261
  145. package/dist/formats/stl/core/json.js +51 -51
  146. package/dist/formats/stl/core/stl.js +634 -629
  147. package/dist/formats/stl/core/stl.js.map +1 -1
  148. package/dist/formats/stl/core/targets.js +163 -163
  149. package/dist/formats/stl/index.js +2 -2
  150. package/dist/formats/tga/CjsTgaFormat.js +197 -192
  151. package/dist/formats/tga/CjsTgaFormat.js.map +1 -1
  152. package/dist/formats/tga/core/helpers.js +446 -446
  153. package/dist/formats/tga/index.js +2 -2
  154. package/dist/formats/wav/CjsWavFormat.js +198 -192
  155. package/dist/formats/wav/CjsWavFormat.js.map +1 -1
  156. package/dist/formats/wav/core/helpers.js +328 -328
  157. package/dist/formats/wav/index.js +2 -2
  158. package/dist/formats/webm/CjsWebmFormat.js +197 -191
  159. package/dist/formats/webm/CjsWebmFormat.js.map +1 -1
  160. package/dist/formats/webm/core/helpers.js +537 -537
  161. package/dist/formats/webm/index.js +2 -2
  162. package/dist/formats/webp/CjsWebpFormat.js +91 -86
  163. package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
  164. package/dist/formats/webp/core/helpers.js +214 -214
  165. package/dist/formats/webp/index.js +2 -2
  166. package/dist/formats/wem/CjsWemFormat.js +242 -241
  167. package/dist/formats/wem/CjsWemFormat.js.map +1 -1
  168. package/dist/formats/wem/core/bitStream.js +259 -259
  169. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  170. package/dist/formats/wem/core/helpers.js +417 -417
  171. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  172. package/dist/formats/wem/core/ptadpcm.js +77 -77
  173. package/dist/formats/wem/core/resolve.js +121 -121
  174. package/dist/formats/wem/core/wemToOgg.js +485 -485
  175. package/dist/formats/wem/index.js +2 -2
  176. package/dist/formats/yaml/CjsYamlFormat.js +89 -83
  177. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  178. package/dist/formats/yaml/core/CjsYamlReader.js +311 -305
  179. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  180. package/dist/formats/yaml/core/helpers.js +160 -160
  181. package/dist/formats/yaml/index.js +2 -2
  182. package/dist/index.js +49 -48
  183. package/dist/index.js.map +1 -1
  184. package/dist/resourcePath.js +18 -18
  185. package/dist/resourceStates.js +9 -9
  186. package/dist/resources/AudioGeometryResData.js +47 -47
  187. package/dist/resources/GStateBindingCallbackData.js +31 -31
  188. package/dist/resources/MeshDecalData.js +37 -37
  189. package/dist/resources/MeshDecalLodData.js +34 -34
  190. package/dist/resources/Tr2EffectRes.js +71 -71
  191. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  192. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  193. package/dist/resources/Tr2ImageRes.js +114 -114
  194. package/dist/resources/Tr2LightProfileRes.js +40 -40
  195. package/dist/resources/Tr2MaterialArea.js +34 -34
  196. package/dist/resources/Tr2MaterialMesh.js +31 -31
  197. package/dist/resources/Tr2MaterialRes.js +34 -34
  198. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  199. package/dist/resources/Tr2TextureLodManager.js +80 -80
  200. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  201. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  202. package/dist/resources/Tr2TexturePipeline.js +52 -52
  203. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  204. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  205. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  206. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  207. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  208. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  209. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  210. package/dist/resources/TriGeometryRes.js +239 -239
  211. package/dist/resources/TriGeometryResAreaData.js +59 -59
  212. package/dist/resources/TriGeometryResJointData.js +38 -38
  213. package/dist/resources/TriGeometryResLodData.js +88 -88
  214. package/dist/resources/TriGeometryResMeshData.js +63 -63
  215. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  216. package/dist/resources/TriGrannyRes.js +43 -43
  217. package/dist/resources/TriJointBinding.js +38 -38
  218. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  219. package/dist/resources/TriRtGeometryConstants.js +88 -88
  220. package/dist/resources/TriTextureRes.js +304 -304
  221. package/dist/resources/enums.js +18 -18
  222. package/dist/resources/resourceBoundary.js +47 -47
  223. package/dist/resources/texturePipelineBehavior.js +308 -308
  224. package/dist/texture/CjsTextureArrayRes.js +406 -406
  225. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  226. package/docs/README.md +73 -0
  227. package/docs/architecture.md +86 -0
  228. package/docs/concepts/resource-lifecycle.md +217 -0
  229. package/docs/formats/README.md +105 -0
  230. package/docs/formats/gr2.md +161 -0
  231. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  232. package/docs/formats/stl.md +37 -0
  233. package/docs/formats/wwise.md +44 -0
  234. package/docs/reference/classes/README.md +33 -0
  235. package/docs/reference/classes/core.md +106 -0
  236. package/docs/reference/classes/dropped.md +46 -0
  237. package/docs/reference/classes/formats.md +522 -0
  238. package/docs/reference/classes/resources.md +346 -0
  239. package/docs/reference/classes/texture.md +26 -0
  240. package/docs/reference/events.md +92 -0
  241. package/docs/reference/motherlode-cache.md +244 -0
  242. package/docs/reference/queues.md +102 -0
  243. package/docs/reference/reload.md +107 -0
  244. package/docs/reference/texture-arrays.md +113 -0
  245. package/docs/reference/texture-pipeline.md +53 -0
  246. package/docs/roadmap.md +104 -0
  247. package/format-notices/black/LICENSE +21 -21
  248. package/format-notices/black/NOTICE +47 -47
  249. package/format-notices/bnk/LICENSE +21 -21
  250. package/format-notices/bnk/NOTICE +20 -20
  251. package/format-notices/cmf/LICENSE +21 -21
  252. package/format-notices/cmf/NOTICE +36 -36
  253. package/format-notices/dds/LICENSE +21 -21
  254. package/format-notices/dds/NOTICE +14 -14
  255. package/format-notices/fbx/LICENSE +21 -21
  256. package/format-notices/fbx/NOTICE +14 -14
  257. package/format-notices/flac/LICENSE +21 -21
  258. package/format-notices/flac/NOTICE +14 -14
  259. package/format-notices/gif/LICENSE +21 -21
  260. package/format-notices/gif/NOTICE +14 -14
  261. package/format-notices/gltf/LICENSE +21 -21
  262. package/format-notices/gltf/NOTICE +27 -27
  263. package/format-notices/gr2/LICENSE +21 -0
  264. package/format-notices/gr2/NOTICE +60 -0
  265. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  266. package/format-notices/jpeg/LICENSE +21 -21
  267. package/format-notices/jpeg/NOTICE +14 -14
  268. package/format-notices/mp3/LICENSE +21 -21
  269. package/format-notices/mp3/NOTICE +14 -14
  270. package/format-notices/mp4/LICENSE +21 -21
  271. package/format-notices/mp4/NOTICE +14 -14
  272. package/format-notices/obj/LICENSE +21 -21
  273. package/format-notices/obj/NOTICE +26 -26
  274. package/format-notices/ogg/LICENSE +21 -21
  275. package/format-notices/ogg/NOTICE +28 -28
  276. package/format-notices/png/LICENSE +21 -21
  277. package/format-notices/png/NOTICE +14 -14
  278. package/format-notices/red/LICENSE +21 -21
  279. package/format-notices/red/NOTICE +31 -31
  280. package/format-notices/stl/LICENSE +21 -21
  281. package/format-notices/stl/NOTICE +21 -21
  282. package/format-notices/tga/LICENSE +21 -21
  283. package/format-notices/tga/NOTICE +14 -14
  284. package/format-notices/wav/LICENSE +21 -21
  285. package/format-notices/wav/NOTICE +14 -14
  286. package/format-notices/webm/LICENSE +21 -21
  287. package/format-notices/webm/NOTICE +14 -14
  288. package/format-notices/webp/LICENSE +21 -21
  289. package/format-notices/webp/NOTICE +14 -14
  290. package/format-notices/wem/LICENSE +57 -57
  291. package/format-notices/wem/NOTICE +33 -33
  292. package/format-notices/yaml/LICENSE +21 -21
  293. package/format-notices/yaml/NOTICE +44 -44
  294. package/package.json +52 -51
  295. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +0 -4
  296. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js.map +0 -1
  297. 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