Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 1 | # Copyright 2014 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.caps |
| 17 | import its.device |
| 18 | import its.objects |
| 19 | import its.target |
| 20 | import os.path |
| 21 | import math |
| 22 | |
| 23 | def main(): |
| 24 | """Test capturing a single frame as both YUV and JPEG outputs. |
| 25 | """ |
| 26 | NAME = os.path.basename(__file__).split(".")[0] |
| 27 | |
| 28 | THRESHOLD_MAX_RMS_DIFF = 0.01 |
| 29 | |
| 30 | fmt_yuv = {"format":"yuv"} |
| 31 | fmt_jpeg = {"format":"jpeg"} |
| 32 | |
| 33 | with its.device.ItsSession() as cam: |
| 34 | props = cam.get_camera_properties() |
Chien-Yu Chen | bad96ca | 2014-10-20 17:30:56 -0700 | [diff] [blame] | 35 | its.caps.skip_unless(its.caps.compute_target_exposure(props)) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 36 | |
| 37 | # Use a manual request with a linear tonemap so that the YUV and JPEG |
| 38 | # should look the same (once converted by the its.image module). |
| 39 | e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"] |
Yin-Chia Yeh | 9afa725 | 2015-05-08 15:05:50 -0700 | [diff] [blame] | 40 | req = its.objects.manual_capture_request(s, e, True, props) |
Ruben Brunk | 370e243 | 2014-10-14 18:33:23 -0700 | [diff] [blame] | 41 | |
| 42 | cap_yuv, cap_jpeg = cam.do_capture(req, [fmt_yuv, fmt_jpeg]) |
| 43 | |
| 44 | img = its.image.convert_capture_to_rgb_image(cap_yuv, True) |
| 45 | its.image.write_image(img, "%s_yuv.jpg" % (NAME)) |
| 46 | tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) |
| 47 | rgb0 = its.image.compute_image_means(tile) |
| 48 | |
| 49 | img = its.image.convert_capture_to_rgb_image(cap_jpeg, True) |
| 50 | its.image.write_image(img, "%s_jpeg.jpg" % (NAME)) |
| 51 | tile = its.image.get_image_patch(img, 0.45, 0.45, 0.1, 0.1) |
| 52 | rgb1 = its.image.compute_image_means(tile) |
| 53 | |
| 54 | rms_diff = math.sqrt( |
| 55 | sum([pow(rgb0[i] - rgb1[i], 2.0) for i in range(3)]) / 3.0) |
| 56 | print "RMS difference:", rms_diff |
| 57 | assert(rms_diff < THRESHOLD_MAX_RMS_DIFF) |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | main() |
| 61 | |