Mercurial > hg > dmlib
diff tools/gfxconv.c @ 1616:36d073c45327
Refactor the format handling a bit.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 15 May 2018 14:29:32 +0300 |
parents | c29adf5ce240 |
children | 625a7867877e |
line wrap: on
line diff
--- a/tools/gfxconv.c Tue May 15 12:38:55 2018 +0300 +++ b/tools/gfxconv.c Tue May 15 14:29:32 2018 +0300 @@ -55,22 +55,20 @@ } DMConvFormat; -static const DMConvFormat convFormatList[] = +static const DMConvFormat baseFormatList[] = { { "ASCII text" , "asc" , DM_FMT_WR , FFMT_ASCII , 0 }, { "ANSI colored text" , "ansi" , DM_FMT_WR , FFMT_ANSI , 0 }, - { "PNG image" , "png" , DM_FMT_RDWR , FFMT_IMAGE , DM_IMGFMT_PNG }, - { "PPM image" , "ppm" , DM_FMT_WR , FFMT_IMAGE , DM_IMGFMT_PPM }, - { "PCX image" , "pcx" , DM_FMT_RDWR , FFMT_IMAGE , DM_IMGFMT_PCX }, - { "IFF ILBM" , "lbm" , DM_FMT_RD , FFMT_IMAGE , DM_IMGFMT_ILBM }, - { "Bitplaned RAW (intl/non-intl) image" , "raw" , DM_FMT_WR , FFMT_IMAGE , DM_IMGFMT_RAW }, - { "IFFMaster RAW image" , "araw" , DM_FMT_WR , FFMT_IMAGE , DM_IMGFMT_ARAW }, { "C64 bitmap image" , NULL , DM_FMT_RDWR , FFMT_BITMAP , -1 }, { "C64 character/font data" , "chr" , DM_FMT_RDWR , FFMT_CHAR , 0 }, { "C64 sprite data" , "spr" , DM_FMT_RDWR , FFMT_SPRITE , 0 }, }; -static const int nconvFormatList = sizeof(convFormatList) / sizeof(convFormatList[0]); +static const int nbaseFormatList = sizeof(baseFormatList) / sizeof(baseFormatList[0]); + + +static DMConvFormat *convFormatList = NULL; +static int nconvFormatList = 0; typedef struct @@ -87,8 +85,8 @@ int optInFormat = FFMT_AUTO, optOutFormat = FFMT_ASCII, - optInSubFormat = DM_IMGFMT_PNG, - optOutSubFormat = DM_IMGFMT_PNG, + optInSubFormat = -1, + optOutSubFormat = -1, optItemCount = -1, optPlanedWidth = 1, optForcedFormat = -1; @@ -1606,12 +1604,30 @@ DMC64Image *inC64Image = NULL, *outC64Image = NULL; Uint8 *dataBuf = NULL, *dataBufOrig = NULL; size_t dataSize, dataSizeOrig; - int i; + int i, n; // Default colors for (i = 0; i < C64_NCOLORS; i++) optColors[i] = i; + // Initialize list of additional conversion formats + nconvFormatList = ndmImageFormatList + nbaseFormatList; + convFormatList = dmCalloc(nconvFormatList, sizeof(DMConvFormat)); + + for (n = i = 0; i < ndmImageFormatList; i++) + { + const DMImageFormat *sfmt = &dmImageFormatList[i]; + DMConvFormat *dfmt = &convFormatList[n++]; + dfmt->name = sfmt->name; + dfmt->fext = sfmt->fext; + dfmt->flags = sfmt->flags; + dfmt->format = FFMT_IMAGE; + dfmt->subformat = sfmt->fmtid; + } + + for (i = 0; i < nbaseFormatList; i++) + memcpy(&convFormatList[n++], &baseFormatList[i], sizeof(DMConvFormat)); + // Initialize and parse commandline dmInitProg("gfxconv", "Simple graphics converter", "0.91", NULL, NULL); @@ -1619,14 +1635,6 @@ argHandleOpt, argHandleFile, OPTH_BAILOUT)) exit(1); -#ifndef DM_USE_LIBPNG - if (optOutFormat == DM_IMGFMT_PNG) - { - dmErrorMsg("PNG output format support not compiled in, sorry.\n"); - goto error; - } -#endif - // Determine input format, if not specified' if (optInFormat == FFMT_AUTO && optInFilename != NULL) { @@ -1709,7 +1717,7 @@ if (optInFormat == FFMT_AUTO || optInFormat == FFMT_IMAGE) { - DMImageFormat *ifmt = NULL; + const DMImageFormat *ifmt = NULL; int index; dmMsg(4, "Trying to probe image formats.\n"); if (dmImageProbeGeneric(dataBuf + optInSkip, dataSize - optInSkip, &ifmt, &index) > 0) @@ -1731,7 +1739,8 @@ if (inFormat != -1 && outFormat != -1) { - char *inFmtName = convFormatList[inFormat].name, + const char + *inFmtName = convFormatList[inFormat].name, *inFmtExt = convFormatList[inFormat].fext, *outFmtName = convFormatList[outFormat].name, *outFmtExt = convFormatList[outFormat].fext; @@ -1810,6 +1819,7 @@ case FFMT_IMAGE: { + const DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat]; DMImage *inImage = NULL; int res = DMERR_OK; DMResource *fp; @@ -1827,7 +1837,6 @@ } // Read input - DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat]; if (ifmt->readFILE != NULL) res = ifmt->readFILE(fp, &inImage); else