Changeset f13f1495 in mainline for uspace/lib/gfxfont/src
- Timestamp:
- 2020-09-29T14:58:57Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- efe0881
- Parents:
- a81d480
- git-author:
- Jiri Svoboda <jiri@…> (2020-09-28 19:58:39)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-09-29 14:58:57)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/src/font.c
ra81d480 rf13f1495 519 519 } 520 520 521 /** Bit pack font bitmap into 1 bits/pixel format. 522 * 523 * @param width Bitmap width 524 * @param height Bitmap height 525 * @param pixels Bitmap pixels 526 * @param rdata Place to store pointer to packed data 527 * @param rsize Place to store size of packed data 528 * @return EOK on sucess, ENOMEM if out of memory 529 */ 530 errno_t gfx_font_bitmap_pack(gfx_coord_t width, gfx_coord_t height, 531 uint32_t *pixels, void **rdata, size_t *rsize) 532 { 533 void *data; 534 size_t size; 535 size_t bytes_line; 536 gfx_coord_t x, y; 537 uint8_t b; 538 uint8_t *dp; 539 uint32_t *sp; 540 uint32_t pix; 541 542 bytes_line = (width + 7) / 8; 543 size = height * bytes_line; 544 545 data = malloc(size); 546 if (data == NULL) 547 return ENOMEM; 548 549 sp = pixels; 550 dp = data; 551 for (y = 0; y < height; y++) { 552 b = 0; 553 for (x = 0; x < width; x++) { 554 pix = *sp++; 555 b = (b << 1) | (pix & 1); 556 /* Write eight bits */ 557 if ((x & 7) == 7) { 558 *dp++ = b; 559 b = 0; 560 } 561 } 562 563 /* Write remaining bits */ 564 if ((x & 7) != 0) { 565 b = b << (8 - (x & 7)); 566 *dp++ = b; 567 } 568 } 569 570 *rdata = data; 571 *rsize = size; 572 return EOK; 573 } 574 575 /** Unpack font bitmap from 1 bits/pixel format. 576 * 577 * @param width Bitmap width 578 * @param height Bitmap height 579 * @param data Packed data 580 * @param dsize Size of packed data 581 * @param pixels Bitmap pixels 582 * @return EOK on success, EINVAL if data size is invalid 583 */ 584 errno_t gfx_font_bitmap_unpack(gfx_coord_t width, gfx_coord_t height, 585 void *data, size_t dsize, uint32_t *pixels) 586 { 587 size_t size; 588 size_t bytes_line; 589 gfx_coord_t x, y; 590 uint8_t b; 591 uint8_t *sp; 592 uint32_t *dp; 593 594 bytes_line = (width + 7) / 8; 595 size = height * bytes_line; 596 597 if (dsize != size) 598 return EINVAL; 599 600 sp = data; 601 dp = pixels; 602 for (y = 0; y < height; y++) { 603 b = 0; 604 for (x = 0; x < width; x++) { 605 if ((x & 7) == 0) 606 b = *sp++; 607 *dp++ = (b >> 7) ? PIXEL(255, 255, 255, 255) : 608 PIXEL(0, 0, 0, 0); 609 b = b << 1; 610 } 611 } 612 613 return EOK; 614 } 615 521 616 /** Load font bitmap from RIFF TPF file. 522 617 * … … 537 632 uint16_t fmt; 538 633 uint16_t depth; 634 size_t bytes_line; 635 void *data = NULL; 636 size_t size; 539 637 size_t nread; 540 638 … … 552 650 depth = uint16_t_le2host(thdr.depth); 553 651 554 if (fmt != 0 || depth != 8 * sizeof(uint32_t)) {652 if (fmt != 0 || depth != 1) { 555 653 rc = ENOTSUP; 654 goto error; 655 } 656 657 bytes_line = (width + 7) / 8; 658 size = height * bytes_line; 659 660 data = malloc(size); 661 if (data == NULL) { 662 rc = ENOMEM; 556 663 goto error; 557 664 } … … 571 678 goto error; 572 679 573 rc = riff_read(&bmpck, (void *) alloc.pixels, 574 width * height * sizeof(uint32_t), &nread); 575 if (rc != EOK) 576 goto error; 577 578 if (nread != width * height * sizeof(uint32_t)) { 680 rc = riff_read(&bmpck, data, size, &nread); 681 if (rc != EOK) 682 goto error; 683 684 if (nread != size) { 579 685 rc = EIO; 580 686 goto error; … … 585 691 goto error; 586 692 693 rc = gfx_font_bitmap_unpack(width, height, data, size, alloc.pixels); 694 if (rc != EOK) 695 goto error; 696 697 free(data); 587 698 gfx_bitmap_destroy(font->bitmap); 588 699 font->bitmap = bitmap; … … 590 701 return EOK; 591 702 error: 703 if (data != NULL) 704 free(data); 592 705 if (bitmap != NULL) 593 706 gfx_bitmap_destroy(bitmap); … … 607 720 riff_wchunk_t bmpck; 608 721 gfx_bitmap_alloc_t alloc; 722 void *data = NULL; 723 size_t dsize; 724 725 rc = gfx_bitmap_get_alloc(font->bitmap, &alloc); 726 if (rc != EOK) 727 return rc; 728 729 rc = gfx_font_bitmap_pack(font->rect.p1.x, font->rect.p1.y, 730 alloc.pixels, &data, &dsize); 731 if (rc != EOK) 732 return rc; 609 733 610 734 thdr.width = host2uint32_t_le(font->rect.p1.x); 611 735 thdr.height = host2uint32_t_le(font->rect.p1.y); 612 736 thdr.fmt = 0; 613 thdr.depth = host2uint16_t_le(8 * sizeof(uint32_t)); 614 615 rc = gfx_bitmap_get_alloc(font->bitmap, &alloc); 616 if (rc != EOK) 617 return rc; 737 thdr.depth = host2uint16_t_le(1); 618 738 619 739 rc = riff_wchunk_start(riffw, CKID_fbmp, &bmpck); 620 740 if (rc != EOK) 621 return rc;741 goto error; 622 742 623 743 rc = riff_write(riffw, &thdr, sizeof(thdr)); 624 744 if (rc != EOK) 625 return rc; 626 627 rc = riff_write(riffw, (void *) alloc.pixels, 628 font->rect.p1.x * font->rect.p1.y * sizeof(uint32_t)); 629 if (rc != EOK) 630 return rc; 745 goto error; 746 747 rc = riff_write(riffw, data, dsize); 748 if (rc != EOK) 749 goto error; 631 750 632 751 rc = riff_wchunk_end(riffw, &bmpck); 633 752 if (rc != EOK) 634 return rc; 635 636 return EOK; 753 goto error; 754 755 free(data); 756 return EOK; 757 error: 758 if (data != NULL) 759 free(data); 760 return rc; 637 761 } 638 762
Note:
See TracChangeset
for help on using the changeset viewer.