Skip to content

Commit

Permalink
Trabalho
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos54vidal committed Dec 17, 2021
1 parent bd45fbc commit 17129ab
Show file tree
Hide file tree
Showing 408 changed files with 146,044 additions and 25,466 deletions.
Binary file added Documentação/scriptie.pdf.pdf
Binary file not shown.
180 changes: 145 additions & 35 deletions Source/Ex1e2_A/BitStreamV2.h → Source/BitStreamV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include <string>
#include <iostream>
#include <fstream>
#include "Dependencies/opencv/build/include/opencv2/opencv.hpp"
#include "AudioFile/AudioFile.h"
#include <filesystem> /** It needs a compiler that supports the C++17 language standard, which is implemented e.g. on the g++ compiler from the version 8 onwards,
but it´s recommended a g++ version release 9 or newer for full support. https://gcc.gnu.org/gcc-9/changes.html#cxx **/

using namespace cv;
using namespace std;
using namespace std::filesystem;/** It needs a compiler that supports the C++17 language standard, which is implemented e.g. on the g++ compiler from the version 8 onwards,
but it´s recommended the g++ version release 9 or newer for full support. https://gcc.gnu.org/gcc-9/changes.html#cxx **/
Expand All @@ -23,13 +26,15 @@ class BitStream
public:
fstream ifs; ///the object name for the input stream.
fstream ofs; /// the object name for the output stream.
uint8_t bit; /// bit attribute
int bit; /// bit attribute
float total_nbits=0; /// the limited number of bits to be treated. (value to be imputed/adquired from the user)
char rbuffer;/// the "buffer" read from the file for coding or decoding.
char wbuffer=0;/// the produced "buffer" to be written to the file (whether it is coded or decoded data).
uint8_t* rbuffer;/// the "buffer" read from the file for coding or decoding.
uint8_t* wbuffer=0;/// the produced "buffer" to be written to the file (whether it is coded or decoded data).
std::string wfilename; /// the name of the output file.
string file_extension;/// is required, to now how to handle the data extraction.


/**------------ Parameterized Constructors-------------*//
/**------------ Parameterized Constructors-------------*/

