37 #include "ompl/util/PPM.h" 38 #include "ompl/util/Exception.h" 41 ompl::PPM::PPM() : width_(0), height_(0)
47 FILE *fp = fopen(filename,
"r");
49 throw Exception(
"Unable to load '" + std::string(filename) +
"'");
52 AutoClose(FILE *f) : f_(f) { }
53 ~AutoClose() { fclose(f_); }
59 if (fscanf(fp,
"%c", &p6[0]) != 1 || fscanf(fp,
"%c", &p6[1]) != 1 || p6[0] !=
'P' || p6[1] !=
'6')
60 throw Exception(
"Invalid format for file '" + std::string(filename) +
61 "'. PPM is expected to start with the characters 'P6'.");
63 while ((
char)nc !=
'#' && ((
char)nc >
'9' || (
char)nc <
'0')) nc = fgetc(fp);
65 while ((
char)nc !=
'\n') nc = fgetc(fp);
68 if (fscanf(fp,
"%d", &width_) != 1 || fscanf(fp,
"%d", &height_) != 1)
69 throw Exception(
"Unable to parse width and height for '" + std::string(filename) +
"'");
70 if (width_ <= 0 || height_ <= 0)
71 throw Exception(
"Invalid image dimensions for '" + std::string(filename) +
"'");
72 if (fscanf(fp,
"%d", &nc) != 1 || nc != 255 )
73 throw Exception(
"Invalid RGB component for '" + std::string(filename) +
"'");
75 nc = width_ * height_ * 3;
76 pixels_.resize(width_ * height_);
77 if ((
int)fread(&pixels_[0],
sizeof(
unsigned char), nc, fp) != nc)
78 throw Exception(
"Unable to load image data from '" + std::string(filename) +
"'");
83 if (pixels_.size() != width_ * height_)
84 throw Exception(
"Number of pixels is " + std::to_string(pixels_.size()) +
85 " but the set width and height require " +
86 std::to_string(width_ * height_) +
" pixels.");
88 fp = fopen(filename,
"wb");
90 throw Exception(
"Unable to open '" + std::string(filename) +
"' for writing");
92 fprintf(fp,
"%d %d\n", width_, height_);
93 fprintf(fp,
"%d\n", 255);
94 fwrite(&pixels_[0], 3 * width_, height_, fp);
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
The exception type for ompl.