Skip to content

Commit 175c776

Browse files
author
Kirk
committed
draw_bitmap() - bounds checking, cleanup
1 parent 72b0b90 commit 175c776

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/qwiic_grssd1306.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,28 @@ void QwGrSSD1306::draw_bitmap(uint8_t x0, uint8_t y0, uint8_t dst_width, uint8_t
693693
if(x0 >= _viewport.width || y0 >= _viewport.height || !bmp_width || !bmp_height)
694694
return;
695695

696+
// Bounds check
697+
if(x0 + dst_width > _viewport.width) // out of bounds
698+
dst_width = _viewport.width - x0;
696699

697-
uint8_t bmp_x = 0; // fix
698-
uint8_t bmp_y = 0; // fix
700+
if(bmp_width < dst_width)
701+
dst_width = bmp_width;
699702

700-
uint8_t xinc, page0, page1;
701-
uint8_t startBit, endBit, grSetBits, grStartBit;
703+
if(y0 + dst_height > _viewport.height) // out of bounds
704+
dst_height = _viewport.height - y0;
705+
706+
if(bmp_height < dst_height)
707+
dst_height = bmp_height;
702708

703-
uint8_t bmp_mask[2];
704-
uint8_t bmp_data, bmpPage;
709+
// current position in the bitmap
710+
uint8_t bmp_x = 0;
711+
uint8_t bmp_y = 0;
705712

706-
uint8_t remainingBits;
707-
uint8_t neededBits;
713+
uint8_t page0, page1;
714+
uint8_t startBit, endBit, grSetBits, grStartBit;
715+
716+
uint8_t bmp_mask[2], bmp_data, bmpPage;
717+
uint8_t remainingBits, neededBits;
708718

709719
uint8_t y1 = y0+dst_height-1;
710720

@@ -725,7 +735,7 @@ void QwGrSSD1306::draw_bitmap(uint8_t x0, uint8_t y0, uint8_t dst_width, uint8_t
725735
// - Write the bitmap bits to the graphis buffer using the current operator
726736

727737
// Loop over the memory pages in the graphics buffer
728-
for(int i=page0; i <= page1; i++){
738+
for(int iPage=page0; iPage <= page1; iPage++){
729739

730740
// First, get the number of destination bits in the current page
731741
grStartBit = mod_byte(y0); //start bit
@@ -761,25 +771,25 @@ void QwGrSSD1306::draw_bitmap(uint8_t x0, uint8_t y0, uint8_t dst_width, uint8_t
761771

762772
// we have the mask for the bmp - loop over the width of the copy region, pulling out
763773
// bmp data and writing it to the graphics buffer
764-
for(xinc = 0; xinc < dst_width; xinc++ ){
774+
for(bmp_x = 0; bmp_x < dst_width; bmp_x++){
765775

766776
// get data bits out of current bitmap location and shift if needed
767-
bmp_data = (pBitmap[bmp_width*bmpPage + bmp_x + xinc] & bmp_mask[0]) >> startBit;
777+
bmp_data = (pBitmap[bmp_width*bmpPage + bmp_x] & bmp_mask[0]) >> startBit;
768778

769779
if(remainingBits) // more data to add from the next byte in this column
770-
bmp_data |= (pBitmap[bmp_width*(bmpPage+1) + bmp_x + xinc] & bmp_mask[1]) << (kByteNBits - remainingBits);
780+
bmp_data |= (pBitmap[bmp_width*(bmpPage+1) + bmp_x] & bmp_mask[1]) << (kByteNBits - remainingBits);
771781

772782
// Write the bmp data to the graphics buffer - using current write op. Note,
773783
// if the location in the buffer didn't start at bit 0, we shift bmp_data
774-
curROP(_pBuffer + i * _viewport.width + xinc + x0,
784+
curROP(_pBuffer + iPage * _viewport.width + bmp_x + x0,
775785
bmp_data << grStartBit, grSetBits);
776786

777787
}
778788
// move up our y values (graphics buffer and bitmap) by the number of bits transferred
779789
y0 += neededBits;
780790
bmp_y += neededBits;
781791

782-
page_check_bounds_range(_pageState[i], x0, x0+dst_width); // mark dirty range in page desc
792+
page_check_bounds_range(_pageState[iPage], x0, x0+dst_width); // mark dirty range in page desc
783793
}
784794

785795
}

0 commit comments

Comments
 (0)