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