summaryrefslogtreecommitdiff
path: root/src/lua5/imlua_capture.c
blob: 59ec0fbcabf42fdb77da6ea05ce892a0f3d97342 (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/** \file
 * \brief IM Lua 5 Binding
 *
 * See Copyright Notice in im_lib.h
 * $Id: imlua_capture.c,v 1.2 2009/08/12 04:09:17 scuri Exp $
 */

#include <string.h>
#include <memory.h>

#include "im.h"
#include "im_image.h"
#include "im_capture.h"

#include <lua.h>
#include <lauxlib.h>

#include "imlua.h"
#include "imlua_aux.h"
#include "imlua_image.h"



static imVideoCapture** imlua_rawcheckvideocapture (lua_State *L, int param)
{
  return (imVideoCapture**)luaL_checkudata(L, param, "imVideoCapture");
}

static imVideoCapture* imlua_checkvideocapture (lua_State *L, int param)
{
  imVideoCapture** vc_p = imlua_rawcheckvideocapture(L, param);

  if (!(*vc_p))
    luaL_argerror(L, param, "destroyed imVideoCapture");

  return *vc_p;
}

static void imlua_pushvideocapture(lua_State *L, imVideoCapture* vc)
{
  if (!vc)
    lua_pushnil(L);
  else
  {
    imVideoCapture** vc_p = (imVideoCapture**) lua_newuserdata(L, sizeof(imVideoCapture*));
    *vc_p = vc;
    luaL_getmetatable(L, "imVideoCapture");
    lua_setmetatable(L, -2);
  }
}

/*****************************************************************************\
 im.VideoCaptureDeviceCount()
\*****************************************************************************/
static int imluaVideoCaptureDeviceCount (lua_State *L)
{
  lua_pushnumber(L, imVideoCaptureDeviceCount());
  return 1;
}

/*****************************************************************************\
 im.VideoCaptureDeviceDesc(device)
\*****************************************************************************/
static int imluaVideoCaptureDeviceDesc (lua_State *L)
{
  lua_pushstring(L, imVideoCaptureDeviceDesc(luaL_checkint(L, 1)));
  return 1;
}

/*****************************************************************************\
 im.VideoCaptureDeviceDesc(device)
\*****************************************************************************/
static int imluaVideoCaptureReloadDevices (lua_State *L)
{
  lua_pushnumber(L, imVideoCaptureReloadDevices());
  return 1;
}

/*****************************************************************************\
 im.VideoCaptureCreate()
\*****************************************************************************/
static int imluaVideoCaptureCreate (lua_State *L)
{
  imlua_pushvideocapture(L, imVideoCaptureCreate());
  return 1;
}

/*****************************************************************************\
 vc:Connect([device])
\*****************************************************************************/
static int imluaVideoCaptureConnect (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int device = luaL_optint(L, 2, -1);
  if (device == -1)
    lua_pushnumber(L, imVideoCaptureConnect(vc, device));
  else
    lua_pushboolean(L, imVideoCaptureConnect(vc, device));
  return 1;
}

/*****************************************************************************\
 vc:Disconnect()
\*****************************************************************************/
static int imluaVideoCaptureDisconnect (lua_State *L)
{
  imVideoCaptureDisconnect(imlua_checkvideocapture(L, 1));
  return 0;
}

/*****************************************************************************\
 vc:DialogCount()
\*****************************************************************************/
static int imluaVideoCaptureDialogCount (lua_State *L)
{
  lua_pushnumber(L, imVideoCaptureDialogCount(imlua_checkvideocapture(L, 1)));
  return 1;
}

/*****************************************************************************\
 vc:ShowDialog()
\*****************************************************************************/
static int imluaVideoCaptureShowDialog (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int dialog = luaL_checkint(L, 2);
  void *parent = lua_touserdata(L, 3); 

  lua_pushboolean(L, imVideoCaptureShowDialog(vc, dialog, parent));
  return 1;
}

/*****************************************************************************\
 vc:DialogDesc()
\*****************************************************************************/
static int imluaVideoCaptureDialogDesc (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int dialog = luaL_checkint(L, 2);

  lua_pushstring(L, imVideoCaptureDialogDesc(vc, dialog));
  return 1;
}

/*****************************************************************************\
 vc:FormatCount()
\*****************************************************************************/
static int imluaVideoCaptureFormatCount (lua_State *L)
{
  lua_pushnumber(L, imVideoCaptureFormatCount(imlua_checkvideocapture(L, 1)));
  return 1;
}

/*****************************************************************************\
 vc:GetFormat()
\*****************************************************************************/
static int imluaVideoCaptureGetFormat (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int format = luaL_checkint(L, 2);
  int width, height;
  char desc[10];

  lua_pushboolean(L, imVideoCaptureGetFormat(vc, format, &width, &height, desc));
  lua_pushnumber(L, width);
  lua_pushnumber(L, height);
  lua_pushstring(L, desc);

  return 4;
}

/*****************************************************************************\
 vc:GetImageSize()
\*****************************************************************************/
static int imluaVideoCaptureGetImageSize (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int width, height;

  imVideoCaptureGetImageSize(vc, &width, &height);
  lua_pushnumber(L, width);
  lua_pushnumber(L, height);

  return 2;
}

/*****************************************************************************\
 vc:SetImageSize()
\*****************************************************************************/
static int imluaVideoCaptureSetImageSize (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int width = luaL_checkint(L, 2);
  int height = luaL_checkint(L, 3);

  lua_pushboolean(L, imVideoCaptureSetImageSize(vc, width, height));

  return 1;
}

/*****************************************************************************\
 vc:SetInOut()
\*****************************************************************************/
static int imluaVideoCaptureSetInOut(lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int input = luaL_checkint(L, 2);
  int output = luaL_checkint(L, 3);
  int cross = luaL_checkint(L, 4);

  lua_pushboolean(L, imVideoCaptureSetInOut(vc, input, output, cross));

  return 1;
}

/*****************************************************************************\
 vc:SetFormat()
\*****************************************************************************/
static int imluaVideoCaptureSetFormat (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int format = luaL_optint(L, 2, -1);
  if (format == -1)
    lua_pushnumber(L, imVideoCaptureSetFormat(vc, format));
  else
    lua_pushboolean(L, imVideoCaptureSetFormat(vc, format));
  return 1;
}

/*****************************************************************************\
 vc:ResetAttribute(attrib, fauto)
\*****************************************************************************/
static int imluaVideoCaptureResetAttribute (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  const char *attrib = luaL_checkstring(L, 2);
  int fauto = lua_toboolean(L, 3);

  lua_pushboolean(L, imVideoCaptureResetAttribute(vc, attrib, fauto));
  return 1;
}

/*****************************************************************************\
 vc:SetAttribute(attrib, percent)
\*****************************************************************************/
static int imluaVideoCaptureSetAttribute (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  const char *attrib = luaL_checkstring(L, 2);
  float percent = (float) luaL_checknumber(L, 3);

  lua_pushboolean(L, imVideoCaptureSetAttribute(vc, attrib, percent));
  return 1;
}

/*****************************************************************************\
 vc:GetAttribute(attrib)
\*****************************************************************************/
static int imluaVideoCaptureGetAttribute (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  const char *attrib = luaL_checkstring(L, 2);
  float percent;

  lua_pushboolean(L, imVideoCaptureGetAttribute(vc, attrib, &percent));
  lua_pushnumber(L, percent);
  return 2;
}

/*****************************************************************************\
 vc:GetAttributeList()
\*****************************************************************************/
static int imluaVideoCaptureGetAttributeList (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int num_attrib;
  const char **attribs;
  int i;

  attribs = imVideoCaptureGetAttributeList(vc, &num_attrib);
  lua_newtable(L);
  for (i = 0; i < num_attrib; i++)
  {
    lua_pushstring(L, attribs[i]);
    lua_rawseti(L, -2, i + 1);
  }

  return 1;
}

/*****************************************************************************\
 vc:Frame(image)
\*****************************************************************************/
static int imluaVideoCaptureFrame (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  imImage *image = imlua_checkimage(L, 2);
  int timeout = luaL_checkint(L, 3);

  if (!(image->color_space == IM_RGB || image->color_space == IM_GRAY))
    luaL_argerror(L, 2, "image must be of RGB or Gray color spaces");
  imlua_checkdatatype(L, 2, image, IM_BYTE);

  lua_pushboolean(L, imVideoCaptureFrame(vc, image->data[0], image->color_space, timeout));

  return 1;
}

/*****************************************************************************\
 vc:OneFrame(image)
\*****************************************************************************/
static int imluaVideoCaptureOneFrame (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  imImage *image = imlua_checkimage(L, 2);

  if (!(image->color_space == IM_RGB || image->color_space == IM_GRAY))
    luaL_argerror(L, 2, "image must be of RGB or Gray color spaces");
  imlua_checkdatatype(L, 2, image, IM_BYTE);

  lua_pushboolean(L, imVideoCaptureOneFrame(vc, image->data[0], image->color_space));

  return 1;
}

/*****************************************************************************\
 vc:Live(image)
\*****************************************************************************/
static int imluaVideoCaptureLive (lua_State *L)
{
  imVideoCapture *vc = imlua_checkvideocapture(L, 1);
  int live = luaL_optint(L, 2, -1);
  if (live == -1)
    lua_pushnumber(L, imVideoCaptureLive(vc, live));
  else
    lua_pushboolean(L, imVideoCaptureLive(vc, live));

  return 1;
}

/*****************************************************************************\
 vc:Destroy()
\*****************************************************************************/
static int imluaVideoCaptureDestroy (lua_State *L)
{
  imVideoCapture **vc_p = imlua_rawcheckvideocapture(L, 1);
  if (!(*vc_p))
    luaL_argerror(L, 1, "destroyed imVideoCapture");

  imVideoCaptureDestroy(*vc_p);
  *vc_p = NULL;  /* mark as destroyed */

  return 0;
}

/*****************************************************************************\
 gc
\*****************************************************************************/
static int imluaVideoCapture_gc (lua_State *L)
{
  imVideoCapture **vc_p = (imVideoCapture **)lua_touserdata(L, 1);
  if (*vc_p)
  {
    imVideoCaptureDestroy(*vc_p);
    *vc_p = NULL;  /* mark as destroyed */
  }
  return 0;
}

/*****************************************************************************\
 tostring
\*****************************************************************************/
static int imluaVideoCapture_tostring (lua_State *L)
{
  imVideoCapture **vc_p = (imVideoCapture **)lua_touserdata(L, 1);
  lua_pushfstring(L, "imVideoCapture (%p)%s", vc_p, (*vc_p)? "": "-destroyed");
  return 1;
}

static const luaL_reg imcapture_lib[] = {
  {"VideoCaptureDeviceCount", imluaVideoCaptureDeviceCount},
  {"VideoCaptureDeviceDesc", imluaVideoCaptureDeviceDesc},
  {"VideoCaptureReloadDevices", imluaVideoCaptureReloadDevices},
  {"VideoCaptureCreate", imluaVideoCaptureCreate},
  {"VideoCaptureDestroy", imluaVideoCaptureDestroy},
  {NULL, NULL}
};

static const luaL_reg imcapture_metalib[] = {
  {"Destroy", imluaVideoCaptureDestroy},
  {"Connect", imluaVideoCaptureConnect},
  {"Disconnect", imluaVideoCaptureDisconnect},
  {"DialogCount", imluaVideoCaptureDialogCount},
  {"ShowDialog", imluaVideoCaptureShowDialog},
  {"DialogDesc", imluaVideoCaptureDialogDesc},
  {"FormatCount", imluaVideoCaptureFormatCount},
  {"GetFormat", imluaVideoCaptureGetFormat},
  {"SetFormat", imluaVideoCaptureSetFormat},
  {"GetImageSize", imluaVideoCaptureGetImageSize},
  {"SetImageSize", imluaVideoCaptureSetImageSize},
  {"SetInOut", imluaVideoCaptureSetInOut},
  {"ResetAttribute", imluaVideoCaptureResetAttribute},
  {"GetAttribute", imluaVideoCaptureGetAttribute},
  {"SetAttribute", imluaVideoCaptureSetAttribute},
  {"GetAttributeList", imluaVideoCaptureGetAttributeList},
  {"OneFrame", imluaVideoCaptureOneFrame},
  {"Frame", imluaVideoCaptureFrame},
  {"Live", imluaVideoCaptureLive},

  {"__gc", imluaVideoCapture_gc},
  {"__tostring", imluaVideoCapture_tostring},

  {NULL, NULL}
};

static void createmeta (lua_State *L) 
{
  /* Object Oriented Access */
  luaL_newmetatable(L, "imVideoCapture");  /* create new metatable for imVideoCapture handle */
  lua_pushliteral(L, "__index");
  lua_pushvalue(L, -2);  /* push metatable */
  lua_rawset(L, -3);  /* metatable.__index = metatable */
  luaL_register(L, NULL, imcapture_metalib);  /* register methods */
  lua_pop(L, 1);  /* removes the metatable from the top of the stack */
}

int imlua_open_capture(lua_State *L)
{
  createmeta(L);
  luaL_register(L, "im", imcapture_lib);  /* leave "im" table at the top of the stack */
  return 1;
}

int luaopen_imlua_capture(lua_State *L)
{
  return imlua_open_capture(L);
}

int luaopen_imlua_capture51(lua_State *L)
{
  return imlua_open_capture(L);
}