This commit is contained in:
Simon Waldherr
2018-10-19 20:10:28 +02:00
parent ad1a2c79cf
commit 30005e0aa8
2 changed files with 15 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"simonwaldherr.de/go/zplgfa"
"flag" "flag"
"fmt" "fmt"
"github.com/anthonynsimon/bild/blur" "github.com/anthonynsimon/bild/blur"
@@ -16,6 +15,7 @@ import (
"log" "log"
"math" "math"
"os" "os"
"simonwaldherr.de/go/zplgfa"
"strings" "strings"
) )
@@ -103,14 +103,14 @@ func main() {
fmt.Println(gfimg) fmt.Println(gfimg)
} }
type ImageSet interface { type imageSet interface {
Set(x, y int, c color.Color) Set(x, y int, c color.Color)
} }
func editImageInvert(img image.Image) image.Image { func editImageInvert(img image.Image) image.Image {
b := img.Bounds() b := img.Bounds()
imgSet := img.(ImageSet) imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ { for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ { for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y) oldPixel := img.At(x, y)
@@ -128,7 +128,7 @@ func editImageInvert(img image.Image) image.Image {
func editImageMonochrome(img image.Image) image.Image { func editImageMonochrome(img image.Image) image.Image {
b := img.Bounds() b := img.Bounds()
imgSet := img.(ImageSet) imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ { for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ { for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y) oldPixel := img.At(x, y)

View File

@@ -9,6 +9,7 @@ import (
"strings" "strings"
) )
// GraphicType is a type to select the graphic format
type GraphicType int type GraphicType int
const ( const (
@@ -18,11 +19,12 @@ const (
) )
// ConvertToZPL is just a wrapper for ConvertToGraphicField which also includes the ZPL // ConvertToZPL is just a wrapper for ConvertToGraphicField which also includes the ZPL
// starting code ^XA and ending code ^XZ, as wall as a Field Separator and Field Origin. // starting code ^XA and ending code ^XZ, as well as a Field Separator and Field Origin.
func ConvertToZPL(img image.Image, graphicType GraphicType) string { func ConvertToZPL(img image.Image, graphicType GraphicType) string {
return fmt.Sprintf("^XA,^FS\n^FO0,0\n%s^FS,^XZ\n", ConvertToGraphicField(img, graphicType)) return fmt.Sprintf("^XA,^FS\n^FO0,0\n%s^FS,^XZ\n", ConvertToGraphicField(img, graphicType))
} }
// FlattenImage optimizes an image for the converting process
func FlattenImage(source image.Image) *image.NRGBA { func FlattenImage(source image.Image) *image.NRGBA {
size := source.Bounds().Size() size := source.Bounds().Size()
background := color.White background := color.White
@@ -40,7 +42,7 @@ func FlattenImage(source image.Image) *image.NRGBA {
func flatten(input color.Color, background color.Color) color.Color { func flatten(input color.Color, background color.Color) color.Color {
source := color.NRGBA64Model.Convert(input).(color.NRGBA64) source := color.NRGBA64Model.Convert(input).(color.NRGBA64)
r, g, b, a := source.RGBA() r, g, b, a := source.RGBA()
bg_r, bg_g, bg_b, _ := background.RGBA() bgR, bgG, bgB, _ := background.RGBA()
alpha := float32(a) / 0xffff alpha := float32(a) / 0xffff
conv := func(c uint32, bg uint32) uint8 { conv := func(c uint32, bg uint32) uint8 {
val := 0xffff - uint32((float32(bg) * alpha)) val := 0xffff - uint32((float32(bg) * alpha))
@@ -48,9 +50,9 @@ func flatten(input color.Color, background color.Color) color.Color {
return uint8(val >> 8) return uint8(val >> 8)
} }
c := color.NRGBA{ c := color.NRGBA{
conv(r, bg_r), conv(r, bgR),
conv(g, bg_g), conv(g, bgG),
conv(b, bg_b), conv(b, bgB),
uint8(0xff), uint8(0xff),
} }
return c return c
@@ -84,10 +86,10 @@ func getRepeatCode(repeatCount int, char string) string {
func compressASCII(in string) string { func compressASCII(in string) string {
in = strings.ToUpper(in) in = strings.ToUpper(in)
var curChar string = "" var curChar string
var lastChar string = "" var lastChar string
var lastCharSince int = 0 var lastCharSince int
var output string = "" var output string
var repCode string var repCode string
for i := 0; i < len(in)+1; i++ { for i := 0; i < len(in)+1; i++ {