'how to store 16 bits buffer to PNG file using sharp.js?

I'm using sharp.js to create PNG images in node.js from a file containing a 16-bit RAW data map.

fs.readFile(myFolder + "frame1250.dat", (err, buf) => {
  let input = Uint16Array.from(buf),
      image = sharp( input, {
                raw: {
                  width: 1544,
                  height: 740,
                  bitdepth: 16,
                  channels : 1
                }
            });
        image.toFile(myFolder+'frame1250.png', (err, info) => { if(!err) console.log(info) });
    })

the info :

{
  format: 'png',
  width: 1544,
  height: 740,
  channels: 3,
  premultiplied: false,
  size: 2438170
}

the output file contains an inconsistent 24-bit color image, instead of a 16-bit grayscale image. any idea ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source