pixel_buffer.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 <memory>
35 #include "../../Core/Math/rect.h"
36 #include "texture_format.h"
37 #include "buffer_usage.h"
38
39 namespace clan
40 {
43
44 class Size;
45 class Rect;
46 class PixelFormat;
47 class Color;
48 class Colorf;
49 class PixelBuffer;
50 class PixelBuffer_Impl;
51 class FileSystem;
52 class IODevice;
53 class GraphicContext;
54 class PixelBufferProvider;
55 class PixelConverter;
56
58 enum PixelBufferDirection
59 {
61 data_to_gpu,
62
64 data_from_gpu
65 };
66
68 class PixelBuffer
69 {
72
73 public:
75 PixelBuffer();
76
84 PixelBuffer(int width, int height, TextureFormat texture_format, const void *data = nullptr, bool only_reference_data = false);
85
89 PixelBuffer(const std::string &fullname, bool srgb = false);
90
95 PixelBuffer(const std::string &filename, const FileSystem &file_system, bool srgb = false);
96
101 PixelBuffer(IODevice &file, const std::string &image_type, bool srgb = false);
102
106 PixelBuffer(PixelBufferProvider *provider);
107
108 virtual ~PixelBuffer();
109
113
114 public:
116 bool is_null() const { return !impl; }
117
119 void throw_if_null() const;
120
122 PixelBuffer copy() const;
123
125 PixelBuffer copy(const Rect &rect) const;
126
128 int get_width() const;
129
131 int get_height() const;
132
134 Size get_size() const { return Size{ get_width(), get_height() }; }
135
137 int get_pitch() const;
138
143 float get_pixel_ratio() const;
144
146 float get_dip_width() const { return get_width() / get_pixel_ratio(); }
147
149 float get_dip_height() const { return get_height() / get_pixel_ratio(); }
150
152 Sizef get_dip_size() const { return Sizef{ get_dip_width(), get_dip_height() }; }
153
155 void *get_data();
156
157 const void *get_data() const;
158
160 bool is_gpu() const;
161
162 template<typename Type> Type *get_data() { return reinterpret_cast<Type*>(get_data()); }
163 template<typename Type> const Type *get_data() const { return reinterpret_cast<Type*>(get_data()); }
164
166 unsigned char *get_data_uint8() { return reinterpret_cast<unsigned char*>(get_data()); }
167 const unsigned char *get_data_uint8() const { return reinterpret_cast<const unsigned char*>(get_data()); }
168
170 unsigned short *get_data_uint16() { return reinterpret_cast<unsigned short*>(get_data()); }
171 const unsigned short *get_data_uint16() const { return reinterpret_cast<const unsigned short*>(get_data()); }
172
174 unsigned int *get_data_uint32() { return reinterpret_cast<unsigned int*>(get_data()); }
175 const unsigned int *get_data_uint32() const { return reinterpret_cast<const unsigned int*>(get_data()); }
176
178 void *get_line(int line) { unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
179 const void *get_line(int line) const { const unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
180
182 unsigned char *get_line_uint8(int line) { return reinterpret_cast<unsigned char*>(get_line(line)); }
183 const unsigned char *get_line_uint8(int line) const { return reinterpret_cast<const unsigned char*>(get_line(line)); }
184
186 unsigned short *get_line_uint16(int line) { return reinterpret_cast<unsigned short*>(get_line(line)); }
187 const unsigned short *get_line_uint16(int line) const { return reinterpret_cast<const unsigned short*>(get_line(line)); }
188
190 unsigned int *get_line_uint32(int line) { return reinterpret_cast<unsigned int*>(get_line(line)); }
191 const unsigned int *get_line_uint32(int line) const { return reinterpret_cast<const unsigned int*>(get_line(line)); }
192
194 bool has_transparency() const;
195
199 unsigned int get_bytes_per_pixel() const;
200
204 unsigned int get_bytes_per_block() const;
205
209 unsigned int get_data_size() const;
210
214 static unsigned int get_data_size(const Size &size, TextureFormat texture_format);
215
219 static unsigned int get_bytes_per_pixel(TextureFormat texture_format);
220
224 static unsigned int get_bytes_per_block(TextureFormat texture_format);
225
227 bool is_compressed() const;
228
230 static bool is_compressed(TextureFormat texture_format);
231
233 TextureFormat get_format() const;
234
238 PixelBufferProvider *get_provider() const;
239
241 Colorf get_pixel(int x, int y);
242
246
247 public:
248
250 void lock(GraphicContext &gc, BufferAccess access);
251
253 void unlock();
254
256 void upload_data(GraphicContext &gc, const Rect &dest_rect, const void *data);
257
261 void set_image(const PixelBuffer &source);
262
266 void set_image(const PixelBuffer &source, PixelConverter &converter);
267
273 void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect);
274
280 void set_subimage(const PixelBuffer &source, const Point &dest_pos, const Rect &src_rect, PixelConverter &converter);
281
285 PixelBuffer to_cpu(GraphicContext &gc);
286
290 PixelBuffer to_gpu(GraphicContext &gc);
291
293 PixelBuffer to_format(TextureFormat texture_format) const;
294
296 PixelBuffer to_format(TextureFormat texture_format, PixelConverter &converter) const;
297
299 void flip_vertical();
300
304 void premultiply_alpha();
305
310 void premultiply_gamma(float gamma);
311
313 void set_pixel_ratio(float ratio);
314
318
319 private:
320 std::shared_ptr<PixelBuffer_Impl> impl;
321 friend class PixelBuffer_Impl;
323 };
324
325 }
326
clan::PixelBuffer::get_width
int get_width() const
Retrieves the actual width of the buffer.
clan::PixelBuffer::get_data
const Type * get_data() const
Definition: pixel_buffer.h:163
clan::PixelBuffer::get_data_uint16
unsigned short * get_data_uint16()
Returns a pointer to the beginning of the pixel buffer as 16 bit data.
Definition: pixel_buffer.h:170
clan
Definition: clanapp.h:35
clan::Colorf
Floating point color description class (for float).
Definition: color.h:660
clan::PixelBuffer::get_dip_height
float get_dip_height() const
Returns the device independent height of this texture.
Definition: pixel_buffer.h:149
clan::PixelBuffer::~PixelBuffer
virtual ~PixelBuffer()
clan::PixelBuffer::set_subimage
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.
clan::PixelBuffer::to_format
PixelBuffer to_format(TextureFormat texture_format) const
Converts current buffer to a new pixel format and returns the result.
clan::PixelBuffer::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: pixel_buffer.h:116
clan::PixelBuffer::flip_vertical
void flip_vertical()
Flip the entire image vertically (turn it upside down)
clan::IODevice
I/O Device interface.
Definition: iodevice.h:50
clan::PixelBuffer::PixelBuffer
PixelBuffer()
Constructs a null instance.
clan::PixelBuffer
Pixel data container.
Definition: pixel_buffer.h:68
clan::Key::x
clan::PixelBuffer::set_pixel_ratio
void set_pixel_ratio(float ratio)
Sets the display pixel ratio for this texture.
clan::PixelBuffer::get_dip_size
Sizef get_dip_size() const
Returns the device independent size of this texture.
Definition: pixel_buffer.h:152
clan::PixelBuffer::to_cpu
PixelBuffer to_cpu(GraphicContext &gc)
Downloads the pixel buffer to CPU memory.
clan::data_to_gpu
Use of the pixel buffer is to send data to the gpu.
Definition: pixel_buffer.h:61
clan::PixelBuffer::get_data_size
unsigned int get_data_size() const
Returns the size in bytes of the image data.
clan::PixelBufferProvider
Element Array Buffer provider.
Definition: pixel_buffer_provider.h:42
clan::PixelBuffer::is_compressed
bool is_compressed() const
Returns true if compressed.
clan::PixelBuffer::get_data
void * get_data()
Returns a pointer to the beginning of the pixel buffer.
clan::PixelBuffer::get_provider
PixelBufferProvider * get_provider() const
Get Provider.
clan::PixelBuffer::get_pixel
Colorf get_pixel(int x, int y)
Return color of pixel at the specified coordinates.
clan::PixelBuffer::get_data_uint32
const unsigned int * get_data_uint32() const
Definition: pixel_buffer.h:175
clan::PixelBuffer::get_line_uint32
const unsigned int * get_line_uint32(int line) const
Definition: pixel_buffer.h:191
clan::PixelBufferDirection
PixelBufferDirection
Pixel buffer prefered direction.
Definition: pixel_buffer.h:58
clan::Key::d
clan::PixelBuffer::get_data_uint8
const unsigned char * get_data_uint8() const
Definition: pixel_buffer.h:167
clan::PixelBuffer::copy
PixelBuffer copy() const
Create a copy of the pixelbuffer that doesn't share data with the original pixel buffer.
clan::PixelBuffer::to_gpu
PixelBuffer to_gpu(GraphicContext &gc)
Uploads the pixel buffer to GPU memory.
clan::PixelBuffer::get_data_uint16
const unsigned short * get_data_uint16() const
Definition: pixel_buffer.h:171
clan::PixelBuffer::get_line_uint16
const unsigned short * get_line_uint16(int line) const
Definition: pixel_buffer.h:187
clan::StyleTokenType::string
@ keyword
clan::Rect
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:470
clan::PixelBuffer::has_transparency
bool has_transparency() const
Returns true if format has an alpha channel.
clan::PixelBuffer::get_height
int get_height() const
Retrieves the actual height of the buffer.
clan::PixelBuffer::get_size
Size get_size() const
Retrieves the actual size of the buffer.
Definition: pixel_buffer.h:134
clan::PixelBuffer::premultiply_gamma
void premultiply_gamma(float gamma)
Multiply the RGB components by gamma value.
clan::PixelBuffer::get_line_uint16
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:186
clan::Key::y
clan::PixelBuffer::PixelBuffer_Impl
friend class PixelBuffer_Impl
Definition: pixel_buffer.h:321
clan::PixelBuffer::get_data
Type * get_data()
Definition: pixel_buffer.h:162
clan::PixelBuffer::get_line
void * get_line(int line)
Returns a pointer to the beginning of a specific line.
Definition: pixel_buffer.h:178
clan::FileSystem
Virtual File System (VFS).
Definition: file_system.h:47
clan::PixelBuffer::unlock
void unlock()
Unmaps element buffer.
clan::PixelBuffer::lock
void lock(GraphicContext &gc, BufferAccess access)
Maps buffer into system memory.
clan::PixelBuffer::premultiply_alpha
void premultiply_alpha()
Multiply the RGB components by the Alpha component.
clan::TextureFormat
TextureFormat
Texture format.
Definition: texture_format.h:40
clan::data_from_gpu
Use of the pixel buffer is to retrieve data from the gpu.
Definition: pixel_buffer.h:64
clan::PixelConverter
Low level pixel format converter class.
Definition: pixel_converter.h:45
clan::PixelBuffer::set_image
void set_image(const PixelBuffer &source)
Copy source pixel buffer into this buffer, doing a format conversion if needed.
clan::PixelBuffer::is_gpu
bool is_gpu() const
Returns true if this pixel buffer is a GPU based one.
clan::PixelBuffer::get_data_uint32
unsigned int * get_data_uint32()
Returns a pointer to the beginning of the pixel buffer as 32 bit data.
Definition: pixel_buffer.h:174
clan::PixelBuffer::get_line_uint8
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:182
clan::GraphicContext
Interface to drawing graphics.
Definition: graphic_context.h:257
clan::Point
2D (x,y) point structure - Integer
Definition: point.h:62
clan::PixelBuffer::get_pitch
int get_pitch() const
Returns the pitch (in bytes per scanline).
clan::PixelBuffer::get_dip_width
float get_dip_width() const
Returns the device independent width of this texture.
Definition: pixel_buffer.h:146
clan::PixelBuffer::get_line_uint8
const unsigned char * get_line_uint8(int line) const
Definition: pixel_buffer.h:183
clan::PixelBuffer::get_line
const void * get_line(int line) const
Definition: pixel_buffer.h:179
clan::PixelBuffer::get_line_uint32
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:190
clan::PixelBuffer::get_bytes_per_pixel
unsigned int get_bytes_per_pixel() const
Returns the number of bytes per pixel.
clan::PixelBuffer::get_format
TextureFormat get_format() const
Returns the pixel format.
clan::PixelBuffer::get_bytes_per_block
unsigned int get_bytes_per_block() const
Returns the number of bytes per compression block.
clan::BufferAccess
BufferAccess
Array Buffer access enum.
Definition: buffer_usage.h:54
clan::Size
2D (width,height) size structure - Integer
Definition: size.h:156
clan::PixelBuffer::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::PixelBuffer::upload_data
void upload_data(GraphicContext &gc, const Rect &dest_rect, const void *data)
Uploads data to buffer.
clan::PixelBuffer::get_data_uint8
unsigned char * get_data_uint8()
Returns a pointer to the beginning of the pixel buffer as 8 bit data.
Definition: pixel_buffer.h:166
clan::PixelBuffer::get_pixel_ratio
float get_pixel_ratio() const
clan::Sizef
2D (width,height) size structure - Float
Definition: size.h:169