# zpl-image
A pure javascript module that converts images to either Z64-encoded or ACS-encoded GRF bitmaps for use with ZPL.
The term ACS (Alternative Compression Scheme) denotes the run-length compression algorithm described in the section
of the ZPL Reference Manual titled "Alternative Data Compression Scheme". Z64 typically gives better compression
but is not available on all printers (especially older ones). The ACS encoding should work on any printer made
since the mid 90s, maybe earlier.
This module provides the following features:
- Works in both node.js and modern browsers.
- Converts the image to grayscale, then applies a user-supplied blackness
threshold to decide which pixels are black.
- Optionally removes any empty/white space around the edges of the image.
- Optionally rotates the image to one of the orthogonal angles. This step
is often necessary as ZPL does not provide the ability to rotate an image
during formatting.
- Converts the monochrome image to a GRF bitmap.
- Converts the GRF bitmap to either Z64 or ACS encoding.
- For Z64, zlib in node.js or pako.js in the browser is used for compression.
The blackness threshold is specified as an integer between 1 and 99 (think of it as a
gray percentage). Pixels darker than the gray% are converted to black. The default is 50.
Rotation is specified as one of the values:
- `'N'` : No rotation, the default.
- `'L'` : Left, 90 degrees counter-clockwise rotation.
- `'R'` : Right, 90 degrees clockwise rotation.
- `'I'` : Inverted, 180 degrees rotation.
- `'B'` : Same as `'L'` but named to match the ZPL notation.
Blackness and rotation are passed via an options object. For example, to specify
a black threshold of 56% and rotation of -90 degrees, you would pass in:
```javascript
{ black:56, rotate:'L' }
```
Trimming of empty space around the image is enabled by default. To disable, specify
the option `notrim:true`.
## Demo
Included with this module is the file `zpl-image.html`. You can run it directly
from the browser using the `file://` scheme. It lets you drag and drop an image
and then interactively adjust the blackness threshold and rotation.
When you are satisfied with the results, select either Z64 or ACS encoding and
click the clipboard icon to copy the ZPL. The ZPL will have the following format:
```
^FX filename.ext (WxHpx, X-Rotate, XX% Black)^FS
^GFA,grflen,grflen,rowlen,...ASCII-armored-encoding...
```
`^FX ... ^FS` is a ZPL comment.
`^GF` is the ZPL command for use-once image rendering (that is, the image is not
saved to the printer for later recall by other label formats).
The rendered image displayed on the page is the actual data decoded and then drawn
to a canvas. If you are interested in that bit of functionality, look for `z64ToCanvas`
and `acsToCanvas` in the `zpl-image.html` file.
## Generic Browser Usage
To use in the browser, include the following two scripts:
```html
```
There is a version of pako.js included with this module, but it will not be updated
frequently. It is primarily intended for the demo html file but should be sufficient
for production use.
```javascript
// Works with
and