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