rw-parser-ng 2.0.5 → 2.1.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.
@@ -1,234 +1,234 @@
1
- import { RwFile } from '../RwFile';
2
- import { ImageDecoder } from '../utils/ImageDecoder';
3
- import {
4
- D3DFormat,
5
- PaletteType,
6
- PlatformType,
7
- RasterFormat
8
- } from '../utils/ImageFormatEnums';
9
-
10
- export interface RwTxd {
11
- textureDictionary: RwTextureDictionary,
12
- }
13
-
14
- export interface RwTextureDictionary {
15
- textureCount: number,
16
- textureNatives: RwTextureNative[],
17
- }
18
-
19
- export interface RwTextureNative {
20
- platformId: number,
21
- filterMode: number,
22
- uAddressing: number,
23
- vAddressing: number,
24
- textureName: string,
25
- maskName: string,
26
- rasterFormat: number,
27
- d3dFormat: string,
28
- width: number,
29
- height: number,
30
- depth: number,
31
- mipmapCount: number,
32
- rasterType: number,
33
- alpha: boolean,
34
- cubeTexture: boolean,
35
- autoMipMaps: boolean,
36
- compressed: boolean,
37
- mipmaps: number[][],
38
- }
39
-
40
- export class TxdParser extends RwFile {
41
-
42
- constructor(stream: Buffer) {
43
- super(stream);
44
- }
45
-
46
- parse(): RwTxd {
47
- return {
48
- textureDictionary: this.readTextureDictionary(),
49
- };
50
- }
51
-
52
- public readTextureDictionary(): RwTextureDictionary {
53
- this.readSectionHeader();
54
- this.readSectionHeader();
55
-
56
- const textureCount = this.readUint16();
57
- this.skip(2);
58
-
59
- let textureNatives: RwTextureNative[] = [];
60
-
61
- for (let i = 0; i < textureCount; i++) {
62
- let textureNative = this.readTextureNative();
63
- textureNatives.push(textureNative);
64
- }
65
-
66
- // Skip unused extension
67
- this.skip(this.readSectionHeader().sectionSize);
68
-
69
- return { textureCount, textureNatives };
70
- }
71
-
72
- public readTextureNative() : RwTextureNative {
73
- this.readSectionHeader();
74
- this.readSectionHeader();
75
-
76
- // TODO: Structure this part better
77
- // Texture format
78
- const platformId = this.readUint32();
79
- const flags = this.readUint32();
80
-
81
- const filterMode = (flags & 0xFF);
82
- const uAddressing = (flags & 0xF00) >> 8;
83
- const vAddressing = (flags & 0xF000) >> 12;
84
-
85
- const textureName = this.readString(32);
86
- const maskName = this.readString(32);
87
-
88
- // Raster format
89
- const rasterFormat = this.readUint32();
90
-
91
- const d3dFormat = this.readString(4);
92
- const width = this.readUint16();
93
- const height = this.readUint16();
94
- const depth = this.readUint8();
95
- const mipmapCount = this.readUint8();
96
- const rasterType = this.readUint8();
97
-
98
- const compressionFlags = this.readUint8(); // Is "dxtType" for III/VC
99
-
100
- // SA
101
- const alpha = (compressionFlags & (1 << 0)) !== 0;
102
- const cubeTexture = (compressionFlags & (1 << 1)) !== 0;
103
- const autoMipMaps = (compressionFlags & (1 << 2)) !== 0;
104
- const compressed = (compressionFlags & (1 << 3)) !== 0;
105
-
106
- const paletteType = (rasterFormat >> 13) & 0b11;
107
-
108
- let mipWidth = width;
109
- let mipHeight = height;
110
-
111
- let mipmaps: number[][] = [];
112
-
113
- const palette = (paletteType !== PaletteType.PALETTE_NONE ? this.readPalette(paletteType, depth) : new Uint8Array(0));
114
-
115
- for (let i = 0; i < mipmapCount; i++) {
116
-
117
- const rasterSize = this.readUint32();
118
- const raster = this.read(rasterSize);
119
-
120
- if (i == 0) {
121
- // Raw RGBA presentation
122
- let bitmap: number[];
123
-
124
- if (palette.length !== 0) {
125
- const rasterFormatsWithoutAlpha = [
126
- RasterFormat.RASTER_565,
127
- RasterFormat.RASTER_LUM,
128
- RasterFormat.RASTER_888,
129
- RasterFormat.RASTER_555
130
- ];
131
-
132
- const hasAlpha = ((platformId === PlatformType.D3D9 && alpha) || (platformId == PlatformType.D3D8 && !rasterFormatsWithoutAlpha.includes(rasterFormat)));
133
-
134
- bitmap = Array.from(this.getBitmapWithPalette(paletteType, depth, hasAlpha, raster, palette, width, height));
135
- }
136
- else if (platformId === PlatformType.D3D8 && compressionFlags !== 0) {
137
- bitmap = Array.from(this.getBitmapWithDXT('DXT' + compressionFlags, raster, width, height));
138
- }
139
- else if (platformId === PlatformType.D3D9 && compressed) {
140
- bitmap = Array.from(this.getBitmapWithDXT(d3dFormat, raster, width, height));
141
- }
142
- else {
143
- bitmap = Array.from(this.getBitmapWithRasterFormat(rasterFormat, raster, width, height))
144
- }
145
-
146
- mipmaps.push(bitmap);
147
- }
148
-
149
- mipWidth /= 2;
150
- mipHeight /= 2;
151
- }
152
-
153
- // Skip extension
154
- this.skip(this.readSectionHeader().sectionSize);
155
-
156
- return {
157
- platformId,
158
- filterMode,
159
- uAddressing, vAddressing,
160
- textureName, maskName,
161
- rasterFormat,
162
- d3dFormat,
163
- width, height, depth,
164
- mipmapCount,
165
- rasterType,
166
- alpha,
167
- cubeTexture,
168
- autoMipMaps,
169
- compressed,
170
- mipmaps,
171
- };
172
- }
173
-
174
- public readPalette(paletteType: number, depth: number): Uint8Array {
175
- const size = (paletteType === PaletteType.PALETTE_8 ? 1024 : (depth === 4 ? 64 : 128))
176
-
177
- return this.read(size);
178
- }
179
-
180
- public getBitmapWithPalette(paletteType: number, depth: number, hasAlpha: boolean, raster: Uint8Array, palette: Uint8Array, width: number, height: number): Uint8Array {
181
- if (paletteType !== PaletteType.PALETTE_8 && depth == 4) {
182
- return (hasAlpha
183
- ? ImageDecoder.pal4(raster, palette, width, height)
184
- : ImageDecoder.pal4NoAlpha(raster, palette, width, height)
185
- );
186
- }
187
-
188
- return (hasAlpha
189
- ? ImageDecoder.pal8(raster, palette, width, height)
190
- : ImageDecoder.pal8NoAlpha(raster, palette, width, height)
191
- )
192
- }
193
-
194
- public getBitmapWithDXT(dxtType:string, raster: Uint8Array, width: number, height: number): Uint8Array {
195
- switch (dxtType) {
196
- case D3DFormat.D3D_DXT1:
197
- return ImageDecoder.bc1(raster, width, height);
198
- case D3DFormat.D3D_DXT2:
199
- return ImageDecoder.bc2(raster, width, height, true);
200
- case D3DFormat.D3D_DXT3:
201
- return ImageDecoder.bc2(raster, width, height, false);
202
- case D3DFormat.D3D_DXT4:
203
- return ImageDecoder.bc3(raster, width, height, true);
204
- case D3DFormat.D3D_DXT5:
205
- return ImageDecoder.bc3(raster, width, height, false);
206
- // LUM8_A8 has compressed flag
207
- case D3DFormat.D3DFMT_A8L8:
208
- return ImageDecoder.lum8a8(raster, width, height);
209
- default:
210
- return new Uint8Array(0);
211
- }
212
- }
213
-
214
- public getBitmapWithRasterFormat (rasterFormat: number, raster: Uint8Array, width: number, height: number): Uint8Array {
215
- switch (rasterFormat) {
216
- case RasterFormat.RASTER_1555:
217
- return ImageDecoder.bgra1555(raster, width, height);
218
- case RasterFormat.RASTER_565:
219
- return ImageDecoder.bgra565(raster, width, height);
220
- case RasterFormat.RASTER_4444:
221
- return ImageDecoder.bgra4444(raster, width, height);
222
- case RasterFormat.RASTER_LUM:
223
- return ImageDecoder.lum8(raster, width, height);
224
- case RasterFormat.RASTER_8888:
225
- return ImageDecoder.bgra8888(raster, width, height);
226
- case RasterFormat.RASTER_888:
227
- return ImageDecoder.bgra888(raster, width, height);
228
- case RasterFormat.RASTER_555:
229
- return ImageDecoder.bgra555(raster, width, height);
230
- default:
231
- return new Uint8Array(0);
232
- }
233
- }
234
- }
1
+ import { RwFile } from '../RwFile';
2
+ import { ImageDecoder } from '../utils/ImageDecoder';
3
+ import {
4
+ D3DFormat,
5
+ PaletteType,
6
+ PlatformType,
7
+ RasterFormat
8
+ } from '../utils/ImageFormatEnums';
9
+
10
+ export interface RwTxd {
11
+ textureDictionary: RwTextureDictionary,
12
+ }
13
+
14
+ export interface RwTextureDictionary {
15
+ textureCount: number,
16
+ textureNatives: RwTextureNative[],
17
+ }
18
+
19
+ export interface RwTextureNative {
20
+ platformId: number,
21
+ filterMode: number,
22
+ uAddressing: number,
23
+ vAddressing: number,
24
+ textureName: string,
25
+ maskName: string,
26
+ rasterFormat: number,
27
+ d3dFormat: string,
28
+ width: number,
29
+ height: number,
30
+ depth: number,
31
+ mipmapCount: number,
32
+ rasterType: number,
33
+ alpha: boolean,
34
+ cubeTexture: boolean,
35
+ autoMipMaps: boolean,
36
+ compressed: boolean,
37
+ mipmaps: number[][],
38
+ }
39
+
40
+ export class TxdParser extends RwFile {
41
+
42
+ constructor(stream: Buffer) {
43
+ super(stream);
44
+ }
45
+
46
+ parse(): RwTxd {
47
+ return {
48
+ textureDictionary: this.readTextureDictionary(),
49
+ };
50
+ }
51
+
52
+ public readTextureDictionary(): RwTextureDictionary {
53
+ this.readSectionHeader();
54
+ this.readSectionHeader();
55
+
56
+ const textureCount = this.readUint16();
57
+ this.skip(2);
58
+
59
+ let textureNatives: RwTextureNative[] = [];
60
+
61
+ for (let i = 0; i < textureCount; i++) {
62
+ let textureNative = this.readTextureNative();
63
+ textureNatives.push(textureNative);
64
+ }
65
+
66
+ // Skip unused extension
67
+ this.skip(this.readSectionHeader().sectionSize);
68
+
69
+ return { textureCount, textureNatives };
70
+ }
71
+
72
+ public readTextureNative() : RwTextureNative {
73
+ this.readSectionHeader();
74
+ this.readSectionHeader();
75
+
76
+ // TODO: Structure this part better
77
+ // Texture format
78
+ const platformId = this.readUint32();
79
+ const flags = this.readUint32();
80
+
81
+ const filterMode = (flags & 0xFF);
82
+ const uAddressing = (flags & 0xF00) >> 8;
83
+ const vAddressing = (flags & 0xF000) >> 12;
84
+
85
+ const textureName = this.readString(32);
86
+ const maskName = this.readString(32);
87
+
88
+ // Raster format
89
+ const rasterFormat = this.readUint32();
90
+
91
+ const d3dFormat = this.readString(4);
92
+ const width = this.readUint16();
93
+ const height = this.readUint16();
94
+ const depth = this.readUint8();
95
+ const mipmapCount = this.readUint8();
96
+ const rasterType = this.readUint8();
97
+
98
+ const compressionFlags = this.readUint8(); // Is "dxtType" for III/VC
99
+
100
+ // SA
101
+ const alpha = (compressionFlags & (1 << 0)) !== 0;
102
+ const cubeTexture = (compressionFlags & (1 << 1)) !== 0;
103
+ const autoMipMaps = (compressionFlags & (1 << 2)) !== 0;
104
+ const compressed = (compressionFlags & (1 << 3)) !== 0;
105
+
106
+ const paletteType = (rasterFormat >> 13) & 0b11;
107
+
108
+ let mipWidth = width;
109
+ let mipHeight = height;
110
+
111
+ let mipmaps: number[][] = [];
112
+
113
+ const palette = (paletteType !== PaletteType.PALETTE_NONE ? this.readPalette(paletteType, depth) : new Uint8Array(0));
114
+
115
+ for (let i = 0; i < mipmapCount; i++) {
116
+
117
+ const rasterSize = this.readUint32();
118
+ const raster = this.read(rasterSize);
119
+
120
+ if (i == 0) {
121
+ // Raw RGBA presentation
122
+ let bitmap: number[];
123
+
124
+ if (palette.length !== 0) {
125
+ const rasterFormatsWithoutAlpha = [
126
+ RasterFormat.RASTER_565,
127
+ RasterFormat.RASTER_LUM,
128
+ RasterFormat.RASTER_888,
129
+ RasterFormat.RASTER_555
130
+ ];
131
+
132
+ const hasAlpha = ((platformId === PlatformType.D3D9 && alpha) || (platformId == PlatformType.D3D8 && !rasterFormatsWithoutAlpha.includes(rasterFormat)));
133
+
134
+ bitmap = Array.from(this.getBitmapWithPalette(paletteType, depth, hasAlpha, raster, palette, width, height));
135
+ }
136
+ else if (platformId === PlatformType.D3D8 && compressionFlags !== 0) {
137
+ bitmap = Array.from(this.getBitmapWithDXT('DXT' + compressionFlags, raster, width, height));
138
+ }
139
+ else if (platformId === PlatformType.D3D9 && compressed) {
140
+ bitmap = Array.from(this.getBitmapWithDXT(d3dFormat, raster, width, height));
141
+ }
142
+ else {
143
+ bitmap = Array.from(this.getBitmapWithRasterFormat(rasterFormat, raster, width, height))
144
+ }
145
+
146
+ mipmaps.push(bitmap);
147
+ }
148
+
149
+ mipWidth /= 2;
150
+ mipHeight /= 2;
151
+ }
152
+
153
+ // Skip extension
154
+ this.skip(this.readSectionHeader().sectionSize);
155
+
156
+ return {
157
+ platformId,
158
+ filterMode,
159
+ uAddressing, vAddressing,
160
+ textureName, maskName,
161
+ rasterFormat,
162
+ d3dFormat,
163
+ width, height, depth,
164
+ mipmapCount,
165
+ rasterType,
166
+ alpha,
167
+ cubeTexture,
168
+ autoMipMaps,
169
+ compressed,
170
+ mipmaps,
171
+ };
172
+ }
173
+
174
+ public readPalette(paletteType: number, depth: number): Uint8Array {
175
+ const size = (paletteType === PaletteType.PALETTE_8 ? 1024 : (depth === 4 ? 64 : 128))
176
+
177
+ return this.read(size);
178
+ }
179
+
180
+ public getBitmapWithPalette(paletteType: number, depth: number, hasAlpha: boolean, raster: Uint8Array, palette: Uint8Array, width: number, height: number): Uint8Array {
181
+ if (paletteType !== PaletteType.PALETTE_8 && depth == 4) {
182
+ return (hasAlpha
183
+ ? ImageDecoder.pal4(raster, palette, width, height)
184
+ : ImageDecoder.pal4NoAlpha(raster, palette, width, height)
185
+ );
186
+ }
187
+
188
+ return (hasAlpha
189
+ ? ImageDecoder.pal8(raster, palette, width, height)
190
+ : ImageDecoder.pal8NoAlpha(raster, palette, width, height)
191
+ )
192
+ }
193
+
194
+ public getBitmapWithDXT(dxtType:string, raster: Uint8Array, width: number, height: number): Uint8Array {
195
+ switch (dxtType) {
196
+ case D3DFormat.D3D_DXT1:
197
+ return ImageDecoder.bc1(raster, width, height);
198
+ case D3DFormat.D3D_DXT2:
199
+ return ImageDecoder.bc2(raster, width, height, true);
200
+ case D3DFormat.D3D_DXT3:
201
+ return ImageDecoder.bc2(raster, width, height, false);
202
+ case D3DFormat.D3D_DXT4:
203
+ return ImageDecoder.bc3(raster, width, height, true);
204
+ case D3DFormat.D3D_DXT5:
205
+ return ImageDecoder.bc3(raster, width, height, false);
206
+ // LUM8_A8 has compressed flag
207
+ case D3DFormat.D3DFMT_A8L8:
208
+ return ImageDecoder.lum8a8(raster, width, height);
209
+ default:
210
+ return new Uint8Array(0);
211
+ }
212
+ }
213
+
214
+ public getBitmapWithRasterFormat (rasterFormat: number, raster: Uint8Array, width: number, height: number): Uint8Array {
215
+ switch (rasterFormat) {
216
+ case RasterFormat.RASTER_1555:
217
+ return ImageDecoder.bgra1555(raster, width, height);
218
+ case RasterFormat.RASTER_565:
219
+ return ImageDecoder.bgra565(raster, width, height);
220
+ case RasterFormat.RASTER_4444:
221
+ return ImageDecoder.bgra4444(raster, width, height);
222
+ case RasterFormat.RASTER_LUM:
223
+ return ImageDecoder.lum8(raster, width, height);
224
+ case RasterFormat.RASTER_8888:
225
+ return ImageDecoder.bgra8888(raster, width, height);
226
+ case RasterFormat.RASTER_888:
227
+ return ImageDecoder.bgra888(raster, width, height);
228
+ case RasterFormat.RASTER_555:
229
+ return ImageDecoder.bgra555(raster, width, height);
230
+ default:
231
+ return new Uint8Array(0);
232
+ }
233
+ }
234
+ }