aboutsummaryrefslogtreecommitdiff
path: root/cmd/ql-print/main.go
blob: 647ba1adef768ff4cce476ce83a54551bb982ecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main

import (
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"

	"flag"
	"fmt"
	"log"
	"os"

	"janouch.name/sklad/imgutil"
	"janouch.name/sklad/ql"
)

var scale = flag.Int("scale", 1, "integer upscaling")
var rotate = flag.Bool("rotate", false, "print sideways")
var redblack = flag.Bool("redblack", false, "red and black print")

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s IMAGE\n", os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(1)
	}

	// Open the picture.
	f, err := os.Open(flag.Arg(0))
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()

	// Load and eventually transform the picture.
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatalln(err)
	}
	if *scale > 1 {
		img = &imgutil.Scale{Image: img, Scale: *scale}
	}
	if *rotate {
		img = &imgutil.LeftRotate{Image: img}
	}

	// Open and initialize the printer.
	p, err := ql.Open()
	if err != nil {
		log.Fatalln(err)
	}
	if p == nil {
		log.Fatalln("no suitable printer found")
	}
	if err := p.Initialize(); err != nil {
		log.Fatalln(err)
	}
	if err := p.UpdateStatus(); err != nil {
		log.Fatalln(err)
	}

	// Check the picture against the media in the printer.
	mi := ql.GetMediaInfo(
		p.LastStatus.MediaWidthMM(),
		p.LastStatus.MediaLengthMM(),
	)
	if mi == nil {
		log.Fatalln("unknown media")
	}

	bounds := img.Bounds()
	dx, dy := bounds.Dx(), bounds.Dy()
	if dx > mi.PrintAreaPins {
		log.Fatalln("the image is too wide,", dx, ">", mi.PrintAreaPins, "pt")
	}
	if dy > mi.PrintAreaLength && mi.PrintAreaLength != 0 {
		log.Fatalln("the image is too high,", dy, ">", mi.PrintAreaLength, "pt")
	}

	if err := p.Print(img, *redblack); err != nil {
		log.Fatalln(err)
	}
}