Tag: c++

  • Accessing data of leptonica PIX data

    This is mainly as a reference

    
    /**
     * Get Pixel value at given  point
     */
    l_uint32 pixAtGet(PIX* pix, int_t x, int_t y)
    {
        l_int32 wpl    = pixGetWpl(pix);
        l_uint32* data = pixGetData(pix);
        l_uint32* line = data + y * wpl;
        l_uint32 value = GET_DATA_BYTE(line, x);
        return value;
    }
    
    

    To set a pixel value we can use this

    /**
     * Set Pixel value at given  point
     */
    void pixAtSet(PIX* pix, int_t x, int_t y, byte_t value)
    {
    	l_int32 wpl     = pixGetWpl(pix);
    	l_uint32* data  = pixGetData(pix);
    	l_uint32* line  = data + y * wpl;
    	SET_DATA_BYTE(line, x, value);
    }
    
  • Tokenizing/splitting string in c++

    This method uses strtok to tokeninze our string given a specific delimeter, results of that are put into supplied vector. There are few other ways we can do this but this one is straight forward.

    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    void split(vector& out, const string& in, const string& delim)
    {
      char* lc = (char*) malloc(in.size());
      strcpy(lc, in.c_str());
      strtok(lc, delim.c_str());
      while (lc)
        {
          string s = lc;
          out.push_back(s);
          lc = strtok(NULL, delim.c_str());
        }
      free(lc);
    }
    
    int main(int argc, char* args[])
    {
      string str = "apple,organge,cherry";
      vector o1;
      split(o1, str, ",");
    
      for (int i = 0; i < o1.size(); ++i)
      {
         cout << "token = " << o1[i] <
    

    Results

    Supplied string : apple,organge,cherry
    Delemeter : ","
    Output

    • apple
    • organge
    • cherry