Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Created February 13, 2018 09:10
Show Gist options
  • Save adrianparvino/3581c4bdcc6fe77cc34181a1813649b9 to your computer and use it in GitHub Desktop.
Save adrianparvino/3581c4bdcc6fe77cc34181a1813649b9 to your computer and use it in GitHub Desktop.
struct imagebuffer *
new_imagebuffer(size_t width, size_t height)
{
size_t size = width * height * color_type_to_bytes(DEFAULT_COLOR_TYPE);
struct imagebuffer *imagebuffer = malloc(sizeof(struct imagebuffer) + size);
*imagebuffer = (struct imagebuffer)
{
.width = width,
.height = height,
.color_type = DEFAULT_COLOR_TYPE,
.pixel_size = color_type_to_bytes(DEFAULT_COLOR_TYPE),
.buffer = imagebuffer->in_buffer
};
memset(imagebuffer->in_buffer, 0, size);
return imagebuffer;
}
void
render_fill(struct asciibuffer *dest,
const struct imagebuffer *src,
const char fontname[])
{
struct charset *font_charset = NULL;
if (strcmp(fontname, "") == 0)
{
fprintf(stderr, "Warning: no fontname provided. Using fallback.\n");
font_charset = charset_read_from_directory("./fonts/FixedsysExcelsior");
}
else
{
font_charset = charset_read_from_directory("./fonts/FixedsysExcelsior");
}
scale_bilinear((struct imagebuffer *) dest, src);
struct cache cache[font_charset->n];
for (size_t character = 0; character < font_charset->n; ++character)
{
// Calculate the arithmetic mean of the glyph
cache[character].value = 0;
for (size_t i = 0; i < font_charset->width * font_charset->height; ++i)
{
float dx = font_charset->characters[character].glyph[i] -
cache[character].value;
cache[character].value += dx / (i + 1);
}
cache[character].character =
font_charset->characters[character].character;
}
qsort(cache, font_charset->n, sizeof(*cache), cmp_cache);
for (size_t i = 0; i < dest->width; ++i)
{
for (size_t j = 0; j < dest->height; ++j)
{
for (size_t k = 0; k < LENGTH(cache); ++k)
{
if (*index_gray((struct imagebuffer *) dest, i, j) < cache[k].value)
{
*index_gray((struct imagebuffer *) dest, i, j) = cache[k].character;
break;
}
}
}
}
free_charset(font_charset);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment