Python获取图片ARGB值并生成代码
立即下载
资源介绍:
实际代码文件和shell文件
import os
gReplaceName = [
'IDS_TT_',
'IDS_SPDIG_',
'IDS_AT_',
'IDS_ADAS_ADAS_',
]
gTelltaleDataInfoText = '/**********************************************************************************************************************/\n'
gTelltaleDataText = '/**********************************************************************************************************************/\n'
gTelltaleTypedefText = '/**********************************************************************************************************************/\n'
gTelltaleVariableText = '/**********************************************************************************************************************/\n'
gSpeedDataInfoText = '/**********************************************************************************************************************/\n'
gSpeedDataText = '/**********************************************************************************************************************/\n'
gSpeedTypedefText = '/**********************************************************************************************************************/\n'
gSpeedVariableText = '/**********************************************************************************************************************/\n'
gGearDataInfoText = '/**********************************************************************************************************************/\n'
gGearDataText = '/**********************************************************************************************************************/\n'
gGearTypedefText = '/**********************************************************************************************************************/\n'
gGearVariableText = '/**********************************************************************************************************************/\n'
gAdasDataInfoText = '/**********************************************************************************************************************/\n'
gAdasDataText = '/**********************************************************************************************************************/\n'
gADASTypedefText = '/**********************************************************************************************************************/\n'
gADASVariableText = '/**********************************************************************************************************************/\n'
gDataInfoText = 0
gDataText = 1
gTypedefText = 2
gVariableText = 3
gUncompressedImageText = {
'Telltale': [gTelltaleDataInfoText, gTelltaleDataText, gTelltaleTypedefText, gTelltaleVariableText],
'Speed': [gSpeedDataInfoText, gSpeedDataText, gSpeedTypedefText, gSpeedVariableText ],
'Gear': [gGearDataInfoText, gGearDataText, gGearTypedefText, gGearVariableText ],
'ADAS': [gAdasDataInfoText, gAdasDataText, gADASTypedefText, gADASVariableText ]
}
class ClassWriteFile:
def __init__(self) -> None:
pass
def CheckFileIsExist(self, FileName):
if os.path.exists(FileName + '.c') :
os.remove(FileName + '.c')
if os.path.exists(FileName + '.h') :
os.remove(FileName + '.h')
def WriteARGBintoCFile(self, PictureName, ARGBhex, FileName, gIsConstFlag) :
cFileName = FileName + '.c'
if os.path.exists(cFileName) :
cFileOpen = open(cFileName, 'a+')
else :
cFileOpen = open(cFileName, 'w')
text = '#include \n'\
'#include \n'\
'#include "Log_print_api.h"\n'\
'#include "' + FileName + '.h"\n'\
'\n'
cFileOpen.write(text)
if gIsConstFlag == 1 :
text = 'const static uint32_t s_' + PictureName + '[] = {'
elif gIsConstFlag == 0 :
text = 'static uint32_t ' + 's_' + PictureName + '[] = {'
text += ', '.join(['0x%08XU'%count for count in ARGBhex])
text += '};\n'
cFileOpen.write(text)
cFileOpen.write('\n')
cFileOpen.close()
def WriteModuleFile(self, ModuleName, FileName, pictureName, gIsConstFlag) :
# Head File
HeadFileName = FileName + '.h'
macro = HeadFileName.upper().replace('.', '_')
hfile = open(HeadFileName, "w")
hfile.write('#ifndef ' + macro + '\n')
hfile.write('#define ' + macro + '\n\n')
text = '#include \n'\
'\n'
hfile.write(text)
if ModuleName in gUncompressedImageText:
text = gUncompressedImageText[ModuleName][gTypedefText]
hfile.write(text)
hfile.write('typedef enum {\n')
moduleName = ModuleName + '_Image_'
for count in pictureName:
upperName = count[0].upper()
name = str(upperName)
for name_count in gReplaceName :
if name_count in name :
name = name.replace(name_count, moduleName)
hfile.write(' ' + name + ',\n')
hfile.write(' ' + ModuleName + '_Image_Count\n')
hfile.write('} ' + ModuleName + '_Image_ID_t;\n\n')
text = 'typedef struct {\n'\
' uint32_t ImageX;\n'\
' uint32_t ImageY;\n'\
' uint32_t ImageWidth;\n'\
' uint32_t ImageHeight;\n'\
'} ' + ModuleName + '_Image_Info_t;\n'\
'\n'\
'' + ModuleName + '_Image_Info_t get' + ModuleName + 'ImageGeometry(' + ModuleName + '_Image_ID_t id);\n'\
'\n'\
'const uint32_t* get' + ModuleName + 'ImageData(' + ModuleName + '_Image_ID_t id);\n'\
'\n'
hfile.write(text)
hfile.write('#endif\n')
hfile.close()
# C File
CFileName = FileName + '.c'
cfile = open(CFileName, 'a+')
if ModuleName in gUncompressedImageText:
text = gUncompressedImageText[ModuleName][gVariableText]
cfile.write(text)
text = 'const static uint32_t* s_' + ModuleName + '_Image_Data[' + ModuleName + '_Image_Count] = {\n'
const_count = 0
for count in pictureName :
if gIsConstFlag[const_count] == 1 :
text += ' ' + 's_' + count[0] + ',\n'
elif gIsConstFlag[const_count] == 0 :
text += ' ' + 's_' + count[0] + ',\n'
const_count = const_count + 1
text += '};\n\n'
cfile.write(text)
if ModuleName in gUncompressedImageText:
text = gUncompressedImageText[ModuleName][gDataInfoText]
cfile.write(text)
text = '' + ModuleName + '_Image_Info_t get' + ModuleName + 'ImageGeometry(' + ModuleName + '_Image_ID_t id) {\n'\
' ' + ModuleName + '_Image_Info_t info = {0};\n'\
'\n'\
' if (id >= ' + ModuleName + '_Image_Count) {\n'\
' K_LogE("[RGUI_' + ModuleName + '_Info] ' + ModuleName + ' info receive error id[0x%02x]\\r\\n", id);\n'\
' } else {\n'\
' const uint32_t* data = s_' + ModuleName + '_Image_Data[id];\n'\
' info.ImageX = (uint32_t)data[0];\n'\
' info.ImageY = (uint32_t)data[1];\n'\
' info.ImageWidth = (uint32_t)data[2];\n'\
' info.ImageHeight = (uint32_t)data[3];\n'\
' }\n'\
'\n'\
' return info;\n'\
'}\n'
cfile.write(text)
text = '\n'
cfile.write(text)
if ModuleName in gUncompressedImageText:
text = gUncompressedImageText[ModuleName][gDataText]
cfile.write(text)
text = 'const uint32_t* get' + ModuleName + 'ImageData(' + ModuleName + '_Image_ID_t id) {\n'\
' if (id >= ' +