ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Bitmap.h
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef Bitmap_h
24 #define Bitmap_h
25 
26 #include <inttypes.h>
27 #include <avr/pgmspace.h>
28 
29 class DMD;
30 class String;
31 
32 class Bitmap
33 {
34 public:
35  Bitmap(int width, int height);
36  ~Bitmap();
37 
38  bool isValid() const { return fb != 0; }
39 
40  typedef uint8_t Color;
41  typedef PGM_VOID_P ProgMem;
42  typedef PGM_VOID_P Font;
43 
44  static const Color Black = 0;
45  static const Color White = 1;
46  static const Color NoFill = 2;
47 
48  int width() const { return _width; }
49  int height() const { return _height; }
50  int stride() const { return _stride; }
51  int bitsPerPixel() const { return 1; }
52 
53  uint8_t *data() { return fb; }
54  const uint8_t *data() const { return fb; }
55 
56  void clear(Color color = Black);
57 
58  Color pixel(int x, int y) const;
59  void setPixel(int x, int y, Color color);
60 
61  void drawLine(int x1, int y1, int x2, int y2, Color color = White);
62  void drawRect(int x1, int y1, int x2, int y2, Color borderColor = White, Color fillColor = NoFill);
63  void drawFilledRect(int x1, int y1, int x2, int y2, Color color = White);
64  void drawCircle(int centerX, int centerY, int radius, Color borderColor = White, Color fillColor = NoFill);
65  void drawFilledCircle(int centerX, int centerY, int radius, Color color = White);
66 
67  void drawBitmap(int x, int y, const Bitmap &bitmap, Color color = White);
68  void drawBitmap(int x, int y, Bitmap::ProgMem bitmap, Color color = White);
69  void drawInvertedBitmap(int x, int y, const Bitmap &bitmap);
70  void drawInvertedBitmap(int x, int y, Bitmap::ProgMem bitmap);
71 
72  Font font() const { return _font; }
73  void setFont(Font font) { _font = font; }
74 
75  Color textColor() const { return _textColor; }
76  void setTextColor(Color color) { _textColor = color; }
77 
78  void drawText(int x, int y, const char *str, int len = -1);
79  void drawText(int x, int y, const String &str, int start = 0, int len = -1);
80 
81  int drawChar(int x, int y, char ch);
82 
83  int charWidth(char ch) const;
84  int textWidth(const char *str, int len = -1) const;
85  int textWidth(const String &str, int start = 0, int len = -1) const;
86  int textHeight() const;
87 
88  void copy(int x, int y, int width, int height, Bitmap *dest, int destX, int destY);
89  void fill(int x, int y, int width, int height, Color color);
90  void fill(int x, int y, int width, int height, Bitmap::ProgMem pattern, Color color = White);
91 
92  void scroll(int dx, int dy, Color fillColor = Black);
93  void scroll(int x, int y, int width, int height, int dx, int dy, Color fillColor = Black);
94 
95  void invert(int x, int y, int width, int height);
96 
97 private:
98  // Disable copy constructor and operator=().
99  Bitmap(const Bitmap &) {}
100  Bitmap &operator=(const Bitmap &) { return *this; }
101 
102  int _width;
103  int _height;
104  int _stride;
105  uint8_t *fb;
106  Font _font;
107  Color _textColor;
108 
109  friend class DMD;
110 
111  void blit(int x1, int y1, int x2, int y2, int x3, int y3);
112  void drawCirclePoints(int centerX, int centerY, int radius, int x, int y, Color borderColor, Color fillColor);
113 };
114 
115 inline void Bitmap::drawFilledRect(int x1, int y1, int x2, int y2, Color color)
116 {
117  drawRect(x1, y1, x2, y2, color, color);
118 }
119 
120 inline void Bitmap::drawFilledCircle(int centerX, int centerY, int radius, Color color)
121 {
122  drawCircle(centerX, centerY, radius, color, color);
123 }
124 
125 inline void Bitmap::drawInvertedBitmap(int x, int y, const Bitmap &bitmap)
126 {
127  drawBitmap(x, y, bitmap, Black);
128 }
129 
130 inline void Bitmap::drawInvertedBitmap(int x, int y, Bitmap::ProgMem bitmap)
131 {
132  drawBitmap(x, y, bitmap, Black);
133 }
134 
135 inline void Bitmap::scroll(int dx, int dy, Color fillColor)
136 {
137  scroll(0, 0, _width, _height, dx, dy, fillColor);
138 }
139 
140 #endif
Handle large dot matrix displays composed of LED's.
Definition: DMD.h:28
int width() const
Returns the width of the bitmap in pixels.
Definition: Bitmap.h:48
void copy(int x, int y, int width, int height, Bitmap *dest, int destX, int destY)
Copies the width x height pixels starting at top-left corner (x, y) to (destX, destY) in the bitmap d...
Definition: Bitmap.cpp:738
void scroll(int dx, int dy, Color fillColor=Black)
Scrolls the entire contents of the bitmap by dx and dy.
Definition: Bitmap.h:135
Represents a monochrome bitmap within main memory.
Definition: Bitmap.h:32
void drawRect(int x1, int y1, int x2, int y2, Color borderColor=White, Color fillColor=NoFill)
Draws a rectangle from (x1, y1) to (x2, y2), with the outline in borderColor and the interior filled ...
Definition: Bitmap.cpp:286
void setPixel(int x, int y, Color color)
Sets the pixel at (x, y) to color.
Definition: Bitmap.cpp:208
void drawFilledCircle(int centerX, int centerY, int radius, Color color=White)
Draws a filled circle with a specific center (centerX, centerY) and radius in color.
Definition: Bitmap.h:120
void drawLine(int x1, int y1, int x2, int y2, Color color=White)
Draws a line from (x1, y1) to (x2, y2) in color.
Definition: Bitmap.cpp:225
void drawBitmap(int x, int y, const Bitmap &bitmap, Color color=White)
Draws bitmap at (x, y) in color.
Definition: Bitmap.cpp:388
void drawFilledRect(int x1, int y1, int x2, int y2, Color color=White)
Draws a filled rectangle from (x1, y1) to (x2, y2) in color.
Definition: Bitmap.h:115
void drawInvertedBitmap(int x, int y, const Bitmap &bitmap)
Draws bitmap at (x, y) in inverted colors.
Definition: Bitmap.h:125
int bitsPerPixel() const
Returns the number of bits per pixel for the bitmap; always 1.
Definition: Bitmap.h:51
void drawCircle(int centerX, int centerY, int radius, Color borderColor=White, Color fillColor=NoFill)
Draws a circle with a specific center (centerX, centerY) and radius, with the outline in borderColor ...
Definition: Bitmap.cpp:334
void setTextColor(Color color)
Sets the color that will be used for drawing text with drawText() and drawChar(). ...
Definition: Bitmap.h:76
int drawChar(int x, int y, char ch)
Draws a single character ch at (x, y).
Definition: Bitmap.cpp:585
PGM_VOID_P ProgMem
Type that represents a bitmap within program memory.
Definition: Bitmap.h:41
uint8_t Color
Type that represents the color of a pixel in a bitmap.
Definition: Bitmap.h:40
Color textColor() const
Returns the color that will be used for drawing text with drawText() and drawChar(). The default is White.
Definition: Bitmap.h:75
int height() const
Returns the height of the bitmap in pixels.
Definition: Bitmap.h:49
static const Color NoFill
Special color value that is used with drawRect() and drawCircle() to indicate that the interior of th...
Definition: Bitmap.h:46
int textWidth(const char *str, int len=-1) const
Returns the width in pixels of the len characters of str in the current font(), including inter-chara...
Definition: Bitmap.cpp:675
void fill(int x, int y, int width, int height, Color color)
Fills the width x height pixels starting at top-left corner (x, y) with color.
Definition: Bitmap.cpp:762
const uint8_t * data() const
Returns a constant pointer to the start of the bitmap's data buffer. This is an overloaded member fun...
Definition: Bitmap.h:54
uint8_t * data()
Returns a pointer to the start of the bitmap's data buffer.
Definition: Bitmap.h:53
int textHeight() const
Returns the height in pixels of the current text drawing font(); or zero if font() is not set...
Definition: Bitmap.cpp:716
int charWidth(char ch) const
Returns the width in pixels of ch in the current font().
Definition: Bitmap.cpp:650
Bitmap(int width, int height)
Constructs a new in-memory bitmap that is width x height pixels in size.
Definition: Bitmap.cpp:88
bool isValid() const
Returns true if the memory for this bitmap is valid; false otherwise.
Definition: Bitmap.h:38
static const Color White
Color value corresponding to "white". If the bitmap is displayed on a LED array, then it may have a d...
Definition: Bitmap.h:45
Font font() const
Returns the currently selected font, or null if none selected.
Definition: Bitmap.h:72
int stride() const
Returns the number of bytes in each line of the bitmap's data() buffer.
Definition: Bitmap.h:50
static const Color Black
Color value corresponding to "black".
Definition: Bitmap.h:44
void clear(Color color=Black)
Clears the entire bitmap to the specified color.
Definition: Bitmap.cpp:174
PGM_VOID_P Font
Type that represents a font within program memory.
Definition: Bitmap.h:42
void setFont(Font font)
Sets the font for use with drawText() and drawChar().
Definition: Bitmap.h:73
void invert(int x, int y, int width, int height)
Inverts the width x height pixels starting at top-left corner (x, y).
Definition: Bitmap.cpp:902
void drawText(int x, int y, const char *str, int len=-1)
Draws the len characters of str at (x, y).
Definition: Bitmap.cpp:526
~Bitmap()
Destroys this bitmap.
Definition: Bitmap.cpp:106
Color pixel(int x, int y) const
Returns the color of the pixel at (x, y); either Black or White.
Definition: Bitmap.cpp:191