Line | Branch | Exec | Source |
---|---|---|---|
1 | // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
2 | // vi: set et ts=4 sw=4 sts=4: | ||
3 | // | ||
4 | // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder | ||
5 | // SPDX-License-Identifier: GPL-3.0-or-later | ||
6 | // | ||
7 | /*! | ||
8 | * \file | ||
9 | * \ingroup InputOutput | ||
10 | * \brief A simple writer class for raster images. | ||
11 | */ | ||
12 | #ifndef DUMUX_RASTER_IMAGE_WRITER_HH | ||
13 | #define DUMUX_RASTER_IMAGE_WRITER_HH | ||
14 | |||
15 | #include <cassert> | ||
16 | #include <string> | ||
17 | #include <vector> | ||
18 | #include <fstream> | ||
19 | #include <sstream> | ||
20 | #include <algorithm> | ||
21 | #include <map> | ||
22 | #include <iterator> | ||
23 | #include <iostream> | ||
24 | |||
25 | #include <dune/common/exceptions.hh> | ||
26 | #include <dumux/common/stringutilities.hh> | ||
27 | #include <dumux/io/rasterimagedata.hh> | ||
28 | |||
29 | namespace Dumux { | ||
30 | |||
31 | /*! | ||
32 | * \ingroup InputOutput | ||
33 | * \brief A simple reader class for the Netpbm format (https://en.wikipedia.org/wiki/Netpbm_format). | ||
34 | * So far, only black and white (*.pbm) and grayscale (*pgm) images are supported. | ||
35 | */ | ||
36 | class NetPBMWriter | ||
37 | { | ||
38 | template<typename T> | ||
39 | using Result = Detail::RasterImageData::Result<T>; | ||
40 | |||
41 | using HeaderData = Detail::RasterImageData::HeaderData; | ||
42 | |||
43 | public: | ||
44 | |||
45 | template<class ValueType> | ||
46 | static void write(const std::string& writeFileName, | ||
47 | Result<ValueType>& img, | ||
48 | const bool useDuneGridOrdering = true) | ||
49 | { | ||
50 | // Pass the image to the writer | ||
51 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | writeRasterImageFile_(writeFileName, img, useDuneGridOrdering); |
52 | } | ||
53 | |||
54 | template<class ValueType> | ||
55 | static void write(const std::string& writeFileName, | ||
56 | const std::size_t& nCols, | ||
57 | const std::size_t& nRows, | ||
58 | const std::string& magicNumber, | ||
59 | const std::string& type, | ||
60 | const std::string& encoding, | ||
61 | const std::vector<ValueType>& img, | ||
62 | const bool useDuneGridOrdering = true) | ||
63 | { | ||
64 | // Fill header data and collect img data | ||
65 | HeaderData headerData; | ||
66 | headerData.nCols = nCols; | ||
67 | headerData.nRows = nRows; | ||
68 | headerData.format.magicNumber = magicNumber; | ||
69 | Result<ValueType> result(std::move(img), std::move(headerData)); | ||
70 | |||
71 | writeRasterImageFile_(writeFileName, result, useDuneGridOrdering); | ||
72 | } | ||
73 | |||
74 | |||
75 | /*! | ||
76 | * \brief Change the ordering of the pixels according | ||
77 | * to Dune's convention, shifting the origin from upper left to lower left. | ||
78 | * | ||
79 | * \param result The image's pixel values ordered from top to bottom. | ||
80 | */ | ||
81 | template<class T> | ||
82 | ✗ | static void applyDuneGridOrdering(Result<T>& result) | |
83 | { | ||
84 | ✗ | auto tmp = result; | |
85 | ✗ | for (std::size_t i = 0; i < result.size(); i += result.header().nCols) | |
86 | ✗ | std::swap_ranges((result.begin() + i), (result.begin() + i + result.header().nCols), (tmp.end() - i - result.header().nCols)); | |
87 | ✗ | } | |
88 | |||
89 | private: | ||
90 | |||
91 | template <class T> | ||
92 | 4 | static void writeRasterImageFile_(const std::string& writeFileName, | |
93 | Result<T>& result, | ||
94 | const bool useDuneGridOrdering = true) | ||
95 | { | ||
96 | // This will reverse any reordering | ||
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
4 | if (useDuneGridOrdering) |
98 | ✗ | applyDuneGridOrdering(result); | |
99 | |||
100 | // Write the corrected image and header information to a file | ||
101 | 8 | std::ofstream outfile(writeFileName, std::ios::trunc); | |
102 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
4 | outfile << result.header().format.magicNumber << "\n"; |
103 |
3/6✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
|
8 | outfile << result.header().nCols << " " << result.header().nRows << "\n"; |
104 |
3/4✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
4 | if ((result.header().format.magicNumber == "P2") || (result.header().format.magicNumber == "P5")) |
105 | { | ||
106 |
4/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 1 times.
|
124 | for (int i = 0; i < result.size(); i++) |
107 |
3/8✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 60 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
240 | outfile << result[i] << "\n"; |
108 | } | ||
109 | else | ||
110 | { | ||
111 |
4/4✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 1 times.
|
244 | for (int i = 0; i < result.size(); i++) |
112 | { | ||
113 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 50 times.
|
120 | if (i % result.header().nCols == 0) // wrap once per row |
114 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | outfile << "\n"; |
115 |
3/6✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 60 times.
✗ Branch 8 not taken.
|
360 | outfile << result[i]; |
116 | } | ||
117 | } | ||
118 | 4 | } | |
119 | |||
120 | }; | ||
121 | |||
122 | } // namespace Dumux | ||
123 | |||
124 | #endif | ||
125 |