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

Method bc3

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

Source from the content-addressed store, hash-verified

256 in DXT4, the color data is interpreted as being premultiplied by alpha
257 */
258 static bc3(data: Uint8Array, width: number, height: number, premultiplied: boolean): Uint8Array {
259 const rgba = new Uint8Array(4 * width * height);
260 const alphaPalette = new Uint8Array(8);
261 const colorPalette = new Uint8Array(16);
262 const alphaIndices = new Uint8Array(16);
263 let offset = 0;
264
265 for (let y = 0; y < height; y += 4) {
266 for (let x = 0; x < width; x += 4) {
267 const alpha0 = data[offset++];
268 const alpha1 = data[offset++];
269
270 const alphaBits = data.subarray(offset, offset + 6);
271 offset += 6;
272
273 const color0 = ImageDecoder.readUInt16LE(data, offset);
274 const color1 = ImageDecoder.readUInt16LE(data, offset + 2);
275 let colorBits = ImageDecoder.readUInt32LE(data, offset + 4);
276 offset += 8;
277
278 let [c0r, c0g, c0b] = ImageDecoder.decode565(color0);
279 let [c1r, c1g, c1b] = ImageDecoder.decode565(color1);
280
281 if (color0 > color1) {
282 colorPalette[0] = c0r; colorPalette[1] = c0g; colorPalette[2] = c0b;
283 colorPalette[4] = c1r; colorPalette[5] = c1g; colorPalette[6] = c1b;
284 colorPalette[8] = (2 * c0r + c1r) / 3;
285 colorPalette[9] = (2 * c0g + c1g) / 3;
286 colorPalette[10] = (2 * c0b + c1b) / 3;
287 colorPalette[12] = (c0r + 2 * c1r) / 3;
288 colorPalette[13] = (c0g + 2 * c1g) / 3;
289 colorPalette[14] = (c0b + 2 * c1b) / 3;
290 }
291 else {
292 colorPalette[0] = c0r; colorPalette[1] = c0g; colorPalette[2] = c0b;
293 colorPalette[4] = c1r; colorPalette[5] = c1g; colorPalette[6] = c1b;
294 colorPalette[8] = (c0r + c1r) >> 1;
295 colorPalette[9] = (c0g + c1g) >> 1;
296 colorPalette[10] = (c0b + c1b) >> 1;
297 colorPalette[12] = 0; colorPalette[13] = 0; colorPalette[14] = 0;
298 }
299
300 if (alpha0 > alpha1) {
301 alphaPalette[0] = alpha0;
302 alphaPalette[1] = alpha1;
303 alphaPalette[2] = (alpha0 * 6 + alpha1 * 1 + 3) / 7;
304 alphaPalette[3] = (alpha0 * 5 + alpha1 * 2 + 3) / 7;
305 alphaPalette[4] = (alpha0 * 4 + alpha1 * 3 + 3) / 7;
306 alphaPalette[5] = (alpha0 * 3 + alpha1 * 4 + 3) / 7;
307 alphaPalette[6] = (alpha0 * 2 + alpha1 * 5 + 3) / 7;
308 alphaPalette[7] = (alpha0 * 1 + alpha1 * 6 + 3) / 7;
309 }
310 else {
311 alphaPalette[0] = alpha0;
312 alphaPalette[1] = alpha1;
313 alphaPalette[2] = (alpha0 * 4 + alpha1 * 1 + 2) / 5;
314 alphaPalette[3] = (alpha0 * 3 + alpha1 * 2 + 2) / 5;
315 alphaPalette[4] = (alpha0 * 2 + alpha1 * 3 + 2) / 5;

Callers 1

getBitmapWithDXTMethod · 0.80

Calls 3

readUInt16LEMethod · 0.80
readUInt32LEMethod · 0.80
decode565Method · 0.80

Tested by

no test coverage detected