0df97798d368573d7d61b9c89a0bfb39b8b7381e
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / OperatingContext.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 class OperatingContext {
12
13     final ColorStats colorStats = new ColorStats();
14     private Image image;
15     private byte[] yMap;
16     private byte[] uMap;
17     private byte[] vMap;
18
19     public OperatingContext() {
20     }
21
22     public int getURange(final int index) {
23         final int colorness = ImageEncoder.byteToInt(uMap[index]);
24         return Math.abs(colorness - colorStats.getAverageU());
25     }
26
27     public int getVRange(final int index) {
28         final int color = ImageEncoder.byteToInt(vMap[index]);
29         return Math.abs(color - colorStats.getAverageV());
30     }
31
32     public int getYRange(final int index) {
33         final int brightness = ImageEncoder.byteToInt(yMap[index]);
34         return Math.abs(brightness - colorStats.getAverageY());
35     }
36
37     public void initialize(final Image image, final byte[] brightnessMap,
38                            final byte[] colornessMap, final byte[] colorMap) {
39         this.image = image;
40         yMap = brightnessMap;
41         uMap = colornessMap;
42         vMap = colorMap;
43
44         colorStats.reset();
45     }
46
47     public void measureNeighborEncode(final int x, final int y) {
48         if ((y >= 0) && (y < image.metaData.height) && (x >= 0)
49                 && (x < image.metaData.width)) {
50
51             final int neighborIndex = (y * image.metaData.width) + x;
52
53             colorStats.ySum = colorStats.ySum
54                     + ImageEncoder.byteToInt(yMap[neighborIndex]);
55             colorStats.uSum = colorStats.uSum
56                     + ImageEncoder.byteToInt(uMap[neighborIndex]);
57             colorStats.vSum = colorStats.vSum
58                     + ImageEncoder.byteToInt(vMap[neighborIndex]);
59             colorStats.pixelCount++;
60         }
61     }
62
63 }