File: pixel.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (322 lines) | stat: -rw-r--r-- 11,924 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#include <boost/gil.hpp>

#include <boost/core/ignore_unused.hpp>
#include <boost/mp11.hpp>

#include <exception>
#include <iostream>
#include <iterator>
#include <type_traits>

using namespace boost::gil;
using std::swap;
using namespace boost;

void error_if(bool condition);

struct increment {
    template <typename Incrementable> void operator()(Incrementable& x) const { ++x; }
};

struct prev
{
    template <typename Subtractable>
    auto operator()(const Subtractable& x) const -> typename channel_traits<Subtractable>::value_type
    {
        using return_type = typename channel_traits<Subtractable>::value_type;
        return static_cast<return_type>(x - 1);
    }
};

struct set_to_one{ int operator()() const { return 1; } };

// Construct with two pixel types. They must be compatible and the second must be mutable
template <typename C1, typename C2>
struct do_basic_test : public C1, public C2
{
    using pixel1_t = typename C1::type;
    using pixel2_t = typename C2::type;
    using pixel1_value_t = typename C1::pixel_t::value_type;
    using pixel2_value_t = typename C2::pixel_t::value_type;
    using pixel_value_t = pixel1_value_t;

    do_basic_test(const pixel_value_t& v) : C1(v), C2(v) {}

    void test_all() {
        test_heterogeneous();

        // test homogeneous algorithms - fill, max, min
        static const int num_chan = num_channels<typename C2::pixel_t>::value;
        static_fill(C2::_pixel, gil::at_c<0>(C1::_pixel)+1);
        error_if(gil::at_c<0>(C2::_pixel) != gil::at_c<num_chan-1>(C2::_pixel));

        C2::_pixel = C1::_pixel;
        error_if(static_max(C2::_pixel) != static_max(C1::_pixel));
        error_if(static_min(C2::_pixel) != static_min(C1::_pixel));
        error_if(static_max(C2::_pixel) < static_min(C2::_pixel));

        // test operator[]
        C2::_pixel[0] = C1::_pixel[0]+1;
        error_if(C2::_pixel[0] != C1::_pixel[0]+1);
    }

    void test_heterogeneous() {
        // Both must be pixel types (not necessarily pixel values). The second must be mutable. They must be compatible
        boost::function_requires<PixelConcept<typename C1::pixel_t> >();
        boost::function_requires<MutablePixelConcept<typename C2::pixel_t> >();
        boost::function_requires<PixelsCompatibleConcept<typename C1::pixel_t,typename C2::pixel_t> >();

        C2::_pixel = C1::_pixel;            // test operator=
        error_if(C1::_pixel != C2::_pixel);   // test operator==

        // construct a pixel value from it
        pixel1_value_t v1(C1::_pixel);
        pixel2_value_t v2(C2::_pixel);
        error_if(v1 != v2);

        // construct from a pixel value
        pixel1_t c1(v1);
        pixel2_t c2(v2);
        error_if(c1 != c2);

        // Invert the first semantic channel.
        C2::_pixel = C1::_pixel;
        semantic_at_c<0>(C2::_pixel) = channel_invert(semantic_at_c<0>(C2::_pixel));
        error_if(C1::_pixel == C2::_pixel);   // now they must not be equal

        // test pixel algorithms
        C2::_pixel = C1::_pixel;
        static_for_each(C2::_pixel, increment());
        static_transform(C2::_pixel, C2::_pixel, prev());
        error_if(C1::_pixel!=C2::_pixel);

        static_generate(C2::_pixel, set_to_one());
        error_if(gil::at_c<0>(C2::_pixel) != 1);

        // Test swap if both are mutable and if their value type is the same
        // (We know the second one is mutable)
        using p1_ref = typename boost::add_reference<typename C1::type>::type;
        using is_swappable = std::integral_constant
            <
                bool,
                pixel_reference_is_mutable<p1_ref>::value &&
                std::is_same<pixel1_value_t, pixel2_value_t>::value
            >;
        test_swap(is_swappable{});
    }

    void test_swap(std::false_type) {}
    void test_swap(std::true_type) {
        // test swap
        static_fill(C1::_pixel, 0);
        static_fill(C2::_pixel, 1);
        pixel_value_t pv1(C1::_pixel);
        pixel_value_t pv2(C2::_pixel);
        error_if(C2::_pixel == C1::_pixel);
        swap(C1::_pixel, C2::_pixel);
        error_if(C1::_pixel != pv2 || C2::_pixel != pv1);
    }
};

