HelenOS sources
This source file includes following definitions.
- gfx_glyph_bmp_open
- gfx_glyph_bmp_save
- gfx_glyph_bmp_close
- gfx_glyph_bmp_get_rect
- gfx_glyph_bmp_find_used_rect
- gfx_glyph_bmp_getpix
- gfx_glyph_bmp_setpix
- gfx_glyph_bmp_clear
- gfx_glyph_bmp_extend
#include <errno.h>
#include <gfx/bitmap.h>
#include <gfx/coord.h>
#include <gfx/glyph_bmp.h>
#include <io/pixelmap.h>
#include <stdlib.h>
#include "../private/font.h"
#include "../private/glyph.h"
#include "../private/glyph_bmp.h"
static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *, gfx_coord2_t *);
errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp)
{
gfx_font_t *font = glyph->font;
gfx_glyph_bmp_t *bmp;
pixelmap_t pmap;
gfx_bitmap_alloc_t alloc;
gfx_coord_t x, y;
pixel_t pixel;
errno_t rc;
bmp = calloc(1, sizeof(gfx_glyph_bmp_t));
if (bmp == NULL)
return ENOMEM;
gfx_rect_rtranslate(&glyph->origin, &glyph->rect, &bmp->rect);
bmp->pixels = calloc((bmp->rect.p1.x - bmp->rect.p0.x) *
(bmp->rect.p1.y - bmp->rect.p0.y), sizeof(int));
if (bmp->pixels == NULL) {
free(bmp);
return ENOMEM;
}
rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
if (rc != EOK) {
free(bmp->pixels);
free(bmp);
return rc;
}
assert(font->rect.p0.x == 0);
assert(font->rect.p0.y == 0);
pmap.width = font->rect.p1.x;
pmap.height = font->rect.p1.y;
pmap.data = alloc.pixels;
for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
pixel = pixelmap_get_pixel(&pmap, glyph->origin.x + x,
glyph->origin.y + y);
bmp->pixels[(y - bmp->rect.p0.y) *
(bmp->rect.p1.x - bmp->rect.p0.x) +
(x - bmp->rect.p0.x)] = (pixel != 0) ? 1 : 0;
}
}
bmp->glyph = glyph;
*rbmp = bmp;
return EOK;
}
errno_t gfx_glyph_bmp_save(gfx_glyph_bmp_t *bmp)
{
gfx_glyph_t *glyph = bmp->glyph;
gfx_font_t *font = glyph->font;
gfx_coord_t x, y;
gfx_rect_t used_rect;
pixel_t pixel;
pixelmap_t pmap;
gfx_bitmap_alloc_t alloc;
errno_t rc;
gfx_glyph_bmp_find_used_rect(bmp, &used_rect);
rc = gfx_font_splice_at_glyph(font, glyph, &used_rect);
if (rc != EOK)
return rc;
rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
if (rc != EOK)
return rc;
assert(font->rect.p0.x == 0);
assert(font->rect.p0.y == 0);
pmap.width = font->rect.p1.x;
pmap.height = font->rect.p1.y;
pmap.data = alloc.pixels;
for (y = used_rect.p0.y; y < used_rect.p1.y; y++) {
for (x = used_rect.p0.x; x < used_rect.p1.x; x++) {
pixel = bmp->pixels[(y - bmp->rect.p0.y) *
(bmp->rect.p1.x - bmp->rect.p0.x) +
(x - bmp->rect.p0.x)] ?
PIXEL(255, 255, 255, 255) : PIXEL(0, 0, 0, 0);
pixelmap_put_pixel(&pmap, glyph->origin.x + x,
glyph->origin.y + y, pixel);
}
}
return EOK;
}
void gfx_glyph_bmp_close(gfx_glyph_bmp_t *bmp)
{
free(bmp->pixels);
free(bmp);
}
void gfx_glyph_bmp_get_rect(gfx_glyph_bmp_t *bmp, gfx_rect_t *rect)
{
*rect = bmp->rect;
}
void gfx_glyph_bmp_find_used_rect(gfx_glyph_bmp_t *bmp, gfx_rect_t *rect)
{
gfx_coord_t x, y;
gfx_coord2_t min;
gfx_coord2_t max;
bool anypix;
int pix;
min.x = bmp->rect.p1.x;
min.y = bmp->rect.p1.y;
max.x = bmp->rect.p0.x;
max.y = bmp->rect.p0.y;
anypix = false;
for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
pix = gfx_glyph_bmp_getpix(bmp, x, y);
if (pix != 0) {
anypix = true;
if (x < min.x)
min.x = x;
if (y < min.y)
min.y = y;
if (x > max.x)
max.x = x;
if (y > max.y)
max.y = y;
}
}
}
if (anypix) {
rect->p0.x = min.x;
rect->p0.y = min.y;
rect->p1.x = max.x + 1;
rect->p1.y = max.y + 1;
} else {
rect->p0.x = 0;
rect->p0.y = 0;
rect->p1.x = 0;
rect->p1.y = 0;
}
}
int gfx_glyph_bmp_getpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x, gfx_coord_t y)
{
gfx_coord2_t pos;
size_t pitch;
pos.x = x;
pos.y = y;
if (!gfx_pix_inside_rect(&pos, &bmp->rect))
return 0;
pitch = bmp->rect.p1.x - bmp->rect.p0.x;
return bmp->pixels[(y - bmp->rect.p0.y) * pitch +
(x - bmp->rect.p0.x)];
}
errno_t gfx_glyph_bmp_setpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x,
gfx_coord_t y, int value)
{
gfx_coord2_t pos;
size_t pitch;
errno_t rc;
pos.x = x;
pos.y = y;
if (!gfx_pix_inside_rect(&pos, &bmp->rect)) {
rc = gfx_glyph_bmp_extend(bmp, &pos);
if (rc != EOK)
return rc;
}
pitch = bmp->rect.p1.x - bmp->rect.p0.x;
bmp->pixels[(y - bmp->rect.p0.y) * pitch +
(x - bmp->rect.p0.x)] = value;
return EOK;
}
errno_t gfx_glyph_bmp_clear(gfx_glyph_bmp_t *bmp)
{
int *npixels;
npixels = calloc(sizeof(int), 1);
if (npixels == NULL)
return ENOMEM;
free(bmp->pixels);
bmp->pixels = npixels;
bmp->rect.p0.x = 0;
bmp->rect.p0.y = 0;
bmp->rect.p1.x = 0;
bmp->rect.p1.y = 0;
return EOK;
}
static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *bmp, gfx_coord2_t *pos)
{
gfx_rect_t prect;
gfx_rect_t nrect;
int *npixels;
size_t npitch;
size_t opitch;
gfx_coord_t x, y;
prect.p0 = *pos;
prect.p1.x = prect.p0.x + 1;
prect.p1.y = prect.p0.y + 1;
gfx_rect_envelope(&bmp->rect, &prect, &nrect);
npixels = calloc(sizeof(int), (nrect.p1.x - nrect.p0.x) *
(nrect.p1.y - nrect.p0.y));
if (npixels == NULL)
return ENOMEM;
opitch = bmp->rect.p1.x - bmp->rect.p0.x;
npitch = nrect.p1.x - nrect.p0.x;
for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
npixels[(y - nrect.p0.y) * npitch + x - nrect.p0.x] =
bmp->pixels[(y - bmp->rect.p0.y) * opitch +
x - bmp->rect.p0.x];
}
}
free(bmp->pixels);
bmp->pixels = npixels;
bmp->rect = nrect;
return EOK;
}
HelenOS homepage, sources at GitHub