// Package raster prepares images for thermal printing: it decodes them within // a pixel budget, scales them to the print head width, and dithers them to // pure black and white. package raster import ( "bytes" "errors" "fmt" "image" "image/color" "image/draw" // Register the formats Decode accepts. _ "image/gif" // registers GIF decoding _ "image/jpeg" // registers JPEG decoding _ "image/png" // registers PNG decoding xdraw "golang.org/x/image/draw" _ "golang.org/x/image/webp" // registers WebP decoding ) var ( // ErrTooLarge is returned when an image exceeds the pixel budget. ErrTooLarge = errors.New("raster: image exceeds pixel limit") // ErrInvalidSize is returned for zero or negative target sizes. ErrInvalidSize = errors.New("raster: invalid size") ) // Decode decodes PNG, JPEG, GIF or WebP data. The header is checked first, // so an image whose declared size exceeds maxPixels is rejected before any // pixel memory is allocated. func Decode(data []byte, maxPixels int) (image.Image, error) { cfg, format, err := image.DecodeConfig(bytes.NewReader(data)) if err != nil { return nil, fmt.Errorf("reading image header: %w", err) } if cfg.Width <= 0 || cfg.Height <= 0 { return nil, fmt.Errorf("%w: %dx%d", ErrInvalidSize, cfg.Width, cfg.Height) } if int64(cfg.Width)*int64(cfg.Height) > int64(maxPixels) { return nil, fmt.Errorf("%w: %dx%d is more than %d pixels", ErrTooLarge, cfg.Width, cfg.Height, maxPixels) } img, _, err := image.Decode(bytes.NewReader(data)) if err != nil { return nil, fmt.Errorf("decoding %s image: %w", format, err) } return img, nil } // palette is the two-colour palette thermal printers can reproduce. // Index 0 is white so a freshly allocated image is blank paper. func palette() color.Palette { return color.Palette{color.White, color.Black} } // Monochrome scales img to width dots, keeping its aspect ratio, composites it // onto white paper, and Floyd–Steinberg dithers it to black and white. func Monochrome(img image.Image, width int) (*image.Paletted, error) { src := img.Bounds() if width <= 0 || src.Dx() <= 0 || src.Dy() <= 0 { return nil, fmt.Errorf("%w: cannot scale %dx%d to width %d", ErrInvalidSize, src.Dx(), src.Dy(), width) } height := max(1, int(int64(src.Dy())*int64(width)/int64(src.Dx()))) // Scale onto white grayscale so transparency becomes paper and the // dither works on luminance rather than RGB distance. gray := image.NewGray(image.Rect(0, 0, width, height)) draw.Draw(gray, gray.Bounds(), image.White, image.Point{}, draw.Src) xdraw.CatmullRom.Scale(gray, gray.Bounds(), img, src, draw.Over, nil) out := image.NewPaletted(gray.Bounds(), palette()) draw.FloydSteinberg.Draw(out, out.Bounds(), gray, image.Point{}) return out, nil } // Place returns a width-dot-wide white canvas with img drawn at x. It is used // to align images, because printers do not apply justification to raster // images consistently. func Place(img *image.Paletted, width, x int) (*image.Paletted, error) { b := img.Bounds() if width < b.Dx() || x < 0 || x+b.Dx() > width { return nil, fmt.Errorf("%w: cannot place %d-dot image at %d on %d dots", ErrInvalidSize, b.Dx(), x, width) } if x == 0 && width == b.Dx() { return img, nil } out := image.NewPaletted(image.Rect(0, 0, width, b.Dy()), palette()) draw.Draw(out, image.Rect(x, 0, x+b.Dx(), b.Dy()), img, b.Min, draw.Src) return out, nil }