-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprettydisplay.lua
95 lines (88 loc) · 2.17 KB
/
prettydisplay.lua
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
require 'math'
local function height(im)
if im:nDimension() == 3 then
return im:size(2)
elseif im:nDimension() == 2 then
return im:size(1)
else
error('prettydisplay.height : image must be 2D or 3D')
end
end
local function width(im)
if im:nDimension() == 3 then
if im:size(1) == 3 then
return im:size(3)
else
local output = 0
for i = 1,im:size(1) do
output = output + width(im[i])
end
return output
end
elseif im:nDimension() == 2 then
return im:size(2)
else
error('prettydisplay.width : image must be 2D or 3D')
end
end
local function printin(src, dst, y, x, ranges)
local h = height(src)
local w = width(src)
src = src:real()
if ranges[1] ~= "no" then
vmin = ranges[1] or src:min()
vmax = ranges[2] or src:max()
local vrange = vmax-vmin
if vrange ~= 0 then
src:add(-vmin)
src:div(vrange)
src:add(src:gt(1):real():cmul(-src+1))
src:add(src:lt(0):real():cmul(-src))
end
end
if src:nDimension() == 3 then
if src:size(1) ~= 3 then
local wlocal = 0
for i = 1,src:size(1) do
printin(src[i], dst, y, x+wlocal, {"no"})
wlocal = wlocal + width(src[i])
end
else
dst[{{},{y, y+h-1},{x,x+w-1}}]:copy(src)
end
elseif src:nDimension() == 2 then
dst[{1,{y, y+h-1},{x,x+w-1}}]:copy(src)
dst[{2,{y, y+h-1},{x,x+w-1}}]:copy(src)
dst[{3,{y, y+h-1},{x,x+w-1}}]:copy(src)
else
error('prettydisplay.printin : image must be 2D or 3D')
end
end
function prettydisplay(input, ranges)
local h = 0
local w = 0
local hs = {}
for i = 1,#input do
local line = input[i]
local hlocal = 0
local wlocal = 0
for j = 1,#line do
hlocal = math.max(hlocal, height(line[j]))
wlocal = wlocal + width(line[j])
end
hs[i] = h
h = h + hlocal
w = math.max(w, wlocal)
end
local todisp = torch.Tensor(3, h, w):zero()
for i = 1,#input do
local line = input[i]
local lineranges = ranges[i]
local wlocal = 0
for j = 1,#line do
printin(line[j], todisp, hs[i]+1, wlocal+1, lineranges[j])
wlocal = wlocal + width(line[j])
end
end
return todisp
end