-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
102 lines (90 loc) · 2.07 KB
/
images.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
package entities
import (
"errors"
"fmt"
"image"
_ "image/jpeg"
"os"
"strconv"
)
var _ = fmt.Println // remove after test
type Image struct {
Key *Key
FileName string
Caption string
AltText string
height int
width int
}
func NewImage(n string) (*Image,error) {
p := new(Image)
p.Key = makeKey("Image")
if reader, err := os.Open(n); err == nil {
defer reader.Close()
im, _, err := image.DecodeConfig(reader)
if err != nil {
return nil, err
}
p.width = im.Width
p.height = im.Height
p.FileName = n
return p, nil
} else {
err := errors.New("Can't open " + n)
return nil, err
}
}
func (a *Image) getHeight() int {
return a.height
}
func (a *Image) getWidth() int {
return a.width
}
func (a *Image) Triples () [][3]string {
var t [][3]string
t = append(t, makeTriple(Triple{(*a).Key,"hasType","Image",nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasFileName",(*a).FileName,nil}))
if (*a).Caption != "" {t = append(t, makeTriple(Triple{(*a).Key,"hasCaption",(*a).Caption,nil}))}
if (*a).AltText != "" {t = append(t, makeTriple(Triple{(*a).Key,"hasAltText",(*a).AltText,nil}))}
if (*a).height != 0 {t = append(t, makeTriple(Triple{(*a).Key,"hasHeight",strconv.Itoa((*a).height),nil}))}
if (*a).width != 0 {t = append(t, makeTriple(Triple{(*a).Key,"hasWidth",strconv.Itoa((*a).width),nil}))}
return t
}
func (a *Image) Row() []string {
var t []string
t = append(t, a.Key.s)
t = append(t, a.FileName)
t = append(t, a.Caption)
t = append(t, a.AltText)
t = append(t, strconv.Itoa((*a).height))
t = append(t, strconv.Itoa((*a).width))
return t
}
func FindImageKey (kf *Key) int {
for i,a := range Images {
if kf.s == a.Key.s {
return i
}
}
return -1
}
func AddImageFact(a []string) {
key := new(Key)
key.s = a[0]
i := FindImageKey(key)
switch a[1] {
case "FileName":
Images[i].FileName = a[2]
case "Caption":
Images[i].Caption = a[2]
case "AltText":
Images[i].AltText = a[2]
case "Height":
n,_ := strconv.Atoi(a[2])
Images[i].height = n
case "Width":
n,_ := strconv.Atoi(a[2])
Images[i].width = n
}
}
var Images []*Image