File: image_apply_opacity.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (105 lines) | stat: -rw-r--r-- 4,020 bytes parent folder | download | duplicates (4)
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

#include "catch.hpp"

// mapnik
#include <mapnik/image_any.hpp>
#include <mapnik/color.hpp>
#include <mapnik/image_util.hpp>

TEST_CASE("image apply_opacity") {

SECTION("test rgba8") {

    mapnik::image_rgba8 im(4,4);
    mapnik::image_rgba8 im2(4,4,true,true); // Initialize as already premultiplied
    mapnik::image_any im_any(mapnik::image_rgba8(4,4));
    mapnik::image_any im2_any(mapnik::image_rgba8(4,4,true,true));

    // Fill the images with meaningfull values
    mapnik::color c1(57,70,128,128); // This color is not premultiplied
    mapnik::color c2(57,70,128,128, true); // This color is premultiplied
    mapnik::fill(im, c1); // Because c1 is not premultiplied it will make the image not premultiplied
    mapnik::fill(im_any, c1); // Because c1 is not premultiplied it will make the image not premultiplied
    mapnik::fill(im2, c2); // Because c1 is premultiplied it will make the image premultiplied
    mapnik::fill(im2_any, c2); // Because c1 is premultiplied it will make the image premultiplied

    mapnik::apply_opacity(im, 0.75);
    mapnik::apply_opacity(im_any, 0.75);
    mapnik::apply_opacity(im2, 0.75);
    mapnik::apply_opacity(im2_any, 0.75);

    mapnik::color out;
    // This should have only changed the alpha, as it was not premultipleid
    out = mapnik::get_pixel<mapnik::color>(im, 0, 0);
    CHECK(static_cast<int>(out.red()) == 57);
    CHECK(static_cast<int>(out.green()) == 70);
    CHECK(static_cast<int>(out.blue()) == 128);
    CHECK(static_cast<int>(out.alpha()) == 96);
    out = mapnik::get_pixel<mapnik::color>(im_any, 0, 0);
    CHECK(static_cast<int>(out.red()) == 57);
    CHECK(static_cast<int>(out.green()) == 70);
    CHECK(static_cast<int>(out.blue()) == 128);
    CHECK(static_cast<int>(out.alpha()) == 96);
    // This will be different because it is demultiplied then premultiplied again after setting alpha
    out = mapnik::get_pixel<mapnik::color>(im2, 0, 0);
    CHECK(static_cast<int>(out.red()) == 43);
    CHECK(static_cast<int>(out.green()) == 53);
    CHECK(static_cast<int>(out.blue()) == 96);
    CHECK(static_cast<int>(out.alpha()) == 96);
    out = mapnik::get_pixel<mapnik::color>(im2_any, 0, 0);
    CHECK(static_cast<int>(out.red()) == 43);
    CHECK(static_cast<int>(out.green()) == 53);
    CHECK(static_cast<int>(out.blue()) == 96);
    CHECK(static_cast<int>(out.alpha()) == 96);

} // END SECTION

SECTION("test rgba8 overflow") {

    mapnik::image_rgba8 im(4,4);
    mapnik::color c(128,128,128,128); // This color is premultiplied
    mapnik::fill(im, c); // Because c1 is not premultiplied it will make the image not premultiplied
    mapnik::color out;
    out = mapnik::get_pixel<mapnik::color>(im, 0, 0);
    CHECK(static_cast<int>(out.red()) == 128);
    CHECK(static_cast<int>(out.green()) == 128);
    CHECK(static_cast<int>(out.blue()) == 128);
    CHECK(static_cast<int>(out.alpha()) == 128);

    mapnik::apply_opacity(im, 2.5);

    out = mapnik::get_pixel<mapnik::color>(im, 0, 0);
    CHECK(static_cast<int>(out.red()) == 128);
    CHECK(static_cast<int>(out.green()) == 128);
    CHECK(static_cast<int>(out.blue()) == 128);
    CHECK(static_cast<int>(out.alpha()) == 128);

} // END SECTION

SECTION("test rgba8 underflow") {

    mapnik::image_rgba8 im(4,4);
    mapnik::color c(128,128,128,128); // This color is premultiplied
    mapnik::fill(im, c); // Because c1 is not premultiplied it will make the image not premultiplied

    mapnik::apply_opacity(im, -2.5);

    mapnik::color out;
    out = mapnik::get_pixel<mapnik::color>(im, 0, 0);
    CHECK(static_cast<int>(out.red()) == 128);
    CHECK(static_cast<int>(out.green()) == 128);
    CHECK(static_cast<int>(out.blue()) == 128);
    CHECK(static_cast<int>(out.alpha()) == 0);

} // END SECTION

SECTION("test gray8") {

    mapnik::image_gray8 im(4,4);
    mapnik::image_any im_any(mapnik::image_gray8(4,4));

    CHECK_THROWS(mapnik::apply_opacity(im, 0.25));
    CHECK_THROWS(mapnik::apply_opacity(im_any, 0.25));

} // END SECTION
} // END TEST_CASE