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 >
C/C++ Source or Header  |  1997-09-20  |  4KB  |  235 lines

  1. ///////////////////
  2. // palette class //
  3. ///////////////////
  4.  
  5. #ifndef __PALETTE_H
  6. #define __PALETTE_H
  7.  
  8. #include "file.h"
  9. #include "misc.h"
  10. #include "convert.h"
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. class Palette
  19. {
  20.     public:
  21.  
  22.         // setup
  23.         Palette();
  24.         Palette(char filename[]);
  25.         Palette(Palette const &other);
  26.         ~Palette();
  27.         
  28.         // file operations
  29.         int Load(char filename[]);
  30.         int Save(char filename[]);
  31.  
  32.         // data access
  33.         inline void* ReadOnly() const;
  34.         inline void* Lock();
  35.         inline void Unlock();
  36.         int LockCount() const;
  37.  
  38.         // set and get palette data
  39.         inline int Set(void *data);
  40.         inline int Get(void *data) const;
  41.  
  42.         // get and set individual colors
  43.         inline int Set(int index,uint color);
  44.         inline uint Get(int index) const;
  45.  
  46.         // color index table access
  47.         inline void* GetIndexTable(FORMAT const &format,int alignment=DEFAULT);
  48.  
  49.         // operators
  50.         int operator ==(Palette const &other) const;
  51.         void operator =(Palette const &other);
  52.  
  53.         // state
  54.         inline int ok() const;
  55.         
  56.     protected:
  57.  
  58.         // palette data
  59.         uint *Data;
  60.         int DataLockCount;
  61.  
  62.         // index table struct
  63.         struct INDEXTABLE
  64.         {
  65.             FORMAT Format;      // index pixel format
  66.             int Alignment;      // byte align (4 byte align default)
  67.             void *Data;         // index table data
  68.         };
  69.  
  70.         // index table management
  71.         void* CreateIndexTable(FORMAT const &format,int alignment);
  72.         inline void ClearIndexTableList();
  73.  
  74.         // index table list
  75.         List<INDEXTABLE> IndexTableList;
  76. };
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. inline void* Palette::Lock()
  87. {
  88.     // increment lock count
  89.     if (Data) DataLockCount++;
  90.     
  91.     // clear index tables
  92.     ClearIndexTableList();
  93.  
  94.     // return palette data pointer
  95.     return Data;
  96. }
  97.  
  98.  
  99. inline void Palette::Unlock()
  100. {
  101.     // decrement lock count
  102.     if (DataLockCount) DataLockCount--;
  103. }
  104.         
  105.  
  106. inline void* Palette::ReadOnly() const
  107. {
  108.     // same as lock - but assumes user will only do read only access!
  109.     return Data;
  110. }
  111.         
  112.         
  113.         
  114.         
  115.  
  116.  
  117.  
  118. inline int Palette::Set(void *data)
  119. {
  120.     // check user data
  121.     if (!data) return 0;
  122.     
  123.     // lock palette data
  124.     void *buffer=Lock();
  125.     if (!buffer) return 0;
  126.  
  127.     // set new data
  128.     memcpy(buffer,data,1024);
  129.     
  130.     // unlock
  131.     Unlock();
  132.     return 1;
  133. }
  134.  
  135.  
  136. inline int Palette::Get(void *data) const
  137. {
  138.     // get palette data
  139.     if (!data || !Data) return 0;
  140.     else
  141.     {
  142.         // copy to user data
  143.         memcpy(data,Data,1024);
  144.         return 1;
  145.     }
  146. }
  147.  
  148.  
  149. inline int Palette::Set(int index,uint color)
  150. {
  151.     // fail if out of bounds
  152.     if (index<0 || index>255) return 0;
  153.  
  154.     // set color
  155.     Data[index]=color;
  156.     return 1;
  157. }
  158.  
  159.  
  160. inline uint Palette::Get(int index) const
  161. {
  162.     // fail if out of bounds
  163.     if (index<0 || index>255) return 0;
  164.  
  165.     // get color
  166.     return Data[index];
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.        
  174.  
  175. inline void* Palette::GetIndexTable(FORMAT const &format,int alignment)
  176. {
  177.     // quick check - if looking for ARGB8888 align 4, return straight palette data
  178.     if (format.id==ARGB8888 && alignment==4) return Data;
  179.  
  180.     // otherwise search for matching index table
  181.     Iterator<INDEXTABLE> iterator(IndexTableList);
  182.     INDEXTABLE *table=iterator.first();
  183.     while (table)
  184.     {              
  185.         // check for a match
  186.         if (table->Format==format && table->Alignment==alignment) return table->Data; 
  187.  
  188.         // no match - try next index table in list...
  189.         table=iterator.next();       
  190.     }
  191.  
  192.     // couldnt find table in list, create it
  193.     return CreateIndexTable(format,alignment);
  194. }
  195.  
  196.  
  197. inline void Palette::ClearIndexTableList()
  198. {
  199.     // free all the table->Data in list
  200.     Iterator<INDEXTABLE> iterator(IndexTableList);
  201.     INDEXTABLE *table=iterator.first();
  202.     while (table)
  203.     {
  204.         delete table->Data;
  205.         table=iterator.next();
  206.     }
  207.  
  208.     // free all list items
  209.     IndexTableList.free();
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. inline int Palette::ok() const
  219. {
  220.     // palette state
  221.     if (!Data) return 0;
  222.     else return 1;
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. #endif
  235.