pixel_buffer.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Mark Page
29 */
30 
31 #pragma once
32 
33 #include <memory>
34 #include "../../Core/Math/rect.h"
35 #include "texture_format.h"
36 #include "buffer_usage.h"
37 
38 namespace clan
39 {
42 
43  class Size;
44  class Rect;
45  class PixelFormat;
46  class Color;
47  class Colorf;
48  class PixelBuffer;
49  class PixelBuffer_Impl;
50  class FileSystem;
51  class IODevice;
52  class GraphicContext;
53  class PixelBufferProvider;
54  class PixelConverter;
55 
58  {
61 
64  };
65 
68  {
69  public:
71  PixelBuffer();
72 
80  PixelBuffer(int width, int height, TextureFormat texture_format, const void *data = nullptr, bool only_reference_data = false);
81 
85  PixelBuffer(const std::string &fullname, bool srgb = false);
86 
91  PixelBuffer(const std::string &filename, const FileSystem &file_system, bool srgb = false);
92 
97  PixelBuffer(IODevice &file, const std::string &image_type, bool srgb = false);
98 
102  PixelBuffer(PixelBufferProvider *provider);
103 
104  virtual ~PixelBuffer();
105 
107  bool is_null() const { return !impl; }
108  explicit operator bool() const { return bool(impl); }
109 
111  void throw_if_null() const;
112 
114  PixelBuffer copy() const;
115 
117  PixelBuffer copy(const Rect &rect) const;
118 
120  int get_width() const;
121 
123  int get_height() const;
124 
126  Size get_size() const { return Size{ get_width(), get_height() }; }
127 
129  int get_pitch() const;
130 
135  float get_pixel_ratio() const;
136 
138  float get_dip_width() const { return get_width() / get_pixel_ratio(); }
139 
141  float get_dip_height() const { return get_height() / get_pixel_ratio(); }
142 
145 
147  void *get_data();
148 
149  const void *get_data() const;
150 
152  bool is_gpu() const;
153 
154  template<typename Type> Type *get_data() { return reinterpret_cast<Type*>(get_data()); }
155  template<typename Type> const Type *get_data() const { return reinterpret_cast<Type*>(get_data()); }
156 
158  unsigned char *get_data_uint8() { return reinterpret_cast<unsigned char*>(get_data()); }
159  const unsigned char *get_data_uint8() const { return reinterpret_cast<const unsigned char*>(get_data()); }
160 
162  unsigned short *get_data_uint16() { return reinterpret_cast<unsigned short*>(get_data()); }
163  const unsigned short *get_data_uint16() const { return reinterpret_cast<const unsigned short*>(get_data()); }
164 
166  unsigned int *get_data_uint32() { return reinterpret_cast<unsigned int*>(get_data()); }
167  const unsigned int *get_data_uint32() const { return reinterpret_cast<const unsigned int*>(get_data()); }
168 
170  void *get_line(int line) { unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
171  const void *get_line(int line) const { const unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
172 
174  unsigned char *get_line_uint8(int line) { return reinterpret_cast<unsigned char*>(get_line(line)); }
175  const unsigned char *get_line_uint8(int line) const { return reinterpret_cast<const unsigned char*>(get_line(line)); }
176 
178  unsigned short *get_line_uint16(int line) { return reinterpret_cast<unsigned short*>(get_line(line)); }
179  const unsigned short *get_line_uint16(int line) const { return reinterpret_cast<const unsigned short*>(get_line(line)); }
180 
182  unsigned int *get_line_uint32(int line) { return reinterpret_cast<unsigned int*>(get_line(line)); }
183  const unsigned int *get_line_uint32(int line) const { return reinterpret_cast<const unsigned int*>(get_line(line)); }
184 
186  bool has_transparency() const;
187 
191  unsigned int get_bytes_per_pixel() const;
192 
196  unsigned int get_bytes_per_block() const;
197 
201  unsigned int get_data_size() const;
202 
206  static unsigned int get_data_size(const Size &size, TextureFormat texture_format);
207 
211  static unsigned int get_bytes_per_pixel(TextureFormat texture_format);
212 
216  static unsigned int get_bytes_per_block(TextureFormat texture_format);
217 
219  bool is_compressed() const;
220 
222  static bool is_compressed(TextureFormat texture_format);
223 
225  TextureFormat get_format() const;
226 
231 
233  Colorf get_pixel(int x, int y);
234 
236  void lock(GraphicContext &gc, BufferAccess access);
237 
239  void unlock();
240 
242  void upload_data(GraphicContext &gc, const Rect &dest_rect, const void *data);
243 
247  void set_image(const PixelBuffer &source);
248 
252  void set_image(const PixelBuffer &source, PixelConverter &converter);
253 
259  void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect);
260 
266  void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect, PixelConverter &converter);
267 
272 
277 
279  PixelBuffer to_format(TextureFormat texture_format) const;
280 
282  PixelBuffer to_format(TextureFormat texture_format, PixelConverter &converter) const;
283 
285  void flip_vertical();
286 
290  void premultiply_alpha();
291 
296  void premultiply_gamma(float gamma);
297 
299  void set_pixel_ratio(float ratio);
300 
301  private:
302  std::shared_ptr<PixelBuffer_Impl> impl;
303  friend class PixelBuffer_Impl;
304  };
305 
307 }
PixelBuffer()
Constructs a null instance.
Definition: clanapp.h:35
Floating point color description class (for float).
Definition: color.h:798
bool has_transparency() const
Returns true if format has an alpha channel.
void premultiply_alpha()
Multiply the RGB components by the Alpha component.
float get_pixel_ratio() const
void set_pixel_ratio(float ratio)
Sets the display pixel ratio for this texture.
I/O Device interface.
Definition: iodevice.h:49
void throw_if_null() const
Throw an exception if this object is invalid.
Pixel data container.
Definition: pixel_buffer.h:67
unsigned int * get_line_uint32(int line)
Returns a pointer to the beginning of a specific line as 32 bit data.
Definition: pixel_buffer.h:182
int get_pitch() const
Returns the pitch (in bytes per scanline).
int get_height() const
Retrieves the actual height of the buffer.
TextureFormat get_format() const
Returns the pixel format.
Use of the pixel buffer is to send data to the gpu.
Definition: pixel_buffer.h:60
Element Array Buffer provider.
Definition: pixel_buffer_provider.h:41
const unsigned char * get_line_uint8(int line) const
Definition: pixel_buffer.h:175
bool is_compressed() const
Returns true if compressed.
PixelBuffer copy() const
Create a copy of the pixelbuffer that doesn't share data with the original pixel buffer.
friend class PixelBuffer_Impl
Definition: pixel_buffer.h:303
void * get_line(int line)
Returns a pointer to the beginning of a specific line.
Definition: pixel_buffer.h:170
Colorf get_pixel(int x, int y)
Return color of pixel at the specified coordinates.
PixelBufferDirection
Pixel buffer prefered direction.
Definition: pixel_buffer.h:57
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
const unsigned int * get_line_uint32(int line) const
Definition: pixel_buffer.h:183
const unsigned int * get_data_uint32() const
Definition: pixel_buffer.h:167
const Type * get_data() const
Definition: pixel_buffer.h:155
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:488
Size get_size() const
Retrieves the actual size of the buffer.
Definition: pixel_buffer.h:126
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:107
void unlock()
Unmaps element buffer.
void set_image(const PixelBuffer &source)
Copy source pixel buffer into this buffer, doing a format conversion if needed.
PixelBuffer to_format(TextureFormat texture_format) const
Converts current buffer to a new pixel format and returns the result.
Virtual File System (VFS).
Definition: file_system.h:46
TextureFormat
Texture format.
Definition: texture_format.h:38
Use of the pixel buffer is to retrieve data from the gpu.
Definition: pixel_buffer.h:63
PixelBuffer to_cpu(GraphicContext &gc)
Downloads the pixel buffer to CPU memory.
const unsigned short * get_data_uint16() const
Definition: pixel_buffer.h:163
Sizef get_dip_size() const
Returns the device independent size of this texture.
Definition: pixel_buffer.h:144
Low level pixel format converter class.
Definition: pixel_converter.h:44
unsigned int get_bytes_per_pixel() const
Returns the number of bytes per pixel.
float get_dip_height() const
Returns the device independent height of this texture.
Definition: pixel_buffer.h:141
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
unsigned char * get_line_uint8(int line)
Returns a pointer to the beginning of a specific line as 8 bit data.
Definition: pixel_buffer.h:174
Interface to drawing graphics.
Definition: graphic_context.h:256
2D (x,y) point structure - Integer
Definition: point.h:61
unsigned short * get_data_uint16()
Returns a pointer to the beginning of the pixel buffer as 16 bit data.
Definition: pixel_buffer.h:162
unsigned char * get_data_uint8()
Returns a pointer to the beginning of the pixel buffer as 8 bit data.
Definition: pixel_buffer.h:158
const void * get_line(int line) const
Definition: pixel_buffer.h:171
void upload_data(GraphicContext &gc, const Rect &dest_rect, const void *data)
Uploads data to buffer.
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:52
2D (width,height) size structure - Integer
Definition: size.h:170
void flip_vertical()
Flip the entire image vertically (turn it upside down)
PixelBuffer to_gpu(GraphicContext &gc)
Uploads the pixel buffer to GPU memory.
int get_width() const
Retrieves the actual width of the buffer.
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
const unsigned char * get_data_uint8() const
Definition: pixel_buffer.h:159
void premultiply_gamma(float gamma)
Multiply the RGB components by gamma value.
PixelBufferProvider * get_provider() const
Get Provider.
Type * get_data()
Definition: pixel_buffer.h:154
void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect)
Copy source pixel buffer into this buffer, doing a format conversion if needed.
virtual ~PixelBuffer()
float get_dip_width() const
Returns the device independent width of this texture.
Definition: pixel_buffer.h:138
const unsigned short * get_line_uint16(int line) const
Definition: pixel_buffer.h:179
unsigned int get_bytes_per_block() const
Returns the number of bytes per compression block.
unsigned short * get_line_uint16(int line)
Returns a pointer to the beginning of a specific line as 16 bit data.
Definition: pixel_buffer.h:178
2D (width,height) size structure - Float
Definition: size.h:183
unsigned int * get_data_uint32()
Returns a pointer to the beginning of the pixel buffer as 32 bit data.
Definition: pixel_buffer.h:166
unsigned int get_data_size() const
Returns the size in bytes of the image data.