阅读 C++代码 CreateFrame 函数(从 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;
}
谁能告诉我 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
line += 3
将指针line
增加 3 个字节,使其指向下一个像素。这里的line
是一个指向 3 字节像素的指针,因此它确实应该被称为其他名称,例如pPixel
。line += 3
increments the pointerline
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, likepPixel
.Line 是指向缓冲区内位置的指针。递增行会推进缓冲区中的处理。
AC# 模拟可能是:
Line is a pointer to a position within buffer. Incrementing line advances the processing down the buffer.
A C# analog might be:
在C/C++中,line中的值
line
实际上是一个数组的内存地址,而line[1]
实际上代表的是变量地址处的值>line
加上 1 项偏移量。 (如果line
中的项的类型是int
,那么它意味着line
的地址加上四个字节;因为它是一个< code>char,表示line
的地址加一个字节。)因此,
line += 3
表示line[1] 现在相当于
[旧“行”值][4]
。编码员可以将代码编写为:In C/C++, the value
line
in line is actually a memory address of an array, andline[1]
actually represents the value at the address of the variableline
plus a 1 item offset. (If the type of the items inline
is anint
, then it means the address ofline
plus four bytes; since it is achar
, it means the address ofline
plus one byte.)So,
line += 3
means thatline[1]
is now equivalent to[old "line" value][4]
. The coder could have written the code as:这就是指针算术。由于您一次性处理数组的 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.
您可以用字节数组替换指针,并用整数对其进行索引,如下所示:
我只是将变量名称保留为
line
,即使根据我的理解,它实际上并不是一行。You would replace the pointer by a byte array and index into it by an integer as follows:
I just left the variable name as
line
, even if from what I understand, it is not really a line.