1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-24 11:31:31 +00:00

Add palette extraction functions

This commit is contained in:
karri 2022-12-30 14:44:18 +02:00
parent ebd1381030
commit 4117e94ed5
5 changed files with 391 additions and 5 deletions

90
src/sp65/lynxpalette.c Normal file
View File

@ -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 <stdlib.h>
/* 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;
}

66
src/sp65/lynxpalette.h Normal file
View File

@ -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

View File

@ -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;

104
src/sp65/palconv.c Normal file
View File

@ -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 <string.h>
#include <stdlib.h>
/* 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);
}

72
src/sp65/palconv.h Normal file
View File

@ -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 <stdio.h>
/* 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