-
Notifications
You must be signed in to change notification settings - Fork 59
/
helper.go
129 lines (120 loc) · 2.65 KB
/
helper.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 gofpdi
import (
"strings"
)
// Determine if a value is numeric
// Courtesy of https://github.com/syyongx/php2go/blob/master/php.go
func is_numeric(val interface{}) bool {
switch val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
case float32, float64, complex64, complex128:
return true
case string:
str := val.(string)
if str == "" {
return false
}
// Trim any whitespace
str = strings.TrimSpace(str)
//fmt.Println(str)
if str[0] == '-' || str[0] == '+' {
if len(str) == 1 {
return false
}
str = str[1:]
}
// hex
if len(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') {
for _, h := range str[2:] {
if !((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) {
return false
}
}
return true
}
// 0-9,Point,Scientific
p, s, l := 0, 0, len(str)
for i, v := range str {
if v == '.' { // Point
if p > 0 || s > 0 || i+1 == l {
return false
}
p = i
} else if v == 'e' || v == 'E' { // Scientific
if i == 0 || s > 0 || i+1 == l {
return false
}
s = i
} else if v < '0' || v > '9' {
return false
}
}
return true
}
return false
}
func in_array(needle interface{}, hystack interface{}) bool {
switch key := needle.(type) {
case string:
for _, item := range hystack.([]string) {
if key == item {
return true
}
}
case int:
for _, item := range hystack.([]int) {
if key == item {
return true
}
}
case int64:
for _, item := range hystack.([]int64) {
if key == item {
return true
}
}
default:
return false
}
return false
}
// Taken from png library
// intSize is either 32 or 64.
const intSize = 32 << (^uint(0) >> 63)
func abs(x int) int {
// m := -1 if x < 0. m := 0 otherwise.
m := x >> (intSize - 1)
// In two's complement representation, the negative number
// of any number (except the smallest one) can be computed
// by flipping all the bits and add 1. This is faster than
// code with a branch.
// See Hacker's Delight, section 2-4.
return (x ^ m) - m
}
// filterPaeth applies the Paeth filter to the cdat slice.
// cdat is the current row's data, pdat is the previous row's data.
func filterPaeth(cdat, pdat []byte, bytesPerPixel int) {
var a, b, c, pa, pb, pc int
for i := 0; i < bytesPerPixel; i++ {
a, c = 0, 0
for j := i; j < len(cdat); j += bytesPerPixel {
b = int(pdat[j])
pa = b - c
pb = a - c
pc = abs(pa + pb)
pa = abs(pa)
pb = abs(pb)
if pa <= pb && pa <= pc {
// No-op.
} else if pb <= pc {
a = b
} else {
a = c
}
a += int(cdat[j])
a &= 0xff
cdat[j] = uint8(a)
c = b
}
}
}