Changeset d2100e2 in mainline for uspace/lib/gfxfont/test/glyph_bmp.c
- Timestamp:
- 2020-08-09T18:40:28Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 06b8383
- Parents:
- 5592c56
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/test/glyph_bmp.c
r5592c56 rd2100e2 39 39 static errno_t testgc_set_color(void *, gfx_color_t *); 40 40 static errno_t testgc_fill_rect(void *, gfx_rect_t *); 41 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *, 42 gfx_bitmap_alloc_t *, void **); 43 static errno_t testgc_bitmap_destroy(void *); 44 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *); 45 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *); 41 46 42 47 static gfx_context_ops_t test_ops = { 43 48 .set_color = testgc_set_color, 44 .fill_rect = testgc_fill_rect 49 .fill_rect = testgc_fill_rect, 50 .bitmap_create = testgc_bitmap_create, 51 .bitmap_destroy = testgc_bitmap_destroy, 52 .bitmap_render = testgc_bitmap_render, 53 .bitmap_get_alloc = testgc_bitmap_get_alloc 45 54 }; 55 56 typedef struct { 57 gfx_bitmap_params_t bm_params; 58 void *bm_pixels; 59 gfx_rect_t bm_srect; 60 gfx_coord2_t bm_offs; 61 } test_gc_t; 62 63 typedef struct { 64 test_gc_t *tgc; 65 gfx_bitmap_alloc_t alloc; 66 bool myalloc; 67 } testgc_bitmap_t; 46 68 47 69 /** Test opening and closing glyph bitmap */ … … 54 76 gfx_context_t *gc; 55 77 gfx_glyph_bmp_t *bmp; 78 test_gc_t tgc; 56 79 errno_t rc; 57 80 58 rc = gfx_context_new(&test_ops, NULL, &gc);81 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc); 59 82 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 60 83 … … 86 109 /** Test glyph_bmp_save() */ 87 110 PCUT_TEST(save) 88 {89 gfx_font_metrics_t fmetrics;90 gfx_font_t *font;91 gfx_glyph_metrics_t gmetrics;92 gfx_glyph_t *glyph;93 gfx_context_t *gc;94 gfx_glyph_bmp_t *bmp;95 errno_t rc;96 97 rc = gfx_context_new(&test_ops, NULL, &gc);98 PCUT_ASSERT_ERRNO_VAL(EOK, rc);99 100 gfx_font_metrics_init(&fmetrics);101 rc = gfx_font_create(gc, &fmetrics, &font);102 PCUT_ASSERT_ERRNO_VAL(EOK, rc);103 104 gfx_glyph_metrics_init(&gmetrics);105 gmetrics.advance = 1;106 107 rc = gfx_glyph_create(font, &gmetrics, &glyph);108 PCUT_ASSERT_ERRNO_VAL(EOK, rc);109 110 bmp = NULL;111 112 rc = gfx_glyph_bmp_open(glyph, &bmp);113 PCUT_ASSERT_ERRNO_VAL(EOK, rc);114 PCUT_ASSERT_NOT_NULL(bmp);115 116 /* TODO: Need a test to verify that save actually worked */117 rc = gfx_glyph_bmp_save(bmp);118 PCUT_ASSERT_ERRNO_VAL(EOK, rc);119 120 gfx_glyph_bmp_close(bmp);121 122 gfx_glyph_destroy(glyph);123 124 gfx_font_destroy(font);125 rc = gfx_context_delete(gc);126 PCUT_ASSERT_ERRNO_VAL(EOK, rc);127 }128 129 /** Test glyph_bmp_getpix() */130 PCUT_TEST(getpix)131 111 { 132 112 gfx_font_metrics_t fmetrics; … … 137 117 gfx_glyph_bmp_t *bmp; 138 118 int pix; 119 test_gc_t tgc; 139 120 errno_t rc; 140 121 141 rc = gfx_context_new(&test_ops, NULL, &gc);122 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc); 142 123 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 143 124 … … 154 135 bmp = NULL; 155 136 137 /* Open bitmap and set some pixels */ 138 139 rc = gfx_glyph_bmp_open(glyph, &bmp); 140 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 141 PCUT_ASSERT_NOT_NULL(bmp); 142 143 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1); 144 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 145 146 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1); 147 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 148 149 rc = gfx_glyph_bmp_save(bmp); 150 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 151 152 gfx_glyph_bmp_close(bmp); 153 bmp = NULL; 154 155 /* Re-open the saved bimap and verify pixel values were preserved */ 156 156 157 rc = gfx_glyph_bmp_open(glyph, &bmp); 157 158 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 159 160 160 161 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 162 PCUT_ASSERT_INT_EQUALS(1, pix); 163 164 pix = gfx_glyph_bmp_getpix(bmp, 1, 1); 165 PCUT_ASSERT_INT_EQUALS(1, pix); 166 167 pix = gfx_glyph_bmp_getpix(bmp, 1, 0); 161 168 PCUT_ASSERT_INT_EQUALS(0, pix); 162 169 163 gfx_glyph_bmp_close(bmp); 164 170 pix = gfx_glyph_bmp_getpix(bmp, 0, 1); 171 PCUT_ASSERT_INT_EQUALS(0, pix); 172 173 gfx_glyph_bmp_close(bmp); 165 174 gfx_glyph_destroy(glyph); 166 175 … … 170 179 } 171 180 172 /** Test glyph_bmp_ setpix() can flip pixel value both ways*/173 PCUT_TEST( setpix_flip)181 /** Test glyph_bmp_getpix() */ 182 PCUT_TEST(getpix) 174 183 { 175 184 gfx_font_metrics_t fmetrics; … … 179 188 gfx_context_t *gc; 180 189 gfx_glyph_bmp_t *bmp; 190 test_gc_t tgc; 181 191 int pix; 182 192 errno_t rc; 183 193 184 rc = gfx_context_new(&test_ops, NULL, &gc); 194 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc); 195 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 196 197 gfx_font_metrics_init(&fmetrics); 198 rc = gfx_font_create(gc, &fmetrics, &font); 199 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 200 201 gfx_glyph_metrics_init(&gmetrics); 202 gmetrics.advance = 1; 203 204 rc = gfx_glyph_create(font, &gmetrics, &glyph); 205 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 206 207 bmp = NULL; 208 209 rc = gfx_glyph_bmp_open(glyph, &bmp); 210 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 211 PCUT_ASSERT_NOT_NULL(bmp); 212 213 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 214 PCUT_ASSERT_INT_EQUALS(0, pix); 215 216 gfx_glyph_bmp_close(bmp); 217 218 gfx_glyph_destroy(glyph); 219 220 gfx_font_destroy(font); 221 rc = gfx_context_delete(gc); 222 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 223 } 224 225 /** Test glyph_bmp_setpix() can flip pixel value both ways */ 226 PCUT_TEST(setpix_flip) 227 { 228 gfx_font_metrics_t fmetrics; 229 gfx_font_t *font; 230 gfx_glyph_metrics_t gmetrics; 231 gfx_glyph_t *glyph; 232 gfx_context_t *gc; 233 gfx_glyph_bmp_t *bmp; 234 test_gc_t tgc; 235 int pix; 236 errno_t rc; 237 238 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc); 185 239 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 186 240 … … 235 289 gfx_glyph_bmp_t *bmp; 236 290 gfx_coord_t x, y; 291 test_gc_t tgc; 237 292 int pix; 238 293 errno_t rc; 239 294 240 rc = gfx_context_new(&test_ops, NULL, &gc);295 rc = gfx_context_new(&test_ops, (void *) &tgc, &gc); 241 296 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 242 297 … … 303 358 } 304 359 360 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params, 361 gfx_bitmap_alloc_t *alloc, void **rbm) 362 { 363 test_gc_t *tgc = (test_gc_t *) arg; 364 testgc_bitmap_t *tbm; 365 366 tbm = calloc(1, sizeof(testgc_bitmap_t)); 367 if (tbm == NULL) 368 return ENOMEM; 369 370 if (alloc == NULL) { 371 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) * 372 sizeof(uint32_t); 373 tbm->alloc.off0 = 0; 374 tbm->alloc.pixels = calloc(sizeof(uint32_t), 375 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y)); 376 tbm->myalloc = true; 377 if (tbm->alloc.pixels == NULL) { 378 free(tbm); 379 return ENOMEM; 380 } 381 } else { 382 tbm->alloc = *alloc; 383 } 384 385 tbm->tgc = tgc; 386 tgc->bm_params = *params; 387 tgc->bm_pixels = tbm->alloc.pixels; 388 *rbm = (void *)tbm; 389 return EOK; 390 } 391 392 static errno_t testgc_bitmap_destroy(void *bm) 393 { 394 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 395 if (tbm->myalloc) 396 free(tbm->alloc.pixels); 397 free(tbm); 398 return EOK; 399 } 400 401 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect, 402 gfx_coord2_t *offs) 403 { 404 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 405 tbm->tgc->bm_srect = *srect; 406 tbm->tgc->bm_offs = *offs; 407 return EOK; 408 } 409 410 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc) 411 { 412 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 413 *alloc = tbm->alloc; 414 return EOK; 415 } 416 305 417 PCUT_EXPORT(glyph_bmp);
Note:
See TracChangeset
for help on using the changeset viewer.