翻转 ppm 图像C++麻烦
C++ trouble flipping a ppm image
我成功地弄清楚了如何从 ppm 文件中读取 rgb 值,现在我需要修改它。 但是,当我翻转图像(readImg 中的第二组 for 循环(时,它会显示完全相同的图像。这是否与数组是无符号字符有关,还是仍在输入流中。任何帮助都会很棒。
#include "finalproj.h"
int readImg(std::string header, unsigned char r[][HEIGHT], unsigned char g[][HEIGHT], unsigned char b[][HEIGHT])
{
int wid, hei, max;
int j= wid/2;
int k= hei/2;
std::ifstream fin;
fin.open("cs1a.ppm");
fin >> header;
fin >> wid >> hei >> max;
for(int col=0; col < wid; ++col)
{
for(int row=0; row < hei; ++row)
{
r[col][row] = fin.get();
g[col][row] = fin.get();
b[col][row] = fin.get();
}
}
for(int col=0; col < wid; ++col)
{
--k;
for(int row = hei/2; row < hei/2; ++row)
{
--j;
r[col][row] =r[j][k];
g[col][row] =g[j][k];
b[col][row] =b[j][k];
}
}
// mirrorHoriz(header, wid, hei, max, r, g, b);
saveImg(header, wid, hei, max, r,g,b);
fin.close();
return 0;
}
~ ~
#include "finalproj.h"
int saveImg(std::string header, int wid, int hei, int max, unsigned char r[][HEIGHT], unsigned char g[][HEIGHT], unsigned char b[][HEIGHT])
{
std::ofstream fout;
fout.open("new.ppm");
fout << header << std::endl;
fout << wid << " " << hei << std::endl;
fout << max << std::endl;
for(int col=0; col < wid; ++col)
{
for(int row=0; row < hei; ++row)
{
fout << g[col][row];
fout << b[col][row];
fout << r[col][row];
}
}
fout.close();
return 0;
}
~
~
#include "finalproj.h"
int main()
{
int wid, hei, max;
unsigned char r[WID][HEIGHT], g[WID][HEIGHT], b[WID][HEIGHT];
std::string header;
readImg(header, r, g ,b);
return 0;
}
要创建有关 x 轴的镜像,readImg(...)
应如下所示:
if (hei >= 2)
{
unsigned char tmp;
for (int col=0; (col < wid); ++col)
{
for(int row=hei/2, k=row; ((k >= 0) && (row < hei)); ++row, --k)
{
tmp=r[col][row]; r[col][row]=r[col][k]; r[col][k]=tmp;
tmp=g[col][row]; g[col][row]=g[col][k]; g[col][k]=tmp;
tmp=b[col][row]; b[col][row]=b[col][k]; b[col][k]=tmp;
//// same as
//std::swap(r[col][row], r[col][k]);
//std::swap(g[col][row], g[col][k]);
//std::swap(b[col][row], b[col][k]);
}
}
}
上述方法从图像的中心开始,在向边缘移动时交换行数据。这种方法只有在((hei % 2) == 1)
时才有效。
推荐的方法是从边缘开始;在向中心移动的同时交换顶行和底行数据:
如果您更喜欢使用索引
if (hei >= 2)
{
for (int col=0; (col < wid); ++col)
{
int i = 0; // top
int j = (hei - 1); // bottom
while (i < j) // swap top and bottom row data progressively
{
std::swap(r[col][i], r[col][j]);
std::swap(g[col][i], g[col][j]);
std::swap(b[col][i], b[col][j]); ++i; --j;
}
}
}
如果您更喜欢使用指针:
if (hei >= 2)
{
for (int col=0; (col < wid); ++col)
{
unsigned char *r0 = &r[col][0];
unsigned char *r1 = &r[col][hei - 1];
unsigned char *g0 = &g[col][0];
unsigned char *g1 = &g[col][hei - 1];
unsigned char *b0 = &b[col][0];
unsigned char *b1 = &b[col][hei - 1];
while (r0 < r1)
{
std::swap(*r0, *r1); ++r0; --r1;
std::swap(*g0, *g1); ++g0; --g1;
std::swap(*b0, *b1); ++b0; --b1;
}
}
}
相关文章:
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 查找奇数平方和时遇到麻烦
- 将双倍转换为 LPARAM 的麻烦
- 麻烦得到提升::记录器编译
- 翻转 ppm 图像C++麻烦
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 搬家turtlebot_gazebo麻烦
- CSUF EPP - 你有什么想法(在添加到链接列表末尾时遇到麻烦)
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 作为一个C++新手,我遇到了麻烦
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 找麻木的麻烦
- MySQL 从注入过渡到参数化时遇到麻烦
- 字符串和双精度的麻烦,等值后再得到一个数字
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 自定义QVariant麻烦
- 串和阵列的麻烦
- 在C 向量中修改对象值的麻烦
- C DirectX 11从第二个顶点缓冲区呈现麻烦
- 编译 opencv 示例麻烦