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();
61 
63  void loadFile(const char *filename);
64 
66  void saveFile(const char *filename);
67 
69  unsigned int getWidth() const
70  {
71  return width_;
72  }
73 
75  unsigned int getHeight() const
76  {
77  return height_;
78  }
79 
82  void setWidth(unsigned int width)
83  {
84  width_ = width;
85  }
86 
89  void setHeight(unsigned int height)
90  {
91  height_ = height;
92  }
93 
96  const std::vector<Color> &getPixels() const
97  {
98  return pixels_;
99  }
103  std::vector<Color> &getPixels()
104  {
105  return pixels_;
106  }
107 
109  const Color &getPixel(const int row, const int col) const
110  {
111  return pixels_[row * width_ + col];
112  }
113 
115  Color &getPixel(const int row, const int col)
116  {
117  return pixels_[row * width_ + col];
118  }
119 
120  private:
121  std::vector<Color> pixels_;
122  unsigned int width_;
123  unsigned int height_;
124  };
125 }
126 
127 #endif
Load and save .ppm files - "portable pixmap format" an image file formats designed to be easily excha...
Definition: PPM.h:46
const Color & getPixel(const int row, const int col) const
Directly access a pixel in the image.
Definition: PPM.h:109
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition: PPM.cpp:88
Color & getPixel(const int row, const int col)
Directly access a pixel in the image.
Definition: PPM.h:115
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:89
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: AppBase.h:21
unsigned int getWidth() const
Get the width of the loaded image.
Definition: PPM.h:69
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:96
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:82
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:103
unsigned int getHeight() const
Get the height of the loaded image.
Definition: PPM.h:75