You are on page 1of 5

----------------------------------------------------------------------

----------------------------------------------------------------------
--------------------------//HUFFMAN CODING//--------------------------
------------------------************************----------------------
----------------------------------------------------------------------
------//PAKEGE DECLERATION//-------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;

package huffmanalgopakege is
--generic ( MessageSize : integer := 80 ) ;
type datasize is array(0 to (51)) of std_logic_vector(0 to 7);
type stringx is array(0 to 51) of string(1 to 100);
type integerx is array(0 to 51) of integer;
type datacount is array(0 to 51) of integer;

type Header is
record
Character : std_logic_vector(0 to 7) ;
HuffmanCode : std_logic_vector(0 to 51) ;
HuffmanCodeSize : integer ;
end record ;
type Head is array(0 to 51) of Header ;
end huffmanalgopakege;
--------//END OF PAKEGE//---------

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
library huffmanalgo;
use huffmanalgo.huffmanalgopakege.all;

entity Huffmanalgorithm is
generic (MessageSize : integer := 51 ) ;
port( Input : in std_logic_vector(0 to 159);
InLimit : in integer ;
InHeader : inout Head ;
InHeaderLimit : inout integer ;
--input table is needed
Mode : in std_logic ; ---MODE=0,COMPRESSION
OutLimit : out integer ; ---MODE=1,DECOMPRESSION
Output: out std_logic_vector(0 to 159);
OutHeader : out Head ;
OutHeaderLimit : out integer);
end Huffmanalgorithm;

architecture algorithm of Huffmanalgorithm is

begin

process(Input,InLimit,Mode)

----//FUNCTION TO CONVERT STRING TO std_logic_vector


variable SymbolsCount,NodeCount,Temp,CodeSize,CodeCount,Limit,Index: integer
;
variable UniqueSymbols: datasize ;
variable TempSymbol : std_logic_vector(0 to 7) ;
variable Message : std_logic_vector(0 to 159) ;
variable SymbolCount,TempCount,NodesCount,HuffmanCodeSize:datacount ;
variable Found:std_logic;
variable Tree,HuffmanCodes:stringx;
variable TempCharacter,Code:string(1 to 100);
variable TempInput : datasize ;
variable TempHeader : Head ;
variable TempCode: std_logic_vector(0 to 51) ;

function StringToStdLogic( a:string;b:integer)


return std_logic_vector is
--only zero and one is converted

variable x:std_logic_vector(0 to b-1);


variable ReturnValue:std_logic_vector(0 to b-1);

begin

for i in 1 to b loop
case a(i) is
when '0'=> x(i-1):='0';
when '1'=> x(i-1):='1';
when others =>null;
end case ;
end loop ;

ReturnValue:=x;

return ReturnValue;
end StringToStdLogic;
-----END OF FUNCTION----------

begin

--------//CHANGE INPUT STREAM TO INPUT TABLE (ARRAY OF CHARACTORS)//-------

for i in 0 to (InLimit-1) loop


TempInput(i):=Input((i*8) to ((i*8)+7)) ;
end loop ;

-------//COUNT THE DIFFERENT SYMBOLS //-------


SymbolsCount := 0 ;

for i in 0 to (InLimit-1) loop


Found := '0';
for j in 0 to (SymbolsCount-1) loop
if(TempInput(i) = UniqueSymbols(j))then
Found := '1' ;
end if;
end loop ;
if(Found = '0')then
SymbolsCount := SymbolsCount + 1;
UniqueSymbols((SymbolsCount-1)) := TempInput(i) ;
end if;
end loop ;
--Setting Counts to 0
for i in 0 to (MessageSize-1) loop --SymbolsCount=NO.OF
UNIQUE CHR.
SymbolCount(i) := 0 ; --SymbolCount=NO.OF EACH
CHR.
end loop;

----------//COUNTING OF EACH SYMBOL// ------

for i in 0 to (SymbolsCount-1) loop


for j in 0 to (InLimit-1) loop
if(UniqueSymbols(i) = TempInput(j))then
SymbolCount(i) := SymbolCount(i) + 1 ;
end if;
end loop ;
end loop ;

---------//CREATING TREE//---------------

for i in 0 to (MessageSize-1) loop


NodesCount(i) := 1;
end loop ;

---------//CHARACTER USED IN TREE//-------

