blob: 1a2e09b5778ae108dc68a69b1f02bdecc0bca6aa [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001# Copyright (c) 2021 Arm Limited. All rights reserved.
2# SPDX-License-Identifier: Apache-2.0
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""
17Utility script to convert a set of RGB images in a given location into
18corresponding cpp files and a single hpp file referencing the vectors
19from the cpp files.
20"""
21import datetime
22import glob
23import math
24import os
25import numpy as np
26
27from argparse import ArgumentParser
28from PIL import Image, UnidentifiedImageError
29from jinja2 import Environment, FileSystemLoader
30
31parser = ArgumentParser()
32parser.add_argument("--image_path", type=str, help="path to images folder or image file to convert.")
33parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.")
34parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.")
35parser.add_argument("--image_size", type=int, nargs=2, help="Size (width and height) of the converted images.")
36parser.add_argument("--license_template", type=str, help="Header template file",
37 default="header_template.txt")
38args = parser.parse_args()
39
40env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),
41 trim_blocks=True,
42 lstrip_blocks=True)
43
44
45def write_hpp_file(header_file_path, cc_file_path, header_template_file, num_images, image_filenames,
46 image_array_names, image_size):
47 print(f"++ Generating {header_file_path}")
48 header_template = env.get_template(header_template_file)
49 hdr = header_template.render(script_name=os.path.basename(__file__),
50 gen_time=datetime.datetime.now(),
51 year=datetime.datetime.now().year)
52 env.get_template('Images.hpp.template').stream(common_template_header=hdr,
53 imgs_count=num_images,
54 img_size=str(image_size[0] * image_size[1] * 3),
55 var_names=image_array_names) \
56 .dump(str(header_file_path))
57
58 env.get_template('Images.cc.template').stream(common_template_header=hdr,
59 var_names=image_array_names,
60 img_names=image_filenames) \
61 .dump(str(cc_file_path))
62
63
64def write_individual_img_cc_file(image_filename, cc_filename, header_template_file, original_image,
65 image_size, array_name):
66 print(f"++ Converting {image_filename} to {os.path.basename(cc_filename)}")
67
68 header_template = env.get_template(header_template_file)
69 hdr = header_template.render(script_name=os.path.basename(__file__),
70 gen_time=datetime.datetime.now(),
71 file_name=os.path.basename(image_filename),
72 year=datetime.datetime.now().year)
73
74 original_image.thumbnail(image_size)
75 delta_w = abs(image_size[0] - original_image.size[0])
76 delta_h = abs(image_size[1] - original_image.size[1])
77 resized_image = Image.new('RGB', args.image_size, (255, 255, 255, 0))
78 resized_image.paste(original_image, (int(delta_w / 2), int(delta_h / 2)))
79
80 # Convert the image and write it to the cc file
81 rgb_data = np.array(resized_image, dtype=np.uint8).flatten()
82 hex_line_generator = (', '.join(map(hex, sub_arr))
83 for sub_arr in np.array_split(rgb_data, math.ceil(len(rgb_data) / 20)))
84 env.get_template('image.cc.template').stream(common_template_header=hdr,
85 var_name=array_name,
86 img_data=hex_line_generator) \
87 .dump(str(cc_filename))
88
89
90def main(args):
91 # Keep the count of the images converted
92 image_idx = 0
93 image_filenames = []
94 image_array_names = []
95
96
97 if os.path.isdir(args.image_path):
98 filepaths = sorted(glob.glob(os.path.join(args.image_path, '**/*.*'), recursive=True))
99 elif os.path.isfile(args.image_path):
100 filepaths = [args.image_path]
101 else:
102 raise OSError("Directory or file does not exist.")
103
104 for filepath in filepaths:
105 filename = os.path.basename(filepath)
106
107 try:
108 original_image = Image.open(filepath).convert("RGB")
109 except UnidentifiedImageError:
110 print(f"-- Skipping file {filepath} due to unsupported image format.")
111 continue
112
113 image_filenames.append(filename)
114
115 # Save the cc file
116 cc_filename = os.path.join(args.source_folder_path,
117 (filename.rsplit(".")[0]).replace(" ", "_") + ".cc")
118 array_name = "im" + str(image_idx)
119 image_array_names.append(array_name)
120 write_individual_img_cc_file(filename, cc_filename, args.license_template,
121 original_image, args.image_size, array_name)
122
123 # Increment image index
124 image_idx = image_idx + 1
125
126 header_filename = "InputFiles.hpp"
127 header_filepath = os.path.join(args.header_folder_path, header_filename)
128 common_cc_filename = "InputFiles.cc"
129 common_cc_filepath = os.path.join(args.source_folder_path, common_cc_filename)
130 write_hpp_file(header_filepath, common_cc_filepath, args.license_template,
131 image_idx, image_filenames, image_array_names, args.image_size)
132
133
134if __name__ == '__main__':
135 main(args)