1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * The Scene class implements a simple physical simulation of a scene, using the
19  * CIE 1931 colorspace to represent light in physical units (lux).
20  *
21  * It's fairly approximate, but does provide a scene with realistic widely
22  * variable illumination levels and colors over time.
23  *
24  */
25 
26 #ifndef HW_EMULATOR_CAMERA2_SCENE_H
27 #define HW_EMULATOR_CAMERA2_SCENE_H
28 
29 #include "utils/Timers.h"
30 
31 namespace android {
32 
33 class Scene {
34   public:
35     Scene(int sensorWidthPx,
36             int sensorHeightPx,
37             float sensorSensitivity);
38     ~Scene();
39 
40     // Set the filter coefficients for the red, green, and blue filters on the
41     // sensor. Used as an optimization to pre-calculate various illuminance
42     // values. Two different green filters can be provided, to account for
43     // possible cross-talk on a Bayer sensor. Must be called before
44     // calculateScene.
45     void setColorFilterXYZ(
46         float rX, float rY, float rZ,
47         float grX, float grY, float grZ,
48         float gbX, float gbY, float gbZ,
49         float bX, float bY, float bZ);
50 
51     // Set time of day (24-hour clock). This controls the general light levels
52     // in the scene. Must be called before calculateScene
53     void setHour(int hour);
54     // Get current hour
55     int getHour();
56 
57     // Set the duration of exposure for determining luminous exposure.
58     // Must be called before calculateScene
59     void setExposureDuration(float seconds);
60 
61     // Calculate scene information for current hour and the time offset since
62     // the hour. Must be called at least once before calling getLuminousExposure.
63     // Resets pixel readout location to 0,0
64     void calculateScene(nsecs_t time);
65 
66     // Set sensor pixel readout location.
67     void setReadoutPixel(int x, int y);
68 
69     // Get sensor response in physical units (electrons) for light hitting the
70     // current readout pixel, after passing through color filters. The readout
71     // pixel will be auto-incremented. The returned array can be indexed with
72     // ColorChannels.
73     const uint32_t* getPixelElectrons();
74 
75     enum ColorChannels {
76         R = 0,
77         Gr,
78         Gb,
79         B,
80         Y,
81         Cb,
82         Cr,
83         NUM_CHANNELS
84     };
85 
86   private:
87     // Sensor color filtering coefficients in XYZ
88     float mFilterR[3];
89     float mFilterGr[3];
90     float mFilterGb[3];
91     float mFilterB[3];
92 
93     int mOffsetX, mOffsetY;
94     int mMapDiv;
95 
96     int mHandshakeX, mHandshakeY;
97 
98     int mSensorWidth;
99     int mSensorHeight;
100     int mCurrentX;
101     int mCurrentY;
102     int mSubX;
103     int mSubY;
104     int mSceneX;
105     int mSceneY;
106     int mSceneIdx;
107     uint32_t *mCurrentSceneMaterial;
108 
109     int mHour;
110     float mExposureDuration;
111     float mSensorSensitivity;
112 
113     enum Materials {
114         GRASS = 0,
115         GRASS_SHADOW,
116         HILL,
117         WALL,
118         ROOF,
119         DOOR,
120         CHIMNEY,
121         WINDOW,
122         SUN,
123         SKY,
124         MOON,
125         NUM_MATERIALS
126     };
127 
128     uint32_t mCurrentColors[NUM_MATERIALS*NUM_CHANNELS];
129 
130     /**
131      * Constants for scene definition. These are various degrees of approximate.
132      */
133 
134     // Fake handshake parameters. Two shake frequencies per axis, plus magnitude
135     // as a fraction of a scene tile, and relative magnitudes for the frequencies
136     static const float kHorizShakeFreq1;
137     static const float kHorizShakeFreq2;
138     static const float kVertShakeFreq1;
139     static const float kVertShakeFreq2;
140     static const float kFreq1Magnitude;
141     static const float kFreq2Magnitude;
142 
143     static const float kShakeFraction;
144 
145     // RGB->YUV conversion
146     static const float kRgb2Yuv[12];
147 
148     // Aperture of imaging lens
149     static const float kAperture;
150 
151     // Sun, moon illuminance levels in 2-hour increments. These don't match any
152     // real day anywhere.
153     static const uint32_t kTimeStep = 2;
154     static const float kSunlight[];
155     static const float kMoonlight[];
156     static const int kSunOverhead;
157     static const int kMoonOverhead;
158 
159     // Illumination levels for various conditions, in lux
160     static const float kDirectSunIllum;
161     static const float kDaylightShadeIllum;
162     static const float kSunsetIllum;
163     static const float kTwilightIllum;
164     static const float kFullMoonIllum;
165     static const float kClearNightIllum;
166     static const float kStarIllum;
167     static const float kLivingRoomIllum;
168 
169     // Chromaticity of various illumination sources
170     static const float kIncandescentXY[2];
171     static const float kDirectSunlightXY[2];
172     static const float kDaylightXY[2];
173     static const float kNoonSkyXY[2];
174     static const float kMoonlightXY[2];
175     static const float kSunsetXY[2];
176 
177     static const uint8_t kSelfLit;
178     static const uint8_t kShadowed;
179     static const uint8_t kSky;
180 
181     static const float kMaterials_xyY[NUM_MATERIALS][3];
182     static const uint8_t kMaterialsFlags[NUM_MATERIALS];
183 
184     static const int kSceneWidth;
185     static const int kSceneHeight;
186     static const uint8_t kScene[];
187 };
188 
189 }
190 
191 #endif // HW_EMULATOR_CAMERA2_SCENE_H
192