Ngo Kien / Graphics

Dependents:   SignalProcessLab DigitalSignalAlgorithm_Lab DigitalSignal_Lab

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Canvas.cpp Source File

Canvas.cpp

00001 //
00002 // Canvas.cpp - Simple canvas.
00003 //
00004 
00005 #include "Canvas.h"
00006 #include "stdlib.h"
00007 
00008 Canvas::Canvas(void)
00009 {
00010     Reset();
00011 }
00012 
00013 
00014 Canvas::Canvas(uint16_t width, uint16_t height)
00015 {
00016     SetSize(width, height);
00017 }
00018 
00019 
00020 Canvas::~Canvas()
00021 {
00022     free(_planeBitmap);
00023 }
00024 
00025 
00026 void Canvas::Reset()
00027 {
00028     _width = 0;
00029     _height = 0;
00030 
00031     _planeBitmap = (uint8_t *)NULL;
00032     _planeBitmapSize = 0;
00033 }
00034 
00035 
00036 uint16_t Canvas::DisplayWidth()
00037 {
00038     return _width;
00039 }
00040 
00041 
00042 uint16_t Canvas::DisplayHeight()
00043 {
00044     return _height;
00045 }
00046 
00047 void Canvas::DrawPoint(int posX, int posY, uint32_t colorMask)
00048 {
00049     if ((posX >= 0) && (posX < DisplayWidth()) && (posY >= 0) && (posY < DisplayHeight())) {
00050 
00051         uint32_t shift = posX % PLANE_BITMAP_ELEMENT_BITS;
00052         uint32_t col = posX / PLANE_BITMAP_ELEMENT_BITS;
00053 
00054         uint32_t colsNum = DisplayWidth() / PLANE_BITMAP_ELEMENT_BITS;
00055         if ((DisplayWidth() % PLANE_BITMAP_ELEMENT_BITS) > 0)
00056         {
00057             colsNum++;
00058         }
00059 
00060         uint32_t position = posY * colsNum + col;
00061 
00062         if (colorMask)
00063             _planeBitmap[position] |= 1 << shift;
00064         else
00065             _planeBitmap[position] &= ~(1 << shift);
00066     }
00067 }
00068 
00069 
00070 bool Canvas::SetSize(uint16_t width, uint16_t height)
00071 {
00072     _width = width;
00073     _height = height;
00074 
00075     int cols = _width / PLANE_BITMAP_ELEMENT_BITS;
00076     if ((_width % PLANE_BITMAP_ELEMENT_BITS) != 0)
00077     {
00078         cols++;
00079     }
00080 
00081     _planeBitmapSize = cols * _height;
00082 
00083     if ((_planeBitmap = IsSet() ? (uint8_t *)realloc((void *)_planeBitmap, _planeBitmapSize) : (uint8_t *)malloc(_planeBitmapSize)) == NULL)
00084     {
00085         Reset();
00086         return false;
00087     }
00088 
00089     Clear();
00090 
00091     return true;
00092 }
00093 
00094 
00095 void Canvas::Clear(void)
00096 {
00097     memset(_planeBitmap, 0, _planeBitmapSize);
00098 }
00099 
00100 
00101 uint8_t* Canvas::GetBitmap(void)
00102 {
00103     return _planeBitmap;
00104 }
00105 
00106 
00107 bool Canvas::IsSet()
00108 {
00109     return _planeBitmapSize != NULL;
00110 }