MCPcopy Create free account
hub / github.com/Timic3/rw-parser / bc2

Method bc2

src/renderware/utils/ImageDecoder.ts:153–232  ·  view source on GitHub ↗
(data: Uint8Array, width: number, height: number, premultiplied: boolean)

Source from the content-addressed store, hash-verified

151 in DXT2, the color data is interpreted as being premultiplied by alpha
152 */
153 static bc2(data: Uint8Array, width: number, height: number, premultiplied: boolean): Uint8Array {
154 const rgba = new Uint8Array(4 * width * height);
155 const colorPalette = new Uint8Array(16);
156
157 const alphaTable = new Uint8Array(16);
158 for (let i = 0; i < 16; i++) {
159 alphaTable[i] = (i * 255 + 7.5) / 15 | 0;
160 }
161
162 let offset = 0;
163
164 for (let y = 0; y < height; y += 4) {
165 for (let x = 0; x < width; x += 4) {
166 const alpha0 = ImageDecoder.readUInt32LE(data, offset);
167 const alpha1 = ImageDecoder.readUInt32LE(data, offset + 4);
168 offset += 8;
169
170 const color0 = ImageDecoder.readUInt16LE(data, offset);
171 const color1 = ImageDecoder.readUInt16LE(data, offset + 2);
172 let colorBits = ImageDecoder.readUInt32LE(data, offset + 4);
173 offset += 8;
174
175 let [c0r, c0g, c0b] = ImageDecoder.decode565(color0);
176 let [c1r, c1g, c1b] = ImageDecoder.decode565(color1);
177
178 if (color0 > color1) {
179 colorPalette[0] = c0r; colorPalette[1] = c0g; colorPalette[2] = c0b;
180 colorPalette[4] = c1r; colorPalette[5] = c1g; colorPalette[6] = c1b;
181 colorPalette[8] = (2 * c0r + c1r) / 3;
182 colorPalette[9] = (2 * c0g + c1g) / 3;
183 colorPalette[10] = (2 * c0b + c1b) / 3;
184 colorPalette[12] = (c0r + 2 * c1r) / 3;
185 colorPalette[13] = (c0g + 2 * c1g) / 3;
186 colorPalette[14] = (c0b + 2 * c1b) / 3;
187 }
188 else {
189 colorPalette[0] = c0r; colorPalette[1] = c0g; colorPalette[2] = c0b;
190 colorPalette[4] = c1r; colorPalette[5] = c1g; colorPalette[6] = c1b;
191 colorPalette[8] = (c0r + c1r) >> 1;
192 colorPalette[9] = (c0g + c1g) >> 1;
193 colorPalette[10] = (c0b + c1b) >> 1;
194 colorPalette[12] = 0; colorPalette[13] = 0; colorPalette[14] = 0;
195 }
196
197 const baseIndex = ((y * width + x) << 2);
198
199 for (let k = 0; k < 16; k++) {
200 const j = k >> 2;
201 const i = k & 3;
202
203 const idx = baseIndex + ((((j * width + i) << 2)));
204
205 const colorIdx = colorBits & 0x3;
206 colorBits >>>= 2;
207
208 const bitPos = (j << 2) + i;
209 const byteIndex = bitPos >> 3;
210 const shift = (bitPos & 7) << 2;

Callers 1

getBitmapWithDXTMethod · 0.80

Calls 3

readUInt32LEMethod · 0.80
readUInt16LEMethod · 0.80
decode565Method · 0.80

Tested by

no test coverage detected