1 #pragma once
2 
3 #ifndef T_PIXEL_INCLUDED
4 #define T_PIXEL_INCLUDED
5 
6 #include "tcommon.h"
7 #include "tmachine.h"
8 
9 #include <math.h>
10 
11 #undef DVAPI
12 #undef DVVAR
13 #ifdef TCOLOR_EXPORTS
14 #define DVAPI DV_EXPORT_API
15 #define DVVAR DV_EXPORT_VAR
16 #else
17 #define DVAPI DV_IMPORT_API
18 #define DVVAR DV_IMPORT_VAR
19 #endif
20 
21 //! r,g,b,m; 4 byte/pixel
22 class TPixelRGBM32;
23 //! r,g,b,m; 8 byte/pixel
24 class TPixelRGBM64;
25 //! POSSIBLY UNUSED! r:5,g:5,b:5; 2 byte/pixel; 1 bit unused
26 class TPixelRGB555;
27 //! POSSIBLY UNUSED! r:5,g:6,b:5; 2 byte/pixel
28 class TPixelRGB565;
29 //! Double r,g,b,m ; 16 byte/pixel
30 class TPixelD;
31 
32 //! Gray Scale 1 byte/pixel
33 class TPixelGR8;
34 //! Gray Scale 2 byte/pixel
35 class TPixelGR16;
36 
37 //-----------------------------------------------------------------------------
38 
39 /*! The standard pixel type: r,g,b,m; 1 byte/channel.
40     A set of predefined colors are included as well.
41     Note that channel ordering is platform depending. */
42 
43 class DVAPI DV_ALIGNED(4) TPixelRGBM32 {
TPixelRGBM32(TUINT32 mask)44   TPixelRGBM32(TUINT32 mask) { *(TUINT32 *)this = mask; };
45 
46 public:
47   static const int maxChannelValue;
48   typedef unsigned char Channel;
49 
50 #if defined(TNZ_MACHINE_CHANNEL_ORDER_BGRM)
51   Channel b, g, r, m;
52 #elif defined(TNZ_MACHINE_CHANNEL_ORDER_MBGR)
53   Channel m, b, g, r;
54 #elif defined(TNZ_MACHINE_CHANNEL_ORDER_RGBM)
55   unsigned char r, g, b, m;
56 #elif defined(TNZ_MACHINE_CHANNEL_ORDER_MRGB)
57   Channel m, r, g, b;
58 #else
59 #error "Undefined machine order !!!!"
60 #endif
61 
62 #ifdef MACOSX
63 
64 #ifdef powerpc
TPixelRGBM32()65   TPixelRGBM32() : m(maxChannelValue), r(0), g(0), b(0){};
TPixelRGBM32(const TPixelRGBM32 & pix)66   TPixelRGBM32(const TPixelRGBM32 &pix)
67       : m(pix.m), r(pix.r), g(pix.g), b(pix.b){};
68   TPixelRGBM32(int rr, int gg, int bb, int mm = maxChannelValue)
m(mm)69       : m(mm), r(rr), g(gg), b(bb){};
70 #else
TPixelRGBM32()71   TPixelRGBM32() : b(0), g(0), r(0), m(maxChannelValue){};
TPixelRGBM32(const TPixelRGBM32 & pix)72   TPixelRGBM32(const TPixelRGBM32 &pix)
73       : b(pix.b), g(pix.g), r(pix.r), m(pix.m){};
74   TPixelRGBM32(int rr, int gg, int bb, int mm = maxChannelValue)
b(bb)75       : b(bb), g(gg), r(rr), m(mm){};
76 #endif
77 
78 #else
79 
TPixelRGBM32()80   TPixelRGBM32() : r(0), g(0), b(0), m(maxChannelValue){};
81   TPixelRGBM32(int rr, int gg, int bb, int mm = maxChannelValue)
r(rr)82       : r(rr), g(gg), b(bb), m(mm){};
83 
84   // Copy constructor and operator=
TPixelRGBM32(const TPixelRGBM32 & pix)85   TPixelRGBM32(const TPixelRGBM32 &pix) {
86     *(TUINT32 *)this = *(const TUINT32 *)&pix;
87   }
88 
89   TPixelRGBM32 &operator=(const TPixelRGBM32 &pix) {
90     *(TUINT32 *)this = *(const TUINT32 *)&pix;
91     return *this;
92   }
93 
94 #endif
95 
96 public:
97   inline bool operator==(const TPixelRGBM32 &p) const {
98     return *(const TUINT32 *)this == *(const TUINT32 *)&p;
99   }
100   inline bool operator!=(const TPixelRGBM32 &p) const {
101     return *(const TUINT32 *)this != *(const TUINT32 *)&p;
102   }
103 
104   inline bool operator<(const TPixelRGBM32 &p) const {
105     return *(const TUINT32 *)this < *(const TUINT32 *)&p;
106   }
107   inline bool operator>=(const TPixelRGBM32 &p) const {
108     return *(const TUINT32 *)this >= *(const TUINT32 *)&p;
109   }
110 
111   inline bool operator>(const TPixelRGBM32 &p) const {
112     return *(const TUINT32 *)this > *(const TUINT32 *)&p;
113   }
114   inline bool operator<=(const TPixelRGBM32 &p) const {
115     return *(const TUINT32 *)this <= *(const TUINT32 *)&p;
116   }
117 
118   /*
119 //!Returns itself
120 static inline TPixelRGBM32 from(const TPixelRGBM32 &pix) {return pix;};
121 //!Converts TPixelRGBM64 into TPixelRGBM32
122 static inline TPixelRGBM32 from(const TPixelRGBM64 &pix);
123 //!Converts TPixelGR8 into TPixelRGBM32
124 static TPixelRGBM32 from(const TPixelGR8 &pix);
125 //!Converts TPixelGR16 into TPixelRGBM32
126 static TPixelRGBM32 from(const TPixelGR16 &pix);
127 //!In this conversion instead of truncating values fron 64 to 32 a randomic
128 dithering is performed.
129 //!r is a unsigned int random value
130 static inline TPixelRGBM32 from(const TPixelRGBM64 &pix, TUINT32 r); // per il
131 dithering
132 // ecc..
133 
134 //!Converts TPixelD into TPixelRGBM32
135 // static inline TPixelRGBM32 from(const TPixelD &pix);
136 */
137   static const TPixelRGBM32 Red;
138   static const TPixelRGBM32 Green;
139   static const TPixelRGBM32 Blue;
140   static const TPixelRGBM32 Yellow;
141   static const TPixelRGBM32 Cyan;
142   static const TPixelRGBM32 Magenta;
143   static const TPixelRGBM32 White;
144   static const TPixelRGBM32 Black;
145   static const TPixelRGBM32 Transparent;
146 };
147 
148 //-----------------------------------------------------------------------------
149 /*!The standard pixel type: r,g,b,m; 2 byte/channel.
150   A set of predefined colors are included as well.
151   Note that channel ordering is platform depending. */
152 //  8 byte alignment cannot be specified for function parameters
153 //  in Visual Studio 32bit platform.
154 //  Since SSE2 mostly require 16 byte aligned, changing 8 byte align to 4 byte
155 //  align will not cause problems.
156 #if defined(_MSC_VER) && !defined(x64)
157 class DVAPI DV_ALIGNED(4) TPixelRGBM64 {
158 #else
159 class DVAPI DV_ALIGNED(8) TPixelRGBM64 {
160 #endif
161 public:
162   static const int maxChannelValue;
163   typedef unsigned short Channel;
164 
165 #ifdef TNZ_MACHINE_CHANNEL_ORDER_BGRM
166   Channel b, g, r, m;
167 #elif defined(TNZ_MACHINE_CHANNEL_ORDER_MRGB)
168   Channel m, r, g, b;
169 #elif defined(TNZ_MACHINE_CHANNEL_ORDER_RGBM)
170 Channel r, g, b, m;
171 #else
172 undefined machine order !!!!
173 #endif
174 
175 #ifdef _WIN32
176   TPixelRGBM64() : r(0), g(0), b(0), m(maxChannelValue){};
177   TPixelRGBM64(int rr, int gg, int bb, int mm = maxChannelValue)
r(rr)178       : r(rr), g(gg), b(bb), m(mm){};
179 #else
180 #if defined(LINUX) || defined(FREEBSD) || defined(MACOSX)
181 
182 #ifdef powerpc
183 
184   TPixelRGBM64() : m(maxChannelValue), b(0), g(0), r(0){};
185   TPixelRGBM64(int rr, int gg, int bb, int mm = maxChannelValue)
186       : m(mm), b(bb), g(gg), r(rr){};
187 #else
188 
189   TPixelRGBM64() : b(0), g(0), r(0), m(maxChannelValue){};
190   TPixelRGBM64(int rr, int gg, int bb, int mm = maxChannelValue)
191       : b(bb), g(gg), r(rr), m(mm){};
192 
193 #endif
194 #endif
195 #endif
196 
197   // Copy constructor and operator=
TPixelRGBM64(const TPixelRGBM64 & pix)198   TPixelRGBM64(const TPixelRGBM64 &pix) {
199     *(TUINT64 *)this = *(const TUINT64 *)&pix;
200   }
201 
202   TPixelRGBM64 &operator=(const TPixelRGBM64 &pix) {
203     *(TUINT64 *)this = *(const TUINT64 *)&pix;
204     return *this;
205   }
206 
207 public:
208   inline bool operator==(const TPixelRGBM64 &p) const {
209     return *(const TUINT64 *)this == *(const TUINT64 *)&p;
210   }
211   inline bool operator!=(const TPixelRGBM64 &p) const {
212     return *(const TUINT64 *)this != *(const TUINT64 *)&p;
213   }
214 
215   inline bool operator<(const TPixelRGBM64 &p) const {
216     return *(const TUINT64 *)this < *(const TUINT64 *)&p;
217   }
218   inline bool operator>=(const TPixelRGBM64 &p) const { return !operator<(p); }
219 
220   inline bool operator>(const TPixelRGBM64 &p) const {
221     return *(const TUINT64 *)this > *(const TUINT64 *)&p;
222   }
223   inline bool operator<=(const TPixelRGBM64 &p) const { return !operator>(p); }
224 
225   /*
226 //!Converts TPixelRGBM32 into TPixelRGBM64
227 static inline TPixelRGBM64 from(const TPixelRGBM32 &pix);
228 //!Converts TPixelGR8 into TPixelRGBM64
229 static TPixelRGBM64 from(const TPixelGR8 &pix   );
230 //!Converts TPixelGR16 into TPixelRGBM64
231 static TPixelRGBM64 from(const TPixelGR16 &pix  );
232 //!Converts TPixelD into TPixelRGBM64
233 static inline TPixelRGBM64 from(const TPixelD &pix);
234 */
235 
236   static const TPixelRGBM64 Red;
237   static const TPixelRGBM64 Green;
238   static const TPixelRGBM64 Blue;
239   static const TPixelRGBM64 Yellow;
240   static const TPixelRGBM64 Cyan;
241   static const TPixelRGBM64 Magenta;
242   static const TPixelRGBM64 White;
243   static const TPixelRGBM64 Black;
244   static const TPixelRGBM64 Transparent;
245 };
246 
247 //-----------------------------------------------------------------------------
248 
249 //! TPixel32 is a shortcut for TPixelRGBM32. Use it!
250 typedef TPixelRGBM32 TPixel32;
251 //! TPixel is a shortcut for TPixelRGBM32.
252 typedef TPixelRGBM32 TPixel;
253 //! TPixel64 is a shortcut for TPixelRGBM64. Use it!
254 typedef TPixelRGBM64 TPixel64;
255 
256 //-----------------------------------------------------------------------------
257 
258 class DVAPI TPixelD {
259 public:
260   typedef double Channel;
261 
262   Channel r, g, b, m;
263 
TPixelD()264   TPixelD() : r(0), g(0), b(0), m(1){};
TPixelD(const TPixelD & pix)265   TPixelD(const TPixelD &pix) : r(pix.r), g(pix.g), b(pix.b), m(pix.m){};
266   TPixelD(double rr, double gg, double bb, double mm = 1)
r(rr)267       : r(rr), g(gg), b(bb), m(mm){};
268 
269   inline bool operator==(const TPixelD &p) const {
270     return r == p.r && g == p.g && b == p.b && m == p.m;
271   };
272   inline bool operator<(const TPixelD &p) const {
273     return r < p.r ||
274            (r == p.r &&
275             (g < p.g || (g == p.g && (b < p.b || (b == p.b && (m < p.m))))));
276   };
277 
278   inline bool operator>=(const TPixelD &p) const { return !operator<(p); };
279   inline bool operator!=(const TPixelD &p) const { return !operator==(p); };
280   inline bool operator>(const TPixelD &p) const {
281     return !operator<(p) && !operator==(p);
282   };
283   inline bool operator<=(const TPixelD &p) const { return !operator>(p); };
284 
285   inline TPixelD operator*=(const TPixelD &p) {
286     r *= p.r;
287     g *= p.g;
288     b *= p.b;
289     m *= p.m;
290     return *this;
291   }
292   inline TPixelD operator*(const TPixelD &p) const {
293     TPixelD ret(*this);
294     return ret *= p;
295   }
296 
297   /*
298 //!Returns TPixelRGBM32 into TPixelD
299 static inline TPixelD from(const TPixelRGBM32 &pix);
300 //!Converts TPixelRGBM64 into TPixelRGBM32
301 static inline TPixelD from(const TPixelRGBM64 &pix);
302 //!Converts TPixelGR8 into TPixelRGBM32
303 static TPixelD from(const TPixelGR8 &pix);
304 //!Converts TPixelGR16 into TPixelRGBM32
305 static TPixelD from(const TPixelGR16 &pix);
306 //!Returns itself
307 static inline TPixelD from(const TPixelD &pix) {return pix;};
308 */
309 
310   static const TPixelD Red;
311   static const TPixelD Green;
312   static const TPixelD Blue;
313   static const TPixelD Yellow;
314   static const TPixelD Cyan;
315   static const TPixelD Magenta;
316   static const TPixelD White;
317   static const TPixelD Black;
318   static const TPixelD Transparent;
319 };
320 
321 //-----------------------------------------------------------------------------
322 
323 class DVAPI TPixelCY {
324 public:
325   UCHAR c, y;
326 };
327 
328 /*
329 
330 TPixel64 DVAPI TPixel64::from(const TPixel32 &pix)
331 {
332    return TPixel64(
333          ushortFromByte(pix.r),
334          ushortFromByte(pix.g),
335          ushortFromByte(pix.b),
336          ushortFromByte(pix.m));
337 }
338 
339 //-----------------------------------------------------------------------------
340 
341 TPixel32 DVAPI TPixel32::from(const TPixel64 &pix)
342 {
343    return TPixel32(
344          byteFromUshort(pix.r),
345          byteFromUshort(pix.g),
346          byteFromUshort(pix.b),
347          byteFromUshort(pix.m));
348 }
349 
350 //-----------------------------------------------------------------------------
351 
352 TPixelD DVAPI TPixelD::from(const TPixel32 &pix)
353 {
354   const double k = 1.0/255.0;
355   return TPixelD(k*pix.r,k*pix.g,k*pix.b,k*pix.m);
356 }
357 
358 //-----------------------------------------------------------------------------
359 
360 TPixelD DVAPI TPixelD::from(const TPixel64 &pix)
361 {
362   const double k = 1.0/65535.0;
363   return TPixelD(k*pix.r,k*pix.g,k*pix.b,k*pix.m);
364 }
365 
366 */
367 //-----------------------------------------------------------------------------
368 /*
369 TPixel32 DVAPI TPixel32::from(const TPixelD &pix)
370 {
371   const int max = 255;
372   return TPixel32(
373     tcrop((int)(pix.r*max), 0,max),
374     tcrop((int)(pix.g*max), 0,max),
375     tcrop((int)(pix.b*max), 0,max),
376     tcrop((int)(pix.m*max), 0,max));
377 }
378 */
379 //-----------------------------------------------------------------------------
380 /*
381 TPixel64 DVAPI TPixel64::from(const TPixelD &pix)
382 {
383   const int max = 65535;
384   return TPixel64(
385     tcrop((int)(pix.r*max), 0,max),
386     tcrop((int)(pix.g*max), 0,max),
387     tcrop((int)(pix.b*max), 0,max),
388     tcrop((int)(pix.m*max), 0,max));
389 }
390 */
391 //-----------------------------------------------------------------------------
392 
393 #endif  //__T_PIXEL_INCLUDED
394