SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
ImageRep.hpp
1 //
2 // ImageRep.hpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 #ifndef ImageRep_hpp
29 #define ImageRep_hpp
30 
31 #include <stdio.h>
32 #include <stdio.h>
33 
34 #include <SketchUpAPI/model/image_rep.h>
35 
36 #include "SUAPI-CppWrapper/model/Entity.hpp"
37 
38 namespace CW {
39 
40 // Forward declarations
41 
42 /*
43 * ImageRep wrapper. References an image representation object.
44 * @since SU 2017, API 5.0
45 */
46 class ImageRep {
47  private:
48  SUImageRepRef m_image_rep;
49  bool m_attached;
50 
51  static SUImageRepRef copy_reference(const ImageRep& other);
52  public:
53  /** Constructor for null ImageRep value */
54  ImageRep();
55 
56  /** Construct ImageRep from existing ImageRep object */
57  ImageRep(SUImageRepRef image_rep, bool attached = true);
58 
59  /** Copy constructor */
60  ImageRep(const ImageRep& other);
61 
62  /** Destructor */
63  ~ImageRep();
64 
65  /** Copy assignment operator */
66  ImageRep& operator=(const ImageRep& other);
67 
68  /**
69  * Returns whether this is a valid object.
70  */
71  bool operator!() const;
72 
73  /*
74  * The class object can be converted to a SUImageRepRef without loss of data.
75  */
76  SUImageRepRef ref() const;
77  operator SUImageRepRef() const;
78  operator SUImageRepRef*();
79 
80  /**
81  * Returns a duplicate ImgaeRep object
82  */
83  ImageRep copy() const;
84 
85  /**
86  * Sets the image data for the given image. Makes a copy of the data rather than taking ownership.
87  */
88  void set_data(size_t width, size_t height, size_t bits_per_pixel, size_t row_padding, std::vector<SUByte> pixel_data);
89 
90  /**
91  * Loads an image from a file path.
92  * @param file_path - string path to the file.
93  */
94  void load_file(const std::string file_path);
95 
96  /**
97  * Saves the image to a file location.
98  */
99  SUResult save_to_file(const std::string file_path) const;
100 
101  /**
102  * Returns the width of the image in pixels.
103  */
104  size_t width() const;
105 
106  /**
107  * Returns the width of the image in pixels.
108  */
109  size_t height() const;
110 
111  /**
112  * Returns the row padding in pixels.
113  */
114  size_t row_padding() const;
115 
116  /**
117  * Resizes the width and height of the ImageRep
118  */
119  void resize(size_t width, size_t height);
120 
121  /**
122  * Converts the image to 32 bits per pixel.
123  */
124  void convert_to_32bits();
125 
126  /**
127  * Returns the total size and bits-per-pixel value of an image. This function is useful to determine the size of the buffer necessary to be passed into SUImageRepGetData. The returned data can be used along with the returned bits-per-pixel value and the image dimensions to compute RGBA values at individual pixels of the image.
128  */
129  size_t data_size() const;
130 
131  /**
132  * Returns the number of bits per pixel in the image.
133  */
134  size_t bits_per_pixel() const;
135 
136  /**
137  * Returns the pixel data for an image.
138  */
139  std::vector<SUByte> pixel_data() const;
140 };
141 
142 } // END namespace CW
143 
144 
145 #endif /* ImageRep_hpp */
size_t data_size() const
Definition: ImageRep.cpp:226
void load_file(const std::string file_path)
Definition: ImageRep.cpp:139
void set_data(size_t width, size_t height, size_t bits_per_pixel, size_t row_padding, std::vector< SUByte > pixel_data)
Definition: ImageRep.cpp:122
bool operator!() const
Definition: ImageRep.cpp:89
void resize(size_t width, size_t height)
Definition: ImageRep.cpp:202
ImageRep & operator=(const ImageRep &other)
Definition: ImageRep.cpp:78
size_t width() const
Definition: ImageRep.cpp:167
size_t row_padding() const
Definition: ImageRep.cpp:191
size_t bits_per_pixel() const
Definition: ImageRep.cpp:238
ImageRep copy() const
Definition: ImageRep.cpp:109
SUResult save_to_file(const std::string file_path) const
Definition: ImageRep.cpp:151
Definition: Color.hpp:34
std::vector< SUByte > pixel_data() const
Definition: ImageRep.cpp:250
void convert_to_32bits()
Definition: ImageRep.cpp:214
size_t height() const
Definition: ImageRep.cpp:179