drek-decoder
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -72,3 +72,3 @@ #!/usr/bin/env node | ||
Decoder.writePng(path, canvas).then(() => { | ||
Decoder.writePng(canvas, path).then(() => { | ||
console.log("File saved"); | ||
@@ -75,0 +75,0 @@ }) |
exports.readFile = require('./src/read-file'); | ||
exports.writePng = require('./src/write-png'); |
{ | ||
"name": "drek-decoder", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "DREK ( .drk ) image decoder", | ||
@@ -5,0 +5,0 @@ "repository": { |
# DrekDecoder | ||
## Installation | ||
``` | ||
npm i drek-decoder | ||
``` | ||
## Usage | ||
@@ -20,3 +25,3 @@ | ||
Decoder.writePng('./img.png', canvas).then(() => { | ||
Decoder.writePng(canvas, './img.png').then(() => { | ||
console.log("File saved"); | ||
@@ -23,0 +28,0 @@ }); |
@@ -9,7 +9,7 @@ module.exports = class Canvas { | ||
// RGBA | ||
this.data = new Buffer(width * height * 4); | ||
this.data = new Uint8ClampedArray(width * height * 4); | ||
this.teamMask = new Uint8ClampedArray(width * height); | ||
} | ||
setPixel(x, y, [r, g, b, a]) { | ||
setPixel(x, y, [r, g, b, a], teamMask = false) { | ||
let di = (y * this.width + x) * 4; | ||
@@ -21,4 +21,6 @@ | ||
this.data[di + 3] = a; | ||
this.teamMask[y * this.width + x] = teamMask ? 255 : 0; | ||
} | ||
} |
@@ -10,2 +10,5 @@ var FrameDecoder = require('./util/frame-decoder'); | ||
this.offsetX = 0; | ||
this.offsetY = 0; | ||
this.scanLineOffsets = null; | ||
@@ -26,3 +29,5 @@ this.data = null; | ||
width: this.width, | ||
height: this.height | ||
height: this.height, | ||
offsetX: this.offsetX, | ||
offsetY: this.offsetY | ||
}; | ||
@@ -29,0 +34,0 @@ } |
@@ -9,2 +9,4 @@ module.exports = class Image { | ||
this.teamColorCount = 0; | ||
} | ||
@@ -11,0 +13,0 @@ |
module.exports = class CanvasPainter { | ||
constructor(canvas, palette) { | ||
constructor(canvas, palette, teamColorCount) { | ||
this.canvas = canvas; | ||
@@ -8,2 +8,4 @@ | ||
this.teamColorCount = teamColorCount; | ||
this.x = 0; | ||
@@ -18,3 +20,5 @@ this.y = 0; | ||
this.canvas.setPixel(this.x, this.y, [r, g, b, a]); | ||
let isTeamColor = (255-palette_index) < this.teamColorCount; | ||
this.canvas.setPixel(this.x, this.y, [r, g, b, a], isTeamColor); | ||
this.step(); | ||
@@ -21,0 +25,0 @@ } |
@@ -51,4 +51,7 @@ var Image = require('../image'); | ||
const palette_size = 3 * 256; | ||
const team_color_count = 16;//this.data.readInt16LE(0x002a) / 3; | ||
image.palette = this.data.slice(palette_offset, palette_offset + palette_size); | ||
image.teamColorCount = team_color_count; | ||
} | ||
@@ -60,2 +63,4 @@ | ||
let ys = data.readInt32LE(0x0018); | ||
let xo = data.readInt32LE(0x001c); | ||
let yo = data.readInt32LE(0x0020); | ||
@@ -67,2 +72,5 @@ let frame = new Frame(img); | ||
frame.offsetX = xo; | ||
frame.offsetY = yo; | ||
const scanline_start = 0x0068; | ||
@@ -69,0 +77,0 @@ const scanline_size = ys * 4; |
@@ -21,3 +21,3 @@ var Canvas = require('../canvas'); | ||
let canvas = new Canvas(this.frame.width, this.frame.height); | ||
let painter = new CanvasPainter(canvas, this.frame.image.palette); | ||
let painter = new CanvasPainter(canvas, this.frame.image.palette, this.frame.image.teamColorCount); | ||
@@ -24,0 +24,0 @@ // Start of the algorithm |
@@ -0,0 +0,0 @@ const WriteMode = { |
var fs = require('fs'); | ||
var PNG = require('pngjs').PNG; | ||
module.exports = function(path, canvas) { | ||
module.exports = function(canvas, path, teamMaskPath = null) { | ||
return new Promise((resolve, reject) => { | ||
let png = new PNG({ | ||
let frameImage = new PNG({ | ||
width: canvas.width, | ||
@@ -12,8 +12,23 @@ height: canvas.height, | ||
canvas.data.copy(png.data); | ||
let teamMaskImage = new PNG({ | ||
width: canvas.width, | ||
height: canvas.height, | ||
colorType: 0, | ||
inputColorType: 0, | ||
filterType: -1 | ||
}); | ||
png.pack().pipe(fs.createWriteStream(path)).on("close", () => { | ||
resolve(); | ||
frameImage.data.set(canvas.data); | ||
teamMaskImage.data.set(canvas.teamMask); | ||
frameImage.pack().pipe(fs.createWriteStream(path)).on("close", () => { | ||
if (teamMaskPath) { | ||
teamMaskImage.pack().pipe(fs.createWriteStream(teamMaskPath)).on("close", () => { | ||
resolve(); | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
16850
435
33