pixel_buffer.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 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Mark Page
29 */
30 
31 
32 #pragma once
33 
34 #include "../api_display.h"
35 #include <memory>
36 #include "../../Core/Math/rect.h"
37 #include "texture_format.h"
38 #include "buffer_usage.h"
39 
40 namespace clan
41 {
44 
45 class Size;
46 class Rect;
47 class PixelFormat;
48 class Color;
49 class Colorf;
50 class PixelBuffer;
51 class PixelBuffer_Impl;
52 class FileSystem;
53 class IODevice;
54 class GraphicContext;
55 class PixelBufferProvider;
56 class PixelConverter;
57 
60 {
63 
66 };
67 
69 class CL_API_DISPLAY PixelBuffer
70 {
73 
74 public:
76  PixelBuffer();
77 
85  PixelBuffer(int width, int height, TextureFormat texture_format, const void *data = 0, bool only_reference_data = false);
86 
90  PixelBuffer(const std::string &fullname, bool srgb = false);
91 
96  PixelBuffer(const std::string &filename, const FileSystem &file_system, bool srgb = false);
97 
102  PixelBuffer(IODevice &file, const std::string &image_type, bool srgb = false);
103 
107  PixelBuffer(PixelBufferProvider *provider);
108 
109  virtual ~PixelBuffer();
110 
114 
115 public:
117  bool is_null() const { return !impl; }
118 
120  void throw_if_null() const;
121 
123  PixelBuffer copy() const;
124 
126  PixelBuffer copy(const Rect &rect) const;
127 
129  int get_width() const;
130 
132  int get_height() const;
133 
135  Size get_size() const;
136 
138  int get_pitch() const;
139 
141  void *get_data();
142 
143  const void *get_data() const;
144 
146  bool is_gpu() const;
147 
148  template<typename Type> Type *get_data() { return reinterpret_cast<Type*>(get_data()); }
149  template<typename Type> const Type *get_data() const { return reinterpret_cast<Type*>(get_data()); }
150 
152  unsigned char *get_data_uint8() { return reinterpret_cast<unsigned char*>(get_data()); }
153  const unsigned char *get_data_uint8() const { return reinterpret_cast<const unsigned char*>(get_data()); }
154 
156  unsigned short *get_data_uint16() { return reinterpret_cast<unsigned short*>(get_data()); }
157  const unsigned short *get_data_uint16() const { return reinterpret_cast<const unsigned short*>(get_data()); }
158 
160  unsigned int *get_data_uint32() { return reinterpret_cast<unsigned int*>(get_data()); }
161  const unsigned int *get_data_uint32() const { return reinterpret_cast<const unsigned int*>(get_data()); }
162 
164  void *get_line(int line) { unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
165  const void *get_line(int line) const { const unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
166 
168  unsigned char *get_line_uint8(int line) { return reinterpret_cast<unsigned char*>(get_line(line)); }
169  const unsigned char *get_line_uint8(int line) const { return reinterpret_cast<const unsigned char*>(get_line(line)); }
170 
172  unsigned short *get_line_uint16(int line) { return reinterpret_cast<unsigned short*>(get_line(line)); }
173  const unsigned short *get_line_uint16(int line) const { return reinterpret_cast<const unsigned short*>(get_line(line)); }
174 
176  unsigned int *get_line_uint32(int line) { return reinterpret_cast<unsigned int*>(get_line(line)); }
177  const unsigned int *get_line_uint32(int line) const { return reinterpret_cast<const unsigned int*>(get_line(line)); }
178 
180  bool has_transparency() const;
181 
185  unsigned int get_bytes_per_pixel() const;
186 
190  unsigned int get_bytes_per_block() const;
191 
195  unsigned int get_data_size() const;
196 
200  static unsigned int get_data_size(const Size &size, TextureFormat texture_format);
201 
205  static unsigned int get_bytes_per_pixel(TextureFormat texture_format);
206 
210  static unsigned int get_bytes_per_block(TextureFormat texture_format);
211 
213  bool is_compressed() const;
214 
216  static bool is_compressed(TextureFormat texture_format);
217 
219  TextureFormat get_format() const;
220 
224  PixelBufferProvider *get_provider() const;
225 
227  Colorf get_pixel(int x, int y);
228 
232 
233 public:
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 
245  operator bool () const;
246 
250  void set_image(const PixelBuffer &source);
251 
255  void set_image(const PixelBuffer &source, PixelConverter &converter);
256 
262  void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect);
263 
269  void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect, PixelConverter &converter);
270 
274  PixelBuffer to_cpu(GraphicContext &gc);
275 
279  PixelBuffer to_gpu(GraphicContext &gc);
280 
282  PixelBuffer to_format(TextureFormat texture_format) const;
283 
285  PixelBuffer to_format(TextureFormat texture_format, PixelConverter &converter) const;
286 
288  void flip_vertical();
289 
293  void premultiply_alpha();
294 
299  void premultiply_gamma(float gamma);
300 
304 
305 private:
306  std::shared_ptr<PixelBuffer_Impl> impl;
307  friend class PixelBuffer_Impl;
309 };
310 
311 }
312 
const Type * get_data() const
Definition: pixel_buffer.h:149
unsigned short * get_data_uint16()
Returns a pointer to the beginning of the pixel buffer as 16 bit data.
Definition: pixel_buffer.h:156
Floating point color description class (for float).
Definition: color.h:661
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:117
I/O Device interface.
Definition: iodevice.h:51
Pixel data container.
Definition: pixel_buffer.h:69
Use of the pixel buffer is to send data to the gpu.
Definition: pixel_buffer.h:62
Element Array Buffer provider.
Definition: pixel_buffer_provider.h:42
const unsigned int * get_data_uint32() const
Definition: pixel_buffer.h:161
const unsigned int * get_line_uint32(int line) const
Definition: pixel_buffer.h:177
PixelBufferDirection
Pixel buffer prefered direction.
Definition: pixel_buffer.h:59
const unsigned char * get_data_uint8() const
Definition: pixel_buffer.h:153
const unsigned short * get_data_uint16() const
Definition: pixel_buffer.h:157
const unsigned short * get_line_uint16(int line) const
Definition: pixel_buffer.h:173
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
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:172
Type * get_data()
Definition: pixel_buffer.h:148
void * get_line(int line)
Returns a pointer to the beginning of a specific line.
Definition: pixel_buffer.h:164
Virtual File System (VFS).
Definition: file_system.h:48
TextureFormat
Texture format.
Definition: texture_format.h:41
Use of the pixel buffer is to retrieve data from the gpu.
Definition: pixel_buffer.h:65
Low level pixel format converter class.
Definition: pixel_converter.h:46
unsigned int * get_data_uint32()
Returns a pointer to the beginning of the pixel buffer as 32 bit data.
Definition: pixel_buffer.h:160
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:168
Interface to drawing graphics.
Definition: graphic_context.h:257
2D (x,y) point structure - Integer
Definition: point.h:63
const unsigned char * get_line_uint8(int line) const
Definition: pixel_buffer.h:169
const void * get_line(int line) const
Definition: pixel_buffer.h:165
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:176
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:55
2D (width,height) size structure - Integer
Definition: size.h:157
unsigned char * get_data_uint8()
Returns a pointer to the beginning of the pixel buffer as 8 bit data.
Definition: pixel_buffer.h:152