OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
AsciiImageWriter Class Reference

#include <AsciiImageWriter.hh>

Inheritance diagram for AsciiImageWriter:
Inheritance graph
Collaboration diagram for AsciiImageWriter:
Collaboration graph

Public Member Functions

 AsciiImageWriter (const BitVector &bits, int rowLength)
 
virtual ~AsciiImageWriter ()
 
virtual void writeImage (std::ostream &stream) const
 
- Public Member Functions inherited from BitImageWriter
virtual ~BitImageWriter ()
 

Protected Member Functions

const BitVectorbits () const
 
int rowLength () const
 
void writeSequence (std::ostream &stream, int length, bool padEnd=false) const
 
void writeHexSequence (std::ostream &stream, int length, bool padEnd=false) const
 

Private Attributes

const BitVectorbits_
 The bits to be written.
 
int rowLength_
 The length of a row in the output.
 
unsigned int nextBitIndex_
 The index of the next bit to be written.
 

Detailed Description

Writes the bit image with ASCII 1's and 0's.

Definition at line 45 of file AsciiImageWriter.hh.

Constructor & Destructor Documentation

◆ AsciiImageWriter()

AsciiImageWriter::AsciiImageWriter ( const BitVector bits,
int  rowLength 
)

The constructor.

Parameters
bitsThe bits to be written.
rowLengthThe length of the row in the output stream.

Definition at line 55 of file AsciiImageWriter.cc.

55 :
57}
int rowLength_
The length of a row in the output.
const BitVector & bits_
The bits to be written.
unsigned int nextBitIndex_
The index of the next bit to be written.
const BitVector & bits() const

◆ ~AsciiImageWriter()

AsciiImageWriter::~AsciiImageWriter ( )
virtual

The destructor.

Definition at line 63 of file AsciiImageWriter.cc.

63 {
64}

Member Function Documentation

◆ bits()

const BitVector & AsciiImageWriter::bits ( ) const
protected

Returns the bit vector to be written by this writer.

Returns
The bit vector.

Definition at line 101 of file AsciiImageWriter.cc.

101 {
102 return bits_;
103}

References bits_.

Referenced by MifImageWriter::writeHeader(), ArrayImageWriter::writeImage(), ArrayProgramImageWriter::writeImage(), AsciiProgramImageWriter::writeImage(), CoeImageWriter::writeImage(), HexImageWriter::writeImage(), and MifImageWriter::writeImage().

◆ rowLength()

int AsciiImageWriter::rowLength ( ) const
protected

Returns the row length.

Returns
The row length.

Definition at line 112 of file AsciiImageWriter.cc.

112 {
113 return rowLength_;
114}

References rowLength_.

Referenced by MifImageWriter::writeHeader(), VhdlImageWriter::writeHeader(), ArrayImageWriter::writeImage(), CoeImageWriter::writeImage(), HexImageWriter::writeImage(), and MifImageWriter::writeImage().

◆ writeHexSequence()

void AsciiImageWriter::writeHexSequence ( std::ostream &  stream,
int  length,
bool  padEnd = false 
) const
protected

Writes a sequence of bits in hex format to the given stream.

When this method is called sequentially, the first bit to be written is the next bit to the last bit written in the previous method call.

Parameters
streamThe output stream.
lengthThe length of the sequence to be written.
Exceptions
OutOfRangeIf the bit vector does not contain enough bits for the row.

Definition at line 160 of file AsciiImageWriter.cc.

161 {
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__

References __func__, bits_, and nextBitIndex_.

Referenced by HexImageWriter::writeImage().

◆ writeImage()

void AsciiImageWriter::writeImage ( std::ostream &  stream) const
virtual

Writes the bits to the given stream.

Parameters
streamThe output stream.

Implements BitImageWriter.

Reimplemented in ArrayImageWriter, ArrayProgramImageWriter, AsciiProgramImageWriter, CoeImageWriter, HexImageWriter, MifImageWriter, VhdlImageWriter, and VhdlProgramImageWriter.

Definition at line 73 of file AsciiImageWriter.cc.

73 {
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}

References bits_, and rowLength_.

Referenced by InstructionDictionary::generateDecompressor(), and MoveSlotDictionary::generateDictionaryVhdl().

◆ writeSequence()

void AsciiImageWriter::writeSequence ( std::ostream &  stream,
int  length,
bool  padEnd = false 
) const
protected

Writes a sequence of bits to the given stream.

When this method is called sequentially, the first bit to be written is the next bit to the last bit written in the previous method call.

Parameters
streamThe output stream.
lengthThe length of the sequence to be written.
Exceptions
OutOfRangeIf the bit vector does not contain enough bits for the row.

Definition at line 129 of file AsciiImageWriter.cc.

130 {
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}

References bits_, and nextBitIndex_.

Referenced by ArrayImageWriter::writeImage(), ArrayProgramImageWriter::writeImage(), AsciiProgramImageWriter::writeImage(), CoeImageWriter::writeImage(), and MifImageWriter::writeImage().

Member Data Documentation

◆ bits_

const BitVector& AsciiImageWriter::bits_
private

The bits to be written.

Definition at line 64 of file AsciiImageWriter.hh.

Referenced by bits(), writeHexSequence(), writeImage(), and writeSequence().

◆ nextBitIndex_

unsigned int AsciiImageWriter::nextBitIndex_
mutableprivate

The index of the next bit to be written.

Definition at line 68 of file AsciiImageWriter.hh.

Referenced by writeHexSequence(), and writeSequence().

◆ rowLength_

int AsciiImageWriter::rowLength_
private

The length of a row in the output.

Definition at line 66 of file AsciiImageWriter.hh.

Referenced by rowLength(), and writeImage().


The documentation for this class was generated from the following files: