Changeset c78a03d in mainline for uspace/lib/gfxfont/src/glyph_bmp.c
- Timestamp:
- 2020-07-21T22:48:59Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5592c56
- Parents:
- 703c743
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/src/glyph_bmp.c
r703c743 rc78a03d 35 35 36 36 #include <errno.h> 37 #include <gfx/coord.h> 37 38 #include <gfx/glyph_bmp.h> 38 39 #include <stdlib.h> 39 40 #include "../private/glyph_bmp.h" 41 42 static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *, gfx_coord2_t *); 43 44 /** Open glyph bitmap for editing. 45 * 46 * @param glyph Glyph 47 * @param rbmp Place to store glyph bitmap 48 * @return EOK on success, ENOMEM if out of memory 49 */ 50 errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp) 51 { 52 gfx_glyph_bmp_t *bmp; 53 54 bmp = calloc(1, sizeof(gfx_glyph_bmp_t)); 55 if (bmp == NULL) 56 return ENOMEM; 57 58 bmp->rect.p0.x = 0; 59 bmp->rect.p0.y = 0; 60 bmp->rect.p1.x = 0; 61 bmp->rect.p1.y = 0; 62 bmp->pixels = NULL; 63 64 bmp->glyph = glyph; 65 *rbmp = bmp; 66 return EOK; 67 } 40 68 41 69 /** Save glyph bitmap. … … 55 83 void gfx_glyph_bmp_close(gfx_glyph_bmp_t *bmp) 56 84 { 85 free(bmp->pixels); 86 free(bmp); 57 87 } 58 88 … … 66 96 int gfx_glyph_bmp_getpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x, gfx_coord_t y) 67 97 { 68 return 0; 98 gfx_coord2_t pos; 99 size_t pitch; 100 101 pos.x = x; 102 pos.y = y; 103 if (!gfx_pix_inside_rect(&pos, &bmp->rect)) 104 return 0; 105 106 pitch = bmp->rect.p1.x - bmp->rect.p0.x; 107 return bmp->pixels[(y - bmp->rect.p0.y) * pitch + 108 (x - bmp->rect.p0.x)]; 69 109 } 70 110 … … 80 120 gfx_coord_t y, int value) 81 121 { 122 gfx_coord2_t pos; 123 size_t pitch; 124 errno_t rc; 125 126 pos.x = x; 127 pos.y = y; 128 if (!gfx_pix_inside_rect(&pos, &bmp->rect)) { 129 rc = gfx_glyph_bmp_extend(bmp, &pos); 130 if (rc != EOK) 131 return rc; 132 } 133 134 pitch = bmp->rect.p1.x - bmp->rect.p0.x; 135 bmp->pixels[(y - bmp->rect.p0.y) * pitch + 136 (x - bmp->rect.p0.x)] = value; 137 return EOK; 138 } 139 140 /** Extend glyph bitmap to cover a patricular pixel. 141 * 142 * @param bmp Glyph bitmap 143 * @param pos Pixel position 144 * 145 * @return EOK on sucesss, ENOMEM if out of memory 146 */ 147 static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *bmp, gfx_coord2_t *pos) 148 { 149 gfx_rect_t prect; 150 gfx_rect_t nrect; 151 int *npixels; 152 size_t npitch; 153 size_t opitch; 154 gfx_coord_t x, y; 155 156 /* Compute new rectangle enveloping current rectangle and new pixel */ 157 prect.p0 = *pos; 158 prect.p1.x = prect.p0.x + 1; 159 prect.p1.y = prect.p0.y + 1; 160 161 gfx_rect_envelope(&bmp->rect, &prect, &nrect); 162 163 /* Allocate new pixel array */ 164 npixels = calloc(sizeof(int), (nrect.p1.x - nrect.p0.x) * 165 (nrect.p1.y - nrect.p0.y)); 166 if (npixels == NULL) 167 return ENOMEM; 168 169 /* Transfer pixel data */ 170 opitch = bmp->rect.p1.x - bmp->rect.p0.x; 171 npitch = nrect.p1.x - nrect.p0.x; 172 173 for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) { 174 for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) { 175 npixels[(y - nrect.p0.y) * npitch + x - nrect.p0.x] = 176 bmp->pixels[(y - bmp->rect.p0.y) * opitch + 177 x - bmp->rect.p0.x]; 178 } 179 } 180 181 /* Switch new and old data */ 182 free(bmp->pixels); 183 bmp->pixels = npixels; 184 bmp->rect = nrect; 185 82 186 return EOK; 83 187 }
Note:
See TracChangeset
for help on using the changeset viewer.