home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Computer Panoráma
/
computer_panorama_1997-12-hibas.iso
/
SHARE
/
GRAPH
/
PTC051.ZIP
/
SRC
/
PALETTE.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-20
|
4KB
|
235 lines
///////////////////
// palette class //
///////////////////
#ifndef __PALETTE_H
#define __PALETTE_H
#include "file.h"
#include "misc.h"
#include "convert.h"
class Palette
{
public:
// setup
Palette();
Palette(char filename[]);
Palette(Palette const &other);
~Palette();
// file operations
int Load(char filename[]);
int Save(char filename[]);
// data access
inline void* ReadOnly() const;
inline void* Lock();
inline void Unlock();
int LockCount() const;
// set and get palette data
inline int Set(void *data);
inline int Get(void *data) const;
// get and set individual colors
inline int Set(int index,uint color);
inline uint Get(int index) const;
// color index table access
inline void* GetIndexTable(FORMAT const &format,int alignment=DEFAULT);
// operators
int operator ==(Palette const &other) const;
void operator =(Palette const &other);
// state
inline int ok() const;
protected:
// palette data
uint *Data;
int DataLockCount;
// index table struct
struct INDEXTABLE
{
FORMAT Format; // index pixel format
int Alignment; // byte align (4 byte align default)
void *Data; // index table data
};
// index table management
void* CreateIndexTable(FORMAT const &format,int alignment);
inline void ClearIndexTableList();
// index table list
List<INDEXTABLE> IndexTableList;
};
inline void* Palette::Lock()
{
// increment lock count
if (Data) DataLockCount++;
// clear index tables
ClearIndexTableList();
// return palette data pointer
return Data;
}
inline void Palette::Unlock()
{
// decrement lock count
if (DataLockCount) DataLockCount--;
}
inline void* Palette::ReadOnly() const
{
// same as lock - but assumes user will only do read only access!
return Data;
}
inline int Palette::Set(void *data)
{
// check user data
if (!data) return 0;
// lock palette data
void *buffer=Lock();
if (!buffer) return 0;
// set new data
memcpy(buffer,data,1024);
// unlock
Unlock();
return 1;
}
inline int Palette::Get(void *data) const
{
// get palette data
if (!data || !Data) return 0;
else
{
// copy to user data
memcpy(data,Data,1024);
return 1;
}
}
inline int Palette::Set(int index,uint color)
{
// fail if out of bounds
if (index<0 || index>255) return 0;
// set color
Data[index]=color;
return 1;
}
inline uint Palette::Get(int index) const
{
// fail if out of bounds
if (index<0 || index>255) return 0;
// get color
return Data[index];
}
inline void* Palette::GetIndexTable(FORMAT const &format,int alignment)
{
// quick check - if looking for ARGB8888 align 4, return straight palette data
if (format.id==ARGB8888 && alignment==4) return Data;
// otherwise search for matching index table
Iterator<INDEXTABLE> iterator(IndexTableList);
INDEXTABLE *table=iterator.first();
while (table)
{
// check for a match
if (table->Format==format && table->Alignment==alignment) return table->Data;
// no match - try next index table in list...
table=iterator.next();
}
// couldnt find table in list, create it
return CreateIndexTable(format,alignment);
}
inline void Palette::ClearIndexTableList()
{
// free all the table->Data in list
Iterator<INDEXTABLE> iterator(IndexTableList);
INDEXTABLE *table=iterator.first();
while (table)
{
delete table->Data;
table=iterator.next();
}
// free all list items
IndexTableList.free();
}
inline int Palette::ok() const
{
// palette state
if (!Data) return 0;
else return 1;
}
#endif