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 its.target
     19 import sys
     20 import numpy
     21 import Image
     22 import pprint
     23 import math
     24 import pylab
     25 import os.path
     26 import matplotlib
     27 import matplotlib.pyplot
     28 
     29 def main():
     30     """Test that device processing can be inverted to linear pixels.
     31 
     32     Captures a sequence of shots with the device pointed at a uniform
     33     target. Attempts to invert all the ISP processing to get back to
     34     linear R,G,B pixel data.
     35     """
     36     NAME = os.path.basename(__file__).split(".")[0]
     37 
     38     NUM_STEPS = 5
     39 
     40     # TODO: Query the allowable tonemap curve sizes; here, it's hardcoded to
     41     # a length=64 list of tuples. The max allowed length should be inside the
     42     # camera properties object.
     43     L = 64
     44     LM1 = float(L-1)
     45 
     46     gamma_lut = numpy.array(
     47             sum([[i/LM1, math.pow(i/LM1, 1/2.2)] for i in xrange(L)], []))
     48     inv_gamma_lut = numpy.array(
     49             sum([[i/LM1, math.pow(i/LM1, 2.2)] for i in xrange(L)], []))
     50 
     51     with its.device.ItsSession() as cam:
     52         expt,_ = its.target.get_target_exposure_combos(cam)["midSensitivity"]
     53         props = cam.get_camera_properties()
     54         sens_range = props['android.sensor.info.sensitivityRange']
     55         sens_step = (sens_range[1] - sens_range[0]) / float(NUM_STEPS-1)
     56         sensitivities = [sens_range[0] + i * sens_step for i in range(NUM_STEPS)]
     57 
     58         req = its.objects.manual_capture_request(0, expt)
     59         req["android.blackLevel.lock"] = True
     60         req["android.tonemap.mode"] = 0
     61         req["android.tonemap.curveRed"] = gamma_lut.tolist()
     62         req["android.tonemap.curveGreen"] = gamma_lut.tolist()
     63         req["android.tonemap.curveBlue"] = gamma_lut.tolist()
     64 
     65         r_means = []
     66         g_means = []
     67         b_means = []
     68 
     69         for sens in sensitivities:
     70             req["android.sensor.sensitivity"] = sens
     71             cap = cam.do_capture(req)
     72             img = its.image.convert_capture_to_rgb_image(cap)
     73             its.image.write_image(
     74                     img, "%s_sens=%04d.jpg" % (NAME, sens))
     75             img = its.image.apply_lut_to_image(img, inv_gamma_lut[1::2] * LM1)
     76             tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     77             rgb_means = its.image.compute_image_means(tile)
     78             r_means.append(rgb_means[0])
     79             g_means.append(rgb_means[1])
     80             b_means.append(rgb_means[2])
     81 
     82         pylab.plot(sensitivities, r_means, 'r')
     83         pylab.plot(sensitivities, g_means, 'g')
     84         pylab.plot(sensitivities, b_means, 'b')
     85 
     86     pylab.ylim([0,1])
     87     matplotlib.pyplot.savefig("%s_plot_means.png" % (NAME))
     88 
     89 if __name__ == '__main__':
     90     main()
     91 
     92