pixel.h 52.1 KB
Newer Older
1
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
2
3
4
5
6
7
8
9
10
11
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_PIXEl_ 
#define DLIB_PIXEl_

#include <iostream>
#include "serialize.h"
#include <cmath>
#include "algs.h"
#include "uintn.h"
#include <limits>
12
#include <complex>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "enable_if.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    /*!
        This file contains definitions of pixel objects and related classes and
        functionality.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    struct pixel_traits;
    /*!
        WHAT THIS OBJECT REPRESENTS
            As the name implies, this is a traits class for pixel types.
32
            It defines the properties of a pixel.
33
34
35
36
37
38

        This traits class will define the following public static members:
            - bool grayscale
            - bool rgb
            - bool rgb_alpha
            - bool hsi
39
            - bool lab
40
41
42
43

            - bool has_alpha

            - long num 
44
45
46
47
48

            - basic_pixel_type
            - basic_pixel_type min()
            - basic_pixel_type max()
            - is_unsigned
49
50

        The above public constants are subject to the following constraints:
51
            - only one of grayscale, rgb, rgb_alpha, hsi or lab is true
52
53
54
55
56
57
            - if (rgb == true) then
                - The type T will be a struct with 3 public members of type 
                  unsigned char named "red" "green" and "blue".  
                - This type of pixel represents the RGB color space.
                - num == 3
                - has_alpha == false
58
59
                - basic_pixel_type == unsigned char
                - min() == 0 
60
                - max() == 255
61
                - is_unsigned == true
62
63
64
65
66
67
68
69
70
            - if (rgb_alpha == true) then
                - The type T will be a struct with 4 public members of type 
                  unsigned char named "red" "green" "blue" and "alpha".  
                - This type of pixel represents the RGB color space with
                  an alpha channel where an alpha of 0 represents a pixel
                  that is totally transparent and 255 represents a pixel 
                  with maximum opacity.
                - num == 4
                - has_alpha == true 
71
72
                - basic_pixel_type == unsigned char
                - min() == 0 
73
                - max() == 255
74
                - is_unsigned == true
75
76
77
78
79
80
            - else if (hsi == true) then
                - The type T will be a struct with 3 public members of type
                  unsigned char named "h" "s" and "i".  
                - This type of pixel represents the HSI color space.
                - num == 3
                - has_alpha == false 
81
82
                - basic_pixel_type == unsigned char
                - min() == 0 
83
                - max() == 255
84
                - is_unsigned == true
85
86
             - else if (lab == true) then
                - The type T will be a struct with 3 public members of type
87
                  unsigned char named "l" "a" and "b".
88
89
90
                - This type of pixel represents the Lab color space.
                - num == 3
                - has_alpha == false
sutr90's avatar
sutr90 committed
91
92
93
                - basic_pixel_type == unsigned char
                - min() == 0
                - max() == 255
94
                - is_unsigned == true 
95
96
            - else
                - grayscale == true
97
98
                - This type of pixel represents a grayscale color space.  T 
                  will be some kind of basic scalar type such as unsigned int.
99
100
                - num == 1
                - has_alpha == false 
101
102
103
104
                - basic_pixel_type == T 
                - min() == the minimum obtainable value of objects of type T 
                - max() == the maximum obtainable value of objects of type T 
                - is_unsigned is true if min() == 0 and false otherwise
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    !*/

// ----------------------------------------------------------------------------------------

    struct rgb_pixel
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is a simple struct that represents an RGB colored graphical pixel.
        !*/

        rgb_pixel (
        ) {}

        rgb_pixel (
            unsigned char red_,
            unsigned char green_,
            unsigned char blue_
        ) : red(red_), green(green_), blue(blue_) {}

        unsigned char red;
        unsigned char green;
        unsigned char blue;
    };

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// ----------------------------------------------------------------------------------------

    struct bgr_pixel
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is a simple struct that represents an BGR colored graphical pixel.
                (the reason it exists in addition to the rgb_pixel is so you can lay
                it down on top of a memory region that organizes its color data in the
                BGR format and still be able to read it)
        !*/

        bgr_pixel (
        ) {}

        bgr_pixel (
            unsigned char blue_,
            unsigned char green_,
            unsigned char red_
        ) : blue(blue_), green(green_), red(red_) {}

        unsigned char blue;
        unsigned char green;
        unsigned char red;
    };

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// ----------------------------------------------------------------------------------------

    struct rgb_alpha_pixel
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is a simple struct that represents an RGB colored graphical pixel
                with an alpha channel.
        !*/

        rgb_alpha_pixel (
        ) {}

        rgb_alpha_pixel (
            unsigned char red_,
            unsigned char green_,
            unsigned char blue_,
            unsigned char alpha_
        ) : red(red_), green(green_), blue(blue_), alpha(alpha_) {}

        unsigned char red;
        unsigned char green;
        unsigned char blue;
        unsigned char alpha;
    };

// ----------------------------------------------------------------------------------------

    struct hsi_pixel
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is a simple struct that represents an HSI colored graphical pixel.
        !*/

        hsi_pixel (
        ) {}

        hsi_pixel (
            unsigned char h_,
            unsigned char s_,
            unsigned char i_
        ) : h(h_), s(s_), i(i_) {}

        unsigned char h;
        unsigned char s;
        unsigned char i;
    };
204
205
206
207
208
209
210
211
212
213
214
215
216
    // ----------------------------------------------------------------------------------------

    struct lab_pixel
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is a simple struct that represents an Lab colored graphical pixel.
        !*/

        lab_pixel (
        ) {}

        lab_pixel (
sutr90's avatar
sutr90 committed
217
218
219
                unsigned char l_,
                unsigned char a_,
                unsigned char b_
220
221
        ) : l(l_), a(a_), b(b_) {}

sutr90's avatar
sutr90 committed
222
223
224
        unsigned char l;
        unsigned char a;
        unsigned char b;
225
    };
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

