@@ -402,3 +402,53 @@ void AdaGfxDialog::drawButton(Adafruit_GFX* gfx, AdaColorGfxMenuConfig* config,
402402 gfx->print (title);
403403}
404404
405+
406+ /* * A couple of helper functions that we'll submit for inclusion once a bit more testing is done */
407+
408+ /* *************************************************************************/
409+ /* !
410+ @brief Draw a RAM-resident 1-bit image at the specified (x,y) position,
411+ from image data that may be wider or taller than the desired width and height.
412+ Imagine a cookie dough rolled out, where you can cut a rectangle out of it.
413+ It uses the specified foreground (for set bits) and background (unset bits) colors.
414+ This is particularly useful for GFXCanvas1 operations, where you can allocate the
415+ largest canvas needed and then use it for all drawing operations.
416+
417+ @param x Top left corner x coordinate
418+ @param y Top left corner y coordinate
419+ @param bitmap byte array with monochrome bitmap
420+ @param w width of the portion you want to draw
421+ @param h Height of the portion you want to draw
422+ @param totalWidth actual width of the bitmap
423+ @param xStart X position of the image in the data
424+ @param yStart Y position of the image in the data
425+ @param color 16-bit 5-6-5 Color to draw pixels with
426+ @param bg 16-bit 5-6-5 Color to draw background with
427+ */
428+ /* *************************************************************************/
429+ void drawCookieCutBitmap (Adafruit_GFX* gfx, int16_t x, int16_t y, const uint8_t *bitmap, int16_t w,
430+ int16_t h, int16_t totalWidth, int16_t xStart, int16_t yStart,
431+ uint16_t fgColor, uint16_t bgColor) {
432+
433+ // total width here is different to the width we are drawing, imagine rolling out a long
434+ // line of dough and cutting cookies from it. The cookie is the part of the image we want
435+ uint16_t byteWidth = (totalWidth + 7 ) / 8 ; // Bitmap scanline pad = whole byte
436+ uint16_t yEnd = h + yStart;
437+ uint16_t xEnd = w + xStart;
438+ uint8_t byte;
439+
440+ gfx->startWrite ();
441+
442+ for (uint16_t j = yStart; j < yEnd; j++, y++) {
443+ byte = bitmap[size_t (((j * byteWidth) + xStart) / 8 )];
444+ for (uint16_t i = xStart; i < xEnd; i++) {
445+ if (i & 7U )
446+ byte <<= 1U ;
447+ else
448+ byte = bitmap[size_t ((j * byteWidth) + i / 8 )];
449+ gfx->writePixel (x + (i - xStart), y, (byte & 0x80U ) ? fgColor : bgColor);
450+ }
451+ }
452+
453+ gfx->endWrite ();
454+ }
0 commit comments