Moved bit input and output streams into Svjatoslav Commons library.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / ImageDecoder.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 /**
13  * Compressed image pixels decoder.
14  */
15
16 import java.awt.image.DataBufferByte;
17 import java.awt.image.WritableRaster;
18 import java.io.IOException;
19
20 import eu.svjatoslav.commons.data.BitInputStream;
21
22 public class ImageDecoder {
23
24         int width, height;
25         Image image;
26
27         byte[] decodedYRangeMap;
28         byte[] decodedYMap;
29
30         byte[] decodedURangeMap;
31         byte[] decodedUMap;
32
33         byte[] decodedVRangeMap;
34         byte[] decodedVMap;
35
36         Color tmpColor = new Color();
37
38         Approximator approximator;
39         BitInputStream bitInputStream;
40
41         ColorStats colorStats = new ColorStats();
42         OperatingContext context = new OperatingContext();
43
44         public ImageDecoder(final Image image, final BitInputStream bitInputStream) {
45                 approximator = new Approximator();
46
47                 this.image = image;
48                 this.bitInputStream = bitInputStream;
49
50                 width = image.metaData.width;
51                 height = image.metaData.height;
52
53                 decodedYRangeMap = new byte[width * height];
54                 decodedYRangeMap[0] = (byte) (255);
55                 decodedYMap = new byte[width * height];
56
57                 decodedURangeMap = new byte[width * height];
58                 decodedURangeMap[0] = (byte) (255);
59                 decodedUMap = new byte[width * height];
60
61                 decodedVRangeMap = new byte[width * height];
62                 decodedVRangeMap[0] = (byte) (255);
63                 decodedVMap = new byte[width * height];
64
65         }
66
67         public void decode() throws IOException {
68                 approximator.load(bitInputStream);
69                 approximator.computeLookupTables();
70
71                 final WritableRaster raster = image.bufferedImage.getRaster();
72                 final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
73                 final byte[] pixels = dbi.getData();
74
75                 // load top-, left-most pixel.
76                 decodedYMap[0] = (byte) bitInputStream.readBits(8);
77                 decodedUMap[0] = (byte) bitInputStream.readBits(8);
78                 decodedVMap[0] = (byte) bitInputStream.readBits(8);
79
80                 final Color color = new Color();
81                 color.y = ImageEncoder.byteToInt(decodedYMap[0]);
82                 color.u = ImageEncoder.byteToInt(decodedUMap[0]);
83                 color.v = ImageEncoder.byteToInt(decodedVMap[0]);
84
85                 color.YUV2RGB();
86
87                 pixels[0] = (byte) color.r;
88                 pixels[0 + 1] = (byte) color.g;
89                 pixels[0 + 2] = (byte) color.b;
90
91                 // detect initial step
92                 int largestDimension;
93                 int initialStep = 2;
94                 if (width > height) {
95                         largestDimension = width;
96                 } else {
97                         largestDimension = height;
98                 }
99
100                 while (initialStep < largestDimension) {
101                         initialStep = initialStep * 2;
102                 }
103
104                 grid(initialStep, pixels);
105         }
106
107         public void grid(final int step, final byte[] pixels) throws IOException {
108
109                 gridDiagonal(step / 2, step / 2, step, pixels);
110                 gridSquare(step / 2, 0, step, pixels);
111                 gridSquare(0, step / 2, step, pixels);
112
113                 if (step > 2)
114                         grid(step / 2, pixels);
115         }
116
117         public void gridDiagonal(final int offsetX, final int offsetY,
118                         final int step, final byte[] pixels) throws IOException {
119
120                 for (int y = offsetY; y < height; y = y + step) {
121                         for (int x = offsetX; x < width; x = x + step) {
122
123                                 final int halfStep = step / 2;
124
125                                 context.initialize(image, decodedYMap, decodedUMap, decodedVMap);
126                                 context.measureNeighborEncode(x - halfStep, y - halfStep);
127                                 context.measureNeighborEncode(x + halfStep, y - halfStep);
128                                 context.measureNeighborEncode(x - halfStep, y + halfStep);
129                                 context.measureNeighborEncode(x + halfStep, y + halfStep);
130
131                                 loadPixel(step, offsetX, offsetY, x, y, pixels,
132                                                 context.colorStats.getAverageY(),
133                                                 context.colorStats.getAverageU(),
134                                                 context.colorStats.getAverageV());
135
136                         }
137                 }
138         }
139
140         public void gridSquare(final int offsetX, final int offsetY,
141                         final int step, final byte[] pixels) throws IOException {
142
143                 for (int y = offsetY; y < height; y = y + step) {
144                         for (int x = offsetX; x < width; x = x + step) {
145
146                                 final int halfStep = step / 2;
147
148                                 context.initialize(image, decodedYMap, decodedUMap, decodedVMap);
149                                 context.measureNeighborEncode(x - halfStep, y);
150                                 context.measureNeighborEncode(x + halfStep, y);
151                                 context.measureNeighborEncode(x, y - halfStep);
152                                 context.measureNeighborEncode(x, y + halfStep);
153
154                                 loadPixel(step, offsetX, offsetY, x, y, pixels,
155                                                 context.colorStats.getAverageY(),
156                                                 context.colorStats.getAverageU(),
157                                                 context.colorStats.getAverageV());
158
159                         }
160                 }
161         }
162
163         private int loadChannel(final byte[] decodedRangeMap,
164                         final byte[] decodedMap, final Table table,
165                         final int averageDecodedValue, final int index,
166                         final int parentIndex) throws IOException {
167                 int decodedValue = averageDecodedValue;
168
169                 final int inheritedRange = ImageEncoder
170                                 .byteToInt(decodedRangeMap[parentIndex]);
171                 int computedRange = inheritedRange;
172
173                 final int bitCount = table.proposeBitcountForRange(inheritedRange);
174                 int computedRangeBitCount = 0;
175                 if (bitCount > 0) {
176
177                         final int rangeDecreases = bitInputStream.readBits(1);
178                         if (rangeDecreases != 0) {
179                                 computedRange = table.proposeDecreasedRange(inheritedRange);
180                         }
181
182                         decodedRangeMap[index] = (byte) computedRange;
183                         computedRangeBitCount = table
184                                         .proposeBitcountForRange(computedRange);
185
186                         if (computedRangeBitCount > 0) {
187
188                                 final int encodedDifference = bitInputStream
189                                                 .readBits(computedRangeBitCount);
190
191                                 final int decodedDifference = ImageEncoder
192                                                 .decodeValueFromGivenBits(encodedDifference,
193                                                                 computedRange, computedRangeBitCount);
194
195                                 decodedValue = averageDecodedValue - decodedDifference;
196                                 if (decodedValue > 255)
197                                         decodedValue = 255;
198                                 if (decodedValue < 0)
199                                         decodedValue = 0;
200                         }
201                 } else {
202                         decodedRangeMap[index] = (byte) inheritedRange;
203                 }
204                 decodedMap[index] = (byte) decodedValue;
205                 return decodedValue;
206         }
207
208         public void loadPixel(final int step, final int offsetX, final int offsetY,
209                         final int x, final int y, final byte[] pixels,
210                         final int averageDecodedY, final int averageDecodedU,
211                         final int averageDecodedV) throws IOException {
212
213                 final int index = (y * width) + x;
214
215                 final int halfStep = step / 2;
216
217                 int parentIndex;
218                 if (offsetX > 0) {
219                         if (offsetY > 0) {
220                                 // diagonal approach
221                                 parentIndex = ((y - halfStep) * width) + (x - halfStep);
222                         } else {
223                                 // take left pixel
224                                 parentIndex = (y * width) + (x - halfStep);
225                         }
226                 } else {
227                         // take upper pixel
228                         parentIndex = ((y - halfStep) * width) + x;
229                 }
230
231                 final int colorBufferIndex = index * 3;
232
233                 final Color color = new Color();
234                 color.y = loadChannel(decodedYRangeMap, decodedYMap,
235                                 approximator.yTable, averageDecodedY, index, parentIndex);
236                 color.u = loadChannel(decodedURangeMap, decodedUMap,
237                                 approximator.uTable, averageDecodedU, index, parentIndex);
238                 color.v = loadChannel(decodedVRangeMap, decodedVMap,
239                                 approximator.vTable, averageDecodedV, index, parentIndex);
240
241                 color.YUV2RGB();
242
243                 pixels[colorBufferIndex] = (byte) color.r;
244                 pixels[colorBufferIndex + 1] = (byte) color.g;
245                 pixels[colorBufferIndex + 2] = (byte) color.b;
246
247         }
248
249 }