summaryrefslogtreecommitdiff
path: root/hocr
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-01-23 20:23:31 +0000
committerNick White <git@njw.name>2019-01-23 20:23:31 +0000
commitc41aa16d8a3d35ce4185184ee50536bf2089a120 (patch)
treed405ce03980a1c89996997693bcd6b968f6c13c2 /hocr
parent2e5f7a80673c29b778f76f8f17896440ab75b4d4 (diff)
Move image copying out to an interface function, so I can share code with line-conf-buckets easily and well
Diffstat (limited to 'hocr')
-rw-r--r--hocr/hocr.go34
1 files changed, 26 insertions, 8 deletions
diff --git a/hocr/hocr.go b/hocr/hocr.go
index 4384bad..0c1295c 100644
--- a/hocr/hocr.go
+++ b/hocr/hocr.go
@@ -2,30 +2,46 @@ package hocr
// TODO: separate out linedetail to a general structure that can incorporate
// line-conf-buckets too, in a different file (and rename package to
-// something more generic). Try to use interfaces to fill it with hocr
-// or ocropy data simply. Could design it so LineDetail is an interface,
-// that can do CopyLines() and BucketUp(). Actually only BucketUp() would
-// be needed, as copylines can be internal to that. Then can create more
-// methods as and when needed. BucketUp() can take some form of arguments
-// that can make it flexible, like []struct { dirname string, minconf float64 }
+// something more generic). Do this using the CopyableLine interface
// TODO: Parse line name to zero pad line numbers, so they come out in the correct order
import (
"encoding/xml"
"image"
+ "image/png"
+ "io"
"regexp"
"strconv"
"strings"
)
+// TODO: move the linedetail stuff out to a separate file, and create a new
+// CopyableLine implementing struct for ocropy, which will just store
+// a file location
type LineDetail struct {
Name string
Avgconf float64
- Img image.Image
+ Img CopyableLine
Text string
Hocrname string
}
+type CopyableLine interface {
+ CopyLineTo(io.Writer) (error)
+}
+
+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
+}
+
type LineDetails []LineDetail
// Used by sort.Sort.
@@ -163,7 +179,9 @@ func GetLineDetails(h Hocr, i image.Image, name string) (LineDetails, error) {
line.Text = strings.TrimRight(linetext, " ")
line.Text += "\n"
line.Hocrname = name
- line.Img = i.(*image.Gray).SubImage(image.Rect(coords[0], coords[1], coords[2], coords[3]))
+ var imgd ImgDirect
+ imgd.img = i.(*image.Gray).SubImage(image.Rect(coords[0], coords[1], coords[2], coords[3]))
+ line.Img = imgd
lines = append(lines, line)
}
return lines, nil