ArduinoLibs
Bitmap.h
00001 /*
00002  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020  * DEALINGS IN THE SOFTWARE.
00021  */
00022 
00023 #ifndef Bitmap_h
00024 #define Bitmap_h
00025 
00026 #include <inttypes.h>
00027 #include <avr/pgmspace.h>
00028 
00029 class DMD;
00030 class String;
00031 
00032 class Bitmap
00033 {
00034 public:
00035     Bitmap(int width, int height);
00036     ~Bitmap();
00037 
00038     bool isValid() const { return fb != 0; }
00039 
00040     typedef uint8_t Color;
00041 
00042     static const Color Black  = 0;
00043     static const Color White  = 1;
00044     static const Color NoFill = 2;
00045 
00046     int width() const { return _width; }
00047     int height() const { return _height; }
00048     int stride() const { return _stride; }
00049     int bitsPerPixel() const { return 1; }
00050 
00051     uint8_t *data() { return fb; }
00052     const uint8_t *data() const { return fb; }
00053 
00054     void clear(Color color = Black);
00055 
00056     Color pixel(int x, int y) const;
00057     void setPixel(int x, int y, Color color);
00058 
00059     void drawLine(int x1, int y1, int x2, int y2, Color color = White);
00060     void drawRect(int x1, int y1, int x2, int y2, Color borderColor = White, Color fillColor = NoFill);
00061     void drawFilledRect(int x1, int y1, int x2, int y2, Color color = White);
00062     void drawCircle(int centerX, int centerY, int radius, Color borderColor = White, Color fillColor = NoFill);
00063     void drawFilledCircle(int centerX, int centerY, int radius, Color color = White);
00064 
00065     void drawBitmap(int x, int y, const Bitmap &bitmap, Color color = White);
00066     void drawBitmap(int x, int y, const prog_uint8_t *bitmap, Color color = White);
00067     void drawInvertedBitmap(int x, int y, const Bitmap &bitmap);
00068     void drawInvertedBitmap(int x, int y, const prog_uint8_t *bitmap);
00069 
00070     const prog_uint8_t *font() const { return _font; }
00071     void setFont(const prog_uint8_t *font) { _font = font; }
00072 
00073     Color textColor() const { return _textColor; }
00074     void setTextColor(Color color) { _textColor = color; }
00075 
00076     void drawText(int x, int y, const char *str, int len = -1);
00077     void drawText(int x, int y, const String &str, int start = 0, int len = -1);
00078 
00079     int drawChar(int x, int y, char ch);
00080 
00081     int charWidth(char ch) const;
00082     int textWidth(const char *str, int len = -1) const;
00083     int textWidth(const String &str, int start = 0, int len = -1) const;
00084     int textHeight() const;
00085 
00086     void copy(int x, int y, int width, int height, Bitmap *dest, int destX, int destY);
00087     void fill(int x, int y, int width, int height, Color color);
00088     void fill(int x, int y, int width, int height, const prog_uint8_t *pattern, Color color = White);
00089 
00090     void scroll(int dx, int dy, Color fillColor = Black);
00091     void scroll(int x, int y, int width, int height, int dx, int dy, Color fillColor = Black);
00092 
00093     void invert(int x, int y, int width, int height);
00094 
00095 private:
00096     // Disable copy constructor and operator=().
00097     Bitmap(const Bitmap &) {}
00098     Bitmap &operator=(const Bitmap &) { return *this; }
00099 
00100     int _width;
00101     int _height;
00102     int _stride;
00103     uint8_t *fb;
00104     const prog_uint8_t *_font;
00105     Color _textColor;
00106 
00107     friend class DMD;
00108 
00109     void blit(int x1, int y1, int x2, int y2, int x3, int y3);
00110     void drawCirclePoints(int centerX, int centerY, int radius, int x, int y, Color borderColor, Color fillColor);
00111 };
00112 
00113 inline void Bitmap::drawFilledRect(int x1, int y1, int x2, int y2, Color color)
00114 {
00115     drawRect(x1, y1, x2, y2, color, color);
00116 }
00117 
00118 inline void Bitmap::drawFilledCircle(int centerX, int centerY, int radius, Color color)
00119 {
00120     drawCircle(centerX, centerY, radius, color, color);
00121 }
00122 
00123 inline void Bitmap::drawInvertedBitmap(int x, int y, const Bitmap &bitmap)
00124 {
00125     drawBitmap(x, y, bitmap, Black);
00126 }
00127 
00128 inline void Bitmap::drawInvertedBitmap(int x, int y, const prog_uint8_t *bitmap)
00129 {
00130     drawBitmap(x, y, bitmap, Black);
00131 }
00132 
00133 inline void Bitmap::scroll(int dx, int dy, Color fillColor)
00134 {
00135     scroll(0, 0, _width, _height, dx, dy, fillColor);
00136 }
00137 
00138 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator