OpenASIP  2.0
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 
17 using std::string;
18 using std::endl;
19 
20 const string COMMENT = "-- Memory initialization file";
21 const string WIDTH = "WIDTH = ";
22 const string DEPTH = "DEPTH = ";
23 const string A_RADIX = "ADDRESS_RADIX = DEC;";
24 const string D_RADIX = "DATA_RADIX = BIN;";
25 const string BEGIN = "CONTENT BEGIN";
26 const string END = "END;";
27 const 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 // }
38 MifImageWriter::MifImageWriter(const BitVector& bits, int rowLength):
39  AsciiImageWriter(bits,rowLength) {
40 }
41 
42 /**
43  * The destructor.
44  */
46 }
47 
48 /**
49  * Writes the bits to the given stream.
50  *
51  * @param stream The output stream.
52  */
53 void 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  */
88 void 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 }
INDENT
const string INDENT
Definition: MifImageWriter.cc:27
BitVector
Definition: BitVector.hh:44
AsciiImageWriter::rowLength
int rowLength() const
Definition: AsciiImageWriter.cc:112
BitVector.hh
AsciiImageWriter::writeSequence
void writeSequence(std::ostream &stream, int length, bool padEnd=false) const
Definition: AsciiImageWriter.cc:129
MifImageWriter::MifImageWriter
MifImageWriter(const BitVector &bits, int rowLength)
Definition: MifImageWriter.cc:38
MifImageWriter::writeHeader
void writeHeader(std::ostream &stream) const
Definition: MifImageWriter.cc:88
END
const string END
Definition: MifImageWriter.cc:26
MifImageWriter::writeImage
virtual void writeImage(std::ostream &stream) const
Definition: MifImageWriter.cc:53
D_RADIX
const string D_RADIX
Definition: MifImageWriter.cc:24
DEPTH
const string DEPTH
Definition: MifImageWriter.cc:22
MifImageWriter.hh
WIDTH
const string WIDTH
Definition: MifImageWriter.cc:21
A_RADIX
const string A_RADIX
Definition: MifImageWriter.cc:23
AsciiImageWriter::bits
const BitVector & bits() const
Definition: AsciiImageWriter.cc:101
COMMENT
const string COMMENT
Definition: MifImageWriter.cc:20
AsciiImageWriter
Definition: AsciiImageWriter.hh:45
BEGIN
const string BEGIN
Definition: MifImageWriter.cc:25
MifImageWriter::~MifImageWriter
virtual ~MifImageWriter()
Definition: MifImageWriter.cc:45