阅读 C++代码 CreateFrame 函数(从 C# 角度来看)

发布于 2024-09-15 10:42:38 字数 599 浏览 7 评论 0原文

// 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;
}

谁能告诉我 line += 3; 的用途是什么?

以及如何在 C# 中创建此类函数模拟?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小苏打饼 2024-09-22 10:42:38

line += 3 将指针 line 增加 3 个字节,使其指向下一个像素。这里的 line 是一个指向 3 字节像素的指针,因此它确实应该被称为其他名称,例如 pPixel

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.

只是在用心讲痛 2024-09-22 10:42:38

Line 是指向缓冲区内位置的指针。递增行会推进缓冲区中的处理。

AC# 模拟可能是:

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;
}

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;
}
我是有多爱你 2024-09-22 10:42:38

在C/C++中,line中的值line实际上是一个数组的内存地址,而line[1]实际上代表的是变量地址处的值>line 加上 1 项偏移量。 (如果line中的项的类型是int,那么它意味着line的地址加上四个字节;因为它是一个< code>char,表示line的地址加一个字节。)

因此,line += 3表示line[1] 现在相当于 [旧“行”值][4]。编码员可以将代码编写为:

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);
}

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);
}
南笙 2024-09-22 10:42:38

这就是指针算术。由于您一次性处理数组的 3 个元素,因此您需要适当更新指针,否则您将读取同一位置两次,当然,这是错误的。

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.

月寒剑心 2024-09-22 10:42:38

您可以用字节数组替换指针,并用整数对其进行索引,如下所示:

// 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;
}

我只是将变量名称保留为 line,即使根据我的理解,它实际上并不是一行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文