/
filters.go
216 lines (178 loc) · 4.42 KB
/
filters.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
206
207
208
209
210
211
212
213
214
215
216
package png
import (
"bytes"
"errors"
)
const (
None =byte(0)
Sub = byte(1)
Up = byte(2)
Average = byte(3)
Paeth = byte(4)
)
type Filter interface {
Process([]byte,[][]byte) ([]byte, error)
}
func get_a(i uint32,minBytesPerPixel uint32,data []byte ) byte{
if i < minBytesPerPixel{
return 0
}else{
return data[i - minBytesPerPixel]
}
}
func get_b(i uint32,line uint32,minBytesPerPixel uint32,data [][]byte) byte{
return data[line-1][i]
}
func get_c(i uint32,line uint32,minBytesPerPixel uint32,data [][]byte) byte{
if i < minBytesPerPixel{
return 0
}else{
before_bLine := line-1
before_bColumn := i - minBytesPerPixel
return get_b(before_bColumn,before_bLine,minBytesPerPixel,data)
}
}
func (ihdr *IHDRChunk) FilterStep() uint32 {
samples,_ := ihdr.GetSamples()
if ihdr.Bitdepth == 16{
samples = samples*2
}
return samples
}
type IdentityFilter struct{}
func (f *IdentityFilter) Process(data []byte,processed [][]byte) ([]byte, error) {
return data, nil
}
type UndoSubFilter struct{
ihdr *IHDRChunk
}
func (f *UndoSubFilter) Process(data []byte,processed [][]byte ) ([]byte, error) {
if len(data) == 0{
return nil, errors.New("No Data To Filter")
}
step := f.ihdr.FilterStep()
recon := make([]byte, len(data))
for i:=uint32(0) ; i < uint32(len(data)) ; i++{
a := get_a(i,step,recon)
recon[i] = data[i] + a
}
return recon, nil
}
type UndoUpFilter struct{
ihdr *IHDRChunk
}
func (self *UndoUpFilter) Process(data []byte,processed [][]byte) ([]byte, error) {
if len(data) == 0{
return nil, errors.New("No Data To Filter")
}
step := self.ihdr.FilterStep()
recon := make([]byte, len(data))
line := uint32(len(processed))
for i:=uint32(0) ; i< uint32(len(data)) ; i++{
reconb := get_b(i,line,step,processed)
recon[i] = data[i] + reconb
}
return recon, nil
}
type UndoAverageFilter struct{
ihdr *IHDRChunk
}
func (self *UndoAverageFilter) Process(data []byte,processed [][]byte) ([]byte, error) {
if len(data) == 0{
return nil, errors.New("No Data To Filter")
}
recon := make([]byte, len(data))
step := self.ihdr.FilterStep()
line := uint32(len(processed))
for i:= uint32(0) ; i< uint32(len(data)) ; i++{
recona := get_a(i,step,data)
reconb := get_b(i,line,step,processed)
recon[i] = data[i] + (recona + reconb)/2
}
return recon, nil
}
type UndoPaethFilter struct{
ihdr *IHDRChunk
}
func abs( a int) int{
if a < 0{
return -a
}
return a
}
func (self *UndoPaethFilter) PaethPredictor(a byte,b byte,c byte) byte{
p := int(a) + int(b) - int(c)
pa := abs( p - int(a))
pb := abs(p - int(b))
pc := abs(p - int(c))
Pr := c
if pa <= pb && pa <= pc {
Pr = a
}else if pb <= pc {
Pr = b
}
return Pr
}
func (self *UndoPaethFilter) Process(data []byte,processed [][]byte) ([]byte, error) {
if len(data) == 0{
return nil, errors.New("No Data To Filter")
}
recon := make([]byte, len(data))
step := self.ihdr.FilterStep()
line := uint32(len(processed))
for i:=uint32(0) ; i< uint32(len(data)) ; i++{
a := get_a(i,step,data)
b := get_b(i,line,step,processed)
c := get_c(i,line,step,processed)
recon[i] = data[i] + self.PaethPredictor(a,b,c)
}
return recon, nil
}
func createInverseFilter(b byte,ihdr *IHDRChunk) (Filter, error) {
switch b {
// No Filtering
case None:
return &IdentityFilter{}, nil
case Sub:
return &UndoSubFilter{ihdr: ihdr},nil// Sub
case Up:
return &UndoUpFilter{ihdr: ihdr},nil
case Average:
return &UndoAverageFilter{ihdr: ihdr},nil
case Paeth:
return &UndoPaethFilter{ihdr: ihdr},nil
}
return nil, errors.New("Not Supported Filtering Type")
}
func Unfilter( filtered_bytes []byte, ihdr *IHDRChunk ) ([][]byte,error) {
line_size,err := ihdr.GetBytesPerLine()
if err != nil{
return nil,err
}
// Add Filter line header byte
line_size++
buffer := bytes.NewBuffer(filtered_bytes)
nLines := len(filtered_bytes) / int(line_size)
if len(filtered_bytes) % int(line_size) != 0 {
nLines++
}
image := make([][]byte, 0)
for iLine:=0 ; iLine < nLines;iLine++ {
filter_type,_ := buffer.ReadByte()
filtered_line := make([]byte,line_size-1)
_,err := buffer.Read(filtered_line)
if err != nil{
return nil,err
}
reverse_filter,err := createInverseFilter(filter_type,ihdr)
if err != nil{
return nil,err
}
unfiltered_line,err := reverse_filter.Process(filtered_line,image)
if err != nil{
return nil,err
}
image = append(image, unfiltered_line)
}
return image,nil
}