home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / FORMAT.H < prev    next >
C/C++ Source or Header  |  1997-09-20  |  4KB  |  164 lines

  1. ////////////////////////
  2. // pixel format class //
  3. ////////////////////////
  4.  
  5. #ifndef __FORMAT_H
  6. #define __FORMAT_H
  7.  
  8. #include "misc.h"
  9. #include "lang.h"
  10. #include "color.h"
  11. #include "globals.h"
  12. #include <math.h>
  13. #include <iostream.h>
  14.  
  15.  
  16.  
  17.  
  18.   
  19.  
  20.  
  21. class FORMAT
  22. {
  23.     public:
  24.  
  25.         // constructors
  26.         FORMAT();
  27.         FORMAT(int id);
  28.         FORMAT(uint c1_mask,uint c2_mask,uint c3_mask,uint c4_mask=0,
  29.                int model=RGBA,int bits=DEFAULT); 
  30.         FORMAT(uint c1_position,uint c1_size,
  31.                uint c2_position,uint c2_size,
  32.                uint c3_position,uint c3_size,
  33.                uint c4_position=0,uint c4_size=0,
  34.                int model=RGBA,int bits=DEFAULT);
  35.  
  36.         // initialize
  37.         int init(int id);
  38.         int init(uint c1_mask,uint c2_mask,uint c3_mask,uint c4_mask=0,
  39.                  int model=RGBA,int bits=DEFAULT); 
  40.         int init(uint c1_position,uint c1_size,
  41.                  uint c2_position,uint c2_size,
  42.                  uint c3_position,uint c3_size,
  43.                  uint c4_position=0,uint c4_size=0,
  44.                  int model=RGBA,int bits=DEFAULT);
  45.  
  46.         // clear format
  47.         void clear();
  48.  
  49.         // color packing/unpacking
  50.         int pack(COLOR const &color,void *data) const;
  51.         int pack(uchar c1,uchar c2,uchar c3,uchar c4,void *data,int model=RGBA) const;
  52.         int unpack(void *data,COLOR &color,int mode=ABSOLUTE,int model=RGBA) const;
  53.         int unpack(void *data,uchar &c1,uchar &c2,uchar &c3,uchar &c4,int model=RGBA);
  54.  
  55.         // operators
  56.         inline void operator =(int const id);
  57.         inline void operator =(FORMAT const &other);
  58.         inline int operator ==(FORMAT const &other) const;
  59.         inline int operator !=(FORMAT const &other) const;
  60.  
  61.         // format status
  62.         int ok() const;
  63.  
  64.         // data
  65.         int id;                         // format id
  66.         int type;                       // format type
  67.         int model;                      // color model
  68.         int bits;                       // bits per pixel
  69.         int bytes;                      // bytes per pixel
  70.  
  71.         // field information struct
  72.         struct FIELDINFO
  73.         {
  74.             uint position;              // bit position of LSB
  75.             uint size;                  // size in bits
  76.             uint mask;                  // field mask
  77.             int shift;                  // shift magnitude to position (for uchar pack only - may be negative!)
  78.         };
  79.         
  80.         // component information
  81.         FIELDINFO c1;                   // |R|C|C|H|H|Y|Y| Y|
  82.         FIELDINFO c2;                   // |G|M|M|S|L|I|U|Cb|
  83.         FIELDINFO c3;                   // |B|Y|Y|I|S|Q|V|Cr|
  84.         FIELDINFO c4;                   // |A|A|K|A|A|A|A| A|
  85.  
  86.     private:
  87.  
  88.         // setup masks and other data
  89.         void setup();
  90.  
  91.         // get field information from mask
  92.         FIELDINFO getfieldinfo(uint mask);
  93. };
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. inline void FORMAT::operator =(const int format_id)
  104. {
  105.     // initialize with format id
  106.     init(format_id);
  107. }
  108.  
  109.  
  110. inline void FORMAT::operator =(FORMAT const &other)
  111. {
  112.     // fail if identical assign
  113.     if (&other==this) return;
  114.  
  115.     // assign
  116.     id=other.id;
  117.     type=other.type;
  118.     bits=other.bits;
  119.     bytes=other.bytes;
  120.     model=other.model;
  121.     c1=other.c1;
  122.     c2=other.c2;
  123.     c3=other.c3;
  124.     c4=other.c4;
  125. }
  126.  
  127.  
  128. inline int FORMAT::operator ==(FORMAT const &other) const
  129. {
  130.     // check for id match
  131.     if (id==other.id && id>=FORMATBASE && id<=FORMATMAX) return 1;
  132.  
  133.     // check data for match
  134.     if (type!=other.type || model!=other.model || bits!=other.bits || bytes!=other.bytes) return 0;
  135.  
  136.     // check bits per pixel
  137.     if (bits!=other.bits) return 0;
  138.  
  139.     // check components
  140.     if (c1.position!=other.c1.position || c1.size!=other.c1.size || c1.mask!=other.c1.mask || c1.shift!=other.c1.shift ||
  141.         c2.position!=other.c2.position || c2.size!=other.c2.size || c2.mask!=other.c2.mask || c2.shift!=other.c2.shift ||
  142.         c3.position!=other.c3.position || c3.size!=other.c3.size || c3.mask!=other.c3.mask || c3.shift!=other.c3.shift ||
  143.         c4.position!=other.c4.position || c4.size!=other.c4.size || c4.mask!=other.c4.mask || c4.shift!=other.c4.shift) return 0;
  144.     else return 1;
  145. }
  146.  
  147.         
  148. inline int FORMAT::operator !=(FORMAT const &other) const
  149. {
  150.     return !(*this==other);
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. #endif
  164.