changes: now Inference engine works fine with the EyeTracking benchmark.
[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 @LATTICE("IMG")
26 @METHODDEFAULT("OUT<THIS,THIS<IN,OUT*,THISLOC=THIS,RETURNLOC=OUT,GLOBALLOC=THIS")
27 class EyeDetector {
28
29   @LOC("IMG")
30   private int width;
31   @LOC("IMG")
32   private int height;
33   @LOC("IMG")
34   private int[] pixelBuffer;
35   @LOC("IMG")
36   double percent;
37
38   // public EyeDetector(Image image, Rectangle2D faceRect) {
39   public EyeDetector(Image image, double fx, double fy, double fwidth, double fheight) {
40
41     percent = 0.15 * fwidth;
42     Rectangle2D adjustedFaceRect =
43         new Rectangle2D(fx + percent, fy + percent, fwidth - percent, fheight - 2 * percent);
44     // percent = 0.15 * faceRect.getWidth();
45     // Rectangle2D adjustedFaceRect =
46     // new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth()
47     // - percent, faceRect.getHeight() - 2 * percent);
48
49     width = (int) adjustedFaceRect.getWidth() / 2;
50     height = (int) adjustedFaceRect.getHeight() / 2;
51     pixelBuffer = new int[width * height];
52
53     int startX = (int) adjustedFaceRect.getX();
54     int startY = (int) adjustedFaceRect.getY();
55
56     for (int y = 0; y < height; y++) {
57       for (int x = 0; x < width; x++) {
58         pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY);
59       }
60     }
61
62   }
63
64   @LATTICE("OUT<V,V<C,C<THIS,C*,V*,THISLOC=THIS,RETURNLOC=OUT,GLOBALLOC=THIS")
65   public Point detectEye() {
66     @LOC("OUT") Point eyePosition = null;
67     @LOC("V") float brightness = 255f;
68     for (@LOC("C") int y = 0; y < height; ++y) {
69       for (@LOC("C") int x = 0; x < width; ++x) {
70         @LOC("V") final int position = y * width + x;
71         @LOC("V") final int[] color =
72             new int[] { (pixelBuffer[position] & 0xFF0000) >> 16,
73                 (pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF };
74         // System.out.println("("+x+","+y+")="+color[0]+" "+color[1]+" "+color[2]);
75         @LOC("V") final float acBrightness = getBrightness(color);
76
77         if (acBrightness < brightness) {
78           eyePosition = new Point(x + (int) percent, y + (int) percent);
79           brightness = acBrightness;
80         }
81       }
82     }
83
84     return eyePosition;
85   }
86
87   @LATTICE("OUT<V,V<G,G<IN,G<THIS,THISLOC=THIS,GLOBALLOC=G,RETURNLOC=OUT")
88   private static float getBrightness(@LOC("IN") int[] color) {
89     @LOC("V") int min = Math.min(Math.min(color[0], color[1]), color[2]);
90     @LOC("V") int max = Math.max(Math.max(color[0], color[1]), color[2]);
91
92     return 0.5f * (max + min);
93   }
94 }