initial commit
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Approximator.java
1 package eu.svjatoslav.imagesqueeze.codec;
2
3 import java.io.IOException;
4
5 /**
6  * Since it is a lossy codec, instead of storing
7  * exact values, approximated values are stored
8  * to save on bit count.
9  */
10
11 public class Approximator implements Comparable<Approximator> {
12
13         public Table yTable = new Table();
14         public Table uTable = new Table();
15         public Table vTable = new Table();
16
17         public Approximator(){
18         }
19
20         public int compareTo(Approximator o) {
21                 int result = yTable.compareTo(o.yTable);
22                 if (result != 0) return result;
23
24                 result = uTable.compareTo(o.uTable);
25                 if (result != 0) return result;
26
27                 result = vTable.compareTo(o.vTable);
28                 return result;
29         }
30         
31         public void initialize(){
32                 yTable.reset();
33                 uTable.reset();
34                 vTable.reset();
35                 
36                 yTable.addEntry(0, 6, 0);
37                 yTable.addEntry(27, 30, 4);
38                 yTable.addEntry(255, 255, 6);
39                 
40                 uTable.addEntry(0, 9, 0);
41                 uTable.addEntry(27, 30, 4);
42                 uTable.addEntry(255, 255, 6);
43
44                 vTable.addEntry(0, 9, 0);
45                 vTable.addEntry(27, 30, 4);
46                 vTable.addEntry(255, 255, 6);
47                 
48                 computeLookupTables();
49         }
50         
51         public void save(BitOutputStream outputStream) throws IOException{
52                 yTable.save(outputStream);
53                 uTable.save(outputStream);
54                 vTable.save(outputStream);              
55         }
56         
57         public void load(BitInputStream inputStream) throws IOException {
58                 yTable.load(inputStream);
59                 uTable.load(inputStream);
60                 vTable.load(inputStream);                               
61         }
62
63         public void computeLookupTables(){
64                 yTable.computeLookupTables();
65                 uTable.computeLookupTables();
66                 vTable.computeLookupTables();           
67         }
68         
69 }