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 (
|
2018-10-19 20:32:28 +02:00
|
|
|
// ASCII graphic type using only hex characters (0-9A-F)
|
2018-10-19 19:52:47 +02:00
|
|
|
ASCII GraphicType = iota
|
2018-10-19 20:32:28 +02:00
|
|
|
// Binary saving the same data as binary
|
2018-10-19 19:52:47 +02:00
|
|
|
Binary
|
2018-10-19 20:32:28 +02:00
|
|
|
// CompressedASCII compresses the hex data via RLE
|
2018-10-19 19:52:47 +02:00
|
|
|
CompressedASCII
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
// ConvertToZPL wraps 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()
|
|
|
|
|
target := image.NewNRGBA(source.Bounds())
|
2024-08-17 12:25:27 +02:00
|
|
|
background := color.White
|
|
|
|
|
|
2018-10-19 19:52:47 +02:00
|
|
|
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-08-17 12:25:27 +02:00
|
|
|
// flatten blends a pixel with the background color based on its alpha value.
|
2024-06-16 15:35:26 +02:00
|
|
|
func flatten(input, background color.Color) color.Color {
|
2024-08-17 12:25:27 +02:00
|
|
|
src := color.NRGBA64Model.Convert(input).(color.NRGBA64)
|
|
|
|
|
r, g, b, a := src.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
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
blend := func(c, bg uint32) uint8 {
|
2024-06-16 15:35:26 +02:00
|
|
|
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{
|
2024-08-17 12:25:27 +02:00
|
|
|
R: blend(r, bgR),
|
|
|
|
|
G: blend(g, bgG),
|
|
|
|
|
B: blend(b, bgB),
|
2024-06-16 15:35:26 +02:00
|
|
|
A: 0xff,
|
2018-10-19 19:52:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
// getRepeatCode generates ZPL repeat codes for character compression.
|
2018-10-19 19:52:47 +02:00
|
|
|
func getRepeatCode(repeatCount int, char string) string {
|
2024-08-17 12:25:27 +02:00
|
|
|
const maxRepeat = 419
|
|
|
|
|
highString := " ghijklmnopqrstuvwxyz"
|
|
|
|
|
lowString := " GHIJKLMNOPQRSTUVWXY"
|
|
|
|
|
|
2018-10-19 19:52:47 +02:00
|
|
|
repeatStr := ""
|
2024-08-17 12:25:27 +02:00
|
|
|
for repeatCount > maxRepeat {
|
|
|
|
|
repeatStr += getRepeatCode(maxRepeat, char)
|
|
|
|
|
repeatCount -= maxRepeat
|
2018-10-19 19:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
high := repeatCount / 20
|
|
|
|
|
low := repeatCount % 20
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-08-17 12:25:27 +02:00
|
|
|
if input == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var output strings.Builder
|
|
|
|
|
var lastChar string
|
|
|
|
|
var lastCharSince int
|
2024-06-16 15:35:26 +02:00
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
for i := 0; i <= len(input); i++ {
|
2024-06-16 15:35:26 +02:00
|
|
|
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 {
|
2024-08-17 12:25:27 +02:00
|
|
|
output.WriteString(getRepeatCode(i-lastCharSince, lastChar))
|
2018-10-19 19:52:47 +02:00
|
|
|
} else {
|
2024-08-17 12:25:27 +02:00
|
|
|
output.WriteString(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
|
|
|
}
|
|
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
return output.String()
|
2018-10-19 19:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
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-08-17 12:25:27 +02:00
|
|
|
var gfType, 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
|
2024-08-17 12:25:27 +02:00
|
|
|
var lastLine string
|
2018-10-19 19:52:47 +02:00
|
|
|
|
2024-08-17 12:25:27 +02:00
|
|
|
for y := 0; y < height; y++ {
|
2018-10-19 19:52:47 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2018-10-21 19:07:04 +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
|
|
|
}
|