OpenASIP 2.2
Loading...
Searching...
No Matches
AsciiImageWriter.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file AsciiImageWriter.cc
26 *
27 * Implementation of AsciiImageWriter class.
28 *
29 * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30 * @author Otto Esko 2008 (otto.esko-no.spam-tut.fi)
31 * @note rating: red
32 */
33
34#include <cstdint>
35#include <string>
36#include <iomanip>
37#include <vector>
38#include <numeric>
39#include <algorithm>
40#include <cmath>
41#include <iostream>
42
43#include "AsciiImageWriter.hh"
44#include "BitVector.hh"
45
46using namespace std;
47using std::string;
48
49/**
50 * The constructor.
51 *
52 * @param bits The bits to be written.
53 * @param rowLength The length of the row in the output stream.
54 */
55AsciiImageWriter::AsciiImageWriter(const BitVector& bits, int rowLength) :
56 bits_(bits), rowLength_(rowLength), nextBitIndex_(0) {
57}
58
59
60/**
61 * The destructor.
62 */
65
66
67/**
68 * Writes the bits to the given stream.
69 *
70 * @param stream The output stream.
71 */
72void
73AsciiImageWriter::writeImage(std::ostream& stream) const {
74 int column = 0;
75 for (BitVector::const_iterator iter = bits_.begin(); iter != bits_.end();
76 iter++) {
77 BitVector::const_iterator nextIter = iter;
78 nextIter++;
79 stream << *iter;
80 column++;
81 if (column == rowLength_ && nextIter != bits_.end()) {
82 stream << '\n';
83 column = 0;
84 } else if (nextIter == bits_.end()) {
85 // pad the remaining bits with zeroes if necessary
86 while (column < rowLength_) {
87 stream << "0";
88 column++;
89 }
90 }
91 }
92}
93
94
95/**
96 * Returns the bit vector to be written by this writer.
97 *
98 * @return The bit vector.
99 */
100const BitVector&
102 return bits_;
103}
104
105
106/**
107 * Returns the row length.
108 *
109 * @return The row length.
110 */
111int
113 return rowLength_;
114}
115
116
117/**
118 * Writes a sequence of bits to the given stream.
119 *
120 * When this method is called sequentially, the first bit to be written is
121 * the next bit to the last bit written in the previous method call.
122 *
123 * @param stream The output stream.
124 * @param length The length of the sequence to be written.
125 * @exception OutOfRange If the bit vector does not contain enough bits for
126 * the row.
127 */
128void
130 std::ostream& stream, int length, bool padEnd) const {
131 unsigned int lastIndex = nextBitIndex_ + length - 1;
132
133 if (lastIndex >= bits_.size() && !padEnd) {
134 const string procName = "AsciiImageWriter::writeSequence";
135 throw OutOfRange(__FILE__, __LINE__, procName);
136 }
137
138 for (unsigned int index = nextBitIndex_; index <= lastIndex; index++) {
139 if (index < bits_.size()) {
140 stream << bits_[index];
141 } else {
142 stream << "0";
143 }
144 }
145 nextBitIndex_ = lastIndex + 1;
146}
147
148/**
149 * Writes a sequence of bits in hex format to the given stream.
150 *
151 * When this method is called sequentially, the first bit to be written is
152 * the next bit to the last bit written in the previous method call.
153 *
154 * @param stream The output stream.
155 * @param length The length of the sequence to be written.
156 * @exception OutOfRange If the bit vector does not contain enough bits for
157 * the row.
158 */
159void
161 std::ostream& stream, int length, bool padEnd) const {
162 unsigned int lastIndex = nextBitIndex_ + length - 1;
163
164 if (lastIndex >= bits_.size() && !padEnd) {
165 const string procName = __func__;
166 throw OutOfRange(__FILE__, __LINE__, procName);
167 }
168
169 const int nibbleCount = static_cast<int>(ceil(
170 static_cast<double>(length) / 4.0));
171
172 std::vector<bool> bitRow;
173
174 // Copy bitstream for one row
175 for (unsigned int i = nextBitIndex_; i <= lastIndex; ++i) {
176 bitRow.push_back(bits_[i]);
177 }
178
179 // optionally extend bit stream
180 int numPadBits = 4 * nibbleCount - length;
181 if (numPadBits) {
182 std::vector<bool> padBits(numPadBits, 0);
183 bitRow.insert(bitRow.begin(), padBits.begin(), padBits.end());
184 }
185
186 // Generate a list of nibble
187 std::vector<uint8_t> Nibble;
188 for (auto it = bitRow.begin(); it < bitRow.end(); it += 4) {
189 Nibble.push_back(std::accumulate(
190 it, it + 4, 0,[] (int x, int y) {return (x << 1) + y;}));
191 }
192
193 // "print" hex stream
194 for (auto ui8 = Nibble.begin(); ui8 < Nibble.end(); ++ui8) {
195 stream << std::hex << (int) *ui8;
196 }
197
198 nextBitIndex_ += length;
199}
#define __func__
int rowLength_
The length of a row in the output.
const BitVector & bits_
The bits to be written.
virtual ~AsciiImageWriter()
unsigned int nextBitIndex_
The index of the next bit to be written.
const BitVector & bits() const
void writeSequence(std::ostream &stream, int length, bool padEnd=false) const
AsciiImageWriter(const BitVector &bits, int rowLength)
void writeHexSequence(std::ostream &stream, int length, bool padEnd=false) const
virtual void writeImage(std::ostream &stream) const