79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
|
|
import UniTAP
|
||
|
|
import copy
|
||
|
|
from PIL import Image
|
||
|
|
from UniTAP.dev.ports.modules.internal_utils import uicl_image_from_vm_and_data
|
||
|
|
from UniTAP.libs.lib_dscl.dscl_utils import dscl_data_processing
|
||
|
|
from UniTAP.libs.lib_uicl.uicl import UICL_CalculateCRC16
|
||
|
|
from UniTAP.libs.lib_uicl.uicl_types import UICL_Image, UICL_CRC16, UICL_SUCCESS
|
||
|
|
from UniTAP.libs.lib_dscl import dscl_image_calculate_crc
|
||
|
|
from UniTAP.libs.lib_uicl.uicl_utils import image_convert_to_parameters, uicl_parameters_from_vm
|
||
|
|
|
||
|
|
|
||
|
|
def data_from_image_file(target_video_mode: UniTAP.VideoMode, path: str) -> UICL_Image:
|
||
|
|
current_video_mode = copy.deepcopy(target_video_mode)
|
||
|
|
target_size = (target_video_mode.timing.hactive, target_video_mode.timing.vactive)
|
||
|
|
|
||
|
|
image = Image.open(path)
|
||
|
|
image = image.resize(target_size)
|
||
|
|
image = image.convert('RGB')
|
||
|
|
data = bytearray([x for sets in list(image.getdata()) for x in sets])
|
||
|
|
|
||
|
|
current_video_mode.color_info.color_format = UniTAP.ColorInfo.ColorFormat.CF_RGB
|
||
|
|
current_video_mode.color_info.bpc = 8
|
||
|
|
current_video_mode.color_info.colorimetry = UniTAP.ColorInfo.Colorimetry.CM_sRGB
|
||
|
|
|
||
|
|
image = uicl_image_from_vm_and_data(current_video_mode, data)
|
||
|
|
if current_video_mode != target_video_mode:
|
||
|
|
image = image_convert_to_parameters(image, uicl_parameters_from_vm(target_video_mode))
|
||
|
|
|
||
|
|
return image
|
||
|
|
|
||
|
|
|
||
|
|
def image_calculate_crc(src_image):
|
||
|
|
crc = UICL_CRC16()
|
||
|
|
|
||
|
|
result = UICL_CalculateCRC16(src_image, crc)
|
||
|
|
|
||
|
|
assert result >= UICL_SUCCESS, f"Calculation CRC failed with error {result}"
|
||
|
|
|
||
|
|
return crc.R, crc.G, crc.B
|
||
|
|
|
||
|
|
|
||
|
|
def calculate_dsc_image_crc(file_path: str):
|
||
|
|
with open(file_path, "rb") as dsc_file:
|
||
|
|
data = bytearray(dsc_file.read())
|
||
|
|
|
||
|
|
return dscl_image_calculate_crc(dscl_data_processing(data))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
#
|
||
|
|
# Case 1: Normal image
|
||
|
|
#
|
||
|
|
file_name = "C:\\Work\\AdditionWorkFiles\\BinData\\Images\\colorbars.bmp"
|
||
|
|
|
||
|
|
# Fill your parameters of destination color mode
|
||
|
|
color_mode = UniTAP.ColorInfo()
|
||
|
|
color_mode.bpc = 8
|
||
|
|
color_mode.color_format = UniTAP.ColorInfo.ColorFormat.CF_RGB
|
||
|
|
color_mode.colorimetry = UniTAP.ColorInfo.Colorimetry.CM_sRGB
|
||
|
|
|
||
|
|
timing = UniTAP.Timing()
|
||
|
|
timing.hactive = 1920
|
||
|
|
timing.vactive = 1080
|
||
|
|
|
||
|
|
image_video_mode = UniTAP.VideoMode(timing=timing, color_info=color_mode)
|
||
|
|
|
||
|
|
crc_0, crc_1, crc_2 = image_calculate_crc(data_from_image_file(image_video_mode, file_name))
|
||
|
|
|
||
|
|
print(f"CRC 0 - {hex(crc_0)}, CRC 1 - {hex(crc_1)}, CRC 2 - {hex(crc_2)}")
|
||
|
|
|
||
|
|
#
|
||
|
|
# Case 2: DSC Image
|
||
|
|
#
|
||
|
|
file_name = "C:\\Work\\AdditionWorkFiles\\BinData\\DSC\\1920x1080_RGB444_BPY_bpc8_bpp128_10slicew_10sliceh_9lb_v12.dsc"
|
||
|
|
|
||
|
|
dsc_crc_0, dsc_crc_1, dsc_crc_2 = calculate_dsc_image_crc(file_name)
|
||
|
|
|
||
|
|
print(f"DSC CRC 0 - {hex(dsc_crc_0)}, DSC CRC 1 - {hex(dsc_crc_1)}, DSC CRC 2 - {hex(dsc_crc_2)}")
|