comparison tools/lib64gfx.c @ 1499:32640e1934d5

Simplify some encoding bits.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 07:07:40 +0300
parents 9217fcc727e8
children 3223d01ac8e8
comparison
equal deleted inserted replaced
1498:9217fcc727e8 1499:32640e1934d5
520 DMGrowBuf tmp; 520 DMGrowBuf tmp;
521 Uint8 rleMarker; 521 Uint8 rleMarker;
522 const char *magicID = (fmt->type & D64_FMT_ILACE) ? "DRAZLACE! 1.0" : "DRAZPAINT 2.0"; 522 const char *magicID = (fmt->type & D64_FMT_ILACE) ? "DRAZLACE! 1.0" : "DRAZPAINT 2.0";
523 523
524 // Encode the data to temp buffer 524 // Encode the data to temp buffer
525 if ((res = dmC64EncodeGenericBMP(&tmp, img, fmt)) != DMERR_OK) 525 if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK)
526 goto out; 526 goto out;
527 527
528 // Analyze the data .. 528 // Analyze the data ..
529 rleMarker = 0xff; 529 rleMarker = 0xff;
530
531 // Allocate the output buffer
532 if ((res = dmGrowBufAlloc(buf, tmp.len + BUF_SIZE_GROW, BUF_SIZE_GROW)) != DMERR_OK)
533 goto out;
534 530
535 // Add the header bits 531 // Add the header bits
536 if (!dmGrowBufPut(buf, magicID, strlen(magicID)) || 532 if (!dmGrowBufPut(buf, magicID, strlen(magicID)) ||
537 !dmGrowBufPutU8(buf, rleMarker)) 533 !dmGrowBufPutU8(buf, rleMarker))
538 { 534 {
539 res = DMERR_MALLOC; 535 res = DMERR_MALLOC;
540 goto out; 536 goto out;
541 } 537 }
542 538
543 // And now RLE compress it to the existing buffer 539 // And now RLE compress the data to the existing buffer
544 res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, 540 res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len,
545 rleMarker, 3, 255, DM_RLE_MARKER); 541 rleMarker, 3, 255, DM_RLE_MARKER);
546 542
547 out: 543 out:
548 dmGrowBufFree(&tmp); 544 dmGrowBufFree(&tmp);
748 res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt); 744 res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt);
749 745
750 out: 746 out:
751 dmGrowBufFree(&mem); 747 dmGrowBufFree(&mem);
752 return res; 748 return res;
753 }
754
755
756 {
757
758 } 749 }
759 750
760 751
761 static Uint8 fmtGetPixelFunPaint2( 752 static Uint8 fmtGetPixelFunPaint2(
762 const DMC64Image *img, const int bmoffs, const int scroffs, 753 const DMC64Image *img, const int bmoffs, const int scroffs,
1528 1519
1529 return DMERR_OK; 1520 return DMERR_OK;
1530 } 1521 }
1531 1522
1532 1523
1533 int dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt) 1524 int dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt)
1534 { 1525 {
1535 int res = DMERR_OK; 1526 int res = DMERR_OK;
1536 1527
1537 if (img == NULL || fmt == NULL) 1528 if (img == NULL || fmt == NULL)
1538 return DMERR_NULLPTR; 1529 return DMERR_NULLPTR;
1539 1530
1540 // Allocate the output buffer 1531 // Allocate the output buffer if requested
1541 if ((res = dmGrowBufAlloc(buf, fmt->size, BUF_SIZE_GROW)) != DMERR_OK) 1532 if (allocate && (res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
1542 { 1533 {
1543 dmError(res, 1534 dmError(res,
1544 "Could not allocate %d bytes of memory for C64 image encoding buffer.\n", 1535 "Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
1545 fmt->size); 1536 fmt->size);
1546 goto err; 1537 goto err;
1895 int res; 1886 int res;
1896 1887
1897 if (img == NULL || fmt == NULL) 1888 if (img == NULL || fmt == NULL)
1898 return DMERR_NULLPTR; 1889 return DMERR_NULLPTR;
1899 1890
1891 // Allocate a buffer
1892 if ((res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
1893 goto err;
1894
1895 // Add the loading address
1896 if (!dmGrowBufPutU8(buf, DM_GET_ADDR_LO(fmt->addr)) ||
1897 !dmGrowBufPutU8(buf, DM_GET_ADDR_HI(fmt->addr)))
1898 goto err;
1899
1900 // Attempt to encode the image to a buffer 1900 // Attempt to encode the image to a buffer
1901 if ((res = dmGrowBufInit(buf)) != DMERR_OK)
1902 return res;
1903
1904 if (fmt->encode != NULL) 1901 if (fmt->encode != NULL)
1905 res = fmt->encode(buf, img, fmt); 1902 res = fmt->encode(buf, img, fmt);
1906 else 1903 else
1907 res = dmC64EncodeGenericBMP(buf, img, fmt); 1904 res = dmC64EncodeGenericBMP(FALSE, buf, img, fmt);
1908 1905
1909 if (res != DMERR_OK) 1906 if (res != DMERR_OK)
1910 goto err; 1907 goto err;
1911
1912 // Add the loading address
1913 if (!dmGrowBufGrow(buf, 2))
1914 {
1915 res = DMERR_MALLOC;
1916 goto err;
1917 }
1918
1919 memmove(buf->data + 2, buf->data, buf->len);
1920
1921 buf->data[0] = DM_GET_ADDR_LO(fmt->addr);
1922 buf->data[1] = DM_GET_ADDR_HI(fmt->addr);
1923 buf->len += 2;
1924 1908
1925 return DMERR_OK; 1909 return DMERR_OK;
1926 1910
1927 err: 1911 err:
1928 dmGrowBufFree(buf); 1912 dmGrowBufFree(buf);