OpenASIP 2.2
Loading...
Searching...
No Matches
Bin2nImageWriter.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2019 Tampere University of Technology.
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 Bin2nImageWriter.cc
26 *
27 * Implementation of Bin2nImageWriter class.
28 *
29 * @author Topi Leppanen 2019 (topi.leppanen-no.spam-tuni.fi)
30 * @note rating: red
31 */
32
33
34#include <string>
35
36#include "Bin2nImageWriter.hh"
37#include "BitVector.hh"
38
39
41 int rowLength) :
42 bits_(bits), rowLength_(rowLength), nextBitIndex_(0) {
43}
44
47
48/**
49 * Writes the data bits to the given stream. Reverses the byte endianness
50 *
51 * @param stream The output stream.
52 */
53void
54Bin2nImageWriter::writeImage(std::ostream& stream) const {
55
56 unsigned int size = bits_.size();
57
58 /// Bitvector-size has to always be exactly divisible by rowlength
59 unsigned int numOfRows = size / rowLength_;
60
61 for (unsigned int i = 0; i < numOfRows; i++){
63
64 }
65}
66
67/**
68 * Returns the byte of the given bit vector that starts at the given index
69 * and is 'length' bits long. Pads from the left to size 8.
70 *
71 * @param bits The bit vector.
72 * @param startIndex The start index.
73 * @param length How many indexes forward is read. 0-8.
74 * @return The byte.
75 */
76char
77Bin2nImageWriter::character(const BitVector& bits, unsigned int startIndex, int length) {
78 unsigned int vectorSize = bits.size();
79 char character = 0;
80
81 /// Pad from the left with zeros.
82 character = character << (8 - length);
83
84 /// Read the first bit.
85 if (bits[startIndex] && length > 0){
86 character++;
87 }
88
89 /// Read the rest of the bits.
90 for (int i = 1; i < length; i++) {
91 character = character << 1;
92 if (startIndex + i < vectorSize && bits[startIndex + i]) {
93 character++;
94 }
95 }
96 return character;
97}
98
99
100/**
101 * Writes a sequence of bits to the given stream.
102 *
103 * When this method is called sequentially, the first bit to be written is
104 * the next bit to the last bit written in the previous method call.
105 * Pads the sequence from the left with zeroes up to the nearest multiple of 2.
106 * After that, reverses the string before writing it to the stream.
107 *
108 * @param stream The output stream.
109 * @param length The length of the sequence to be written.
110 */
111void
112Bin2nImageWriter::writeSequence(std::ostream& stream, unsigned int length) const{
113
114 if (length == 0){
115 return;
116 }
117
118 unsigned int lastIndex = nextBitIndex_ + length -1;
119 unsigned int imem_width = nextPowerOf2(length);
120 std::string sequence_word = "";
121
122 /// Pad from the left the correct amount of "0 bytes".
123 for (unsigned int i = 0;i < (imem_width-length)/8; i++){
124 char zero = 0;
125 sequence_word.push_back(zero);
126 }
127
128 /// There might be a partial byte, which needs to be padded from the left
129 unsigned int partial_start_index = nextBitIndex_;
130 unsigned int partial_char_len = length % 8;
131
132 /// If there is no partial byte, we don't want to insert anything
133 if (partial_char_len != 0){
134 sequence_word.push_back(character(bits_, partial_start_index, partial_char_len));
135 partial_start_index += partial_char_len;
136 }
137
138 /// Create the full bytes.
139 for (unsigned int index = partial_start_index; index <= lastIndex; index +=8){
140
141 sequence_word.push_back(character(bits_, index, 8));
142
143 }
144
145 /// Update this for the next iteration of the function.
146 nextBitIndex_ = lastIndex + 1;
147
148 /// Reverse the byte endianness and write the stream.
149 for (std::string::reverse_iterator iter = sequence_word.rbegin();
150 iter != sequence_word.rend(); iter++){
151
152 stream << *iter;
153 }
154}
155
156const BitVector&
158 return bits_;
159}
160
161/**
162 * Returns the next power of 2 of the given parameter
163 *
164 * @param n
165 * @return p the next power of 2 of n
166 */
167unsigned int
168Bin2nImageWriter::nextPowerOf2(unsigned int n) const{
169
170 unsigned int p = 1;
171
172 if (!(n & (n - 1)))
173 return n;
174
175 while(p < n) {
176 p <<= 1;
177 }
178
179 return p;
180}
unsigned int nextPowerOf2(unsigned int n) const
virtual void writeImage(std::ostream &stream) const
void writeSequence(std::ostream &stream, unsigned int length) const
int rowLength_
The length of a row in the output.
unsigned int nextBitIndex_
The index of the next bit to be written.
const BitVector & bits_
The bits to be written.
Bin2nImageWriter(const BitVector &bits, int rowLength)
static char character(const BitVector &bits, unsigned int startIndex, int length)
const BitVector & bits() const
virtual ~Bin2nImageWriter()