// ----------------------------------------------------------------------------------------

    template <
        typename P1,
        typename P2  
        >
    inline void assign_pixel (
        P1& dest,
        const P2& src
    );
    /*!
        requires
            - pixel_traits<P1> must be defined
            - pixel_traits<P2> must be defined
        ensures
            - if (P1 and P2 are the same type of pixel) then
243
                - simply copies the value of src into dest.  In other words,
244
245
246
247
248
                  dest will be identical to src after this function returns.
            - else if (P1 and P2 are not the same type of pixel) then
                - assigns pixel src to pixel dest and does any necessary color space
                  conversions.   
                - When converting from a grayscale color space with more than 255 values the
249
250
                  pixel intensity is saturated at pixel_traits<P1>::max() or pixel_traits<P1>::min()
                  as appropriate.
251
252
253
254
255
256
257
258
259
260
261
262
263
                - if (the dest pixel has an alpha channel and the src pixel doesn't) then
                    - #dest.alpha == 255 
                - else if (the src pixel has an alpha channel but the dest pixel doesn't) then
                    - #dest == the original dest value blended with the src value according
                      to the alpha channel in src.  
                      (i.e.  #dest == src*(alpha/255) + dest*(1-alpha/255))
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename P
        >
264
    inline typename pixel_traits<P>::basic_pixel_type get_pixel_intensity (
265
266
267
268
269
270
271
272
273
        const P& src
    );
    /*!
        requires
            - pixel_traits<P> must be defined
        ensures
            - if (pixel_traits<P>::grayscale == true) then
                - returns src
            - else
274
                - converts src to grayscale and returns the resulting value.
275
276
277
278
279
    !*/

// ----------------------------------------------------------------------------------------

    template <
280
281
        typename P,
        typename T
282
283
284
        >
    inline void assign_pixel_intensity (
        P& dest,
285
        const T& new_intensity
286
287
288
289
    );
    /*!
        requires
            - pixel_traits<P> must be defined
290
            - pixel_traits<T> must be defined
291
        ensures
292
293
294
295
296
297
298
299
            - This function changes the intensity of the dest pixel. So if the pixel in 
              question is a grayscale pixel then it simply assigns that pixel with the 
              value of get_pixel_intensity(new_intensity).  However, if the pixel is not 
              a grayscale pixel then it converts the pixel to the HSI color space and sets 
              the I channel to the given intensity and then converts this HSI value back to 
              the original pixel's color space.
            - Note that we don't necessarily have #get_pixel_intensity(dest) == get_pixel_intensity(new_intensity) 
              due to vagaries of how converting to and from HSI works out.
300
301
302
303
304
305
            - if (the dest pixel has an alpha channel) then
                - #dest.alpha == dest.alpha
    !*/

// ----------------------------------------------------------------------------------------

306
    inline void serialize (
307
308
309
310
311
312
313
314
315
        const rgb_pixel& item, 
        std::ostream& out 
    );   
    /*!
        provides serialization support for the rgb_pixel struct
    !*/

// ----------------------------------------------------------------------------------------

316
    inline void deserialize (
317
318
319
320
321
322
323
        rgb_pixel& item, 
        std::istream& in
    );   
    /*!
        provides deserialization support for the rgb_pixel struct
    !*/

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// ----------------------------------------------------------------------------------------

    inline void serialize (
        const bgr_pixel& item, 
        std::ostream& out 
    );   
    /*!
        provides serialization support for the bgr_pixel struct
    !*/

// ----------------------------------------------------------------------------------------

    inline void deserialize (
        bgr_pixel& item, 
        std::istream& in
    );   
    /*!
        provides deserialization support for the bgr_pixel struct
    !*/

344
345
// ----------------------------------------------------------------------------------------

346
    inline void serialize (
347
348
349
350
351
352
353
354
355
        const rgb_alpha_pixel& item, 
        std::ostream& out 
    );   
    /*!
        provides serialization support for the rgb_alpha_pixel struct
    !*/

// ----------------------------------------------------------------------------------------

356
    inline void deserialize (
357
358
359
360
361
362
363
364
365
        rgb_alpha_pixel& item, 
        std::istream& in
    );   
    /*!
        provides deserialization support for the rgb_alpha_pixel struct
    !*/

// ----------------------------------------------------------------------------------------

366
    inline void serialize (
367
368
369
370
371
372
373
        const hsi_pixel& item, 
        std::ostream& out 
    );   
    /*!
        provides serialization support for the hsi_pixel struct
    !*/

374
375
376
377
378
379
380
381
382
383
384
// ----------------------------------------------------------------------------------------

    inline void serialize (
            const lab_pixel& item,
            std::ostream& out
    );
    /*!
        provides serialization support for the lab_pixel struct
    !*/


385
386
// ----------------------------------------------------------------------------------------

387
    inline void deserialize (
388
389
390
391
392
393
        hsi_pixel& item, 
        std::istream& in
    );   
    /*!
        provides deserialization support for the hsi_pixel struct
    !*/
394
395
396
397
398
399
400
401
402
// ----------------------------------------------------------------------------------------

    inline void deserialize (
            lab_pixel& item,
            std::istream& in
    );
    /*!
        provides deserialization support for the lab_pixel struct
    !*/
403
404
405
406
407
408
409
410
411
412

// ----------------------------------------------------------------------------------------

    template <>
    struct pixel_traits<rgb_pixel>
    {
        const static bool rgb  = true;
        const static bool rgb_alpha  = false;
        const static bool grayscale = false;
        const static bool hsi = false;
413
        const static bool lab = false;
414
        enum {num = 3};
415
416
417
418
        typedef unsigned char basic_pixel_type;
        static basic_pixel_type min() { return 0;}
        static basic_pixel_type max() { return 255;}
        const static bool is_unsigned = true;
419
420
421
        const static bool has_alpha = false;
    };

422
// ----------------------------------------------------------------------------------------
Davis King's avatar
Davis King committed
423

424
425
426
427
428
429
430
    template <>
    struct pixel_traits<bgr_pixel>
    {
        const static bool rgb  = true;
        const static bool rgb_alpha  = false;
        const static bool grayscale = false;
        const static bool hsi = false;
431
        const static bool lab = false;
432
        enum {num = 3};
433
434
435
436
        typedef unsigned char basic_pixel_type;
        static basic_pixel_type min() { return 0;}
        static basic_pixel_type max() { return 255;}
        const static bool is_unsigned = true;
437
438
439
        const static bool has_alpha = false;
    };

440
// ----------------------------------------------------------------------------------------
Davis King's avatar
Davis King committed
441

