File: bmp_test.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (284 lines) | stat: -rw-r--r-- 7,900 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
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
//
// Copyright 2013 Christian Henning
//
// 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
//
#define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
#define BOOST_FILESYSTEM_VERSION 3
#include <boost/gil.hpp>
#include <boost/gil/extension/io/bmp.hpp>

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

#include <fstream>
#include <sstream>
#include <string>

#include "mandel_view.hpp"
#include "paths.hpp"
#include "subimage_test.hpp"

namespace fs = boost::filesystem;
namespace gil = boost::gil;
namespace mp11 = boost::mp11;

void test_read_image_info_using_string()
{
    {
        using backend_t = gil::get_reader_backend<std::string const, gil::bmp_tag>::type;
        backend_t backend = read_image_info(bmp_filename, gil::bmp_tag());

        BOOST_TEST_EQ(backend._info._width, 1000);
        BOOST_TEST_EQ(backend._info._height, 600);
    }
    {
        std::ifstream in(bmp_filename.c_str(), std::ios::binary);
        using backend_t = gil::get_reader_backend<std::ifstream, gil::bmp_tag>::type;
        backend_t backend = read_image_info(in, gil::bmp_tag());

        BOOST_TEST_EQ(backend._info._width, 1000);
        BOOST_TEST_EQ(backend._info._height, 600);
    }
    {
        FILE *file = fopen(bmp_filename.c_str(), "rb");
        using backend_t = gil::get_reader_backend<FILE *, gil::bmp_tag>::type;
        backend_t backend = read_image_info(file, gil::bmp_tag());

        BOOST_TEST_EQ(backend._info._width, 1000);
        BOOST_TEST_EQ(backend._info._height, 600);
    }
    {
        fs::path my_path(bmp_filename);
        using backend_t = gil::get_reader_backend<fs::path, gil::bmp_tag>::type;
        backend_t backend = read_image_info(my_path, gil::bmp_tag());

        BOOST_TEST_EQ(backend._info._width, 1000);
        BOOST_TEST_EQ(backend._info._height, 600);
    }
}

