/
coding.go
62 lines (48 loc) · 1.7 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
// Package imagecoding provides go bindings for en-/de-coding images using
// image processing c libraries found in common systems
package imagecoding
import (
"errors"
"math"
"github.com/harukasan/go-libwebp/webp"
)
// RGBImage is a good idea, so let's borrow it and make it our own
type RGBImage = webp.RGBImage
type RGB = webp.RGB
var RGBModel = webp.RGBModel
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 EmptyInputError = errors.New("empty input data")