Пример #1
0
/*****************************************************************************\
 file:GetAttribute(attrib)
\*****************************************************************************/
static int imluaFileGetAttribute (lua_State *L)
{
  int data_type;
  int i, count;
  const void *data;
  int as_string = 0;

  imFile *ifile = imlua_checkfile(L, 1);
  const char *attrib = luaL_checkstring(L, 2);

  data = imFileGetAttribute(ifile, attrib, &data_type, &count);
  if (!data)
  {
    lua_pushnil(L);
    return 1;
  }

  if (data_type == IM_BYTE && lua_isboolean(L, 3))
    as_string = lua_toboolean(L, 3);

  if (!as_string)
    lua_newtable(L);

  switch (data_type)
  {
  case IM_BYTE:
    {
      if (as_string)
      {
        lua_pushstring(L, (const char*)data);
      }
      else
      {
        imbyte *data_byte = (imbyte*) data;
        for (i = 0; i < count; i++, data_byte++)
        {
          lua_pushnumber(L, *data_byte);
          lua_rawseti(L, -2, i+1);
        }
      }
    }
    break;

  case IM_SHORT:
    {
      short *data_short = (short*) data;
      for (i = 0; i < count; i++, data_short += 2)
      {
        lua_pushnumber(L, *data_short);
        lua_rawseti(L, -2, i+1);
      }
    }
    break;

  case IM_USHORT:
    {
      imushort *data_ushort = (imushort*) data;
      for (i = 0; i < count; i++, data_ushort += 2)
      {
        lua_pushnumber(L, *data_ushort);
        lua_rawseti(L, -2, i+1);
      }
    }
    break;

  case IM_INT:
    {
      int *data_int = (int*) data;
      for (i = 0; i < count; i++, data_int++)
      {
        lua_pushnumber(L, *data_int);
        lua_rawseti(L, -2, i+1);
      }
    }
    break;

  case IM_FLOAT:
    {
      float *data_float = (float*) data;
      for (i = 0; i < count; i++, data_float++)
      {
        lua_pushnumber(L, *data_float);
        lua_rawseti(L, -2, i+1);
      }
    }
    break;

  case IM_CFLOAT:
    {
      float *data_float = (float*) data;
      for (i = 0; i < count; i++, data_float += 2)
      {
        imlua_newarrayfloat(L, data_float, 2, 1);
        lua_rawseti(L, -2, i+1);
      }        
    }
    break;

  case IM_DOUBLE:
    {
      double *data_double = (double*) data;
      for (i = 0; i < count; i++, data_double++)
      {
        lua_pushnumber(L, *data_double);
        lua_rawseti(L, -2, i+1);
      }
    }
    break;

  case IM_CDOUBLE:
    {
      double *data_double = (double*) data;
      for (i = 0; i < count; i++, data_double += 2)
      {
        imlua_newarraydouble(L, data_double, 2, 1);
        lua_rawseti(L, -2, i+1);
      }        
    }
    break;
  }

  lua_pushinteger(L, data_type);

  return 2;
}
Пример #2
0
/*****************************************************************************\
 image row indexing
\*****************************************************************************/
static int imluaImageRow_index (lua_State *L)
{
  int index;
  imluaImageRow *imagerow = imlua_checkimagerow(L, 1);
  imImage *image = imagerow->image;
  int channel = imagerow->channel;
  int row = imagerow->row;
  int column = luaL_checkint(L, 2);
  void* channel_buffer = image->data[channel];

  if (column < 0 || column >= imagerow->image->width)
    luaL_argerror(L, 2, "invalid column, out of bounds");

  index = row * image->width + column;

  switch (image->data_type)
  {
  case IM_BYTE:
    {
      imbyte *bdata = (imbyte*) channel_buffer;
      lua_pushnumber(L, (lua_Number) bdata[index]);
    }
    break;

  case IM_SHORT:
    {
      short *sdata = (short*) channel_buffer;
      lua_pushnumber(L, (lua_Number) sdata[index]);
    }
    break;

  case IM_USHORT:
    {
      imushort *udata = (imushort*) channel_buffer;
      lua_pushnumber(L, (lua_Number) udata[index]);
    }
    break;

  case IM_INT:
    {
      int *idata = (int*) channel_buffer;
      lua_pushnumber(L, (lua_Number) idata[index]);
    }
    break;

  case IM_FLOAT:
    {
      float *fdata = (float*) channel_buffer;
      lua_pushnumber(L, (lua_Number) fdata[index]);
    }
    break;
    
  case IM_CFLOAT:
    {
      float *cdata = (float*) channel_buffer;
      imlua_newarrayfloat(L, cdata + (2*index), 2, 1);
    }
    break;
  }

  return 1;
}