File size: 2,710 Bytes
e9fe176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
require 'lfs'
local ut = require 'utils'

local www_utils = {}

-- isNorm is deprecated
function www_utils.saveIms(ims, imPath, isNorm, count)
  count = count or 0
  isNorm = isNorm or false
  local numIms = ut.getLength(ims)

  local filenames = {}
  for i = 1, numIms do
    filenames[i] = string.format("%08d.jpg", count + i)
    -- Scale the image and save
    local im = ims[i]
    if isNorm then
      im = ims[i]:clone()
      local maxVal = im:max()
      local minVal = im:min()
      im:add(-minVal):mul(1.0 / (maxVal - minVal))
    end
    image.save(paths.concat(imPath, filenames[i]), im)
  end

  return filenames, count + numIms
end

-- ims and captions are both tables
function www_utils.renderTables(ims, captions, ncol, width, imPath)
  assert(#captions == 0 or #captions == #ims, "#captions should be either 0 or equal to #ims")

  local nrow = math.ceil(#ims / ncol)
  local htmlStr = "<table>\n"

  local k = 0

  for i = 1, nrow do
    local captionRow = "<td>"..i.."</td>"
    local imRow = "<td>"..i.."</td>"
    for j = 1, ncol do
      k = k + 1

      if #captions > 0 then
        captionRow = captionRow .. string.format("<td>%s</td>", captions[k])
      end
      imRow = imRow .. string.format("<td><img width="..width.." src='%s'></img></td>", imPath .. '/' .. ims[k])
      if k >= #ims then break end
    end

    if #captions > 0 then
      htmlStr = htmlStr .. "<tr>" .. captionRow .. "</tr>\n"
    end
    htmlStr = htmlStr .. "<tr>" .. imRow .. "</tr>\n"

    if k >= #ims then break end
  end

  return htmlStr .. "</table>\n"
end

function www_utils.renderKeyValues(t)
  -- Write all key_value pairs
  local htmlStr = "<table>\n<tr><td>Key</td><td>Value</td></tr>\n"

  for k, v in pairs(t) do
    htmlStr = htmlStr .. "<tr><td>" .. k .. "</td><td>" .. v .. "</td><tr>\n"
  end

  return htmlStr .. "</table>\n"
end

function www_utils.renderHeader()
  return [[
<head>
<style>
    * {
        font-size: 24px;
    }
</style>
</head>
]]
end

function www_utils.renderHtml(rootDir, ims, captions, ncol, isNorm, width, htmlFile)
  captions = captions or {}
  ncol = ncol or 8
  isNorm = isNorm or false
  width = width or 256
  htmlFile = htmlFile or 'index.html'

  local imRelativeDir = './im'
  local imDir = paths.concat(rootDir, imRelativeDir)

  lfs.mkdir(rootDir)
  lfs.mkdir(imDir)

  local count = 0

  local f = io.open(paths.concat(rootDir, htmlFile), "w")
  f:write('<html>\n'..www_utils.renderHeader()..'<body>\n')

  local imNames = www_utils.saveIms(ims, imDir, isNorm, count)
  local htmlStr = www_utils.renderTables(imNames, captions, ncol, width, imRelativeDir)

  f:write(htmlStr .. '</body>\n</html>')
  f:close()

  return count
end

return www_utils