Gosu
Bitmap.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Gosu/Color.hpp>
4 #include <Gosu/IO.hpp>
5 #include <string>
6 #include <vector>
7 
8 namespace Gosu
9 {
14  class Bitmap
15  {
16  int m_width = 0, m_height = 0;
17  std::vector<Color> m_pixels;
18 
19  public:
20  Bitmap() = default;
21 
22  Bitmap(int width, int height, Color c = Color::NONE);
23 
24  int width() const
25  {
26  return m_width;
27  }
28 
29  int height() const
30  {
31  return m_height;
32  }
33 
34  void swap(Bitmap& other);
35 
36  void resize(int width, int height, Color c = Color::NONE);
37 
39  Color get_pixel(int x, int y) const
40  {
41  return m_pixels[y * m_width + x];
42  }
43 
45  void set_pixel(int x, int y, Color c)
46  {
47  m_pixels[y * m_width + x] = c;
48  }
49 
52  void blend_pixel(int x, int y, Color c);
53 
56  void insert(int x, int y, const Bitmap& source);
57 
60  void insert(int x, int y, const Bitmap& source,
61  int src_x, int src_y, int src_width, int src_height);
62 
65  const Color* data() const
66  {
67  return &m_pixels[0];
68  }
69 
73  {
74  return &m_pixels[0];
75  }
76 
77  #ifdef FRIEND_TEST
78  FRIEND_TEST(BitmapTests, MemoryManagement);
79  #endif
80  };
81 
83  Bitmap load_image_file(const std::string& filename);
86 
88  void save_image_file(const Bitmap& bitmap, const std::string& filename);
90  void save_image_file(const Bitmap& bitmap, Writer writer,
91  const std::string_view& format_hint = "png");
92 
96  void apply_color_key(Bitmap& bitmap, Color key);
97 
98  Bitmap apply_border_flags(unsigned image_flags, const Bitmap& source,
99  int src_x, int src_y, int src_width, int src_height);
100 }
Bitmap load_image_file(const std::string &filename)
Loads any supported image into a Bitmap.
Bitmap()=default
Definition: Audio.hpp:12
const Color * data() const
Direct access to the array of color values.
Definition: Bitmap.hpp:65
Represents an RGBA color value with 8 bits for each channel.
Definition: Color.hpp:16
void swap(Bitmap &other)
void set_pixel(int x, int y, Color c)
Sets the pixel at the specified position without any bounds checking.
Definition: Bitmap.hpp:45
void insert(int x, int y, const Bitmap &source)
Inserts a bitmap at the given position.
Color * data()
Direct access to the array of color values.
Definition: Bitmap.hpp:72
void blend_pixel(int x, int y, Color c)
This updates a pixel using the "over" alpha compositing operator, see: https://en.wikipedia.org/wiki/Alpha_compositing.
Bitmap apply_border_flags(unsigned image_flags, const Bitmap &source, int src_x, int src_y, int src_width, int src_height)
Utility class that points to a specific position in a resource and offers an interface for sequential...
Definition: IO.hpp:26
A two-dimensional array area of pixels, each represented by a Color value.
Definition: Bitmap.hpp:14
void resize(int width, int height, Color c=Color::NONE)
void save_image_file(const Bitmap &bitmap, const std::string &filename)
Saves a Bitmap to a file.
void apply_color_key(Bitmap &bitmap, Color key)
Set the alpha value of all pixels which are equal to the color key to zero.
Utility class that points to a specific position in a resource and offers an interface for sequential...
Definition: IO.hpp:84
int height() const
Definition: Bitmap.hpp:29
int width() const
Definition: Bitmap.hpp:24
Interface of the Color class.
static const Color NONE
Definition: Color.hpp:133
Color get_pixel(int x, int y) const
Returns the color at the specified position without any bounds checking.
Definition: Bitmap.hpp:39
Contains everything related to input and output.