refactor and gh-actions

This commit is contained in:
Simon Waldherr
2024-06-16 15:35:26 +02:00
committed by GitHub
parent c0d018ffa9
commit 755efa31c4
8 changed files with 317 additions and 281 deletions

142
zplgfa.go
View File

@@ -21,19 +21,15 @@ const (
CompressedASCII
)
// ConvertToZPL is just a wrapper for ConvertToGraphicField which also includes the ZPL
// starting code ^XA and ending code ^XZ, as well as a Field Separator and Field Origin.
// ConvertToZPL is a wrapper for ConvertToGraphicField, adding ZPL start and end codes.
func ConvertToZPL(img image.Image, graphicType GraphicType) string {
//Check before entering the ConvertToGraphicField function
//if the width is 0, the function can exit early
check := img.Bounds().Size().X / 8
if check == 0 {
if img.Bounds().Size().X/8 == 0 {
return ""
}
return fmt.Sprintf("^XA,^FS\n^FO0,0\n%s^FS,^XZ\n", ConvertToGraphicField(img, graphicType))
}
// FlattenImage optimizes an image for the converting process
// FlattenImage optimizes an image for the converting process.
func FlattenImage(source image.Image) *image.NRGBA {
size := source.Bounds().Size()
background := color.White
@@ -41,37 +37,36 @@ func FlattenImage(source image.Image) *image.NRGBA {
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
p := source.At(x, y)
flat := flatten(p, background)
target.Set(x, y, flat)
target.Set(x, y, flatten(p, background))
}
}
return target
}
func flatten(input color.Color, background color.Color) color.Color {
func flatten(input, background color.Color) color.Color {
source := color.NRGBA64Model.Convert(input).(color.NRGBA64)
r, g, b, a := source.RGBA()
bgR, bgG, bgB, _ := background.RGBA()
alpha := float32(a) / 0xffff
conv := func(c uint32, bg uint32) uint8 {
val := 0xffff - uint32((float32(bg) * alpha))
val = val | uint32(float32(c)*alpha)
conv := func(c, bg uint32) uint8 {
val := 0xffff - uint32(float32(bg)*alpha)
val |= uint32(float32(c) * alpha)
return uint8(val >> 8)
}
c := color.NRGBA{
conv(r, bgR),
conv(g, bgG),
conv(b, bgB),
uint8(0xff),
return color.NRGBA{
R: conv(r, bgR),
G: conv(g, bgG),
B: conv(b, bgB),
A: 0xff,
}
return c
}
func getRepeatCode(repeatCount int, char string) string {
repeatStr := ""
if repeatCount > 419 {
repeatCount -= 419
repeatStr += getRepeatCode(repeatCount, char)
repeatStr += getRepeatCode(repeatCount-419, char)
repeatCount = 419
}
@@ -88,118 +83,89 @@ func getRepeatCode(repeatCount int, char string) string {
repeatStr += string(lowString[low])
}
repeatStr += char
return repeatStr
return repeatStr + char
}
// CompressASCII compresses the ASCII data of a ZPL Graphic Field using RLE
func CompressASCII(in string) string {
var curChar string
var lastChar string
var lastCharSince int
var output string
var repCode string
// 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(in)+1; i++ {
if i == len(in) {
curChar = ""
if lastCharSince == 0 {
switch lastChar {
case "0":
output = ","
return output
case "F":
output = "!"
return output
}
}
} else {
curChar = string(in[i])
for i := 0; i < len(input)+1; i++ {
curChar := ""
if i < len(input) {
curChar = string(input[i])
}
if lastChar != curChar {
if i-lastCharSince > 4 {
repCode = getRepeatCode(i-lastCharSince, lastChar)
output += repCode
} else {
for j := 0; j < i-lastCharSince; j++ {
output += lastChar
}
output += strings.Repeat(lastChar, i-lastCharSince)
}
lastChar = curChar
lastCharSince = i
}
if curChar == "" && lastCharSince == 0 {
switch lastChar {
case "0":
return ","
case "F":
return "!"
}
}
}
if output == "" {
output += getRepeatCode(len(in), lastChar)
output += getRepeatCode(len(input), lastChar)
}
return output
}
// ConvertToGraphicField converts an image.Image picture to a ZPL compatible Graphic Field.
// The ZPL ^GF (Graphic Field) supports various data formats, this package supports the
// normal ASCII encoded, as well as a RLE compressed ASCII format. It also supports the
// Binary Graphic Field format. The encoding can be chosen by the second argument.
// ConvertToGraphicField converts an image.Image to a ZPL compatible Graphic Field.
func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
var gfType string
var lastLine string
var gfType, lastLine, graphicFieldData string
size := source.Bounds().Size()
width := size.X / 8
width := (size.X + 7) / 8 // round up division
height := size.Y
if size.Y%8 != 0 {
width = width + 1
}
var GraphicFieldData string
for y := 0; y < size.Y; y++ {
line := make([]uint8, width)
lineIndex := 0
index := uint8(0)
currentByte := line[lineIndex]
for x := 0; x < size.X; x++ {
index = index + 1
p := source.At(x, y)
lum := color.Gray16Model.Convert(p).(color.Gray16)
if lum.Y < math.MaxUint16/2 {
currentByte = currentByte | (1 << (8 - index))
if x%8 == 0 {
line[x/8] = 0
}
if index >= 8 {
line[lineIndex] = currentByte
lineIndex++
if lineIndex < len(line) {
currentByte = line[lineIndex]
}
index = 0
if lum := color.Gray16Model.Convert(source.At(x, y)).(color.Gray16).Y; lum < math.MaxUint16/2 {
line[x/8] |= 1 << (7 - uint(x)%8)
}
}
hexstr := strings.ToUpper(hex.EncodeToString(line))
hexStr := strings.ToUpper(hex.EncodeToString(line))
switch graphicType {
case ASCII:
GraphicFieldData += fmt.Sprintln(hexstr)
graphicFieldData += fmt.Sprintln(hexStr)
case CompressedASCII:
curLine := CompressASCII(hexstr)
curLine := CompressASCII(hexStr)
if lastLine == curLine {
GraphicFieldData += ":"
graphicFieldData += ":"
} else {
GraphicFieldData += curLine
graphicFieldData += curLine
}
lastLine = curLine
case Binary:
GraphicFieldData += fmt.Sprintf("%s", line)
graphicFieldData += string(line)
}
}
if graphicType == ASCII || graphicType == CompressedASCII {
switch graphicType {
case ASCII, CompressedASCII:
gfType = "A"
} else if graphicType == Binary {
case Binary:
gfType = "B"
}
return fmt.Sprintf("^GF%s,%d,%d,%d,\n%s", gfType, len(GraphicFieldData), width*height, width, GraphicFieldData)
return fmt.Sprintf("^GF%s,%d,%d,%d,\n%s", gfType, len(graphicFieldData), width*height, width, graphicFieldData)
}