FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
export_tga.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/adjacency/adjactor.hpp>
11#include <kernel/util/string.hpp>
12
13// includes, system
14#include <vector>
15#include <iostream>
16#include <fstream>
17#include <cstdint>
18
19namespace FEAT
20{
21 namespace Adjacency
22 {
42 {
43 public:
53 template<typename Adjactor_>
54 static void write(const String& filename, const Adjactor_& adj)
55 {
56 // try to open output file
57 std::ofstream ofs(filename, std::ios_base::binary|std::ios_base::out);
58 if(!(ofs.is_open() && ofs.good()))
59 throw FileError(String("Failed to open '") + filename + "'");
60
61 // write
62 write(ofs, adj);
63
64 // close file
65 ofs.close();
66 }
67
77 template<typename Adjactor_>
78 static void write(std::ostream& os, const Adjactor_& adj)
79 {
80 typedef std::uint8_t u8;
81
82 // set up header
83 u8 header[18];
84 for(int i(0); i < 18; ++i)
85 header[i] = 0;
86
87 // get dimensions
88 const Index w = adj.get_num_nodes_image();
89 const Index h = adj.get_num_nodes_domain();
90
91 // make sure we do not exceed the 16-bit range
92 XASSERTM((w <= Index(32767)) && (h <= Index(32767)), "TGA dimensions are limited to 32767");
93
94 // set dimensions
95 header[12] = u8( w & 0xFF);
96 header[13] = u8((w >> 8) & 0x7F);
97 header[14] = u8( h & 0xFF);
98 header[15] = u8((h >> 8) & 0x7F);
99
100 // set basic stuff
101 header[ 2] = u8( 3); // datatype code
102 header[16] = u8( 8); // bits per pixel
103 header[17] = u8(32); // image descriptor
104
105 // write header to file
106 os.write(reinterpret_cast<char*>(header), 18);
107
108 // allocate a scan-line
109 std::vector<char> scan(std::size_t(w), -1);
110
111 // okay, let's loop over all domain nodes
112 for(Index i(0); i < h; ++i)
113 {
114 // get the image iterators
115 auto jt = adj.image_end(i);
116
117 // loop over all image nodes and set black pixels
118 for(auto it = adj.image_begin(i); it != jt; ++it)
119 {
120 // set pixel to black
121 scan[*it] = 0;
122 }
123
124 // write scan-line
125 os.write(scan.data(), std::streamsize(scan.size()));
126
127 // clear pixels
128 for(auto it = adj.image_begin(i); it != jt; ++it)
129 {
130 scan[*it] = -1;
131 }
132 }
133
134 // finally, write the TGA v2 footer
135 const char* footer = "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0";
136 os.write(footer, 26);
137 }
138 }; // class ExportTGA
139 } // namespace Adjacency
140} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Truevision TGA exporter.
Definition: export_tga.hpp:42
static void write(std::ostream &os, const Adjactor_ &adj)
Writes out an adjactor to a TGA image file.
Definition: export_tga.hpp:78
static void write(const String &filename, const Adjactor_ &adj)
Writes out an adjactor to a TGA image file.
Definition: export_tga.hpp:54
Base class for file related errors.
Definition: exception.hpp:173
String class implementation.
Definition: string.hpp:46
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.