sff: automatically scale pixel value to 0..=255
This commit is contained in:
parent
5764b6fb09
commit
d33bed4eb3
4 changed files with 45 additions and 7 deletions
|
|
@ -70,18 +70,40 @@ struct sff_glyph_offset {
|
||||||
#define SFF_GLYPH_FOUND(data) ((data).off != 0)
|
#define SFF_GLYPH_FOUND(data) ((data).off != 0)
|
||||||
|
|
||||||
/// Decodes a sff file which is entirely in memory.
|
/// Decodes a sff file which is entirely in memory.
|
||||||
|
///
|
||||||
/// Fills the `out` structure with data from the file.
|
/// Fills the `out` structure with data from the file.
|
||||||
|
///
|
||||||
/// Returns whether decoding was successful or not.
|
/// Returns whether decoding was successful or not.
|
||||||
bool sff_decode(const uint8_t *file, size_t file_len, struct sff_file *out);
|
bool sff_decode(const uint8_t *file, size_t file_len, struct sff_file *out);
|
||||||
|
|
||||||
/// Searches for a glyph inside the file by its glyph value.
|
/// Searches for a glyph inside the file by its glyph value.
|
||||||
|
///
|
||||||
/// Returns an opaque handle when the glyph is found, or a sentinel value otherwise.
|
/// Returns an opaque handle when the glyph is found, or a sentinel value otherwise.
|
||||||
/// Use the `SFF_GLYPH_FOUND` macro to differentiate the two cases.
|
/// Use the `SFF_GLYPH_FOUND` macro to differentiate the two cases.
|
||||||
struct sff_glyph_offset sff_find_glyph(const struct sff_file *file, uint32_t glyph);
|
struct sff_glyph_offset sff_find_glyph(const struct sff_file *file, uint32_t glyph);
|
||||||
|
|
||||||
/// Returns the pixel value of the glyph indexed by `off`, in the specified row and column.
|
/// Returns the raw pixel value of the glyph indexed by `off`, in the specified row and column.
|
||||||
|
///
|
||||||
|
/// Use the offset from `sff_find_glyph`.
|
||||||
|
///
|
||||||
|
/// "Raw" in this case means unscaled, so the range depends on the encoding.
|
||||||
|
///
|
||||||
/// Panics if `row` or `col` are outside the valid range.
|
/// Panics if `row` or `col` are outside the valid range.
|
||||||
uint8_t sff_get_pixel_value(
|
uint8_t sff_get_pixel_value_raw(
|
||||||
|
const struct sff_file *file,
|
||||||
|
struct sff_glyph_offset off,
|
||||||
|
uint16_t row,
|
||||||
|
uint16_t col);
|
||||||
|
|
||||||
|
/// Returns the scaled pixel value of the glyph indexed by `off`, in the specified row and column.
|
||||||
|
///
|
||||||
|
/// Use the offset from `sff_find_glyph`.
|
||||||
|
///
|
||||||
|
/// Independent of encoding, the value will be in the range from 0 to 255, inclusive.
|
||||||
|
/// The lowest value possible in the encoding is mapped to 0, the highest to 255.
|
||||||
|
///
|
||||||
|
/// Panics if `row` or `col` are outside the valid range.
|
||||||
|
uint8_t sff_get_pixel_value_scaled(
|
||||||
const struct sff_file *file,
|
const struct sff_file *file,
|
||||||
struct sff_glyph_offset off,
|
struct sff_glyph_offset off,
|
||||||
uint16_t row,
|
uint16_t row,
|
||||||
|
|
|
||||||
|
|
@ -447,6 +447,9 @@ ram_init(void)
|
||||||
ment++;
|
ment++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// positioned here because we use public functionality below
|
||||||
|
ram_initialized = true;
|
||||||
|
|
||||||
// HACK by luck the frame at phys. addr 0 might be returned by the allocator.
|
// HACK by luck the frame at phys. addr 0 might be returned by the allocator.
|
||||||
// Since the allocator always descends into the leftmost free child block,
|
// Since the allocator always descends into the leftmost free child block,
|
||||||
// that would happen on the very first allocation.
|
// that would happen on the very first allocation.
|
||||||
|
|
@ -455,6 +458,4 @@ ram_init(void)
|
||||||
// we allocate (and don't use) a single frame before allocating anything else.
|
// we allocate (and don't use) a single frame before allocating anything else.
|
||||||
struct ppn ppn_ignored;
|
struct ppn ppn_ignored;
|
||||||
ram_alloc_frame(&ppn_ignored, RAM_PAGE_NORMAL);
|
ram_alloc_frame(&ppn_ignored, RAM_PAGE_NORMAL);
|
||||||
|
|
||||||
ram_initialized = true;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/sff.c
18
src/sff.c
|
|
@ -73,7 +73,7 @@ struct sff_glyph_offset sff_find_glyph(const struct sff_file *file, uint32_t gly
|
||||||
return (struct sff_glyph_offset) { .off = 0 };
|
return (struct sff_glyph_offset) { .off = 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t sff_get_pixel_value(
|
uint8_t sff_get_pixel_value_raw(
|
||||||
const struct sff_file *file,
|
const struct sff_file *file,
|
||||||
struct sff_glyph_offset off,
|
struct sff_glyph_offset off,
|
||||||
uint16_t row,
|
uint16_t row,
|
||||||
|
|
@ -92,3 +92,19 @@ uint8_t sff_get_pixel_value(
|
||||||
default: UNREACHABLE();
|
default: UNREACHABLE();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t sff_get_pixel_value_scaled(
|
||||||
|
const struct sff_file *file,
|
||||||
|
struct sff_glyph_offset off,
|
||||||
|
uint16_t row,
|
||||||
|
uint16_t col)
|
||||||
|
{
|
||||||
|
uint8_t unscaled = sff_get_pixel_value_raw(file, off, row, col);
|
||||||
|
switch (file->encoding) {
|
||||||
|
case SFF_ENC_BITS_PER_PX_1: return (unsigned int)unscaled * 255;
|
||||||
|
case SFF_ENC_BITS_PER_PX_2: return ((unsigned int)unscaled * 255) / 3;
|
||||||
|
case SFF_ENC_BITS_PER_PX_4: return ((unsigned int)unscaled * 255) / 15;
|
||||||
|
case SFF_ENC_BITS_PER_PX_8: return unscaled;
|
||||||
|
default: UNREACHABLE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,7 @@ void window_glyph_to_framebuffer_norefresh(struct window *win,
|
||||||
for (unsigned int px_col = 0; px_col < font_file.char_width; px_col++) {
|
for (unsigned int px_col = 0; px_col < font_file.char_width; px_col++) {
|
||||||
unsigned int x = cx + px_col;
|
unsigned int x = cx + px_col;
|
||||||
unsigned int y = cy + px_row;
|
unsigned int y = cy + px_row;
|
||||||
uint8_t px_value = sff_get_pixel_value(&font_file, off, px_row, px_col);
|
uint8_t brightness = sff_get_pixel_value_scaled(&font_file, off, px_row, px_col);
|
||||||
uint8_t brightness = 255 * (unsigned int)px_value / 15;
|
|
||||||
if (invert_color) {
|
if (invert_color) {
|
||||||
brightness = 255 - brightness;
|
brightness = 255 - brightness;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue