-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpalette-to-grayscale
More file actions
executable file
·38 lines (33 loc) · 1.28 KB
/
palette-to-grayscale
File metadata and controls
executable file
·38 lines (33 loc) · 1.28 KB
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
#!/usr/bin/env node
var fs = require('fs')
var PNG = require('pngjs').PNG
var createClassLookup = require('./lib/lookup-class')
if (require.main === module) {
var classData = JSON.parse(fs.readFileSync(process.argv[2]))
convertIndexToGrayLevel(process.stdout, process.stdin, classData)
}
module.exports = convertIndexToGrayLevel
// Most image libraries don't give easy access to the PNG palette index for
// each pixel, giving you instead the rgb values. In our colored label images,
// it's the index into the PNG palette that tells us which class a pixel
// belongs to. This converts such a colored image to a grayscale one where
// the gray value of each pixel is the class number. Harder to inspect
// visually, but easier to process programmatically
function convertIndexToGrayLevel (output, input, classData) {
var getClass = createClassLookup(classData)
input.pipe(
new PNG()
.on('parsed', function () {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var r = (this.width * y + x) << 2
var g = r + 1
var b = r + 2
var value = getClass(this.data[r], this.data[g], this.data[b])
this.data[r] = this.data[g] = this.data[b] = value
}
}
this.pack().pipe(output)
})
)
}