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
|
#include "catch.hpp"
// mapnik
#include <mapnik/image.hpp>
#include <mapnik/color.hpp>
#include <mapnik/image_view_any.hpp>
#include <mapnik/image_util.hpp>
TEST_CASE("image view") {
SECTION("test rgba8") {
mapnik::image_rgba8 im(4,4);
mapnik::color c_red("red");
mapnik::color c_blue("blue");
mapnik::color c_green("green");
mapnik::color c_yellow("yellow");
mapnik::fill(im, c_red);
// Upper Left 2x2 is blue
mapnik::set_pixel(im, 0, 0, c_blue);
mapnik::set_pixel(im, 0, 1, c_blue);
mapnik::set_pixel(im, 1, 0, c_blue);
mapnik::set_pixel(im, 1, 1, c_blue);
// Upper Right 2x2 is green
mapnik::set_pixel(im, 2, 0, c_green);
mapnik::set_pixel(im, 2, 1, c_green);
mapnik::set_pixel(im, 3, 0, c_green);
mapnik::set_pixel(im, 3, 1, c_green);
// Lower Left 2x2 is yellow
mapnik::set_pixel(im, 0, 2, c_yellow);
mapnik::set_pixel(im, 0, 3, c_yellow);
mapnik::set_pixel(im, 1, 2, c_yellow);
mapnik::set_pixel(im, 1, 3, c_yellow);
mapnik::image_rgba8 im2(5,5);
mapnik::fill(im2, c_red);
// Now that we have test data run tests
mapnik::image_view_rgba8 view_all(0,0,4,4,im);
mapnik::image_view_rgba8 view_blue(0,0,2,2,im);
mapnik::image_view_rgba8 view_green(2,0,2,2,im);
mapnik::image_view_rgba8 view_yellow(0,2,2,2,im);
mapnik::image_view_rgba8 view_red(2,2,2,2,im);
mapnik::image_view_rgba8 view_bad(99,99,99,99,im);
const mapnik::image_view_rgba8 view_all_2(0,0,4,4,im2);
// Check that image_views all have the same underlying data
CHECK(view_all == view_blue);
CHECK(view_all == view_green);
CHECK(view_all == view_yellow);
CHECK(view_all == view_red);
CHECK(view_all.data() == im);
// Check that view_all and view_all_2 are not the same underlying data
CHECK_FALSE(view_all == view_all_2);
CHECK(view_all < view_all_2);
// Check that copy constructor works
mapnik::image_view_rgba8 view_all_3(view_all_2);
CHECK(view_all_2 == view_all_3);
// Check other constructor
mapnik::image_view_rgba8 view_all_4(std::move(view_all_3));
CHECK(view_all_2 == view_all_4);
// Check that x offset is correct
CHECK(view_all.x() == 0);
CHECK(view_blue.x() == 0);
CHECK(view_green.x() == 2);
CHECK(view_yellow.x() == 0);
CHECK(view_red.x() == 2);
CHECK(view_bad.x() == 3);
// Check that y offset is correct
CHECK(view_all.y() == 0);
CHECK(view_blue.y() == 0);
CHECK(view_green.y() == 0);
CHECK(view_yellow.y() == 2);
CHECK(view_red.y() == 2);
CHECK(view_bad.y() == 3);
// Check that width is correct
CHECK(view_all.width() == 4);
CHECK(view_blue.width() == 2);
CHECK(view_green.width() == 2);
CHECK(view_yellow.width() == 2);
CHECK(view_red.width() == 2);
CHECK(view_bad.width() == 1);
// Check that height is correct
CHECK(view_all.height() == 4);
CHECK(view_blue.height() == 2);
CHECK(view_green.height() == 2);
CHECK(view_yellow.height() == 2);
CHECK(view_red.height() == 2);
CHECK(view_bad.height() == 1);
// Check that size is correct
CHECK(view_all.size() == 64);
CHECK(view_blue.size() == 16);
CHECK(view_green.size() == 16);
CHECK(view_yellow.size() == 16);
CHECK(view_red.size() == 16);
// Check that row_size is correct
CHECK(view_all.row_size() == 16);
CHECK(view_blue.row_size() == 8);
CHECK(view_green.row_size() == 8);
CHECK(view_yellow.row_size() == 8);
CHECK(view_red.row_size() == 8);
// Check that get_premultiplied is correct
CHECK_FALSE(view_all.get_premultiplied());
CHECK_FALSE(view_blue.get_premultiplied());
CHECK_FALSE(view_green.get_premultiplied());
CHECK_FALSE(view_yellow.get_premultiplied());
CHECK_FALSE(view_red.get_premultiplied());
// Check that operator to retrieve value works properly
CHECK(view_all(0,0) == c_blue.rgba());
CHECK(view_blue(0,0) == c_blue.rgba());
CHECK(view_green(0,0) == c_green.rgba());
CHECK(view_yellow(0,0) == c_yellow.rgba());
CHECK(view_red.row_size() == 8);
// Check that offset is correct
CHECK(view_all.get_offset() == 0.0);
CHECK(view_blue.get_offset() == 0.0);
CHECK(view_green.get_offset() == 0.0);
CHECK(view_yellow.get_offset() == 0.0);
CHECK(view_red.get_offset() == 0.0);
// Check that scaling is correct
CHECK(view_all.get_scaling() == 1.0);
CHECK(view_blue.get_scaling() == 1.0);
CHECK(view_green.get_scaling() == 1.0);
CHECK(view_yellow.get_scaling() == 1.0);
CHECK(view_red.get_scaling() == 1.0);
// CHECK that image dtype is correct
CHECK(view_all.get_dtype() == mapnik::image_dtype_rgba8);
CHECK(view_blue.get_dtype() == mapnik::image_dtype_rgba8);
CHECK(view_green.get_dtype() == mapnik::image_dtype_rgba8);
CHECK(view_yellow.get_dtype() == mapnik::image_dtype_rgba8);
CHECK(view_red.get_dtype() == mapnik::image_dtype_rgba8);
unsigned expected_val;
using pixel_type = mapnik::image_view_rgba8::pixel_type;
// Check that all data in the view is correct
// Blue
expected_val = c_blue.rgba();
for (std::size_t y = 0; y < view_blue.height(); ++y)
{
std::size_t width = view_blue.width();
pixel_type const* data_1 = view_blue.get_row(y);
pixel_type const* data_2 = view_blue.get_row(y, 1);
for (std::size_t x = 0; x < width; ++x)
{
CHECK(*data_1 == expected_val);
++data_1;
}
for (std::size_t x = 1; x < width; ++x)
{
CHECK(*data_2 == expected_val);
++data_2;
}
}
// Green
expected_val = c_green.rgba();
for (std::size_t y = 0; y < view_green.height(); ++y)
{
std::size_t width = view_green.width();
pixel_type const* data_1 = view_green.get_row(y);
pixel_type const* data_2 = view_green.get_row(y, 1);
for (std::size_t x = 0; x < width; ++x)
{
CHECK(*data_1 == expected_val);
++data_1;
}
for (std::size_t x = 1; x < width; ++x)
{
CHECK(*data_2 == expected_val);
++data_2;
}
}
// Yellow
expected_val = c_yellow.rgba();
for (std::size_t y = 0; y < view_yellow.height(); ++y)
{
std::size_t width = view_yellow.width();
pixel_type const* data_1 = view_yellow.get_row(y);
pixel_type const* data_2 = view_yellow.get_row(y, 1);
for (std::size_t x = 0; x < width; ++x)
{
CHECK(*data_1 == expected_val);
++data_1;
}
for (std::size_t x = 1; x < width; ++x)
{
CHECK(*data_2 == expected_val);
++data_2;
}
}
// Red
expected_val = c_red.rgba();
for (std::size_t y = 0; y < view_red.height(); ++y)
{
std::size_t width = view_red.width();
pixel_type const* data_1 = view_red.get_row(y);
pixel_type const* data_2 = view_red.get_row(y, 1);
for (std::size_t x = 0; x < width; ++x)
{
CHECK(*data_1 == expected_val);
++data_1;
}
for (std::size_t x = 1; x < width; ++x)
{
CHECK(*data_2 == expected_val);
++data_2;
}
}
} // END SECTION
SECTION("image_view_null")
{
mapnik::image_view_null view_null;
const mapnik::image_view_null view_null2;
mapnik::image_view_null view_null3(view_null2);
mapnik::image_view_null & view_null4 = view_null3;
// All nulls are equal
CHECK(view_null == view_null4);
CHECK(view_null == view_null2);
// No null is greater
CHECK_FALSE(view_null < view_null4);
CHECK_FALSE(view_null < view_null2);
// Check defaults
CHECK(view_null.x() == 0);
CHECK(view_null.y() == 0);
CHECK(view_null.width() == 0);
CHECK(view_null.height() == 0);
CHECK(view_null.size() == 0);
CHECK(view_null.row_size() == 0);
CHECK(view_null.get_offset() == 0.0);
CHECK(view_null.get_scaling() == 1.0);
CHECK(view_null.get_dtype() == mapnik::image_dtype_null);
CHECK_FALSE(view_null.get_premultiplied());
// Should throw if we try to access data.
REQUIRE_THROWS(view_null(0,0));
CHECK(view_null.get_row(0) == nullptr);
CHECK(view_null.get_row(0,0) == nullptr);
} // END SECTION
SECTION("image view any")
{
mapnik::image_view_any im_any_null;
CHECK(im_any_null.get_dtype() == mapnik::image_dtype_null);
mapnik::image_gray8 im(4,4);
mapnik::image_view_gray8 im_view(0,0,4,4,im);
mapnik::image_view_any im_view_any(im_view);
CHECK(im_view_any.get_dtype() == mapnik::image_dtype_gray8);
CHECK(im_view_any.width() == 4);
CHECK(im_view_any.height() == 4);
CHECK(im_view_any.size() == 16);
CHECK(im_view_any.row_size() == 4);
CHECK_FALSE(im_view_any.get_premultiplied());
CHECK(im_view_any.get_offset() == 0.0);
CHECK(im_view_any.get_scaling() == 1.0);
} // END SECTION
} // END TEST CASE
|