summaryrefslogtreecommitdiff
path: root/glcdgraphics/bitmap.h
blob: dcfa26d91da9c7d01e623607ab6d776c50c9a198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * GraphLCD graphics library
 *
 * bitmap.h  -  cBitmap class
 *
 * based on graphlcd plugin 0.1.1 for the Video Disc Recorder
 *  (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2004-2010 Andreas Regel <andreas.regel AT powarman.de>
 * (c) 2010-2011 Wolfgang Astleitner <mrwastl AT users sourceforge net>
 *               Andreas 'randy' Weinberger 
 */

#ifndef _GLCDGRAPHICS_BITMAP_H_
#define _GLCDGRAPHICS_BITMAP_H_

#include <string>
#include <inttypes.h>

// graphlcd-base uses ARGB bitmaps instead of 1bit ones
// this flag will be checked in current versions of vdr-graphlcd-plugin
#define GRAPHLCD_CBITMAP_ARGB

namespace GLCD
{

#if 0
enum eColor
{
   clrTransparent,
   clrGray50,
   clrBlack,
   clrRed,
   clrGreen,
   clrYellow,
   clrMagenta,
   clrBlue,
   clrCyan,
   clrWhite                  
};
#endif


class cColor
{
protected:
  uint32_t color;

public:
   cColor(uint32_t col) { color = col; }
   cColor(const cColor & col) { color = col.color; }

   static const uint32_t Black       = 0xFF000000;
   static const uint32_t White       = 0xFFFFFFFF;
   static const uint32_t Red         = 0xFFFF0000;
   static const uint32_t Green       = 0xFF00FF00;
   static const uint32_t Blue        = 0xFF0000FF;
   static const uint32_t Magenta     = 0xFFFF00FF;
   static const uint32_t Cyan        = 0xFF00FFFF;
   static const uint32_t Yellow      = 0xFFFFFF00;
   static const uint32_t Transparent = 0x00FFFFFF;
   static const uint32_t ERRCOL      = 0x00000000;

   uint32_t GetColor (void)         { return color; }
   void     SetColor (uint32_t col) { color = col; }

   cColor Invert   (void);

   operator uint32_t(void) { return GetColor(); }

   static cColor ParseColor (std::string col);
   static uint32_t AlignAlpha  (uint32_t col) { 
       switch (col) {
           case Transparent:
           case ERRCOL:
               return col;
           default:
               return (col & 0xFF000000) ? col : (col | 0xFF000000);
       }
   }
};


class cFont;

class cBitmap
{
protected:
    int width;
    int height;
    int lineSize;
    uint32_t * bitmap;
    bool ismonochrome;
    bool processAlpha;

    uint32_t backgroundColor;

public:
    cBitmap(int width, int height, uint32_t * data = NULL);
    cBitmap(int width, int height, uint32_t initcol);
    cBitmap(const cBitmap & b);
    ~cBitmap();

    int Width() const { return width; }
    int Height() const { return height; }
    int LineSize() const { return lineSize; }
    const uint32_t * Data() const { return bitmap; }

    void Clear(uint32_t color = cColor::Transparent);
    void Invert();
    void DrawPixel(int x, int y, uint32_t color);
    void DrawLine(int x1, int y1, int x2, int y2, uint32_t color);
    void DrawHLine(int x1, int y, int x2, uint32_t color);
    void DrawVLine(int x, int y1, int y2, uint32_t color);
    void DrawRectangle(int x1, int y1, int x2, int y2, uint32_t color, bool filled);
    void DrawRoundRectangle(int x1, int y1, int x2, int y2, uint32_t color, bool filled, int size);
    void DrawEllipse(int x1, int y1, int x2, int y2, uint32_t color, bool filled, int quadrants);
    void DrawSlope(int x1, int y1, int x2, int y2, uint32_t color, int type);
    void DrawBitmap(int x, int y, const cBitmap & bitmap, uint32_t color = cColor::White, uint32_t bgcolor = cColor::Black, int opacity = 255);
    int DrawText(int x, int y, int xmax, const std::string & text, const cFont * font,
                 uint32_t color = cColor::White, uint32_t bgcolor = cColor::Black, bool proportional = true, int skipPixels = 0);
    int DrawCharacter(int x, int y, int xmax, uint32_t c, const cFont * font,
                      uint32_t color = cColor::White, uint32_t bgcolor = cColor::Black, int skipPixels = 0);

    cBitmap * SubBitmap(int x1, int y1, int x2, int y2) const;
    uint32_t GetPixel(int x, int y) const;

    void SetMonochrome(bool mono) { ismonochrome = mono; }
    bool IsMonochrome(void) const { return ismonochrome; }

    void SetProcessAlpha(bool procAlpha) { processAlpha = procAlpha; }
    bool IsProcessAlpha(void) const { return processAlpha; }
    
    static const unsigned char* ConvertTo1BPP(const cBitmap & bitmap, int threshold = 127);
    static const cBitmap* ConvertFrom1BPP(const unsigned char* monobmp, int w, int h, uint32_t fg = cColor::White, uint32_t bg = cColor::Black);

#if 0
    bool LoadPBM(const std::string & fileName);
    void SavePBM(const std::string & fileName);
#endif
};

} // end of namespace

#endif