SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
ImageRep.cpp
1 //
2 // ImageRep.cpp
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 // Macro for getting rid of unused variables commonly for assert checking
29 #define _unused(x) ((void)(x))
30 
31 #include "SUAPI-CppWrapper/model/ImageRep.hpp"
32 
33 #include <cassert>
34 #include <stdexcept>
35 
36 namespace CW {
37 
38 /*************************
39 * Private Static Functions
40 **************************/
41 SUImageRepRef ImageRep::copy_reference(const ImageRep& other) {
42  if (other.m_attached || SUIsInvalid(other.m_image_rep)) {
43  return other.m_image_rep;
44  }
45  ImageRep new_copy = other.copy();
46  new_copy.m_attached = true; // Ensures that the object will not be destroyed at the end this scope.
47  return new_copy.ref();
48 }
49 
50 /***************************
51 * Constructors / Destructors
52 ****************************/
53 
55  m_image_rep(SU_INVALID),
56  m_attached(false)
57 {}
58 
59 ImageRep::ImageRep(SUImageRepRef image_rep, bool attached):
60  m_image_rep(image_rep),
61  m_attached(attached)
62 {}
63 
64 
66  ImageRep(copy_reference(other), other.m_attached)
67 {}
68 
69 
71  if (SUIsValid(m_image_rep) && !m_attached) {
72  SUResult res = SUImageRepRelease(&m_image_rep);
73  assert(res == SU_ERROR_NONE); _unused(res);
74  }
75 }
76 
77 
79  if (!m_attached && SUIsValid(m_image_rep)) {
80  SUResult res = SUImageRepRelease(&m_image_rep);
81  assert(res == SU_ERROR_NONE); _unused(res);
82  }
83  m_image_rep = copy_reference(other);
84  m_attached = other.m_attached;
85  return (*this);
86 }
87 
88 
89 bool ImageRep::operator!() const {
90  return SUIsInvalid(m_image_rep);
91 }
92 
93 
94 SUImageRepRef ImageRep::ref() const {
95  return m_image_rep;
96 }
97 
98 
99 ImageRep::operator SUImageRepRef() const {
100  return ref();
101 }
102 
103 
104 ImageRep::operator SUImageRepRef*() {
105  return &m_image_rep;
106 }
107 
108 
110  if(!(*this)) {
111  throw std::logic_error("CW::ImageRep::copy(): ImageRep is null");
112  }
113  SUImageRepRef copy_image = SU_INVALID;
114  SUResult res = SUImageRepCreate(&copy_image);
115  assert(res == SU_ERROR_NONE);
116  res = SUImageRepCopy(copy_image, m_image_rep);
117  assert(res == SU_ERROR_NONE); _unused(res);
118  return ImageRep(copy_image, false);
119 }
120 
121 
122 void ImageRep::set_data(size_t width, size_t height, size_t bits_per_pixel, size_t row_padding, const std::vector<SUByte> pixel_data) {
123  if(!(*this)) {
124  throw std::logic_error("CW::ImageRep::set_data(): ImageRep is null");
125  }
126  SUResult res = SUImageRepSetData(m_image_rep, width, height, bits_per_pixel, row_padding, &pixel_data[0]);
127  if (res == SU_ERROR_OUT_OF_RANGE) {
128  if (width == 0 || height == 0) {
129  throw std::invalid_argument("CW::ImageRep::set_data(): given width or height is 0 - it must be greater than 0");
130  }
131  else {
132  throw std::invalid_argument("CW::ImageRep::set_data(): given bits_per_pixel must be 8, 24 or 32");
133  }
134  }
135  assert(res == SU_ERROR_NONE); _unused(res);
136 }
137 
138 
139 void ImageRep::load_file(const std::string file_path) {
140  if(!(*this)) {
141  throw std::logic_error("CW::ImageRep::load_file(): ImageRep is null");
142  }
143  SUResult res = SUImageRepLoadFile(m_image_rep, file_path.c_str());
144  if (res == SU_ERROR_SERIALIZATION) {
145  throw std::invalid_argument("CW::ImageRep::load_file(): Loading file failed. Probably invalid file path given.");
146  }
147  assert(res == SU_ERROR_NONE); _unused(res);
148 }
149 
150 
151 SUResult ImageRep::save_to_file(const std::string file_path) const {
152  if(!(*this)) {
153  throw std::logic_error("CW::ImageRep::save_to_file(): ImageRep is null");
154  }
155  SUResult res = SUImageRepSaveToFile(m_image_rep, file_path.c_str());
156  if (res == SU_ERROR_SERIALIZATION) {
157  throw std::invalid_argument("CW::ImageRep::save_to_file(): Saving file failed. Probably invalid file path given.");
158  }
159  else if (res == SU_ERROR_NO_DATA) {
160  throw std::logic_error("CW::ImageRep::save_to_file(): Image contains no data to save.");
161  }
162  assert(res == SU_ERROR_NONE); _unused(res);
163  return res;
164 }
165 
166 
167 size_t ImageRep::width() const {
168  if(!(*this)) {
169  throw std::logic_error("CW::ImageRep::width(): ImageRep is null");
170  }
171  size_t width;
172  size_t height;
173  SUResult res = SUImageRepGetPixelDimensions(m_image_rep, &width, &height);
174  assert(res == SU_ERROR_NONE); _unused(res);
175  return width;
176 }
177 
178 
179 size_t ImageRep::height() const {
180  if(!(*this)) {
181  throw std::logic_error("CW::ImageRep::height(): ImageRep is null");
182  }
183  size_t width;
184  size_t height;
185  SUResult res = SUImageRepGetPixelDimensions(m_image_rep, &width, &height);
186  assert(res == SU_ERROR_NONE); _unused(res);
187  return height;
188 }
189 
190 
191 size_t ImageRep::row_padding() const {
192  if(!(*this)) {
193  throw std::logic_error("CW::ImageRep::row_padding(): ImageRep is null");
194  }
195  size_t padding;
196  SUResult res = SUImageRepGetRowPadding(m_image_rep, &padding);
197  assert(res == SU_ERROR_NONE); _unused(res);
198  return padding;
199 }
200 
201 
202 void ImageRep::resize(size_t width, size_t height) {
203  if(!(*this)) {
204  throw std::logic_error("CW::ImageRep::resize(): ImageRep is null");
205  }
206  SUResult res = SUImageRepResize(m_image_rep, width, height);
207  if (res == SU_ERROR_OUT_OF_RANGE) {
208  throw std::invalid_argument("CW::ImageRep::resize(): width and height must be greater than 0.");
209  }
210  assert(res == SU_ERROR_NONE); _unused(res);
211 }
212 
213 
215  if(!(*this)) {
216  throw std::logic_error("CW::ImageRep::convert_to_32bits(): ImageRep is null");
217  }
218  SUResult res = SUImageRepConvertTo32BitsPerPixel(m_image_rep);
219  if (res == SU_ERROR_NO_DATA) {
220  throw std::logic_error("CW::ImageRep::convert_to_32bits(): Image contains no data.");
221  }
222  assert(res == SU_ERROR_NONE); _unused(res);
223 }
224 
225 
226 size_t ImageRep::data_size() const {
227  if(!(*this)) {
228  throw std::logic_error("CW::ImageRep::data_size(): ImageRep is null");
229  }
230  size_t data_size;
231  size_t bits_per_pixel;
232  SUResult res = SUImageRepGetDataSize(m_image_rep, &data_size, &bits_per_pixel);
233  assert(res == SU_ERROR_NONE); _unused(res);
234  return data_size;
235 }
236 
237 
238 size_t ImageRep::bits_per_pixel() const {
239  if(!(*this)) {
240  throw std::logic_error("CW::ImageRep::bits_per_pixel(): ImageRep is null");
241  }
242  size_t data_size;
243  size_t bits_per_pixel;
244  SUResult res = SUImageRepGetDataSize(m_image_rep, &data_size, &bits_per_pixel);
245  assert(res == SU_ERROR_NONE); _unused(res);
246  return bits_per_pixel;
247 }
248 
249 
250 std::vector<SUByte> ImageRep::pixel_data() const {
251  if(!(*this)) {
252  throw std::logic_error("CW::ImageRep::pixel_data(): ImageRep is null");
253  }
254  std::vector<SUByte> pixel_data;
255  size_t data_size = this->data_size();
256  pixel_data.reserve(this->data_size());
257  SUResult res = SUImageRepGetData(m_image_rep, data_size, &pixel_data[0]);
258  assert(res == SU_ERROR_NONE); _unused(res);
259  return pixel_data;
260 }
261 
262 } // END namespace CW
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