ompl/util/src/PPM.cpp
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Rice University 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Rice University nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 /* Author: Ioan Sucan */ 00036 00037 #include "ompl/util/PPM.h" 00038 #include "ompl/util/Exception.h" 00039 #include <cstdio> 00040 #include <boost/lexical_cast.hpp> 00041 00042 ompl::PPM::PPM() : width_(0), height_(0) 00043 { 00044 } 00045 00046 void ompl::PPM::loadFile(const char *filename) 00047 { 00048 FILE *fp = fopen(filename, "r"); 00049 if (!fp) 00050 throw Exception("Unable to load '" + std::string(filename) + "'"); 00051 struct AutoClose 00052 { 00053 AutoClose(FILE *f) : f_(f) { } 00054 ~AutoClose() { fclose(f_); } 00055 FILE *f_; 00056 }; 00057 AutoClose _(fp); // close fp when this variable goes out of scope. 00058 00059 char p6[2] = {0}; 00060 if (fscanf(fp, "%c", &p6[0]) != 1 || fscanf(fp, "%c", &p6[1]) != 1 || p6[0] != 'P' || p6[1] != '6') 00061 throw Exception("Invalid format for file '" + std::string(filename) + 00062 "'. PPM is expected to start with the characters 'P6'."); 00063 int nc = fgetc(fp); 00064 while ((char)nc != '#' && ((char)nc > '9' || (char)nc < '0')) nc = fgetc(fp); 00065 if ((char)nc == '#') 00066 while ((char)nc != '\n') nc = fgetc(fp); 00067 else 00068 ungetc(nc, fp); 00069 if (fscanf(fp, "%d", &width_) != 1 || fscanf(fp, "%d", &height_) != 1) 00070 throw Exception("Unable to parse width and height for '" + std::string(filename) + "'"); 00071 if (width_ <= 0 || height_ <= 0) 00072 throw Exception("Invalid image dimensions for '" + std::string(filename) + "'"); 00073 if (fscanf(fp, "%d", &nc) != 1 || nc != 255 /* RGB component color marker*/) 00074 throw Exception("Invalid RGB component for '" + std::string(filename) + "'"); 00075 fgetc(fp); 00076 nc = width_ * height_ * 3; 00077 pixels_.resize(width_ * height_); 00078 if ((int)fread(&pixels_[0], sizeof(unsigned char), nc, fp) != nc) 00079 throw Exception("Unable to load image data from '" + std::string(filename) + "'"); 00080 } 00081 00082 void ompl::PPM::saveFile(const char *filename) 00083 { 00084 if (pixels_.size() != width_ * height_) 00085 throw Exception("Number of pixels is " + boost::lexical_cast<std::string>(pixels_.size()) + 00086 " but the set width and height require " + 00087 boost::lexical_cast<std::string>(width_ * height_) + " pixels."); 00088 FILE *fp; 00089 fp = fopen(filename, "wb"); 00090 if (!fp) 00091 throw Exception("Unable to open '" + std::string(filename) + "' for writing"); 00092 fprintf(fp, "P6\n"); 00093 fprintf(fp, "%d %d\n", width_, height_); 00094 fprintf(fp, "%d\n", 255); // RGB marker 00095 fwrite(&pixels_[0], 3 * width_, height_, fp); 00096 fclose(fp); 00097 }