- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须读取、修改和写入 PPM 图像。我试图通过尝试将其写入新文件来检查我是否正确获取了数组中 PPM 图像的所有数据。但我得到的图像看起来像一台坏掉的电视:a pattern of this:
我不知道我做错了什么,我想至少知道一种方法来检查可能出了什么问题。这些是我的代码:主要的: 图片实例;
string filename = "Image03.ppm";
string format = "ppm";
bool fileWritten = false;
if (instance.load(filename, format)) {
cout << "The file has been successfully loaded" << endl;
cout << "The dimensions of the image are:" << endl;
cout << instance.getWidth() << "x" << instance.getHeight() << endl;
...
bool imaging::Image::load(const string & filename, const string & format){
string extension = filename.substr(filename.find_last_of(".") + 1);
bool flag = false; // to stop the loop when a different letter is found.
bool result = false; // stores the return value
unsigned int sz = extension.size();
for (unsigned int i = 0; i < sz; ++i) {
if (tolower(extension[i]) != tolower(format[i]) && !flag) {
cout << "The extension of the file does not match the required format." << endl;
flag = true;
}
}
if (!flag) {
string filename1 = filename;
const char *cstr = filename1.c_str();
int pw = 0;
int ph = 0;
float* buff = ReadPPM(cstr, &pw, &ph);
width = pw;
height = ph;
bool h = WritePPM(buff, width, height, "kokoo.ppm");
...
bool WritePPM(const float * data, int w, int h, const char * filename) {
ofstream myfile;
myfile.open(filename, ios::out | ios::binary);
myfile << "P6\n" << w << "\n" << h << "\n255\n";
for (int i = 0; i < w*h; i++) {
myfile.write((char*)&data[i], sizeof(float));
}
myfile.close();
return 0;
}
...
float * ReadPPM(const char * filename, int* w, int* h) {
ifstream myfile;
myfile.open(filename, ios::binary);
if (myfile.is_open())
{
int i = 0;
string letter;
cout << "File successfully open" << endl;
myfile.ignore(2, ' ');
int width;
int height;
myfile >> width;
myfile >> height;
myfile.ignore(4, ' ');
*w = width;
*h = height;
int size = width*height * 3;
int k = myfile.peek();
while (k == 32) { //32 is ascii for whitespace.
myfile.ignore(256,' ');
k = myfile.peek();
}
unsigned char * temp = new unsigned char[size];
myfile.read((char*)temp, size);
float * buff = new float[size];
while (i < size) {
buff[i] = (float)temp[i]/255;
i++;
cout << "before" << buff[i] << endl;
cin.get();
}
delete[] temp;
return buff;
myfile.close();
}
else
{
cout << "Error opening file";
cin.get();
return 0;
}
}
即使我的代码看起来很傻,这也是我对这门语言不熟悉的原因,除非我做错了什么,我喜欢它的样子,无论如何这是一项作业。
最佳答案
如果您真的必须转换为 float 组,这就是您做错的地方:
您在 WritePPM
中的循环是完全错误的。你忘记了 3,你忘记了乘以 255 来缩减,你不能将浮点值写入 PPM。它应该是这样的:
for (int i = 0; i < 3*w*h; i++) {
unsigned char c = (unsigned char) (255.0f * data[i] + 0.5f);
myfile.write((char*) &c, sizeof(c));
}
我没有检查所有其他代码,但你肯定还有一些其他错误,例如在 ReadPPM
中返回语句后关闭文件。
关于c++ - 用 C++ 写一个 PPM 文件并得到扭曲的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562507/
是否可以将 ppm 文件从 p3 转换为 p6 或使用枕头库打开和读取 ppm p3 文件? 我实际上有 ppm p3 文件并尝试使用枕头 lib 将它们转换为 jpg,但不幸的是,它不能仅读取 p3
我安装了草莓 Perl 和 PPM。我对从 CPAN 手动安装模块并不陌生,但最近遇到了一种情况,即一个模块需要一个依赖项,而那个依赖项需要 20 个。 所以我决定试一试 PPM(因为它会自动安装依赖
我目前正在为我的学校项目制定一个程序。它应该读取 ppm 图像并返回梯度作为输出。但我在处理大图像(例如 420 x 360)时遇到问题。它对于两侧低于 300 像素的图像效果很好。谁能告诉我我的代码
大家下午好。首先,我想为我的代码道歉 - 我是 C 语言的真正初学者。我的问题是 - 我得到了 ppm 文件,我需要将其中的值存储到数组中。我已经存储了颜色的高度、宽度和最大值,现在我存储值的想法将如
我正在尝试向左/或向右旋转图像 ppm。我成功旋转了 180 度,但 90 度似乎不起作用.. 这是我的代码 // working 1-D array of pixels pixel *n_array
我安装了 On Windows 7 64-bit OS ActivePerl 64bit Version 5.16.3.1603 on D disk as D:/perl64 我控制了windows路
我找到了将视频转换为 ppm 的教程。 http://webcache.googleusercontent.com/search?q=cache:http://dranger.com/ffmpeg/t
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
当我打开 ppm ,用户界面向我展示了几个可升级的包。不幸的是,我无法通过键入 Ctrla 或单击第一个元素,按住 Shift 然后选择列表中的最后一个元素来选择它们。 难道我真的必须遍历所有元素并按
我的 Sprite 移动得太慢了。基本上我想用更少的力来移动我的玩家。目前这个: getBody().applyForceToCenter(new Vector2(-10000000f,0f), tr
我想用 spatstat::ppm 拟合泊松点过程模型,但我不确定向函数提供协变量数据的最佳方法是什么。我知道 spatstat 需要平面坐标,因此在创建 ppp 点模式对象之前,我已将点位置数据转换
我找到了一个 PPM 图像,其 header 以 PF 开头(而不是标准的 P6、P5、P4 ……签名)。分辨率数字以 float (-1.000) 存储。没有插入评论来了解它是如何产生的。 因为分辨
C:\Users\Jason>ppm 版本不能在 X:/ampp/perl/vendor/lib/PP 中使用未定义的值作为 SCALAR 引用下午1684行,41行。 最初我解决了更多错误。错误号
我在让我的算法成功读取 PPM 图像时遇到了很多麻烦......它对某些图像工作完美,但对其他图像则严重失败,导致半灰色(RGB 205、205、205)图像。 我已经尝试了我能找到的一切并研究了几个
这个问题已经有答案了: read PPM file and store it in an array; coded with C (2 个回答) 已关闭 6 年前。 我是 C 编程新手,正在尝试文件操
有人可以告诉我为什么运行此代码时会出现段错误吗?我正在尝试打开 P6 格式的 PPM 文件,第二行有它的尺寸,第三行有一个 255 常数。下面是代表每个像素的数字的“二维数组”。我知道每个像素(RGB
我无法弄清楚如何将 ppm 文件读入标准输入,将像素存储在 C 中的 new_pix 二维数组中。我真的不知道如何开始。提前致谢! 最佳答案 我假设您可以从文件中获取图像的尺寸(将它们称为W和H)。如
我想用 C 将 Mandelbrot 绘制为 PPM 文件。我的代码可以工作,但我的绘图始终是黑色的。我有来自维基百科的代码。我成功的关键是思考“alfa”(我是这么认为的)。我不知道阿尔法应该是什么
我正在尝试编写一个读取 PPM 图像的函数,该函数应该返回内容。 PPM 图像具有以下文本格式: P3 numOfRows numOfColumns maxColor numOfRows-by-num
我从下面的代码中得到了这个输出 circle.ppm它有很多锯齿状的边缘。我相信我需要某种类型的抗锯齿方法,但我很难开始。 摆脱它们的最简单方法是什么? //********************
我是一名优秀的程序员,十分优秀!