Browse Source

Split out imgutil

master
Přemysl Janouch 1 month ago
parent
commit
b287ba5500
Signed by: Přemysl Janouch <p@janouch.name> GPG Key ID: A0420B94F92B9493
2 changed files with 60 additions and 56 deletions
  1. 57
    0
      imgutil/imgutil.go
  2. 3
    56
      label-tool/main.go

+ 57
- 0
imgutil/imgutil.go View File

@@ -0,0 +1,57 @@
1
+package imgutil
2
+
3
+import (
4
+	"image"
5
+	"image/color"
6
+)
7
+
8
+// Scale is a scaling image.Image wrapper.
9
+type Scale struct {
10
+	Image image.Image
11
+	Scale int
12
+}
13
+
14
+// ColorModel implements image.Image.
15
+func (s *Scale) ColorModel() color.Model {
16
+	return s.Image.ColorModel()
17
+}
18
+
19
+// Bounds implements image.Image.
20
+func (s *Scale) Bounds() image.Rectangle {
21
+	r := s.Image.Bounds()
22
+	return image.Rect(r.Min.X*s.Scale, r.Min.Y*s.Scale,
23
+		r.Max.X*s.Scale, r.Max.Y*s.Scale)
24
+}
25
+
26
+// At implements image.Image.
27
+func (s *Scale) At(x, y int) color.Color {
28
+	if x < 0 {
29
+		x = x - s.Scale + 1
30
+	}
31
+	if y < 0 {
32
+		y = y - s.Scale + 1
33
+	}
34
+	return s.Image.At(x/s.Scale, y/s.Scale)
35
+}
36
+
37
+// LeftRotate is a 90 degree rotating image.Image wrapper.
38
+type LeftRotate struct {
39
+	Image image.Image
40
+}
41
+
42
+// ColorModel implements image.Image.
43
+func (lr *LeftRotate) ColorModel() color.Model {
44
+	return lr.Image.ColorModel()
45
+}
46
+
47
+// Bounds implements image.Image.
48
+func (lr *LeftRotate) Bounds() image.Rectangle {
49
+	r := lr.Image.Bounds()
50
+	// Min is inclusive, Max is exclusive.
51
+	return image.Rect(r.Min.Y, -(r.Max.X - 1), r.Max.Y, -(r.Min.X - 1))
52
+}
53
+
54
+// At implements image.Image.
55
+func (lr *LeftRotate) At(x, y int) color.Color {
56
+	return lr.Image.At(-y, x)
57
+}

+ 3
- 56
label-tool/main.go View File

@@ -4,7 +4,6 @@ import (
4 4
 	"errors"
5 5
 	"html/template"
6 6
 	"image"
7
-	"image/color"
8 7
 	"image/draw"
9 8
 	"image/png"
10 9
 	"log"
@@ -16,62 +15,10 @@ import (
16 15
 	"github.com/boombuler/barcode/qr"
17 16
 
18 17
 	"janouch.name/sklad/bdf"
18
+	"janouch.name/sklad/imgutil"
19 19
 	"janouch.name/sklad/ql"
20 20
 )
21 21
 
22
-// scaler is a scaling image.Image wrapper.
23
-type scaler struct {
24
-	image image.Image
25
-	scale int
26
-}
27
-
28
-// ColorModel implements image.Image.
29
-func (s *scaler) ColorModel() color.Model {
30
-	return s.image.ColorModel()
31
-}
32
-
33
-// Bounds implements image.Image.
34
-func (s *scaler) Bounds() image.Rectangle {
35
-	r := s.image.Bounds()
36
-	return image.Rect(r.Min.X*s.scale, r.Min.Y*s.scale,
37
-		r.Max.X*s.scale, r.Max.Y*s.scale)
38
-}
39
-
40
-// At implements image.Image.
41
-func (s *scaler) At(x, y int) color.Color {
42
-	if x < 0 {
43
-		x = x - s.scale + 1
44
-	}
45
-	if y < 0 {
46
-		y = y - s.scale + 1
47
-	}
48
-	return s.image.At(x/s.scale, y/s.scale)
49
-}
50
-
51
-// leftRotate is a 90 degree rotating image.Image wrapper.
52
-type leftRotate struct {
53
-	image image.Image
54
-}
55
-
56
-// ColorModel implements image.Image.
57
-func (lr *leftRotate) ColorModel() color.Model {
58
-	return lr.image.ColorModel()
59
-}
60
-
61
-// Bounds implements image.Image.
62
-func (lr *leftRotate) Bounds() image.Rectangle {
63
-	r := lr.image.Bounds()
64
-	// Min is inclusive, Max is exclusive.
65
-	return image.Rect(r.Min.Y, -(r.Max.X - 1), r.Max.Y, -(r.Min.X - 1))
66
-}
67
-
68
-// At implements image.Image.
69
-func (lr *leftRotate) At(x, y int) color.Color {
70
-	return lr.image.At(-y, x)
71
-}
72
-
73
-// -----------------------------------------------------------------------------
74
-
75 22
 var font *bdf.Font
76 23
 
77 24
 func genLabelForHeight(text string, height, scale int) image.Image {
@@ -81,7 +28,7 @@ func genLabelForHeight(text string, height, scale int) image.Image {
81 28
 	draw.Draw(textImg, textRect, image.White, image.ZP, draw.Src)
82 29
 	font.DrawString(textImg, image.ZP, text)
83 30
 
84
-	scaledTextImg := scaler{image: textImg, scale: scale}
31
+	scaledTextImg := imgutil.Scale{Image: textImg, Scale: scale}
85 32
 	scaledTextRect := scaledTextImg.Bounds()
86 33
 
87 34
 	remains := height - scaledTextRect.Dy() - 20
@@ -238,7 +185,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
238 185
 
239 186
 	var label image.Image
240 187
 	if mediaInfo != nil {
241
-		label = &leftRotate{image: genLabelForHeight(
188
+		label = &imgutil.LeftRotate{Image: genLabelForHeight(
242 189
 			params.Text, mediaInfo.PrintAreaPins, params.Scale)}
243 190
 		if r.FormValue("print") != "" {
244 191
 			if err := printer.Print(label); err != nil {

Loading…
Cancel
Save