adding color arguments to drawBitMap functions

I need some help colorizing some bit map functions. ive been trying by myself for a week and just cant seem to get it right. can some one please help me.

void drawTriangle(int8_t x0, int8_t y0, int8_t x1, int8_t y1, int8_t x2, int8_t y2);
 void fillTriangle(int8_t x0, int8_t y0, int8_t x1, int8_t y1, int8_t x2, int8_t y2);
 void drawRoundRect(int8_t x0, int8_t y0, int8_t w, int8_t h, int8_t radius);
 void fillRoundRect(int8_t x0, int8_t y0, int8_t w, int8_t h, int8_t radius);
 
 void drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap);
 void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h , const uint8_t *bitmap);
 void drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap, uint8_t rotation, uint8_t flip);
 void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh);
 boolean getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y);
 
 void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet);
 void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet,uint8_t dx,uint8_t dy,uint8_t dw,uint8_t dh);

typedef uint16_t color;

I cannot post the cpp part so im attaching the file

It is unclear to me what you mean to colorize.

Each bitmap function should have a uint16_t color argument which is easy to set on the .h side but I can't figure out how to add the color argument correctly to the .cpp. I'm hoping to get some help in adding color arguments to the cpp versions of the above.

here are the cpp versions

void Display::drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap) {
   int8_t w = pgm_read_byte(bitmap);
   int8_t h = pgm_read_byte(bitmap + 1);
   bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
   drawBitmap(x,y,w,h,bitmap);
}

void Display::drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h , const uint8_t *bitmap) {
#if (ENABLE_BITMAPS > 0)
//   original code
    int8_t i, j, byteWidth = (w + 7) / 8;
    for (j = 0; j < h; j++) {
        for (i = 0; i < w; i++) {
            if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
                drawPixel(x + i, y + j);
            }
        }
    }

void Display::drawBitmap(int8_t x, int8_t y, int8_t w,int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh) {
    int8_t i, j, byteWidth = (w + 7) / 8;
    dw += dx;
    dh += dy;
    int8_t largest = 0;
    int8_t largesty = 0;
    for (j = 0; j < h; j++) {
        for (i = 0; i < w; i++) {
            if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
                int8_t drawX = x + i;
                int8_t drawY = y + j;
                
                if(drawX >= dx && drawX < dw && drawY >= dy && drawY < dh){
                    drawPixel(drawX, drawY);
                }
            }
        }
    }
}

boolean Display::getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y){
  return pgm_read_byte(bitmap+2 + y * ((pgm_read_byte(bitmap)+7)/8) + (x >> 3)) & (B10000000 >> (x % 8));
}

void Display::drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap,
        uint8_t rotation, uint8_t flip) {
    if((rotation == NOROT) && (flip == NOFLIP)){
        drawBitmap(x,y,bitmap); //use the faster algorithm
        return;
    }
    uint8_t w = pgm_read_byte(bitmap);
    uint8_t h = pgm_read_byte(bitmap + 1);
    bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
#if (ENABLE_BITMAPS > 0)
    int8_t i, j, //coordinates in the raw bitmap
            k, l, //coordinates in the rotated/flipped bitmap
            byteNum, bitNum, byteWidth = (w + 7) >> 3;

    rotation %= 4;

    for (i = 0; i < w; i++) {
        byteNum = i / 8;
        bitNum = i % 8;
        for (j = 0; j < h; j++) {
            if (pgm_read_byte(bitmap + j * byteWidth + byteNum) & (B10000000 >> bitNum)) {
                switch (rotation) {
                    case NOROT: //no rotation
                        k = i;
                        l = j;
                        break;
                    case ROTCCW: //90° counter-clockwise
                        k = j;
                        l = w - i - 1;
                        break;
                    case ROT180: //180°
                        k = w - i - 1;
                        l = h - j - 1;
                        break;
                    case ROTCW: //90° clockwise
                        k = h - j - 1;
                        l = i;
                        break;
                }
                if (flip) {
                    flip %= 4;
                    if (flip & B00000001) { //horizontal flip
                        k = w - k - 1;
                    }
                    if (flip & B00000010) { //vertical flip
                        l = h - l;
                    }
                }
                k += x; //place the bitmap on the screen
                l += y;
                drawPixel(k, l);
            }
        }
    }
#else
    drawRect(x, y, w, h);
#endif
}

I guess you basically want to set colors before you draw something. If that is the case, you are aware of the method setColor() in the cpp file? If so, have you considered to write a wrapper function in your code?

/*
  drawTriangle wrapper with colour
  in:
    xN, yN as coordinates
    color: desired foreeground (ms byte) / background (ms byte) colour    
*/
void drawTriangleWrapper(int8_t x0, int8_t y0, int8_t x1, int8_t y1, int8_t x2, int8_t y2, uint16_t color)
{
  // set the color
  display.SetColor(color >> 8, color & 0xFF);
  // draw the triangle
  display.drawTriangle(x0, y0, x1, y1, x2, y2);
}

