gpt4 book ai didi

c - 在 C 中翻转 PPM 图像

转载 作者:行者123 更新时间:2023-11-30 15:21:48 26 4
gpt4 key购买 nike

我早些时候向大家提出了一个问题,这只是一个简单的错误,所以我希望看到这个问题的人会是一样的。

这是 C 语言

我必须水平翻转 PPM 图像,但我当前的代码要么绘制段错误,要么实际上没有翻转任何内容。

我使用的绘制段错误的代码是:

int a, b, x, y;
x = 3 * myPic->rows;
y = 3 * myPic->cols;
for(a = 0; a < (y / 2); a++) {
for(b = 0; b < x; b++) {
Pixel temp = myPic->pixels[a][b];
myPic->pixels[a][b] = myPic->pixels[y - a - 1][b];
myPic->pixels[y - a - 1][b] = temp;
}
}
return myPic;

}

不返回任何更改的代码是:

int a, b, x, y;
for(a = 0; a < myPic->rows; a++) {
for(b = 0; b < myPic->cols; b++) {
Pixel temp = myPic->pixels[a][b];
myPic->pixels[a][b] = myPic->pixels[myPic->cols - a - 1][b];
myPic->pixels[myPic->cols - a - 1][b] = temp;
}
}
return myPic;

因为 PPM 图像具有 RGB 值,所以我假设行和列值应乘以三。我认为一直穿过会导致它回到原来的状态,所以我将宽度(列)除以二。我被困住了,我希望这是一个小错误,有人可以帮忙吗?

最佳答案

你的第一个代码很糟糕,它访问数组超出了它的边界。您的第二个代码更好,尽管它翻转图像然后将其重新翻转回来。

简单来说,行数是图片高度,列数是图片宽度,二维数组中的第一个索引是行选择(高度索引),第二个索引是列选择(宽度指数)。水平翻转从左到右交换像素。垂直翻转从上到下交换像素。

这样,这应该是水平翻转

int row, col;
for(row = 0; row < myPic->rows; row++) {
for(col = 0; col < myPic->cols / 2 ; col++) { /*notice the division with 2*/
Pixel temp = myPic->pixels[row][col];
myPic->pixels[row][col] = myPic->pixels[row][myPic->cols - col -1];
myPic->pixels[row][myPic->cols - col -1] = temp;
}
}
return myPic;

修改内存中的图像后,需要保存它,或者用您正在使用的图形库重新绘制修改后的图像。

关于c - 在 C 中翻转 PPM 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29475398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com