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 
41 ompl::PPM::PPM() : width_(0), height_(0)
42 {
43 }
44 
45 void ompl::PPM::loadFile(const char *filename)
46 {
47  FILE *fp = fopen(filename, "r");
48  if (!fp)
49  throw Exception("Unable to load '" + std::string(filename) + "'");
50  struct AutoClose
51  {
52  AutoClose(FILE *f) : f_(f)
53  {
54  }
55  ~AutoClose()
56  {
57  fclose(f_);
58  }
59  FILE *f_;
60  };
61  AutoClose _(fp); // close fp when this variable goes out of scope.
62 
63  char p6[2] = {0};
64  if (fscanf(fp, "%c", &p6[0]) != 1 || fscanf(fp, "%c", &p6[1]) != 1 || p6[0] != 'P' || p6[1] != '6')
65  throw Exception("Invalid format for file '" + std::string(filename) + "'. PPM is expected to start with the "
66  "characters 'P6'.");
67  int nc = fgetc(fp);
68  while ((char)nc != '#' && ((char)nc > '9' || (char)nc < '0'))
69  nc = fgetc(fp);
70  if ((char)nc == '#')
71  while ((char)nc != '\n')
72  nc = fgetc(fp);
73  else
74  ungetc(nc, fp);
75  if (fscanf(fp, "%d", &width_) != 1 || fscanf(fp, "%d", &height_) != 1)
76  throw Exception("Unable to parse width and height for '" + std::string(filename) + "'");
77  if (width_ <= 0 || height_ <= 0)
78  throw Exception("Invalid image dimensions for '" + std::string(filename) + "'");
79  if (fscanf(fp, "%d", &nc) != 1 || nc != 255 /* RGB component color marker*/)
80  throw Exception("Invalid RGB component for '" + std::string(filename) + "'");
81  fgetc(fp);
82  nc = width_ * height_ * 3;
83  pixels_.resize(width_ * height_);
84  if ((int)fread(&pixels_[0], sizeof(unsigned char), nc, fp) != nc)
85  throw Exception("Unable to load image data from '" + std::string(filename) + "'");
86 }
87 
88 void ompl::PPM::saveFile(const char *filename)
89 {
90  if (pixels_.size() != width_ * height_)
91  throw Exception("Number of pixels is " + std::to_string(pixels_.size()) +
92  " but the set width and height require " + std::to_string(width_ * height_) + " pixels.");
93  FILE *fp;
94  fp = fopen(filename, "wb");
95  if (!fp)
96  throw Exception("Unable to open '" + std::string(filename) + "' for writing");
97  fprintf(fp, "P6\n");
98  fprintf(fp, "%d %d\n", width_, height_);
99  fprintf(fp, "%d\n", 255); // RGB marker
100  fwrite(&pixels_[0], 3 * width_, height_, fp);
101  fclose(fp);
102 }
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:88
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:45
The exception type for ompl.
Definition: Exception.h:46