From 6d12ed685b4bc43b0dac42da6c2dcad1420b3064 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 6 Sep 2015 00:22:57 +0200 Subject: [PATCH] enhance renders by using binary transparency this is a temporary fix for #32. it doesn't solve the problem, but it makes the renders much less worse. in combination with #134 this will hopefully lead to fixing the problem entirely --- lib/renders.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/renders.js b/lib/renders.js index 470006d..2d0cc26 100644 --- a/lib/renders.js +++ b/lib/renders.js @@ -24,6 +24,18 @@ function scale_image(imageData, context, d_x, d_y, scale) { } } +// makes images less worse by using binary transparency +function enhance(context) { + var imagedata = context.getImageData(0, 0, context.canvas.width, context.canvas.height); + var data = imagedata.data; + // data is [r,g,b,a, r,g,b,a, *] + for (var i = 3; i < data.length; i += 4) { + // round to 0 or 255 + data[i] = Math.round(data[i] / 255) * 255; + } + context.putImageData(imagedata, 0, 0); +} + // draws the helmet on to the +skin_canvas+ // using the skin from the +model_ctx+ at the +scale+ exp.draw_helmet = function(skin_canvas, model_ctx, scale) { @@ -155,7 +167,7 @@ exp.draw_model = function(rid, img, scale, helm, body, callback) { image.onload = function() { var width = 64 * scale; - var original_height = (image.height === 32 ? 32 : 64); + var original_height = image.height === 32 ? 32 : 64; var height = original_height * scale; var model_canvas = new Canvas(20 * scale, (body ? 44.8 : 17.6) * scale); var skin_canvas = new Canvas(width, height); @@ -173,7 +185,10 @@ exp.draw_model = function(rid, img, scale, helm, body, callback) { exp.draw_helmet(skin_canvas, model_ctx, scale); } - model_canvas.toBuffer(function(err, buf){ + // FIXME: This is a temporary fix for #32 + enhance(model_ctx); + + model_canvas.toBuffer(function(err, buf) { if (err) { logging.error(rid, "error creating buffer:", err); } @@ -187,7 +202,7 @@ exp.draw_model = function(rid, img, scale, helm, body, callback) { // helper method to open a render from +renderpath+ // callback: error, image buffer exp.open_render = function(rid, renderpath, callback) { - fs.readFile(renderpath, function (err, buf) { + fs.readFile(renderpath, function(err, buf) { if (err) { logging.error(rid, "error while opening skin file:", err); }