Modules-old/cutil/lua_cutil.cpp
//Label-specific string conversion function
//Bin each label into 6 class 
//And run Run-length encoding
Label-specific string conversion function
Bin each label into 6 class
And run Run-length encoding
Label - specific string conversion function Bin each label into 6 class And run Run - length encoding
static int lua_label2string_rle(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;
  char cdata[size + 1];
  int ind = 0;
  int cind = 0;
  int last_data=label_color_pack_lut[data[0]];
  int current_size=1;
  int total_byte = 0;
  ind++;
  while (ind < size) {
    int current_data = label_color_pack_lut[data[ind]];
    if (ind==size-1) {
      cdata[cind++] = '0' + last_data;
      cdata[cind++] = '0' + current_size;
      total_byte+=2;
      ind++;
    }else{
      if ((current_data==last_data) && (current_size<75)){
        current_size++;
        ind++;
      }else{
	if (current_size>1){
          cdata[cind++] = '0' + last_data;
          cdata[cind++] = '0' + current_size;
          total_byte+=2;
	}else{
          cdata[cind++] = '6' + last_data;
          total_byte+=1;
	}
        last_data = current_data;
        current_size = 1;
        ind++;
      }
    }
  }
  cdata[cind] = '\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, arr_size);
  lua_settable(L, -3);
  lua_pushstring(L, "bsize");
  lua_pushnumber(L, total_byte);
  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_rle ( 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 ; char cdata [ size + 1 ] ; int ind = 0 ; int cind = 0 ; int last_data = label_color_pack_lut [ data [ 0 ] ] ; int current_size = 1 ; int total_byte = 0 ; ind ++ ; while ( ind < size ) { int current_data = label_color_pack_lut [ data [ ind ] ] ; if ( ind == size - 1 ) { cdata [ cind ++ ] = ' ' + last_data ; cdata [ cind ++ ] = ' ' + current_size ; total_byte += 2 ; ind ++ ; } else { if ( ( current_data == last_data ) && ( current_size < 75 ) ) { current_size ++ ; ind ++ ; } else { if ( current_size > 1 ) { cdata [ cind ++ ] = ' ' + last_data ; cdata [ cind ++ ] = ' ' + current_size ; total_byte += 2 ; } else { cdata [ cind ++ ] = ' ' + last_data ; total_byte += 1 ; } last_data = current_data ; current_size = 1 ; ind ++ ; } } } cdata [ cind ] = ' \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 , arr_size ) ; lua_settable ( L , -3 ) ; lua_pushstring ( L , " " ) ; lua_pushnumber ( L , total_byte ) ; 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 run Run-length encoding
Label - specific string conversion function Bin each label into 6 class And run Run - length encoding
//Multiple data : "012345" //	printf("C%dS%d, ",last_data,current_size); //Multiple data : "012345" //Single data : "6789:;" // create lua table