SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
Texture.hpp
1 //
2 // Texture.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 Texture_hpp
29 #define Texture_hpp
30 
31 #include <stdio.h>
32 
33 #include <SketchUpAPI/model/texture.h>
34 
35 #include "SUAPI-CppWrapper/model/Entity.hpp"
36 
37 namespace CW {
38 
39 // Forward declarations
40 class ImageRep;
41 class String;
42 
43 /*
44 * Texture wrapper
45 */
46 class Texture :public Entity {
47  private:
48  /**
49  * Create texture from ImageRep
50  */
51  static SUTextureRef create_texture(ImageRep& image_rep);
52 
53  /**
54  * Create texture from file path
55  */
56  static SUTextureRef create_texture(const std::string file_path, double s_scale = 1.0, double t_scale = 1.0);
57 
58  static SUTextureRef copy_reference(const Texture& other);
59  public:
60  /** Constructor for null Texture value */
61  Texture();
62 
63  /** Construct Texture from existing texture object */
64  Texture(SUTextureRef texture, bool attached = true);
65 
66  /**
67  * Creates a new texture object from an image file specified by a path.
68  * @param file_path - The file path of the source image file. Assumed to be UTF-8 encoded
69  * @param s_scale - The scale factor for s coordinate value.
70  * @param t_scale - The scale factor for t coordinate value.
71  */
72  Texture(const std::string file_path, double s_scale = 1.0, double t_scale = 1.0);
73 
74  /**
75  * Creates a new texture object with the specified image data.
76  * This function is due to be depreciated - use Texture(ImageRep& image) instead from SU 2017, API 5.0 onwards.
77  * @param width - the width in pixels of the texture data
78  * @param height - the height in pixels of the texture data
79  * @param bits_per_pixel - the number of bits per pixel of the image data
80  * @param pixel_data[] - the source of the pixel data
81  */
82  Texture (size_t width, size_t height, size_t bits_per_pixel, const SUByte pixel_data[]);
83 
84  /**
85  * Creates a new texture object from an image representation object.
86  * @since Sketchup 2017, API 5.0
87  */
88  Texture(ImageRep& image);
89 
90  /** Copy constructor */
91  Texture(const Texture& other);
92 
93  /** Destructor */
94  ~Texture();
95 
96  /** Copy assignment operator */
97  Texture& operator=(const Texture& other);
98 
99  /*
100  * The class object can be converted to a SUTextureRef without loss of data.
101  */
102  SUTextureRef ref() const;
103  operator SUTextureRef() const;
104  operator SUTextureRef*();
105 
106  /**
107  * Returns a copy of the texture object - note that a Texture object can only be assigned to one Material object. So this method is useful for copying Textures to new Material objects.
108  * @since SU 2017, API 5.0
109  */
110  Texture copy() const;
111 
112  /**
113  * Returns whether the alpha channel is used by the texture image.
114  */
115  bool alpha_used() const;
116 
117  /**
118  * Returns the ImageRep object of the texture.
119  * @since SU 2017, API 5.0
120  */
121  ImageRep image_rep() const;
122 
123  /**
124  * Returns the file name of the texture
125  */
126  String file_name() const;
127 
128  /**
129  * Sets theimage file name associated with the texture object. If the input texture was constructed using SUTextureCreateFromFile the name will already be set, so calling this function will override the texture's file name string.
130  */
131  void file_name(const String& string) const;
132 
133  /**
134  * Returns the width of the texture in pixels.
135  */
136  size_t width() const;
137 
138  /**
139  * Returns the height of the texture in pixels.
140  */
141  size_t height() const;
142 
143  /**
144  * Returns the s_scale of the texture. The s_scale and t_scale values are useful when a face doesn't have a material applied directly, but instead inherit from a parent group or component instance. Then you want use these values to multiply the result of SUMeshHelperGetFrontSTQCoords or SUUVHelperGetFrontUVQ. If the material is applied directly then this would not be needed.
145  */
146  double s_scale() const;
147 
148  /**
149  * Returns the t_scale of the texture. The s_scale and t_scale values are useful when a face doesn't have a material applied directly, but instead inherit from a parent group or component instance. Then you want use these values to multiply the result of SUMeshHelperGetFrontSTQCoords or SUUVHelperGetFrontUVQ. If the material is applied directly then this would not be needed.
150  */
151  double t_scale() const;
152 
153  /**
154  * Writes a texture object as an image to disk.
155  * @return SUResult object with the following possible values:
156  * - SU_ERROR_NONE on success
157  * - SU_ERROR_INVALID_INPUT if texture is not a valid object
158  * - SU_ERROR_NULL_POINTER_INPUT if file_path is NULL
159  * - SU_ERROR_SERIALIZATION if image file could not be written to disk
160  *
161  */
162  SUResult save(const std::string& file_path) const;
163 
164 };
165 
166 } // END namespace CW
167 #endif /* Texture_hpp */
size_t height() const
Definition: Texture.cpp:193
bool attached() const
Returns true if the entity is attached to another object.
Definition: Entity.cpp:100
size_t width() const
Definition: Texture.cpp:179
Texture copy() const
Definition: Texture.cpp:116
double s_scale() const
Definition: Texture.cpp:207
ImageRep image_rep() const
Definition: Texture.cpp:150
Definition: Color.hpp:34
Texture & operator=(const Texture &other)
Definition: Texture.cpp:98
bool alpha_used() const
Definition: Texture.cpp:139
String file_name() const
Definition: Texture.cpp:163
SUResult save(const std::string &file_path) const
Definition: Texture.cpp:235
double t_scale() const
Definition: Texture.cpp:221