comparison tools/libgfx.c @ 2069:83a3d05b5c1d

Fixes to ACBM support.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Dec 2018 14:41:19 +0200
parents e4dc8fbaa5ad
children 3dac7a781317
comparison
equal deleted inserted replaced
2068:e4dc8fbaa5ad 2069:83a3d05b5c1d
1700 // Decode the chunk 1700 // Decode the chunk
1701 if (iff->idsig == IFF_ID_ACBM) 1701 if (iff->idsig == IFF_ID_ACBM)
1702 { 1702 {
1703 for (int plane = 0; plane < nplanes; plane++) 1703 for (int plane = 0; plane < nplanes; plane++)
1704 { 1704 {
1705 if (!dmf_read_str(fp, buf, bufLen)) 1705 // Decompress or read data
1706 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1706 { 1707 {
1707 res = dmError(DMERR_FREAD, 1708 res = dmError(DMERR_FREAD,
1708 "IFF: Error in reading image plane #%d.\n", 1709 "IFF: Error in reading ACBM image plane #%d.\n",
1709 plane); 1710 plane);
1710 goto out; 1711 goto out;
1711 } 1712 }
1712 1713
1713 for (int yc = 0; yc < img->height; yc++) 1714 for (int yc = 0; yc < img->height; yc++)
1735 { 1736 {
1736 // Decompress or read data 1737 // Decompress or read data
1737 if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) 1738 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1738 { 1739 {
1739 res = dmError(DMERR_FREAD, 1740 res = dmError(DMERR_FREAD,
1740 "IFF: Error in reading image plane #%d @ %d.\n", 1741 "IFF: Error in reading ILBM image plane #%d @ %d.\n",
1741 plane, yc); 1742 plane, yc);
1742 goto out; 1743 goto out;
1743 } 1744 }
1744 1745
1745 // Decode bitplane 1746 // Decode bitplane
1752 { 1753 {
1753 // Decompress or read data 1754 // Decompress or read data
1754 if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) 1755 if (!dmIFFReadOneRow(fp, iff, buf, bufLen))
1755 { 1756 {
1756 res = dmError(DMERR_FREAD, 1757 res = dmError(DMERR_FREAD,
1757 "IFF: Error in reading mask plane.\n"); 1758 "IFF: Error in reading ILBM mask plane.\n");
1758 goto out; 1759 goto out;
1759 } 1760 }
1760 1761
1761 // Decode mask 1762 // Decode mask
1762 for (int xc = 0; xc < img->width; xc++) 1763 for (int xc = 0; xc < img->width; xc++)
1868 iff.bmhd.masking != IFF_MASK_TRANSP)) 1869 iff.bmhd.masking != IFF_MASK_TRANSP))
1869 { 1870 {
1870 return dmError(DMERR_NOT_SUPPORTED, 1871 return dmError(DMERR_NOT_SUPPORTED,
1871 "IFF: Unsupported features, refusing to load.\n"); 1872 "IFF: Unsupported features, refusing to load.\n");
1872 } 1873 }
1874
1875 if (iff.idsig == IFF_ID_ACBM &&
1876 iff.bmhd.compression != IFF_COMP_NONE)
1877 {
1878 dmMsg(1, "IFF: ACBM image with compression != none (%d). Ignoring.\n",
1879 iff.bmhd.compression);
1880
1881 iff.bmhd.compression = IFF_COMP_NONE;
1882 }
1873 break; 1883 break;
1874 1884
1875 1885
1876 case IFF_ID_CMAP: 1886 case IFF_ID_CMAP:
1877 // Check for multiple occurences of CMAP 1887 // Check for multiple occurences of CMAP
2243 iff.bmhd.xasp = 1; // XXX TODO: compute the xasp/yasp from the img->aspect 2253 iff.bmhd.xasp = 1; // XXX TODO: compute the xasp/yasp from the img->aspect
2244 iff.bmhd.yasp = 1; 2254 iff.bmhd.yasp = 1;
2245 2255
2246 iff.camg = 0; // XXX TODO: when/if HAM support 2256 iff.camg = 0; // XXX TODO: when/if HAM support
2247 iff.bmhd.masking = (img->ctransp < 0) ? IFF_MASK_NONE : spec->mask; 2257 iff.bmhd.masking = (img->ctransp < 0) ? IFF_MASK_NONE : spec->mask;
2248 iff.bmhd.compression = spec->compression ? IFF_COMP_BYTERUN1 : IFF_COMP_NONE;
2249 iff.bmhd.transp = (img->ctransp >= 0 && spec->mask == IFF_MASK_TRANSP) ? img->ctransp : 0xffff; 2258 iff.bmhd.transp = (img->ctransp >= 0 && spec->mask == IFF_MASK_TRANSP) ? img->ctransp : 0xffff;
2250 iff.bmhd.nplanes = (iff.idsig == IFF_ID_PBM && spec->nplanes < 8) ? 8 : spec->nplanes; 2259 iff.bmhd.nplanes = (iff.idsig == IFF_ID_PBM && spec->nplanes < 8) ? 8 : spec->nplanes;
2260
2261 // Apparently ACBM can't/should not use compression .. even though
2262 // some files in the wild have bmhd.compression != 0 (but are not
2263 // actually compressed.) To be more compliant with the spec,
2264 iff.bmhd.compression = (spec->compression && iff.idsig != IFF_ID_ACBM) ? IFF_COMP_BYTERUN1 : IFF_COMP_NONE;
2251 2265
2252 dmMsg(2, "IFF: nplanes=%d, comp=%d, mask=%d\n", 2266 dmMsg(2, "IFF: nplanes=%d, comp=%d, mask=%d\n",
2253 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking); 2267 iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking);
2254 2268
2255 // Write IFF FORM header 2269 // Write IFF FORM header
2334 2348
2335 2349
2336 // 2350 //
2337 // Encode the body 2351 // Encode the body
2338 // 2352 //
2339 if ((res = dmWriteIFFChunkHdr(fp, &iff.chBODY, IFF_ID_BODY)) != DMERR_OK) 2353 if ((res = dmWriteIFFChunkHdr(fp, &iff.chBODY,
2354 (iff.idsig == IFF_ID_ACBM) ? IFF_ID_ABIT : IFF_ID_BODY)) != DMERR_OK)
2340 goto out; 2355 goto out;
2341 2356
2342 // Allocate encoding buffer 2357 // Allocate encoding buffer
2343 if (iff.idsig == IFF_ID_ILBM) 2358 if (iff.idsig == IFF_ID_ILBM)
2344 bufLen = (((img->width * spec->scaleX) + 15) / 16) * 2; 2359 bufLen = (((img->width * spec->scaleX) + 15) / 16) * 2;
2345 else 2360 else
2361 if (iff.idsig == IFF_ID_ACBM)
2362 bufLen = (img->width * spec->scaleX * img->height * spec->scaleY) / 8;
2363 else
2346 bufLen = img->width * spec->scaleX; 2364 bufLen = img->width * spec->scaleX;
2347 2365
2348 dmMsg(2, "IFF: Line/plane row size %d bytes.\n", bufLen); 2366 dmMsg(2, "IFF: Line/plane row size %d bytes.\n", bufLen);
2349 2367
2350 if ((buf = dmMalloc(bufLen)) == NULL) 2368 if ((buf = dmMalloc(bufLen)) == NULL)
2351 return DMERR_MALLOC; 2369 return DMERR_MALLOC;
2352 2370
2353 // Encode the body 2371 // Encode the body
2372 if (iff.idsig == IFF_ID_ACBM)
2373 {
2374 for (int plane = 0; plane < iff.bmhd.nplanes; plane++)
2375 {
2376 // Encode bitplane
2377 dmMemset(buf, 0, bufLen);
2378
2379 for (int yc = 0; yc < img->height * spec->scaleY; yc++)
2380 {
2381 Uint8 *sp = img->data + (yc * img->pitch);
2382 Uint8 *dp = buf + (yc * img->width * spec->scaleX) / 8;
2383 for (int xc = 0; xc < img->width * spec->scaleX; xc++)
2384 dp[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7));
2385 }
2386
2387 if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen))
2388 {
2389 res = dmError(DMERR_FWRITE,
2390 "IFF: Error writing ACBM image plane %d.\n",
2391 plane);
2392 goto out;
2393 }
2394 }
2395 }
2396 else
2354 { 2397 {
2355 for (int yc = 0; yc < img->height; yc++) 2398 for (int yc = 0; yc < img->height; yc++)
2356 for (int yscale = 0; yscale < spec->scaleY; yscale++) 2399 for (int yscale = 0; yscale < spec->scaleY; yscale++)
2357 { 2400 {
2358 const Uint8 *sp = img->data + (yc * img->pitch); 2401 const Uint8 *sp = img->data + (yc * img->pitch);