如何在循环中开始新的迭代?
How to start a new iteration within a loop?
在下面的 SystemC
SC_THREAD
中,我正在尝试执行以下操作: image_buffer
是一个 3 行 x 720 列的矩阵,其中填充了从端口传入的字节p_in
。当 image_buffer
有 2 行并且前 5 个字节已填充时,它必须开始将字节复制到另一个名为 img
的矩阵中,其大小为 576 行 x 720 列。只要信号 h_in
为 true
,该字节就会被保存。 img
完成后,将生成 png 格式的图像。我编写的代码存在的问题是 i
和 j
分别始终为 2 和 4,只有第一行的 2 行和第三行的 4 个像素保存在图像中。你能给我一个关于如何实现这个的提示吗?
void FILTER::filtering(){
while(1){
while(!h_in) wait(h_in.posedge_event());
for(i=0; i<3; i++){
for(j=0; j<720; j++){
image_buffer.pixel[i*image_buffer.width+j] = p_in;
wait();
while(!h_in) wait();
//Begin of the block code I'm trying to test with
if ( (i > 1) && (j > 3) ){
for(int bi=0; bi<576; bi++){
for(int bj=0; bj<720; bj++){
if (bi < 3){
img.pixel[bi*img.width+bj] = image_buffer.pixel[bi*image_buffer.width+bj];
}
else{
img.pixel[bi*img.width+bj] = image_buffer.pixel[(bi%3)*image_buffer.width+bj];
}
cout << "i: " << i << " j: " << j << endl;
if ( (bi == 575)&&(bj == 719) ){
image_write( &img, "test.png" );
exit(1);
}
}
}
}
wait();
//End of the block code I'm trying to test with
}
}
}
}
更新
我修改了代码如下,但是当我这样做时,新的计数器 bi
和 bj
总是 0
:
while(1){
while(!h_in) wait(h_in.posedge_event());
k = 0;
for(i=0; i<3; i++){
for(j=0; j<720; j++){
image_buffer.pixel[i*image_buffer.width+j] = p_in;
wait();
while(!h_in) wait();
if( (i == 2 )&&(j > 4) ){
for(bi=i-2; bi<576; i++){
for(bj=j-5; bj<720; j++){
cout << "bi: " << bi << " bj: " << bj << endl;
img.pixel[bi*img.width+bj] = image_buffer.pixel[bi*image_buffer.width+bj];
}
}
}
}
}
}
如果我对你的问题的理解正确,你不想循环你要复制到的第二个数组。您需要跟踪第二组基于外循环递增的索引变量。
int ii, jj
for i = 0 to n
for j = 0 to m
if i > v and j > u
img[ii,jj] = val
++ii, ++jj
要执行单线程解决方案,您将遍历最终图像的尺寸,并跟踪 (row,col)
和计算出的 (bufrow,bufcol)
。您可能不想为此使用 for 循环,因为您将拥有可以增加图像位置和缓冲区位置的迭代。每次迭代都可以具有以下条件之一:
- 缓冲区最初正在填充 -
(bufrow,bufcol)
增量,
(row,col)
没有
- 缓冲区正在填充并复制到图像 -
h
是真的 && (bufrow,bufcol)
在某个时候超过了 (2,5)
(粘性 buffer_is_ready 指标)
- 缓冲区未填充 (
!h
),
但正在复制到图像 - 递增 (row,col)
您可能需要一个 while(row<576&&col<720)
作为您唯一的循环(在 while(1) 内)。在每次迭代中,手动处理 (row,col) 和 (bufrow,bufcol) 的增量。不要忘记缓冲区环绕:
if (h) {
bufrow = (bufcol == 719 && bufrow == 2) ? 0
(bufcol == 719) ? bufrow+1 : bufrow;
bufcol = (bufcol == 719) ? 0 : bufcol+1;
}
它还可以简化代码以使用双线程解决方案。使用两个线程还可以将缓冲区填充与任何计算和复制到最终图像分离。
在下面的 SystemC
SC_THREAD
中,我正在尝试执行以下操作: image_buffer
是一个 3 行 x 720 列的矩阵,其中填充了从端口传入的字节p_in
。当 image_buffer
有 2 行并且前 5 个字节已填充时,它必须开始将字节复制到另一个名为 img
的矩阵中,其大小为 576 行 x 720 列。只要信号 h_in
为 true
,该字节就会被保存。 img
完成后,将生成 png 格式的图像。我编写的代码存在的问题是 i
和 j
分别始终为 2 和 4,只有第一行的 2 行和第三行的 4 个像素保存在图像中。你能给我一个关于如何实现这个的提示吗?
void FILTER::filtering(){
while(1){
while(!h_in) wait(h_in.posedge_event());
for(i=0; i<3; i++){
for(j=0; j<720; j++){
image_buffer.pixel[i*image_buffer.width+j] = p_in;
wait();
while(!h_in) wait();
//Begin of the block code I'm trying to test with
if ( (i > 1) && (j > 3) ){
for(int bi=0; bi<576; bi++){
for(int bj=0; bj<720; bj++){
if (bi < 3){
img.pixel[bi*img.width+bj] = image_buffer.pixel[bi*image_buffer.width+bj];
}
else{
img.pixel[bi*img.width+bj] = image_buffer.pixel[(bi%3)*image_buffer.width+bj];
}
cout << "i: " << i << " j: " << j << endl;
if ( (bi == 575)&&(bj == 719) ){
image_write( &img, "test.png" );
exit(1);
}
}
}
}
wait();
//End of the block code I'm trying to test with
}
}
}
}
更新
我修改了代码如下,但是当我这样做时,新的计数器 bi
和 bj
总是 0
:
while(1){
while(!h_in) wait(h_in.posedge_event());
k = 0;
for(i=0; i<3; i++){
for(j=0; j<720; j++){
image_buffer.pixel[i*image_buffer.width+j] = p_in;
wait();
while(!h_in) wait();
if( (i == 2 )&&(j > 4) ){
for(bi=i-2; bi<576; i++){
for(bj=j-5; bj<720; j++){
cout << "bi: " << bi << " bj: " << bj << endl;
img.pixel[bi*img.width+bj] = image_buffer.pixel[bi*image_buffer.width+bj];
}
}
}
}
}
}
如果我对你的问题的理解正确,你不想循环你要复制到的第二个数组。您需要跟踪第二组基于外循环递增的索引变量。
int ii, jj
for i = 0 to n
for j = 0 to m
if i > v and j > u
img[ii,jj] = val
++ii, ++jj
要执行单线程解决方案,您将遍历最终图像的尺寸,并跟踪 (row,col)
和计算出的 (bufrow,bufcol)
。您可能不想为此使用 for 循环,因为您将拥有可以增加图像位置和缓冲区位置的迭代。每次迭代都可以具有以下条件之一:
- 缓冲区最初正在填充 -
(bufrow,bufcol)
增量,(row,col)
没有 - 缓冲区正在填充并复制到图像 -
h
是真的 &&(bufrow,bufcol)
在某个时候超过了 (2,5) (粘性 buffer_is_ready 指标) - 缓冲区未填充 (
!h
), 但正在复制到图像 - 递增(row,col)
您可能需要一个 while(row<576&&col<720)
作为您唯一的循环(在 while(1) 内)。在每次迭代中,手动处理 (row,col) 和 (bufrow,bufcol) 的增量。不要忘记缓冲区环绕:
if (h) {
bufrow = (bufcol == 719 && bufrow == 2) ? 0
(bufcol == 719) ? bufrow+1 : bufrow;
bufcol = (bufcol == 719) ? 0 : bufcol+1;
}
它还可以简化代码以使用双线程解决方案。使用两个线程还可以将缓冲区填充与任何计算和复制到最终图像分离。