37 #include "ompl/util/PPM.h"
38 #include "ompl/util/Exception.h"
43 FILE *fp = fopen(filename,
"r");
45 throw Exception(
"Unable to load '" + std::string(filename) +
"'");
48 AutoClose(FILE *f) : f_(f)
60 if (fscanf(fp,
"%c", &p6[0]) != 1 || fscanf(fp,
"%c", &p6[1]) != 1 || p6[0] !=
'P' || p6[1] !=
'6')
61 throw Exception(
"Invalid format for file '" + std::string(filename) +
"'. PPM is expected to start with the "
64 while ((
char)nc !=
'#' && ((
char)nc >
'9' || (
char)nc <
'0'))
67 while ((
char)nc !=
'\n')
71 if (fscanf(fp,
"%d", &width_) != 1 || fscanf(fp,
"%d", &height_) != 1)
72 throw Exception(
"Unable to parse width and height for '" + std::string(filename) +
"'");
73 if (width_ <= 0 || height_ <= 0)
74 throw Exception(
"Invalid image dimensions for '" + std::string(filename) +
"'");
75 if (fscanf(fp,
"%d", &nc) != 1 || nc != 255 )
76 throw Exception(
"Invalid RGB component for '" + std::string(filename) +
"'");
78 nc = width_ * height_ * 3;
79 pixels_.resize(width_ * height_);
80 if ((
int)fread(&pixels_[0],
sizeof(
unsigned char), nc, fp) != nc)
81 throw Exception(
"Unable to load image data from '" + std::string(filename) +
"'");
86 if (pixels_.size() != width_ * height_)
87 throw Exception(
"Number of pixels is " + std::to_string(pixels_.size()) +
88 " but the set width and height require " + std::to_string(width_ * height_) +
" pixels.");
90 fp = fopen(filename,
"wb");
92 throw Exception(
"Unable to open '" + std::string(filename) +
"' for writing");
94 fprintf(fp,
"%d %d\n", width_, height_);
95 fprintf(fp,
"%d\n", 255);
96 fwrite(&pixels_[0], 3 * width_, height_, fp);