template <typename PixelValue, int Tag=0>
class value_core
{
public:
    using type = PixelValue;
    using pixel_t = type;
    type _pixel;

    value_core() : _pixel(0) {}
    value_core(const type& val) : _pixel(val) {  // test copy constructor
        boost::function_requires<PixelValueConcept<pixel_t> >();
        type p2;            // test default constructor
        boost::ignore_unused(p2);
    }
};

template <typename PixelRef, int Tag=0>
class reference_core : public value_core<typename std::remove_reference<PixelRef>::type::value_type, Tag>
{
public:
    using type = PixelRef;
    using pixel_t = typename std::remove_reference<PixelRef>::type;
    using parent_t = value_core<typename pixel_t::value_type, Tag>;

    type _pixel;

    reference_core() : parent_t(), _pixel(parent_t::_pixel) {}
    reference_core(const typename pixel_t::value_type& val) : parent_t(val), _pixel(parent_t::_pixel) {
        boost::function_requires<PixelConcept<pixel_t> >();
    }
};

// Use a subset of pixel models that covers all color spaces, channel depths, reference/value, planar/interleaved, const/mutable
// color conversion will be invoked on pairs of them. Having an exhaustive binary check would be too big/expensive.
using representative_pixels_t = mp11::mp_list
<
    value_core<gray8_pixel_t>,
    reference_core<gray16_pixel_t&>,
    value_core<bgr8_pixel_t>,
    reference_core<rgb8_planar_ref_t>,
    value_core<argb32_pixel_t>,
    reference_core<cmyk32f_pixel_t&>,
    reference_core<abgr16c_ref_t>,           // immutable reference
    reference_core<rgb32fc_planar_ref_t>
>;

template <typename Pixel1>
struct ccv2 {
    template <typename P1, typename P2>
    void color_convert_compatible(const P1& p1, P2& p2, std::true_type) {
        using value_t = typename P1::value_type;
        p2 = p1;
        value_t converted;
        color_convert(p1, converted);
        error_if(converted != p2);
    }

    template <typename P1, typename P2>
    void color_convert_compatible(const P1& p1, P2& p2, std::false_type) {
        color_convert(p1,p2);
    }

    template <typename P1, typename P2>
    void color_convert_impl(const P1& p1, P2& p2) {
        using is_compatible = typename pixels_are_compatible<P1,P2>::type;
        color_convert_compatible(p1, p2, is_compatible());
    }

    template <typename Pixel2>
    void operator()(Pixel2) {
        // convert from Pixel1 to Pixel2 (or, if Pixel2 is immutable, to its value type)
        using p2_is_mutable = pixel_reference_is_mutable<typename Pixel2::type>;
        using pixel_model_t = typename std::remove_reference<typename Pixel2::type>::type;
        using p2_value_t = typename pixel_model_t::value_type;
        using pixel2_mutable = mp11::mp_if<p2_is_mutable, Pixel2, value_core<p2_value_t>>;

        Pixel1 p1;
        pixel2_mutable p2;

        color_convert_impl(p1._pixel, p2._pixel);
    }
};

struct ccv1 {
    template <typename Pixel>
    void operator()(Pixel) {
        mp11::mp_for_each<representative_pixels_t>(ccv2<Pixel>());
    }
};

void test_color_convert() {
   mp11::mp_for_each<representative_pixels_t>(ccv1());
}