'display' is your Display variable (whatever you have called it).

Alternative is to modify or duplicate the library methods; personally I will not easily go for that approach as one day I might forget to copy the library to another machine and when trying to compile it will fail.

Make a duplicate of the required method in the library code and add the additional parameter; e.g.

void Display::drawRect(int8_t x, int8_t y, int8_t w, int8_t h, uint16_t color) {

    setColor(color >> 8, color &0xFF);

    drawFastHLine(x, y, w);
    drawFastHLine(x, y + h - 1, w);
    drawFastVLine(x, y, h);
    drawFastVLine(x + w - 1, y, h);
}

Find the prototype of the method in the .h file and make a copy of it; add the new parameter in this copy as well.

Code not compiled / tested

Couldnt i just call the setcolor command when i call the bitmap function in the sketch. Thats pretty much how they work any way but using three calls one for grey one for black and white.

So

Display.Setcolor(blue)
Display.drawBitMap(70, 70, blue_square, 16, 16,)

Using the tile map will be fun though. I will have to make a bit map for each color then make my arrays then stack them by calling the command as many times as i need colors.

Still pretty cheap on flash. That technique allowed me to create most of a game and all the world map pieces for under 47%of the progmem used of an uno or pro mini

And ive got 256K instead of the usual 32k so imagine the possibilites of game creation you can do with that plus a 32-bit processor.

Duhjoker:
Couldnt i just call the setcolor command when i call the bitmap function in the sketch. Thats pretty much how they work any way but using three calls one for grey one for black and white.

So

Display.Setcolor(blue)
Display.drawBitMap(70, 70, blue_square, 16, 16,)

Of course you can, but that is not how I interpreted your original question. And if you knew this, why ask?

Just note that your 'version' of setColor only sets one of the two colors (I think the background).

Well I was hoping to use the color bitmap functions. For example ......

DrawBitMap(X, Y, bitmap, w, h, color)

Which means I can call the binary bitmap with the color already added to. I have those color commands but the problem is they won't work with the Gamebuino tilemap routine and collision.

So what I really want to do is add the color arguments to the h code above then figure out how to add the same color argument to the cpp version of the function.

The second example that I gave involved modifying the library as you intend to do.

Ok so I didn't have a setColor()function so I took the setTextColor() function and renamed it set color like this
Cpp
Void display::setColor (uint16_t color c) {
color = c;
bgcolor= bg;}

Void display::setColor(uint16_t color c, uint16_t color bg){
color = C
bgcolor = bg; }

Void setColor(uint16_t color c, uint16_t color bg)

Now I should be able to call setColor before the old Gamebuino drawBitmap() function to get a color per call. Only problem is that it's not optimized which means calling a bitmap function as many times as colors are needed. Good thing I have 256K for all that. But to be honest the tilemap routine is so efficient I could probably do all that and more.

I don't understand why you don't have a setColor() function. Line 205 and line 210 of the cpp that you provided are the start of those methods. You did not provide the .h file so I can't check if they are private or public.

They are public

It wasn't included in either of my LCD's local or utility files and I kept passing it over with out realizing I could that do that. So I was trying to optimize the wrong part of the code.

Thank you for mentioning the setColor thing to begin with!!!!!

Ok can we look at the these two function definitions or what ever.....

void Display::drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap) {
   int8_t w = pgm_read_byte(bitmap);
   int8_t h = pgm_read_byte(bitmap + 1);
   bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
   drawBitmap(x,y,w,h,bitmap);
}

void Display::drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h , const uint8_t *bitmap) {
#if (ENABLE_BITMAPS > 0)
//   original code
    int8_t i, j, byteWidth = (w + 7) / 8;
    for (j = 0; j < h; j++) {
        for (i = 0; i < w; i++) {
            if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
                drawPixel(x + i, y + j);
            }
        }
    }

The first is the first bitmap and it gives 3 arguments but the closing statement suggests 5 arguments.

Now the second drawbit map has 5 arguments but it boy list two in its last statement.

I know these two are mixed up but I've been tying to fix them and nada

The reason for the //comment is there are buffer and color stuff that was supposed to go after that to allow grey black white and invert colors I'm a monochrome scr en so I don't that. Though I might try to hack it one day so I can do animations and stuff.

I would really appreciate it if could take a look and help me fix then

Now the second drawbit map has 5 arguments but it boy list two in its last statement.

Which last statement?

The first one takes three arguments; the first two provide a position and the last one a pointer to a bitmap. The first two elements of the bitmap (which is stored in PROGMEN) indicate the width and the height and are read by the first function before it calls the second one.

Where does that second function come from? I can't find it in the original display.cpp that you attached in opening post. Looks like you tried to modify something.

Where is drawPixel defined?

I think it's getting outside my area of expertise (with regards to bitmaps). If you have modified the library, please attach both the original (cpp and h file) and the modified ones so I can see what you're trying to do.

// edit
PS
as far as I know, color information is stored in the bitmap.