Files
ZPLify/zplgfa.go

172 lines
3.8 KiB
Go
Raw Normal View History

2018-10-19 19:52:47 +02:00
package zplgfa
import (
"encoding/hex"
"fmt"
"image"
"image/color"
"math"
"strings"
)
2018-10-19 20:10:28 +02:00
// GraphicType is a type to select the graphic format
2018-10-19 19:52:47 +02:00
type GraphicType int
const (
// ASCII graphic type using only hex characters (0-9A-F)
2018-10-19 19:52:47 +02:00
ASCII GraphicType = iota
// Binary saving the same data as binary
2018-10-19 19:52:47 +02:00
Binary
// CompressedASCII compresses the hex data via RLE
2018-10-19 19:52:47 +02:00
CompressedASCII
)
2024-06-16 15:35:26 +02:00
// ConvertToZPL is a wrapper for ConvertToGraphicField, adding ZPL start and end codes.
2018-10-19 19:52:47 +02:00
func ConvertToZPL(img image.Image, graphicType GraphicType) string {
2024-06-16 15:35:26 +02:00
if img.Bounds().Size().X/8 == 0 {
2023-06-17 14:37:13 +08:00
return ""
}
2018-10-19 19:52:47 +02:00
return fmt.Sprintf("^XA,^FS\n^FO0,0\n%s^FS,^XZ\n", ConvertToGraphicField(img, graphicType))
}
2024-06-16 15:35:26 +02:00
// FlattenImage optimizes an image for the converting process.
2018-10-19 19:52:47 +02:00
func FlattenImage(source image.Image) *image.NRGBA {
size := source.Bounds().Size()
background := color.White
target := image.NewNRGBA(source.Bounds())
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
p := source.At(x, y)
2024-06-16 15:35:26 +02:00
target.Set(x, y, flatten(p, background))
2018-10-19 19:52:47 +02:00
}
}
return target
}
2024-06-16 15:35:26 +02:00
func flatten(input, background color.Color) color.Color {
2018-10-19 19:52:47 +02:00
source := color.NRGBA64Model.Convert(input).(color.NRGBA64)
r, g, b, a := source.RGBA()
2018-10-19 20:10:28 +02:00
bgR, bgG, bgB, _ := background.RGBA()
2018-10-19 19:52:47 +02:00
alpha := float32(a) / 0xffff
2024-06-16 15:35:26 +02:00
conv := func(c, bg uint32) uint8 {
val := 0xffff - uint32(float32(bg)*alpha)
val |= uint32(float32(c) * alpha)
2018-10-19 19:52:47 +02:00
return uint8(val >> 8)
}
2024-06-16 15:35:26 +02:00
return color.NRGBA{
R: conv(r, bgR),
G: conv(g, bgG),
B: conv(b, bgB),
A: 0xff,
2018-10-19 19:52:47 +02:00
}
}
func getRepeatCode(repeatCount int, char string) string {
repeatStr := ""
if repeatCount > 419 {
2024-06-16 15:35:26 +02:00
repeatStr += getRepeatCode(repeatCount-419, char)
2018-10-19 19:52:47 +02:00
repeatCount = 419
}
high := repeatCount / 20
low := repeatCount % 20
lowString := " GHIJKLMNOPQRSTUVWXY"
highString := " ghijklmnopqrstuvwxyz"
if high > 0 {
repeatStr += string(highString[high])
}
if low > 0 {
repeatStr += string(lowString[low])
}
2024-06-16 15:35:26 +02:00
return repeatStr + char
2018-10-19 19:52:47 +02:00
}
2024-06-16 15:35:26 +02:00
// CompressASCII compresses the ASCII data of a ZPL Graphic Field using RLE.
func CompressASCII(input string) string {
var output, lastChar, repCode string
lastCharSince := 0
for i := 0; i < len(input)+1; i++ {
curChar := ""
if i < len(input) {
curChar = string(input[i])
2018-10-19 19:52:47 +02:00
}
2024-06-16 15:35:26 +02:00
2018-10-19 19:52:47 +02:00
if lastChar != curChar {
2018-10-19 20:51:21 +02:00
if i-lastCharSince > 4 {
2018-10-19 19:52:47 +02:00
repCode = getRepeatCode(i-lastCharSince, lastChar)
output += repCode
} else {
2024-06-16 15:35:26 +02:00
output += strings.Repeat(lastChar, i-lastCharSince)
2018-10-19 19:52:47 +02:00
}
lastChar = curChar
lastCharSince = i
}
2024-06-16 15:35:26 +02:00
if curChar == "" && lastCharSince == 0 {
switch lastChar {
case "0":
return ","
case "F":
return "!"
}
}
2018-10-19 19:52:47 +02:00
}
if output == "" {
2024-06-16 15:35:26 +02:00
output += getRepeatCode(len(input), lastChar)
2018-10-19 19:52:47 +02:00
}
return output
}
2024-06-16 15:35:26 +02:00
// ConvertToGraphicField converts an image.Image to a ZPL compatible Graphic Field.
2018-10-19 19:52:47 +02:00
func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
2024-06-16 15:35:26 +02:00
var gfType, lastLine, graphicFieldData string
2018-10-19 19:52:47 +02:00
size := source.Bounds().Size()
2024-06-16 15:35:26 +02:00
width := (size.X + 7) / 8 // round up division
2018-10-19 19:52:47 +02:00
height := size.Y
for y := 0; y < size.Y; y++ {
line := make([]uint8, width)
for x := 0; x < size.X; x++ {
2024-06-16 15:35:26 +02:00
if x%8 == 0 {
line[x/8] = 0
2018-10-19 19:52:47 +02:00
}
2024-06-16 15:35:26 +02:00
if lum := color.Gray16Model.Convert(source.At(x, y)).(color.Gray16).Y; lum < math.MaxUint16/2 {
line[x/8] |= 1 << (7 - uint(x)%8)
2018-10-19 19:52:47 +02:00
}
}
2024-06-16 15:35:26 +02:00
hexStr := strings.ToUpper(hex.EncodeToString(line))
2018-10-19 19:52:47 +02:00
switch graphicType {
case ASCII:
2024-06-16 15:35:26 +02:00
graphicFieldData += fmt.Sprintln(hexStr)
2018-10-19 19:52:47 +02:00
case CompressedASCII:
2024-06-16 15:35:26 +02:00
curLine := CompressASCII(hexStr)
2018-10-19 19:52:47 +02:00
if lastLine == curLine {
2024-06-16 15:35:26 +02:00
graphicFieldData += ":"
2018-10-19 19:52:47 +02:00
} else {
2024-06-16 15:35:26 +02:00
graphicFieldData += curLine
2018-10-19 19:52:47 +02:00
}
lastLine = curLine
case Binary:
2024-06-16 15:35:26 +02:00
graphicFieldData += string(line)
2018-10-19 19:52:47 +02:00
}
}
2024-06-16 15:35:26 +02:00
switch graphicType {
case ASCII, CompressedASCII:
2018-10-19 19:52:47 +02:00
gfType = "A"
2024-06-16 15:35:26 +02:00
case Binary:
2018-10-19 19:52:47 +02:00
gfType = "B"
}
2024-06-16 15:35:26 +02:00
return fmt.Sprintf("^GF%s,%d,%d,%d,\n%s", gfType, len(graphicFieldData), width*height, width, graphicFieldData)
2018-10-19 19:52:47 +02:00
}