png-stash
A very low level tool to read and write a message in the least significant bits of a PNG image's pixels.
Also contains two command-line programs bin/readfile.js
and
bin/writefile.js
to read and write data up to 16777215 characters in
length. These will print usage instructions when executed.
Quick Example 1: Write Data
var pngStash = ; var stash = ;
Quick Example 2: Read Data
var pngStash = ; var stash = ;
npm install png-stash
Documentation
Constructor
Instance Functions
## pngStash(pngFileName, callback)
Reads the specified PNG file and yields a reader-writer object with the following properties:
- length - Total number of bytes that can be hidden in this PNG file.
- getByte()
- setByte()
- write()
- read()
- save()
See below for function documentation.
Arguments
- pngFileName - Must be an existing valid PNG file.
- callback -
function(err, stash)
. If PNG file was successfully loaded,err
will beundefined
.stash
will be reader-writer object mentioned in description above.
Example
var pngStash = ; ;
## stash.getByte(index)
Reads a single byte from the "invisible" bits of the PNG file.
Arguments
- index - Position among all bytes composed by the PNG's "invisible" bits. Must be less than the
pngStash
instance'slength
property.
Example
var pngStash = ; ;
## stash.setByte(index, value)
Writes a single byte to the "invisible" bits of the PNG, but does not
save it to disk. To save, call save()
.
Arguments
- index - Position among all bytes composed by the PNG's "invisible" bits. Must be less than the
pngStash
instance'slength
property. - value - Byte to store. Must be an integer in the 8-bit range (
0 < value < 256
).
Example
var pngStash = ; ;
## stash.read(offset, length) ## stash.read(offset) ## stash.read()
Reads a sequence of bytes from the PNG's "invisible" bits, and returns
them as a Buffer
.
Arguments
- offset - default=
0
. At which byte position to start reading from. - length - default=
stash.length
. How many bytes to read.
Example
var pngStash = ; ;
## stash.write(data, offset, length) ## stash.write(data, offset) ## stash.write(data)
Writes a sequence of bytes to the PNG's "invisible" bits, but does not
save to disk. To save to disk, call save()
.
Arguments
- data - Bytes to store in the PNG. Must be string or Buffer. If string, will be UTF-8 encoded.
- offset - default=
0
. At which byte position to start writing to. - length - default=all. How many bytes from
data
to write. Be aware that the.length
of a string may not correspond to the number of bytes in its resulting UTF-8 representation.
Example
var pngStash = ; ;
## stash.save(callback)
Stores the PNG data back into the file. You want to call this
function after writing data to the stash
instance.
Arguments
- callback -
function(err)
.err
will beundefined
if save was successful. Otherwise it will represent an error.
Example
// See examples for stash.write()
and stash.setByte()
.