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 void ompl::PPM::loadFile(const char *filename)
42 {
43  FILE *fp = fopen(filename, "r");
44  if (fp == nullptr)
45  throw Exception("Unable to load '" + std::string(filename) + "'");
46  struct AutoClose
47  {
48  AutoClose(FILE *f) : f_(f)
49  {
50  }
51  ~AutoClose()
52  {
53  fclose(f_);
54  }
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) + "'. PPM is expected to start with the "
62  "characters 'P6'.");
63  int nc = fgetc(fp);
64  while ((char)nc != '#' && ((char)nc > '9' || (char)nc < '0'))
65  nc = fgetc(fp);
66  if ((char)nc == '#')
67  while ((char)nc != '\n')
68  nc = fgetc(fp);
69  else
70  ungetc(nc, fp);
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 /* RGB component color marker*/)
76  throw Exception("Invalid RGB component for '" + std::string(filename) + "'");
77  fgetc(fp);
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) + "'");
82 }
83 
84 void ompl::PPM::saveFile(const char *filename)
85 {
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.");
89  FILE *fp;
90  fp = fopen(filename, "wb");
91  if (fp == nullptr)
92  throw Exception("Unable to open '" + std::string(filename) + "' for writing");
93  fprintf(fp, "P6\n");
94  fprintf(fp, "%d %d\n", width_, height_);
95  fprintf(fp, "%d\n", 255); // RGB marker
96  fwrite(&pixels_[0], 3 * width_, height_, fp);
97  fclose(fp);
98 }
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:41
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:84
The exception type for ompl.
Definition: Exception.h:78