ICF 3.1.1.10
Technical documentation of ICF Libraries
CPixelManip.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#ifndef iipr_CPixelManip_included
7#define iipr_CPixelManip_included
8
9
10// Qt includes
11#include <QtCore/QtGlobal>
12
13
14namespace iipr
15{
16
17
18#pragma pack(push, 1)
19
20
25{
26public:
27 class Rgba
28 {
29 public:
31 : m_r(0), m_g(0), m_b(0), m_a(255)
32 {
33 }
34
35 Rgba(quint8 r, quint8 g, quint8 b, quint8 a = 255)
36 : m_r(r), m_g(g), m_b(b), m_a(a)
37 {
38 }
39
40 quint8 m_r;
41 quint8 m_g;
42 quint8 m_b;
43 quint8 m_a;
44 };
45
46 class Rgb
47 {
48 public:
50 : m_r(0), m_g(0), m_b(0)
51 {
52 }
53
54 Rgb(quint8 r, quint8 g, quint8 b)
55 : m_r(r), m_g(g), m_b(b)
56 {
57 }
58
59 quint8 m_r;
60 quint8 m_g;
61 quint8 m_b;
62 };
63
64 template <typename IntType, int Shift, bool DoCropMin = true, bool DoCropMax = true>
66 {
67 public:
69 {
70 }
71
73 : m_gray(value.m_gray)
74 {
75 }
76
77 GrayCropAccum32(int value)
78 : m_gray(IntType(value) << Shift)
79 {
80 }
81
82 GrayCropAccum32(quint8 value)
83 : m_gray(IntType(value) << Shift)
84 {
85 }
86
87 GrayCropAccum32(quint16 value)
88 : m_gray(IntType(value) << Shift)
89 {
90 }
91
92 GrayCropAccum32(quint32 value)
93 : m_gray(IntType(value) << Shift)
94 {
95 }
96
97 GrayCropAccum32(quint64 value)
98 : m_gray(IntType(value) << Shift)
99 {
100 }
101
102 GrayCropAccum32(double value)
103 : m_gray(IntType(value * (1 << Shift)))
104 {
105 }
106
107 bool operator==(const GrayCropAccum32& value)
108 {
109 return m_gray == value.m_gray;
110 }
111
112 bool operator==(int value)
113 {
114 return m_gray == (IntType(value) << Shift);
115 }
116
117 bool operator==(double value)
118 {
119 return m_gray == IntType(value * (1 << Shift));
120 }
121
122 bool operator!=(const GrayCropAccum32& value)
123 {
124 return m_gray != value.m_gray;
125 }
126
127 bool operator!=(int value)
128 {
129 return m_gray != (IntType(value) << Shift);
130 }
131
132 bool operator!=(double value)
133 {
134 return m_gray != IntType(value * (1 << Shift));
135 }
136
138 {
139 m_gray = value.m_gray;
140
141 return *this;
142 }
143/*
144 GrayCropAccum32& operator=(unsigned long long value)
145 {
146 m_gray = IntType(value) << Shift;
147
148 return *this;
149 }
150
151 GrayCropAccum32& operator=(unsigned int value)
152 {
153 m_gray = IntType(value) << Shift;
154
155 return *this;
156 }
157
158 GrayCropAccum32& operator=(double value)
159 {
160 m_gray = IntType(value * (1 << Shift));
161
162 return *this;
163 }
164*/
166 {
167 m_gray += value.m_gray;
168
169 return *this;
170 }
171
172 GrayCropAccum32& operator+=(unsigned int value)
173 {
174 m_gray += IntType(value) << Shift;
175
176 return *this;
177 }
178
180 {
181 m_gray += IntType(value * (1 << Shift));
182
183 return *this;
184 }
185
187 {
188 m_gray -= value.m_gray;
189
190 return *this;
191 }
192
193 GrayCropAccum32& operator-=(unsigned int value)
194 {
195 m_gray -= IntType(value) << Shift;
196
197 return *this;
198 }
199
201 {
202 m_gray -= IntType(value * (1 << Shift));
203
204 return *this;
205 }
206
208 {
209 m_gray = IntType((quint64(m_gray) * quint64(value.m_gray)) >> Shift);
210
211 return *this;
212 }
213
214 GrayCropAccum32& operator*=(unsigned int value)
215 {
216 m_gray *= IntType(value);
217
218 return *this;
219 }
220
222 {
223 m_gray = IntType(m_gray * value);
224
225 return *this;
226 }
227
229 {
230 m_gray = m_gray / value;
231
232 return *this;
233 }
234
236 {
237 m_gray = IntType(m_gray / value);
238
239 return *this;
240 }
241
242 operator quint8()
243 {
244
245 if (DoCropMin && (m_gray < 0)){
246 return 0;
247 }
248 if (DoCropMax && (m_gray > (IntType(0xff) << Shift))){
249 return 0xff;
250 }
251
252 return quint8(m_gray >> Shift);
253 }
254
255 operator quint16()
256 {
257 if (DoCropMin && (m_gray < 0)){
258 return 0;
259 }
260 if (DoCropMin && (m_gray > (IntType(0xffff) << Shift))){
261 return 0xffff;
262 }
263
264 return quint16(m_gray >> Shift);
265 }
266
267 operator quint32()
268 {
269 if (DoCropMin && (m_gray < 0)){
270 return 0;
271 }
272
273 return quint32(m_gray >> Shift);
274 }
275
276 operator Rgba()
277 {
278 quint8 grayValue;
279
280 if (DoCropMin && (m_gray < 0)){
281 grayValue = 0;
282 }
283 else if (DoCropMin && (m_gray > (0xff << Shift))){
284 grayValue = 0xff;
285 }
286 else{
287 grayValue = quint8(m_gray >> Shift);
288 }
289
290 return Rgba(grayValue, grayValue, grayValue);
291 }
292
293 protected:
294 IntType m_gray;
295 };
296
297 template <typename IntType, int Shift, bool DoCropMin = true, bool DoCropMax = true>
299 {
300 public:
302 {
303 }
304
306 : m_r(value.m_r), m_g(value.m_g), m_b(value.m_b)
307 {
308 }
309
310 RgbCropAccum32(const Rgba& value)
311 : m_r(IntType(value.m_r) << Shift), m_g(IntType(value.m_g) << Shift), m_b(IntType(value.m_b) << Shift)
312 {
313 }
314
315 RgbCropAccum32(const Rgb& value)
316 : m_r(IntType(value.m_r) << Shift), m_g(IntType(value.m_g) << Shift), m_b(IntType(value.m_b) << Shift)
317 {
318 }
319
320 RgbCropAccum32(int value)
321 : m_r(IntType(value) << Shift), m_g(IntType(value) << Shift), m_b(IntType(value) << Shift)
322 {
323 }
324
325 RgbCropAccum32(quint8 value)
326 : m_r(IntType(value) << Shift), m_g(IntType(value) << Shift), m_b(IntType(value) << Shift)
327 {
328 }
329
330 RgbCropAccum32(quint16 value)
331 : m_r(IntType(value) << Shift), m_g(IntType(value) << Shift), m_b(IntType(value) << Shift)
332 {
333 }
334
335 RgbCropAccum32(quint32 value)
336 : m_r(IntType(value) << Shift), m_g(IntType(value) << Shift), m_b(IntType(value) << Shift)
337 {
338 }
339
340 RgbCropAccum32(quint64 value)
341 : m_r(IntType(value) << Shift), m_g(IntType(value) << Shift), m_b(IntType(value) << Shift)
342 {
343 }
344
345 RgbCropAccum32(double value)
346 : m_r(IntType(value * (1 << Shift))), m_g(IntType(value * (1 << Shift))), m_b(IntType(value * (1 << Shift)))
347 {
348 }
349
350 bool operator==(const RgbCropAccum32& value)
351 {
352 return (m_r == value.m_r) && (m_g == value.m_g) && (m_b == value.m_b);
353 }
354
355 bool operator==(int value)
356 {
357 IntType gray = (IntType(value) << Shift);
358
359 return (m_r == gray) && (m_g == gray) && (m_b == gray);
360 }
361
362 bool operator==(double value)
363 {
364 IntType gray = IntType(value * (1 << Shift));
365
366 return (m_r == gray) && (m_g == gray) && (m_b == gray);
367 }
368
369 bool operator!=(const RgbCropAccum32& value)
370 {
371 return (m_r != value.m_r) || (m_g != value.m_g) || (m_b != value.m_b);
372 }
373
374 bool operator!=(int value)
375 {
376 IntType gray = (IntType(value) << Shift);
377
378 return (m_r != gray) || (m_g != gray) || (m_b != gray);
379 }
380
381 bool operator!=(double value)
382 {
383 IntType gray = IntType(value * (1 << Shift));
384
385 return (m_r != gray) || (m_g != gray) || (m_b != gray);
386 }
387
389 {
390 m_r = value.m_r;
391 m_g = value.m_g;
392 m_b = value.m_b;
393
394 return *this;
395 }
396
398 {
399 m_r += value.m_r;
400 m_g += value.m_g;
401 m_b += value.m_b;
402
403 return *this;
404 }
405
407 {
408 m_r += IntType(value.m_r) << Shift;
409 m_g += IntType(value.m_g) << Shift;
410 m_b += IntType(value.m_b) << Shift;
411
412 return *this;
413 }
414
415 RgbCropAccum32& operator+=(unsigned int value)
416 {
417 IntType gray = IntType(value) << Shift;
418 m_r += gray;
419 m_g += gray;
420 m_b += gray;
421
422 return *this;
423 }
424
426 {
427 IntType gray = IntType(value * (1 << Shift));
428 m_r += gray;
429 m_g += gray;
430 m_b += gray;
431
432 return *this;
433 }
434
436 {
437 m_r -= value.m_r;
438 m_g -= value.m_g;
439 m_b -= value.m_b;
440
441 return *this;
442 }
443
445 {
446 m_r -= IntType(value.m_r) << Shift;
447 m_g -= IntType(value.m_g) << Shift;
448 m_b -= IntType(value.m_b) << Shift;
449
450 return *this;
451 }
452
454 {
455 IntType gray = IntType(value) << Shift;
456 m_r -= gray;
457 m_g -= gray;
458 m_b -= gray;
459
460 return *this;
461 }
462
464 {
465 IntType gray = IntType(value * (1 << Shift));
466 m_r -= gray;
467 m_g -= gray;
468 m_b -= gray;
469
470 return *this;
471 }
472
474 {
475 m_r = IntType(quint64(m_r) * quint64(value.m_r) >> Shift);;
476 m_g = IntType(quint64(m_g) * quint64(value.m_g) >> Shift);;
477 m_b = IntType(quint64(m_b) * quint64(value.m_b) >> Shift);;
478
479 return *this;
480 }
481
483 {
484 m_r *= IntType(value);
485 m_g *= IntType(value);
486 m_b *= IntType(value);
487
488 return *this;
489 }
490
492 {
493 m_r = IntType(m_r * value);
494 m_g = IntType(m_g * value);
495 m_b = IntType(m_b * value);
496
497 return *this;
498 }
499
501 {
502 m_r = m_r / value;
503 m_g = m_g / value;
504 m_b = m_b / value;
505
506 return *this;
507 }
508
510 {
511 m_r = IntType(m_r / value);
512 m_g = IntType(m_g / value);
513 m_b = IntType(m_b / value);
514
515 return *this;
516 }
517
518 operator quint8()
519 {
520 quint64 grayValue = (quint64(m_r) + quint64(m_g) + quint64(m_b)) / 3;
521
522 if (DoCropMin && (grayValue < 0)){
523 return 0;
524 }
525 if (DoCropMin && (grayValue > (0xff << Shift))){
526 return 0xff;
527 }
528
529 return quint8(grayValue >> Shift);
530 }
531
532 operator quint16()
533 {
534 quint64 grayValue = (quint64(m_r) + quint64(m_g) + quint64(m_b)) / 3;
535
536 if (DoCropMin && (grayValue < 0)){
537 return 0;
538 }
539 if (DoCropMin && (grayValue > (0xffff << Shift))){
540 return 0xffff;
541 }
542
543 return quint16(grayValue >> Shift);
544 }
545
546 operator quint32()
547 {
548 quint64 grayValue = (quint64(m_r) + quint64(m_g) + quint64(m_b)) / 3;
549
550 if (DoCropMin && (grayValue < 0)){
551 return 0;
552 }
553
554 return quint32(grayValue >> Shift);
555 }
556
557 operator float()
558 {
559 return (float(m_r) + float(m_g) + float(m_b)) / (3 << Shift);
560 }
561
562 operator double()
563 {
564 return (double(m_r) + double(m_g) + double(m_b)) / (3 << Shift);
565 }
566
567 operator Rgba()
568 {
569 IntType red = m_r;
570 if (DoCropMin && (red < 0)){
571 red = 0;
572 }
573 else if (DoCropMin && (red > (0xff << Shift))){
574 red = 0xff;
575 }
576
577 IntType green = m_g;
578 if (DoCropMin && (green < 0)){
579 green = 0;
580 }
581 else if (DoCropMin && (green > (0xff << Shift))){
582 green = 0xff;
583 }
584
585 IntType blue = m_b;
586 if (DoCropMin && (blue < 0)){
587 blue = 0;
588 }
589 else if (DoCropMin && (blue > (0xff << Shift))){
590 blue = 0xff;
591 }
592
593 return Rgba(quint8(red >> Shift), quint8(green >> Shift), quint8(blue >> Shift));
594 }
595
596 operator Rgb()
597 {
598 IntType red = m_r;
599 if (DoCropMin && (red < 0)){
600 red = 0;
601 }
602 else if (DoCropMin && (red > (0xff << Shift))){
603 red = 0xff;
604 }
605
606 IntType green = m_g;
607 if (DoCropMin && (green < 0)){
608 green = 0;
609 }
610 else if (DoCropMin && (green > (0xff << Shift))){
611 green = 0xff;
612 }
613
614 IntType blue = m_b;
615 if (DoCropMin && (blue < 0)){
616 blue = 0;
617 }
618 else if (DoCropMin && (blue > (0xff << Shift))){
619 blue = 0xff;
620 }
621
622 return Rgb(quint8(red >> Shift), quint8(green >> Shift), quint8(blue >> Shift));
623 }
624
625 protected:
626 IntType m_r;
627 IntType m_g;
628 IntType m_b;
629 };
630
631 template <typename IntType, int Shift, bool DoCropMin = true, bool DoCropMax = true>
632 class RgbaCropAccum32: public RgbCropAccum32<IntType, Shift, DoCropMin, DoCropMax>
633 {
634 public:
636
638 {
639 }
640
642 : BaseClass(value),
643 m_a(value.m_a)
644 {
645 }
646
647 RgbaCropAccum32(const Rgba& value)
648 : BaseClass(value),
649 m_a(IntType(255) << Shift)
650 {
651 }
652
654 : BaseClass(value),
655 m_a(IntType(255) << Shift)
656 {
657 }
658
659 RgbaCropAccum32(quint8 value)
660 : BaseClass(value),
661 m_a(IntType(255) << Shift)
662 {
663 }
664
665 RgbaCropAccum32(quint16 value)
666 : BaseClass(value),
667 m_a(IntType(255) << Shift)
668 {
669 }
670
671 RgbaCropAccum32(quint32 value)
672 : BaseClass(value),
673 m_a(IntType(255) << Shift)
674 {
675 }
676
677 RgbaCropAccum32(quint64 value)
678 : BaseClass(value),
679 m_a(IntType(255) << Shift)
680 {
681 }
682
683 RgbaCropAccum32(double value)
684 : BaseClass(value),
685 m_a(IntType(255) << Shift)
686 {
687 }
688
689 bool operator==(const RgbaCropAccum32& value)
690 {
691 return (BaseClass::m_r == value.m_r) && (BaseClass::m_g == value.m_g) && (BaseClass::m_b == value.m_b) && (m_a == value.m_a);
692 }
693
694 bool operator==(int value)
695 {
696 IntType gray = (IntType(value) << Shift);
697
698 return (BaseClass::m_r == gray) && (BaseClass::m_g == gray) && (BaseClass::m_b == gray) && (m_a == (IntType(255) << Shift));
699 }
700
701 bool operator==(double value)
702 {
703 IntType gray = IntType(value * (1 << Shift));
704
705 return (BaseClass::m_r == gray) && (BaseClass::m_g == gray) && (BaseClass::m_b == gray) && (m_a == (IntType(255) << Shift));
706 }
707
708 bool operator!=(const RgbaCropAccum32& value)
709 {
710 return (BaseClass::m_r != value.m_r) || (BaseClass::m_g != value.m_g) || (BaseClass::m_b != value.m_b) || (m_a != value.m_a);
711 }
712
713 bool operator!=(int value)
714 {
715 IntType gray = (IntType(value) << Shift);
716
717 return (BaseClass::m_r != gray) || (BaseClass::m_g != gray) || (BaseClass::m_b != gray) || (m_a != (IntType(255) << Shift));
718 }
719
720 bool operator!=(double value)
721 {
722 IntType gray = IntType(value * (1 << Shift));
723
724 return (BaseClass::m_r != gray) || (BaseClass::m_g != gray) || (BaseClass::m_b != gray) || (m_a != (IntType(255) << Shift));
725 }
726
728 {
729 BaseClass::m_r = value.m_r;
730 BaseClass::m_g = value.m_g;
731 BaseClass::m_b = value.m_b;
732
733 m_a = value.m_a;
734
735 return *this;
736 }
737
739 {
740 IntType a = (m_a + value.m_a) / 2;
741 if (a != 0){
742 BaseClass::m_r = (BaseClass::m_r * m_a + value.m_r * value.m_a) / a;
743 BaseClass::m_g = (BaseClass::m_g * m_a + value.m_g * value.m_a) / a;
744 BaseClass::m_b = (BaseClass::m_b * m_a + value.m_b * value.m_a) / a;
745 }
746 m_a = a;
747
748 return *this;
749 }
750
752 {
753 IntType a = (m_a + (IntType(value.m_a) << Shift)) / 2;
754 if (a != 0){
755 BaseClass::m_r = (BaseClass::m_r * (m_a >> Shift) + (IntType(value.m_r) * IntType(value.m_a) << Shift)) / a;
756 BaseClass::m_g = (BaseClass::m_g * (m_a >> Shift) + (IntType(value.m_g) * IntType(value.m_a) << Shift)) / a;
757 BaseClass::m_b = (BaseClass::m_b * (m_a >> Shift) + (IntType(value.m_b) * IntType(value.m_a) << Shift)) / a;
758 }
759 m_a = a;
760
761 return *this;
762 }
763
764 RgbaCropAccum32& operator+=(unsigned int value)
765 {
766 IntType gray_corr = (IntType(value) * IntType(255) << Shift);
767 IntType a = (m_a + (IntType(255) << Shift)) / 2;
768 if (a != 0){
769 BaseClass::m_r = (BaseClass::m_r * (m_a >> Shift) + gray_corr) / a;
770 BaseClass::m_g = (BaseClass::m_g * (m_a >> Shift) + gray_corr) / a;
771 BaseClass::m_b = (BaseClass::m_b * (m_a >> Shift) + gray_corr) / a;
772 }
773 m_a = a;
774
775 return *this;
776 }
777
779 {
780 IntType gray_corr = IntType(value * (255 << Shift));
781 IntType a = (m_a + (IntType(255) << Shift)) / 2;
782 if (a != 0){
783 BaseClass::m_r = (BaseClass::m_r * (m_a >> Shift) + gray_corr) / a;
784 BaseClass::m_g = (BaseClass::m_g * (m_a >> Shift) + gray_corr) / a;
785 BaseClass::m_b = (BaseClass::m_b * (m_a >> Shift) + gray_corr) / a;
786 }
787 m_a = a;
788
789 return *this;
790 }
791
793 {
795
796 return *this;
797 }
798
800 {
802
803 return *this;
804 }
805
807 {
809
810 return *this;
811 }
812
814 {
816
817 return *this;
818 }
819
821 {
823
824 return *this;
825 }
826
827 operator Rgba()
828 {
829 IntType red = BaseClass::m_r;
830 if (DoCropMin && (red < 0)){
831 red = 0;
832 }
833 else if (DoCropMin && (red > (0xff << Shift))){
834 red = 0xff;
835 }
836
837 IntType green = BaseClass::m_g;
838 if (DoCropMin && (green < 0)){
839 green = 0;
840 }
841 else if (DoCropMin && (green > (0xff << Shift))){
842 green = 0xff;
843 }
844
845 IntType blue = BaseClass::m_b;
846 if (DoCropMin && (blue < 0)){
847 blue = 0;
848 }
849 else if (DoCropMin && (blue > (0xff << Shift))){
850 blue = 0xff;
851 }
852
853 return Rgba(quint8(red >> Shift), quint8(green >> Shift), quint8(blue >> Shift), quint8(m_a >> Shift));
854 }
855
856 protected:
857 IntType m_a;
858 };
859};
860
861
862#pragma pack(pop)
863
864
865} // namespace iipr
866
867
868#endif // !iipr_CPixelManip_included
869
870
GrayCropAccum32 & operator/=(int value)
bool operator!=(const GrayCropAccum32 &value)
GrayCropAccum32 & operator*=(unsigned int value)
GrayCropAccum32 & operator-=(unsigned int value)
GrayCropAccum32 & operator*=(double value)
GrayCropAccum32 & operator*=(const GrayCropAccum32 &value)
GrayCropAccum32 & operator+=(const GrayCropAccum32 &value)
GrayCropAccum32 & operator=(const GrayCropAccum32 &value)
GrayCropAccum32 & operator/=(double value)
GrayCropAccum32 & operator+=(double value)
GrayCropAccum32 & operator-=(double value)
bool operator==(const GrayCropAccum32 &value)
GrayCropAccum32 & operator+=(unsigned int value)
GrayCropAccum32 & operator-=(const GrayCropAccum32 &value)
GrayCropAccum32(const GrayCropAccum32 &value)
Definition CPixelManip.h:72
RgbCropAccum32(const RgbCropAccum32 &value)
RgbCropAccum32 & operator+=(const RgbCropAccum32 &value)
RgbCropAccum32 & operator-=(const RgbCropAccum32 &value)
RgbCropAccum32 & operator-=(double value)
RgbCropAccum32 & operator*=(int value)
RgbCropAccum32(const Rgba &value)
bool operator!=(const RgbCropAccum32 &value)
RgbCropAccum32 & operator-=(const Rgba &value)
RgbCropAccum32 & operator/=(int value)
RgbCropAccum32 & operator*=(const RgbCropAccum32 &value)
bool operator==(const RgbCropAccum32 &value)
RgbCropAccum32 & operator/=(double value)
RgbCropAccum32 & operator=(const RgbCropAccum32 &value)
RgbCropAccum32 & operator-=(quint32 value)
RgbCropAccum32 & operator+=(const Rgba &value)
RgbCropAccum32 & operator+=(unsigned int value)
RgbCropAccum32 & operator+=(double value)
RgbCropAccum32 & operator*=(double value)
Rgb(quint8 r, quint8 g, quint8 b)
Definition CPixelManip.h:54
RgbaCropAccum32 & operator+=(double value)
RgbaCropAccum32 & operator+=(const RgbaCropAccum32 &value)
RgbaCropAccum32 & operator*=(int value)
RgbaCropAccum32 & operator=(const RgbaCropAccum32 &value)
RgbaCropAccum32 & operator/=(double value)
RgbaCropAccum32 & operator*=(double value)
bool operator!=(const RgbaCropAccum32 &value)
RgbaCropAccum32 & operator+=(unsigned int value)
RgbaCropAccum32 & operator*=(const RgbaCropAccum32 &value)
RgbaCropAccum32(const RgbaCropAccum32 &value)
RgbCropAccum32< IntType, Shift, DoCropMin, DoCropMax > BaseClass
bool operator==(const RgbaCropAccum32 &value)
RgbaCropAccum32 & operator+=(const Rgba &value)
RgbaCropAccum32 & operator/=(int value)
Rgba(quint8 r, quint8 g, quint8 b, quint8 a=255)
Definition CPixelManip.h:35
Set pixel performance optimized classes designed to be used for pixel manipulation in template proces...
Definition CPixelManip.h:25
Contains the image processing classes.

© Witold Gantzke and Kirill Lepskiy