From c9d65c56c29ad63b62161dc4cbdd719a21b64a2e Mon Sep 17 00:00:00 2001 From: uz Date: Sun, 11 Mar 2012 11:10:41 +0000 Subject: [PATCH] Remove the bitmap type since it is not really needed and complicate things. git-svn-id: svn://svn.cc65.org/cc65/trunk@5603 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sp65/bitmap.c | 14 ++--- src/sp65/bitmap.h | 23 ++----- src/sp65/koala.c | 2 +- src/sp65/palette.c | 7 ++- src/sp65/pcx.c | 142 ++++++++++++++++++++---------------------- src/sp65/vic2sprite.c | 4 +- 6 files changed, 86 insertions(+), 106 deletions(-) diff --git a/src/sp65/bitmap.c b/src/sp65/bitmap.c index b00a01651..e9fa676fb 100644 --- a/src/sp65/bitmap.c +++ b/src/sp65/bitmap.c @@ -64,7 +64,6 @@ Bitmap* NewBitmap (unsigned Width, unsigned Height) B = xmalloc (sizeof (*B) + (Size - 1) * sizeof (B->Data[0])); /* Initialize the data */ - B->Type = bmUnknown; B->Name = EmptyStrBuf; B->Width = Width; B->Height = Height; @@ -120,7 +119,6 @@ Bitmap* SliceBitmap (const Bitmap* O, unsigned OrigX, unsigned OrigY, B = NewBitmap (Width, Height); /* Copy fields from the original */ - B->Type = O->Type; if (SB_GetLen (&O->Name) > 0) { SB_CopyStr (&B->Name, "Slice of "); SB_Append (&B->Name, &O->Name); @@ -185,15 +183,11 @@ unsigned GetBitmapColors (const Bitmap* B) * of palette entries for indexed bitmaps and 2^24 for non indexed bitmaps. */ { - switch (B->Type) { - case bmMonochrome: return 2; - case bmIndexed: return B->Pal->Count; - case bmRGB: - case bmRGBA: return (1U << 24); - default: Internal ("Unknown bitmap type %u", B->Type); + if (B->Pal) { + return B->Pal->Count; + } else { + return (1U << 24); } - /* NOTREACHED */ - return 0; } diff --git a/src/sp65/bitmap.h b/src/sp65/bitmap.h index 5c111a27b..d4e09e84d 100644 --- a/src/sp65/bitmap.h +++ b/src/sp65/bitmap.h @@ -60,23 +60,10 @@ /* Safety limit for the size of the bitmap in pixels */ #define BM_MAX_SIZE 4194304UL -/* Bitmap type */ -enum BitmapType { - bmUnknown, - bmMonochrome, - bmIndexed, - bmRGB, - bmRGBA -}; -typedef enum BitmapType BitmapType; - /* Bitmap structure */ typedef struct Bitmap Bitmap; struct Bitmap { - /* Type of the bitmap */ - BitmapType Type; - /* Name of the bitmap. This is used for error messages and should be * something that allows the user to identify which bitmap the message * refers to. For bitmaps loaded from a file, using the file name is @@ -88,7 +75,7 @@ struct Bitmap { unsigned Width; unsigned Height; - /* Palette for monochrome and indexed bitmap types, otherwise NULL */ + /* Palette for indexed bitmap types, otherwise NULL */ Palette* Pal; /* Pixel data, dynamically allocated */ @@ -131,13 +118,13 @@ Pixel GetPixel (const Bitmap* B, unsigned X, unsigned Y); */ #if defined(HAVE_INLINE) -INLINE BitmapType GetBitmapType (const Bitmap* B) -/* Get the type of a bitmap */ +INLINE int BitmapIsIndexed (const Bitmap* B) +/* Return true if this is an indexed bitmap */ { - return B->Type; + return (B->Pal != 0); } #else -# define GetBitmapType(B) ((B)->Type) +# define BitmapIsIndexed(B) ((B)->Pal != 0) #endif #if defined(HAVE_INLINE) diff --git a/src/sp65/koala.c b/src/sp65/koala.c index 723cd0f5f..9212f9751 100644 --- a/src/sp65/koala.c +++ b/src/sp65/koala.c @@ -71,7 +71,7 @@ StrBuf* GenKoala (const Bitmap* B, const Collection* A attribute ((unused))) unsigned char X, Y; /* Koala pictures are always 160x200 in size with 16 colors */ - if (GetBitmapType (B) != bmIndexed || + if (!BitmapIsIndexed (B) || GetBitmapColors (B) > 16 || GetBitmapHeight (B) != HEIGHT || GetBitmapWidth (B) != WIDTH) { diff --git a/src/sp65/palette.c b/src/sp65/palette.c index 4aa4e7036..afde3d7a2 100644 --- a/src/sp65/palette.c +++ b/src/sp65/palette.c @@ -86,7 +86,12 @@ Palette* NewMonochromePalette (void) Palette* DupPalette (const Palette* P) /* Create a copy of a palette */ -{ +{ + /* Allow to pass a NULL palette */ + if (P == 0) { + return 0; + } + /* Create a new palette */ Palette* N = NewPalette (P->Count); diff --git a/src/sp65/pcx.c b/src/sp65/pcx.c index 7dc9d9c9b..51767c931 100644 --- a/src/sp65/pcx.c +++ b/src/sp65/pcx.c @@ -275,14 +275,6 @@ Bitmap* ReadPCXFile (const Collection* A) /* Create the bitmap */ B = NewBitmap (P->Width, P->Height); - /* Determine the type of the bitmap */ - switch (P->Planes) { - case 1: B->Type = (P->PalInfo? bmIndexed : bmMonochrome); break; - case 3: B->Type = bmRGB; break; - case 4: B->Type = bmRGBA; break; - default:Internal ("Unexpected number of planes"); - } - /* Copy the name */ SB_CopyStr (&B->Name, Name); @@ -332,7 +324,75 @@ Bitmap* ReadPCXFile (const Collection* A) } } } + + /* One plane means we have a palette which is either part of the header + * or follows. + */ + if (P->PalInfo == 0) { + + /* Create the monochrome palette */ + B->Pal = NewMonochromePalette (); + + } else { + + unsigned Count; + unsigned I; + unsigned char Palette[256][3]; + unsigned long EndPos; + + /* Determine the current file position */ + unsigned long CurPos = FileGetPos (F); + + /* Seek to the end of the file */ + (void) fseek (F, 0, SEEK_END); + + /* Get this position */ + EndPos = FileGetPos (F); + + /* There's a palette if the old location is 769 bytes from the end */ + if (EndPos - CurPos == sizeof (Palette) + 1) { + + /* Seek back */ + FileSetPos (F, CurPos); + + /* Check for palette marker */ + if (Read8 (F) != 0x0C) { + Error ("Invalid palette marker in PCX file `%s'", Name); + } + + } else if (EndPos == CurPos) { + + /* The palette is in the header */ + FileSetPos (F, 16); + + /* Check the maximum index for safety */ + if (MaxIdx > 15) { + Error ("PCX file `%s' contains more than 16 indexed colors " + "but no extra palette", Name); + } + + } else { + Error ("Error in PCX file `%s': %lu bytes at end of pixel data", + Name, EndPos - CurPos); + } + + /* Read the palette. We will just read what we need. */ + Count = MaxIdx + 1; + ReadData (F, Palette, Count * sizeof (Palette[0])); + + /* Create the palette from the data */ + B->Pal = NewPalette (Count); + for (I = 0; I < Count; ++I) { + B->Pal->Entries[I].R = Palette[I][0]; + B->Pal->Entries[I].G = Palette[I][1]; + B->Pal->Entries[I].B = Palette[I][2]; + B->Pal->Entries[I].A = 0; + } + + } + } else { + /* 3 or 4 planes are RGB or RGBA (don't know if this exists) */ for (Y = 0, Px = B->Data; Y < P->Height; ++Y) { @@ -368,72 +428,6 @@ Bitmap* ReadPCXFile (const Collection* A) } } - /* One plane means we have a palette which is either part of the header - * or follows. - */ - if (B->Type == bmMonochrome) { - - /* Create the monochrome palette */ - B->Pal = NewMonochromePalette (); - - } else if (B->Type == bmIndexed) { - - unsigned Count; - unsigned I; - unsigned char Palette[256][3]; - unsigned long EndPos; - - /* Determine the current file position */ - unsigned long CurPos = FileGetPos (F); - - /* Seek to the end of the file */ - (void) fseek (F, 0, SEEK_END); - - /* Get this position */ - EndPos = FileGetPos (F); - - /* There's a palette if the old location is 769 bytes from the end */ - if (EndPos - CurPos == sizeof (Palette) + 1) { - - /* Seek back */ - FileSetPos (F, CurPos); - - /* Check for palette marker */ - if (Read8 (F) != 0x0C) { - Error ("Invalid palette marker in PCX file `%s'", Name); - } - - } else if (EndPos == CurPos) { - - /* The palette is in the header */ - FileSetPos (F, 16); - - /* Check the maximum index for safety */ - if (MaxIdx > 15) { - Error ("PCX file `%s' contains more than 16 indexed colors " - "but no extra palette", Name); - } - - } else { - Error ("Error in PCX file `%s': %lu bytes at end of pixel data", - Name, EndPos - CurPos); - } - - /* Read the palette. We will just read what we need. */ - Count = MaxIdx + 1; - ReadData (F, Palette, Count * sizeof (Palette[0])); - - /* Create the palette from the data */ - B->Pal = NewPalette (Count); - for (I = 0; I < Count; ++I) { - B->Pal->Entries[I].R = Palette[I][0]; - B->Pal->Entries[I].G = Palette[I][1]; - B->Pal->Entries[I].B = Palette[I][2]; - B->Pal->Entries[I].A = 0; - } - - } - /* Close the file */ fclose (F); diff --git a/src/sp65/vic2sprite.c b/src/sp65/vic2sprite.c index 371b9616b..bc4de4bf7 100644 --- a/src/sp65/vic2sprite.c +++ b/src/sp65/vic2sprite.c @@ -74,10 +74,10 @@ StrBuf* GenVic2Sprite (const Bitmap* B, const Collection* A attribute ((unused)) /* Output the image properties */ Print (stdout, 1, "Image is %ux%u with %u colors%s\n", GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B), - (GetBitmapType (B) == bmIndexed)? " (indexed)" : ""); + BitmapIsIndexed (B)? " (indexed)" : ""); /* Sprites pictures are always 24x21 in size with 2 colors */ - if (GetBitmapType (B) != bmIndexed || + if (!BitmapIsIndexed (B) || GetBitmapColors (B) != 2 || GetBitmapHeight (B) != HEIGHT || GetBitmapWidth (B) != WIDTH) { -- 2.39.5