442
443
444
445
446
447
448
    template <>
    struct pixel_traits<rgb_alpha_pixel>
    {
        const static bool rgb  = false;
        const static bool rgb_alpha  = true;
        const static bool grayscale = false;
        const static bool hsi = false;
449
        const static bool lab = false;
450
        enum {num = 4};
451
452
453
454
        typedef unsigned char basic_pixel_type;
        static basic_pixel_type min() { return 0;}
        static basic_pixel_type max() { return 255;}
        const static bool is_unsigned = true;
455
456
457
458
459
460
461
462
463
464
465
466
467
        const static bool has_alpha = true;
    };

// ----------------------------------------------------------------------------------------


    template <>
    struct pixel_traits<hsi_pixel>
    {
        const static bool rgb  = false;
        const static bool rgb_alpha  = false;
        const static bool grayscale = false;
        const static bool hsi = true;
468
        const static bool lab = false;
469
        enum {num = 3};
470
471
472
473
        typedef unsigned char basic_pixel_type;
        static basic_pixel_type min() { return 0;}
        static basic_pixel_type max() { return 255;}
        const static bool is_unsigned = true;
474
475
476
        const static bool has_alpha = false;
    };

477
478
479
480
481
482
483
484
485
486
487
488
// ----------------------------------------------------------------------------------------


    template <>
    struct pixel_traits<lab_pixel>
    {
        const static bool rgb  = false;
        const static bool rgb_alpha  = false;
        const static bool grayscale = false;
        const static bool hsi = false;
        const static bool lab = true;
        const static long num = 3;
sutr90's avatar
sutr90 committed
489
490
491
492
        typedef unsigned char basic_pixel_type;
        static basic_pixel_type min() { return 0;}
        static basic_pixel_type max() { return 255;}
        const static bool is_unsigned = true;
493
494
495
        const static bool has_alpha = false;
    };

496
497
498
499
500
501
502
503
504
// ----------------------------------------------------------------------------------------

    template <typename T>
    struct grayscale_pixel_traits
    {
        const static bool rgb  = false;
        const static bool rgb_alpha  = false;
        const static bool grayscale = true;
        const static bool hsi = false;
505
        const static bool lab = false;
506
        enum {num = 1};
507
        const static bool has_alpha = false;
508
509
510
511
        typedef T basic_pixel_type;
        static basic_pixel_type min() { return std::numeric_limits<T>::min();}
        static basic_pixel_type max() { return std::numeric_limits<T>::max();}
        const static bool is_unsigned = is_unsigned_type<T>::value;
512
513
514
515
    };

    template <> struct pixel_traits<unsigned char>  : public grayscale_pixel_traits<unsigned char> {};
    template <> struct pixel_traits<unsigned short> : public grayscale_pixel_traits<unsigned short> {};
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    template <> struct pixel_traits<unsigned int>   : public grayscale_pixel_traits<unsigned int> {};
    template <> struct pixel_traits<unsigned long>  : public grayscale_pixel_traits<unsigned long> {};

    template <> struct pixel_traits<char>           : public grayscale_pixel_traits<char> {};
    template <> struct pixel_traits<signed char>    : public grayscale_pixel_traits<signed char> {};
    template <> struct pixel_traits<short>          : public grayscale_pixel_traits<short> {};
    template <> struct pixel_traits<int>            : public grayscale_pixel_traits<int> {};
    template <> struct pixel_traits<long>           : public grayscale_pixel_traits<long> {};

    template <> struct pixel_traits<int64>          : public grayscale_pixel_traits<int64> {};
    template <> struct pixel_traits<uint64>         : public grayscale_pixel_traits<uint64> {};

// ----------------------------------------------------------------------------------------

    template <typename T>
    struct float_grayscale_pixel_traits
    {
        const static bool rgb  = false;
        const static bool rgb_alpha  = false;
        const static bool grayscale = true;
        const static bool hsi = false;
537
        const static bool lab = false;
538
        enum {num = 1};
539
540
541
542
543
544
545
546
547
548
        const static bool has_alpha = false;
        typedef T basic_pixel_type;
        static basic_pixel_type min() { return -std::numeric_limits<T>::max();}
        static basic_pixel_type max() { return std::numeric_limits<T>::max();}
        const static bool is_unsigned = false;
    };

    template <> struct pixel_traits<float>          : public float_grayscale_pixel_traits<float> {};
    template <> struct pixel_traits<double>         : public float_grayscale_pixel_traits<double> {};
    template <> struct pixel_traits<long double>    : public float_grayscale_pixel_traits<long double> {};
549

550
551
552
553
554
555
    // These are here mainly so you can easily copy images into complex arrays.  This is
    // useful when you want to do a FFT on an image or some similar operation.
    template <> struct pixel_traits<std::complex<float> > :       public float_grayscale_pixel_traits<float> {};
    template <> struct pixel_traits<std::complex<double> > :      public float_grayscale_pixel_traits<double> {};
    template <> struct pixel_traits<std::complex<long double> > : public float_grayscale_pixel_traits<long double> {};

