0024428: Implementation of LGPL license
[occt.git] / src / Image / Image_Color.hxx
1 // Created on: 2014-07-18
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Image_Color_H__
17 #define _Image_Color_H__
18
19 #include <Standard.hxx>
20
21 //! POD structure for packed RGB color value (3 bytes)
22 struct Image_ColorRGB
23 {
24
25   //! Component type.
26   typedef Standard_Byte ComponentType_t;
27
28   //! Returns the number of components.
29   static Standard_Integer Length()
30   {
31     return 3;
32   }
33
34 public: // access methods
35
36   //! Alias to 1st component (red intensity).
37   Standard_Byte r() const { return v[0]; }
38
39   //! Alias to 2nd component (green intensity).
40   Standard_Byte g() const { return v[1]; }
41
42   //! Alias to 3rd component (blue intensity).
43   Standard_Byte b() const { return v[2]; }
44
45   //! Alias to 1st component (red intensity).
46   Standard_Byte& r() { return v[0]; }
47
48   //! Alias to 2nd component (green intensity).
49   Standard_Byte& g() { return v[1]; }
50
51   //! Alias to 3rd component (blue intensity).
52   Standard_Byte& b() { return v[2]; }
53
54 public:
55
56   Standard_Byte v[3];
57
58 };
59
60 //! POD structure for packed RGB color value (4 bytes with extra byte for alignment)
61 struct Image_ColorRGB32
62 {
63
64   //! Component type.
65   typedef Standard_Byte ComponentType_t;
66
67   //! Returns the number of components.
68   static Standard_Integer Length()
69   {
70     return 3;
71   }
72
73   //! Alias to 1st component (red intensity).
74   Standard_Byte r() const { return v[0]; }
75
76   //! Alias to 2nd component (green intensity).
77   Standard_Byte g() const { return v[1]; }
78
79   //! Alias to 3rd component (blue intensity).
80   Standard_Byte b() const { return v[2]; }
81
82   //! Alias to 4th component (dummy).
83   Standard_Byte a_() const { return v[3]; }
84
85   //! Alias to 1st component (red intensity).
86   Standard_Byte& r() { return v[0]; }
87
88   //! Alias to 2nd component (green intensity).
89   Standard_Byte& g() { return v[1]; }
90
91   //! Alias to 3rd component (blue intensity).
92   Standard_Byte& b() { return v[2]; }
93
94   //! Alias to 4th component (dummy).
95   Standard_Byte& a_() { return v[3]; }
96
97 public:
98
99   Standard_Byte v[4];
100
101 };
102
103 //! POD structure for packed RGBA color value (4 bytes)
104 struct Image_ColorRGBA
105 {
106
107   //! Component type.
108   typedef Standard_Byte ComponentType_t;
109
110   //! Returns the number of components.
111   static Standard_Integer Length()
112   {
113     return 4;
114   }
115
116   //! Alias to 1st component (red intensity).
117   Standard_Byte r() const { return v[0]; }
118
119   //! Alias to 2nd component (green intensity).
120   Standard_Byte g() const { return v[1]; }
121
122   //! Alias to 3rd component (blue intensity).
123   Standard_Byte b() const { return v[2]; }
124
125   //! Alias to 4th component (alpha value).
126   Standard_Byte a() const { return v[3]; }
127
128   //! Alias to 1st component (red intensity).
129   Standard_Byte& r() { return v[0]; }
130
131   //! Alias to 2nd component (green intensity).
132   Standard_Byte& g() { return v[1]; }
133
134   //! Alias to 3rd component (blue intensity).
135   Standard_Byte& b() { return v[2]; }
136
137   //! Alias to 4th component (alpha value).
138   Standard_Byte& a() { return v[3]; }
139
140 public:
141
142   Standard_Byte v[4];
143
144 };
145
146 //! POD structure for packed BGR color value (3 bytes)
147 struct Image_ColorBGR
148 {
149
150   //! Component type.
151   typedef Standard_Byte ComponentType_t;
152
153   //! Returns the number of components.
154   static Standard_Integer Length()
155   {
156     return 3;
157   }
158
159   //! Alias to 3rd component (red intensity).
160   Standard_Byte r() const { return v[2]; }
161
162   //! Alias to 2nd component (green intensity).
163   Standard_Byte g() const { return v[1]; }
164
165   //! Alias to 1st component (blue intensity).
166   Standard_Byte b() const { return v[0]; }
167
168   //! Alias to 3rd component (red intensity).
169   Standard_Byte& r() { return v[2]; }
170
171   //! Alias to 2nd component (green intensity).
172   Standard_Byte& g() { return v[1]; }
173
174   //! Alias to 1st component (blue intensity).
175   Standard_Byte& b() { return v[0]; }
176
177 public:
178
179   Standard_Byte v[3];
180
181 };
182
183 //! POD structure for packed BGR color value (4 bytes with extra byte for alignment)
184 struct Image_ColorBGR32
185 {
186
187   //! Component type.
188   typedef Standard_Byte ComponentType_t;
189
190   //! Returns the number of components.
191   static Standard_Integer Length()
192   {
193     return 3;
194   }
195
196   //! Alias to 3rd component (red intensity).
197   Standard_Byte r() const { return v[2]; }
198
199   //! Alias to 2nd component (green intensity).
200   Standard_Byte g() const { return v[1]; }
201
202   //! Alias to 1st component (blue intensity).
203   Standard_Byte b() const { return v[0]; }
204
205   //! Alias to 4th component (dummy).
206   Standard_Byte a_() const { return v[3]; }
207
208   //! Alias to 3rd component (red intensity).
209   Standard_Byte& r() { return v[2]; }
210
211   //! Alias to 2nd component (green intensity).
212   Standard_Byte& g() { return v[1]; }
213
214   //! Alias to 1st component (blue intensity).
215   Standard_Byte& b() { return v[0]; }
216
217   //! Alias to 4th component (dummy).
218   Standard_Byte& a_() { return v[3]; }
219
220 public:
221
222   Standard_Byte v[4];
223
224 };
225
226 //! POD structure for packed BGRA color value (4 bytes)
227 struct Image_ColorBGRA
228 {
229
230   //! Component type.
231   typedef Standard_Byte ComponentType_t;
232
233   //! Returns the number of components.
234   static Standard_Integer Length()
235   {
236     return 4;
237   }
238
239   //! Alias to 3rd component (red intensity).
240   Standard_Byte r() const { return v[2]; }
241
242   //! Alias to 2nd component (green intensity).
243   Standard_Byte g() const { return v[1]; }
244
245   //! Alias to 1st component (blue intensity).
246   Standard_Byte b() const { return v[0]; }
247
248   //! Alias to 4th component (alpha value).
249   Standard_Byte a() const { return v[3]; }
250
251   //! Alias to 3rd component (red intensity).
252   Standard_Byte& r() { return v[2]; }
253
254   //! Alias to 2nd component (green intensity).
255   Standard_Byte& g() { return v[1]; }
256
257   //! Alias to 1st component (blue intensity).
258   Standard_Byte& b() { return v[0]; }
259
260   //! Alias to 4th component (alpha value).
261   Standard_Byte& a() { return v[3]; }
262
263 public:
264
265   Standard_Byte v[4];
266
267 };
268
269 //! POD structure for packed float RGB color value (3 floats)
270 struct Image_ColorRGBF
271 {
272
273   //! Component type.
274   typedef Standard_ShortReal ComponentType_t;
275
276   //! Returns the number of components.
277   static Standard_Integer Length()
278   {
279     return 3;
280   }
281
282   //! Alias to 1st component (red intensity).
283   Standard_ShortReal r() const { return v[0]; }
284
285   //! Alias to 2nd component (green intensity).
286   Standard_ShortReal g() const { return v[1]; }
287
288   //! Alias to 3rd component (blue intensity).
289   Standard_ShortReal b() const { return v[2]; }
290
291   //! Alias to 1st component (red intensity).
292   Standard_ShortReal& r() { return v[0]; }
293
294   //! Alias to 2nd component (green intensity).
295   Standard_ShortReal& g() { return v[1]; }
296
297   //! Alias to 3rd component (blue intensity).
298   Standard_ShortReal& b() { return v[2]; }
299
300 public:
301
302   Standard_ShortReal v[3];
303
304 };
305
306 //! POD structure for packed BGR float color value (3 floats)
307 struct Image_ColorBGRF
308 {
309
310   //! Component type.
311   typedef Standard_ShortReal ComponentType_t;
312
313   //! Returns the number of components.
314   static Standard_Integer Length()
315   {
316     return 3;
317   }
318
319   //! Alias to 3rd component (red intensity).
320   Standard_ShortReal r() const { return v[2]; }
321
322   //! Alias to 2nd component (green intensity).
323   Standard_ShortReal g() const { return v[1]; }
324
325   //! Alias to 1st component (blue intensity).
326   Standard_ShortReal b() const { return v[0]; }
327
328   //! Alias to 3rd component (red intensity).
329   Standard_ShortReal& r() { return v[2]; }
330
331   //! Alias to 2nd component (green intensity).
332   Standard_ShortReal& g() { return v[1]; }
333
334   //! Alias to 1st component (blue intensity).
335   Standard_ShortReal& b() { return v[0]; }
336
337 public:
338
339   Standard_ShortReal v[3];
340
341 };
342
343 //! POD structure for packed RGBA color value (4 floats)
344 struct Image_ColorRGBAF
345 {
346
347   //! Component type.
348   typedef Standard_ShortReal ComponentType_t;
349
350   //! Returns the number of components.
351   static Standard_Integer Length()
352   {
353     return 4;
354   }
355
356   //! Alias to 1st component (red intensity).
357   Standard_ShortReal r() const { return v[0]; }
358
359   //! Alias to 2nd component (green intensity).
360   Standard_ShortReal g() const { return v[1]; }
361
362   //! Alias to 3rd component (blue intensity).
363   Standard_ShortReal b() const { return v[2]; }
364
365   //! Alias to 4th component (alpha value).
366   Standard_ShortReal a() const { return v[3]; }
367
368   //! Alias to 1st component (red intensity).
369   Standard_ShortReal& r() { return v[0]; }
370
371   //! Alias to 2nd component (green intensity).
372   Standard_ShortReal& g() { return v[1]; }
373
374   //! Alias to 3rd component (blue intensity).
375   Standard_ShortReal& b() { return v[2]; }
376
377   //! Alias to 4th component (alpha value).
378   Standard_ShortReal& a() { return v[3]; }
379
380 public:
381
382   Standard_ShortReal v[4];
383
384 };
385
386 //! POD structure for packed float BGRA color value (4 floats)
387 struct Image_ColorBGRAF
388 {
389
390   //! Component type.
391   typedef Standard_ShortReal ComponentType_t;
392
393   //! Returns the number of components.
394   static Standard_Integer Length()
395   {
396     return 4;
397   }
398
399   //! Alias to 3rd component (red intensity).
400   Standard_ShortReal r() const { return v[2]; }
401
402   //! Alias to 2nd component (green intensity).
403   Standard_ShortReal g() const { return v[1]; }
404
405   //! Alias to 1st component (blue intensity).
406   Standard_ShortReal b() const { return v[0]; }
407
408   //! Alias to 4th component (alpha value).
409   Standard_ShortReal a() const { return v[3]; }
410
411   //! Alias to 3rd component (red intensity).
412   Standard_ShortReal& r() { return v[2]; }
413
414   //! Alias to 2nd component (green intensity).
415   Standard_ShortReal& g() { return v[1]; }
416
417   //! Alias to 1st component (blue intensity).
418   Standard_ShortReal& b() { return v[0]; }
419
420   //! Alias to 4th component (alpha value).
421   Standard_ShortReal& a() { return v[3]; }
422
423 public:
424
425   Standard_ShortReal v[4];
426
427 };
428
429 //! Addition operator
430 template<typename ColorType_t>
431 inline ColorType_t Image_ColorSumm3 (const ColorType_t& theA, const ColorType_t& theB)
432 {
433   ColorType_t aRes = {{typename ColorType_t::ComponentType_t (theA.v[0] + theB.v[0]),
434                        typename ColorType_t::ComponentType_t (theA.v[1] + theB.v[1]),
435                        typename ColorType_t::ComponentType_t (theA.v[2] + theB.v[2])}};
436   return aRes;
437 }
438
439 inline Image_ColorRGB operator+ (const Image_ColorRGB& theA, const Image_ColorRGB& theB)
440 {
441   return Image_ColorSumm3 (theA, theB);
442 }
443
444 inline Image_ColorBGR operator+ (const Image_ColorBGR& theA, const Image_ColorBGR& theB)
445 {
446   return Image_ColorSumm3 (theA, theB);
447 }
448
449 inline Image_ColorRGBF operator+ (const Image_ColorRGBF& theA, const Image_ColorRGBF& theB)
450 {
451   return Image_ColorSumm3 (theA, theB);
452 }
453
454 inline Image_ColorBGRF operator+ (const Image_ColorBGRF& theA, const Image_ColorBGRF& theB)
455 {
456   return Image_ColorSumm3 (theA, theB);
457 }
458
459 template<typename ColorType_t>
460 inline ColorType_t Image_ColorSumm4 (const ColorType_t& theA, const ColorType_t& theB)
461 {
462   ColorType_t aRes = {{typename ColorType_t::ComponentType_t (theA.v[0] + theB.v[0]),
463                        typename ColorType_t::ComponentType_t (theA.v[1] + theB.v[1]),
464                        typename ColorType_t::ComponentType_t (theA.v[2] + theB.v[2]),
465                        typename ColorType_t::ComponentType_t (theA.v[3] + theB.v[3])}};
466   return aRes;
467 }
468
469 inline Image_ColorRGBA operator+ (const Image_ColorRGBA& theA, const Image_ColorRGBA& theB)
470 {
471   return Image_ColorSumm4 (theA, theB);
472 }
473
474 inline Image_ColorBGRA operator+ (const Image_ColorBGRA& theA, const Image_ColorBGRA& theB)
475 {
476   return Image_ColorSumm4 (theA, theB);
477 }
478
479 inline Image_ColorRGB32 operator+ (const Image_ColorRGB32& theA, const Image_ColorRGB32& theB)
480 {
481   return Image_ColorSumm4 (theA, theB);
482 }
483
484 inline Image_ColorBGR32 operator+ (const Image_ColorBGR32& theA, const Image_ColorBGR32& theB)
485 {
486   return Image_ColorSumm4 (theA, theB);
487 }
488
489 inline Image_ColorRGBAF operator+ (const Image_ColorRGBAF& theA, const Image_ColorRGBAF& theB)
490 {
491   return Image_ColorSumm4 (theA, theB);
492 }
493
494 inline Image_ColorBGRAF operator+ (const Image_ColorBGRAF& theA, const Image_ColorBGRAF& theB)
495 {
496   return Image_ColorSumm4 (theA, theB);
497 }
498
499 //! Subtraction operator
500 template<typename ColorType_t>
501 inline ColorType_t Image_ColorSub3 (const ColorType_t& theA, const ColorType_t& theB)
502 {
503   ColorType_t aRes = {{typename ColorType_t::ComponentType_t (theA.v[0] - theB.v[0]),
504                        typename ColorType_t::ComponentType_t (theA.v[1] - theB.v[1]),
505                        typename ColorType_t::ComponentType_t (theA.v[2] - theB.v[2])}};
506   return aRes;
507 }
508
509 inline Image_ColorRGB operator- (const Image_ColorRGB& theA, const Image_ColorRGB& theB)
510 {
511   return Image_ColorSub3 (theA, theB);
512 }
513
514 inline Image_ColorBGR operator- (const Image_ColorBGR& theA, const Image_ColorBGR& theB)
515 {
516   return Image_ColorSub3 (theA, theB);
517 }
518
519 inline Image_ColorRGBF operator- (const Image_ColorRGBF& theA, const Image_ColorRGBF& theB)
520 {
521   return Image_ColorSub3 (theA, theB);
522 }
523
524 inline Image_ColorBGRF operator- (const Image_ColorBGRF& theA, const Image_ColorBGRF& theB)
525 {
526   return Image_ColorSub3 (theA, theB);
527 }
528
529 template<typename ColorType_t>
530 inline ColorType_t Image_ColorSub4 (const ColorType_t& theA, const ColorType_t& theB)
531 {
532   ColorType_t aRes = {{typename ColorType_t::ComponentType_t (theA.v[0] - theB.v[0]),
533                        typename ColorType_t::ComponentType_t (theA.v[1] - theB.v[1]),
534                        typename ColorType_t::ComponentType_t (theA.v[2] - theB.v[2]),
535                        typename ColorType_t::ComponentType_t (theA.v[3] - theB.v[3])}};
536   return aRes;
537 }
538
539 inline Image_ColorRGBA operator- (const Image_ColorRGBA& theA, const Image_ColorRGBA& theB)
540 {
541   return Image_ColorSub4 (theA, theB);
542 }
543
544 inline Image_ColorBGRA operator- (const Image_ColorBGRA& theA, const Image_ColorBGRA& theB)
545 {
546   return Image_ColorSub4 (theA, theB);
547 }
548
549 inline Image_ColorRGB32 operator- (const Image_ColorRGB32& theA, const Image_ColorRGB32& theB)
550 {
551   return Image_ColorSub4 (theA, theB);
552 }
553
554 inline Image_ColorBGR32 operator- (const Image_ColorBGR32& theA, const Image_ColorBGR32& theB)
555 {
556   return Image_ColorSub4 (theA, theB);
557 }
558
559 inline Image_ColorRGBAF operator- (const Image_ColorRGBAF& theA, const Image_ColorRGBAF& theB)
560 {
561   return Image_ColorSub4 (theA, theB);
562 }
563
564 inline Image_ColorBGRAF operator- (const Image_ColorBGRAF& theA, const Image_ColorBGRAF& theB)
565 {
566   return Image_ColorSub4 (theA, theB);
567 }
568
569 #endif // _Image_Color_H__