Tree(0)(1 to 1) := "A" ;
Tree(1)(1 to 1) := "B" ;
Tree(2)(1 to 1) := "C" ;
Tree(3)(1 to 1) := "D" ;
Tree(4)(1 to 1) := "E" ;
Tree(5)(1 to 1) := "F" ;
Tree(6)(1 to 1) := "G" ;
Tree(7)(1 to 1) := "H" ;
Tree(8)(1 to 1) := "I" ;
Tree(9)(1 to 1):= "J" ;
Tree(10)(1 to 1) := "K" ;
Tree(11)(1 to 1) := "L" ;
Tree(12)(1 to 1):= "M" ;
Tree(13)(1 to 1) := "N" ;
Tree(14)(1 to 1) := "O" ;
Tree(15)(1 to 1) := "P" ;
Tree(16)(1 to 1):= "Q" ;
Tree(17)(1 to 1) := "R" ;
Tree(18)(1 to 1) := "S" ;
Tree(19)(1 to 1) := "T" ;
Tree(20)(1 to 1) := "U" ;
Tree(21)(1 to 1) := "V" ;
Tree(22)(1 to 1) := "W" ;
Tree(23)(1 to 1) := "X" ;
Tree(24)(1 to 1) := "Y" ;
Tree(25)(1 to 1) := "Z" ;
Tree(26)(1 to 1) := "a" ;
Tree(27)(1 to 1) := "b" ;
Tree(28)(1 to 1) := "c" ;
Tree(29)(1 to 1) := "d" ;
Tree(30)(1 to 1) := "e" ;
Tree(31)(1 to 1) := "f" ;
Tree(32)(1 to 1) := "g" ;
Tree(33)(1 to 1) := "h" ;
Tree(34)(1 to 1) := "i" ;
Tree(35)(1 to 1) := "j" ;
Tree(36)(1 to 1) := "k" ;
Tree(37)(1 to 1) := "l" ;
Tree(38)(1 to 1) := "m" ;
Tree(39)(1 to 1) := "n" ;
Tree(40)(1 to 1) := "o" ;
Tree(41)(1 to 1) := "p" ;
Tree(42)(1 to 1) := "q" ;
Tree(43)(1 to 1) := "r" ;
Tree(44)(1 to 1) := "s" ;
Tree(45)(1 to 1) := "t" ;
Tree(46)(1 to 1) := "u" ;
Tree(47)(1 to 1) := "v" ;
Tree(48)(1 to 1) := "w" ;
Tree(49)(1 to 1) := "x" ;
Tree(50)(1 to 1) := "y" ;
Tree(51)(1 to 1) := "z" ;

--------//THE CHARACTER COUNT IS USED TO FORM THE TREE(NODE)//---


NodeCount := SymbolsCount ; --number of characters
TempCount := SymbolCount ; --count of each characters

while ( NodeCount > 1 ) loop

------SORT THE CHARACTER ACCORDING THEIR COUNT(ASSENDING)//---

for i in 0 to (NodeCount-2) loop


for j in i+1 to (NodeCount-1) loop
if(TempCount(i)>TempCount(j))then

--swapping TempCount
Temp := TempCount(i) ;
TempCount(i) := TempCount(j) ;
TempCount(j) := Temp ;
--swapping Tree character
TempCharacter := Tree(i) ;
Tree(i) := Tree(j) ;
Tree(j) := TempCharacter ;

--swaping nodescount
Temp := NodesCount(i) ;
NodesCount(i) := NodesCount(j) ;
NodesCount(j) := Temp ;

end if;
end loop ;
end loop ;

--merging characters((,),+)----
Temp := NodesCount(0) + NodesCount(1) + 3 ;

Tree(1)(1 to Temp):="(" & Tree(0)(1 to NodesCount(0)) & "+" & Tree(1)(1


to NodesCount(1)) & ")" ;
TempCount(1) := TempCount(0) + TempCount(1) ;
NodesCount(1):= NodesCount(0) + NodesCount(1) + 3 ;
--Rearranging
for i in 0 to NodeCount-1 loop
Tree(i):= Tree(i+1) ;
TempCount(i) := TempCount(i+1) ;
NodesCount(i) := NodesCount(i+1) ;
end loop ;
--decrementing NodeCount
NodeCount := NodeCount - 1 ;
end loop;

---------TREE CREATED----------------

You might also like