Documentation
¶
Overview ¶
Package escposimg provides functionality to process images for ESC/POS thermal printers. It supports loading PNG, JPEG, and Netpbm images; applying dithering (skipped for PBM); scaling to paper width; and generating ESC/POS commands for printing.
Index ¶
- Constants
- func ApplyDithering(img image.Image, algo DitheringType) (image.Image, error)
- func GenerateESCPOS(img image.Image, config *Config) ([]byte, error)
- func GenerateTestPattern(width, height int) []byte
- func LoadImage(imagePath string) (image.Image, error)
- func ProcessImage(imagePath string, config *Config, output OutputMethod) error
- func SaveDebugImage(img image.Image, path string) error
- func ScaleImage(img image.Image, targetWidth int) (image.Image, error)
- func ScaleImageInterp(img image.Image, targetWidth int, interp resize.InterpolationFunction) (image.Image, error)
- func Version() string
- type Config
- type DitheringType
- type FileOutput
- type LoadImageResult
- type NetworkOutput
- type OutputMethod
- type PrintMode
- type StdoutOutput
Constants ¶
const ( ESC = 0x1B // Escape character GS = 0x1D // Group separator LF = 0x0A // Line feed CR = 0x0D // Carriage return )
ESC/POS command constants
const ( DPI203 = 203 // Standard thermal printer DPI DPI300 = 300 // High quality thermal printer DPI DPI180 = 180 // Older thermal printer models )
Common DPI values for thermal printers
const ( PaperWidth58mm = 58 PaperWidth72mm = 72 PaperWidth80mm = 80 )
Common paper widths in millimeters
Variables ¶
This section is empty.
Functions ¶
func ApplyDithering ¶
ApplyDithering applies the specified dithering algorithm to the image
func GenerateESCPOS ¶
GenerateESCPOS generates ESC/POS commands from a dithered image Supports raster mode (GS v 0), graphics mode (GS ( L), and column mode (ESC *)
func GenerateTestPattern ¶
GenerateTestPattern generates a simple test pattern for debugging
func LoadImage ¶
LoadImage loads an image from the specified file path. Supports PNG, JPEG, and Netpbm (pbm, pgm, ppm, pam).
func ProcessImage ¶
func ProcessImage(imagePath string, config *Config, output OutputMethod) error
ProcessImage is the main function that processes an image and sends it to the specified output. It performs the complete pipeline: load → scale → dither → generate ESC/POS → output. PBM inputs skip dithering and use nearest-neighbor scaling to preserve bitmap edges.
func SaveDebugImage ¶
SaveDebugImage saves an image to the specified path for debugging purposes
func ScaleImage ¶
ScaleImage scales an image to the specified width while maintaining aspect ratio. Uses Lanczos3 interpolation for high quality scaling.
func ScaleImageInterp ¶
func ScaleImageInterp(img image.Image, targetWidth int, interp resize.InterpolationFunction) (image.Image, error)
ScaleImageInterp scales an image to the target width while maintaining aspect ratio, using the given nfnt/resize interpolation (e.g. Lanczos3 or NearestNeighbor).
Types ¶
type Config ¶
type Config struct {
// Paper width in millimeters (default: 80mm)
PaperWidthMM int
// Printer DPI (default: 203 DPI)
DPI int
// Dithering algorithm to use
DitheringAlgo DitheringType
// ESC/POS printing mode for images (default: PrintModeRaster).
//
// Determines which ESC/POS command sequence to use for image printing:
// - PrintModeRaster: Modern GS v 0 command, efficient, single command
// - PrintModeGraphics: Advanced GS ( L command, extended features
// - PrintModeColumn: Legacy ESC * command, compatible, line-by-line
//
// Use PrintModeRaster for modern printers, PrintModeGraphics for advanced
// features, or PrintModeColumn for legacy compatibility.
PrintMode PrintMode
// Save dithered image for debugging
DebugOutput bool
// Path to save debug image (if DebugOutput is true)
DebugImagePath string
// Optional debug text to print before image
DebugText string
// Send paper cut command after printing
CutPaper bool
}
Config holds the configuration for image processing and printing
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a configuration with sensible defaults
func (*Config) CalculatePixelWidth ¶
CalculatePixelWidth calculates the pixel width based on paper width and DPI
type DitheringType ¶
type DitheringType int
DitheringType represents the available dithering algorithms
const ( DitheringFloydSteinberg DitheringType = iota DitheringAtkinson DitheringThreshold DitheringBayer DitheringBurkes DitheringSierraLite DitheringJarvisJudiceNinke DitheringShadura // DitheringNone converts each pixel to black or white with a fixed threshold only // (no error diffusion, no ordered dither matrix). Same implementation as DitheringThreshold. DitheringNone )
func (DitheringType) String ¶
func (d DitheringType) String() string
String returns the string representation of the dithering type
type FileOutput ¶
type FileOutput struct {
// contains filtered or unexported fields
}
FileOutput writes data to a file
func NewFileOutput ¶
func NewFileOutput(filePath string) (*FileOutput, error)
NewFileOutput creates a new file output method
func (*FileOutput) Write ¶
func (f *FileOutput) Write(data []byte) error
Write writes data to the file
type LoadImageResult ¶
type LoadImageResult struct {
// Image is the decoded bitmap.
Image image.Image
// Format is the string returned by image.Decode (e.g. "png", "jpeg", "pbm").
Format string
// SkipDithering is true when the source is portable bitmap (PBM); error diffusion is not applied.
SkipDithering bool
}
LoadImageResult holds a decoded image and hints derived from the file format.
func LoadImageDetails ¶
func LoadImageDetails(imagePath string) (LoadImageResult, error)
LoadImageDetails loads an image and reports format-specific processing hints. Supports PNG, JPEG, and Netpbm family (pbm, pgm, ppm, pam) via github.com/spakin/netpbm.
type NetworkOutput ¶
type NetworkOutput struct {
// contains filtered or unexported fields
}
NetworkOutput writes data to a network connection
func NewNetworkOutput ¶
func NewNetworkOutput(address string) (*NetworkOutput, error)
NewNetworkOutput creates a new network output method
func (*NetworkOutput) Close ¶
func (n *NetworkOutput) Close() error
Close closes the network connection
func (*NetworkOutput) Write ¶
func (n *NetworkOutput) Write(data []byte) error
Write writes data to the network connection
type OutputMethod ¶
OutputMethod defines the interface for different output methods
type PrintMode ¶
type PrintMode int
PrintMode defines the ESC/POS printing mode for images.
ESC/POS supports three main approaches for printing bitmap images: - Raster mode (GS v 0): Modern, efficient, single command per image - Graphics mode (GS ( L): Advanced graphics with extended features - Column mode (ESC *): Legacy compatible, line-by-line processing
All modes produce identical visual output when using the same dithering algorithm, but differ in command structure and printer compatibility.
const ( // PrintModeRaster uses the GS v 0 command (Print Raster Bit Image). // // This is the modern standard for thermal printers and is recommended // for most use cases. The entire image is sent as a single command, // making it more efficient for large images and network printing. // // Command format: GS v 0 m xL xH yL yH [data] // // Best for: // - Modern thermal printers (post-2010) // - Network/Ethernet connections // - High-volume printing // - Large or high-resolution images // // Compatibility: Most modern ESC/POS printers support this mode. PrintModeRaster PrintMode = iota // PrintModeGraphics uses the GS ( L command (Graphics Mode). // // This is an advanced graphics mode that provides extended functionality // for modern thermal printers. It supports more complex graphics operations // and may offer better quality or performance on compatible printers. // // Command format: GS ( L pL pH fn [parameters] [data] // // Best for: // - Modern thermal printers with graphics support // - Advanced formatting requirements // - Applications requiring extended graphics features // // Compatibility: Supported by modern ESC/POS printers that implement // the extended graphics command set. PrintModeGraphics // PrintModeColumn uses the ESC * command (Column/Bit Image Mode). // // This is the traditional approach that processes images in 8-pixel // height bands, sending one command per band. While less efficient // than raster mode, it offers better compatibility with older printers. // // Command format: ESC * m nL nH [data] (repeated for each 8-pixel band) // // Best for: // - Legacy thermal printers (pre-2010) // - Serial/RS232 connections // - Compatibility troubleshooting // - Printers that don't support GS v 0 or GS ( L // // Compatibility: Supported by virtually all ESC/POS printers, // including very old models. PrintModeColumn )
type StdoutOutput ¶
type StdoutOutput struct{}
StdoutOutput writes data to stdout
func NewStdoutOutput ¶
func NewStdoutOutput() *StdoutOutput
NewStdoutOutput creates a new stdout output method
func (*StdoutOutput) Write ¶
func (s *StdoutOutput) Write(data []byte) error
Write writes data to stdout