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 os.path
     19 import Image
     20 import shutil
     21 import numpy
     22 import math
     23 import copy
     24 
     25 def main():
     26     """Test that converted YUV images and device JPEG images look the same.
     27     """
     28     NAME = os.path.basename(__file__).split(".")[0]
     29 
     30     THRESHOLD_MAX_RMS_DIFF = 0.1
     31 
     32     with its.device.ItsSession() as cam:
     33         props = cam.get_camera_properties()
     34 
     35         # YUV
     36         req = its.objects.manual_capture_request(100,100)
     37         size = props['android.scaler.availableProcessedSizes'][0]
     38         out_surface = copy.deepcopy(size)
     39         out_surface["format"] = "yuv"
     40         fname, w, h, cap_md = cam.do_capture(req, out_surface)
     41         img = its.image.load_yuv420_to_rgb_image(fname, w, h)
     42         its.image.write_image(img, "%s_fmt=yuv.jpg" % (NAME))
     43         tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     44         rgb0 = its.image.compute_image_means(tile)
     45 
     46         # JPEG
     47         req = its.objects.manual_capture_request(100,100)
     48         size = props['android.scaler.availableJpegSizes'][0]
     49         out_surface = copy.deepcopy(size)
     50         out_surface["format"] = "jpg"
     51         fname, w, h, cap_md = cam.do_capture(req, out_surface)
     52         img = numpy.array(Image.open(fname)).reshape(w,h,3) / 255.0
     53         tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1)
     54         rgb1 = its.image.compute_image_means(tile)
     55         shutil.copy2(fname, "%s_fmt=jpg.jpg" % (NAME))
     56 
     57         rms_diff = math.sqrt(
     58                 sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0)
     59         print "RMS difference:", rms_diff
     60         assert(rms_diff < THRESHOLD_MAX_RMS_DIFF)
     61 
     62 if __name__ == '__main__':
     63     main()
     64 
     65