355cb76962db2d13b9411132ef92bf87c2ffb46a
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Approximator.java
1 /*
2  * Imagesqueeze - Image codec optimized for photos.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.imagesqueeze.codec;
11
12 import eu.svjatoslav.commons.data.BitInputStream;
13 import eu.svjatoslav.commons.data.BitOutputStream;
14
15 import java.io.IOException;
16
17 /**
18  * Since it is a lossy codec, instead of storing exact values, approximated
19  * values are stored to save on bit count.
20  */
21
22 class Approximator {
23
24     public final Table yTable = new Table();
25     public final Table uTable = new Table();
26     public final Table vTable = new Table();
27
28     public Approximator() {
29     }
30
31
32     public void computeLookupTables() {
33         yTable.computeLookupTables();
34         uTable.computeLookupTables();
35         vTable.computeLookupTables();
36     }
37
38     public void initialize() {
39         yTable.reset();
40         uTable.reset();
41         vTable.reset();
42
43         yTable.addEntry(0, 6, 0);
44         yTable.addEntry(27, 30, 4);
45         yTable.addEntry(255, 255, 6);
46
47         uTable.addEntry(0, 9, 0);
48         uTable.addEntry(27, 30, 4);
49         uTable.addEntry(255, 255, 6);
50
51         vTable.addEntry(0, 9, 0);
52         vTable.addEntry(27, 30, 4);
53         vTable.addEntry(255, 255, 6);
54
55         computeLookupTables();
56     }
57
58     public void load(final BitInputStream inputStream) throws IOException {
59         yTable.load(inputStream);
60         uTable.load(inputStream);
61         vTable.load(inputStream);
62     }
63
64     public void save(final BitOutputStream outputStream) throws IOException {
65         yTable.save(outputStream);
66         uTable.save(outputStream);
67         vTable.save(outputStream);
68     }
69
70 }