556
557
558
559
560
561
562
563
564
565
566
// ----------------------------------------------------------------------------------------

    // The following is a bunch of conversion stuff for the assign_pixel function.

    namespace assign_pixel_helpers
    {

    // -----------------------------
        // all the same kind 

        template < typename P >
567
568
569
        typename enable_if_c<pixel_traits<P>::grayscale>::type
        assign(P& dest, const P& src) 
        { 
Davis King's avatar
Davis King committed
570
            dest = src;
571
        }
572

573
574
    // -----------------------------

575
576
577
578
579
580
581
582
583
584
        template <typename T>
        typename unsigned_type<T>::type make_unsigned (
            const T& val
        ) { return static_cast<typename unsigned_type<T>::type>(val); }

        inline float make_unsigned(const float& val) { return val; }
        inline double make_unsigned(const double& val) { return val; }
        inline long double make_unsigned(const long double& val) { return val; }


585
        template <typename T, typename P>
586
587
588
        typename enable_if_c<pixel_traits<T>::is_unsigned == pixel_traits<P>::is_unsigned, bool>::type less_or_equal_to_max (
            const P& p
        ) 
589
590
591
592
593
        /*!
            ensures
                - returns true if p is <= max value of T
        !*/
        { 
594
595
596
597
            return p <= pixel_traits<T>::max();         
        }

        template <typename T, typename P>
598
        typename enable_if_c<pixel_traits<T>::is_unsigned && !pixel_traits<P>::is_unsigned, bool>::type less_or_equal_to_max (
599
600
601
602
603
            const P& p
        ) 
        { 
            if (p <= 0)
                return true;
604
            else if (make_unsigned(p) <= pixel_traits<T>::max())
605
606
607
608
609
610
                return true;
            else
                return false;
        }

        template <typename T, typename P>
611
        typename enable_if_c<!pixel_traits<T>::is_unsigned && pixel_traits<P>::is_unsigned, bool>::type less_or_equal_to_max (
612
613
614
            const P& p
        ) 
        { 
615
            return p <= make_unsigned(pixel_traits<T>::max());
616
617
        }

618
619
    // -----------------------------

620
        template <typename T, typename P>
621
        typename enable_if_c<pixel_traits<P>::is_unsigned, bool >::type greater_or_equal_to_min (
622
623
624
625
626
627
628
629
            const P& 
        ) { return true; }
        /*!
            ensures
                - returns true if p is >= min value of T
        !*/

        template <typename T, typename P>
630
631
632
633
634
635
636
637
638
        typename enable_if_c<!pixel_traits<P>::is_unsigned && pixel_traits<T>::is_unsigned, bool >::type greater_or_equal_to_min (
            const P& p
        ) 
        { 
            return p >= 0;
        }

        template <typename T, typename P>
        typename enable_if_c<!pixel_traits<P>::is_unsigned && !pixel_traits<T>::is_unsigned, bool >::type greater_or_equal_to_min (
639
640
641
642
643
            const P& p
        ) 
        { 
            return p >= pixel_traits<T>::min();
        }
644
    // -----------------------------
645

646
        template < typename P1, typename P2 >
647
648
649
        typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::grayscale>::type
        assign(P1& dest, const P2& src) 
        { 
650
651
652
653
654
            /*
                The reason for these weird comparison functions is to avoid getting compiler
                warnings about comparing signed types to unsigned and stuff like that.  
            */

Davis King's avatar
Davis King committed
655
656
657
658
659
660
661
            if (less_or_equal_to_max<P1>(src))
                if (greater_or_equal_to_min<P1>(src))
                    dest = static_cast<P1>(src);
                else
                    dest = pixel_traits<P1>::min();
            else
                dest = pixel_traits<P1>::max();
662
        }
663

664
665
666
667
    // -----------------------------
    // -----------------------------
    // -----------------------------

668
        template < typename P1, typename P2 >
669
670
671
        typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::rgb>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
672
673
674
            dest.red = src.red; 
            dest.green = src.green; 
            dest.blue = src.blue; 
675
        }
676
677

        template < typename P1, typename P2 >
678
679
680
        typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::rgb_alpha>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
681
682
683
684
            dest.red = src.red; 
            dest.green = src.green; 
            dest.blue = src.blue; 
            dest.alpha = src.alpha; 
685
        }
686
687

        template < typename P1, typename P2 >
688
689
690
        typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::hsi>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
691
692
693
            dest.h = src.h; 
            dest.s = src.s; 
            dest.i = src.i; 
694
        }
695

696
697
698
699
700
701
702
703
704
        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::lab && pixel_traits<P2>::lab>::type
        assign(P1& dest, const P2& src)
        {
            dest.l = src.l;
            dest.a = src.a;
            dest.b = src.b;
        }

705
706
707
708
    // -----------------------------
        // dest is a grayscale

        template < typename P1, typename P2 >
709
710
711
        typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb>::type
        assign(P1& dest, const P2& src) 
        { 
712
713
714
715
            const unsigned int temp = ((static_cast<unsigned int>(src.red) +
                                        static_cast<unsigned int>(src.green) +  
                                        static_cast<unsigned int>(src.blue))/3);
            assign_pixel(dest, temp);
716
        }
717
718

        template < typename P1, typename P2 >
719
720
721
        typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb_alpha>::type
        assign(P1& dest, const P2& src) 
        { 
722

Davis King's avatar
Davis King committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
            const unsigned char avg = static_cast<unsigned char>((static_cast<unsigned int>(src.red) +
                                                                  static_cast<unsigned int>(src.green) +  
                                                                  static_cast<unsigned int>(src.blue))/3); 

            if (src.alpha == 255)
            {
                assign_pixel(dest, avg);
            }
            else
            {
                // perform this assignment using fixed point arithmetic: 
                // dest = src*(alpha/255) + dest*(1 - alpha/255);
                // dest = src*(alpha/255) + dest*1 - dest*(alpha/255);
                // dest = dest*1 + src*(alpha/255) - dest*(alpha/255);
                // dest = dest*1 + (src - dest)*(alpha/255);
                // dest += (src - dest)*(alpha/255);

                int temp = avg;
                // copy dest into dest_copy using assign_pixel to avoid potential
                // warnings about implicit float to int warnings.
                int dest_copy;
                assign_pixel(dest_copy, dest);

                temp -= dest_copy;

                temp *= src.alpha;

                temp /= 255;

                assign_pixel(dest, temp+dest_copy);
            }
754
        }
755
756

        template < typename P1, typename P2 >
757
758
759
        typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::hsi>::type
        assign(P1& dest, const P2& src) 
        { 
760
            assign_pixel(dest, src.i);
761
        }
762

763
764
765
766
767
768
769
        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::lab>::type
        assign(P1& dest, const P2& src)
        {
            assign_pixel(dest, src.l);
        }

770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787

    // -----------------------------

        struct HSL
        {
            double h;
            double s;
            double l;
        };

        struct COLOUR
        {
            double r;
            double g;
            double b;
        };

        /*
788
789
            I found this excellent bit of code for dealing with HSL spaces at 
            http://local.wasp.uwa.edu.au/~pbourke/colour/hsl/
790
        */
791
792
793
794
795
796
797
798
799
800
801
802
        /*
            Calculate HSL from RGB
            Hue is in degrees
            Lightness is between 0 and 1
            Saturation is between 0 and 1
        */
        inline HSL RGB2HSL(COLOUR c1)
        {
            double themin,themax,delta;
            HSL c2;
            using namespace std;

803
804
            themin = std::min(c1.r,std::min(c1.g,c1.b));
            themax = std::max(c1.r,std::max(c1.g,c1.b));
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
            delta = themax - themin;
            c2.l = (themin + themax) / 2;
            c2.s = 0;
            if (c2.l > 0 && c2.l < 1)
                c2.s = delta / (c2.l < 0.5 ? (2*c2.l) : (2-2*c2.l));
            c2.h = 0;
            if (delta > 0) {
                if (themax == c1.r && themax != c1.g)
                    c2.h += (c1.g - c1.b) / delta;
                if (themax == c1.g && themax != c1.b)
                    c2.h += (2 + (c1.b - c1.r) / delta);
                if (themax == c1.b && themax != c1.r)
                    c2.h += (4 + (c1.r - c1.g) / delta);
                c2.h *= 60;
            }
            return(c2);
        }
