@@ -1758,12 +1758,21 @@ const uint8_t PROGMEM GFXcanvas1::GFXclrBit[] = {0x7F, 0xBF, 0xDF, 0xEF,
1758
1758
@brief Instatiate a GFX 1-bit canvas context for graphics
1759
1759
@param w Display width, in pixels
1760
1760
@param h Display height, in pixels
1761
- */
1762
- /* *************************************************************************/
1763
- GFXcanvas1::GFXcanvas1 (uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
1764
- uint32_t bytes = ((w + 7 ) / 8 ) * h;
1765
- if ((buffer = (uint8_t *)malloc (bytes))) {
1766
- memset (buffer, 0 , bytes);
1761
+ @param allocate_buffer If true, a buffer is allocated with malloc. If
1762
+ false, the subclass must initialize the buffer before any drawing operation,
1763
+ and free it in the destructor. If false (the default), the buffer is
1764
+ allocated and freed by the library.
1765
+ */
1766
+ /* *************************************************************************/
1767
+ GFXcanvas1::GFXcanvas1 (uint16_t w, uint16_t h, bool allocate_buffer)
1768
+ : Adafruit_GFX(w, h), buffer_owned(allocate_buffer) {
1769
+ if (allocate_buffer) {
1770
+ uint32_t bytes = ((w + 7 ) / 8 ) * h;
1771
+ if ((buffer = (uint8_t *)malloc (bytes))) {
1772
+ memset (buffer, 0 , bytes);
1773
+ }
1774
+ } else {
1775
+ buffer = nullptr ;
1767
1776
}
1768
1777
}
1769
1778
@@ -1773,7 +1782,7 @@ GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
1773
1782
*/
1774
1783
/* *************************************************************************/
1775
1784
GFXcanvas1::~GFXcanvas1 (void ) {
1776
- if (buffer)
1785
+ if (buffer && buffer_owned )
1777
1786
free (buffer);
1778
1787
}
1779
1788
@@ -2111,13 +2120,21 @@ void GFXcanvas1::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
2111
2120
@brief Instatiate a GFX 8-bit canvas context for graphics
2112
2121
@param w Display width, in pixels
2113
2122
@param h Display height, in pixels
2114
- */
2115
- /* *************************************************************************/
2116
- GFXcanvas8::GFXcanvas8 (uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
2117
- uint32_t bytes = w * h;
2118
- if ((buffer = (uint8_t *)malloc (bytes))) {
2119
- memset (buffer, 0 , bytes);
2120
- }
2123
+ @param allocate_buffer If true, a buffer is allocated with malloc. If
2124
+ false, the subclass must initialize the buffer before any drawing operation,
2125
+ and free it in the destructor. If false (the default), the buffer is
2126
+ allocated and freed by the library.
2127
+ */
2128
+ /* *************************************************************************/
2129
+ GFXcanvas8::GFXcanvas8 (uint16_t w, uint16_t h, bool allocate_buffer)
2130
+ : Adafruit_GFX(w, h), buffer_owned(allocate_buffer) {
2131
+ if (allocate_buffer) {
2132
+ uint32_t bytes = w * h;
2133
+ if ((buffer = (uint8_t *)malloc (bytes))) {
2134
+ memset (buffer, 0 , bytes);
2135
+ }
2136
+ } else
2137
+ buffer = nullptr ;
2121
2138
}
2122
2139
2123
2140
/* *************************************************************************/
@@ -2126,7 +2143,7 @@ GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
2126
2143
*/
2127
2144
/* *************************************************************************/
2128
2145
GFXcanvas8::~GFXcanvas8 (void ) {
2129
- if (buffer)
2146
+ if (buffer && buffer_owned )
2130
2147
free (buffer);
2131
2148
}
2132
2149
@@ -2379,12 +2396,21 @@ void GFXcanvas8::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
2379
2396
@brief Instatiate a GFX 16-bit canvas context for graphics
2380
2397
@param w Display width, in pixels
2381
2398
@param h Display height, in pixels
2382
- */
2383
- /* *************************************************************************/
2384
- GFXcanvas16::GFXcanvas16 (uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
2385
- uint32_t bytes = w * h * 2 ;
2386
- if ((buffer = (uint16_t *)malloc (bytes))) {
2387
- memset (buffer, 0 , bytes);
2399
+ @param allocate_buffer If true, a buffer is allocated with malloc. If
2400
+ false, the subclass must initialize the buffer before any drawing operation,
2401
+ and free it in the destructor. If false (the default), the buffer is
2402
+ allocated and freed by the library.
2403
+ */
2404
+ /* *************************************************************************/
2405
+ GFXcanvas16::GFXcanvas16 (uint16_t w, uint16_t h, bool allocate_buffer)
2406
+ : Adafruit_GFX(w, h), buffer_owned(allocate_buffer) {
2407
+ if (allocate_buffer) {
2408
+ uint32_t bytes = w * h * 2 ;
2409
+ if ((buffer = (uint16_t *)malloc (bytes))) {
2410
+ memset (buffer, 0 , bytes);
2411
+ }
2412
+ } else {
2413
+ buffer = nullptr ;
2388
2414
}
2389
2415
}
2390
2416
@@ -2394,7 +2420,7 @@ GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
2394
2420
*/
2395
2421
/* *************************************************************************/
2396
2422
GFXcanvas16::~GFXcanvas16 (void ) {
2397
- if (buffer)
2423
+ if (buffer && buffer_owned )
2398
2424
free (buffer);
2399
2425
}
2400
2426
0 commit comments