From b8e998e37530c820eeb59f65f81cf4973f721b86 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Fri, 17 Sep 2021 17:21:57 +0900 Subject: [PATCH 1/2] feat: Added support for options for PNG export --- save-pixels.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/save-pixels.js b/save-pixels.js index 9efce4d..80f7314 100644 --- a/save-pixels.js +++ b/save-pixels.js @@ -116,7 +116,8 @@ module.exports = function savePixels (array, type, options) { case '.PNG': var png = new PNG({ width: array.shape[0], - height: array.shape[1] + height: array.shape[1], + ...options.png }) var data = handleData(array, png.data) if (typeof data === 'Error') return haderror(data) From 206519d73f164e4ea7268bb69288019278fab344 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Mon, 27 Sep 2021 16:20:45 +0900 Subject: [PATCH 2/2] test: Added tests for options for PNG export --- test/test.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/test.js b/test/test.js index 829c5aa..cd59ff3 100644 --- a/test/test.js +++ b/test/test.js @@ -321,3 +321,86 @@ tap("save-pixels saving 2 jpeg images with the different qualities are different }) }) }) + + +tap("save-pixels saving 2 png images with the same colorType are identical", function(t) { + var x = zeros([64, 64, 4]) // RGBA + var firstFilepath = "temp1.png" + var secondFilepath = "temp2.png" + + for(var i=0; i<64; ++i) { + for(var j=0; j<64; ++j) { + // 1x1 black and white checkerboard pattern + var value = (i % 2 === 0 && j % 2 === 0) ? 255 : 0 + x.set(i, j, 0, value) + x.set(i, j, 1, value) + x.set(i, j, 2, value) + x.set(i, j, 3, 255) + } + } + writePixels(t, x, firstFilepath, "png", {png: { colorType: 6 } }, function(err) { + if(err) { + t.assert(false, err) + t.end() + return + } + + writePixels(t, x, secondFilepath, "png", {png: { colorType: 6 } }, function(err) { + if(err) { + t.assert(false, err) + t.end() + return + } + + assertImagesEqual(t, firstFilepath, secondFilepath, function() { + if (!process.env.TEST_DEBUG) { + fs.unlinkSync(firstFilepath) + fs.unlinkSync(secondFilepath) + } + + t.end() + }) + }) + }) +}) + +tap("save-pixels saving 2 png images with the different colorType are different", function(t) { + var x = zeros([64, 64, 4]) // RGBA + var withoutAlphaFilepath = "temp-rgb.png" + var withAlphaFilepath = "temp-rgba.png" + + for(var i=0; i<64; ++i) { + for(var j=0; j<64; ++j) { + // 1x1 black and white checkerboard pattern + var value = (i % 2 === 0 && j % 2 === 0) ? 255 : 0 + x.set(i, j, 0, value) + x.set(i, j, 1, value) + x.set(i, j, 2, value) + x.set(i, j, 3, 0) + } + } + writePixels(t, x, withoutAlphaFilepath, "png", {png: { colorType: 2 } }, function(err) { + if(err) { + t.assert(false, err) + t.end() + return + } + + writePixels(t, x, withAlphaFilepath, "png", {png: { colorType: 6 } }, function(err) { + if(err) { + t.assert(false, err) + t.end() + return + } + + assertImagesNotEqual(t, withoutAlphaFilepath, withAlphaFilepath, function() { + if (!process.env.TEST_DEBUG) { + fs.unlinkSync(withoutAlphaFilepath) + fs.unlinkSync(withAlphaFilepath) + } + + t.end() + }) + }) + }) +})