From b7c774613a847c3824b621c4622d225c78d62c03 Mon Sep 17 00:00:00 2001 From: Dario Wagner <dario.wagner@rwth-aachen.de> Date: Mon, 21 Jun 2021 19:01:13 +0200 Subject: [PATCH] Huffman ver. 1.0 --- greyscale.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++---- greyscale.h | 2 + 2 files changed, 115 insertions(+), 9 deletions(-) diff --git a/greyscale.cpp b/greyscale.cpp index 9a7a19f..63cbb43 100644 --- a/greyscale.cpp +++ b/greyscale.cpp @@ -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: H�HE 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 aufgeh�rt 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 drau�en"); + if((i>breite) or (j > hoehe) or (i < -1) or (j < -1)) greyError("lesezugriff drau�en"); 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,15 +328,33 @@ std::istream &operator>>(std::istream &s, GreyScale &x) { x.resize(tmpbreite,tmphoehe); x.check(s); s >> skra; - - for(int i=0; i< x.hoehe*x.breite;i++){ + 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; + x.mat[i] = float(inpo)/skra; + } x.check(s); - s >> inpo; - x.mat[i] = float(inpo)/skra; + 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"){ + } - x.check(s); - if(s.rdstate()!= std::ios::eofbit) GreyScale::greyError("nicht am ende"); + else return s; } diff --git a/greyscale.h b/greyscale.h index 40f3357..2660a7b 100644 --- a/greyscale.h +++ b/greyscale.h @@ -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&); -- GitLab