views:

61

answers:

5
// 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#?

+5  A: 

line += 3 increments the pointer line by 3 bytes, so that it points to the next pixel. line here is a pointer to a 3-byte pixel, so it really should be called something else, like pPixel.

Dima
+3  A: 

Line is a pointer to a position within buffer. Incrementing line advances the processing down the buffer.

A C# analog might be:

static float seed = 1.0f;
static void CreateFrame(byte[] buffer, int w, int h, int bytespan)
{
  int wxh = w * h;
  for (int i = 0; i < h; i ++)
  {
    int line = i * bytespan;
    for (int j = 0; j < w; j ++)
    {
      // RGB
      buffer[line + 0] = (byte)(255 * Math.Sin(((float)i / wxh * seed) * 3.14));
      buffer[line + 1] = (byte)(255 * Math.Cos(((float)j / wxh * seed) * 3.14));
      buffer[line + 2] = (byte)(255 * Math.Sin(((float)(i + j) / wxh * seed) * 3.14));
      line += 3;
    }
  }
  seed = seed + 2.2f;
}
kbrimington
+1  A: 

This is pointer arithmetic. Since you are dealing with 3 elements of the array in one go you will need to update the pointer suitably otherwise you will be reading the same location twice and of course, erroneously.

dirkgently
+1  A: 

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);
}
palswim
+1  A: 

You would replace the pointer by a byte array and index into it by an integer as follows:

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

I just left the variable name as line, even if from what I understand, it is not really a line.

Frank