-
Notifications
You must be signed in to change notification settings - Fork 2
/
coding.go
129 lines (107 loc) · 3.31 KB
/
coding.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package imagecoding provides go bindings for en-/de-coding images using
// image processing c libraries found in common systems
package imagecoding
import (
"errors"
"image"
"image/color"
"math"
)
type ScaleFunc func(pageWidth, pageHeight int) (imgWidth, imgHeight int, scaleFactor float64)
const (
A4Short = 210 / 25.4 * 150 // 210 mm / 25.4 mm/inch * 150 ppi ≈ 1204 pixels
A4Long = 297 / 25.4 * 150 // 297 mm / 25.4 mm/inch * 150 ppi ≈ 1754 pixels
)
// Calculcate at what scale to use for OCR optimized pages
// We prefer maximum what would be the equivalent for a A4 page at 150 ppi
func DefaultScale(pageWidth, pageHeight int) (imgWidth, imgHeight int, scaleFactor float64) {
w := float64(pageWidth)
h := float64(pageHeight)
// The maximum size we will render, capped at A4 paper equivalent
// Expressed as short & long for orientation support
reqShort := math.Min(w, h)
reqLong := math.Max(w, h)
maxShort := math.Min(reqShort, A4Short)
maxLong := math.Min(reqLong, A4Long)
// Calculate the scale factor
shortRatio := maxShort / math.Min(w, h)
longRatio := maxLong / math.Max(w, h)
scaleFactor = math.Min(shortRatio, longRatio)
// Round to integers
imgWidth = int(math.Round(w * scaleFactor))
imgHeight = int(math.Round(h * scaleFactor))
return imgWidth, imgHeight, scaleFactor
}
type ImgFormat string
const (
Bmp ImgFormat = "bmp"
Gif ImgFormat = "gif"
Png ImgFormat = "png"
Jpeg ImgFormat = "jpg"
Tiff ImgFormat = "tif"
Webp ImgFormat = "webp"
Heif ImgFormat = "heif"
)
var ErrEmptyInput = errors.New("empty input data")
// RGBImage represent image data which has RGB colors.
// RGBImage is compatible with image.RGBA, but does not have alpha channel to reduce using memory.
type RGBImage struct {
// Pix holds the image's stream, in R, G, B order.
Pix []uint8
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewRGBImage allocates and returns RGB image
func NewRGBImage(r image.Rectangle) *RGBImage {
w, h := r.Dx(), r.Dy()
return &RGBImage{Pix: make([]uint8, 3*w*h), Stride: 3 * w, Rect: r}
}
// ColorModel returns RGB color model.
func (p *RGBImage) ColorModel() color.Model {
return RGBModel
}
// Bounds implements image.Image.At
func (p *RGBImage) Bounds() image.Rectangle {
return p.Rect
}
// At implements image.Image.At
func (p *RGBImage) At(x, y int) color.Color {
return p.RGBAAt(x, y)
}
// RGBAAt returns the color of the pixel at (x, y) as RGBA.
func (p *RGBImage) RGBAAt(x, y int) color.RGBA {
if !(image.Point{x, y}.In(p.Rect)) {
return color.RGBA{}
}
i := (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
return color.RGBA{p.Pix[i+0], p.Pix[i+1], p.Pix[i+2], 0xFF}
}
// RGBModel is RGB color model instance
var RGBModel = color.ModelFunc(rgbModel)
func rgbModel(c color.Color) color.Color {
if _, ok := c.(RGB); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
}
// RGB color
type RGB struct {
R, G, B uint8
}
// RGBA implements Color.RGBA
func (c RGB) RGBA() (r, g, b, a uint32) {
r = uint32(c.R)
r |= r << 8
g = uint32(c.G)
g |= g << 8
b = uint32(c.B)
b |= b << 8
a = uint32(0xFFFF)
return
}
// Make sure RGBImage implements image.Image.
// See https://golang.org/doc/effective_go.html#blank_implements.
var _ image.Image = new(RGBImage)