diff --git a/src/sp65/lynxpalette.c b/src/sp65/lynxpalette.c new file mode 100644 index 000000000..e822fa5ee --- /dev/null +++ b/src/sp65/lynxpalette.c @@ -0,0 +1,90 @@ +/*****************************************************************************/ +/* */ +/* lynxpalette.c */ +/* */ +/* Lynx palette backend for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2022, Karri Kaksonen */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include + +/* common */ +#include "attrib.h" +#include "print.h" + +/* sp65 */ +#include "attr.h" +#include "error.h" +#include "palette.h" +#include "lynxpalette.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + +StrBuf* GenLynxPalette (const Bitmap* B, const Collection* A) +/* Generate binary output in Lynx palette format for the bitmap B. The output +** is stored in a string buffer (which is actually a dynamic char array) and +** returned. +** +*/ +{ + StrBuf* D; + Palette* P; + unsigned I; + + P = GetBitmapPalette (B); + D = NewStrBuf (); + for (I = 0; I < 16; ++I) { + + /* Get the color entry */ + const Color* C = P->Entries + I; + + /* Add the green component */ + SB_AppendChar (D, C->G >> 4); + } + for (I = 0; I < 16; ++I) { + + /* Get the color entry */ + const Color* C = P->Entries + I; + + /* Add the blue,red component */ + SB_AppendChar (D, (C->B & 0xF0) | (C->R >> 4)); + } + + /* Return the converted palette */ + return D; +} + diff --git a/src/sp65/lynxpalette.h b/src/sp65/lynxpalette.h new file mode 100644 index 000000000..3ba4526fb --- /dev/null +++ b/src/sp65/lynxpalette.h @@ -0,0 +1,66 @@ +/*****************************************************************************/ +/* */ +/* lynxpalette.h */ +/* */ +/* Lynx palette format backend for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2022, Karri Kaksonen */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef LYNXPALETTE_H +#define LYNXPALETTE_H + + + +/* common */ +#include "coll.h" +#include "strbuf.h" + +/* sp65 */ +#include "bitmap.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +StrBuf* GenLynxPalette (const Bitmap* B, const Collection* A); +/* Generate binary output in Lynx palette format for the bitmap B. The output + * is stored in a string buffer (which is actually a dynamic char array) and + * returned. + */ + + + +/* End of lynxpalette.h */ + +#endif + + + diff --git a/src/sp65/main.c b/src/sp65/main.c index 1dda696d6..62dec0952 100644 --- a/src/sp65/main.c +++ b/src/sp65/main.c @@ -1,6 +1,6 @@ /*****************************************************************************/ /* */ -/* main.c */ +/* main.c */ /* */ /* Main program of the sp65 sprite and bitmap utility */ /* */ @@ -47,6 +47,7 @@ /* sp65 */ #include "attr.h" #include "convert.h" +#include "palconv.h" #include "error.h" #include "input.h" #include "output.h" @@ -68,10 +69,13 @@ static Bitmap* C; /* Output data from convertion */ static StrBuf* D; +/* Output data from palconv */ +static StrBuf* E; + /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ @@ -88,11 +92,11 @@ static void Usage (void) " -lc\t\t\t\tList all possible conversions\n" " -r file[,attrlist]\t\tRead an input file\n" " -v\t\t\t\tIncrease verbosity\n" + " -p tgt,file[,attrlist]\t\tWrite the palette to a file\n" " -w file[,attrlist]\t\tWrite the output to a file\n" "\n" "Long options:\n" " --convert-to fmt[,attrlist]\tConvert into target format\n" - " --dump-palette\t\tDump palette as table\n" " --help\t\t\tHelp (this text)\n" " --list-conversions\t\tList all possible conversions\n" " --pop\t\t\t\tRestore the original loaded image\n" @@ -100,6 +104,7 @@ static void Usage (void) " --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n" " --verbose\t\t\tIncrease verbosity\n" " --version\t\t\tPrint the version number and exit\n" + " --palette tgt,file[,attrlist]\tWrite the palette to a file\n" " --write file[,attrlist]\tWrite the output to a file\n", ProgName); } @@ -137,6 +142,21 @@ static void SetOutputData (StrBuf* N) } +static void SetPalOutputData (StrBuf* N) +/* Delete the old output data and replace it by the given one. The new one +** may be NULL to clear it. +*/ +{ + /* Delete the old output data */ + if (E != 0) { + FreeStrBuf (E); + } + + /* Set the new one */ + E = N; +} + + static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg) /* Convert the bitmap into a target format */ @@ -282,15 +302,45 @@ static void OptVerbose (const char* Opt attribute ((unused)), static void OptVersion (const char* Opt attribute ((unused)), - const char* Arg attribute ((unused))) + const char* Arg attribute ((unused))) /* Print the assembler version */ { fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ()); - exit(EXIT_SUCCESS); } +static void OptPalette (const char* Opt attribute ((unused)), const char* Arg) +/* Write an output file */ +{ + static const char* const NameList[] = { + "target", "name", "format" + }; + + + /* Parse the argument */ + Collection* A = ParseAttrList (Arg, NameList, 2); + + /* We must have a bitmap ... */ + if (C == 0) { + Error ("No bitmap"); + } + + /* ... which must be indexed */ + if (!BitmapIsIndexed (C)) { + Error ("Current bitmap is not indexed"); + } + + /* Convert the palette */ + SetPalOutputData (PaletteTo (C, A)); + + /* Write the file */ + WriteOutputFile (E, A, C); + + /* Delete the attribute list */ + FreeAttrList (A); +} + static void OptWrite (const char* Opt attribute ((unused)), const char* Arg) /* Write an output file */ { @@ -381,6 +431,10 @@ int main (int argc, char* argv []) OptVerbose (Arg, 0); break; + case 'p': + OptPalette (Arg, GetArg (&I, 2)); + break; + case 'w': OptWrite (Arg, GetArg (&I, 2)); break; diff --git a/src/sp65/palconv.c b/src/sp65/palconv.c new file mode 100644 index 000000000..e92f3c22e --- /dev/null +++ b/src/sp65/palconv.c @@ -0,0 +1,104 @@ +/*****************************************************************************/ +/* */ +/* palconv.c */ +/* */ +/* Color palette conversions for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2022, Karri Kaksonen */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include +#include + +/* common */ +#include "check.h" +#include "xmalloc.h" + +/* sp65 */ +#include "attr.h" +#include "error.h" +#include "palette.h" +#include "lynxpalette.h" + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + +/* Type of the entry in the palette table */ +typedef struct PaletteMapEntry PaletteMapEntry; +struct PaletteMapEntry { + const char* Format; + StrBuf* (*PaletteFunc) (const Bitmap*, const Collection*); +}; + +/* Converter table, alphabetically sorted */ +static const PaletteMapEntry PaletteMap[] = { + { "lynx-palette", GenLynxPalette }, +}; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + +static int Compare (const void* Key, const void* MapEntry) +/* Compare function for bsearch */ +{ + return strcmp (Key, ((const PaletteMapEntry*) MapEntry)->Format); +} + + + +StrBuf* PaletteTo (const Bitmap* B, const Collection* A) +/* Convert the palette of bitmap B into some sort of other binary format. +** The output is stored in a string buffer (which is actually a dynamic +** char array) and returned. The actual output format is taken from the +** "format" attribute in the attribute collection A. +*/ +{ + const PaletteMapEntry* E; + + /* Get the format to convert to */ + const char* Format = NeedAttrVal (A, "target", "palette"); + + /* Search for the matching converter */ + E = bsearch (Format, + PaletteMap, + sizeof (PaletteMap) / sizeof (PaletteMap[0]), + sizeof (PaletteMap[0]), + Compare); + if (E == 0) { + Error ("No such target format: '%s'", Format); + } + + /* Do the conversion */ + return E->PaletteFunc (B, A); +} + diff --git a/src/sp65/palconv.h b/src/sp65/palconv.h new file mode 100644 index 000000000..693281c20 --- /dev/null +++ b/src/sp65/palconv.h @@ -0,0 +1,72 @@ +/*****************************************************************************/ +/* */ +/* palconv.h */ +/* */ +/* Color palette conversions for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2022, Karri Kaksonen */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef PALCONV_H +#define PALCONV_H + + + +#include + +/* common */ +#include "coll.h" +#include "strbuf.h" + +/* sp65 */ +#include "bitmap.h" + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + +StrBuf* PaletteTo (const Bitmap* B, const Collection* A); +/* Convert the palette of bitmap B into some sort of other binary format. +** The output is stored in a string buffer (which is actually a dynamic char +** array) and returned. The actual output format is taken from the "target" +** attribute in the attribute collection A. +*/ + +void ListPaletteTargets (FILE* F); +/* Output a list of palette targets */ + +/* End of palette.h */ + +#endif + + +