summaryrefslogtreecommitdiff
path: root/html/examples/videocapture.wlua
blob: f6b065fd4d3f62ebd35a59c6c0995baf981e532f (plain)
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
local im = require "imlua"
require "imlua_capture"
local gl = require "luagl"
local iup = require "iuplua"
require "iupluagl"

local vc = im.VideoCaptureCreate()
if (not vc) then
  error("ERROR: No capture device found!")
end

if (vc:Connect(0) == 0) then
  error("ERROR: Fail to connect to capture device")
end

local capw, caph = vc:GetImageSize()
local initimgsize = string.format("%ix%i", capw, caph)


local frbuf = im.ImageCreate(capw, caph, im.RGB, im.BYTE)
local gldata, glformat = frbuf:GetOpenGLData()

cnv = iup.glcanvas{buffer="DOUBLE", rastersize = initimgsize}

function cnv:resize_cb(width, height)
  iup.GLMakeCurrent(self)
  gl.Viewport(0, 0, width, height)
end

function cnv:action(x, y)
  iup.GLMakeCurrent(self)
  gl.PixelStore(gl.UNPACK_ALIGNMENT, 1)

  gldata, glformat = frbuf:GetOpenGLData() --update the data
  gl.DrawPixelsRaw (capw, caph, glformat, gl.UNSIGNED_BYTE, gldata)

  iup.GLSwapBuffers(self)
end


vc:Live(1)

local dlg = iup.dialog{title = "Oh Snap!", cnv}

local in_loop = true

function dlg:close_cb()
  in_loop = false
end

function dlg:k_any(c)
  if c == iup.K_q or c == iup.K_ESC then
    return iup.CLOSE
  end

  if c == iup.K_F1 then
    if fullscreen then
      fullscreen = false
      dlg.fullscreen = "No"
    else
      fullscreen = true
      dlg.fullscreen = "Yes"
    end
    iup.SetFocus(cnv)
  end
end

dlg:show()
iup.SetFocus(cnv)

while in_loop do
  vc:Frame(frbuf,-1)
  iup.Update(cnv)
  local result = iup.LoopStep()
  if result == iup.CLOSE then
    in_loop = false
  end
end

vc:Live(0)
vc:Disconnect()
vc:Destroy()