sixel_blit: no big arrays on the stack #200

This commit is contained in:
nick black 2021-02-27 17:41:21 -05:00 committed by Nick Black
parent 74f7597d09
commit 7b29dd0925

View File

@ -855,7 +855,9 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize,
int visx = begx; int visx = begx;
for(x = placex ; visx < (begx + lenx) && x < dimx ; ++x, visx += 1){ for(x = placex ; visx < (begx + lenx) && x < dimx ; ++x, visx += 1){
size_t offset = 0; size_t offset = 0;
char sixel[128]; // FIXME #define GROWTHFACTOR 256
size_t avail = GROWTHFACTOR;
char* sixel = malloc(avail);
// FIXME find sixels with common colors for single register program // FIXME find sixels with common colors for single register program
unsigned bitsused = 0; // once 63, we're done unsigned bitsused = 0; // once 63, we're done
int colorreg = 1; // leave 0 as background int colorreg = 1; // leave 0 as background
@ -882,7 +884,8 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize,
char c = 63 + thesebits; char c = 63 + thesebits;
// FIXME use percentages(rgb) // FIXME use percentages(rgb)
// bitstring is added to 63, resulting in [63, 126] aka '?'..'~' // bitstring is added to 63, resulting in [63, 126] aka '?'..'~'
int n = snprintf(sixel + offset, sizeof(sixel) - offset, // FIXME grow if necessary
int n = snprintf(sixel + offset, avail - offset,
"%s#%d;2;%d;%d;%d#%d%c", printed ? "$" : "", "%s#%d;2;%d;%d;%d#%d%c", printed ? "$" : "",
colorreg, 100, 100, 100, colorreg, c); colorreg, 100, 100, 100, colorreg, c);
if(n < 0){ if(n < 0){
@ -899,13 +902,16 @@ sixel_blit(ncplane* nc, int placey, int placex, int linesize,
if(offset){ if(offset){
nccell* c = ncplane_cell_ref_yx(nc, y, x); nccell* c = ncplane_cell_ref_yx(nc, y, x);
if(pool_blit_direct(&nc->pool, c, sixel, offset, 1) <= 0){ if(pool_blit_direct(&nc->pool, c, sixel, offset, 1) <= 0){
free(sixel);
return -1; return -1;
} }
cell_set_pixels(c, 1); cell_set_pixels(c, 1);
} // FIXME otherwise, reset? } // FIXME otherwise, reset?
free(sixel);
} }
} }
return total; return total;
#undef GROWTHFACTOR
} }
// NCBLIT_DEFAULT is not included, as it has no defined properties. It ought // NCBLIT_DEFAULT is not included, as it has no defined properties. It ought