822
823

        /*
824
825
826
827
            Calculate RGB from HSL, reverse of RGB2HSL()
            Hue is in degrees
            Lightness is between 0 and 1
            Saturation is between 0 and 1
828
        */
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
        inline COLOUR HSL2RGB(HSL c1)
        {
            COLOUR c2,sat,ctmp;
            using namespace std;

            if (c1.h < 120) {
                sat.r = (120 - c1.h) / 60.0;
                sat.g = c1.h / 60.0;
                sat.b = 0;
            } else if (c1.h < 240) {
                sat.r = 0;
                sat.g = (240 - c1.h) / 60.0;
                sat.b = (c1.h - 120) / 60.0;
            } else {
                sat.r = (c1.h - 240) / 60.0;
                sat.g = 0;
                sat.b = (360 - c1.h) / 60.0;
            }
847
848
849
            sat.r = std::min(sat.r,1.0);
            sat.g = std::min(sat.g,1.0);
            sat.b = std::min(sat.b,1.0);
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

            ctmp.r = 2 * c1.s * sat.r + (1 - c1.s);
            ctmp.g = 2 * c1.s * sat.g + (1 - c1.s);
            ctmp.b = 2 * c1.s * sat.b + (1 - c1.s);

            if (c1.l < 0.5) {
                c2.r = c1.l * ctmp.r;
                c2.g = c1.l * ctmp.g;
                c2.b = c1.l * ctmp.b;
            } else {
                c2.r = (1 - c1.l) * ctmp.r + 2 * c1.l - 1;
                c2.g = (1 - c1.l) * ctmp.g + 2 * c1.l - 1;
                c2.b = (1 - c1.l) * ctmp.b + 2 * c1.l - 1;
            }

            return(c2);
        }
867

868
869
870
871
872
873
874
875
876
877
878
879
880
        // -----------------------------

        struct Lab
        {
            double l;
            double a;
            double b;
        };
        /*
            Calculate Lab from RGB
            L is between 0 and 100
            a is between -128 and 127
            b is between -128 and 127
sutr90's avatar
sutr90 committed
881
            RGB is between 0.0 and 1.0
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
        */
        inline Lab RGB2Lab(COLOUR c1)
        {
            Lab c2;
            using namespace std;

            double var_R = c1.r;
            double var_G = c1.g;
            double var_B = c1.b;

            if (var_R > 0.04045) {
                var_R = pow(((var_R + 0.055) / 1.055), 2.4);
            } else {
                var_R = var_R / 12.92;
            }

            if (var_G > 0.04045) {
                var_G = pow(((var_G + 0.055) / 1.055), 2.4);
            } else {
                var_G = var_G / 12.92;
            }

            if (var_B > 0.04045) {
                var_B = pow(((var_B + 0.055) / 1.055), 2.4);
            } else {
                var_B = var_B / 12.92;
            }

            var_R = var_R * 100;
            var_G = var_G * 100;
            var_B = var_B * 100;

//Observer. = 2°, Illuminant = D65
            double X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
            double Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
            double Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;

            double var_X = X / 95.047;
            double var_Y = Y / 100.000;
            double var_Z = Z / 108.883;

            if (var_X > 0.008856) {
                var_X = pow(var_X, (1.0 / 3));
            }
            else {
                var_X = (7.787 * var_X) + (16.0 / 116);
            }

            if (var_Y > 0.008856) {
                var_Y = pow(var_Y, (1.0 / 3));
            }
            else {
                var_Y = (7.787 * var_Y) + (16.0 / 116);
            }

            if (var_Z > 0.008856) {
                var_Z = pow(var_Z, (1.0 / 3));
            }
            else {
                var_Z = (7.787 * var_Z) + (16.0 / 116);
            }

sutr90's avatar
sutr90 committed
944
945
946
947
            //clamping
            c2.l = max(0.0, (116.0 * var_Y) - 16);
            c2.a = max(-128.0, min(127.0, 500.0 * (var_X - var_Y)));
            c2.b = max(-128.0, min(127.0, 200.0 * (var_Y - var_Z)));
948
949
950
951
952

            return c2;
        }

        /*
sutr90's avatar
sutr90 committed
953
954
955
956
957
            Calculate RGB from Lab, reverse of RGB2LAb()
            L is between 0 and 100
            a is between -128 and 127
            b is between -128 and 127
            RGB is between 0.0 and 1.0
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
        */
        inline COLOUR Lab2RGB(Lab c1) {
            COLOUR c2;
            using namespace std;

            double var_Y = (c1.l + 16) / 116.0;
            double var_X = (c1.a / 500.0) + var_Y;
            double var_Z = var_Y - (c1.b / 200);

            if (pow(var_Y, 3) > 0.008856) {
                var_Y = pow(var_Y, 3);
            } else {
                var_Y = (var_Y - 16.0 / 116) / 7.787;
            }

            if (pow(var_X, 3) > 0.008856) {
                var_X = pow(var_X, 3);
            } else {
                var_X = (var_X - 16.0 / 116) / 7.787;
            }

            if (pow(var_Z, 3) > 0.008856) {
                var_Z = pow(var_Z, 3);
            } else {
                var_Z = (var_Z - 16.0 / 116) / 7.787;
            }

            double X = var_X * 95.047;
            double Y = var_Y * 100.000;
            double Z = var_Z * 108.883;

            var_X = X / 100.0;
            var_Y = Y / 100.0;
            var_Z = Z / 100.0;

            double var_R = var_X * 3.2406 + var_Y * -1.5372 + var_Z * -0.4986;
            double var_G = var_X * -0.9689 + var_Y * 1.8758 + var_Z * 0.0415;
            double var_B = var_X * 0.0557 + var_Y * -0.2040 + var_Z * 1.0570;

            if (var_R > 0.0031308) {
                var_R = 1.055 * pow(var_R, (1 / 2.4)) - 0.055;
            } else {
                var_R = 12.92 * var_R;
            }

            if (var_G > 0.0031308) {
                var_G = 1.055 * pow(var_G, (1 / 2.4)) - 0.055;
            } else {
                var_G = 12.92 * var_G;
            }

            if (var_B > 0.0031308) {
                var_B = 1.055 * pow(var_B, (1 / 2.4)) - 0.055;
            } else {
                var_B = 12.92 * var_B;
            }

sutr90's avatar
sutr90 committed
1015
1016
1017
1018
            // clamping
            c2.r = max(0.0, min(1.0, var_R));
            c2.g = max(0.0, min(1.0, var_G));
            c2.b = max(0.0, min(1.0, var_B));
1019
1020
1021
1022

            return (c2);
        }

