Fix file extension check in snapshot function

This commit is contained in:
Matthias Hochsteger 2024-08-07 10:49:21 +02:00
parent 325175c88f
commit 7f666547c9

View File

@ -2130,19 +2130,15 @@ namespace netgen
#endif // JPEGLIB #endif // JPEGLIB
{ {
string command; string command;
string filename2; std::filesystem::path filepath(filename);
filename2 = filename; bool need_conversion = filepath.extension() != ".ppm";
if (need_conversion)
filepath += ".ppm";
if(filename2.substr(len-3) != ".ppm") cout << IM(3) << "Snapshot to file '" << filepath.string() << endl;
filename2 += ".ppm";
cout << "Snapshot to file '" << filename << endl; ofstream outfile(filepath);
// int w = Togl_Width (togl);
// int h = Togl_Height (togl);
ofstream outfile(filename2);
outfile << "P6" << endl outfile << "P6" << endl
<< "# CREATOR: Netgen" << endl << "# CREATOR: Netgen" << endl
<< w << " " << h << endl << w << " " << h << endl
@ -2153,12 +2149,10 @@ namespace netgen
outfile.put (buffer[k+3*j+3*w*(h-i-1)]); outfile.put (buffer[k+3*j+3*w*(h-i-1)]);
outfile << flush; outfile << flush;
if (filename2 == string(filename)) if (need_conversion)
return TCL_OK;
else
{ {
// convert image file (Unix/Linux only): // convert image file (Unix/Linux only):
command = string("convert -quality 100 ") + filename2 + " " + filename; command = string("convert -quality 100 ") + filepath.string() + " " + filename;
int err = system(command.c_str()); int err = system(command.c_str());
if (err != 0) if (err != 0)
@ -2167,18 +2161,12 @@ namespace netgen
return TCL_ERROR; return TCL_ERROR;
} }
command = string("rm ") + filename2; std::filesystem::remove(filepath);
err = system(command.c_str()); }
if (err != 0)
{
Tcl_SetResult (Togl_Interp(togl), (char*)"Cannot delete temporary file", TCL_VOLATILE);
return TCL_ERROR;
}
return TCL_OK; return TCL_OK;
} }
} }
}