Home | History | Annotate | Download | only in tests
      1 # Copyright 2013 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 import its.image
     16 import its.device
     17 import its.objects
     18 import sys
     19 import numpy
     20 import Image
     21 import pprint
     22 import math
     23 import pylab
     24 import os.path
     25 import matplotlib
     26 import matplotlib.pyplot
     27 
     28 def main():
     29     """Test that device processing can be inverted to linear pixels.
     30 
     31     Captures a sequence of shots with the device pointed at a uniform
     32     target. Attempts to invert all the ISP processing to get back to
     33     linear R,G,B pixel data.
     34     """
     35     NAME = os.path.basename(__file__).split(".")[0]
     36 
     37     # TODO: Query the allowable tonemap curve sizes; here, it's hardcoded to
     38     # a length=64 list of tuples. The max allowed length should be inside the
     39     # camera properties object.
     40     L = 64
     41     LM1 = float(L-1)
     42 
     43     gamma_lut = numpy.array(
     44             sum([[i/LM1, math.pow(i/LM1, 1/2.2)] for i in xrange(L)], []))
     45     inv_gamma_lut = numpy.array(
     46             sum([[i/LM1, math.pow(i/LM1, 2.2)] for i in xrange(L)], []))
     47 
     48     req = {
     49         "android.sensor.exposureTime": 10*1000*1000,
     50         "android.sensor.frameDuration": 0,
     51         "android.control.mode": 0,
     52         "android.control.aeMode": 0,
     53         "android.control.awbMode": 0,
     54         "android.control.afMode": 0,
     55         "android.blackLevel.lock": True,
     56 
     57         # Each channel is a simple gamma curve.
     58         "android.tonemap.mode": 0,
     59         "android.tonemap.curveRed": gamma_lut.tolist(),
     60         "android.tonemap.curveGreen": gamma_lut.tolist(),
     61         "android.tonemap.curveBlue": gamma_lut.tolist(),
     62         }
     63 
     64     sensitivities = range(100,500,50)+range(500,1000,100)+range(1000,3000,300)
     65 
     66     with its.device.ItsSession() as cam:
     67 
     68         # For i=0, don't set manual color correction gains and transform. Graph
     69         # with solid R,G,B curves.
     70         #
     71         # For i=1, set identity transform and unit gains. Graph with dashed
     72         # curves.
     73 
     74         for i in xrange(2):
     75 
     76             r_means = []
     77             g_means = []
     78             b_means = []
     79 
     80             if i == 1:
     81                 req["android.colorCorrection.mode"] = 0
     82                 req["android.colorCorrection.transform"] = (
     83                         its.objects.int_to_rational([1,0,0, 0,1,0, 0,0,1]))
     84                 req["android.colorCorrection.gains"] = [1,1,1,1]
     85 
     86             for sens in sensitivities:
     87                 req["android.sensor.sensitivity"] = sens
     88                 fname, w, h, cap_md = cam.do_capture(req)
     89                 img = its.image.load_yuv420_to_rgb_image(fname, w, h)
     90                 its.image.write_image(
     91                         img, "%s_sens=%04d.jpg" % (NAME, sens))
     92                 img = its.image.apply_lut_to_image(img, inv_gamma_lut[1::2] * LM1)
     93                 tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     94                 rgb_means = its.image.compute_image_means(tile)
     95                 r_means.append(rgb_means[0])
     96                 g_means.append(rgb_means[1])
     97                 b_means.append(rgb_means[2])
     98 
     99             pylab.plot(sensitivities, r_means, ['r','r--'][i])
    100             pylab.plot(sensitivities, g_means, ['g','g--'][i])
    101             pylab.plot(sensitivities, b_means, ['b','b--'][i])
    102 
    103     pylab.ylim([0,1])
    104     matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
    105 
    106 if __name__ == '__main__':
    107     main()
    108 
    109