Skip to content

Commit

Permalink
vo_sixel: don't divide by zero on small terminal
Browse files Browse the repository at this point in the history
Our canvas size calculation is affected by few factors, and rounded
down more than once - which can result in 0 width or (more typically)
height - e.g. when terminal height is one row.

If the width or height are 0 then all bets are off, so simply skip
the setups and rendering on this case. We can still recover
automatically if the terminal is resized to become bigger.
  • Loading branch information
avih committed Dec 2, 2020
1 parent da48bb6 commit 84d0930
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions video/out/vo_sixel.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,10 @@ struct priv {
uint8_t *buffer;
bool skip_frame_draw;

// The dimensions that will be actually
// be used after processing user inputs
int top;
int left;
int width;
int height;
int num_rows;
int num_cols;
int left, top; // image origin cell (1 based)
int width, height; // actual image px size - always reflects dst_rect.
int num_cols, num_rows; // terminal size in cells
int canvas_ok; // whether canvas vo->dwidth and vo->dheight are positive

int previous_histgram_colors;

Expand Down Expand Up @@ -262,6 +258,8 @@ static void update_canvas_dimensions(struct vo *vo)

priv->num_rows = num_rows;
priv->num_cols = num_cols;

priv->canvas_ok = vo->dwidth > 0 && vo->dheight > 0;
}

static void set_sixel_output_parameters(struct vo *vo)
Expand Down Expand Up @@ -330,9 +328,13 @@ static int update_sixel_swscaler(struct vo *vo, struct mp_image_params *params)

static int reconfig(struct vo *vo, struct mp_image_params *params)
{
struct priv *priv = vo->priv;
int ret = 0;
update_canvas_dimensions(vo);
set_sixel_output_parameters(vo);
int ret = update_sixel_swscaler(vo, params);
if (priv->canvas_ok) { // if too small - succeed but skip the rendering
set_sixel_output_parameters(vo);
ret = update_sixel_swscaler(vo, params);
}

printf(ESC_CLEAR_SCREEN);
vo->want_redraw = true;
Expand All @@ -352,6 +354,8 @@ static void draw_frame(struct vo *vo, struct vo_frame *frame)
int prev_width = vo->dwidth;
bool resized = false;
update_canvas_dimensions(vo);
if (!priv->canvas_ok)
return;

if (prev_rows != priv->num_rows || prev_cols != priv->num_cols ||
prev_width != vo->dwidth || prev_height != vo->dheight)
Expand Down Expand Up @@ -426,6 +430,8 @@ static int sixel_write(char *data, int size, void *priv)
static void flip_page(struct vo *vo)
{
struct priv* priv = vo->priv;
if (!priv->canvas_ok)
return;

// If frame is repeated and no update required, then we skip encoding
if (priv->skip_frame_draw)
Expand Down

0 comments on commit 84d0930

Please sign in to comment.