| """
|
| Authors(s):
|
| Abdelrahman Abdelhamed (a.abdelhamed@samsung.com)
|
|
|
| Copyright (c) 2022 Samsung Electronics Co., Ltd.
|
|
|
| Licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License, (the "License");
|
| you may not use this file except in compliance with the License.
|
| You may obtain a copy of the License at https://creativecommons.org/licenses/by-nc/4.0
|
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| See the License for the specific language governing permissions and limitations under the License.
|
| For conditions of distribution and use, see the accompanying LICENSE.md file.
|
|
|
|
|
| Utility functions for handling DNG opcode lists.
|
| """
|
| import struct
|
| import numpy as np
|
| from pipeline.exif_utils import get_tag_values_from_ifds
|
|
|
|
|
| class Opcode:
|
| def __init__(self, id_, dng_spec_ver, option_bits, size_bytes, data):
|
| self.id = id_
|
| self.dng_spec_ver = dng_spec_ver
|
| self.size_bytes = size_bytes
|
| self.option_bits = option_bits
|
| self.data = data
|
|
|
|
|
| def parse_opcode_lists(ifds):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| opcode_list_tag_nums = [51008, 51009, 51022]
|
| opcode_lists = {}
|
| for i, tag_num in enumerate(opcode_list_tag_nums):
|
| opcode_list_ = get_tag_values_from_ifds(tag_num, ifds)
|
| if opcode_list_ is not None:
|
| opcode_list_ = bytearray(opcode_list_)
|
| opcodes = parse_opcodes(opcode_list_)
|
| opcode_lists.update({tag_num: opcodes})
|
| else:
|
| pass
|
|
|
| return opcode_lists
|
|
|
|
|
| def parse_opcodes(opcode_list):
|
| """
|
| Parse a byte array representing an opcode list.
|
| :param opcode_list: An opcode list as a byte array.
|
| :return: Opcode lists as a dictionary.
|
| """
|
|
|
| endian_sign = ">"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| i = 0
|
| num_opcodes = struct.unpack(endian_sign + "I", opcode_list[i:i + 4])[0]
|
| i += 4
|
|
|
| opcodes = {}
|
| for j in range(num_opcodes):
|
| opcode_id_ = struct.unpack(endian_sign + "I", opcode_list[i:i + 4])[0]
|
| i += 4
|
| dng_spec_ver = [struct.unpack(endian_sign + "B", opcode_list[i + k:i + k + 1])[0] for k in range(4)]
|
| i += 4
|
| option_bits = struct.unpack(endian_sign + "I", opcode_list[i:i + 4])[0]
|
| i += 4
|
|
|
|
|
| if option_bits & 1 == 1:
|
| pass
|
| elif option_bits & 2 == 2:
|
| pass
|
| else:
|
| pass
|
|
|
| opcode_size_bytes = struct.unpack(endian_sign + "I", opcode_list[i:i + 4])[0]
|
| i += 4
|
|
|
| opcode_data = opcode_list[i:i + 4 * opcode_size_bytes]
|
| i += 4 * opcode_size_bytes
|
|
|
|
|
| if opcode_id_ == 9:
|
| opcode_gain_map_data = parse_opcode_gain_map(opcode_data)
|
| opcode_data = opcode_gain_map_data
|
|
|
| elif opcode_id_ == 1:
|
| opcode_rect_warp_data = parse_opcode_rect_warp(opcode_data)
|
| opcode_data = opcode_rect_warp_data
|
|
|
|
|
| opcode = Opcode(id_=opcode_id_, dng_spec_ver=dng_spec_ver, option_bits=option_bits,
|
| size_bytes=opcode_size_bytes,
|
| data=opcode_data)
|
| opcodes.update({opcode_id_: opcode})
|
|
|
| return opcodes
|
|
|
|
|
| def parse_opcode_rect_warp(opcode_data):
|
| endian_sign = ">"
|
| opcode_dict = {}
|
| '''
|
| opcode_dict = {
|
| 'N': 3,
|
| 'coefficient_set': [
|
| {
|
| 'k_r0': 1,
|
| 'k_r1': 0,
|
| 'k_r2': 0,
|
| 'k_r3': 0,
|
| 'k_t0': 0,
|
| 'k_t1': 0,
|
| },
|
| ...
|
| ],
|
| 'cx': 0.5,
|
| 'cy': 0.5
|
| }
|
| '''
|
| i = 0
|
| num_planes = struct.unpack(endian_sign + "L", opcode_data[i:i + 4])[0]
|
| i += 4
|
|
|
| opcode_dict['N'] = num_planes
|
| opcode_dict['coefficient_set'] = []
|
|
|
| for j in range(num_planes):
|
| keys = ['k_r0', 'k_r1', 'k_r2', 'k_r3', 'k_t0', 'k_t1']
|
| coefficient_set = {}
|
| for key in keys:
|
| coefficient_set[key] = struct.unpack(endian_sign + "d", opcode_data[i:i + 8])[0]
|
| i += 8
|
| opcode_dict['coefficient_set'].append(coefficient_set)
|
|
|
| opcode_dict['cx'] = struct.unpack(endian_sign + "d", opcode_data[i:i + 8])[0]
|
| i += 8
|
| opcode_dict['cy'] = struct.unpack(endian_sign + "d", opcode_data[i:i + 8])[0]
|
|
|
| return opcode_dict
|
|
|
|
|
| def parse_opcode_gain_map(opcode_data):
|
| endian_sign = ">"
|
| opcode_dict = {}
|
| keys = ['top', 'left', 'bottom', 'right', 'plane', 'planes', 'row_pitch', 'col_pitch', 'map_points_v',
|
| 'map_points_h', 'map_spacing_v', 'map_spacing_h', 'map_origin_v', 'map_origin_h', 'map_planes', 'map_gain']
|
| dtypes = ['L'] * 10 + ['d'] * 4 + ['L'] + ['f']
|
| dtype_sizes = [4] * 10 + [8] * 4 + [4] * 2
|
| counts = [1] * 15 + [0]
|
|
|
|
|
| i = 0
|
| for k in range(len(keys)):
|
| if counts[k] == 0:
|
| counts[k] = opcode_dict['map_points_v'] * opcode_dict['map_points_h']
|
|
|
| if counts[k] == 1:
|
| vals = struct.unpack(endian_sign + dtypes[k], opcode_data[i:i + dtype_sizes[k]])[0]
|
| i += dtype_sizes[k]
|
| else:
|
| vals = []
|
| for j in range(counts[k]):
|
| vals.append(struct.unpack(endian_sign + dtypes[k], opcode_data[i:i + dtype_sizes[k]])[0])
|
| i += dtype_sizes[k]
|
|
|
| opcode_dict[keys[k]] = vals
|
|
|
| opcode_dict['map_gain_2d'] = np.reshape(opcode_dict['map_gain'],
|
| (opcode_dict['map_points_v'], opcode_dict['map_points_h']))
|
|
|
| return opcode_dict
|
|
|