1023
1024
1025
1026
1027

    // -----------------------------
        // dest is a color rgb_pixel

        template < typename P1 >
1028
1029
1030
        typename enable_if_c<pixel_traits<P1>::rgb>::type
        assign(P1& dest, const unsigned char& src) 
        { 
Davis King's avatar
Davis King committed
1031
1032
1033
            dest.red = src; 
            dest.green = src; 
            dest.blue = src; 
1034
        }
1035
1036

        template < typename P1, typename P2 >
1037
1038
1039
        typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::grayscale>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1040
1041
1042
1043
1044
            unsigned char p;
            assign_pixel(p, src);
            dest.red = p; 
            dest.green = p; 
            dest.blue = p; 
1045
        }
1046
1047

        template < typename P1, typename P2 >
1048
1049
1050
        typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::rgb_alpha>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
            if (src.alpha == 255)
            {
                dest.red = src.red;
                dest.green = src.green;
                dest.blue = src.blue;
            }
            else
            {
                // perform this assignment using fixed point arithmetic: 
Davis King's avatar
Davis King committed
1060
                // dest = src*(alpha/255) + dest*(1 - alpha/255);
Davis King's avatar
Davis King committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
                // dest = src*(alpha/255) + dest*1 - dest*(alpha/255);
                // dest = dest*1 + src*(alpha/255) - dest*(alpha/255);
                // dest = dest*1 + (src - dest)*(alpha/255);
                // dest += (src - dest)*(alpha/255);

                unsigned int temp_r = src.red;
                unsigned int temp_g = src.green;
                unsigned int temp_b = src.blue;

                temp_r -= dest.red;
                temp_g -= dest.green;
                temp_b -= dest.blue;

                temp_r *= src.alpha;
                temp_g *= src.alpha;
                temp_b *= src.alpha;

                temp_r >>= 8;
                temp_g >>= 8;
                temp_b >>= 8;

                dest.red += static_cast<unsigned char>(temp_r&0xFF);
                dest.green += static_cast<unsigned char>(temp_g&0xFF);
                dest.blue += static_cast<unsigned char>(temp_b&0xFF);
            }
1086
        }
1087
1088

        template < typename P1, typename P2 >
1089
1090
1091
        typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::hsi>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1092
1093
1094
1095
1096
1097
1098
1099
            COLOUR c;
            HSL h;
            h.h = src.h;
            h.h = h.h/255.0*360;
            h.s = src.s/255.0;
            h.l = src.i/255.0;
            c = HSL2RGB(h);

1100
1101
1102
            dest.red = static_cast<unsigned char>(c.r*255.0 + 0.5);
            dest.green = static_cast<unsigned char>(c.g*255.0 + 0.5);
            dest.blue = static_cast<unsigned char>(c.b*255.0 + 0.5);
1103
        }
1104

1105
1106
1107
1108
1109
1110
        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::lab>::type
        assign(P1& dest, const P2& src)
        {
            COLOUR c;
            Lab l;
sutr90's avatar
sutr90 committed
1111
1112
1113
            l.l = (src.l/255.0)*100;
            l.a = (src.a-128.0);
            l.b = (src.b-128.0);
1114
1115
1116
1117
1118
1119
1120
1121
            c = Lab2RGB(l);

            dest.red = static_cast<unsigned char>(c.r*255.0 + 0.5);
            dest.green = static_cast<unsigned char>(c.g*255.0 + 0.5);
            dest.blue = static_cast<unsigned char>(c.b*255.0 + 0.5);
        }


1122
    // -----------------------------
Davis King's avatar
Davis King committed
1123
    // dest is a color rgb_alpha_pixel
1124
1125

        template < typename P1 >
1126
1127
1128
        typename enable_if_c<pixel_traits<P1>::rgb_alpha>::type
        assign(P1& dest, const unsigned char& src) 
        { 
Davis King's avatar
Davis King committed
1129
1130
1131
1132
            dest.red = src; 
            dest.green = src; 
            dest.blue = src; 
            dest.alpha = 255;
1133
        }
1134
1135
1136


        template < typename P1, typename P2 >
1137
1138
1139
        typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::grayscale>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1140
1141
            unsigned char p;
            assign_pixel(p, src);
1142

Davis King's avatar
Davis King committed
1143
1144
1145
1146
            dest.red = p; 
            dest.green = p; 
            dest.blue = p; 
            dest.alpha = 255;
1147
        }
1148
1149

        template < typename P1, typename P2 >
1150
1151
1152
        typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::rgb>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1153
1154
1155
1156
            dest.red = src.red;
            dest.green = src.green;
            dest.blue = src.blue;
            dest.alpha = 255;
1157
        }
1158
1159

        template < typename P1, typename P2 >
1160
1161
1162
        typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::hsi>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1163
1164
1165
1166
1167
1168
1169
1170
            COLOUR c;
            HSL h;
            h.h = src.h;
            h.h = h.h/255.0*360;
            h.s = src.s/255.0;
            h.l = src.i/255.0;
            c = HSL2RGB(h);

1171
1172
1173
            dest.red = static_cast<unsigned char>(c.r*255.0 + 0.5);
            dest.green = static_cast<unsigned char>(c.g*255.0 + 0.5);
            dest.blue = static_cast<unsigned char>(c.b*255.0 + 0.5);
Davis King's avatar
Davis King committed
1174
            dest.alpha = 255;
1175
        }
1176

1177
1178
1179
1180
1181
1182
        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::lab>::type
        assign(P1& dest, const P2& src)
        {
            COLOUR c;
            Lab l;
sutr90's avatar
sutr90 committed
1183
1184
1185
            l.l = (src.l/255.0)*100;
            l.a = (src.a-128.0);
            l.b = (src.b-128.0);
1186
1187
            c = Lab2RGB(l);

sutr90's avatar
sutr90 committed
1188
1189
1190
            dest.red = static_cast<unsigned char>(c.r * 255 + 0.5);
            dest.green = static_cast<unsigned char>(c.g * 255 + 0.5);
            dest.blue = static_cast<unsigned char>(c.b * 255 + 0.5);
1191
1192
            dest.alpha = 255;
        }
