-
Notifications
You must be signed in to change notification settings - Fork 14
/
img.go
139 lines (114 loc) · 3.18 KB
/
img.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
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"log"
"strconv"
"strings"
"os"
"github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/creator"
"golang.org/x/image/tiff"
)
var pageMargin [4]float64
var pageSize creator.PageSize
var sizeHasSet, marginHasSet = false, false
var tiffExts = []string{".tiff", ".TIFF", ".tif", ".TIF"}
type ImgSource struct {
source
}
func (s ImgSource) MergeTo(c *creator.Creator) error {
debugInfo(fmt.Sprintf("Adding Image: %+v", s.source))
return addImage(s.source, c)
}
func addImage(s source, c *creator.Creator) error {
img, err := createImage(s, c)
if err != nil {
return err
}
// The following funcs must be called in sequence
setEncoding(img, s)
setMargin(img, c)
setSize(img, c)
c.NewPage()
err = c.Draw(img)
if err != nil {
return err
}
return nil
}
func createImage(s source, c *creator.Creator) (*creator.Image, error) {
if in_array(s.ext, tiffExts) {
f, _ := os.Open(s.path)
i, err := tiff.Decode(f)
if err != nil {
log.Fatalln(err.Error())
}
return creator.NewImageFromGoImage(i)
}
return creator.NewImageFromFile(s.path)
}
func setMargin(img *creator.Image, c *creator.Creator) {
if !marginHasSet {
for i, m := range strings.Split(margin, ",") {
floatVal, err := strconv.ParseFloat(m, 64)
if err != nil {
log.Fatalln("Error: -m|--margin MUST be 4 comma separated int/float numbers. %s found.", m)
}
pageMargin[i] = floatVal * creator.PPI
}
if len(pageMargin) != 4 {
log.Fatalln("Error: -m|--margin MUST be 4 comma separated int/float numbers. %s provided.", margin)
}
marginHasSet = true
}
c.SetPageMargins(pageMargin[0], pageMargin[1], pageMargin[2], pageMargin[3])
img.SetPos(pageMargin[0], pageMargin[3])
}
func setSize(img *creator.Image, c *creator.Creator) {
if size == DefaultSize {
// sizeHasSet will remain false so that it changes for each image
// Width height with adding margin
w := img.Width() + pageMargin[0] + pageMargin[1]
h := img.Height() + pageMargin[2] + pageMargin[3]
pageSize = creator.PageSize{w, h}
} else {
sizeHasSet = true
switch size {
case "A4":
pageSize = creator.PageSizeA4
case "A3":
pageSize = creator.PageSizeA3
case "Legal":
pageSize = creator.PageSizeLegal
case "Letter":
pageSize = creator.PageSizeLetter
default:
log.Fatalln("Error: -s|--size MUST be one of A4, A3, Legal or Letter. %s given.", size)
}
if scaleH {
img.ScaleToHeight(pageSize[1] - (pageMargin[2] + pageMargin[3]))
} else if scaleW {
img.ScaleToWidth(pageSize[0] - (pageMargin[0] + pageMargin[1]))
}
}
debugInfo(fmt.Sprintf("Created Page Size: %v", pageSize))
if landscape {
c.SetPageSize(creator.PageSize{pageSize[1], pageSize[0]})
} else {
c.SetPageSize(pageSize)
}
}
// Set appropriate encoding for JPEG and TIFF
// MUST be called before changing image size
func setEncoding(img *creator.Image, s source) {
switch s.mime {
case "image/jpeg":
debugInfo(fmt.Sprintf("JPEG found. Setting encoding to DCTEncoder."))
encoder := core.NewDCTEncoder()
encoder.Quality = JPEGQuality
// Encoder dimensions must match the raw image data dimensions.
encoder.Width = int(img.Width())
encoder.Height = int(img.Height())
img.SetEncoder(encoder)
}
}