/** the tree attributes inserted to the constructor are:
"filename": The name of the decoded file (and realtive path, if necessary). For coding it means the name of the file to be coded, for decoding,
Expand All @@ -47,17 +52,21 @@ of the inputed file name (and path), changing only the extension to ".bin".
total_nbits = y;
if (x == "d"){
wfilename = filename;
std::filesystem::path p = filename, e = ".bin";
p.replace_extension(e);
rfilename = p.string();
cout << "In = " << wfilename << endl;
std::filesystem::path p1 = filename, e = "bin";
file_extension = std::filesystem::path(p1).extension().string();
cout << "ext = " << file_extension << endl;
p1.replace_extension(e);
rfilename = p1.string();
cout << "Out = " << wfilename << endl;
}

if (x == "c"){
rfilename = filename;
std::filesystem::path p = filename, e = "bin";
p.replace_extension(e);
wfilename = p.string();
std::filesystem::path p2 = filename, e = "bin";
file_extension = std::filesystem::path(p2).extension().string();
cout << "ext = " << file_extension << endl;
p2.replace_extension(e);
wfilename = p2.string();
cout << "Out = " << wfilename << endl;
}

Expand All @@ -83,37 +92,96 @@ Write(uint8_t bit): para juntar o byte bit a bit;
/// Method for reading a file sample by sample, character or text, with no limits.
void Read_file ()
{
ifs.open(rfilename, std::fstream::app | std::fstream::binary | std::fstream::in | std::fstream::ate);
cout << "filesize " << ifs.tellg() << endl;
cout << "In = " << rfilename << endl;
ifs.seekg (0, ios::beg);
if (file_extension==".wav"){
AudioFile<double> audioFile;

audioFile.load(rfilename);
double rbuffer = static_cast<double>(rbuffer);
int channels = audioFile.getNumChannels();
int numSamples = audioFile.getNumSamplesPerChannel();

for (int i = 0; i <= numSamples; i++){
for (int c = 0; c <= channels; c++){
rbuffer = audioFile.samples[c][i];
}
}
}

if (file_extension==".txt"){

while (ifs.get(rbuffer)){
ifs.open(rfilename, std::fstream::app | std::fstream::binary | std::fstream::in | std::fstream::ate);
cout << "filesize " << ifs.tellg() << endl;
cout << "In = " << rfilename << endl;
ifs.seekg (0, ios::beg);
char rbuffer = static_cast<char>(rbuffer);
while (ifs.get(rbuffer)){
// Read (rbuffer); // it was used to "chain" the methods together, to copy the bits to a new file with no treatment.
}
ofs.close();
}

if (file_extension==".png"){
Mat img = imread(rfilename);

if (img.empty()){
cout << "Error : Image cannot be loaded..!!" << endl;
}
uint8_t* pixelPtr = (uint8_t*)img.data;
uint8_t rbuffer = static_cast<uint8_t>(rbuffer);

int cn = img.channels(); //gives us the colour channels (which colours they are)

for (int r = 0; r < img.rows; r++){
for (int c = 0; c < img.cols; c++){
img.at<Vec3b>(r,c);
}
}
}
ofs.close();

}

/// Method for reading a file sample by sample, character or text, with bit limits.
void Read_file_tbits ()
{
tnbytes = ceil(total_nbits/buffer_size);
ifs.open(rfilename, std::fstream::app | std::fstream::binary | std::fstream::in | std::fstream::ate);
cout << "Filesize = " << ifs.tellg() << " Bytes -> " << tnbytes << " Byte(s)" << "(" << total_nbits << " bit(s))" << endl;
cout << "In = " << rfilename << endl;
ifs.seekg (0, ios::beg);

for (long i = 1; i <= tnbytes; i++){
ifs.get(rbuffer);
tnbytes--;
char rbuffer = static_cast<char>(rbuffer);

if (file_extension==".txt"){
ifs.open(rfilename, std::fstream::app | std::fstream::binary | std::fstream::in | std::fstream::ate);
cout << "Filesize = " << ifs.tellg() << " Bytes -> " << tnbytes << " Byte(s)" << "(" << total_nbits << " bit(s))" << endl;
cout << "In = " << rfilename << endl;
ifs.seekg (0, ios::beg);

for (long i = 1; i <= tnbytes; i++){
ifs.get(rbuffer);
tnbytes--;
// Read (rbuffer); // it was used to "chain" the methods together, to copy the bits to a new file with no treatment.
}
ofs.close();
ofs.close();
}

if (file_extension==".wav"){
AudioFile<double> audioFile;

audioFile.load(rfilename);

int channels = audioFile.getNumChannels();
int numSamples = audioFile.getNumSamplesPerChannel();

for (long i = 1; i <= tnbytes; i++){
for (int c = 0; c < channels; c++){
for (int i = 0; i < numSamples; i++){
rbuffer = audioFile.samples[c][i];
}
}
tnbytes--;
}
}
}

/** Method for extraction of each bit from the buffer. The buffer size should correspond to the sample, character or pixel size.
It starts reading from the least significant bit towards the most significant bit. It also works for either Read_file methods.**/
void Read(char rb)
void Read(uint8_t rb)
{
if (tnbytes == 0 && total_nbits != 0){
int a = static_cast<int>(total_nbits); int b = static_cast<int>(buffer_size);
Expand All @@ -135,25 +203,66 @@ It starts reading from the least significant bit towards the most significant bi

/// Method for writing the produced buffer to the new file with no limit number of bits parameter.
void Write_file (char wb){
if (file_extension==".txt"){
ofs.open(wfilename, std::fstream::app | std::fstream::binary); // para adicionar, caso o ficheiro exista usar: std::ofstream::app . std::ofstream::binary para que o output seja em binário
ofs.write(&wb, 1);
// ofs.flush();
ofs.close();
}

if (file_extension==".wav"){
AudioFile<double> audioFile;
AudioFile<double> audioFileOut;
double wb = static_cast<double>(wb);

audioFile.load(rfilename);

audioFileOut.setNumChannels(audioFile.getNumChannels());
audioFileOut.setNumSamplesPerChannel(audioFile.getNumSamplesPerChannel());

int channels = audioFile.getNumChannels();
int numSamples = audioFile.getNumSamplesPerChannel();

for (int c = 0; c < channels; c++){
for (int i = 0; i < numSamples; i++)
{
audioFileOut.samples[c][i] = wb;
}
}
audioFile.save (wfilename, AudioFileFormat::Wave);
}

if (file_extension==".png"){
Mat img = imread(rfilename);
if (img.empty()){
cout << "Error : Image cannot be loaded..!!" << endl;
}

Mat img2 = Mat::zeros(Size(img.rows, img.cols), CV_8UC3);

for (int r = 0; r < img.rows; r++){
for (int c = 0; c < img.cols; c++){
img2.at<Vec3b>(r,c);
}
}
imwrite(wfilename, img2);
}

ofs.open(wfilename, std::fstream::app | std::fstream::binary); // para adicionar, caso o ficheiro exista usar: std::ofstream::app . std::ofstream::binary para que o output seja em binário
ofs.write(&wb, 1);
// ofs.flush();
ofs.close();
}

/// Method for constructing the buffer bit by bit, starting from the most significant bit towards the least significant bit.
void Write(uint8_t bit)
{
uint8_t wbuffer = static_cast<uint8_t>(wbuffer);
if (nbits < buffer_size+1){ //reset do contador dos bits no buffer para saber quando o buffer está cheio e que deve escrever para o fichreiro
bit = (bit << nbits-1);
wbuffer = (wbuffer | bit ); ///shifts 1 possição e set the last bit of the buffer to bit
wbuffer = (wbuffer | bit ); ///shifts 1 possition and set the last bit of the buffer to bit
nbits--;
}

if (nbits==0)
{
nbits=8;
nbits=buffer_size;
Write_file (wbuffer);
}
}
Expand All @@ -163,16 +272,17 @@ operations on C++ is "8bits" so, when the user establish a smaller number of bit
it fills the remaining bits with "0" to complete the buffer.**/
void Write_tbits(uint8_t bit)
{
uint8_t wbuffer = static_cast<uint8_t>(wbuffer);
for(int i = 1; i = total_nbits; i++){
if (nbits < 9){ //reset do contador dos bits no buffer para saber quando o buffer está cheio e que deve escrever para o fichreiro
if (nbits < buffer_size+1){ //reset do contador dos bits no buffer para saber quando o buffer está cheio e que deve escrever para o fichreiro
bit = (bit << nbits-1);
wbuffer = (wbuffer | bit ); ///shifts 1 possição e set the last bit of the buffer to bit
nbits--;
}

if (nbits==0)
{
nbits=8;
nbits=buffer_size;
Write_file (wbuffer);
}
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 17129ab

Please sign in to comment.