PPM.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #include "ompl/util/PPM.h"
38 #include "ompl/util/Exception.h"
39 #include <cstdio>
40 #include <boost/lexical_cast.hpp>
41 
42 ompl::PPM::PPM() : width_(0), height_(0)
43 {
44 }
45 
46 void ompl::PPM::loadFile(const char *filename)
47 {
48  FILE *fp = fopen(filename, "r");
49  if (!fp)
50  throw Exception("Unable to load '" + std::string(filename) + "'");
51  struct AutoClose
52  {
53  AutoClose(FILE *f) : f_(f) { }
54  ~AutoClose() { fclose(f_); }
55  FILE *f_;
56  };
57  AutoClose _(fp); // close fp when this variable goes out of scope.
58 
59  char p6[2] = {0};
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) +
62  "'. PPM is expected to start with the characters 'P6'.");
63  int nc = fgetc(fp);
64  while ((char)nc != '#' && ((char)nc > '9' || (char)nc < '0')) nc = fgetc(fp);
65  if ((char)nc == '#')
66  while ((char)nc != '\n') nc = fgetc(fp);
67  else
68  ungetc(nc, fp);
69  if (fscanf(fp, "%d", &width_) != 1 || fscanf(fp, "%d", &height_) != 1)
70  throw Exception("Unable to parse width and height for '" + std::string(filename) + "'");
71  if (width_ <= 0 || height_ <= 0)
72  throw Exception("Invalid image dimensions for '" + std::string(filename) + "'");
73  if (fscanf(fp, "%d", &nc) != 1 || nc != 255 /* RGB component color marker*/)
74  throw Exception("Invalid RGB component for '" + std::string(filename) + "'");
75  fgetc(fp);
76  nc = width_ * height_ * 3;
77  pixels_.resize(width_ * height_);
78  if ((int)fread(&pixels_[0], sizeof(unsigned char), nc, fp) != nc)
79  throw Exception("Unable to load image data from '" + std::string(filename) + "'");
80 }
81 
82 void ompl::PPM::saveFile(const char *filename)
83 {
84  if (pixels_.size() != width_ * height_)
85  throw Exception("Number of pixels is " + boost::lexical_cast<std::string>(pixels_.size()) +
86  " but the set width and height require " +
87  boost::lexical_cast<std::string>(width_ * height_) + " pixels.");
88  FILE *fp;
89  fp = fopen(filename, "wb");
90  if (!fp)
91  throw Exception("Unable to open '" + std::string(filename) + "' for writing");
92  fprintf(fp, "P6\n");
93  fprintf(fp, "%d %d\n", width_, height_);
94  fprintf(fp, "%d\n", 255); // RGB marker
95  fwrite(&pixels_[0], 3 * width_, height_, fp);
96  fclose(fp);
97 }
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:82
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:46
The exception type for ompl.
Definition: Exception.h:47