comparison lib64gfx.c @ 532:128a50feff07

Implement initial generic bitmap "encoding" function, that constructs generic bitmap formats as a "reverse" of the decoding process.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 22 Nov 2012 17:44:45 +0200
parents 2ac364d0ace9
children 91e2d0d74e2f
comparison
equal deleted inserted replaced
531:2ac364d0ace9 532:128a50feff07
597 break; 597 break;
598 } 598 }
599 } 599 }
600 600
601 return DMERR_OK; 601 return DMERR_OK;
602 }
603
604
605 int dmC64EncodeGenericBMP(Uint8 **pbuf, size_t *plen, const DMC64Image *img, const DMC64ImageFormat *fmt)
606 {
607 int i, res = DMERR_OK;
608 Uint8 *buf;
609 size_t allocated;
610
611 if (pbuf == NULL || plen == NULL || img == NULL || fmt == NULL)
612 return DMERR_NULLPTR;
613
614 // Allocate the output buffer
615 *plen = 0;
616 if (fmt->size > 0)
617 *plen = allocated = fmt->size;
618 else
619 allocated = 8 * 1024;
620
621 if ((buf = dmMalloc(allocated)) == NULL)
622 {
623 dmError("Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
624 allocated);
625 res = DMERR_MALLOC;
626 goto error;
627 }
628
629 // Perform encoding
630 for (i = 0; i < fmt->nencdecOps; i++)
631 {
632 const DMC64EncDecOp *op = &fmt->encdecOps[i];
633 Uint8 *dst = buf + op->offs;
634 size_t size;
635
636 if ((res = dmC64SanityCheckEncDecOp(i, op)) != DMERR_OK)
637 goto error;
638
639 size = (op->size == 0) ? dmC64DefaultSizes[op->type] : op->size;
640
641 if (op->offs + size > allocated)
642 {
643 if ((buf = dmRealloc(buf, allocated)) == NULL)
644 {
645 size_t diff = allocated - (op->offs + size),
646 grow = (diff / (BUF_SIZE_GROW - 1)) * BUF_SIZE_GROW;
647 allocated = allocated + grow;
648 dmError("Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n",
649 allocated);
650 res = DMERR_MALLOC;
651 goto error;
652 }
653 }
654
655 if (fmt->size == 0 && op->offs + size > *plen)
656 *plen = op->offs + size;
657
658 switch (op->type)
659 {
660 case DT_COLOR_RAM: memcpy(dst, img->color[op->bank], size); break;
661 case DT_BITMAP: memcpy(dst, img->bitmap[op->bank], size); break;
662 case DT_SCREEN_RAM: memcpy(dst, img->screen[op->bank], size); break;
663 case DT_BGCOLOR: *dst = img->bgcolor; break;
664 case DT_EXTRADATA: memcpy(dst, img->extradata, size); break;
665 case DT_ENC_FUNCTION:
666 break;
667 }
668 }
669
670 return DMERR_OK;
671
672 error:
673 dmFree(*pbuf);
674 *pbuf = NULL;
675 *plen = 0;
676 return res;
602 } 677 }
603 678
604 679
605 static inline Uint8 dmC64GetMCColor(const DMC64Image *img, const int bits, const int cbank, const int vbank, const int scroffs) 680 static inline Uint8 dmC64GetMCColor(const DMC64Image *img, const int bits, const int cbank, const int vbank, const int scroffs)
606 { 681 {