summaryrefslogtreecommitdiff
path: root/pkg/line/line.go
blob: b1e90218161e1d0d02f1539eba277207037cc57b (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
// Copyright 2019 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package line

import (
	"image"
	"image/png"
	"io"
	"os"
)

type Detail struct {
	Name    string
	Avgconf float64
	Img     CopyableImg
	Text    string
	OcrName string
}

type CopyableImg interface {
	CopyLineTo(io.Writer) error
}

type Details []Detail

func (l Details) Len() int           { return len(l) }
func (l Details) Less(i, j int) bool { return l[i].Avgconf < l[j].Avgconf }
func (l Details) Swap(i, j int)      { l[i], l[j] = l[j], l[i] }

// This is an implementation of the CopyableImg interface that
// stores the image directly as an image.Image
type ImgDirect struct {
	Img image.Image
}

func (i ImgDirect) CopyLineTo(w io.Writer) error {
	err := png.Encode(w, i.Img)
	if err != nil {
		return err
	}
	return nil
}

// This is an implementation of the CopyableImg interface that
// stores the path of an image
type ImgPath struct {
	Path string
}

func (i ImgPath) CopyLineTo(w io.Writer) error {
	f, err := os.Open(i.Path)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = io.Copy(w, f)
	return err
}