Chris Taylor / Mbed 2 deprecated RETRO-CityRally

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bitmap2bpp.cpp Source File

Bitmap2bpp.cpp

00001 #include "mbed.h"
00002 #include "Bitmap2bpp.h"
00003 #include "Base.h"
00004 
00005 Bitmap2bpp::Bitmap2bpp(const uint8_t *bitmap) :
00006     _width(*(bitmap + 1) << 8 | *bitmap),
00007     _height(*(bitmap + 3) << 8 | *(bitmap + 2)),
00008     _stride((_width >> 2) + ((_width & 0x03) > 0 ? 1 : 0)),
00009     _pBitmapData((uint8_t*)(bitmap + 4))
00010 {
00011 }
00012 
00013 Bitmap2bpp::Bitmap2bpp(uint16_t width, uint16_t height) :
00014     _width(width), 
00015     _height(height),
00016     _stride((width >> 2) + ((width & 0x03) > 0 ? 1 : 0)),
00017     _pBitmapData(new uint8_t[_stride * height])    
00018 {
00019  
00020 }
00021 
00022 Bitmap2bpp::~Bitmap2bpp()
00023 {
00024     if (_pBitmapData != NULL)
00025     {
00026         delete []_pBitmapData;
00027         _pBitmapData = NULL;
00028     }
00029 }
00030 
00031 void Bitmap2bpp::clear()
00032 {
00033     memset(_pBitmapData, 0, _stride * _height);
00034 }
00035 
00036 void Bitmap2bpp::setPixel(int16_t x, int16_t y, uint16_t color)
00037 {
00038     if (x < 0 || x >= _width || y < 0 || y >= _height) return;
00039     
00040     int shift = 6 - ((x % 4) << 1);
00041     uint8_t mask = ~(0x03 << shift);
00042     uint8_t pixelColor = (color  & 0x03) << shift;
00043     uint8_t *p = _pBitmapData + ((y * _stride) + (x / 4));
00044     
00045     *p &= mask; 
00046     *p |= pixelColor;
00047 }
00048 
00049 void Bitmap2bpp::fastHLine(int16_t x1, int16_t x2, int16_t y, uint16_t color)
00050 {
00051     for (int x = x1; x <= x2; ++x)
00052     {
00053         setPixel(x, y, color);
00054     }
00055 }
00056 
00057 void Bitmap2bpp::fastVLine(int16_t y1, int16_t y2, int16_t x, uint16_t color)
00058 {
00059     for (int y = y1; y <= y2; ++y)
00060     {
00061         setPixel(x, y, color);
00062     }
00063 }
00064 void Bitmap2bpp::drawBitmap(int16_t x, int16_t y, Bitmap2bpp &bmp, uint16_t srcX, uint16_t srcY, uint16_t srcWidth, uint16_t srcHeight, bool transparent)
00065 {
00066     // Clip if out of bitmap    
00067     if ((x >= _width) || (x + srcWidth < 0) || 
00068         (y >= _height) || (y + srcHeight < 0))
00069     {
00070         return;
00071     }
00072     
00073     // Clip X
00074     if (x < 0) { srcX += -x; srcWidth += x; x = 0; }
00075     if (x + srcWidth >= _width) { srcWidth += _width - (x + srcWidth); }
00076     
00077     // Clip Y
00078     if (y  < 0) {srcY += -y; srcHeight += y; y = 0; }  
00079     if (y + srcHeight >= _height) { srcHeight += _height - (y + srcHeight); }
00080     
00081     int srcStartShift = 6 - ((srcX % 4) << 1);
00082     int dstStartShift = 6 - ((x % 4) << 1);
00083     
00084     int srcOffset = ((srcY * bmp.getStride()) + (srcX / 4));
00085     int dstOffset = ((y * getStride()) + (x / 4));
00086     for (int r = 0; r < srcHeight; ++r)
00087     {        
00088         uint8_t *src = bmp.getBitmapData() + srcOffset;
00089         uint8_t *dst = getBitmapData() + dstOffset;
00090         
00091         uint8_t sb = *src;
00092         for (int c = 0, srcShift = srcStartShift, dstShift = dstStartShift; c < srcWidth; ++c, srcShift -= 2, dstShift -= 2)
00093         {
00094             if (srcShift < 0) 
00095             {
00096                 srcShift = 6;
00097                 sb = *++src;
00098             }
00099             
00100             if (dstShift < 0) 
00101             {
00102                 dstShift = 6;
00103                 ++dst;
00104             }
00105             
00106             uint8_t srcPixel = (sb >> srcShift) &0x3;
00107             if (transparent && srcPixel == 0) continue;
00108             
00109             uint8_t mask = ~(0x03 << dstShift);
00110             uint8_t pixelColor = srcPixel << dstShift;
00111             
00112             *dst &= mask; 
00113             *dst |= pixelColor;
00114         }
00115         
00116         srcOffset += bmp.getStride();
00117         dstOffset += getStride();
00118     }
00119 }