Modules-old/cutil/lua_cutil.cpp
//Label-specific string conversion function
//Bin each label into 6 class 
//And pack two pixel into one byte
Label-specific string conversion function
Bin each label into 6 class
And pack two pixel into one byte
Label - specific string conversion function Bin each label into 6 class And pack two pixel into one byte
static int lua_label2string_double(lua_State *L) {
  uint8_t *data = (uint8_t *) lua_touserdata(L, 1);
  if ((data == NULL) || !lua_islightuserdata(L, 1)) {
    return luaL_error(L, "Input image not light user data");
  }
  int arr_size = luaL_checkint(L, 2);
  std::string dtype(luaL_checkstring(L, 3));
  std::string name(luaL_checkstring(L, 4));
  std::map<std::string, int>::iterator idataTypeMap = dataTypeMap.find(dtype);
  if (idataTypeMap == dataTypeMap.end()) {
    return luaL_error(L, "unkown dtype: %s", dtype.c_str());
  }
  int nbytes = idataTypeMap->second;
  int size = arr_size * nbytes/2;
  char cdata[size + 1];
  int ind = 0;
  int cind = 0;
  while (ind < size*2) {
    char pixel1=label_color_pack_lut[data[ind++]];
    char pixel2=label_color_pack_lut[data[ind++]];
    cdata[cind++] = '0' + pixel1 * 6 + pixel2;
  }
  cdata[size] = '\0';
  lua_createtable(L, 0, 5);
  lua_pushstring(L, "name");
  lua_pushstring(L, name.c_str());
  lua_settable(L, -3);
  lua_pushstring(L, "size");
  lua_pushnumber(L, size);
  lua_settable(L, -3);
  lua_pushstring(L, "dtype");
  lua_createtable(L, 0, 2);
  lua_pushstring(L, "name");
  lua_pushstring(L, dtype.c_str());
  lua_settable(L, -3);
  lua_pushstring(L, "nbytes");
  lua_pushnumber(L, nbytes);
  lua_settable(L, -3);
  lua_settable(L, -3);
  lua_pushstring(L, "data");
  lua_pushstring(L, cdata);
  lua_settable(L, -3);
  return 1;
}
static int lua_label2string_double ( lua_State * L ) { uint8_t * data = ( uint8_t * ) lua_touserdata ( L , 1 ) ; if ( ( data == NULL ) || ! lua_islightuserdata ( L , 1 ) ) { return luaL_error ( L , " " ) ; } int arr_size = luaL_checkint ( L , 2 ) ; std :: string dtype ( luaL_checkstring ( L , 3 ) ) ; std :: string name ( luaL_checkstring ( L , 4 ) ) ; std :: map < std :: string , int > :: iterator idataTypeMap = dataTypeMap . find ( dtype ) ; if ( idataTypeMap == dataTypeMap . end ( ) ) { return luaL_error ( L , " " , dtype . c_str ( ) ) ; } int nbytes = idataTypeMap -> second ; int size = arr_size * nbytes / 2 ; char cdata [ size + 1 ] ; int ind = 0 ; int cind = 0 ; while ( ind < size * 2 ) { char pixel1 = label_color_pack_lut [ data [ ind ++ ] ] ; char pixel2 = label_color_pack_lut [ data [ ind ++ ] ] ; cdata [ cind ++ ] = ' ' + pixel1 * 6 + pixel2 ; } cdata [ size ] = ' \0 ' ; lua_createtable ( L , 0 , 5 ) ; lua_pushstring ( L , " " ) ; lua_pushstring ( L , name . c_str ( ) ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_pushnumber ( L , size ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_createtable ( L , 0 , 2 ) ; lua_pushstring ( L , " " ) ; lua_pushstring ( L , dtype . c_str ( ) ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_pushnumber ( L , nbytes ) ; lua_settable ( L , -3 ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_pushstring ( L , cdata ) ; lua_settable ( L , -3 ) ; return 1 ; }
Label-specific string conversion function
Bin each label into 6 class
And pack two pixel into one byte
Label - specific string conversion function Bin each label into 6 class And pack two pixel into one byte
//bin label data (0-31) to 6 class (0-5) //encode two pixels into a single byte //Now we use ascii #48 ~ #122 (total 75 characters) // create lua table