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