c++:尝试将创建的图像保存到.jpg文件,但图像无法打开
c++: Trying to save an image created to a .jpg file but the image won't open
我正在Visual Studio中使用C ,并且我有一种算法,该算法为N体型仿真问题创建图像。但是,该算法本身有效,但是我需要存储图像以证明其有效。我尝试了许多不同的方式(在代码中已经评论了),以实现这一目标。我对C 不太熟悉,并且代码已将其提供给我,因此任何帮助或建议都将不胜感激。
我尝试存储图像的功能是printparticle()函数:
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <windows.h>
using namespace std;
#define N 100
#define G 6.673e-11
#define TIMESTAMP 1e11
struct Particle {
double rx, ry;//position components
double vx, vy;//velocity components
double fx, fy;//force components
double mass;//mass of the particle
};
Particle Update(Particle p, double timestamp)
{
p.vx += timestamp*p.fx / p.mass;
p.vy += timestamp*p.fy / p.mass;
p.rx += timestamp*p.vx;
p.ry += timestamp*p.vy;
return p;
}
void PrintParticle(Particle p)
{
ofstream fout("particle.jpg");
fout << ("rx == %f ry == %f vx == %f vy == %f mass == %fn", p.rx, p.ry, p.vx, p.vy, p.mass)<< endl;
fout.close();
//FILE *f = fopen("particle.ppm", "w"); // Write image to PPM file.
//fprintf(f, "rx == %f ry == %f vx == %f vy == %f mass == %fn", p.rx, p.ry, p.vx, p.vy, p.mass);
//string filename = "/Users/Tasha/Documents/Visual Studio 2015/Projects/n-bodySim/n-bodySim";
//("rx == %f ry == %f vx == %f vy == %f mass == %fn", p.rx, p.ry, p.vx, p.vy, p.mass)->WriteImage(filename);
}
//Reset the forces on particle
Particle ResetForce(Particle p)
{
p.fx = 0.0;
p.fy = 0.0;
return p;
}
//Add force to particle a by particle b
Particle AddForce(Particle a, Particle b)
{
double EPS = 3E4; // softening parameter (just to avoid infinities)
double dx = b.rx - a.rx;
double dy = b.ry - a.ry;
double dist = sqrt(dx*dx + dy*dy);
double F = (G * a.mass * b.mass) / (dist*dist + EPS*EPS);
a.fx += F * dx / dist;
a.fy += F * dy / dist;
return a;
}
int main()
{
Particle particles[N];
srand(time(NULL));
//randomly generating N Particles
for (int i = 0; i < N; i++) {
double rx = 1e18*exp(-1.8)*(.5 - rand());
particles[i].rx = rx;
double ry = 1e18*exp(-1.8)*(.5 - rand());
particles[i].ry = ry;
double vx = 1e18*exp(-1.8)*(.5 - rand());
particles[i].vx = vx;
double vy = 1e18*exp(-1.8)*(.5 - rand());
particles[i].vy = vy;
double mass = 1.98892e30*rand() * 10 + 1e20;
particles[i].mass = mass;
}
int numberofiterations = 10;
int count = 0;
while (count < numberofiterations) {
for (int i = 0; i < N; i++)
{
particles[i] = ResetForce(particles[i]);
for (int j = 0; j < N; j++)
{
if (i != j)
{
particles[i] = AddForce(particles[i], particles[j]);
}
}
}
//loop again to update the time stamp here
for (int i = 0; i < N; i++)
{
particles[i] = Update(particles[i], TIMESTAMP);
}
for (int i = 0; i < N; i++)
{
PrintParticle(particles[i]);
}
count++;
}
return 0;
}
jpeg需要一个特殊的文件格式,您的代码无法实现。我建议您首先搜索图书馆以处理此图像形式或实施自己的编解码器。
简单地将文本写入文件并将其称为" .jpg"不起作用。
相关文章:
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何从所选目录(位于listWidget中)中筛选图像文件
- 如何将图像文件夹添加到CMake项目以便在c ++可执行文件中使用这些图像?
- 解码并保存 base64 C++中的图像文件
- BASE64图像文件用C 编码
- 如何在使用 c++/COM 发送时在电子邮件中插入/嵌入图像文件(.png)
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 使用 Imread 打开具有 Unicode 名称的图像文件
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- C 读取/写作PPM图像文件灰色图像
- 如何在C 中删除或删除.jpg和.dpx图像文件
- C++图像处理 - 将图像文件读入 2D 数组
- 将图像文件(.BMP)复制到其他位置
- 从位图图像文件读取像素数据值
- Boost:Asio.从服务器下载图像文件
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 使用 QPixmap 和 QLabel 在 Qt Creator 中以编程方式添加图像文件 (.PNG)
- C++:将整数的文本文件转换为BMP格式的位图图像文件
- 读取/写入PPM图像文件C++
- 将C++图像文件转换为数组