PPM.h
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 #ifndef OMPL_UTIL_PPM_
38 #define OMPL_UTIL_PPM_
39 
40 #include <vector>
41 
42 namespace ompl
43 {
46  class PPM
47  {
48  public:
49  struct Color
50  {
51  unsigned char red, green, blue;
52 
53  // needed for Python bindings
54  bool operator==(const Color c)
55  {
56  return red == c.red && green == c.green && blue == c.blue;
57  }
58  };
59 
60  PPM() = default;
61 
62  /* \brief Load a .ppm file. Throw an exception in case of an error. */
63  PPM(const char *filename)
64  {
65  loadFile(filename);
66  }
67 
69  void loadFile(const char *filename);
70 
72  void saveFile(const char *filename);
73 
75  unsigned int getWidth() const
76  {
77  return width_;
78  }
79 
81  unsigned int getHeight() const
82  {
83  return height_;
84  }
85 
88  void setWidth(unsigned int width)
89  {
90  width_ = width;
91  }
92 
95  void setHeight(unsigned int height)
96  {
97  height_ = height;
98  }
99 
102  const std::vector<Color> &getPixels() const
103  {
104  return pixels_;
105  }
109  std::vector<Color> &getPixels()
110  {
111  return pixels_;
112  }
113 
115  const Color &getPixel(const int row, const int col) const
116  {
117  return pixels_[row * width_ + col];
118  }
119 
121  Color &getPixel(const int row, const int col)
122  {
123  return pixels_[row * width_ + col];
124  }
125 
126  private:
127  std::vector<Color> pixels_;
128  unsigned int width_{0};
129  unsigned int height_{0};
130  };
131 }
132 
133 #endif
unsigned int getHeight() const
Get the height of the loaded image.
Definition: PPM.h:145
const std::vector< Color > & getPixels() const
Get read-only access to the pixels in the image. To access a pixel at coordinate (row,...
Definition: PPM.h:166
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:41
void setHeight(unsigned int height)
Set the height for the loaded image. This must eventually match the number of pixels,...
Definition: PPM.h:159
void setWidth(unsigned int width)
Set the width for the loaded image. This must eventually match the number of pixels,...
Definition: PPM.h:152
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:84
unsigned int getWidth() const
Get the width of the loaded image.
Definition: PPM.h:139
const Color & getPixel(const int row, const int col) const
Directly access a pixel in the image.
Definition: PPM.h:179
Main namespace. Contains everything in this library.