OpenASIP 2.2
Loading...
Searching...
No Matches
MifImageWriter.cc
Go to the documentation of this file.
1/**
2 * @file MifImageWriter.cc
3 *
4 * Implementation of MifImageWriter class.
5 *
6 * @author Otto Esko 2009 (otto.esko-no.spam-tut.fi)
7 * @note rating: red
8 */
9
10#include <string>
11#include <iostream>
12#include <cmath>
13
14#include "MifImageWriter.hh"
15#include "BitVector.hh"
16
17using std::string;
18using std::endl;
19
20const string COMMENT = "-- Memory initialization file";
21const string WIDTH = "WIDTH = ";
22const string DEPTH = "DEPTH = ";
23const string A_RADIX = "ADDRESS_RADIX = DEC;";
24const string D_RADIX = "DATA_RADIX = BIN;";
25const string BEGIN = "CONTENT BEGIN";
26const string END = "END;";
27const string INDENT = " ";
28
29/**
30 * The constructor.
31 *
32 * @param bits The bits to be written.
33 * @param rowLength The length of the row in the output stream.
34 */
35// MifImageWriter::MifImageWriter(const BitVector& bits, int rowLength):
36// bits_(bits), rowLength_(rowLength) {
37// }
38MifImageWriter::MifImageWriter(const BitVector& bits, int rowLength):
39 AsciiImageWriter(bits,rowLength) {
40}
41
42/**
43 * The destructor.
44 */
47
48/**
49 * Writes the bits to the given stream.
50 *
51 * @param stream The output stream.
52 */
53void MifImageWriter::writeImage(std::ostream& stream) const {
54 int wordCount =
55 static_cast<int>(ceil((float)bits().size() / rowLength()));
56 writeHeader(stream);
57
58 int address = 0;
59 if (wordCount == 0) {
60 // empty MIF is invalid, fill one row with zeroes
61 stream << address << INDENT << ":" << INDENT;
62 for (int i = 0; i < rowLength(); i++) {
63 stream << "0";
64 }
65 stream << ";" << endl;
66 } else {
67 bool padEndings = false;
68 for (int i = 0; i < wordCount-1; i++) {
69 stream << address << INDENT << ":" << INDENT;
70 writeSequence(stream, rowLength(), padEndings);
71 stream << ";" << endl;
72 address++;
73 }
74 // last line might need to be padded
75 padEndings = true;
76 stream << address << INDENT << ":" << INDENT;
77 writeSequence(stream, rowLength(), padEndings);
78 stream << ";" << endl;
79 }
80 stream << END << endl;
81}
82
83/**
84 * Writes the MIF header to the given stream.
85 *
86 * @param stream The output stream.
87 */
88void MifImageWriter::writeHeader(std::ostream& stream) const {
89 int wordCount = static_cast<int>(ceil((float)bits().size() / rowLength()));
90 stream << COMMENT << endl
91 << WIDTH << rowLength() << ";" << endl;
92
93 // empty MIF is invalid
94 if (wordCount == 0) {
95 stream << DEPTH << "1" << ";" << endl;
96 } else {
97 stream << DEPTH << wordCount << ";" << endl;
98 }
99
100 stream << A_RADIX << endl
101 << D_RADIX << endl << endl
102 << BEGIN << endl;
103}
#define INDENT
const string END
const string INDENT
const string DEPTH
const string BEGIN
const string A_RADIX
const string WIDTH
const string COMMENT
const string D_RADIX
const BitVector & bits() const
void writeSequence(std::ostream &stream, int length, bool padEnd=false) const
virtual void writeImage(std::ostream &stream) const
virtual ~MifImageWriter()
void writeHeader(std::ostream &stream) const
MifImageWriter(const BitVector &bits, int rowLength)