sixel: use a struct coord for current sixel position

This commit is contained in:
Daniel Eklöf 2020-02-22 10:54:52 +01:00
parent 1b728dcac8
commit f193695960
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 33 additions and 35 deletions

46
sixel.c
View file

@ -20,8 +20,7 @@ sixel_init(struct terminal *term)
assert(term->sixel.image.data == NULL);
term->sixel.state = SIXEL_GROUND;
term->sixel.row = 0;
term->sixel.col = 0;
term->sixel.pos = (struct coord){0, 0};
term->sixel.color_idx = 0;
term->sixel.max_col = 0;
term->sixel.param = 0;
@ -53,16 +52,16 @@ sixel_unhook(struct terminal *term)
free(term->sixel.palette);
term->sixel.palette = NULL;
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.row++;
term->sixel.col = 0;
if (term->sixel.pos.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.pos.col;
term->sixel.pos.row++;
term->sixel.pos.col = 0;
struct sixel image = {
.data = term->sixel.image.data,
.width = term->sixel.max_col,
.height = term->sixel.row * 6,
.rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height,
.height = term->sixel.pos.row * 6,
.rows = (term->sixel.pos.row * 6 + term->cell_height - 1) / term->cell_height,
.pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row},
};
@ -87,8 +86,7 @@ sixel_unhook(struct terminal *term)
term->sixel.image.width = 0;
term->sixel.image.height = 0;
term->sixel.max_col = 0;
term->sixel.col = 0;
term->sixel.row = 0;
term->sixel.pos = (struct coord){0, 0};
const size_t lines = (image.height + term->cell_height - 1) / term->cell_height;
for (size_t i = 0; i < lines; i++)
@ -156,26 +154,26 @@ sixel_add(struct terminal *term, uint32_t color, uint8_t sixel)
{
//LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color);
if (term->sixel.col >= term->sixel.image.width ||
term->sixel.row * 6 >= term->sixel.image.height)
if (term->sixel.pos.col >= term->sixel.image.width ||
term->sixel.pos.row * 6 >= term->sixel.image.height)
{
resize(term,
max(term->sixel.max_col, term->sixel.col + 1),
(term->sixel.row + 1) * 6);
max(term->sixel.max_col, term->sixel.pos.col + 1),
(term->sixel.pos.row + 1) * 6);
}
for (int i = 0; i < 6; i++, sixel >>= 1) {
if (sixel & 1) {
size_t pixel_row = term->sixel.row * 6 + i;
size_t pixel_row = term->sixel.pos.row * 6 + i;
size_t stride = term->sixel.image.width;
size_t idx = pixel_row * stride + term->sixel.col;
size_t idx = pixel_row * stride + term->sixel.pos.col;
term->sixel.image.data[idx] = term->colors.alpha / 256 << 24 | color;
}
}
assert(sixel == 0);
term->sixel.col++;
term->sixel.pos.col++;
}
static void
@ -196,16 +194,16 @@ sixel_sixel(struct terminal *term, uint8_t c)
break;
case '$':
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.col = 0;
if (term->sixel.pos.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.pos.col;
term->sixel.pos.col = 0;
break;
case '-':
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.row++;
term->sixel.col = 0;
if (term->sixel.pos.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.pos.col;
term->sixel.pos.row++;
term->sixel.pos.col = 0;
break;
case '!':

View file

@ -356,21 +356,21 @@ struct terminal {
struct {
enum { SIXEL_GROUND, SIXEL_REPEAT, SIXEL_RASTER, SIXEL_COLOR, SIXEL_COLOR_SPEC} state;
int row;
int col;
int color_idx;
int max_col;
unsigned params[4];
uint32_t *palette;
struct coord pos; /* Current sixel coordinate */
int color_idx; /* Current palette index */
int max_col; /* Largest column index we've seen (aka the image width) */
uint32_t *palette; /* Color palette */
struct {
uint32_t *data;
int width;
int height;
uint32_t *data; /* Raw image data, in ARGB */
int width; /* Image width, in pixels */
int height; /* Image height, in pixels */
} image;
unsigned int param;
unsigned param_idx;
unsigned params[4]; /* Collected parmaeters, for RASTER, COLOR_SPEC */
unsigned int param; /* Currently collecting parameter, for RASTER, COLOR_SPEC and REPEAT */
unsigned param_idx; /* Parameters seen */
} sixel;
tll(struct sixel) sixel_images;