void test_read_image()
{
    {
        gil::rgb8_image_t img;
        read_image(bmp_filename, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
    {
        std::ifstream in(bmp_filename.c_str(), std::ios::binary);

        gil::rgb8_image_t img;
        read_image(in, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
    {
        FILE *file = fopen(bmp_filename.c_str(), "rb");

        gil::rgb8_image_t img;
        read_image(file, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
    {
        fs::path my_path(bmp_filename);

        gil::rgb8_image_t img;
        read_image(my_path, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
}

void test_read_and_convert_image()
{
    {
        gil::rgb8_image_t img;
        read_and_convert_image(bmp_filename, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
    {
        std::ifstream in(bmp_filename.c_str(), std::ios::binary);

        gil::rgb8_image_t img;
        read_and_convert_image(in, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
    {
        FILE *file = fopen(bmp_filename.c_str(), "rb");

        gil::rgb8_image_t img;
        read_and_convert_image(file, img, gil::bmp_tag());

        BOOST_TEST_EQ(img.width(), 1000);
        BOOST_TEST_EQ(img.height(), 600);
    }
}

void test_read_view()
{
    {
        gil::rgb8_image_t img(1000, 600);
        read_view(bmp_filename, gil::view(img), gil::bmp_tag());
    }
    {
        std::ifstream in(bmp_filename.c_str(), std::ios::binary);

        gil::rgb8_image_t img(1000, 600);
        read_view(in, gil::view(img), gil::bmp_tag());
    }
    {
        FILE *file = fopen(bmp_filename.c_str(), "rb");

        gil::rgb8_image_t img(1000, 600);
        read_view(file, gil::view(img), gil::bmp_tag());
    }
}

void test_read_and_convert_view()
{
    {
        gil::rgb8_image_t img(1000, 600);
        read_and_convert_view(bmp_filename, gil::view(img), gil::bmp_tag());
    }
    {
        std::ifstream in(bmp_filename.c_str(), std::ios::binary);

        gil::rgb8_image_t img(1000, 600);
        read_and_convert_view(in, gil::view(img), gil::bmp_tag());
    }
    {
        FILE *file = fopen(bmp_filename.c_str(), "rb");

        gil::rgb8_image_t img(1000, 600);
        read_and_convert_view(file, gil::view(img), gil::bmp_tag());
    }
}

void test_write_view()
{
    auto const b = gil::rgb8_pixel_t(0, 0, 255);
    auto const g = gil::rgb8_pixel_t(0, 255, 0);
    {
        std::string filename(bmp_out + "write_test_string.bmp");
        gil::write_view(filename, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
    }
    {
        std::string filename(bmp_out + "write_test_ofstream.bmp");
        std::ofstream out(filename.c_str(), std::ios::binary);

        gil::write_view(out, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
    }
    {
        std::string filename(bmp_out + "write_test_file.bmp");

        FILE *file = fopen(filename.c_str(), "wb");
        gil::write_view(file, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
    }
    {
        std::string filename(bmp_out + "write_test_info.bmp");

        gil::image_write_info<gil::bmp_tag> info;
        FILE *file = fopen(filename.c_str(), "wb");
        gil::write_view(file, create_mandel_view(1000, 600, b, g), info);
    }
}

void test_stream()
{
    // 1. Read an image.
    std::ifstream in(bmp_filename.c_str(), std::ios::binary);

    gil::rgb8_image_t img;
    gil::read_image(in, img, gil::bmp_tag());

    // 2. Write image to in-memory buffer.
    std::stringstream out_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
    gil::write_view(out_buffer, gil::view(img), gil::bmp_tag());

    // 3. Copy in-memory buffer to another.
    std::stringstream in_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
    in_buffer << out_buffer.rdbuf();

    // 4. Read in-memory buffer to gil image
    gil::rgb8_image_t dst;
    gil::read_image(in_buffer, dst, gil::bmp_tag());

    // 5. Write out image.
    std::string filename(bmp_out + "stream_test.bmp");
    std::ofstream out(filename.c_str(), std::ios_base::binary);

    gil::write_view( out, gil::view( dst ), gil::bmp_tag() );
}

void test_stream_2()
{
    std::filebuf in_buf;
    if (!in_buf.open(bmp_filename.c_str(), std::ios::in | std::ios::binary))
    {
        BOOST_TEST(false);
    }
    std::istream in(&in_buf);

    gil::rgb8_image_t img;
    read_image(in, img, gil::bmp_tag());
}

void test_subimage()
{
    run_subimage_test<gil::rgb8_image_t, gil::bmp_tag>(
        bmp_filename, gil::point_t(0, 0), gil::point_t(1000, 1));

    run_subimage_test<gil::rgb8_image_t, gil::bmp_tag>(
        bmp_filename, gil::point_t(39, 7), gil::point_t(50, 50));
}

void test_dynamic_image()
{
    using my_img_types = mp11::mp_list
        <
            gil::gray8_image_t,
            gil::gray16_image_t,
            gil::rgb8_image_t,
            gil::rgba8_image_t
        >;

    gil::any_image<my_img_types> image;
    gil::read_image(bmp_filename.c_str(), image, gil::bmp_tag());

    gil::write_view(bmp_out + "dynamic_image_test.bmp", gil::view(image), gil::bmp_tag());
}

int main(int argc, char *argv[])
{
    try
    {
        test_read_image_info_using_string();
        test_read_image();
        test_read_and_convert_image();
        test_read_view();
        test_read_and_convert_view();
        test_write_view();
        test_stream();
        test_stream_2();
        test_subimage();
        test_dynamic_image();
    }
    catch (std::exception const& e)
    {
        BOOST_ERROR(e.what());
    }
    return boost::report_errors();
}