mirror of
https://github.com/dankamongmen/notcurses
synced 2025-03-09 17:19:03 -04:00
fade: factor out duplicated maxsteps determination #659
This commit is contained in:
parent
3ff083b73f
commit
0766dd08e7
@ -1,18 +1,19 @@
|
|||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
typedef struct planepalette {
|
typedef struct ncfadectx {
|
||||||
int rows; // number of rows when allocated
|
int rows; // number of rows when allocated
|
||||||
int cols; // number of columns when allocated
|
int cols; // number of columns when allocated
|
||||||
|
int maxsteps; // maximum number of iterations
|
||||||
unsigned maxr, maxg, maxb; // maxima across foreground channels
|
unsigned maxr, maxg, maxb; // maxima across foreground channels
|
||||||
unsigned maxbr, maxbg, maxbb; // maxima across background channels
|
unsigned maxbr, maxbg, maxbb; // maxima across background channels
|
||||||
uint64_t* channels; // all channels from the framebuffer
|
uint64_t* channels; // all channels from the framebuffer
|
||||||
} planepalette;
|
} ncfadectx;
|
||||||
|
|
||||||
// These arrays are too large to be safely placed on the stack. Get an atomic
|
// These arrays are too large to be safely placed on the stack. Get an atomic
|
||||||
// snapshot of all channels on the plane. While copying the snapshot, determine
|
// snapshot of all channels on the plane. While copying the snapshot, determine
|
||||||
// the maxima across each of the six components.
|
// the maxima across each of the six components.
|
||||||
static int
|
static int
|
||||||
alloc_ncplane_palette(ncplane* n, planepalette* pp){
|
alloc_ncplane_palette(ncplane* n, ncfadectx* pp){
|
||||||
ncplane_dim_yx(n, &pp->rows, &pp->cols);
|
ncplane_dim_yx(n, &pp->rows, &pp->cols);
|
||||||
// add an additional element for the background cell
|
// add an additional element for the background cell
|
||||||
int size = pp->rows * pp->cols + 1;
|
int size = pp->rows * pp->cols + 1;
|
||||||
@ -73,22 +74,22 @@ alloc_ncplane_palette(ncplane* n, planepalette* pp){
|
|||||||
if(bb > pp->maxbb){
|
if(bb > pp->maxbb){
|
||||||
pp->maxbb = bb;
|
pp->maxbb = bb;
|
||||||
}
|
}
|
||||||
|
int maxfsteps = pp->maxg > pp->maxr ? (pp->maxb > pp->maxg ? pp->maxb : pp->maxg) :
|
||||||
|
(pp->maxb > pp->maxr ? pp->maxb : pp->maxr);
|
||||||
|
int maxbsteps = pp->maxbg > pp->maxbr ? (pp->maxbb > pp->maxbg ? pp->maxbb : pp->maxbg) :
|
||||||
|
(pp->maxbb > pp->maxbr ? pp->maxbb : pp->maxbr);
|
||||||
|
pp->maxsteps = maxfsteps > maxbsteps ? maxfsteps : maxbsteps;
|
||||||
|
if(pp->maxsteps == 0){
|
||||||
|
pp->maxsteps = 1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ncplane_fadein_internal(ncplane* n, const struct timespec* ts,
|
ncplane_fadein_internal(ncplane* n, const struct timespec* ts,
|
||||||
fadecb fader, planepalette* pp, void* curry){
|
fadecb fader, ncfadectx* pp, void* curry){
|
||||||
int maxfsteps = pp->maxg > pp->maxr ? (pp->maxb > pp->maxg ? pp->maxb : pp->maxg) :
|
|
||||||
(pp->maxb > pp->maxr ? pp->maxb : pp->maxr);
|
|
||||||
int maxbsteps = pp->maxbg > pp->maxbr ? (pp->maxbb > pp->maxbg ? pp->maxbb : pp->maxbg) :
|
|
||||||
(pp->maxbb > pp->maxbr ? pp->maxbb : pp->maxbr);
|
|
||||||
int maxsteps = maxfsteps > maxbsteps ? maxfsteps : maxbsteps;
|
|
||||||
if(maxsteps == 0){
|
|
||||||
maxsteps = 1;
|
|
||||||
}
|
|
||||||
uint64_t nanosecs_total = ts->tv_sec * NANOSECS_IN_SEC + ts->tv_nsec;
|
uint64_t nanosecs_total = ts->tv_sec * NANOSECS_IN_SEC + ts->tv_nsec;
|
||||||
uint64_t nanosecs_step = nanosecs_total / maxsteps;
|
uint64_t nanosecs_step = nanosecs_total / pp->maxsteps;
|
||||||
if(nanosecs_step == 0){
|
if(nanosecs_step == 0){
|
||||||
nanosecs_step = 1;
|
nanosecs_step = 1;
|
||||||
}
|
}
|
||||||
@ -103,7 +104,7 @@ ncplane_fadein_internal(ncplane* n, const struct timespec* ts,
|
|||||||
clock_gettime(CLOCK_MONOTONIC, ×);
|
clock_gettime(CLOCK_MONOTONIC, ×);
|
||||||
curns = times.tv_sec * NANOSECS_IN_SEC + times.tv_nsec;
|
curns = times.tv_sec * NANOSECS_IN_SEC + times.tv_nsec;
|
||||||
int iter = (curns - startns) / nanosecs_step + 1;
|
int iter = (curns - startns) / nanosecs_step + 1;
|
||||||
if(iter > maxsteps){
|
if(iter > pp->maxsteps){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int y, x;
|
int y, x;
|
||||||
@ -119,15 +120,15 @@ ncplane_fadein_internal(ncplane* n, const struct timespec* ts,
|
|||||||
channels_bg_rgb(pp->channels[pp->cols * y + x], &br, &bg, &bb);
|
channels_bg_rgb(pp->channels[pp->cols * y + x], &br, &bg, &bb);
|
||||||
cell* c = &n->fb[dimx * y + x];
|
cell* c = &n->fb[dimx * y + x];
|
||||||
if(!cell_fg_default_p(c)){
|
if(!cell_fg_default_p(c)){
|
||||||
r = r * iter / maxsteps;
|
r = r * iter / pp->maxsteps;
|
||||||
g = g * iter / maxsteps;
|
g = g * iter / pp->maxsteps;
|
||||||
b = b * iter / maxsteps;
|
b = b * iter / pp->maxsteps;
|
||||||
cell_set_fg_rgb(c, r, g, b);
|
cell_set_fg_rgb(c, r, g, b);
|
||||||
}
|
}
|
||||||
if(!cell_bg_default_p(c)){
|
if(!cell_bg_default_p(c)){
|
||||||
br = br * iter / maxsteps;
|
br = br * iter / pp->maxsteps;
|
||||||
bg = bg * iter / maxsteps;
|
bg = bg * iter / pp->maxsteps;
|
||||||
bb = bb * iter / maxsteps;
|
bb = bb * iter / pp->maxsteps;
|
||||||
cell_set_bg_rgb(c, br, bg, bb);
|
cell_set_bg_rgb(c, br, bg, bb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,23 +153,15 @@ ncplane_fadein_internal(ncplane* n, const struct timespec* ts,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
||||||
planepalette pp;
|
ncfadectx pp;
|
||||||
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(alloc_ncplane_palette(n, &pp)){
|
if(alloc_ncplane_palette(n, &pp)){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int maxfsteps = pp.maxg > pp.maxr ? (pp.maxb > pp.maxg ? pp.maxb : pp.maxg) :
|
|
||||||
(pp.maxb > pp.maxr ? pp.maxb : pp.maxr);
|
|
||||||
int maxbsteps = pp.maxbg > pp.maxbr ? (pp.maxbb > pp.maxbg ? pp.maxbb : pp.maxbg) :
|
|
||||||
(pp.maxbb > pp.maxbr ? pp.maxbb : pp.maxbr);
|
|
||||||
int maxsteps = maxfsteps > maxbsteps ? maxfsteps : maxbsteps;
|
|
||||||
if(maxsteps == 0){
|
|
||||||
maxsteps = 1;
|
|
||||||
}
|
|
||||||
uint64_t nanosecs_total = timespec_to_ns(ts);
|
uint64_t nanosecs_total = timespec_to_ns(ts);
|
||||||
uint64_t nanosecs_step = nanosecs_total / maxsteps;
|
uint64_t nanosecs_step = nanosecs_total / pp.maxsteps;
|
||||||
if(nanosecs_step == 0){
|
if(nanosecs_step == 0){
|
||||||
nanosecs_step = 1;
|
nanosecs_step = 1;
|
||||||
}
|
}
|
||||||
@ -182,7 +175,7 @@ int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* c
|
|||||||
unsigned r, g, b;
|
unsigned r, g, b;
|
||||||
uint64_t curns = times.tv_sec * NANOSECS_IN_SEC + times.tv_nsec;
|
uint64_t curns = times.tv_sec * NANOSECS_IN_SEC + times.tv_nsec;
|
||||||
int iter = (curns - startns) / nanosecs_step + 1;
|
int iter = (curns - startns) / nanosecs_step + 1;
|
||||||
if(iter > maxsteps){
|
if(iter > pp.maxsteps){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int y, x;
|
int y, x;
|
||||||
@ -195,16 +188,16 @@ int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* c
|
|||||||
cell* c = &n->fb[dimx * y + x];
|
cell* c = &n->fb[dimx * y + x];
|
||||||
if(!cell_fg_default_p(c)){
|
if(!cell_fg_default_p(c)){
|
||||||
channels_fg_rgb(pp.channels[pp.cols * y + x], &r, &g, &b);
|
channels_fg_rgb(pp.channels[pp.cols * y + x], &r, &g, &b);
|
||||||
r = r * (maxsteps - iter) / maxsteps;
|
r = r * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
g = g * (maxsteps - iter) / maxsteps;
|
g = g * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
b = b * (maxsteps - iter) / maxsteps;
|
b = b * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
cell_set_fg_rgb(c, r, g, b);
|
cell_set_fg_rgb(c, r, g, b);
|
||||||
}
|
}
|
||||||
if(!cell_bg_default_p(c)){
|
if(!cell_bg_default_p(c)){
|
||||||
channels_bg_rgb(pp.channels[pp.cols * y + x], &br, &bg, &bb);
|
channels_bg_rgb(pp.channels[pp.cols * y + x], &br, &bg, &bb);
|
||||||
br = br * (maxsteps - iter) / maxsteps;
|
br = br * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
bg = bg * (maxsteps - iter) / maxsteps;
|
bg = bg * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
bb = bb * (maxsteps - iter) / maxsteps;
|
bb = bb * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
cell_set_bg_rgb(c, br, bg, bb);
|
cell_set_bg_rgb(c, br, bg, bb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -212,16 +205,16 @@ int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* c
|
|||||||
cell* c = &n->basecell;
|
cell* c = &n->basecell;
|
||||||
if(!cell_fg_default_p(c)){
|
if(!cell_fg_default_p(c)){
|
||||||
channels_fg_rgb(pp.channels[pp.cols * y], &r, &g, &b);
|
channels_fg_rgb(pp.channels[pp.cols * y], &r, &g, &b);
|
||||||
r = r * (maxsteps - iter) / maxsteps;
|
r = r * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
g = g * (maxsteps - iter) / maxsteps;
|
g = g * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
b = b * (maxsteps - iter) / maxsteps;
|
b = b * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
cell_set_fg_rgb(&n->basecell, r, g, b);
|
cell_set_fg_rgb(&n->basecell, r, g, b);
|
||||||
}
|
}
|
||||||
if(!cell_bg_default_p(c)){
|
if(!cell_bg_default_p(c)){
|
||||||
channels_bg_rgb(pp.channels[pp.cols * y], &br, &bg, &bb);
|
channels_bg_rgb(pp.channels[pp.cols * y], &br, &bg, &bb);
|
||||||
br = br * (maxsteps - iter) / maxsteps;
|
br = br * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
bg = bg * (maxsteps - iter) / maxsteps;
|
bg = bg * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
bb = bb * (maxsteps - iter) / maxsteps;
|
bb = bb * (pp.maxsteps - iter) / pp.maxsteps;
|
||||||
cell_set_bg_rgb(&n->basecell, br, bg, bb);
|
cell_set_bg_rgb(&n->basecell, br, bg, bb);
|
||||||
}
|
}
|
||||||
uint64_t nextwake = (iter + 1) * nanosecs_step + startns;
|
uint64_t nextwake = (iter + 1) * nanosecs_step + startns;
|
||||||
@ -250,7 +243,7 @@ int ncplane_fadeout(ncplane* n, const struct timespec* ts, fadecb fader, void* c
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ncplane_fadein(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
int ncplane_fadein(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
||||||
planepalette pp;
|
ncfadectx pp;
|
||||||
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
@ -270,7 +263,7 @@ int ncplane_fadein(ncplane* n, const struct timespec* ts, fadecb fader, void* cu
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ncplane_pulse(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
int ncplane_pulse(ncplane* n, const struct timespec* ts, fadecb fader, void* curry){
|
||||||
planepalette pp;
|
ncfadectx pp;
|
||||||
int ret;
|
int ret;
|
||||||
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
if(!n->nc->tcache.RGBflag && !n->nc->tcache.CCCflag){ // terminal can't fade
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user