1193
1194
1195
1196
    // -----------------------------
        // dest is an hsi pixel

        template < typename P1>
1197
1198
1199
        typename enable_if_c<pixel_traits<P1>::hsi>::type
        assign(P1& dest, const unsigned char& src) 
        { 
Davis King's avatar
Davis King committed
1200
1201
1202
            dest.h = 0;
            dest.s = 0;
            dest.i = src;
1203
        }
1204
1205
1206


        template < typename P1, typename P2 >
1207
1208
1209
        typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::grayscale>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1210
1211
1212
            dest.h = 0;
            dest.s = 0;
            assign_pixel(dest.i, src);
1213
        }
1214
1215

        template < typename P1, typename P2 >
1216
1217
1218
        typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::rgb>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1219
1220
1221
1222
1223
1224
1225
            COLOUR c1;
            HSL c2;
            c1.r = src.red/255.0;
            c1.g = src.green/255.0;
            c1.b = src.blue/255.0;
            c2 = RGB2HSL(c1);

1226
1227
1228
            dest.h = static_cast<unsigned char>(c2.h/360.0*255.0 + 0.5);
            dest.s = static_cast<unsigned char>(c2.s*255.0 + 0.5);
            dest.i = static_cast<unsigned char>(c2.l*255.0 + 0.5);
1229
        }
1230
1231

        template < typename P1, typename P2 >
1232
1233
1234
        typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::rgb_alpha>::type
        assign(P1& dest, const P2& src) 
        { 
Davis King's avatar
Davis King committed
1235
1236
            rgb_pixel temp;
            // convert target hsi pixel to rgb
1237
            assign_pixel_helpers::assign(temp,dest);
1238

Davis King's avatar
Davis King committed
1239
            // now assign the rgb_alpha value to our temp rgb pixel
1240
            assign_pixel_helpers::assign(temp,src);
1241

Davis King's avatar
Davis King committed
1242
1243
            // now we can just go assign the new rgb value to the
            // hsi pixel
1244
            assign_pixel_helpers::assign(dest,temp);
1245
1246
        }

1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::lab>::type
        assign(P1& dest, const P2& src)
        {
            rgb_pixel temp;
            // convert lab value to our temp rgb pixel
            assign_pixel_helpers::assign(temp,src);
            // now we can just go assign the new rgb value to the
            // hsi pixel
            assign_pixel_helpers::assign(dest,temp);
        }

1259
1260
1261
1262
1263
1264
    // -----------------------------
        // dest is an lab pixel
        template < typename P1>
        typename enable_if_c<pixel_traits<P1>::lab>::type
        assign(P1& dest, const unsigned char& src)
        {
sutr90's avatar
sutr90 committed
1265
1266
            dest.a = 128;
            dest.b = 128;
1267
1268
1269
1270
1271
1272
1273
1274
            dest.l = src;
        }


        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::lab && pixel_traits<P2>::grayscale>::type
        assign(P1& dest, const P2& src)
        {
sutr90's avatar
sutr90 committed
1275
1276
            dest.a = 128;
            dest.b = 128;
1277
1278
1279
1280
1281
1282
1283
1284
1285
            assign_pixel(dest.l, src);
        }

        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::lab && pixel_traits<P2>::rgb>::type
        assign(P1& dest, const P2& src)
        {
            COLOUR c1;
            Lab c2;
sutr90's avatar
sutr90 committed
1286
1287
1288
            c1.r = src.red / 255.0;
            c1.g = src.green / 255.0;
            c1.b = src.blue / 255.0;
1289
1290
            c2 = RGB2Lab(c1);

sutr90's avatar
sutr90 committed
1291
1292
1293
            dest.l = static_cast<unsigned char>((c2.l / 100) * 255 + 0.5);
            dest.a = static_cast<unsigned char>(c2.a + 128 + 0.5);
            dest.b = static_cast<unsigned char>(c2.b + 128 + 0.5);
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
        }

        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::lab && pixel_traits<P2>::rgb_alpha>::type
        assign(P1& dest, const P2& src)
        {
            rgb_pixel temp;
            // convert target lab pixel to rgb
            assign_pixel_helpers::assign(temp,dest);

            // now assign the rgb_alpha value to our temp rgb pixel
            assign_pixel_helpers::assign(temp,src);

            // now we can just go assign the new rgb value to the
sutr90's avatar
sutr90 committed
1308
            // lab pixel
1309
1310
            assign_pixel_helpers::assign(dest,temp);
        }
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324

        template < typename P1, typename P2 >
        typename enable_if_c<pixel_traits<P1>::lab && pixel_traits<P2>::hsi>::type
        assign(P1& dest, const P2& src)
        {
            rgb_pixel temp;

            // convert hsi value to our temp rgb pixel
            assign_pixel_helpers::assign(temp,src);

            // now we can just go assign the new rgb value to the
            // lab pixel
            assign_pixel_helpers::assign(dest,temp);
        }
1325
1326
1327
1328
1329
1330
1331
1332
    }

    // -----------------------------

    template < typename P1, typename P2 >
    inline void assign_pixel (
        P1& dest,
        const P2& src
1333
    ) { assign_pixel_helpers::assign(dest,src); }
1334
1335
1336
1337

// ----------------------------------------------------------------------------------------

    template <
1338
1339
        typename P,
        typename T
1340
1341
1342
        >
    inline typename enable_if_c<pixel_traits<P>::grayscale>::type assign_pixel_intensity_helper (
        P& dest,
1343
        const T& new_intensity
1344
1345
1346
1347
1348
1349
    )
    {
        assign_pixel(dest, new_intensity);
    }

    template <
1350
1351
        typename P,
        typename T
1352
        >
1353
    inline typename enable_if_c<pixel_traits<P>::grayscale == false &&
