-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_ximage.cpp
More file actions
113 lines (98 loc) · 4.15 KB
/
Copy pathtest_ximage.cpp
File metadata and controls
113 lines (98 loc) · 4.15 KB
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
/***************************************************************************
* Copyright (c) Wolf Vollprecht, Sylvain Corlay and Johan Mabille *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor/core/xoperation.hpp"
#include "xtensor/views/xview.hpp"
#include "xtensor/io/xio.hpp"
#include "xtensor-io/ximage.hpp"
namespace xt
{
xarray<uint8_t> test_image_rgb = {{{ 0, 0, 0},
{255, 0, 0},
{ 0, 255, 0}},
{{ 0, 0, 255},
{228, 135, 255},
{255, 255, 255}}};
xarray<uint8_t> test_image_rgba = {{{ 0, 0, 0, 255},
{255, 0, 0, 255},
{ 0, 255, 0, 255}},
{{ 0, 0, 255, 255},
{228, 135, 255, 255},
{255, 255, 255, 255}}};
xarray<uint8_t> test_image_jpg = {{{ 0, 0, 35},
{255, 12, 0},
{ 0, 255, 0}},
{{ 5, 10, 236},
{222, 124, 237},
{255, 253, 255}}};
TEST(ximage, load_png)
{
auto img = load_image("files/test.png");
EXPECT_TRUE(all(equal(test_image_rgb, img)));
}
TEST(ximage, load_gif)
{
auto img = load_image("files/test.gif");
EXPECT_TRUE(all(equal(test_image_rgba, img)));
}
TEST(ximage, load_jpg)
{
auto img = load_image("files/test.jpg");
bool test = (amax(test_image_rgb - img)() <= 35);
EXPECT_TRUE(test);
}
TEST(ximage, exceptions)
{
EXPECT_THROW(dump_image("files/dump_test.png", view(test_image_rgb, all(), 0, 0)), std::runtime_error);
EXPECT_THROW(dump_image("files/dump_test.png", view(test_image_rgb, all(), all(), all(), newaxis())), std::runtime_error);
}
TEST(ximage, save_png)
{
{
// save as 'unsigned char'
dump_image("files/dump_test.png", test_image_rgb);
auto img = load_image("files/dump_test.png");
EXPECT_TRUE(all(equal(test_image_rgb, img)));
}
{
// save as 'int'
dump_image("files/dump_test.png", 1*test_image_rgb);
auto img = load_image("files/dump_test.png");
EXPECT_TRUE(all(equal(test_image_rgb, img)));
}
}
TEST(ximage, save_gif)
{
dump_image("files/dump_test.gif", test_image_rgba);
auto img = load_image("files/dump_test.gif");
EXPECT_TRUE(all(equal(test_image_rgba, img)));
}
TEST(ximage, save_jpg)
{
auto img_large = load_image("files/big.jpg");
dump_image("files/dump_test.jpg", img_large);
auto img = load_image("files/dump_test.jpg");
bool test = (amax(img - img_large)() <= 50);
EXPECT_TRUE(test);
}
TEST(ximage, float_img)
{
xarray<float> test_float(test_image_rgb);
// TIFF supports float pixels
dump_image("files/dump_test_float.tif", test_float);
auto img = load_image<float>("files/dump_test_float.tif");
EXPECT_EQ(test_float, img);
// PNG does not support float pixels => test automatic conversion
dump_image("files/dump_test_float.png", test_float);
auto cimg = load_image("files/dump_test_float.png");
EXPECT_EQ(test_image_rgb, cimg);
img = load_image<float>("files/dump_test_float.png");
EXPECT_TRUE(allclose(test_float, 255.0*img));
}
}