Files
ZPLify/cmd/zplgfa/main.go

146 lines
3.5 KiB
Go
Raw Normal View History

2018-10-19 19:52:47 +02:00
package main
import (
"flag"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"math"
"os"
"strings"
2018-11-04 16:52:42 +01:00
"github.com/anthonynsimon/bild/blur"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/segment"
"github.com/nfnt/resize"
"simonwaldherr.de/go/zplgfa"
2018-10-19 19:52:47 +02:00
)
func main() {
var filenameFlag string
var graphicTypeFlag string
var imageEditFlag string
var imageResizeFlag float64
var graphicType zplgfa.GraphicType
flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl")
flag.StringVar(&graphicTypeFlag, "type", "CompressedASCII", "type of graphic field encoding")
flag.StringVar(&imageEditFlag, "edit", "", "manipulate the image [invert,monochrome]")
flag.Float64Var(&imageResizeFlag, "resize", 1.0, "zoom/resize the image")
// load flag input arguments
flag.Parse()
// open file
file, err := os.Open(filenameFlag)
if err != nil {
log.Printf("Warning: could not open the file \"%s\": %s\n", filenameFlag, err)
return
}
defer file.Close()
// load image head information
config, format, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Warning: image not compatible, format: %s, config: %v, error: %s\n", format, config, err)
}
// reset file pointer to the beginning of the file
file.Seek(0, 0)
// load and decode image
img, _, err := image.Decode(file)
if err != nil {
log.Printf("Warning: could not decode the file, %s\n", err)
return
}
// select graphic field type
switch graphicTypeFlag {
case "ASCII":
graphicType = zplgfa.ASCII
case "Binary":
graphicType = zplgfa.Binary
case "CompressedASCII":
graphicType = zplgfa.CompressedASCII
default:
graphicType = zplgfa.CompressedASCII
}
// apply image manipulation functions
2018-11-04 16:52:42 +01:00
switch {
case strings.Contains(imageEditFlag, "monochrome"):
2018-10-19 19:52:47 +02:00
img = editImageMonochrome(img)
2018-11-04 16:52:42 +01:00
case strings.Contains(imageEditFlag, "blur"):
2018-10-19 19:52:47 +02:00
img = blur.Gaussian(img, float64(config.Width)/300)
2018-11-04 16:52:42 +01:00
case strings.Contains(imageEditFlag, "edge"):
2018-10-19 19:52:47 +02:00
img = effect.Sobel(img)
2018-11-04 16:52:42 +01:00
case strings.Contains(imageEditFlag, "segment"):
2018-10-19 19:52:47 +02:00
img = segment.Threshold(img, 128)
2018-11-04 16:52:42 +01:00
case strings.Contains(imageEditFlag, "invert"):
2018-10-19 19:52:47 +02:00
img = editImageInvert(img)
}
// resize image
if imageResizeFlag != 1.0 {
img = resize.Resize(uint(float64(config.Width)*imageResizeFlag), uint(float64(config.Height)*imageResizeFlag), img, resize.MitchellNetravali)
}
// flatten image
flat := zplgfa.FlattenImage(img)
// convert image to zpl compatible type
gfimg := zplgfa.ConvertToZPL(flat, graphicType)
// output zpl with graphic field date to stdout
fmt.Println(gfimg)
}
2018-10-19 20:10:28 +02:00
type imageSet interface {
2018-10-19 19:52:47 +02:00
Set(x, y int, c color.Color)
}
func editImageInvert(img image.Image) image.Image {
b := img.Bounds()
2018-10-19 20:10:28 +02:00
imgSet := img.(imageSet)
2018-10-19 19:52:47 +02:00
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
r = 65535 - r
g = 65535 - g
b = 65535 - b
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
}
return img
}
func editImageMonochrome(img image.Image) image.Image {
b := img.Bounds()
2018-10-19 20:10:28 +02:00
imgSet := img.(imageSet)
2018-10-19 19:52:47 +02:00
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
if r > math.MaxUint16/2 || g > math.MaxUint16/2 || b > math.MaxUint16/2 {
r, g, b = 65535, 65535, 65535
} else {
r, g, b = 0, 0, 0
}
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
}
return img
}