Changed license to CC0
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Approximator.java
1 /*
2  * Image codec. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.imagesqueeze.codec;
6
7 import eu.svjatoslav.commons.data.BitInputStream;
8 import eu.svjatoslav.commons.data.BitOutputStream;
9
10 import java.io.IOException;
11
12 /**
13  * Since it is a lossy codec, instead of storing exact values, approximated
14  * values are stored to save on bit count.
15  */
16
17 class Approximator {
18
19     public final Table yTable = new Table();
20     public final Table uTable = new Table();
21     public final Table vTable = new Table();
22
23     public Approximator() {
24     }
25
26
27     public void computeLookupTables() {
28         yTable.computeLookupTables();
29         uTable.computeLookupTables();
30         vTable.computeLookupTables();
31     }
32
33     public void initialize() {
34         yTable.reset();
35         uTable.reset();
36         vTable.reset();
37
38         yTable.addEntry(0, 6, 0);
39         yTable.addEntry(27, 30, 4);
40         yTable.addEntry(255, 255, 6);
41
42         uTable.addEntry(0, 9, 0);
43         uTable.addEntry(27, 30, 4);
44         uTable.addEntry(255, 255, 6);
45
46         vTable.addEntry(0, 9, 0);
47         vTable.addEntry(27, 30, 4);
48         vTable.addEntry(255, 255, 6);
49
50         computeLookupTables();
51     }
52
53     public void load(final BitInputStream inputStream) throws IOException {
54         yTable.load(inputStream);
55         uTable.load(inputStream);
56         vTable.load(inputStream);
57     }
58
59     public void save(final BitOutputStream outputStream) throws IOException {
60         yTable.save(outputStream);
61         uTable.save(outputStream);
62         vTable.save(outputStream);
63     }
64
65 }