diff --git a/png-to-jpg.jpg b/png-to-jpg.jpg new file mode 100644 index 0000000..84c88ab Binary files /dev/null and b/png-to-jpg.jpg differ diff --git a/test.js b/test.js index c7940f0..f518e09 100644 --- a/test.js +++ b/test.js @@ -9,15 +9,19 @@ const writeImage = require("./write-image"); test("jpg to png", async ({ eq }) => { const jpg = findAndRead("flower.jpg"); const { height, width, pixels } = await readim({ data: jpg }); - const png = await writeImage({ data: pixels, debug: true, format: "png", height, width }); - console.log("png:", png); - fs.writeFileSync("jpg-to-png.png", png); + const result = await writeImage({ data: pixels, debug: false, format: "png", height, width }); + fs.writeFileSync("jpg-to-png.png", result.data); + eq(result.height, height); + eq(result.width, width); + eq(Buffer.isBuffer(result.data), true); }); test("png to jpg", async ({ eq }) => { const png = findAndRead("flower.png"); const { height, width, pixels } = await readim({ data: png }); - const jpg = await writeImage({ data: pixels, debug: true, format: "jpg", height, width }); - console.log("jpg:", jpg); - fs.writeFileSync("png-to-jpg.jpg", jpg); + const result = await writeImage({ data: pixels, debug: false, format: "jpg", height, width }); + fs.writeFileSync("png-to-jpg.jpg", result.data); + eq(result.height, height); + eq(result.width, width); + eq(Buffer.isBuffer(result.data), true); }); diff --git a/write-image.js b/write-image.js index 8c51bbb..34de228 100644 --- a/write-image.js +++ b/write-image.js @@ -12,14 +12,18 @@ module.exports = async function writeImage({ data, debug = false, format, height const imageData = toImageData({ data, height, width }); if (debug) console.log(`[write-image] image data is`, imageData); + let result; if (format === "PNG") { const png = new PNG({ filterType: 4, height, width }); png.data = imageData.data; if (debug) console.log(`[write-image] png is`, png); const buffer = PNG.sync.write(png); - return { data: buffer, height, width }; + result = { data: buffer, height, width }; } else if (format === "JPG") { - const { data, height, width } = jpeg.encode(imageData, quality).data; - return { data, height, width }; + const encoded = jpeg.encode(imageData, quality).data; + if (debug) console.log("`[write-image] jpeg.encode returned", encoded); + result = { data: encoded, height, width }; } + if (debug) console.log(`[write-image] returning`, result); + return result; };