forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_obj.go
205 lines (168 loc) · 3.66 KB
/
image_obj.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gopdf
import (
"bytes"
"fmt"
"image"
// Packages image/jpeg and image/png are not used explicitly in the code below,
// but are imported for their initialization side-effect, which allows
// image.Decode to understand JPEG formatted images.
_ "image/jpeg"
_ "image/png"
"io"
"log"
"os"
)
// ImageObj image object
type ImageObj struct {
//imagepath string
IsMask bool
SplittedMask bool
rawImgReader *bytes.Reader
imginfo imgInfo
pdfProtection *PDFProtection
//getRoot func() *GoPdf
}
func (i *ImageObj) init(funcGetRoot func() *GoPdf) {
}
func (i *ImageObj) setProtection(p *PDFProtection) {
i.pdfProtection = p
}
func (i *ImageObj) protection() *PDFProtection {
return i.pdfProtection
}
func (i *ImageObj) write(w io.Writer, objID int) error {
data := i.imginfo.data
if i.IsMask {
data = i.imginfo.smask
if err := writeMaskImgProps(w, i.imginfo); err != nil {
return err
}
} else {
if err := writeImgProps(w, i.imginfo, i.SplittedMask); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "\t/Length %d\n>>\n", len(data)); err != nil {
return err
}
if _, err := io.WriteString(w, "stream\n"); err != nil {
return err
}
if i.protection() != nil {
tmp, err := rc4Cip(i.protection().objectkey(objID), data)
if err != nil {
return err
}
if _, err := w.Write(tmp); err != nil {
return err
}
if _, err := io.WriteString(w, "\n"); err != nil {
return err
}
} else {
if _, err := w.Write(data); err != nil {
return err
}
}
if _, err := io.WriteString(w, "\nendstream\n"); err != nil {
return err
}
return nil
}
func (i *ImageObj) isColspaceIndexed() bool {
return isColspaceIndexed(i.imginfo)
}
func (i *ImageObj) haveSMask() bool {
return haveSMask(i.imginfo)
}
func (i *ImageObj) createSMask() (*SMask, error) {
var smk SMask
smk.setProtection(i.protection())
smk.w = i.imginfo.w
smk.h = i.imginfo.h
smk.colspace = "DeviceGray"
smk.bitsPerComponent = "8"
smk.filter = i.imginfo.filter
smk.data = i.imginfo.smask
smk.decodeParms = fmt.Sprintf("/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns %d", i.imginfo.w)
return &smk, nil
}
func (i *ImageObj) createDeviceRGB() (*DeviceRGBObj, error) {
var dRGB DeviceRGBObj
dRGB.data = i.imginfo.pal
return &dRGB, nil
}
func (i *ImageObj) getType() string {
return "Image"
}
// SetImagePath set image path
func (i *ImageObj) SetImagePath(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
err = i.SetImage(file)
if err != nil {
return err
}
return nil
}
// SetImage set image
func (i *ImageObj) SetImage(r io.Reader) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
i.rawImgReader = bytes.NewReader(data)
return nil
}
// GetRect get rect of img
func (i *ImageObj) GetRect() *Rect {
rect, err := i.getRect()
if err != nil {
log.Fatalf("%+v", err)
}
return rect
}
// GetRect get rect of img
func (i *ImageObj) getRect() (*Rect, error) {
i.rawImgReader.Seek(0, 0)
m, _, err := image.Decode(i.rawImgReader)
if err != nil {
return nil, err
}
imageRect := m.Bounds()
k := 1
w := -128 //init
h := -128 //init
if w < 0 {
w = -imageRect.Dx() * 72 / w / k
}
if h < 0 {
h = -imageRect.Dy() * 72 / h / k
}
if w == 0 {
w = h * imageRect.Dx() / imageRect.Dy()
}
if h == 0 {
h = w * imageRect.Dy() / imageRect.Dx()
}
var rect = new(Rect)
rect.H = float64(h)
rect.W = float64(w)
return rect, nil
}
func (i *ImageObj) parse() error {
i.rawImgReader.Seek(0, 0)
imginfo, err := parseImg(i.rawImgReader)
if err != nil {
return err
}
i.imginfo = imginfo
return nil
}
// Parse parse img
func (i *ImageObj) Parse() error {
return i.parse()
}