forked from ikawaha/waifu2x.go
/
image_plane.go
156 lines (130 loc) · 4 KB
/
image_plane.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
package waifu2x
import (
"fmt"
"math"
)
const (
BlockSize = 128
Overlap = 14
)
type ImagePlane struct {
Width int
Height int
Buffer []float64
}
func NewImagePlane(w, h int) *ImagePlane {
return &ImagePlane{
Width: w,
Height: h,
Buffer: make([]float64, w*h),
}
}
func (p *ImagePlane) getLength() int {
return len(p.Buffer)
}
func (p *ImagePlane) getBuffer() []float64 {
return p.Buffer
}
func (p *ImagePlane) index(w, h int) int {
return w + h*p.Width
}
func (p *ImagePlane) getValue(w, h int) float64 {
i := p.index(w, h)
if i < 0 || i >= len(p.Buffer) {
panic(fmt.Errorf("w %d, h %d, index %d, len(buf) %d", w, h, i, len(p.Buffer)))
}
//fmt.Printf("w %d, h %d, index %d, len(buf) %d\n", w, h, i, len(p.Buffer))
return p.Buffer[i]
}
func (p *ImagePlane) getBlock(x, y int) (float64, float64, float64, float64, float64, float64, float64, float64, float64) {
i := (x - 1) + (y-1)*p.Width
j := i + p.Width
k := j + p.Width
a := p.Buffer[i : i+3 : i+3]
b := p.Buffer[j : j+3 : j+3]
c := p.Buffer[k : k+3 : k+3]
return a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2]
}
func (p *ImagePlane) setValue(w, h int, v float64) {
p.Buffer[p.index(w, h)] = v
}
func (p *ImagePlane) getValueIndexed(i int) float64 {
return p.Buffer[i]
}
func (p *ImagePlane) setValueIndexed(i int, v float64) {
p.Buffer[i] = v
}
func blocking(initialPlanes []*ImagePlane) ([][]*ImagePlane, int, int) {
widthInput := initialPlanes[0].Width
heightInput := initialPlanes[0].Height
blocksW := int(math.Ceil(float64(widthInput-Overlap) / float64(BlockSize-Overlap)))
blocksH := int(math.Ceil(float64(heightInput-Overlap) / float64(BlockSize-Overlap)))
blocks := blocksW * blocksH
//fmt.Println("BlockSize:", BlockSize)
//fmt.Printf("blocksW:%d, blocksH:%d, blocks:%d\n", blocksW, blocksH, blocks)
inputBlocks := make([][]*ImagePlane, blocks)
for b := 0; b < blocks; b++ {
blockIndexW := b % blocksW
blockIndexH := b / blocksW
//fmt.Printf("blockIndexW:%d, blockIndexH:%d\n", blockIndexW, blockIndexH)
blockWidth := BlockSize
blockHeight := BlockSize
if blockIndexW == blocksW-1 {
blockWidth = widthInput - ((BlockSize - Overlap) * blockIndexW) // right end block
}
if blockIndexH == blocksH-1 {
blockHeight = heightInput - ((BlockSize - Overlap) * blockIndexH) // bottom end block
}
//fmt.Printf("\t>>blockWidth:%d, blockHeight:%d\n", blockWidth, blockHeight)
channels := make([]*ImagePlane, len(initialPlanes))
for i := 0; i < len(initialPlanes); i++ {
channels[i] = NewImagePlane(blockWidth, blockHeight)
}
for w := 0; w < blockWidth; w++ {
for h := 0; h < blockHeight; h++ {
for i := 0; i < len(initialPlanes); i++ {
targetIndexW := blockIndexW*(BlockSize-Overlap) + w
targetIndexH := blockIndexH*(BlockSize-Overlap) + h
channel := initialPlanes[i]
v := channel.getValue(targetIndexW, targetIndexH)
channels[i].setValue(w, h, v)
}
}
}
inputBlocks[b] = channels
}
return inputBlocks, blocksW, blocksH
}
func deblocking(outputBlocks [][]*ImagePlane, blocksW, blocksH int) []*ImagePlane {
blockSize := outputBlocks[0][0].Width
var width int
for b := 0; b < blocksW; b++ {
width += outputBlocks[b][0].Width
}
var height int
for b := 0; b < blocksW*blocksH; b += blocksW {
height += outputBlocks[b][0].Height
}
outputPlanes := make([]*ImagePlane, len(outputBlocks[0])) //XXX ???
for b := 0; b < len(outputBlocks); b++ {
block := outputBlocks[b]
blockIndexW := b % blocksW
blockIndexH := int(math.Floor(float64(b) / float64(blocksW)))
for i := 0; i < len(block); i++ {
if outputPlanes[i] == nil {
outputPlanes[i] = NewImagePlane(width, height)
}
channelBlock := block[i]
for w := 0; w < channelBlock.Width; w++ {
for h := 0; h < channelBlock.Height; h++ {
targetIndexW := blockIndexW*blockSize + w
targetIndexH := blockIndexH*blockSize + h
targetIndex := targetIndexH*width + targetIndexW
v := channelBlock.getValue(w, h)
outputPlanes[i].setValueIndexed(targetIndex, v)
}
}
}
}
return outputPlanes
}