comparison tools/libgfx.c @ 1903:8ad98bc7402c

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Jun 2018 13:01:25 +0300
parents 74405ff6bc4a
children 5930ff7879b5
comparison
equal deleted inserted replaced
1902:74405ff6bc4a 1903:8ad98bc7402c
1421 1421
1422 typedef struct 1422 typedef struct
1423 { 1423 {
1424 DMIFFChunk chFORM, chBMHD, chCMAP, chBODY; 1424 DMIFFChunk chFORM, chBMHD, chCMAP, chBODY;
1425 DMIFFBMHD bmhd; 1425 DMIFFBMHD bmhd;
1426 BOOL planar;
1426 Uint32 camg; 1427 Uint32 camg;
1427 int ncolors; 1428 int ncolors;
1428 DMColor *pal; 1429 DMColor *pal;
1429 } DMIFF; 1430 } DMIFF;
1430 1431
1497 { 1498 {
1498 return dmError(DMERR_FSEEK, 1499 return dmError(DMERR_FSEEK,
1499 "IFF: Failed to skip chunk end.\n"); 1500 "IFF: Failed to skip chunk end.\n");
1500 } 1501 }
1501 } 1502 }
1503
1502 return DMERR_OK; 1504 return DMERR_OK;
1503 } 1505 }
1504 1506
1505 1507
1506 static int dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk, 1508 static int dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk,
1581 { 1583 {
1582 return (buf[xc / 8] >> (7 - (xc & 7))) & 1; 1584 return (buf[xc / 8] >> (7 - (xc & 7))) & 1;
1583 } 1585 }
1584 1586
1585 1587
1586 static int dmDecodeILBMBody(DMResource *fp, DMIFF *iff, DMImage *img) 1588 static int dmDecodeIFFBody(DMResource *fp, DMIFF *iff, DMImage *img)
1587 { 1589 {
1588 Uint8 *buf; 1590 Uint8 *buf = NULL;
1589 size_t bufLen; 1591 size_t bufLen = 0;
1590 int res = DMERR_OK; 1592 int res = DMERR_OK;
1591 const int nplanes = iff->bmhd.nplanes; 1593 const int nplanes = iff->bmhd.nplanes;
1592 1594
1593 // Allocate planar decoding buffer 1595 if (iff->planar)
1594 bufLen = ((img->width + 15) / 16) * 2; 1596 {
1595 if ((buf = dmMalloc(bufLen)) == NULL) 1597 // Allocate planar decoding buffer
1596 return DMERR_MALLOC; 1598 bufLen = ((img->width + 15) / 16) * 2;
1597 1599 if ((buf = dmMalloc(bufLen)) == NULL)
1598 dmMsg(2, "IFF: plane row size %d bytes.\n", bufLen); 1600 return DMERR_MALLOC;
1601
1602 dmMsg(2, "IFF: plane row size %d bytes.\n", bufLen);
1603 }
1599 1604
1600 // Decode the chunk 1605 // Decode the chunk
1601 for (int yc = 0; yc < img->height; yc++) 1606 for (int yc = 0; yc < img->height; yc++)
1602 { 1607 {
1603 Uint8 *dp = img->data + (yc * img->pitch); 1608 Uint8 *dp = img->data + (yc * img->pitch);
1604 1609
1605 dmMemset(dp, 0, img->pitch); 1610 if (iff->planar)
1606 1611 {
1607 for (int plane = 0; plane < nplanes; plane++) 1612 dmMemset(dp, 0, img->pitch);
1608 { 1613
1609 // Decompress or read data 1614 for (int plane = 0; plane < nplanes; plane++)
1610 if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) 1615 {
1616 // Decompress or read data
1617 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1618 {
1619 res = dmError(DMERR_FREAD,
1620 "IFF: Error in reading image plane #%d @ %d.\n",
1621 plane, yc);
1622 goto out;
1623 }
1624
1625 // Decode bitplane
1626 for (int xc = 0; xc < img->width; xc++)
1627 dp[xc] |= dmDecodeBit(buf, xc) << plane;
1628 }
1629
1630 // Read mask data
1631 if (iff->bmhd.masking == IFF_MASK_HAS_MASK)
1632 {
1633 // Decompress or read data
1634 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1635 {
1636 res = dmError(DMERR_FREAD,
1637 "IFF: Error in reading mask plane.\n");
1638 goto out;
1639 }
1640
1641 // Decode mask
1642 for (int xc = 0; xc < img->width; xc++)
1643 {
1644 const Uint8 data = dmDecodeBit(buf, xc);
1645
1646 // Black out any pixels with mask bit 0
1647 if (!data)
1648 dp[xc] = img->ctransp < 0 ? 0 : img->ctransp;
1649 }
1650 }
1651 }
1652 else
1653 {
1654 if (!dmIFFReadOneRow(fp, iff, dp, img->width))
1611 { 1655 {
1612 res = dmError(DMERR_FREAD, 1656 res = dmError(DMERR_FREAD,
1613 "IFF: Error in reading image plane #%d @ %d.\n", 1657 "IFF: Error reading PBM image row #%d.\n", yc);
1614 plane, yc); 1658 goto out;
1615 goto error; 1659 }
1616 } 1660 }
1617 1661 }
1618 // Decode bitplane 1662
1619 for (int xc = 0; xc < img->width; xc++) 1663 out:
1620 dp[xc] |= dmDecodeBit(buf, xc) << plane;
1621 }
1622
1623 // Read mask data
1624 if (iff->bmhd.masking == IFF_MASK_HAS_MASK)
1625 {
1626 // Decompress or read data
1627 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1628 {
1629 res = dmError(DMERR_FREAD,
1630 "IFF: Error in reading mask plane.\n");
1631 goto error;
1632 }
1633
1634 // Decode mask
1635 for (int xc = 0; xc < img->width; xc++)
1636 {
1637 const Uint8 data = dmDecodeBit(buf, xc);
1638
1639 // Black out any pixels with mask bit 0
1640 if (!data)
1641 dp[xc] = img->ctransp < 0 ? 0 : img->ctransp;
1642 }
1643 }
1644 }
1645
1646 error:
1647 dmFree(buf); 1664 dmFree(buf);
1648 return res; 1665 return res;
1649 }
1650
1651
1652 static int dmDecodePBMBody(DMResource *fp, DMIFF *iff, DMImage *img)
1653 {
1654 for (int yc = 0; yc < img->height; yc++)
1655 {
1656 if (!dmIFFReadOneRow(fp, iff, img->data + (yc * img->pitch), img->width))
1657 {
1658 return dmError(DMERR_FREAD,
1659 "IFF: Error reading PBM image row #%d.\n", yc);
1660 }
1661 }
1662
1663 return DMERR_OK;
1664 } 1666 }
1665 1667
1666 1668
1667 int dmReadIFFImage(DMResource *fp, DMImage **pimg) 1669 int dmReadIFFImage(DMResource *fp, DMImage **pimg)
1668 { 1670 {
1669 DMIFFChunk chunk; 1671 DMIFFChunk chunk;
1670 DMIFF iff; 1672 DMIFF iff;
1671 Uint32 idsig; 1673 Uint32 idsig;
1672 BOOL parsed = FALSE, planar; 1674 BOOL parsed = FALSE;
1673 int res = DMERR_OK; 1675 int res = DMERR_OK;
1674 1676
1675 dmMemset(&iff, 0, sizeof(iff)); 1677 dmMemset(&iff, 0, sizeof(iff));
1676 1678
1677 // Read IFF FORM header 1679 // Read IFF FORM header
1691 { 1693 {
1692 return dmError(DMERR_INVALID_DATA, 1694 return dmError(DMERR_INVALID_DATA,
1693 "IFF: Not a IFF ILBM/PBM file.\n"); 1695 "IFF: Not a IFF ILBM/PBM file.\n");
1694 } 1696 }
1695 1697
1696 planar = (idsig == IFF_ID_ILBM); 1698 iff.planar = (idsig == IFF_ID_ILBM);
1697 1699
1698 while (!parsed && !dmfeof(fp)) 1700 while (!parsed && !dmfeof(fp))
1699 { 1701 {
1700 // Read chunk header 1702 // Read chunk header
1701 if ((res = dmReadIFFChunkHdr(fp, &chunk)) != DMERR_OK) 1703 if ((res = dmReadIFFChunkHdr(fp, &chunk)) != DMERR_OK)
1812 // Set image aspect ratio 1814 // Set image aspect ratio
1813 if (iff.bmhd.xasp > 0 && iff.bmhd.yasp > 0) 1815 if (iff.bmhd.xasp > 0 && iff.bmhd.yasp > 0)
1814 (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp; 1816 (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp;
1815 1817
1816 // Decode the body 1818 // Decode the body
1817 if (planar) 1819 if ((res = dmDecodeIFFBody(fp, &iff, *pimg)) != DMERR_OK)
1818 { 1820 return res;
1819 if ((res = dmDecodeILBMBody(fp, &iff, *pimg)) != DMERR_OK)
1820 return res;
1821 }
1822 else
1823 {
1824 if ((res = dmDecodePBMBody(fp, &iff, *pimg)) != DMERR_OK)
1825 return res;
1826 }
1827 1821
1828 if ((res = dmSkipIFFChunkRest(fp, &chunk)) != DMERR_OK) 1822 if ((res = dmSkipIFFChunkRest(fp, &chunk)) != DMERR_OK)
1829 return res; 1823 return res;
1830 1824
1831 if (iff.chCMAP.count) 1825 if (iff.chCMAP.count)
1871 { 1865 {
1872 // If halfbrite is used, duplicate the palette 1866 // If halfbrite is used, duplicate the palette
1873 if (iff.camg & IFF_CAMG_HALFBRITE) 1867 if (iff.camg & IFF_CAMG_HALFBRITE)
1874 { 1868 {
1875 void *ptmp; 1869 void *ptmp;
1876 if (!planar) 1870 if (!iff.planar)
1877 { 1871 {
1878 dmErrorMsg("IFF: Non-planar PBM file with Halfbrite enabled! This might not work.\n"); 1872 dmErrorMsg("IFF: Non-planar PBM file with Halfbrite enabled! This might not work.\n");
1879 } 1873 }
1880 1874
1881 if (iff.ncolors > 128) 1875 if (iff.ncolors > 128)