d162bc0c446c17931d94ab322e0109ed9f1a6b45
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTrackingInfer / EyeDetector.java
1 /*
2  * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
3  * 
4  * This file is part of LEA.
5  * 
6  * LEA is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option) any
9  * later version.
10  * 
11  * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  * 
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LEA. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * No description given.
22  * 
23  * @author Florian Frankenberger
24  */
25
26 class EyeDetector {
27
28   private int width;
29
30   private int height;
31
32   private int[] pixelBuffer;
33
34   double percent;
35
36   public EyeDetector(Image image, Rectangle2D faceRect) {
37
38     percent = 0.15 * faceRect.getWidth();
39     Rectangle2D adjustedFaceRect =
40         new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth()
41             - percent, faceRect.getHeight() - 2 * percent);
42
43     width = (int) adjustedFaceRect.getWidth() / 2;
44     height = (int) adjustedFaceRect.getHeight() / 2;
45     pixelBuffer = new int[width * height];
46
47     int startX = (int) adjustedFaceRect.getX();
48     int startY = (int) adjustedFaceRect.getY();
49
50     for (int y = 0; y < height; y++) {
51       for (int x = 0; x < width; x++) {
52         pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY);
53       }
54     }
55
56   }
57
58   public Point detectEye() {
59     Point eyePosition = null;
60     float brightness = 255f;
61     for (int y = 0; y < height; ++y) {
62       for (int x = 0; x < width; ++x) {
63         int position = y * width + x;
64         int[] color = new int[] { (pixelBuffer[position] & 0xFF0000) >> 16, (pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF };
65         // System.out.println("("+x+","+y+")="+color[0]+" "+color[1]+" "+color[2]);
66         float acBrightness = getBrightness(color);
67
68         if (acBrightness < brightness) {
69           eyePosition = new Point(x + (int) percent, y + (int) percent);
70           brightness = acBrightness;
71         }
72       }
73     }
74
75     return eyePosition;
76   }
77
78   private static float getBrightness(int[] color) {
79     int min = Math.min(Math.min(color[0], color[1]), color[2]);
80     int max = Math.max(Math.max(color[0], color[1]), color[2]);
81
82     return 0.5f * (max + min);
83   }
84 }