00001 package commandline.flatFileToBinaryPackage;
00002
00003
00004 import java.io.IOException;
00005 import java.util.ArrayList;
00006
00007 import commandline.CodingUtils.ByteConvertor;
00008 import commandline.CodingUtils.CodedFile;
00009 import commandline.CodingUtils.DataStructure;
00010 import commandline.Encoder.Timer;
00011 import commandline.ImageStuff.Image;
00012 import commandline.ImageStuff.GeometricObjects.GenericEdge;
00013 import commandline.NormalMeshAlgorithm.Configuration;
00014 import commandline.NormalMeshAlgorithm.SnijPixelStrategy;
00015 import commandline.debuggingCode.Debug;
00016 import commandline.factories.SnijPixelStrategyBuilder;
00017 import commandline.flatFileToBinaryPackage.HuffMan.Huffman;
00018
00019
00020 public abstract class TreeCompressedCoder extends AbstractFlatToBin{
00021
00022
00023
00024 public static final float PROB_SCALE_FACTOR=1000;
00032 protected Configuration configuration;
00033 public TreeCompressedCoder(Configuration configuration, DataStructure dataStructure,CodedFile treeStucture,MyDataOutPutStream dos) {
00034 super(configuration, dataStructure,treeStucture,dos);
00035 if(!Debug.DISTIBUTION) Debug.print("Starting to convert readeble file to binary file...");
00036 this.configuration=configuration;
00037 }
00038
00039
00043 protected abstract String[] getNormalIndex(GenericEdge e);
00044
00045
00046
00050 protected abstract String[] getVerticalIndex(int verticalOffset,GenericEdge genericEdge);
00051
00055 protected void writeOffsets(){
00056 this.writeVerticalOffsets();
00057 this.writeNormalOffsets();
00058 }
00059
00060 protected abstract String getMethodName();
00061
00062 protected void writeNormalOffsets(){
00063 System.out.println("begin writing normal offsets");
00064 Timer timer = new Timer();
00065 timer.tic();
00066
00067 if(!Debug.DISTIBUTION) Debug.print("* writing normal offsets");
00068 ArrayList<String> normaloffsetbits=new ArrayList<String>();
00069
00070 int winstopNormalOffsets=0;
00071
00072 for(GenericEdge e: super.breathFirstDatastructure.getParentCommitOrderedList()){
00073 String[] normalindex=getNormalIndex(e);
00074 winstopNormalOffsets+=ByteConvertor.nbrBitsForNbrCodeWords(e.getLengte())-normalindex.length;
00075 for(String s: normalindex){
00076 normaloffsetbits.add(s);
00077 }
00078 }
00079
00080
00081 if(!Debug.DISTIBUTION) Debug.print("BITWINST OP DE NORMALE OFFSETS DOOR "+this.getClass().getSimpleName() +" = " + winstopNormalOffsets +" of "+(int)(winstopNormalOffsets/8000f) +"kB");
00082
00083 try{
00084 int numberofnormaloffsetbits=(int) Math.ceil((float) normaloffsetbits.size()/(float) ByteConvertor.BITS_PER_BYTE)*ByteConvertor.BITS_PER_BYTE;
00085 if(!Debug.DISTIBUTION) Debug.print("* writing number of normaloffsets bits= " + numberofnormaloffsetbits);
00086 metaBits+= super.writeMyInt(numberofnormaloffsetbits);
00087 if(!Debug.DISTIBUTION) Debug.print("* writing series of normaloffset bits");
00088 normalOffsetBits= super.writeArrayListOfBitStringsToBytes(normaloffsetbits);
00089 normalOffsetGain=winstopNormalOffsets;}
00090 catch(IOException exception){
00091 exception.printStackTrace(System.out);
00092 }
00093
00094 System.out.println("encoding normal offsets took "+ timer.tac());
00095 System.out.println("end writing normal offsets");
00096
00097 }
00098
00099 protected void writeVerticalOffsets(){
00100 System.out.println("begin writing verticaloffsets");
00101 int[] hist=new int[512];
00102 if(!Debug.DISTIBUTION) Debug.print("* Writing vertical offsets");
00103 ArrayList<String> verticaloffsetbits=new ArrayList<String>(breathFirstDatastructure.getParentCommitOrderedList().size()*ByteConvertor.BITS_PER_VERTICAL_OFFSET);
00104 int winstopVerticalOffsets=0;
00105 SnijPixelStrategy snij=SnijPixelStrategyBuilder.makeSnijPixelStrategy(configuration.getSnijPixelStrategyTYPE(),configuration);
00106 for(GenericEdge e: super.breathFirstDatastructure.getParentCommitOrderedList()){
00107
00108 int verticalOffset=(int)e.getVerticalOffset();
00109
00110
00111 if(e.isFlat() && e.getLengte()>2){
00112
00113 verticalOffset=snij.calculateVerticalOffset(snij.berekenMiddenHoogte(e),Image.getInstance().getPuntOpEnty(e.getMidLoc()).getZ());
00114 }
00115
00116 String[] verticalindex=getVerticalIndex(verticalOffset,e);
00117 hist[verticalOffset+ByteConvertor.MAX_COLOR_VALUE-1]++;
00118 winstopVerticalOffsets+=ByteConvertor.BITS_PER_VERTICAL_OFFSET-verticalindex.length;
00119 for(String s: verticalindex){
00120 verticaloffsetbits.add(s);
00121 }
00122 }
00123
00124 entropy(hist);
00125
00126 if(!Debug.DISTIBUTION) Debug.print("BITWINST OP DE Verticale OFFSETS DOOR "+getMethodName()+" = " + winstopVerticalOffsets +" of "+(int)(winstopVerticalOffsets/8000f) +"kB");
00127 try{
00128 writeMetaData(verticaloffsetbits);
00129 if(!Debug.DISTIBUTION) Debug.print("* writing series of verticaloffset bits");
00130 verticalOffsetBits+= super.writeArrayListOfBitStringsToBytes(verticaloffsetbits);
00131 verticalOffsetGain=winstopVerticalOffsets;
00132 }
00133 catch(IOException exception){
00134 exception.printStackTrace(System.out);
00135 }
00136 System.out.println("end writing vertical offsets");
00137
00138 }
00139
00140
00141
00146 protected void writeMetaData(ArrayList<String> verticaloffsetbits){
00147 int number_of_vertical_offset_bits= verticaloffsetbits.size();
00148 if(!Debug.DISTIBUTION) Debug.print("* writing number of verticaloffsetbits = " + number_of_vertical_offset_bits + " bits");
00149 metaBits+= super.writeMyInt(number_of_vertical_offset_bits);
00150 writeOtherMetaData();
00151 }
00152
00153 protected void entropy(int[] hist){
00154
00155 float sum=0;
00156 for (int i = 0; i < hist.length; i++) {
00157 sum+= hist[i];
00158 }
00159 float entropy=0;
00160 float p=0;
00161 for (int i = 0; i < hist.length; i++) {
00162 p=hist[i]/sum;
00163 if(p!=0)entropy-=p*Math.log10(p)/Math.log10(2);
00164
00165 }
00166 Debug.print("vert. offset entropy:=" + entropy +" | "+ entropy*sum/8f+ " bytes | " +entropy*sum+ " bits");
00167
00168
00169 }
00170
00171 protected abstract void writeOtherMetaData();
00172
00176 protected void writeTesselations(){
00177
00178 double[] count=new double[4];
00179 for(String s: breathFirstDatastructure.getTesselationFile().getCodeWoorden()){
00180 if(s.equals("00")){count[0]++;}
00181 if(s.equals("01")){count[1]++;}
00182 if(s.equals("10")){count[2]++;}
00183 if(s.equals("11")){count[3]++;}
00184 }
00185 double totalcount=count[0]+count[1]+count[2]+count[3];
00186
00187
00188 count[0]=(int)Math.round(count[0]/totalcount*PROB_SCALE_FACTOR);
00189 count[1]=(int)Math.round(count[1]/totalcount*PROB_SCALE_FACTOR);
00190 count[2]=(int)Math.round(count[2]/totalcount*PROB_SCALE_FACTOR);
00191 count[3]=(int)Math.round(count[3]/totalcount*PROB_SCALE_FACTOR);
00192 if(!Debug.DISTIBUTION) Debug.print("* writing the percentage of occurence of symbol 00 = "+ (int)count[0]);
00193 super.writeMyShort((int)count[0]);
00194 if(!Debug.DISTIBUTION) Debug.print("* writing the percentage of occurence of symbol 01 = "+ (int)count[1]);
00195 super.writeMyShort((int)count[1]);
00196 if(!Debug.DISTIBUTION) Debug.print("* writing the percentage of occurence of symbol 10 = "+ (int)count[2]);
00197 super.writeMyShort((int)count[2]);
00198 if(!Debug.DISTIBUTION) Debug.print("* writing the percentage of occurence of symbol 11 = "+ (int)count[3]);
00199 super.writeMyShort((int)count[3]);
00200
00201 count[0]=count[0]/PROB_SCALE_FACTOR;
00202 count[1]=count[1]/PROB_SCALE_FACTOR;
00203 count[2]=count[2]/PROB_SCALE_FACTOR;
00204 count[3]=count[3]/PROB_SCALE_FACTOR;
00205 Huffman h =new Huffman(count);
00206 String code;
00207
00208 for (int i =0;i!=count.length;i++){
00209 code =h.charCode(i);
00210 if(!Debug.DISTIBUTION) Debug.print("de code van de het " +i+"-de woord met kans "+ h.PMF[i]+ " ="+ code);
00211 }
00212
00213 ArrayList<String> string_code_00=ByteConvertor.StringToStringArrayOfBits(h.charCode(0));
00214 ArrayList<String> string_code_01=ByteConvertor.StringToStringArrayOfBits(h.charCode(1));
00215 ArrayList<String> string_code_10=ByteConvertor.StringToStringArrayOfBits(h.charCode(2));
00216 ArrayList<String> string_code_11=ByteConvertor.StringToStringArrayOfBits(h.charCode(3));
00217
00218 ArrayList<String> tesselationbits=new ArrayList<String>();
00219 for(String s: breathFirstDatastructure.getTesselationFile().getCodeWoorden()){
00220 if(s.equals("00")){tesselationbits.addAll(string_code_00);}
00221 if(s.equals("01")){tesselationbits.addAll(string_code_01);}
00222 if(s.equals("10")){tesselationbits.addAll(string_code_10);}
00223 if(s.equals("11")){tesselationbits.addAll(string_code_11);}
00224 if(!s.equals("00")&&!s.equals("01")&&!s.equals("10")&&!s.equals("11")){Debug.print("received code that not existed= "+s); System.exit(-2);}
00225 }
00226
00227 tesselationGain=2*breathFirstDatastructure.getTesselationFile().getCodeWoorden().size()
00228 -tesselationbits.size();
00229 if(!Debug.DISTIBUTION) Debug.print("winst gehaald uit het Huffmanencoderen van de Tesselatie= " + tesselationGain +" byte of " + (int)(tesselationGain/8000) + "kB");
00230
00231 try{
00232 int number_of_tesselationcodewords=breathFirstDatastructure.getTesselationFile().getCodeWoorden().size();
00233 if(!Debug.DISTIBUTION) Debug.print("* writing the number of tesselation codewords " + number_of_tesselationcodewords);
00234 metaBits+= writeMyShort(number_of_tesselationcodewords);
00235 if(!Debug.DISTIBUTION) Debug.print("* writing series of tesselation bits");
00236 tesselationBits+= super.writeArrayListOfBitStringsToBytes(tesselationbits);}
00237 catch(IOException exception){
00238 exception.printStackTrace(System.out);
00239 }
00240
00241 }
00242
00243
00244
00245 }