image.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Kenneth Gangstoe
27 */
28 
29 
30 #pragma once
31 
32 #include "../api_display.h"
33 #include <memory>
34 #include "../../Core/Math/origin.h"
35 #include "../../Core/Resources/resource.h"
36 #include "color.h"
37 #include "../Image/image_import_description.h"
38 #include "../Render/texture.h"
39 
40 namespace clan
41 {
44 
45 class GraphicContext;
46 class FileSystem;
47 class Rect;
48 class Size;
49 class Rectf;
50 class Image_Impl;
51 class Texture2D;
52 class Subtexture;
53 class PixelBuffer;
54 class ResourceManager;
55 class XMLResourceDocument;
56 class Canvas;
57 class Quadf;
58 
60 class CL_API_DISPLAY Image
61 {
64 public:
66  Image();
67 
72  Image(Texture2D texture, const Rect &rect);
73 
77  Image(Subtexture &sub_texture);
78 
84  Image(Canvas &canvas, const PixelBuffer &pixelbuffer, const Rect &rect);
85 
91  Image(Canvas &canvas, const std::string &filename, const ImageImportDescription &import_desc = ImageImportDescription ());
92 
99  Image(Canvas &canvas, const std::string &filename, FileSystem &fs, const ImageImportDescription &import_desc = ImageImportDescription ());
100 
101  virtual ~Image();
103 
106 public:
112  static Resource<Image> resource(Canvas &canvas, const std::string &id, const ResourceManager &resources);
113 
115  static Image load(Canvas &canvas, const std::string &id, const XMLResourceDocument &doc);
117 
120 public:
122  bool is_null() const { return !impl; }
123 
125  void throw_if_null() const;
126 
128 
129  float get_scale_x() const;
130 
132 
133  float get_scale_y() const;
134 
136 
137  float get_alpha() const;
138 
140 
141  Colorf get_color() const;
142 
144  void get_alignment(Origin &origin, int &x, int &y) const;
145 
147  Subtexture get_texture() const;
148 
152  Size get_size() const;
153 
155  int get_width() const;
156 
158  int get_height() const;
159 
163 public:
165  bool operator==(const Image &other) const
166  {
167  return impl==other.impl;
168  }
169 
171  bool operator!=(const Image &other) const
172  {
173  return impl!=other.impl;
174  }
175 
177  bool operator<(const Image &other) const
178  {
179  return impl < other.impl;
180  }
182 
185 public:
187  Image clone() const;
188 
193  void draw(
194  Canvas &canvas,
195  float x,
196  float y) const;
197 
202  void draw(
203  Canvas &canvas,
204  int x,
205  int y) const;
206 
212  void draw(
213  Canvas &canvas,
214  const Rectf &src,
215  const Rectf &dest) const;
216 
221  void draw(
222  Canvas &canvas,
223  const Rectf &dest) const;
224 
230  void draw(
231  Canvas &canvas,
232  const Rectf &src,
233  const Quadf &dest) const;
234 
239  void draw(
240  Canvas &canvas,
241  const Quadf &dest) const;
242 
244 
245  void set_scale(float x, float y);
246 
248 
249  void set_alpha(float alpha);
250 
252 
253  void set_color(const Colorf &color);
254 
258  void set_color(const Color& c) {Colorf color; color.r = c.get_red() / 255.0f; color.g = c.get_green() / 255.0f; color.b = c.get_blue() / 255.0f; color.a = c.get_alpha() / 255.0f; set_color(color);}
259 
261  void set_alignment(Origin origin, int x = 0, int y = 0);
262 
263  void set_wrap_mode(
264  TextureWrapMode wrap_s,
265  TextureWrapMode wrap_t);
266 
268  void set_linear_filter(bool linear_filter = true);
269 
274  void set_subimage(
275  Canvas &canvas,
276  int x,
277  int y,
278  const PixelBuffer &image,
279  const Rect &src_rect,
280  int level = 0);
282 
285 private:
286  std::shared_ptr<Image_Impl> impl;
288 };
289 
290 }
291 
Floating point color description class (for float).
Definition: color.h:661
Sub-texture description.
Definition: subtexture.h:46
Resource proxy of a specific type.
Definition: resource.h:59
2D Graphics Canvas
Definition: canvas.h:70
TextureWrapMode
Texture coordinate wrapping modes.
Definition: texture.h:66
void set_color(const Color &c)
Set color.
Definition: image.h:258
unsigned char get_green() const
Returns the green color component, in the range 0-255.
Definition: color.h:91
bool operator<(const Image &other) const
Less than operator.
Definition: image.h:177
Pixel data container.
Definition: pixel_buffer.h:69
unsigned char get_blue() const
Returns the blue color component, in the range 0-255.
Definition: color.h:94
Origin
Alignment origins.
Definition: origin.h:41
Image Import Description Class.
Definition: image_import_description.h:49
unsigned char get_alpha() const
Returns the alpha color component, in the range 0-255.
Definition: color.h:85
unsigned char get_red() const
Returns the red color component, in the range 0-255.
Definition: color.h:88
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
Type a
Definition: vec4.h:84
2D texture object class.
Definition: texture_2d.h:42
Type r
Definition: vec4.h:81
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:467
2D quad structure - Float
Definition: quad.h:216
Virtual File System (VFS).
Definition: file_system.h:48
bool is_null() const
Returns true if this object is invalid.
Definition: image.h:122
bool operator!=(const Image &other) const
Inequality operator.
Definition: image.h:171
Resource manager.
Definition: resource_manager.h:45
bool operator==(const Image &other) const
Equality operator.
Definition: image.h:165
Image class.
Definition: image.h:60
2D (width,height) size structure - Integer
Definition: size.h:157
XML Resource Document.
Definition: xml_resource_document.h:49
Type b
Definition: vec4.h:83
Color description class.
Definition: color.h:47
Type g
Definition: vec4.h:82