Search code examples
c#c++cframecreation

Reading C++ code CreateFrame function (from C# prespective)


// Create test video frame
void CreateFrame(char * buffer, int w, int h, int bytespan)
{
  int wxh = w * h;
  static float seed = 1.0;
  for (int i = 0; i < h; i ++)
  {
    char* line = buffer + i * bytespan;
    for (int j = 0; j < w; j ++)
    {
      // RGB
      line[0] = 255 * sin(((float)i / wxh * seed) * 3.14);
      line[1] = 255 * cos(((float)j / wxh * seed) * 3.14);
      line[2] = 255 * sin(((float)(i + j) / wxh * seed) * 3.14);
      line += 3;
    }
  }
  seed = seed + 2.2;
}

can any one please tall me what is line += 3; for?

and how to create such function analog in C#?


Solution

  • In C/C++, the value line in line is actually a memory address of an array, and line[1] actually represents the value at the address of the variable line plus a 1 item offset. (If the type of the items in line is an int, then it means the address of line plus four bytes; since it is a char, it means the address of line plus one byte.)

    So, line += 3 means that line[1] is now equivalent to [old "line" value][4]. The coder could have written the code as:

    for (int j = 0; j < w; j ++)
    {
      // RGB
      line[(3 * j)] = 255 * sin(((float)i / wxh * seed) * 3.14);
      line[(3 * j) + 1] = 255 * cos(((float)j / wxh * seed) * 3.14);
      line[(3 * j) + 2] = 255 * sin(((float)(i + j) / wxh * seed) * 3.14);
    }