rw-parser-ng 2.1.1 → 2.3.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.
- package/README.md +252 -52
- package/lib/codecs/bitmap.d.ts +6 -0
- package/lib/codecs/bitmap.js +77 -0
- package/lib/codecs/color.d.ts +6 -0
- package/lib/codecs/color.js +43 -0
- package/lib/codecs/dxt.d.ts +3 -0
- package/lib/codecs/dxt.js +212 -0
- package/lib/codecs/formats.d.ts +25 -0
- package/lib/codecs/formats.js +32 -0
- package/lib/codecs/index.d.ts +5 -0
- package/lib/codecs/index.js +21 -0
- package/lib/codecs/palette.d.ts +6 -0
- package/lib/codecs/palette.js +86 -0
- package/lib/common/types.d.ts +50 -0
- package/lib/common/types.js +2 -0
- package/lib/core/byte-stream.d.ts +20 -0
- package/lib/core/byte-stream.js +71 -0
- package/lib/core/errors.d.ts +5 -0
- package/lib/core/errors.js +34 -0
- package/lib/core/index.d.ts +5 -0
- package/lib/core/index.js +21 -0
- package/lib/core/rw-file.d.ts +9 -0
- package/lib/core/rw-file.js +36 -0
- package/lib/core/rw-sections.d.ts +24 -0
- package/lib/core/rw-sections.js +28 -0
- package/lib/core/rw-version.d.ts +3 -0
- package/lib/core/rw-version.js +27 -0
- package/lib/dff/dff-model-type.d.ts +5 -0
- package/lib/dff/dff-model-type.js +9 -0
- package/lib/dff/index.d.ts +4 -0
- package/lib/dff/index.js +20 -0
- package/lib/dff/parser.d.ts +5 -0
- package/lib/dff/parser.js +154 -0
- package/lib/dff/readers/2d-effect.d.ts +3 -0
- package/lib/dff/readers/2d-effect.js +211 -0
- package/lib/dff/readers/geometry.d.ts +4 -0
- package/lib/dff/readers/geometry.js +229 -0
- package/lib/dff/readers/index.d.ts +5 -0
- package/lib/dff/readers/index.js +21 -0
- package/lib/dff/readers/material.d.ts +5 -0
- package/lib/dff/readers/material.js +72 -0
- package/lib/dff/readers/mesh.d.ts +5 -0
- package/lib/dff/readers/mesh.js +84 -0
- package/lib/dff/readers/structure.d.ts +6 -0
- package/lib/dff/readers/structure.js +67 -0
- package/lib/dff/types.d.ts +237 -0
- package/lib/dff/types.js +44 -0
- package/lib/fxp/index.d.ts +2 -0
- package/lib/fxp/index.js +18 -0
- package/lib/fxp/info-parser.d.ts +2 -0
- package/lib/fxp/info-parser.js +169 -0
- package/lib/fxp/parser.d.ts +3 -0
- package/lib/fxp/parser.js +285 -0
- package/lib/fxp/types/common.d.ts +36 -0
- package/lib/fxp/types/common.js +2 -0
- package/lib/fxp/types/index.d.ts +3 -0
- package/lib/fxp/types/index.js +19 -0
- package/lib/fxp/types/info.d.ts +186 -0
- package/lib/fxp/types/info.js +2 -0
- package/lib/fxp/types/system.d.ts +27 -0
- package/lib/fxp/types/system.js +2 -0
- package/lib/ifp/index.d.ts +4 -0
- package/lib/ifp/index.js +20 -0
- package/lib/ifp/parser.d.ts +5 -0
- package/lib/ifp/parser.js +38 -0
- package/lib/ifp/readers/anp3.d.ts +3 -0
- package/lib/ifp/readers/anp3.js +56 -0
- package/lib/ifp/readers/anpk.d.ts +3 -0
- package/lib/ifp/readers/anpk.js +86 -0
- package/lib/ifp/types.d.ts +28 -0
- package/lib/ifp/types.js +9 -0
- package/lib/index.d.ts +6 -9
- package/lib/index.js +6 -13
- package/lib/renderware/fxp/FxpData.d.ts +246 -0
- package/lib/renderware/fxp/FxpData.js +2 -0
- package/lib/renderware/fxp/FxpParser.d.ts +3 -0
- package/lib/renderware/fxp/FxpParser.js +512 -0
- package/lib/renderware/fxp/index.d.ts +2 -0
- package/lib/renderware/fxp/index.js +18 -0
- package/lib/txd/bitmap-decoder.d.ts +3 -0
- package/lib/txd/bitmap-decoder.js +57 -0
- package/lib/txd/index.d.ts +3 -0
- package/lib/txd/index.js +19 -0
- package/lib/txd/parser.d.ts +8 -0
- package/lib/txd/parser.js +122 -0
- package/lib/txd/types.d.ts +27 -0
- package/lib/txd/types.js +2 -0
- package/package.json +16 -9
- package/src/codecs/bitmap.ts +75 -0
- package/src/codecs/color.ts +40 -0
- package/src/codecs/dxt.ts +243 -0
- package/src/codecs/formats.ts +28 -0
- package/src/codecs/index.ts +5 -0
- package/src/codecs/palette.ts +106 -0
- package/src/common/types.ts +59 -0
- package/src/core/byte-stream.ts +87 -0
- package/src/core/errors.ts +7 -0
- package/src/core/index.ts +5 -0
- package/src/core/rw-file.ts +23 -0
- package/src/core/rw-sections.ts +24 -0
- package/src/core/rw-version.ts +24 -0
- package/src/dff/dff-model-type.ts +5 -0
- package/src/dff/index.ts +4 -0
- package/src/dff/parser.ts +142 -0
- package/src/dff/readers/2d-effect.ts +265 -0
- package/src/dff/readers/geometry.ts +264 -0
- package/src/dff/readers/index.ts +5 -0
- package/src/dff/readers/material.ts +89 -0
- package/src/dff/readers/mesh.ts +93 -0
- package/src/dff/readers/structure.ts +79 -0
- package/src/dff/types.ts +278 -0
- package/src/fxp/index.ts +2 -0
- package/src/fxp/info-parser.ts +166 -0
- package/src/fxp/parser.ts +323 -0
- package/src/fxp/types/common.ts +23 -0
- package/src/fxp/types/index.ts +3 -0
- package/src/fxp/types/info.ts +251 -0
- package/src/fxp/types/system.ts +30 -0
- package/src/ifp/index.ts +4 -0
- package/src/ifp/parser.ts +16 -0
- package/src/ifp/readers/anp3.ts +75 -0
- package/src/ifp/readers/anpk.ts +110 -0
- package/src/ifp/types.ts +33 -0
- package/src/index.ts +6 -12
- package/src/txd/bitmap-decoder.ts +73 -0
- package/src/txd/index.ts +3 -0
- package/src/txd/parser.ts +148 -0
- package/src/txd/types.ts +29 -0
- package/src/renderware/RwFile.ts +0 -26
- package/src/renderware/RwSections.ts +0 -27
- package/src/renderware/common/types.ts +0 -59
- package/src/renderware/dff/DffModelType.ts +0 -5
- package/src/renderware/dff/DffParser.ts +0 -611
- package/src/renderware/errors/RwParseError.ts +0 -11
- package/src/renderware/ifp/IfpData.ts +0 -33
- package/src/renderware/ifp/IfpParser.ts +0 -203
- package/src/renderware/txd/TxdParser.ts +0 -235
- package/src/renderware/utils/ImageDecoder.ts +0 -571
- package/src/renderware/utils/ImageFormatEnums.ts +0 -28
- package/src/renderware/utils/RwVersion.ts +0 -28
- package/src/utils/ByteStream.ts +0 -80
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.TxdParser = void 0;
|
|
19
|
+
var formats_1 = require("../codecs/formats");
|
|
20
|
+
var rw_file_1 = require("../core/rw-file");
|
|
21
|
+
var bitmap_decoder_1 = require("./bitmap-decoder");
|
|
22
|
+
var TxdParser = /** @class */ (function (_super) {
|
|
23
|
+
__extends(TxdParser, _super);
|
|
24
|
+
function TxdParser() {
|
|
25
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
26
|
+
}
|
|
27
|
+
TxdParser.prototype.parse = function () {
|
|
28
|
+
return { textureDictionary: this.readTextureDictionary() };
|
|
29
|
+
};
|
|
30
|
+
TxdParser.prototype.readTextureDictionary = function () {
|
|
31
|
+
this.readSectionHeader();
|
|
32
|
+
this.readSectionHeader();
|
|
33
|
+
var textureCount = this.readUint16();
|
|
34
|
+
this.skip(2);
|
|
35
|
+
var textureNatives = [];
|
|
36
|
+
for (var i = 0; i < textureCount; i++) {
|
|
37
|
+
textureNatives.push(this.readTextureNative());
|
|
38
|
+
}
|
|
39
|
+
this.skip(this.readSectionHeader().sectionSize);
|
|
40
|
+
return { textureCount: textureCount, textureNatives: textureNatives };
|
|
41
|
+
};
|
|
42
|
+
TxdParser.prototype.readTextureNative = function () {
|
|
43
|
+
this.readSectionHeader();
|
|
44
|
+
this.readSectionHeader();
|
|
45
|
+
var platformId = this.readUint32();
|
|
46
|
+
var flags = this.readUint32();
|
|
47
|
+
var filterMode = flags & 0xff;
|
|
48
|
+
var uAddressing = (flags & 0xf00) >> 8;
|
|
49
|
+
var vAddressing = (flags & 0xf000) >> 12;
|
|
50
|
+
var textureName = this.readString(32);
|
|
51
|
+
var maskName = this.readString(32);
|
|
52
|
+
var rasterFormat = this.readUint32();
|
|
53
|
+
var d3dFormat = this.readString(4);
|
|
54
|
+
var width = this.readUint16();
|
|
55
|
+
var height = this.readUint16();
|
|
56
|
+
var depth = this.readUint8();
|
|
57
|
+
var mipmapCount = this.readUint8();
|
|
58
|
+
var rasterType = this.readUint8();
|
|
59
|
+
var compressionFlags = this.readUint8();
|
|
60
|
+
var alpha = (compressionFlags & (1 << 0)) !== 0;
|
|
61
|
+
var cubeTexture = (compressionFlags & (1 << 1)) !== 0;
|
|
62
|
+
var autoMipMaps = (compressionFlags & (1 << 2)) !== 0;
|
|
63
|
+
var compressed = (compressionFlags & (1 << 3)) !== 0;
|
|
64
|
+
var paletteType = (rasterFormat >> 13) & 3;
|
|
65
|
+
var pixelFormat = rasterFormat & 0x0f00;
|
|
66
|
+
var palette = paletteType !== formats_1.PaletteType.PALETTE_NONE
|
|
67
|
+
? this.read(paletteType === formats_1.PaletteType.PALETTE_8 ? 1024 : depth === 4 ? 64 : 128)
|
|
68
|
+
: new Uint8Array(0);
|
|
69
|
+
var mipmaps = [];
|
|
70
|
+
for (var i = 0; i < mipmapCount; i++) {
|
|
71
|
+
var rasterSize = this.readUint32();
|
|
72
|
+
var raster = this.read(rasterSize);
|
|
73
|
+
if (i === 0) {
|
|
74
|
+
var bitmap = this.decodeBitmap(platformId, compressionFlags, compressed, alpha, paletteType, depth, pixelFormat, d3dFormat, raster, palette, width, height);
|
|
75
|
+
mipmaps.push(Array.from(bitmap));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
this.skip(this.readSectionHeader().sectionSize);
|
|
79
|
+
return {
|
|
80
|
+
platformId: platformId,
|
|
81
|
+
filterMode: filterMode,
|
|
82
|
+
uAddressing: uAddressing,
|
|
83
|
+
vAddressing: vAddressing,
|
|
84
|
+
textureName: textureName,
|
|
85
|
+
maskName: maskName,
|
|
86
|
+
rasterFormat: rasterFormat,
|
|
87
|
+
d3dFormat: d3dFormat,
|
|
88
|
+
width: width,
|
|
89
|
+
height: height,
|
|
90
|
+
depth: depth,
|
|
91
|
+
mipmapCount: mipmapCount,
|
|
92
|
+
rasterType: rasterType,
|
|
93
|
+
alpha: alpha,
|
|
94
|
+
cubeTexture: cubeTexture,
|
|
95
|
+
autoMipMaps: autoMipMaps,
|
|
96
|
+
compressed: compressed,
|
|
97
|
+
mipmaps: mipmaps,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
TxdParser.prototype.decodeBitmap = function (platformId, compressionFlags, compressed, alpha, paletteType, depth, pixelFormat, d3dFormat, raster, palette, width, height) {
|
|
101
|
+
if (palette.length !== 0) {
|
|
102
|
+
var noAlphaFormats = [
|
|
103
|
+
formats_1.RasterFormat.RASTER_565,
|
|
104
|
+
formats_1.RasterFormat.RASTER_LUM,
|
|
105
|
+
formats_1.RasterFormat.RASTER_888,
|
|
106
|
+
formats_1.RasterFormat.RASTER_555,
|
|
107
|
+
];
|
|
108
|
+
var hasAlpha = (platformId === formats_1.PlatformType.D3D9 && alpha) ||
|
|
109
|
+
(platformId === formats_1.PlatformType.D3D8 && !noAlphaFormats.includes(pixelFormat));
|
|
110
|
+
return (0, bitmap_decoder_1.decodePaletteBitmap)(paletteType, depth, hasAlpha, raster, palette, width, height);
|
|
111
|
+
}
|
|
112
|
+
if (platformId === formats_1.PlatformType.D3D8 && compressionFlags !== 0) {
|
|
113
|
+
return (0, bitmap_decoder_1.decodeDxtBitmap)("DXT".concat(compressionFlags), raster, width, height);
|
|
114
|
+
}
|
|
115
|
+
if (platformId === formats_1.PlatformType.D3D9 && compressed) {
|
|
116
|
+
return (0, bitmap_decoder_1.decodeDxtBitmap)(d3dFormat, raster, width, height);
|
|
117
|
+
}
|
|
118
|
+
return (0, bitmap_decoder_1.decodeRasterBitmap)(pixelFormat, raster, width, height);
|
|
119
|
+
};
|
|
120
|
+
return TxdParser;
|
|
121
|
+
}(rw_file_1.RwFile));
|
|
122
|
+
exports.TxdParser = TxdParser;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface RwTxd {
|
|
2
|
+
textureDictionary: RwTextureDictionary;
|
|
3
|
+
}
|
|
4
|
+
export interface RwTextureDictionary {
|
|
5
|
+
textureCount: number;
|
|
6
|
+
textureNatives: RwTextureNative[];
|
|
7
|
+
}
|
|
8
|
+
export interface RwTextureNative {
|
|
9
|
+
platformId: number;
|
|
10
|
+
filterMode: number;
|
|
11
|
+
uAddressing: number;
|
|
12
|
+
vAddressing: number;
|
|
13
|
+
textureName: string;
|
|
14
|
+
maskName: string;
|
|
15
|
+
rasterFormat: number;
|
|
16
|
+
d3dFormat: string;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
depth: number;
|
|
20
|
+
mipmapCount: number;
|
|
21
|
+
rasterType: number;
|
|
22
|
+
alpha: boolean;
|
|
23
|
+
cubeTexture: boolean;
|
|
24
|
+
autoMipMaps: boolean;
|
|
25
|
+
compressed: boolean;
|
|
26
|
+
mipmaps: number[][];
|
|
27
|
+
}
|
package/lib/txd/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rw-parser-ng",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Parses RenderWare DFF, TXD and IFP files into usable format!",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "DepsCian",
|
|
@@ -35,30 +35,37 @@
|
|
|
35
35
|
"rw-parser",
|
|
36
36
|
"dff",
|
|
37
37
|
"txd",
|
|
38
|
-
"ifp"
|
|
38
|
+
"ifp",
|
|
39
|
+
"fxp"
|
|
39
40
|
],
|
|
40
41
|
"main": "lib/index.js",
|
|
42
|
+
"module": "lib/index.js",
|
|
41
43
|
"types": "lib/index.d.ts",
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"types": "./lib/index.d.ts",
|
|
47
|
+
"import": "./lib/index.js",
|
|
48
|
+
"require": "./lib/index.js"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
42
51
|
"devDependencies": {
|
|
43
|
-
"@
|
|
52
|
+
"@biomejs/biome": "^2.3.10",
|
|
44
53
|
"@types/node": "^24.0.3",
|
|
45
|
-
"jest": "^30.0.0",
|
|
46
|
-
"ts-jest": "^29.4.0",
|
|
47
54
|
"typescript": "^5.8.3"
|
|
48
55
|
},
|
|
49
56
|
"scripts": {
|
|
50
57
|
"build": "tsc -p tsconfig.json",
|
|
51
58
|
"dev": "tsc -p tsconfig.json --watch",
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
59
|
+
"lint": "biome check .",
|
|
60
|
+
"format": "biome format --write .",
|
|
61
|
+
"prepare": "bun run build",
|
|
62
|
+
"test": "bun test"
|
|
55
63
|
},
|
|
56
64
|
"files": [
|
|
57
65
|
"lib",
|
|
58
66
|
"src",
|
|
59
67
|
"LICENSE",
|
|
60
68
|
"README.md",
|
|
61
|
-
"package.json",
|
|
62
69
|
"tsconfig.json"
|
|
63
70
|
]
|
|
64
71
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { decode555, decode565, decode1555, decode4444, readUInt16LE } from "./color";
|
|
2
|
+
|
|
3
|
+
export function bgra1555(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
4
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
5
|
+
let offset = 0;
|
|
6
|
+
for (let i = 0; i < data.length; i += 2) {
|
|
7
|
+
const [a, r, g, b] = decode1555(readUInt16LE(data, i));
|
|
8
|
+
rgba[offset++] = r;
|
|
9
|
+
rgba[offset++] = g;
|
|
10
|
+
rgba[offset++] = b;
|
|
11
|
+
rgba[offset++] = a;
|
|
12
|
+
}
|
|
13
|
+
return rgba;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function bgra4444(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
17
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
18
|
+
let offset = 0;
|
|
19
|
+
for (let i = 0; i < data.length; i += 2) {
|
|
20
|
+
const [a, r, g, b] = decode4444(readUInt16LE(data, i));
|
|
21
|
+
rgba[offset++] = r;
|
|
22
|
+
rgba[offset++] = g;
|
|
23
|
+
rgba[offset++] = b;
|
|
24
|
+
rgba[offset++] = a;
|
|
25
|
+
}
|
|
26
|
+
return rgba;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function bgra555(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
30
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
31
|
+
let offset = 0;
|
|
32
|
+
for (let i = 0; i < data.length; i += 2) {
|
|
33
|
+
const [r, g, b] = decode555(readUInt16LE(data, i));
|
|
34
|
+
rgba[offset++] = r;
|
|
35
|
+
rgba[offset++] = g;
|
|
36
|
+
rgba[offset++] = b;
|
|
37
|
+
rgba[offset++] = 0xff;
|
|
38
|
+
}
|
|
39
|
+
return rgba;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function bgra565(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
43
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
44
|
+
let offset = 0;
|
|
45
|
+
for (let i = 0; i < data.length; i += 2) {
|
|
46
|
+
const [r, g, b] = decode565(readUInt16LE(data, i));
|
|
47
|
+
rgba[offset++] = r;
|
|
48
|
+
rgba[offset++] = g;
|
|
49
|
+
rgba[offset++] = b;
|
|
50
|
+
rgba[offset++] = 0xff;
|
|
51
|
+
}
|
|
52
|
+
return rgba;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function bgra888(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
56
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
57
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
58
|
+
rgba[i] = data[i + 2];
|
|
59
|
+
rgba[i + 1] = data[i + 1];
|
|
60
|
+
rgba[i + 2] = data[i];
|
|
61
|
+
rgba[i + 3] = 0xff;
|
|
62
|
+
}
|
|
63
|
+
return rgba;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function bgra8888(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
67
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
68
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
69
|
+
rgba[i] = data[i + 2];
|
|
70
|
+
rgba[i + 1] = data[i + 1];
|
|
71
|
+
rgba[i + 2] = data[i];
|
|
72
|
+
rgba[i + 3] = data[i + 3];
|
|
73
|
+
}
|
|
74
|
+
return rgba;
|
|
75
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function readUInt16LE(buf: Uint8Array, offset: number): number {
|
|
2
|
+
return buf[offset] | (buf[offset + 1] << 8);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function readUInt32LE(buf: Uint8Array, offset: number): number {
|
|
6
|
+
return buf[offset] | (buf[offset + 1] << 8) | (buf[offset + 2] << 16) | (buf[offset + 3] << 24);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function decode565(bits: number): [number, number, number] {
|
|
10
|
+
const r = (bits >> 11) & 0b11111;
|
|
11
|
+
const g = (bits >> 5) & 0b111111;
|
|
12
|
+
const b = bits & 0b11111;
|
|
13
|
+
return [(r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2)];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function decode555(bits: number): [number, number, number] {
|
|
17
|
+
return [
|
|
18
|
+
Math.round((((bits >> 10) & 0b11111) * 255) / 31),
|
|
19
|
+
Math.round((((bits >> 5) & 0b11111) * 255) / 31),
|
|
20
|
+
Math.round(((bits & 0b11111) * 255) / 31),
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function decode1555(bits: number): [number, number, number, number] {
|
|
25
|
+
return [
|
|
26
|
+
Math.round(((bits >> 15) & 0b1) * 255),
|
|
27
|
+
Math.round((((bits >> 10) & 0b11111) * 255) / 31),
|
|
28
|
+
Math.round((((bits >> 5) & 0b11111) * 255) / 31),
|
|
29
|
+
Math.round(((bits & 0b11111) * 255) / 31),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function decode4444(bits: number): [number, number, number, number] {
|
|
34
|
+
return [
|
|
35
|
+
Math.round((((bits >> 12) & 0b1111) * 255) / 15),
|
|
36
|
+
Math.round((((bits >> 8) & 0b1111) * 255) / 15),
|
|
37
|
+
Math.round((((bits >> 4) & 0b1111) * 255) / 15),
|
|
38
|
+
Math.round(((bits & 0b1111) * 255) / 15),
|
|
39
|
+
];
|
|
40
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { decode565, readUInt16LE, readUInt32LE } from "./color";
|
|
2
|
+
|
|
3
|
+
export function bc1(data: Uint8Array, width: number, height: number): Uint8Array {
|
|
4
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
5
|
+
const colorPalette = new Uint8Array(16);
|
|
6
|
+
let offset = 0;
|
|
7
|
+
|
|
8
|
+
for (let y = 0; y < height; y += 4) {
|
|
9
|
+
for (let x = 0; x < width; x += 4) {
|
|
10
|
+
const color0 = readUInt16LE(data, offset);
|
|
11
|
+
const color1 = readUInt16LE(data, offset + 2);
|
|
12
|
+
let colorBits = readUInt32LE(data, offset + 4);
|
|
13
|
+
offset += 8;
|
|
14
|
+
|
|
15
|
+
const [c0r, c0g, c0b] = decode565(color0);
|
|
16
|
+
const [c1r, c1g, c1b] = decode565(color1);
|
|
17
|
+
|
|
18
|
+
colorPalette[0] = c0r;
|
|
19
|
+
colorPalette[1] = c0g;
|
|
20
|
+
colorPalette[2] = c0b;
|
|
21
|
+
colorPalette[4] = c1r;
|
|
22
|
+
colorPalette[5] = c1g;
|
|
23
|
+
colorPalette[6] = c1b;
|
|
24
|
+
|
|
25
|
+
if (color0 > color1) {
|
|
26
|
+
colorPalette[8] = (2 * c0r + c1r) / 3;
|
|
27
|
+
colorPalette[9] = (2 * c0g + c1g) / 3;
|
|
28
|
+
colorPalette[10] = (2 * c0b + c1b) / 3;
|
|
29
|
+
colorPalette[12] = (c0r + 2 * c1r) / 3;
|
|
30
|
+
colorPalette[13] = (c0g + 2 * c1g) / 3;
|
|
31
|
+
colorPalette[14] = (c0b + 2 * c1b) / 3;
|
|
32
|
+
} else {
|
|
33
|
+
colorPalette[8] = (c0r + c1r) >> 1;
|
|
34
|
+
colorPalette[9] = (c0g + c1g) >> 1;
|
|
35
|
+
colorPalette[10] = (c0b + c1b) >> 1;
|
|
36
|
+
colorPalette[12] = 0;
|
|
37
|
+
colorPalette[13] = 0;
|
|
38
|
+
colorPalette[14] = 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const baseIndex = (y * width + x) * 4;
|
|
42
|
+
for (let k = 0; k < 16; k++) {
|
|
43
|
+
const colorIdx = colorBits & 0x3;
|
|
44
|
+
colorBits >>>= 2;
|
|
45
|
+
const j = k >> 2,
|
|
46
|
+
i = k & 3;
|
|
47
|
+
const idx = baseIndex + ((j * width + i) << 2);
|
|
48
|
+
|
|
49
|
+
rgba[idx] = colorPalette[colorIdx * 4];
|
|
50
|
+
rgba[idx + 1] = colorPalette[colorIdx * 4 + 1];
|
|
51
|
+
rgba[idx + 2] = colorPalette[colorIdx * 4 + 2];
|
|
52
|
+
rgba[idx + 3] = color0 <= color1 && colorIdx === 3 ? 0 : 255;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return rgba;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function bc2(
|
|
60
|
+
data: Uint8Array,
|
|
61
|
+
width: number,
|
|
62
|
+
height: number,
|
|
63
|
+
premultiplied: boolean,
|
|
64
|
+
): Uint8Array {
|
|
65
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
66
|
+
const colorPalette = new Uint8Array(16);
|
|
67
|
+
const alphaTable = new Uint8Array(16);
|
|
68
|
+
for (let i = 0; i < 16; i++) alphaTable[i] = ((i * 255 + 7.5) / 15) | 0;
|
|
69
|
+
|
|
70
|
+
let offset = 0;
|
|
71
|
+
for (let y = 0; y < height; y += 4) {
|
|
72
|
+
for (let x = 0; x < width; x += 4) {
|
|
73
|
+
const alpha0 = readUInt32LE(data, offset);
|
|
74
|
+
const alpha1 = readUInt32LE(data, offset + 4);
|
|
75
|
+
offset += 8;
|
|
76
|
+
|
|
77
|
+
const color0 = readUInt16LE(data, offset);
|
|
78
|
+
const color1 = readUInt16LE(data, offset + 2);
|
|
79
|
+
let colorBits = readUInt32LE(data, offset + 4);
|
|
80
|
+
offset += 8;
|
|
81
|
+
|
|
82
|
+
const [c0r, c0g, c0b] = decode565(color0);
|
|
83
|
+
const [c1r, c1g, c1b] = decode565(color1);
|
|
84
|
+
|
|
85
|
+
colorPalette[0] = c0r;
|
|
86
|
+
colorPalette[1] = c0g;
|
|
87
|
+
colorPalette[2] = c0b;
|
|
88
|
+
colorPalette[4] = c1r;
|
|
89
|
+
colorPalette[5] = c1g;
|
|
90
|
+
colorPalette[6] = c1b;
|
|
91
|
+
|
|
92
|
+
if (color0 > color1) {
|
|
93
|
+
colorPalette[8] = (2 * c0r + c1r) / 3;
|
|
94
|
+
colorPalette[9] = (2 * c0g + c1g) / 3;
|
|
95
|
+
colorPalette[10] = (2 * c0b + c1b) / 3;
|
|
96
|
+
colorPalette[12] = (c0r + 2 * c1r) / 3;
|
|
97
|
+
colorPalette[13] = (c0g + 2 * c1g) / 3;
|
|
98
|
+
colorPalette[14] = (c0b + 2 * c1b) / 3;
|
|
99
|
+
} else {
|
|
100
|
+
colorPalette[8] = (c0r + c1r) >> 1;
|
|
101
|
+
colorPalette[9] = (c0g + c1g) >> 1;
|
|
102
|
+
colorPalette[10] = (c0b + c1b) >> 1;
|
|
103
|
+
colorPalette[12] = 0;
|
|
104
|
+
colorPalette[13] = 0;
|
|
105
|
+
colorPalette[14] = 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const baseIndex = (y * width + x) << 2;
|
|
109
|
+
for (let k = 0; k < 16; k++) {
|
|
110
|
+
const j = k >> 2,
|
|
111
|
+
i = k & 3;
|
|
112
|
+
const idx = baseIndex + ((j * width + i) << 2);
|
|
113
|
+
const colorIdx = colorBits & 0x3;
|
|
114
|
+
colorBits >>>= 2;
|
|
115
|
+
|
|
116
|
+
const bitPos = (j << 2) + i;
|
|
117
|
+
const byteIndex = bitPos >> 3;
|
|
118
|
+
const shift = (bitPos & 7) << 2;
|
|
119
|
+
const alpha4 = ((byteIndex === 0 ? alpha0 : alpha1) >>> shift) & 0xf;
|
|
120
|
+
const alpha = alphaTable[alpha4];
|
|
121
|
+
|
|
122
|
+
rgba[idx] = colorPalette[colorIdx * 4];
|
|
123
|
+
rgba[idx + 1] = colorPalette[colorIdx * 4 + 1];
|
|
124
|
+
rgba[idx + 2] = colorPalette[colorIdx * 4 + 2];
|
|
125
|
+
rgba[idx + 3] = alpha;
|
|
126
|
+
|
|
127
|
+
if (premultiplied && alpha > 0 && alpha < 255) {
|
|
128
|
+
const factor = 255 / alpha;
|
|
129
|
+
rgba[idx] = Math.min(255, Math.round(rgba[idx] * factor));
|
|
130
|
+
rgba[idx + 1] = Math.min(255, Math.round(rgba[idx + 1] * factor));
|
|
131
|
+
rgba[idx + 2] = Math.min(255, Math.round(rgba[idx + 2] * factor));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return rgba;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function bc3(
|
|
140
|
+
data: Uint8Array,
|
|
141
|
+
width: number,
|
|
142
|
+
height: number,
|
|
143
|
+
premultiplied: boolean,
|
|
144
|
+
): Uint8Array {
|
|
145
|
+
const rgba = new Uint8Array(4 * width * height);
|
|
146
|
+
const alphaPalette = new Uint8Array(8);
|
|
147
|
+
const colorPalette = new Uint8Array(16);
|
|
148
|
+
const alphaIndices = new Uint8Array(16);
|
|
149
|
+
let offset = 0;
|
|
150
|
+
|
|
151
|
+
for (let y = 0; y < height; y += 4) {
|
|
152
|
+
for (let x = 0; x < width; x += 4) {
|
|
153
|
+
const alpha0 = data[offset++];
|
|
154
|
+
const alpha1 = data[offset++];
|
|
155
|
+
const alphaBits = data.subarray(offset, offset + 6);
|
|
156
|
+
offset += 6;
|
|
157
|
+
|
|
158
|
+
const color0 = readUInt16LE(data, offset);
|
|
159
|
+
const color1 = readUInt16LE(data, offset + 2);
|
|
160
|
+
let colorBits = readUInt32LE(data, offset + 4);
|
|
161
|
+
offset += 8;
|
|
162
|
+
|
|
163
|
+
const [c0r, c0g, c0b] = decode565(color0);
|
|
164
|
+
const [c1r, c1g, c1b] = decode565(color1);
|
|
165
|
+
|
|
166
|
+
colorPalette[0] = c0r;
|
|
167
|
+
colorPalette[1] = c0g;
|
|
168
|
+
colorPalette[2] = c0b;
|
|
169
|
+
colorPalette[4] = c1r;
|
|
170
|
+
colorPalette[5] = c1g;
|
|
171
|
+
colorPalette[6] = c1b;
|
|
172
|
+
|
|
173
|
+
if (color0 > color1) {
|
|
174
|
+
colorPalette[8] = (2 * c0r + c1r) / 3;
|
|
175
|
+
colorPalette[9] = (2 * c0g + c1g) / 3;
|
|
176
|
+
colorPalette[10] = (2 * c0b + c1b) / 3;
|
|
177
|
+
colorPalette[12] = (c0r + 2 * c1r) / 3;
|
|
178
|
+
colorPalette[13] = (c0g + 2 * c1g) / 3;
|
|
179
|
+
colorPalette[14] = (c0b + 2 * c1b) / 3;
|
|
180
|
+
} else {
|
|
181
|
+
colorPalette[8] = (c0r + c1r) >> 1;
|
|
182
|
+
colorPalette[9] = (c0g + c1g) >> 1;
|
|
183
|
+
colorPalette[10] = (c0b + c1b) >> 1;
|
|
184
|
+
colorPalette[12] = 0;
|
|
185
|
+
colorPalette[13] = 0;
|
|
186
|
+
colorPalette[14] = 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (alpha0 > alpha1) {
|
|
190
|
+
alphaPalette[0] = alpha0;
|
|
191
|
+
alphaPalette[1] = alpha1;
|
|
192
|
+
alphaPalette[2] = (alpha0 * 6 + alpha1 + 3) / 7;
|
|
193
|
+
alphaPalette[3] = (alpha0 * 5 + alpha1 * 2 + 3) / 7;
|
|
194
|
+
alphaPalette[4] = (alpha0 * 4 + alpha1 * 3 + 3) / 7;
|
|
195
|
+
alphaPalette[5] = (alpha0 * 3 + alpha1 * 4 + 3) / 7;
|
|
196
|
+
alphaPalette[6] = (alpha0 * 2 + alpha1 * 5 + 3) / 7;
|
|
197
|
+
alphaPalette[7] = (alpha0 + alpha1 * 6 + 3) / 7;
|
|
198
|
+
} else {
|
|
199
|
+
alphaPalette[0] = alpha0;
|
|
200
|
+
alphaPalette[1] = alpha1;
|
|
201
|
+
alphaPalette[2] = (alpha0 * 4 + alpha1 + 2) / 5;
|
|
202
|
+
alphaPalette[3] = (alpha0 * 3 + alpha1 * 2 + 2) / 5;
|
|
203
|
+
alphaPalette[4] = (alpha0 * 2 + alpha1 * 3 + 2) / 5;
|
|
204
|
+
alphaPalette[5] = (alpha0 + alpha1 * 4 + 2) / 5;
|
|
205
|
+
alphaPalette[6] = 0;
|
|
206
|
+
alphaPalette[7] = 255;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (let k = 0; k < 16; k++) {
|
|
210
|
+
const bitOffset = k * 3;
|
|
211
|
+
const byteOffset = bitOffset >> 3;
|
|
212
|
+
const shift = bitOffset & 7;
|
|
213
|
+
alphaIndices[k] =
|
|
214
|
+
shift <= 5
|
|
215
|
+
? (alphaBits[byteOffset] >> shift) & 0x7
|
|
216
|
+
: ((alphaBits[byteOffset] >> shift) | (alphaBits[byteOffset + 1] << (8 - shift))) & 0x7;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const baseIndex = (y * width + x) << 2;
|
|
220
|
+
for (let k = 0; k < 16; k++) {
|
|
221
|
+
const j = k >> 2,
|
|
222
|
+
i = k & 3;
|
|
223
|
+
const idx = baseIndex + ((j * width + i) << 2);
|
|
224
|
+
const colorIdx = colorBits & 0x3;
|
|
225
|
+
colorBits >>>= 2;
|
|
226
|
+
const alpha = alphaPalette[alphaIndices[k] & 0x7];
|
|
227
|
+
|
|
228
|
+
rgba[idx] = colorPalette[colorIdx * 4];
|
|
229
|
+
rgba[idx + 1] = colorPalette[colorIdx * 4 + 1];
|
|
230
|
+
rgba[idx + 2] = colorPalette[colorIdx * 4 + 2];
|
|
231
|
+
rgba[idx + 3] = alpha;
|
|
232
|
+
|
|
233
|
+
if (premultiplied && alpha > 0 && alpha < 255) {
|
|
234
|
+
const factor = 255 / alpha;
|
|
235
|
+
rgba[idx] = Math.min(255, Math.round(rgba[idx] * factor));
|
|
236
|
+
rgba[idx + 1] = Math.min(255, Math.round(rgba[idx + 1] * factor));
|
|
237
|
+
rgba[idx + 2] = Math.min(255, Math.round(rgba[idx + 2] * factor));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return rgba;
|
|
243
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export enum RasterFormat {
|
|
2
|
+
RASTER_1555 = 0x0100,
|
|
3
|
+
RASTER_565 = 0x0200,
|
|
4
|
+
RASTER_4444 = 0x0300,
|
|
5
|
+
RASTER_LUM = 0x0400,
|
|
6
|
+
RASTER_8888 = 0x0500,
|
|
7
|
+
RASTER_888 = 0x0600,
|
|
8
|
+
RASTER_555 = 0x0a00,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum D3DFormat {
|
|
12
|
+
D3DFMT_A8L8 = "3",
|
|
13
|
+
D3D_DXT1 = "DXT1",
|
|
14
|
+
D3D_DXT2 = "DXT2",
|
|
15
|
+
D3D_DXT3 = "DXT3",
|
|
16
|
+
D3D_DXT4 = "DXT4",
|
|
17
|
+
D3D_DXT5 = "DXT5",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export enum PaletteType {
|
|
21
|
+
PALETTE_NONE = 0,
|
|
22
|
+
PALETTE_8 = 1,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export enum PlatformType {
|
|
26
|
+
D3D8 = 0x8,
|
|
27
|
+
D3D9 = 0x9,
|
|
28
|
+
}
|