view tools/lib64util.c @ 2265:48b48251610a

Refactor how the image "mode/type" is handled. It is still not perfect for our purposes, but better now.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 17 Jun 2019 02:03:35 +0300
parents e25fa516b53a
children 87533af8db64
line wrap: on
line source

/*
 * Common utility functions for gfxconv and 64vw
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2019 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#include "lib64util.h"
#include "dmfile.h"


char * dmC64GetImageTypeString(char *buf, const size_t len, const int type, const BOOL lng)
{
    static const char *fmtModesShort[] = { "*", "HiR", "MC", "ECM" };
    static const char *fmtModesLong[] = { "*", "HiRes", "MultiColor", "Extended Color Mode" };
    const char *fmtStr;
    size_t nfmt = type & D64_FMT_MODE_MASK;

    if (nfmt < sizeof(fmtModesShort) / sizeof(fmtModesShort[0]))
        fmtStr = lng ? fmtModesLong[nfmt] : fmtModesShort[nfmt];
    else
        fmtStr = lng ? "ERROR" : "ERR";

    snprintf(buf, len,
        "%s %s%s%s",
        fmtStr,
        (type & D64_FMT_ILACE) ? (lng ? "Interlaced " : "ILace ") : "",
        (type & D64_FMT_FLI)   ? "FLI " : "",
        (type & D64_FMT_CHAR)  ? "CHAR" : ""
        );

    return buf;
}


static void dmC64ImageDumpLine(FILE *fh, const char *indent, const char *field, const char *fmt, ...)
{
    va_list ap;

    fputs(indent, fh);
    fprintf(fh, "%-20s: ", field);
    va_start(ap, fmt);
    vfprintf(fh, fmt, ap);
    va_end(ap);
    fputs("\n", fh);
}


void dmC64ImageDump(FILE *fh, const DMC64Image *img, const DMC64ImageFormat *fmt, const char *indent)
{
    char typeStr[64];

    if (fmt != NULL)
    {
        dmC64ImageDumpLine(fh, indent,
            "Format", "%s [%s]",
            fmt->name, fmt->fext);
    }

    if (img != NULL)
    {
        dmC64GetImageTypeString(typeStr, sizeof(typeStr), img->extraInfo[D64_EI_MODE], TRUE);

        dmC64ImageDumpLine(fh, indent,
            "Type", "%s", typeStr);

        dmC64ImageDumpLine(fh, indent,
            "Internal blocks", "%d", img->nblocks);

        if (img->extraInfo[D64_EI_MODE] & D64_FMT_ILACE)
        {
            char *tmps;
            switch (img->extraInfo[D64_EI_ILACE_TYPE])
            {
                case D64_ILACE_COLOR: tmps = "color"; break;
                case D64_ILACE_RES: tmps = "resolution"; break;
                default: tmps = "ERROR"; break;
            }
            dmC64ImageDumpLine(fh, indent,
                "Interlace type", "%s", tmps);
        }

        if (img->extraInfo[D64_EI_MODE] & D64_FMT_FLI)
        {
            dmC64ImageDumpLine(fh, indent,
                "FLI type", "%d",
                img->extraInfo[D64_EI_FLI_TYPE]);
        }

        dmC64ImageDumpLine(fh, indent,
            "Width x Height", "%d x %d pixels",
            img->fmt->width, img->fmt->height);

        dmC64ImageDumpLine(fh, indent,
            "CHwidth x CHheight", "%d x %d",
            img->fmt->chWidth, img->fmt->chHeight);

        dmC64ImageDumpLine(fh, indent,
            "d020 / border", "%d ($%02x)",
            img->d020, img->d020);

        dmC64ImageDumpLine(fh, indent,
            "d021 / background", "%d ($%02x)",
            img->bgcolor, img->bgcolor);

        if (img->extraInfo[D64_EI_MODE] & D64_FMT_CHAR)
        {
            if ((img->extraInfo[D64_EI_MODE] & D64_FMT_MODE_MASK) == (D64_FMT_MC | D64_FMT_ECM))
            {
                dmC64ImageDumpLine(fh, indent,
                    "d022", "%d ($%02x)",
                    img->d022, img->d022);
                dmC64ImageDumpLine(fh, indent,
                    "d023", "%d ($%02x)",
                    img->d023, img->d023);
            }

            if ((img->extraInfo[D64_EI_MODE] & D64_FMT_MODE_MASK) == D64_FMT_ECM)
            {
                dmC64ImageDumpLine(fh, indent,
                    "d024", "%d ($%02x)",
                    img->d024, img->d024);
            }
        }
    }
    else
    if (fmt != NULL)
    {
        dmC64GetImageTypeString(typeStr, sizeof(typeStr), fmt->format->mode, TRUE);

        dmC64ImageDumpLine(fh, indent,
            "Type", "%s", typeStr);

        dmC64ImageDumpLine(fh, indent,
            "Width x Height", "%d x %d pixels",
            fmt->format->width, fmt->format->height);

        dmC64ImageDumpLine(fh, indent,
            "CHwidth x CHheight", "%d x %d",
            fmt->format->chWidth, fmt->format->chHeight);
    }
}


void argShowC64Formats(FILE *fh, const BOOL rw)
{
    fprintf(fh,
        "Available C64 bitmap formats (-f <frmt>):\n"
        " frmt %s| Type            | Description\n"
        "------%s+-----------------+-------------------------------------\n",
        rw ? "| RW " : "",
        rw ? "+----" : ""
        );

    for (int i = 0; i < ndmC64ImageFormats; i++)
    {
        const DMC64ImageFormat *fmt = dmC64ImageFormatsSorted[i];
        char buf[64];

        fprintf(fh, "%-6s",
            fmt->fext);

        if (rw)
        {
            fprintf(fh, "| %c%c ",
                (fmt->flags & DM_FMT_RD) ? 'R' : ' ',
                (fmt->flags & DM_FMT_WR) ? 'W' : ' '
                );
        }

        fprintf(fh,
            "| %-15s | %s%s\n",
            dmC64GetImageTypeString(buf, sizeof(buf), fmt->format->mode, FALSE),
            fmt->name,
            fmt->flags & DM_FMT_BROKEN ? " [BROKEN]" : "");
    }

    fprintf(fh, "%d formats supported.\n", ndmC64ImageFormats);
}


void argShowC64PaletteHelp(FILE *fh)
{
    fprintf(fh,
        "\n"
        "Available C64 palettes (-p <name>):\n"
        "-----------------------------------\n");

    for (int n = 0; n < ndmC64DefaultPalettes; n++)
    {
        DMC64Palette *pal = &dmC64DefaultPalettes[n];
        fprintf(fh,
            "%-10s | %s\n",
            pal->name, pal->desc);
    }

    fprintf(fh,
        "\n"
        "Instead one of the internal palettes, you can\n"
        "also specify an external palette file.\n"
        "Supported palette file formats are:\n"
        "-----------------------------------\n"
        );

    for (int n = 0; n < ndmPaletteFormatList; n++)
    {
        const DMPaletteFormat *fmt = &dmPaletteFormatList[n];
        if (fmt->flags & DM_FMT_RD)
        {
            fprintf(fh,
                "%-6s | %s\n",
                fmt->fext, fmt->name);
        }
    }
    fprintf(fh, "\n");
}


BOOL argHandleC64PaletteOption(char *optArg, DMC64Palette **ppal, char **palFile)
{
    if (strcasecmp(optArg, "help") == 0 ||
        strcasecmp(optArg, "list") == 0)
    {
        argShowC64PaletteHelp(stdout);
        return FALSE;
    }

    for (int n = 0; n < ndmC64DefaultPalettes; n++)
    {
        DMC64Palette *pal = &dmC64DefaultPalettes[n];
        if (strcasecmp(pal->name, optArg) == 0)
        {
            *ppal = pal;
            return TRUE;
        }
    }

    *palFile = optArg;
    return TRUE;
}


int dmHandleExternalPalette(const char *filename, DMPalette **ppal)
{
    DMResource *fh = NULL;
    const DMImageFormat *ifmt = NULL;
    const DMPaletteFormat *pfmt = NULL;
    DMImage *inImage = NULL;
    Uint8 *dataBuf = NULL;
    size_t dataSize;
    int index, res;

    dmMsg(1, "Probing file '%s' for palette data.\n", filename);

    if ((res = dmReadDataFile(NULL, filename, &dataBuf, &dataSize)) != DMERR_OK)
    {
        dmErrorMsg("No such palette '%s', and no such file found (%s).\n",
            filename, dmErrorStr(res));
        goto done;
    }

    if ((res = dmf_open_memio(NULL, filename, dataBuf, dataSize, &fh)) != DMERR_OK)
    {
        dmErrorMsg("Could not create MemIO handle for input.\n");
        goto done;
    }

    if (dmImageProbeGeneric(dataBuf, dataSize, &ifmt, &index) > 0 &&
        ifmt->read != NULL)
    {
        dmMsg(1, "Probed image format %s (%s)\n",
            ifmt->name, ifmt->fext);

        res = ifmt->read(fh, &inImage);
        if (res != DMERR_OK)
        {
            dmErrorMsg("Could not read image file: %s\n",
                dmErrorStr(res));
            goto done;
        }

        if (inImage->pal != NULL)
            res = dmPaletteCopy(ppal, inImage->pal);
        else
        {
            dmErrorMsg("Image file does not have a palette.\n");
            res = DMERR_NULLPTR;
        }
    }
    else
    if (dmPaletteProbeGeneric(dataBuf, dataSize, &pfmt, &index) > 0 &&
        pfmt->read != NULL)
    {
        dmMsg(1, "Probed palette format %s (%s)\n",
            pfmt->name, pfmt->fext);

        res = pfmt->read(fh, ppal);
    }
    else
    {
        res = DMERR_NOT_SUPPORTED;
        dmErrorMsg("Not an internal palette or recognized palette file '%s'.\n",
            filename);
    }

done:
    dmf_close(fh);
    dmImageFree(inImage);
    dmFree(dataBuf);

    return res;
}