minor fixes
[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
38                 result = uTable.compareTo(o.uTable);
39                 if (result != 0) {
40                         return result;
41                 }
42
43                 result = vTable.compareTo(o.vTable);
44                 return result;
45         }
46
47         public void computeLookupTables() {
48                 yTable.computeLookupTables();
49                 uTable.computeLookupTables();
50                 vTable.computeLookupTables();
51         }
52
53         public void initialize() {
54                 yTable.reset();
55                 uTable.reset();
56                 vTable.reset();
57
58                 yTable.addEntry(0, 6, 0);
59                 yTable.addEntry(27, 30, 4);
60                 yTable.addEntry(255, 255, 6);
61
62                 uTable.addEntry(0, 9, 0);
63                 uTable.addEntry(27, 30, 4);
64                 uTable.addEntry(255, 255, 6);
65
66                 vTable.addEntry(0, 9, 0);
67                 vTable.addEntry(27, 30, 4);
68                 vTable.addEntry(255, 255, 6);
69
70                 computeLookupTables();
71         }
72
73         public void load(final BitInputStream inputStream) throws IOException {
74                 yTable.load(inputStream);
75                 uTable.load(inputStream);
76                 vTable.load(inputStream);
77         }
78
79         public void save(final BitOutputStream outputStream) throws IOException {
80                 yTable.save(outputStream);
81                 uTable.save(outputStream);
82                 vTable.save(outputStream);
83         }
84
85 }