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 {
44 
47  class PPM
48  {
49  public:
50  struct Color
51  {
52  unsigned char red, green, blue;
53 
54  // needed for Python bindings
55  bool operator==(const Color c)
56  {
57  return red == c.red && green == c.green && blue == c.blue;
58  }
59  };
60 
61  PPM();
62 
64  void loadFile(const char *filename);
65 
67  void saveFile(const char *filename);
68 
70  unsigned int getWidth() const
71  {
72  return width_;
73  }
74 
76  unsigned int getHeight() const
77  {
78  return height_;
79  }
80 
83  void setWidth(unsigned int width)
84  {
85  width_ = width;
86  }
87 
90  void setHeight(unsigned int height)
91  {
92  height_ = height;
93  }
94 
97  const std::vector<Color> &getPixels() const
98  {
99  return pixels_;
100  }
104  std::vector<Color> &getPixels()
105  {
106  return pixels_;
107  }
108 
110  const Color& getPixel(const int row, const int col) const
111  {
112  return pixels_[row * width_ + col];
113  }
114 
116  Color& getPixel(const int row, const int col)
117  {
118  return pixels_[row * width_ + col];
119  }
120  private:
121 
122  std::vector<Color> pixels_;
123  unsigned int width_;
124  unsigned int height_;
125  };
126 }
127 
128 #endif
Load and save .ppm files - "portable pixmap format" an image file formats designed to be easily excha...
Definition: PPM.h:47
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:81
unsigned int getWidth() const
Get the width of the loaded image.
Definition: PPM.h:70
Color & getPixel(const int row, const int col)
Directly access a pixel in the image.
Definition: PPM.h:116
const std::vector< Color > & getPixels() const
Get read-only access to the pixels in the image. To access a pixel at coordinate (row,col), use getPixels()[row * getWidth() + col].
Definition: PPM.h:97
void setHeight(unsigned int height)
Set the height for the loaded image. This must eventually match the number of pixels, if saveFile() gets called.
Definition: PPM.h:90
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:45
Main namespace. Contains everything in this library.
Definition: Cost.h:42
unsigned int getHeight() const
Get the height of the loaded image.
Definition: PPM.h:76
void setWidth(unsigned int width)
Set the width for the loaded image. This must eventually match the number of pixels, if saveFile() gets called.
Definition: PPM.h:83
const Color & getPixel(const int row, const int col) const
Directly access a pixel in the image.
Definition: PPM.h:110
std::vector< Color > & getPixels()
Get write access to the pixels in the image. To access a pixel at coordinate (row,col), use getPixels()[row * getWidth() + col]. This must eventually match the width & height set by setWidth() and setHeight().
Definition: PPM.h:104