b79e1e63c7cc0ee4b9af48c7d0198e0c6e221879
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / OperatingContext.java
1 /*
2  * Image codec. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.imagesqueeze.codec;
6
7 class OperatingContext {
8
9     final ColorStats colorStats = new ColorStats();
10     private Image image;
11     private byte[] yMap;
12     private byte[] uMap;
13     private byte[] vMap;
14
15     public OperatingContext() {
16     }
17
18     public int getURange(final int index) {
19         final int colorness = ImageEncoder.byteToInt(uMap[index]);
20         return Math.abs(colorness - colorStats.getAverageU());
21     }
22
23     public int getVRange(final int index) {
24         final int color = ImageEncoder.byteToInt(vMap[index]);
25         return Math.abs(color - colorStats.getAverageV());
26     }
27
28     public int getYRange(final int index) {
29         final int brightness = ImageEncoder.byteToInt(yMap[index]);
30         return Math.abs(brightness - colorStats.getAverageY());
31     }
32
33     public void initialize(final Image image, final byte[] brightnessMap,
34                            final byte[] colornessMap, final byte[] colorMap) {
35         this.image = image;
36         yMap = brightnessMap;
37         uMap = colornessMap;
38         vMap = colorMap;
39
40         colorStats.reset();
41     }
42
43     public void measureNeighborEncode(final int x, final int y) {
44         if ((y >= 0) && (y < image.metaData.height) && (x >= 0)
45                 && (x < image.metaData.width)) {
46
47             final int neighborIndex = (y * image.metaData.width) + x;
48
49             colorStats.ySum = colorStats.ySum
50                     + ImageEncoder.byteToInt(yMap[neighborIndex]);
51             colorStats.uSum = colorStats.uSum
52                     + ImageEncoder.byteToInt(uMap[neighborIndex]);
53             colorStats.vSum = colorStats.vSum
54                     + ImageEncoder.byteToInt(vMap[neighborIndex]);
55             colorStats.pixelCount++;
56         }
57     }
58
59 }