a4f78af536609df43c6df329ad856e1b0441cbfe
[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 java.io.IOException;
13
14 import eu.svjatoslav.commons.data.BitInputStream;
15 import eu.svjatoslav.commons.data.BitOutputStream;
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 public class Approximator implements Comparable<Approximator> {
23
24         public Table yTable = new Table();
25         public Table uTable = new Table();
26         public Table vTable = new Table();
27
28         public Approximator() {
29         }
30
31         @Override
32         public int compareTo(final Approximator o) {
33                 int result = yTable.compareTo(o.yTable);
34                 if (result != 0)
35                         return result;
36
37                 result = uTable.compareTo(o.uTable);
38                 if (result != 0)
39                         return result;
40
41                 result = vTable.compareTo(o.vTable);
42                 return result;
43         }
44
45         public void computeLookupTables() {
46                 yTable.computeLookupTables();
47                 uTable.computeLookupTables();
48                 vTable.computeLookupTables();
49         }
50
51         public void initialize() {
52                 yTable.reset();
53                 uTable.reset();
54                 vTable.reset();
55
56                 yTable.addEntry(0, 6, 0);
57                 yTable.addEntry(27, 30, 4);
58                 yTable.addEntry(255, 255, 6);
59
60                 uTable.addEntry(0, 9, 0);
61                 uTable.addEntry(27, 30, 4);
62                 uTable.addEntry(255, 255, 6);
63
64                 vTable.addEntry(0, 9, 0);
65                 vTable.addEntry(27, 30, 4);
66                 vTable.addEntry(255, 255, 6);
67
68                 computeLookupTables();
69         }
70
71         public void load(final BitInputStream inputStream) throws IOException {
72                 yTable.load(inputStream);
73                 uTable.load(inputStream);
74                 vTable.load(inputStream);
75         }
76
77         public void save(final BitOutputStream outputStream) throws IOException {
78                 yTable.save(outputStream);
79                 uTable.save(outputStream);
80                 vTable.save(outputStream);
81         }
82
83 }