-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitmap.ts
137 lines (112 loc) · 2.99 KB
/
Bitmap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
interface HeaderType {
size : number,
name : string
}
const headerType : HeaderType[] = [
{
size: 12,
name: 'BITMAPCOREHEADER'
},
{
size: 16,
name: 'OS22XBITMAPHEADER'
},
{
size: 40,
name: 'BITMAPINFOHEADER'
},
{
size: 52,
name: 'BITMAPV2INFOHEADER'
},
{
size: 56,
name: 'BITMAPV3INFOHEADER'
},
{
size: 64,
name: 'OS22XBITMAPHEADER'
},
{
size: 108,
name: 'BITMAPV4HEADER'
},
{
size: 124,
name: 'BITMAPV5HEADER'
}
];
const decoder = new TextDecoder();
/**
*
* @param array
*/
function uInt8ArrayToNumber( array : Uint8Array ) : number {
let currentPower = 1;
return array.reduce( ( previousValue, currentValue ) => {
previousValue += currentValue * currentPower;
currentPower *= 256;
return previousValue;
}, 0 );
}
export class Bitmap {
private bitmap : Uint8Array;
/**
*
* @param bitmap
*/
constructor( bitmap : Uint8Array ) {
this.bitmap = bitmap;
}
public get bitmapData() : Uint8Array {
return this.bitmap;
}
public get signature() : string {
return decoder.decode( this.bitmap.subarray( 0, 2 ) );
}
public get fileSize() : number {
return this.bitmap.subarray( 2, 6 ).buffer.byteLength;
}
public get reserved() : number {
return 0;
}
public get dataOffset() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 10, 14 ) );
}
public get size() : HeaderType | undefined {
return headerType.find( type => type.size === uInt8ArrayToNumber( this.bitmap.subarray( 14, 18 ) ) );
}
public get width() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 18, 22 ) );
}
public get height() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 22, 26 ) );
}
public get colorPlanes() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 26, 28 ) );
}
public get bitsPerPixel() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 28, 30 ) );
}
public get compression() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 30, 34 ) );
}
public get imageSize() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 34, 38 ) );
}
public get xPixelsPerMeter() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 38, 42 ) );
}
public get yPixelsPerMeter() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 42, 46 ) );
}
public get colorsUsed() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 46, 50 ) );
}
public get importantColors() : number {
return uInt8ArrayToNumber( this.bitmap.subarray( 50, 54 ) );
}
public getPixelData() : Uint8Array {
return this.bitmap.subarray( 14 + ( this.size?.size || 12 ) );
}
}