/*****************************************************************************\ file:SetAttribute(attrib, data_type, data) \*****************************************************************************/ static int imluaFileSetAttribute (lua_State *L) { int i, count = 0; void *data = NULL; imFile *ifile = imlua_checkfile(L, 1); const char *attrib = luaL_checkstring(L, 2); int data_type = luaL_checkinteger(L, 3); if (!lua_isnil(L, 4)) { if (!lua_isstring(L, 4)) { luaL_checktype(L, 4, LUA_TTABLE); count = imlua_getn(L, 4); data = malloc(imDataTypeSize(data_type) * count); } else if (data_type != IM_BYTE) luaL_argerror(L, 4, "if value is string, then data type must be byte"); switch (data_type) { case IM_BYTE: { if (lua_isstring(L, 4)) { const char* str = lua_tostring(L, 4); count = (int)strlen(str)+1; data = malloc(imDataTypeSize(data_type) * count); memcpy(data, str, count); } else { imbyte *d = (imbyte*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (imbyte) luaL_checkinteger(L, -1); lua_pop(L, 1); } } } break; case IM_SHORT: { short *d = (short*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (short) luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_USHORT: { imushort *d = (imushort*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (imushort) luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_INT: { int *d = (int*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = luaL_checkinteger(L, -1); lua_pop(L, 1); } } break; case IM_FLOAT: { float *d = (float*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (float) luaL_checknumber(L, -1); lua_pop(L, 1); } } break; case IM_CFLOAT: { float *data_float = (float*) data; for (i = 0; i < count; i++) { int two; float *value = imlua_toarrayfloat(L, -1, &two, 1); if (two != 2) { free(value); luaL_argerror(L, 4, "invalid value"); } data_float[i] = value[0]; data_float[i+1] = value[1]; free(value); lua_pop(L, 1); } } break; case IM_DOUBLE: { double *d = (double*) data; for (i = 0; i < count; i++) { lua_rawgeti(L, 4, i+1); d[i] = (double)luaL_checknumber(L, -1); lua_pop(L, 1); } } break; case IM_CDOUBLE: { double *data_double = (double*) data; for (i = 0; i < count; i++) { int two; double *value = imlua_toarraydouble(L, -1, &two, 1); if (two != 2) { free(value); luaL_argerror(L, 4, "invalid value"); } data_double[i] = value[0]; data_double[i+1] = value[1]; free(value); lua_pop(L, 1); } } break; } } imFileSetAttribute(ifile, attrib, data_type, count, data); return 0; }
/*****************************************************************************\ image row new index \*****************************************************************************/ static int imluaImageRow_newindex (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: { lua_Number value = luaL_checknumber(L, 3); imbyte *bdata = (imbyte*) channel_buffer; bdata[index] = (imbyte) value; } break; case IM_SHORT: { lua_Number value = luaL_checknumber(L, 3); short *sdata = (short*) channel_buffer; sdata[index] = (short) value; } break; case IM_USHORT: { lua_Number value = luaL_checknumber(L, 3); imushort *udata = (imushort*) channel_buffer; udata[index] = (imushort) value; } break; case IM_INT: { lua_Number value = luaL_checknumber(L, 3); int *idata = (int*) channel_buffer; idata[index] = (int) value; } break; case IM_FLOAT: { lua_Number value = luaL_checknumber(L, 3); float *fdata = (float*) channel_buffer; fdata[index] = (float) value; } break; case IM_CFLOAT: { int count; float *cdata = (float*) channel_buffer; float *value = imlua_toarrayfloat(L, 3, &count, 1); if (count != 2) { free(value); luaL_argerror(L, 3, "invalid value"); } cdata[2*index] = value[0]; cdata[2*index+1] = value[1]; free(value); } break; } return 0; }