pixel_buffer.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 
37 namespace uicore
38 {
39  class Size;
40  class Rect;
41  class PixelFormat;
42  class Color;
43  class Colorf;
44  class PixelBuffer;
45  class IODevice;
46  class GraphicContext;
47  typedef std::shared_ptr<GraphicContext> GraphicContextPtr;
49  typedef std::shared_ptr<PixelConverter> PixelConverterPtr;
50 
53  {
54  public:
62  static std::shared_ptr<PixelBuffer> create(int width, int height, TextureFormat texture_format, const void *data = nullptr, bool only_reference_data = false);
63 
65  std::shared_ptr<PixelBuffer> copy() const;
66  std::shared_ptr<PixelBuffer> copy(const Rect &rect) const;
67 
69  virtual int width() const = 0;
70 
72  virtual int height() const = 0;
73 
75  Size size() const { return Size{ width(), height() }; }
76 
78  virtual int pitch() const = 0;
79 
84  virtual float pixel_ratio() const = 0;
85 
87  float dip_width() const { return width() / pixel_ratio(); }
88 
90  float dip_height() const { return height() / pixel_ratio(); }
91 
93  Sizef dip_size() const { return Sizef{ dip_width(), dip_height() }; }
94 
96  virtual void *data() = 0;
97  virtual const void *data() const = 0;
98 
99  template<typename Type> Type *data() { return reinterpret_cast<Type*>(data()); }
100  template<typename Type> const Type *data() const { return reinterpret_cast<const Type*>(data()); }
101 
103  unsigned char *data_uint8() { return reinterpret_cast<unsigned char*>(data()); }
104  const unsigned char *data_uint8() const { return reinterpret_cast<const unsigned char*>(data()); }
105 
107  unsigned short *data_uint16() { return reinterpret_cast<unsigned short*>(data()); }
108  const unsigned short *data_uint16() const { return reinterpret_cast<const unsigned short*>(data()); }
109 
111  unsigned int *data_uint32() { return reinterpret_cast<unsigned int*>(data()); }
112  const unsigned int *data_uint32() const { return reinterpret_cast<const unsigned int*>(data()); }
113 
115  void *line(int line) { unsigned char *d = data_uint8(); return d + line * pitch(); }
116  const void *line(int line) const { const unsigned char *d = data_uint8(); return d + line * pitch(); }
117 
118  template<typename T> T *line(int line) { unsigned char *d = data_uint8(); return reinterpret_cast<T*>(d + line * pitch()); }
119  template<typename T> const T *line(int line) const { const unsigned char *d = data_uint8(); return reinterpret_cast<const T*>(d + line * pitch()); }
120 
122  unsigned char *line_uint8(int index) { return reinterpret_cast<unsigned char*>(line(index)); }
123  const unsigned char *line_uint8(int index) const { return reinterpret_cast<const unsigned char*>(line(index)); }
124 
126  unsigned short *line_uint16(int index) { return reinterpret_cast<unsigned short*>(line(index)); }
127  const unsigned short *line_uint16(int index) const { return reinterpret_cast<const unsigned short*>(line(index)); }
128 
130  unsigned int *line_uint32(int index) { return reinterpret_cast<unsigned int*>(line(index)); }
131  const unsigned int *line_uint32(int index) const { return reinterpret_cast<const unsigned int*>(line(index)); }
132 
134  bool has_transparency() const;
135 
139  unsigned int bytes_per_pixel() const;
140 
144  unsigned int bytes_per_block() const;
145 
149  unsigned int data_size() const;
150 
154  static unsigned int data_size(const Size &size, TextureFormat texture_format);
155 
159  static unsigned int bytes_per_pixel(TextureFormat texture_format);
160 
164  static unsigned int bytes_per_block(TextureFormat texture_format);
165 
167  bool is_compressed() const;
168 
170  static bool is_compressed(TextureFormat texture_format);
171 
173  virtual TextureFormat format() const = 0;
174 
178  void set_image(const std::shared_ptr<PixelBuffer> &source);
179 
183  void set_image(const std::shared_ptr<PixelBuffer> &source, const PixelConverterPtr &converter);
184 
190  void set_subimage(const std::shared_ptr<PixelBuffer> &source, const Point &dest_pos, const Rect &src_rect);
191 
197  void set_subimage(const std::shared_ptr<PixelBuffer> &source, const Point &dest_pos, const Rect &src_rect, const PixelConverterPtr &converter);
198 
200  std::shared_ptr<PixelBuffer> to_format(TextureFormat texture_format) const;
201 
203  std::shared_ptr<PixelBuffer> to_format(TextureFormat texture_format, const PixelConverterPtr &converter) const;
204 
206  void flip_vertical();
207 
211  void premultiply_alpha();
212 
217  void premultiply_gamma(float gamma);
218 
220  virtual void set_pixel_ratio(float ratio) = 0;
221 
223  static std::shared_ptr<PixelBuffer> add_border(const std::shared_ptr<PixelBuffer> &pb, int border_size, const Rect &rect);
224  };
225 
226  typedef std::shared_ptr<PixelBuffer> PixelBufferPtr;
227 }
unsigned short * data_uint16()
Returns a pointer to the beginning of the pixel buffer as 16 bit data.
Definition: pixel_buffer.h:107
const unsigned short * line_uint16(int index) const
Definition: pixel_buffer.h:127
2D (width,height) size structure - Integer
Definition: size.h:167
std::shared_ptr< PixelBuffer > to_format(TextureFormat texture_format) const
Converts current buffer to a new pixel format and returns the result.
virtual void set_pixel_ratio(float ratio)=0
Sets the display pixel ratio for this image.
virtual int width() const =0
Retrieves the width of the buffer.
Pixel data container.
Definition: pixel_buffer.h:52
unsigned int * line_uint32(int index)
Returns a pointer to the beginning of a specific line as 32 bit data.
Definition: pixel_buffer.h:130
const unsigned short * data_uint16() const
Definition: pixel_buffer.h:108
std::shared_ptr< PixelBuffer > copy() const
Create a copy of the pixelbuffer that doesn't share data with the original pixel buffer.
float dip_width() const
Returns the device independent width of this texture.
Definition: pixel_buffer.h:87
const unsigned int * line_uint32(int index) const
Definition: pixel_buffer.h:131
T * line(int line)
Definition: pixel_buffer.h:118
std::shared_ptr< PixelConverter > PixelConverterPtr
Definition: pixel_buffer.h:48
Type * data()
Definition: pixel_buffer.h:99
TextureFormat
Texture format.
Definition: texture_format.h:35
unsigned short * line_uint16(int index)
Returns a pointer to the beginning of a specific line as 16 bit data.
Definition: pixel_buffer.h:126
static std::shared_ptr< PixelBuffer > add_border(const std::shared_ptr< PixelBuffer > &pb, int border_size, const Rect &rect)
Add a border around a pixelbuffer, duplicating the edge pixels.
2D (x,y) point structure - Integer
Definition: point.h:58
void flip_vertical()
Flip the entire image vertically (turn it upside down)
virtual void * data()=0
Returns a pointer to the beginning of the pixel buffer.
Sizef dip_size() const
Returns the device independent size of this texture.
Definition: pixel_buffer.h:93
virtual TextureFormat format() const =0
Returns the pixel format.
const Type * data() const
Definition: pixel_buffer.h:100
2D (width,height) size structure - Float
Definition: size.h:180
void set_subimage(const std::shared_ptr< PixelBuffer > &source, const Point &dest_pos, const Rect &src_rect)
Copy source pixel buffer into this buffer, doing a format conversion if needed.
virtual float pixel_ratio() const =0
void premultiply_alpha()
Multiply the RGB components by the Alpha component.
unsigned char * line_uint8(int index)
Returns a pointer to the beginning of a specific line as 8 bit data.
Definition: pixel_buffer.h:122
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:491
const unsigned char * line_uint8(int index) const
Definition: pixel_buffer.h:123
void premultiply_gamma(float gamma)
Multiply the RGB components by gamma value.
virtual int height() const =0
Retrieves the height of the buffer.
unsigned char * data_uint8()
Returns a pointer to the beginning of the pixel buffer as 8 bit data.
Definition: pixel_buffer.h:103
unsigned int bytes_per_block() const
Returns the number of bytes per compression block.
std::shared_ptr< PixelBuffer > PixelBufferPtr
Definition: d3d_target.h:39
const unsigned char * data_uint8() const
Definition: pixel_buffer.h:104
std::shared_ptr< GraphicContext > GraphicContextPtr
Definition: d3d_target.h:49
void set_image(const std::shared_ptr< PixelBuffer > &source)
Copy source pixel buffer into this buffer, doing a format conversion if needed.
bool has_transparency() const
Returns true if format has an alpha channel.
virtual int pitch() const =0
Returns the pitch (bytes allocated per row).
void * line(int line)
Returns a pointer to the beginning of a specific line.
Definition: pixel_buffer.h:115
unsigned int bytes_per_pixel() const
Returns the number of bytes per pixel.
const void * line(int line) const
Definition: pixel_buffer.h:116
Pixel format converter class.
Definition: pixel_converter.h:39
Size size() const
Retrieves the size of the buffer.
Definition: pixel_buffer.h:75
float dip_height() const
Returns the device independent height of this texture.
Definition: pixel_buffer.h:90
bool is_compressed() const
Returns true if compressed.
static std::shared_ptr< PixelBuffer > create(int width, int height, TextureFormat texture_format, const void *data=nullptr, bool only_reference_data=false)
Constructs a PixelBuffer.
const unsigned int * data_uint32() const
Definition: pixel_buffer.h:112
Definition: Application/application.h:35
unsigned int data_size() const
Returns the size in bytes of the image data.
unsigned int * data_uint32()
Returns a pointer to the beginning of the pixel buffer as 32 bit data.
Definition: pixel_buffer.h:111
const T * line(int line) const
Definition: pixel_buffer.h:119