1354
1355
                                pixel_traits<P>::has_alpha>::type assign_pixel_intensity_helper (
        P& dest,
1356
        const T& new_intensity
1357
1358
1359
    )
    {
        hsi_pixel p;
1360
1361
1362
1363
1364
        const unsigned long old_alpha = dest.alpha;
        dest.alpha = 255;
        rgb_pixel temp;
        assign_pixel(temp, dest); // put dest into an rgb_pixel to avoid the somewhat complicated assign_pixel(hsi,rgb_alpha).
        assign_pixel(p,temp);
1365
1366
        assign_pixel(p.i, new_intensity);
        assign_pixel(dest,p);
1367
        dest.alpha = old_alpha;
1368
1369
1370
    }

    template <
1371
1372
        typename P,
        typename T
1373
        >
1374
    inline typename enable_if_c<pixel_traits<P>::grayscale == false &&
1375
1376
                                pixel_traits<P>::has_alpha == false>::type assign_pixel_intensity_helper (
        P& dest,
1377
        const T& new_intensity
1378
1379
1380
1381
1382
1383
1384
1385
1386
    )
    {
        hsi_pixel p;
        assign_pixel(p,dest);
        assign_pixel(p.i, new_intensity);
        assign_pixel(dest,p);
    }

    template <
1387
1388
        typename P,
        typename T
1389
1390
1391
        >
    inline void assign_pixel_intensity (
        P& dest,
1392
        const T& new_intensity
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
    )
    {
        assign_pixel_intensity_helper(dest, new_intensity);
    }

// ----------------------------------------------------------------------------------------

    template <
        typename P
        >
1403
    inline typename enable_if_c<pixel_traits<P>::grayscale, P>::type get_pixel_intensity_helper (
1404
1405
1406
        const P& src 
    )
    {
1407
        return src;
1408
1409
1410
1411
1412
1413
    }

    template <
        typename P
        >
    inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
1414
1415
                                pixel_traits<P>::has_alpha, 
                                typename pixel_traits<P>::basic_pixel_type>::type get_pixel_intensity_helper (
1416
1417
1418
1419
1420
        const P& src
    )
    {
        P temp = src;
        temp.alpha = 255;
1421
        typename pixel_traits<P>::basic_pixel_type p;
1422
        assign_pixel(p,temp);
1423
        return p;
1424
1425
1426
1427
1428
1429
    }

    template <
        typename P
        >
    inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
1430
1431
                                pixel_traits<P>::has_alpha == false, 
                                typename pixel_traits<P>::basic_pixel_type>::type get_pixel_intensity_helper (
1432
1433
1434
        const P& src
    )
    {
1435
        typename pixel_traits<P>::basic_pixel_type p;
1436
        assign_pixel(p,src);
1437
        return p;
1438
1439
1440
1441
1442
    }

    template <
        typename P
        >
1443
    inline typename pixel_traits<P>::basic_pixel_type get_pixel_intensity (
1444
1445
1446
1447
1448
1449
        const P& src
    )
    {
        return get_pixel_intensity_helper(src);
    }

1450
1451
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
1452
1453
// ----------------------------------------------------------------------------------------

1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
    inline void serialize (
        const rgb_alpha_pixel& item, 
        std::ostream& out 
    )   
    {
        try
        {
            serialize(item.red,out);
            serialize(item.green,out);
            serialize(item.blue,out);
            serialize(item.alpha,out);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while serializing object of type rgb_alpha_pixel"); 
        }
    }

// ----------------------------------------------------------------------------------------

    inline void deserialize (
        rgb_alpha_pixel& item, 
        std::istream& in
    )   
    {
        try
        {
            deserialize(item.red,in);
            deserialize(item.green,in);
            deserialize(item.blue,in);
            deserialize(item.alpha,in);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while deserializing object of type rgb_alpha_pixel"); 
        }
    }

// ----------------------------------------------------------------------------------------

    inline void serialize (
        const rgb_pixel& item, 
        std::ostream& out 
    )   
    {
        try
        {
            serialize(item.red,out);
            serialize(item.green,out);
            serialize(item.blue,out);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while serializing object of type rgb_pixel"); 
        }
    }

// ----------------------------------------------------------------------------------------

    inline void deserialize (
        rgb_pixel& item, 
        std::istream& in
    )   
    {
        try
        {
            deserialize(item.red,in);
            deserialize(item.green,in);
            deserialize(item.blue,in);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while deserializing object of type rgb_pixel"); 
        }
    }

1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
// ----------------------------------------------------------------------------------------

    inline void serialize (
        const bgr_pixel& item, 
        std::ostream& out 
    )   
    {
        try
        {
            serialize(item.blue,out);
1540
1541
            serialize(item.green,out);
            serialize(item.red,out);
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while serializing object of type bgr_pixel"); 
        }
    }

// ----------------------------------------------------------------------------------------

    inline void deserialize (
        bgr_pixel& item, 
        std::istream& in
    )   
    {
        try
        {
            deserialize(item.blue,in);
1559
1560
            deserialize(item.green,in);
            deserialize(item.red,in);
1561
1562
1563
1564
1565
1566
1567
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while deserializing object of type bgr_pixel"); 
        }
    }

1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
// ----------------------------------------------------------------------------------------

    inline void serialize (
        const hsi_pixel& item, 
        std::ostream& out 
    )   
    {
        try
        {
            serialize(item.h,out);
            serialize(item.s,out);
            serialize(item.i,out);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while serializing object of type hsi_pixel"); 
        }
    }

// ----------------------------------------------------------------------------------------

    inline void deserialize (
        hsi_pixel& item, 
        std::istream& in
    )   
    {
        try
        {
            deserialize(item.h,in);
            deserialize(item.s,in);
            deserialize(item.i,in);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while deserializing object of type hsi_pixel"); 
        }
    }
1605

1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
// ----------------------------------------------------------------------------------------

    inline void serialize (
            const lab_pixel& item,
            std::ostream& out
    )
    {
        try
        {
            serialize(item.l,out);
            serialize(item.a,out);
            serialize(item.b,out);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while serializing object of type lab_pixel");
        }
    }

// ----------------------------------------------------------------------------------------

    inline void deserialize (
            lab_pixel& item,
            std::istream& in
    )
    {
        try
        {
            deserialize(item.l,in);
            deserialize(item.a,in);
            deserialize(item.b,in);
        }
        catch (serialization_error& e)
        {
            throw serialization_error(e.info + "\n   while deserializing object of type lab_pixel");
        }
    }

1644
// ----------------------------------------------------------------------------------------
1645

1646
}
1647
1648
1649

#endif // DLIB_PIXEl_