void test_packed_pixel()
{
    using rgb565_pixel_t = packed_pixel_type<uint16_t, mp11::mp_list_c<unsigned,5,6,5>, rgb_layout_t>::type;
    boost::function_requires<PixelValueConcept<rgb565_pixel_t> >();
    static_assert(sizeof(rgb565_pixel_t) == 2, "");

    // define a bgr556 pixel
    using bgr556_pixel_t = packed_pixel_type<uint16_t, mp11::mp_list_c<unsigned,5,6,5>, bgr_layout_t>::type;
    boost::function_requires<PixelValueConcept<bgr556_pixel_t> >();

    // Create a zero packed pixel and a full regular unpacked pixel.
    rgb565_pixel_t r565;//((uint16_t)0);
    rgb8_pixel_t rgb_full(255,255,255);

    // Convert all channels of the unpacked pixel to the packed one & ensure the packed one is full
    get_color(r565,red_t())   = channel_convert<kth_element_type<rgb565_pixel_t, 0>::type>(get_color(rgb_full,red_t()));
    get_color(r565,green_t()) = channel_convert<kth_element_type<rgb565_pixel_t, 1>::type>(get_color(rgb_full,green_t()));
    get_color(r565,blue_t())  = channel_convert<kth_element_type<rgb565_pixel_t, 2>::type>(get_color(rgb_full,blue_t()));
    error_if(r565 != rgb565_pixel_t((uint16_t)65535));

    // rgb565 is compatible with bgr556. Test interoperability
    boost::function_requires<PixelsCompatibleConcept<rgb565_pixel_t,bgr556_pixel_t> >();

    do_basic_test<value_core<rgb565_pixel_t,0>, value_core<bgr556_pixel_t,1> >(r565).test_heterogeneous();

    color_convert(r565,rgb_full);
    color_convert(rgb_full,r565);

    // Test bit-aligned pixel reference
    using bgr121_ref_t = const bit_aligned_pixel_reference<std::uint8_t, mp11::mp_list_c<int,1,2,1>, bgr_layout_t, true>;
    using rgb121_ref_t = const bit_aligned_pixel_reference<std::uint8_t, mp11::mp_list_c<int,1,2,1>, rgb_layout_t, true>;
    using rgb121_pixel_t = rgb121_ref_t::value_type;
    rgb121_pixel_t p121;
    do_basic_test<reference_core<bgr121_ref_t,0>, reference_core<rgb121_ref_t,1> >(p121).test_heterogeneous();
    do_basic_test<value_core<rgb121_pixel_t,0>, reference_core<rgb121_ref_t,1> >(p121).test_heterogeneous();

    static_assert(pixel_reference_is_proxy<rgb8_planar_ref_t>::value, "");
    static_assert(pixel_reference_is_proxy<bgr121_ref_t>::value, "");

    static_assert(!pixel_reference_is_proxy<rgb8_pixel_t>::value, "");
    static_assert(!pixel_reference_is_proxy<rgb8_pixel_t&>::value, "");
    static_assert(!pixel_reference_is_proxy<rgb8_pixel_t const&>::value, "");

    static_assert(pixel_reference_is_mutable<rgb8_pixel_t&>::value, "");
    static_assert(!pixel_reference_is_mutable<rgb8_pixel_t const&>::value, "");

    static_assert(pixel_reference_is_mutable<rgb8_planar_ref_t>::value, "");
    static_assert(pixel_reference_is_mutable<rgb8_planar_ref_t const&>::value, "");

    static_assert(!pixel_reference_is_mutable<rgb8c_planar_ref_t>::value, "");
    static_assert(!pixel_reference_is_mutable<rgb8c_planar_ref_t const&>::value, "");

    static_assert(pixel_reference_is_mutable<bgr121_ref_t>::value, "");
    static_assert(!pixel_reference_is_mutable<bgr121_ref_t::const_reference>::value, "");
}

void test_pixel() {
    test_packed_pixel();
    rgb8_pixel_t rgb8(1,2,3);

    do_basic_test<value_core<rgb8_pixel_t,0>, reference_core<rgb8_pixel_t&,1> >(rgb8).test_all();
    do_basic_test<value_core<bgr8_pixel_t,0>, reference_core<rgb8_planar_ref_t,1> >(rgb8).test_all();
    do_basic_test<reference_core<rgb8_planar_ref_t,0>, reference_core<bgr8_pixel_t&,1> >(rgb8).test_all();
    do_basic_test<reference_core<const rgb8_pixel_t&,0>, reference_core<rgb8_pixel_t&,1> >(rgb8).test_all();

    test_color_convert();

    // Semantic vs physical channel accessors. Named channel accessors
    bgr8_pixel_t bgr8(rgb8);
    error_if(bgr8[0] == rgb8[0]);
    error_if(dynamic_at_c(bgr8,0) == dynamic_at_c(rgb8,0));
    error_if(gil::at_c<0>(bgr8) == gil::at_c<0>(rgb8));
    error_if(semantic_at_c<0>(bgr8) != semantic_at_c<0>(rgb8));
    error_if(get_color(bgr8,blue_t()) != get_color(rgb8,blue_t()));

    // Assigning a grayscale channel to a pixel
    gray16_pixel_t g16(34);
    g16 = 8;
    uint16_t g = get_color(g16,gray_color_t());
    error_if(g != 8);
    error_if(g16 != 8);
}


int main()
{
    try
    {
        test_pixel();
        return EXIT_SUCCESS;
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    catch (...)
    {
        return EXIT_FAILURE;
    }
}