Skip to content
Snippets Groups Projects
Commit b7c77461 authored by Dario Wagner's avatar Dario Wagner
Browse files

Huffman ver. 1.0

parent a5f878fd
No related branches found
No related tags found
No related merge requests found
......@@ -2,9 +2,13 @@
#include <bits/stdc++.h>
#include "greyscale.h"
#include "unit.h"
#include <vector>
#include <queue>
//BERALL SIZE T ZU INT gemacht !
//POTENTIELLER FEHLER: HHE BREITE GETAUSCHT gemacht!
using std::vector;
GreyScale::GreyScale(int b,int h)
{
if (h < 0) greyError("Nur Vektoren mit positiver hoehe!");
......@@ -15,8 +19,91 @@ GreyScale::GreyScale(int b,int h)
for(int i =0;i< h*b;i++) mat[i] = 1;
}
struct Node {
byte_t value;
int freq;
Node *p0, *p1;
};
//void GreyScale::readman() const {
//
//}
//
//void GreyScale::writeman() const {
//
//}
//besser keine Funktion, sondern einfach reinschreiben
int arr[256];
for (int i=0; i < hoehe*breite; i++)
{
arr[std::rint(mat[i]*255)] += 1;
}
return arr;
bool operator >(Node x, Node y)
{
if (x.freq > y.freq)
return true;
else if (x.freq < y.freq)
return false;
else
{
if (x.value > y.value)
return true;
else
return false;
}
}
std::priority_queue<Node, std::vector<Node>, std::greater<Node>> q //?
//
void GreyScale::baummacher(int arr[]){
vector<Node> vec;
Node gw;
for (int i=0; i < 256; i++)
{
if (arr[i] != 0) //std priorityqueue (vergleichsoperator schreiben)
{
gw.value = i;
gw.freq = arr[i];
vec.push_back(gw);
}
}
Node k0;
Node k1;
Node papa;
std::priority_queue<Node, vec, std::greater<Node>> q;
while (q.size() > 1)
{
k0 = q.top();
q.pop();
//q.push(); //Frage?
k1 = q.top();
q.pop();
papa.p0 = k0;
papa.p1 = k1;
q.push(papa);
}
}
//void GreyScale::nodeschreiber(int arr[]){
// Node big;
// Node pipi[256];
// big.p0 = nodeschreiber()
//}
//for schleife die durch die gs geht,
//while schleife bis p0/p1 Nullzeiger sind (8bit durchgehen und weitermachen wo man aufgehrt hat)
//letzenode.value in gs rein
byte_t GreyScale::baumleser(std::istream &s, "baum oder 1te Node"){
if(p0 != 'leere menge lmao') char a = s.get();
if (a == 1)
baumleser(s,*p1);
if (a == 0)
baumleser(s,*p0);
return value;
}
GreyScale::GreyScale(const GreyScale& x){
hoehe = x.hoehe;
......@@ -34,7 +121,7 @@ float &GreyScale::operator()(int i,int j){
}
float GreyScale::operator()(int i,int j) const{
if((i>breite+1) or (j > hoehe+1) or (i < -1) or (j < -1)) greyError("lesezugriff drauen");
if((i>breite) or (j > hoehe) or (i < -1) or (j < -1)) greyError("lesezugriff drauen");
if(i == -1) i = 0;
if(j == -1) j = 0;
if(i == breite) i = breite - 1;
......@@ -228,13 +315,12 @@ GreyScale GreyScale::convolve(const float mask[], int size) const
std::istream &operator>>(std::istream &s, GreyScale &x) {
int inpo;
char blar;
std::string mnr;
float skra;
//std::string mull;
int tmpbreite = 0;
int tmphoehe =0;
s >> mnr;
if(mnr != "P2") GreyScale::greyError("NOT PC");
x.check(s);
s >> tmpbreite;
x.check(s);
......@@ -242,7 +328,7 @@ std::istream &operator>>(std::istream &s, GreyScale &x) {
x.resize(tmpbreite,tmphoehe);
x.check(s);
s >> skra;
if(mnr == "P2"){ //besser vlt mit s.get und dann byte vergleichen
for(int i=0; i< x.hoehe*x.breite;i++){
x.check(s);
s >> inpo;
......@@ -250,7 +336,25 @@ std::istream &operator>>(std::istream &s, GreyScale &x) {
}
x.check(s);
if(s.rdstate()!= std::ios::eofbit) GreyScale::greyError("nicht am ende");
}
else if (mnr =="P5"){
for(int i = 0; i < x.hoehe*x.breite; i++){
x.check(s); //maybe falsch
blar=s.get();
inpo = int(blar);
x.mat[i] = float(inpo)/skra;
}
x.check(s);
if(s.rdstate()!= std::ios::eofbit) GreyScale::greyError("nicht am ende");
}
else if ( mnr =="MHa" ){
}
else if (mnr =="MHb"){
}
else
return s;
}
......
......@@ -40,6 +40,8 @@ class GreyScale
GreyScale convolve(const float mask[], int size=3) const;
void check(std::istream &s);
void baummacher(int arr[]);
friend std::istream& operator>>(std::istream&, GreyScale&);
friend std::ostream& operator<<(std::ostream&, const GreyScale&);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment