mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: more prog
This commit is contained in:
1 parent
4ffb7cac7c
commit
2f650af0f7
6 files changed
+46
-26
No files matched your search
+18
-10
@@ -1,13 +1,13 @@
|
|||||||
import { times } from 'lodash';
|
import { times } from 'lodash';
|
||||||
import { Vector4Tuple } from 'three';
|
import { Vector4Tuple } from 'three';
|
||||||
import { BinaryStream } from './binary';
|
import { BitStream } from './bit';
|
||||||
|
|
||||||
const MAX_UINT5 = 2 ** 5 - 1;
|
const MAX_UINT5 = 2 ** 5 - 1;
|
||||||
const MAX_UINT6 = 2 ** 6 - 1;
|
const MAX_UINT6 = 2 ** 6 - 1;
|
||||||
|
|
||||||
type InterpolationKey = 0b00 | 0b01 | 0b10 | 0b11;
|
type InterpolationKey = 0b00 | 0b01 | 0b10 | 0b11;
|
||||||
|
|
||||||
export class Bc1Stream extends BinaryStream {
|
export class Bc1Stream extends BitStream {
|
||||||
color() {
|
color() {
|
||||||
const r = this.consume(5) / MAX_UINT5;
|
const r = this.consume(5) / MAX_UINT5;
|
||||||
const g = this.consume(6) / MAX_UINT6;
|
const g = this.consume(6) / MAX_UINT6;
|
||||||
@@ -17,18 +17,27 @@ export class Bc1Stream extends BinaryStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interpolation() {
|
interpolation() {
|
||||||
|
const color0Numeric = this.readBytes(2).readUInt16LE();
|
||||||
const color0 = this.color();
|
const color0 = this.color();
|
||||||
|
const color1Numeric = this.readBytes(2).readUInt16LE();
|
||||||
const color1 = this.color();
|
const color1 = this.color();
|
||||||
const color2 = color0.map(
|
const alpha = color0Numeric > color1Numeric;
|
||||||
(channel0, index) => (channel0 + color1[index]) / 2,
|
const color2 = color0.map((channel0, index) =>
|
||||||
|
alpha
|
||||||
|
? (1 / 2) * channel0 + (1 / 2) * color1[index]
|
||||||
|
: (2 / 3) * channel0 + (1 / 3) * color1[index],
|
||||||
);
|
);
|
||||||
const color3 = [...color2.slice(0, -1), 0];
|
const color3 = alpha
|
||||||
|
? [0, 0, 0, 0]
|
||||||
|
: color0.map(
|
||||||
|
(channel0, index) => (1 / 3) * channel0 + (2 / 3) * color1[index],
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[0b00]: color0,
|
[0b00]: color0,
|
||||||
[0b01]: color1,
|
[0b01]: color0,
|
||||||
[0b10]: color2,
|
[0b10]: color0,
|
||||||
[0b11]: color3,
|
[0b11]: color0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +62,7 @@ export class Bc1Stream extends BinaryStream {
|
|||||||
throw new RangeError('Width and height must be divisible by 4');
|
throw new RangeError('Width and height must be divisible by 4');
|
||||||
}
|
}
|
||||||
|
|
||||||
const count = (width / 4) * (height / 4);
|
const blocks = this.blocks((width / 4) * (height / 4));
|
||||||
const blocks = this.blocks(count);
|
|
||||||
const image = Buffer.alloc(width * height * 4);
|
const image = Buffer.alloc(width * height * 4);
|
||||||
let pixelIndex = 0;
|
let pixelIndex = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
import { times } from 'lodash';
|
||||||
|
|
||||||
type Bit = 0 | 1;
|
type Bit = 0 | 1;
|
||||||
|
|
||||||
export class BinaryStream {
|
export class BitStream {
|
||||||
public index = 0;
|
public index = 0;
|
||||||
bits: Bit[] = [];
|
bits: Bit[] = [];
|
||||||
|
|
||||||
@@ -18,6 +20,10 @@ export class BinaryStream {
|
|||||||
this.index += size;
|
this.index += size;
|
||||||
}
|
}
|
||||||
read(size: number) {
|
read(size: number) {
|
||||||
|
if (size > 32) {
|
||||||
|
throw new RangeError('Cannot read more than 32 bits at a time');
|
||||||
|
}
|
||||||
|
|
||||||
if (this.index + size > this.bits.length) {
|
if (this.index + size > this.bits.length) {
|
||||||
throw new RangeError('Cannot read beyond end of stream');
|
throw new RangeError('Cannot read beyond end of stream');
|
||||||
}
|
}
|
||||||
@@ -26,6 +32,12 @@ export class BinaryStream {
|
|||||||
.slice(this.index, this.index + size)
|
.slice(this.index, this.index + size)
|
||||||
.reduce((accumulator, bit) => (accumulator << 1) | bit, 0 as number);
|
.reduce((accumulator, bit) => (accumulator << 1) | bit, 0 as number);
|
||||||
}
|
}
|
||||||
|
readBytes(size: number) {
|
||||||
|
const buffer = Buffer.from(times(size, () => this.consume(8)));
|
||||||
|
this.skip(-8 * size);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
consume(size: number) {
|
consume(size: number) {
|
||||||
const value = this.read(size);
|
const value = this.read(size);
|
||||||
this.skip(size);
|
this.skip(size);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export class PrimitiveStream {
|
export class ByteStream {
|
||||||
public index = 0;
|
public index = 0;
|
||||||
constructor(public stream: Buffer) {}
|
constructor(public stream: Buffer) {}
|
||||||
|
|
||||||
+10
-6
@@ -177,6 +177,7 @@ enum DdsCaps2 {
|
|||||||
export class DdsStream extends WindowsStream {
|
export class DdsStream extends WindowsStream {
|
||||||
async dds() {
|
async dds() {
|
||||||
this.magicNumber();
|
this.magicNumber();
|
||||||
|
|
||||||
const header = this.header();
|
const header = this.header();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -190,8 +191,11 @@ export class DdsStream extends WindowsStream {
|
|||||||
|
|
||||||
switch (headerDxt10.dxgiFormat) {
|
switch (headerDxt10.dxgiFormat) {
|
||||||
case DxgiFormat.BC1_UNORM_SRGB: {
|
case DxgiFormat.BC1_UNORM_SRGB: {
|
||||||
|
this.skip(1);
|
||||||
|
|
||||||
const image = new Bc1Stream(
|
const image = new Bc1Stream(
|
||||||
this.consume((header.width / 4) * (header.height / 4) * 8),
|
// this.consume((header.width / 4) * (header.height / 4) * 8),
|
||||||
|
this.consumeRemaining(),
|
||||||
).bc1(header.width, header.height);
|
).bc1(header.width, header.height);
|
||||||
|
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
@@ -257,11 +261,11 @@ export class DdsStream extends WindowsStream {
|
|||||||
|
|
||||||
headerDxt10() {
|
headerDxt10() {
|
||||||
return {
|
return {
|
||||||
dxgiFormat: this.uint() as DxgiFormat,
|
dxgiFormat: this.dword() as DxgiFormat,
|
||||||
resourceDimension: this.uint() as DdsDimension,
|
resourceDimension: this.dword() as DdsDimension,
|
||||||
miscFlag: this.uint(),
|
miscFlag: this.dword(),
|
||||||
arraySize: this.uint(),
|
arraySize: this.dword(),
|
||||||
miscFlags2: this.uint(),
|
miscFlags2: this.dword(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { times } from 'lodash';
|
import { times } from 'lodash';
|
||||||
import { PrimitiveStream } from './primitive';
|
import { ByteStream } from './byte';
|
||||||
|
|
||||||
enum KAType {
|
enum KAType {
|
||||||
NONE = 0,
|
NONE = 0,
|
||||||
@@ -55,7 +55,7 @@ export enum VertexAttribute {
|
|||||||
CUBETEXCOORD3 = 19,
|
CUBETEXCOORD3 = 19,
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ScpgStream extends PrimitiveStream {
|
export class ScpgStream extends ByteStream {
|
||||||
vectorN(size: number) {
|
vectorN(size: number) {
|
||||||
return times(size, () => this.float());
|
return times(size, () => this.float());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import { PrimitiveStream } from './primitive';
|
import { ByteStream } from './byte';
|
||||||
|
|
||||||
export class WindowsStream extends PrimitiveStream {
|
export class WindowsStream extends ByteStream {
|
||||||
dword() {
|
dword() {
|
||||||
return this.uint32();
|
return this.uint32();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint() {
|
|
||||||
return this.uint32();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user