From fbbf7e50d2bd54189e75b1d5d5d87d74759bf6cf Mon Sep 17 00:00:00 2001 From: uz Date: Tue, 13 Mar 2012 21:12:27 +0000 Subject: [PATCH] Added two more conversion targets: geos-bitmap, which will create a GEOS compacted bitmap, and raw, which is probably mostly useful for debugging. It converts an indexed image to a row of bytes which correspond to the color indices of the pixels. git-svn-id: svn://svn.cc65.org/cc65/trunk@5615 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sp65/convert.c | 4 + src/sp65/geosbitmap.c | 234 +++++++++++++++++++++++++++++++++++++++ src/sp65/geosbitmap.h | 70 ++++++++++++ src/sp65/make/gcc.mak | 2 + src/sp65/make/watcom.mak | 2 + src/sp65/raw.c | 89 +++++++++++++++ src/sp65/raw.h | 67 +++++++++++ 7 files changed, 468 insertions(+) create mode 100644 src/sp65/geosbitmap.c create mode 100644 src/sp65/geosbitmap.h create mode 100644 src/sp65/raw.c create mode 100644 src/sp65/raw.h diff --git a/src/sp65/convert.c b/src/sp65/convert.c index 0063bf261..f5022246c 100644 --- a/src/sp65/convert.c +++ b/src/sp65/convert.c @@ -39,9 +39,11 @@ #include "attr.h" #include "convert.h" #include "error.h" +#include "geosbitmap.h" #include "geosicon.h" #include "koala.h" #include "lynxsprite.h" +#include "raw.h" #include "vic2sprite.h" @@ -61,9 +63,11 @@ struct ConverterMapEntry { /* Converter table, alphabetically sorted */ static const ConverterMapEntry ConverterMap[] = { + { "geos-bitmap", GenGeosBitmap }, { "geos-icon", GenGeosIcon }, { "koala", GenKoala }, { "lynx-sprite", GenLynxSprite }, + { "raw", GenRaw }, { "vic2-sprite", GenVic2Sprite }, }; diff --git a/src/sp65/geosbitmap.c b/src/sp65/geosbitmap.c new file mode 100644 index 000000000..f5770f720 --- /dev/null +++ b/src/sp65/geosbitmap.c @@ -0,0 +1,234 @@ +/*****************************************************************************/ +/* */ +/* geosbitmap.c */ +/* */ +/* GEOS compacted bitmap backend for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2012, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* 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. */ +/* */ +/*****************************************************************************/ + + + +/* common */ +#include "attrib.h" +#include "print.h" +#include "xmalloc.h" + +/* sp65 */ +#include "attr.h" +#include "error.h" +#include "geosbitmap.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +#define UNIQUE_MAX 91U /* Maximum number of unique bytes */ +#define REPEAT_MAX 127U /* Maximum number of repeated bytes */ + + + +/* Structure used for RLE compression */ +struct RLE { + unsigned char* Buf; /* Pointer to pixel buffer */ + unsigned Remaining; /* Remaining bytes in buffer */ + unsigned char LastVal; /* Last value seen */ + unsigned Count; /* Number of occurrences for LastVal */ + StrBuf* D; /* Buffer for compressed data */ + StrBuf UniqueData; /* Temp buffer for unique data */ +}; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +static void StoreByte (struct RLE* RLE) +/* Store a unique byte or a run of repeated bytes. If count is zero, the + * function will flush the internal buffers, so that all data is in RLE->D. + */ +{ + + + if (RLE->Count == 1 || RLE->Count == 2) { + /* A run of two identical bytes is treated as two unique bytes, since + * this will usually merge with the following run. + */ + SB_AppendChar (&RLE->UniqueData, RLE->LastVal); + if (RLE->Count == 2) { + SB_AppendChar (&RLE->UniqueData, RLE->LastVal); + } + + /* Clear the repeat counter */ + RLE->Count = 0; + + } else { + + /* Run of repeated bytes. First flush the temp buffer for unique + * bytes. + */ + const char* Buf = SB_GetConstBuf (&RLE->UniqueData); + unsigned Count = SB_GetLen (&RLE->UniqueData); + while (Count) { + + /* Determine the count for this sequence */ + unsigned Chunk = Count; + if (Chunk > UNIQUE_MAX) { + Chunk = UNIQUE_MAX; + } + + /* Output the unique op */ + SB_AppendChar (RLE->D, 0x80 + (unsigned char) Chunk); + SB_AppendBuf (RLE->D, Buf, Chunk); + + /* Bump the counters */ + Buf += Chunk; + Count -= Chunk; + } + + /* Clear the unique byte buffer */ + SB_Clear (&RLE->UniqueData); + + /* Now output the repeat sequence */ + while (RLE->Count) { + + /* Determine the count for this sequence */ + unsigned Chunk = RLE->Count; + if (Chunk > REPEAT_MAX) { + Chunk = REPEAT_MAX; + } + /* Output the repeat op */ + SB_AppendChar (RLE->D, (unsigned char) Chunk); + SB_AppendChar (RLE->D, RLE->LastVal); + + /* Bump the counters */ + RLE->Count -= Chunk; + } + } +} + + + +StrBuf* GenGeosBitmap (const Bitmap* B, const Collection* A attribute ((unused))) +/* Generate binary output in GEOS compacted bitmap format for the bitmap B. + * The output is stored in a string buffer (which is actually a dynamic char + * array) and returned. + */ +{ + unsigned LineWidth; + unsigned char* Buf; + unsigned char* BP; + StrBuf* D; + unsigned X, Y; + struct RLE RLE; + + + /* Output the image properties */ + Print (stdout, 1, "Image is %ux%u with %u colors%s\n", + GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B), + BitmapIsIndexed (B)? " (indexed)" : ""); + + /* Check the bitmap properties */ + if (!BitmapIsIndexed (B) || GetBitmapColors (B) > 2) { + Error ("Bitmaps converted to GEOS compacted bitmap must be " + "indexed with two colors"); + } + + /* Calculate the width of one line in bytes */ + LineWidth = (GetBitmapWidth (B) + 7U) / 8U; + + /* Allocate a buffer for the raw image */ + Buf = xmalloc (LineWidth * GetBitmapHeight (B)); + + /* Convert the bitmap into a raw image */ + BP = Buf; + for (Y = 0; Y < GetBitmapHeight (B); ++Y) { + for (X = 0; X < GetBitmapWidth (B); ) { + unsigned char V = 0; + int Bits = 8; + if ((unsigned)Bits > GetBitmapWidth (B) - X) { + Bits = (GetBitmapWidth (B) - X); + } + while (--Bits >= 0) { + V |= (GetPixel (B, X++, Y).Index & 0x01) << Bits; + } + *BP++ = V; + } + } + + /* Create the output buffer and resize it to something reasonable */ + D = NewStrBuf (); + SB_Realloc (D, 64); + + /* Compact the image. We're currently using only REPEAT and UNIQUE opcodes. + * BIGCOUNT is rather difficult to apply. + */ + RLE.Buf = Buf; + RLE.Remaining = LineWidth * GetBitmapHeight (B) - 1; + RLE.LastVal = *RLE.Buf++; + RLE.Count = 1; + RLE.D = D; + RLE.UniqueData = EmptyStrBuf; + while (RLE.Remaining--) { + /* */ + if (RLE.LastVal == *RLE.Buf) { + ++RLE.Buf; + ++RLE.Count; + } else { + StoreByte (&RLE); + RLE.LastVal = *RLE.Buf++; + RLE.Count = 1; + } + } + + /* If something remains, store it */ + if (RLE.Count) { + StoreByte (&RLE); + } + + /* Flush the unique byte buffer */ + StoreByte (&RLE); + + /* Release the unique byte buffer */ + SB_Done (&RLE.UniqueData); + + /* Free the raw image buffer */ + xfree (Buf); + + /* Return the converted bitmap */ + return D; +} + + + diff --git a/src/sp65/geosbitmap.h b/src/sp65/geosbitmap.h new file mode 100644 index 000000000..87fccf666 --- /dev/null +++ b/src/sp65/geosbitmap.h @@ -0,0 +1,70 @@ +/*****************************************************************************/ +/* */ +/* geosbitmap.h */ +/* */ +/* GEOS compacted bitmap backend for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2012, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* 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 GEOSBITMAP_H +#define GEOSBITMAP_H + + + +/* common */ +#include "coll.h" +#include "strbuf.h" + +/* sp65 */ +#include "bitmap.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +StrBuf* GenGeosBitmap (const Bitmap* B, const Collection* A); +/* Generate binary output in GEOS compacted bitmap format for the bitmap B. + * The output is stored in a string buffer (which is actually a dynamic char + * array) and returned. + */ + + + +/* End of geosbitmap.h */ + +#endif + + + + diff --git a/src/sp65/make/gcc.mak b/src/sp65/make/gcc.mak index 29cc42a60..5215e70e7 100644 --- a/src/sp65/make/gcc.mak +++ b/src/sp65/make/gcc.mak @@ -31,6 +31,7 @@ OBJS = asm.o \ convert.o \ error.o \ fileio.o \ + geosbitmap.o \ geosicon.o \ input.o \ koala.o \ @@ -39,6 +40,7 @@ OBJS = asm.o \ output.o \ palette.o \ pcx.o \ + raw.o \ vic2sprite.o LIBS = $(COMMON)/common.a diff --git a/src/sp65/make/watcom.mak b/src/sp65/make/watcom.mak index 0b724491e..b69429e7b 100644 --- a/src/sp65/make/watcom.mak +++ b/src/sp65/make/watcom.mak @@ -69,6 +69,7 @@ OBJS = asm.obj \ convert.obj \ error.obj \ fileio.obj \ + geosbitmap.obj \ geosicon.obj \ input.obj \ koala.obj \ @@ -77,6 +78,7 @@ OBJS = asm.obj \ output.obj \ palette.obj \ pcx.obj \ + raw.obj \ vic2sprite.obj LIBS = ../common/common.lib diff --git a/src/sp65/raw.c b/src/sp65/raw.c new file mode 100644 index 000000000..871200652 --- /dev/null +++ b/src/sp65/raw.c @@ -0,0 +1,89 @@ +/*****************************************************************************/ +/* */ +/* raw.c */ +/* */ +/* Raw image converter for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2012, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* 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. */ +/* */ +/*****************************************************************************/ + + + +/* common */ +#include "attrib.h" +#include "print.h" + +/* sp65 */ +#include "attr.h" +#include "error.h" +#include "raw.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +StrBuf* GenRaw (const Bitmap* B, const Collection* A attribute ((unused))) +/* Generate binary output in raw format. The output is stored in a string + * buffer (which is actually a dynamic char array) and returned. + */ +{ + StrBuf* D; + unsigned X, Y; + + + /* Output the image properties */ + Print (stdout, 1, "Image is %ux%u with %u colors%s\n", + GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B), + BitmapIsIndexed (B)? " (indexed)" : ""); + + /* Check the bitmap properties */ + if (!BitmapIsIndexed (B)) { + Error ("The raw format needs an input bitmap in indexed format"); + } + + /* Create the output buffer and resize it to the required size. */ + D = NewStrBuf (); + SB_Realloc (D, GetBitmapWidth (B) * GetBitmapHeight (B)); + + /* Convert the image */ + for (Y = 0; Y < GetBitmapHeight (B); ++Y) { + for (X = 0; X < GetBitmapWidth (B); ++X) { + /* Place one pixel into the buffer */ + SB_AppendChar (D, (unsigned char) GetPixel (B, X, Y).Index); + } + } + + /* Return the converted bitmap */ + return D; +} + + + diff --git a/src/sp65/raw.h b/src/sp65/raw.h new file mode 100644 index 000000000..d4c44f7ea --- /dev/null +++ b/src/sp65/raw.h @@ -0,0 +1,67 @@ +/*****************************************************************************/ +/* */ +/* raw.h */ +/* */ +/* Raw image converter for the sp65 sprite and bitmap utility */ +/* */ +/* */ +/* */ +/* (C) 2012, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* 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 RAW_H +#define RAW_H + + + +/* common */ +#include "coll.h" +#include "strbuf.h" + +/* sp65 */ +#include "bitmap.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +StrBuf* GenRaw (const Bitmap* B, const Collection* A); +/* Generate binary output in raw format. The output is stored in a string + * buffer (which is actually a dynamic char array) and returned. + */ + + + +/* End of raw.h */ +#endif + + + -- 2.39.5