-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathbmp.js
More file actions
44 lines (35 loc) · 944 Bytes
/
bmp.js
File metadata and controls
44 lines (35 loc) · 944 Bytes
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
'use strict'
var str2arr = require('../common').str2arr
var sliceEq = require('../common').sliceEq
var readInt16LE = require('../common').readInt16LE
var readInt32LE = require('../common').readInt32LE
var readUInt32LE = require('../common').readUInt32LE
var SIG_BM = str2arr('BM')
module.exports = function (data) {
if (data.length < 26) return
if (!sliceEq(data, 0, SIG_BM)) return
var h
var w
var headerSize = readUInt32LE(data, 14)
if (headerSize === 12) {
// BMP v2 header
w = readInt16LE(data, 18)
h = readInt16LE(data, 20)
} else if (headerSize > 12) {
// BMP v3+ header
w = readInt32LE(data, 18)
h = readInt32LE(data, 22)
} else {
// BPM v1 and other garbage (10 bytes usually)
return
}
return {
width: w,
// Height can be negative to indicate a top-down bitmap
height: Math.abs(h),
type: 'bmp',
mime: 'image/bmp',
wUnits: 'px',
hUnits: 'px'
}
}