comparison lib64gfx.c @ 556:44d1e0d4acf3

Improve DMC64Image -> DMImage conversion facilities.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 23 Nov 2012 18:37:09 +0200
parents 3c968e027a58
children cf39f51d4e74
comparison
equal deleted inserted replaced
555:31652085440a 556:44d1e0d4acf3
698 } 698 }
699 699
700 700
701 // Convert a generic "C64" format bitmap in DMC64Image struct to 701 // Convert a generic "C64" format bitmap in DMC64Image struct to
702 // a indexed/paletted bitmap image. 702 // a indexed/paletted bitmap image.
703 int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src) 703 int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const BOOL doubleMC)
704 { 704 {
705 int yc;
706 Uint8 *dp = dst->data; 705 Uint8 *dp = dst->data;
707 706 int divisor = doubleMC ? 4 : 8,
708 for (yc = 0; yc < C64_SCR_HEIGHT; yc++) 707 wdivisor = doubleMC ? 2 : 1,
708 yc;
709
710 // Sanity check arguments
711 if (dst == NULL || src == NULL)
712 return DMERR_NULLPTR;
713
714 if (dst->width < 8)
715 return DMERR_INVALID_ARGS;
716
717 // Perform generic conversion
718 for (yc = 0; yc < dst->height; yc++)
709 { 719 {
710 Uint8 *d = dp; 720 Uint8 *d = dp;
711 const int y = yc / 8, yb = yc & 7; 721 const int y = yc / 8, yb = yc & 7;
712 const int scroffsy = y * C64_SCR_CH_WIDTH; 722 const int scroffsy = y * C64_SCR_CH_WIDTH;
713 const int bmoffsy = y * C64_SCR_WIDTH; 723 const int bmoffsy = y * C64_SCR_WIDTH;
714 int xc; 724 int xc;
715 725
716 if ((src->type & D64_FMT_MC) == D64_FMT_HIRES) 726 if ((src->type & D64_FMT_MC) == D64_FMT_HIRES)
717 { 727 {
718 for (xc = 0; xc < C64_SCR_WIDTH; xc++) 728 for (xc = 0; xc < dst->width; xc++)
719 { 729 {
720 const int x = xc / 8; 730 const int x = xc / 8;
721 const int scroffs = scroffsy + x; 731 const int scroffs = scroffsy + x;
722 const int b = src->bitmap[0][bmoffsy + (x * 8) + yb]; 732 const int bmoffs = bmoffsy + (x * 8) + yb;
723 const int v = 7 - (xc & 7); 733 const int v = 7 - (xc & 7);
724 734
725 if ((b >> v) & 1) 735 if ((src->bitmap[0][bmoffs] >> v) & 1)
726 *d++ = src->screen[0][scroffs] >> 4; 736 *d++ = src->screen[0][scroffs] >> 4;
727 else 737 else
728 *d++ = src->screen[0][scroffs] & 15; 738 *d++ = src->screen[0][scroffs] & 15;
729 } 739 }
730 } 740 }
731 else 741 else
732 { 742 {
733 for (xc = 0; xc < C64_SCR_WIDTH / 2; xc++) 743 for (xc = 0; xc < dst->width / wdivisor; xc++)
734 { 744 {
735 const int x = xc / 4; 745 const int x = xc / divisor;
736 const int scroffs = scroffsy + x; 746 const int scroffs = scroffsy + x;
737 const int bmoffs = bmoffsy + (x * 8) + yb; 747 const int bmoffs = bmoffsy + (x * 8) + yb;
738 const int v = 6 - ((xc * 2) & 6); 748 const int v = 6 - ((xc * 2) & 6);
739 Uint8 c; 749 Uint8 c;
740 750
769 *d++ = c; 779 *d++ = c;
770 *d++ = c; 780 *d++ = c;
771 } 781 }
772 } 782 }
773 } 783 }
774
775 dp += dst->pitch; 784 dp += dst->pitch;
776 } 785 }
777 786
778 return DMERR_OK; 787 return DMERR_OK;
779 } 788 }
780 789
790
791 int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt, const BOOL doubleMC)
792 {
793 int width, res;
794 DMImage *dst;
795
796 if (pdst == NULL || src == NULL)
797 return DMERR_NULLPTR;
798
799 // Calculate output image width
800 if ((src->type & D64_FMT_MC) && !doubleMC)
801 width = C64_SCR_WIDTH / 2;
802 else
803 width = C64_SCR_WIDTH;
804
805 // Allocate image structure
806 if ((*pdst = dst = dmImageAlloc(width, C64_SCR_HEIGHT)) == NULL)
807 return DMERR_MALLOC;
808
809 // Set palette
810 dst->pal = (DMColor *) &dmC64Palette;
811 dst->ncolors = C64_NCOLORS;
812 dst->constpal = TRUE;
813
814 // Convert
815 if (fmt->convertFrom != NULL)
816 res = fmt->convertFrom(dst, src, doubleMC);
817 else
818 res = dmC64ConvertGenericBMP2Image(dst, src, doubleMC);
819
820 return res;
821 }
781 822
782 823
783 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize) 824 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
784 { 825 {
785 FILE *f; 826 FILE *f;