00001 package Decoder;
00002
00003 import java.io.IOException;
00004 import java.nio.charset.CoderResult;
00005 import java.util.ArrayList;
00006 import java.util.PriorityQueue;
00007
00008 import debuggingCode.Debug;
00009
00010 import junit.framework.Assert;
00011
00012 import flatFileToBinaryPackage.LogDecayNormalIndexFlatToBin;
00013
00014 import CodingUtils.ByteConvertor;
00015 import CodingUtils.CodedFile;
00016 import CodingUtils.DataStructure;
00017 import Decoder.iterators.NormalOffsetIterator;
00018 import Decoder.iterators.TesselationIterator;
00019 import Decoder.iterators.TreeStructureIterator;
00020 import Decoder.iterators.VerticalOffsetIterator;
00021 import Encoder.SnijPixelPiercingWaardeStrategy;
00022 import HuffMan.CodeRetriever;
00023 import HuffMan.Huffman;
00024 import HuffMan.NotEnoughBytesException;
00025 import ImageStuff.Vertex;
00026 import NormalMeshAlgorithm.Configuration;
00027 import TxTPackage.DecodedFileWithRealValues;
00028
00029 public class BinaryFeed {
00030
00031 MyDataInPutStream dis;
00032 TreeStructureIterator treeStructureIterator;
00033 TesselationIterator tesselationIterator;
00034 VerticalOffsetIterator verticalOffsetIterator;
00035 NormalOffsetIterator normalOffsetIterator;
00036 DecoderMesh decoderMesh;
00037 Configuration configuration;
00038 public BinaryFeed(MyDataInPutStream dis,Configuration configuration) {
00039 this.dis=dis;
00040 this.configuration=configuration;
00041 read();
00042 }
00043
00044 protected void read(){
00045
00046 this.readBaseMesh();
00047 this.treeStructureIterator= this.readTreeStructure();
00048 this.tesselationIterator= this.readTesselations();
00049 this.verticalOffsetIterator= this.readVerticalOffsets();
00050 this.normalOffsetIterator=new NormalOffsetIterator(dis);
00051 }
00052
00053 protected void readBaseMesh(){
00054 int number_of_basemesh_vertices=dis.readMyShort();
00055 Assert.assertTrue(number_of_basemesh_vertices>=0);
00056 Debug.print("** reading number of basemeshvertices " + number_of_basemesh_vertices);
00057 Vertex[] basemeshVertices=new Vertex[number_of_basemesh_vertices];
00058 for(int i =0;i!=basemeshVertices.length;i++){
00059 basemeshVertices[i]=new Vertex(dis.readMyShort(),dis.readMyShort(),dis.readMyShort(),0.0f);
00060 }
00061
00062 if(Debug.DEBUG_DECODER_FILE){
00063 DecodedFileWithRealValues.getStructuredFile().schrijf(""+number_of_basemesh_vertices);
00064 DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00065 for(Vertex v:basemeshVertices){
00066 DecodedFileWithRealValues.getStructuredFile().schrijf(""+v.getX()+ " ");
00067 DecodedFileWithRealValues.getStructuredFile().schrijf(""+v.getY()+ " ");
00068 DecodedFileWithRealValues.getStructuredFile().schrijf(""+v.getZ());
00069 DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00070 }
00071 }
00072
00073 Assert.assertEquals(4,number_of_basemesh_vertices);
00074 if(number_of_basemesh_vertices==4){
00075 decoderMesh= new DecoderMesh(new SnijPixelPiercingWaardeStrategy(configuration), basemeshVertices[0],
00076 basemeshVertices[1], basemeshVertices[2],basemeshVertices[3],configuration);
00077 }
00078 }
00079
00080 protected TreeStructureIterator readTreeStructure(){
00081 int nbr_leaves=dis.readMyShort();
00082 Assert.assertTrue(nbr_leaves>=0);
00083 Debug.print("reading the number of leaves " +nbr_leaves);
00084 if(Debug.DEBUG_DECODER_FILE){
00085 DecodedFileWithRealValues.getStructuredFile().schrijf(""+nbr_leaves);
00086 DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00087 }
00088
00089
00090 int[] bytes=new int[(int)Math.ceil(nbr_leaves/8f) ];
00091 try{
00092 dis.readMyBytes(bytes);}
00093 catch(IOException exception){
00094 exception.printStackTrace(System.out);
00095 }
00096 ArrayList<String> treelist= ByteConvertor.getStringArrayListofBitsFor(bytes);
00097 if(Debug.DEBUG_DECODER_FILE){for(String bit:treelist){DecodedFileWithRealValues.getStructuredFile().schrijf(bit);DecodedFileWithRealValues.getStructuredFile().nieuweLijn();}}
00098 return new TreeStructureIterator(treelist);
00099 }
00100
00101 protected TesselationIterator readTesselations(){
00102
00103
00104 Debug.print("reading the percentage of occurence of symbol 00");
00105 int occurence_00=dis.readMyShort();
00106 Debug.print("reading the percentage of occurence of symbol 01");
00107 int occurence_01=dis.readMyShort();
00108 Debug.print("reading the percentage of occurence of symbol 10");
00109 int occurence_10=dis.readMyShort();
00110 Debug.print("reading the percentage of occurence of symbol 11");
00111 int occurence_11=dis.readMyShort();
00112
00113 float PROB_SCALE_FACTOR=LogDecayNormalIndexFlatToBin.PROB_SCALE_FACTOR;
00114 float[] count=new float[]{occurence_00/PROB_SCALE_FACTOR,occurence_01/PROB_SCALE_FACTOR,occurence_10/PROB_SCALE_FACTOR,occurence_11/PROB_SCALE_FACTOR};
00115 Huffman huffman=new Huffman(count);
00116 huffman.compress();
00117 String[] originalcodewords=new String[]{"00","01","10","11"};
00118 ArrayList<String> huffman_codewords=new ArrayList<String>();
00119 for(int i=0;i!=count.length;i++){
00120 huffman_codewords.add(huffman.charCode(i));
00121 }
00122
00123 int nbr_tesselations=dis.readMyShort();
00124 Debug.print("reading the number of tesselations " +nbr_tesselations);
00125 if(Debug.DEBUG_DECODER_FILE){
00126 DecodedFileWithRealValues.getStructuredFile().schrijf(""+nbr_tesselations); DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00127 }
00128 ArrayList<String> initialbitList=null;
00129 ArrayList<String> originalCodeWords=new ArrayList<String>(nbr_tesselations);
00130 try{
00131 initialbitList=ByteConvertor.getStringArrayListofBitsFor(dis.readMyByte(),8);
00132 }
00133 catch(IOException exception){exception.printStackTrace(System.out);}
00134 for(int _tes=0;_tes!=nbr_tesselations;_tes++){
00135 int pos=getPositionOfNextCodeWord(initialbitList,huffman_codewords);
00136 Debug.print("originalcodeword = "+originalcodewords[pos]);
00137 if(Debug.DEBUG_DECODER_FILE){DecodedFileWithRealValues.getStructuredFile().schrijf(originalcodewords[pos]+"");DecodedFileWithRealValues.getStructuredFile().nieuweLijn();}
00138 originalCodeWords.add(originalcodewords[pos]);
00139 }
00140
00141 return new TesselationIterator(originalCodeWords);
00142 }
00143
00144 protected VerticalOffsetIterator readVerticalOffsets(){
00145 int nbr_verticaloffsets=dis.readMyShort();
00146 Debug.print("nbr_verticaloffsets= "+nbr_verticaloffsets);
00147 Assert.assertTrue(nbr_verticaloffsets>=0);
00148 if(Debug.DEBUG_DECODER_FILE){
00149 DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00150 DecodedFileWithRealValues.getStructuredFile().schrijf(""+nbr_verticaloffsets);
00151 DecodedFileWithRealValues.getStructuredFile().nieuweLijn();
00152 }
00153
00154 int BITS_PER_VERTICAL_OFFSET=ByteConvertor.BITS_PER_VERTICAL_OFFSET;
00155 int[] bytes=new int[(int)Math.ceil(nbr_verticaloffsets*(float)BITS_PER_VERTICAL_OFFSET/(float)ByteConvertor.BITS_PER_BYTE)];
00156 try{
00157 dis.readMyBytes(bytes);}
00158 catch(IOException exception){
00159 exception.printStackTrace();}
00160
00161
00162 ArrayList<String> bitlist= ByteConvertor.getStringArrayListofBitsFor(bytes);
00163 Assert.assertEquals(bytes.length*ByteConvertor.BITS_PER_BYTE,bitlist.size());
00164
00165 ArrayList<Integer> verticalOffsetArrayList=new ArrayList<Integer>(nbr_verticaloffsets);
00166 for(int i =0;i!=nbr_verticaloffsets;i++){
00167 String s="";
00168 for(int k =0;k!=BITS_PER_VERTICAL_OFFSET;k++){
00169 s=s.concat(bitlist.remove(0));
00170 }
00171 Assert.assertEquals(BITS_PER_VERTICAL_OFFSET,s.length());
00172 int verticaloffset=ByteConvertor.getIntegerfor(s.toCharArray())-ByteConvertor.MAX_COLOR_VALUE;
00173 verticalOffsetArrayList.add(new Integer(verticaloffset));
00174
00175 Debug.print("vo= " + verticaloffset);
00176 }
00177
00178 return new VerticalOffsetIterator(verticalOffsetArrayList);
00179 }
00180
00181
00182
00183
00184
00192 protected int getPositionOfNextCodeWord(ArrayList<String> previouscodelist,ArrayList<String> codewords){
00193 int pos=-1;
00194 try{
00195 pos=CodeRetriever.getAndRemoveOriginal(previouscodelist,codewords);
00196 }catch(NotEnoughBytesException bytesException){
00197 int b=-1;
00198 try{b=dis.readMyByte();}catch(IOException exception){exception.printStackTrace(System.out);}
00199 Assert.assertTrue(b!=-1);
00200 ArrayList<String> bits= ByteConvertor.getStringArrayListofBitsFor(b,8);
00201 previouscodelist.addAll(bits);
00202 return getPositionOfNextCodeWord(previouscodelist,codewords);
00203 }
00204
00205 Assert.assertTrue(pos!=-1);
00206 return pos;
00207 }
00208
00209 public TreeStructureIterator getTreeStructureIterator() {
00210 return treeStructureIterator;
00211 }
00212
00213 public TesselationIterator getTesselationIterator() {
00214 return tesselationIterator;
00215 }
00216
00217 public VerticalOffsetIterator getVerticalOffsetIterator() {
00218 return verticalOffsetIterator;
00219 }
00220
00221 public NormalOffsetIterator getNormalOffsetIterator() {
00222 return normalOffsetIterator;
00223 }
00224
00225 public DecoderMesh getDecoderMesh() {
00226 return decoderMesh;
00227 }
00228
00229 }