more changes.
[IRC.git] / Robust / src / Benchmarks / SSJava / EyeTracking / 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 class EyeDetector {
26
27   private int width;
28   private int height;
29   private int[] pixelBuffer;
30
31   double percent;
32
33   public EyeDetector(Image image, Rectangle2D faceRect) {
34
35     percent = 0.15 * faceRect.getWidth();
36     faceRect =
37         new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth()
38             - percent, faceRect.getHeight() - 2 * percent);
39
40     width = (int) faceRect.getWidth() / 2;
41     height = (int) faceRect.getHeight() / 2;
42     pixelBuffer = new int[width * height];
43
44     // System.out.println("eye w=" + width + " h=" + height);
45     // System.out
46     // .println("faceRect.getX()=" + faceRect.getX() + " faceRect.getY()=" +
47     // faceRect.getY());
48     // System.out.println("image w=" + image.getWidth() + " h=" +
49     // image.getHeight());
50
51     int startX = (int) faceRect.getX();
52     int startY = (int) faceRect.getY();
53
54     for (int y = 0; y < height; y++) {
55       for (int x = 0; x < width; x++) {
56         pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY);
57       }
58     }
59
60   }
61
62   public Point detectEye() {
63     Point eyePosition = null;
64     float brightness = 255f;
65     System.out.println("detectEye=" + percent);
66     for (int y = 0; y < height; ++y) {
67       for (int x = 0; x < width; ++x) {
68         final int position = y * width + x;
69         final int[] color =
70             new int[] { (pixelBuffer[position] & 0xFF0000) >> 16,
71                 (pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF };
72         final float acBrightness = getBrightness(color);
73         System.out.println("p=" + pixelBuffer[position] + " acBrightness=" + acBrightness);
74
75         if (acBrightness < brightness) {
76           eyePosition = new Point(x + (int) percent, y + (int) percent);
77           brightness = acBrightness;
78         }
79       }
80     }
81
82     System.out.println("eyePosition=" + eyePosition);
83     System.exit(0);
84
85     return eyePosition;
86   }
87
88   private static float getBrightness(int[] color) {
89     int min = Math.min(Math.min(color[0], color[1]), color[2]);
90     int max = Math.max(Math.max(color[0], color[1]), color[2]);
91
92     return 0.5f * (max + min);
93   }
94 }