1 /*
2   FILE  : NPixel.h
3 
4   CLASSES : NColor, NPixel
5 
6   AUTHOR  : Nicolas ROMANTZOFF
7 
8   PURPOSE : Provide a native class for pixels : aRGB on MOTOROLA, BGRa on INTEL...
9 
10   USAGE : Shouldn't have any .cpp with it...
11 
12   SCCSID      : @(#)npixel.h  1.1 11:50:03 18 Dec 1996
13   ----------------------------------------------------------------------------
14    Copyright (c) 1999 Digital Imaging Group, Inc.
15    For conditions of distribution and use, see copyright notice
16    in Flashpix.h
17   ----------------------------------------------------------------------------
18 */
19 #ifndef NPixel_h
20 #define NPixel_h
21 
22   #include "b_types.h"
23   #include "pixel.h"
24 
25 //  Classes declaration
26 //  -------------------
27 
28   class NPixel;
29     typedef NPixel* ptr_NPixel;
30 
31 //  Classes definition
32 //  ------------------
33 
34 //////////////////////////////////////////////////////////////////////////////////////////////////////
35   #ifdef __bigEndian
36     struct NColor {
37       unsigned8   alpha;
38       unsigned8   red;
39       unsigned8   green;
40       unsigned8   blue;
41     };
42   #else
43     struct NColor {
44       unsigned8   blue;
45       unsigned8   green;
46       unsigned8   red;
47       unsigned8   alpha;
48     };
49   #endif
50 
51 //////////////////////////////////////////////////////////////////////////////////////////////////////
52   class NPixel  {
53     public:
54                 NPixel(const unsigned32 color = 0);
55                 NPixel(const unsigned8 red, const unsigned8 green, const unsigned8 blue, const unsigned8 alpha = 0);
56                 NPixel(const NColor& col);
57                 NPixel(const NPixel& pix);
58                 NPixel(const Pixel& pix);
59           NPixel&   operator=(const NPixel& pix);
60           NPixel&   operator=(const NColor& col);
61           NPixel&   operator=(const unsigned32 color);
62 
63                 operator unsigned32(void) const;
64                 operator NColor(void) const;
65                 operator Pixel(void) const;
66 
67 
68           unsigned8 Alpha(void) const;
69           unsigned8 Red(void) const;
70           unsigned8 Green(void) const;
71           unsigned8 Blue(void) const;
72 
73           unsigned8&  Alpha(void);
74           unsigned8&  Red(void);
75           unsigned8&  Green(void);
76           unsigned8&  Blue(void);
77 
78       union {
79         NColor    itsStruct;
80         unsigned32  itsColor;
81       };
82   };
83 
84 
85   // Inlines
86   // -------
87 
88   // Swap a long from big to little endian or vice-versa
89   // Added by Peter Hughes 21 May 1996
90   #define SWAP4(x) ( (((x)>>24) & 0x000000FF) | (((x)>>8) & 0x0000FF00) | \
91              (((x)<<8) & 0x00FF0000) | (((x)<<24) & 0xFF000000) )
92 
NPixel(const unsigned32 color)93   inline  NPixel::NPixel(const unsigned32 color)
94   { itsColor = color; }
95 
NPixel(const unsigned8 red,const unsigned8 green,const unsigned8 blue,const unsigned8 alpha)96   inline  NPixel::NPixel(const unsigned8 red, const unsigned8 green, const unsigned8 blue, const unsigned8 alpha)
97   { itsStruct.red = red; itsStruct.green = green; itsStruct.blue = blue; itsStruct.alpha = alpha; }
98 
NPixel(const NColor & col)99   inline  NPixel::NPixel(const NColor& col)
100   { itsStruct = col; }
101 
NPixel(const NPixel & pix)102   inline  NPixel::NPixel(const NPixel& pix)
103   { itsColor = pix.itsColor; }
104 
NPixel(const Pixel & pix)105   inline  NPixel::NPixel(const Pixel& pix)
106   {
107     register unsigned32 newColor = *((unsigned32 *)(&pix));
108 #ifdef __bigEndian
109     itsColor = newColor;                    // Simple copy
110 #else
111     itsColor = SWAP4(newColor);                 // Bytes swapped copy
112 #endif
113   }
114 
115   inline  NPixel&   NPixel::operator= (const NPixel& pix)
116   { itsColor = pix.itsColor; return *this; }
117 
118   inline  NPixel&   NPixel::operator= (const NColor& col)
119   { itsStruct = col; return *this; }
120 
121   inline  NPixel&   NPixel::operator= (const unsigned32 color)
122   { itsColor = color; return *this; }
123 
unsigned32(void)124   inline  NPixel::operator unsigned32(void) const
125   { return itsColor; }
126 
NColor(void)127   inline  NPixel::operator NColor(void) const
128   { return itsStruct; }
129 
Pixel(void)130   inline  NPixel::operator Pixel(void) const
131   {
132     Pixel pix;
133 #ifdef __bigEndian
134     *((unsigned32 *)(&pix)) = itsColor;             // Simple copy
135 #else
136     *((unsigned32 *)(&pix)) = SWAP4(itsColor);          // Bytes swapped copy
137 #endif
138     return pix;
139   }
140 
Alpha(void)141   inline unsigned8 NPixel::Alpha(void) const
142   { return itsStruct.alpha; }
143 
Red(void)144   inline unsigned8 NPixel::Red(void) const
145   { return itsStruct.red; }
146 
Green(void)147   inline unsigned8 NPixel::Green(void) const
148   { return itsStruct.green; }
149 
Blue(void)150   inline unsigned8 NPixel::Blue(void) const
151   { return itsStruct.blue; }
152 
Alpha(void)153   inline unsigned8& NPixel::Alpha(void)
154   { return itsStruct.alpha; }
155 
Red(void)156   inline unsigned8& NPixel::Red(void)
157   { return itsStruct.red; }
158 
Green(void)159   inline unsigned8& NPixel::Green(void)
160   { return itsStruct.green; }
161 
Blue(void)162   inline unsigned8& NPixel::Blue(void)
163   { return itsStruct.blue; }
164 
165 #endif
166