-
Notifications
You must be signed in to change notification settings - Fork 43
Feature/oro 0 precomp zstd #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,39 @@ | ||
| # convert_binary_to_header.py | ||
| # convert_binary_to_array.py | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| def binary_to_c_array(bin_file, array_name): | ||
| def binary_to_c_array(bin_file, array_name, size_BeforeCompression, compression_activated): | ||
| with open(bin_file, 'rb') as f: | ||
| binary_data = f.read() | ||
|
|
||
| hex_array = ', '.join(f'0x{b:02x}' for b in binary_data) | ||
| c_array = f'const unsigned char {array_name}[] = {{\n {hex_array}\n}};\n' | ||
| c_array += f'const size_t {array_name}_size = sizeof({array_name});\n' | ||
| c_array += f'const size_t {array_name}_size = sizeof({array_name}); // {len(binary_data)}\n' | ||
|
|
||
| if not compression_activated: | ||
| size_BeforeCompression = 0 # set value to 0 if we are not using compression. | ||
| c_array += f'const size_t {array_name}_size_uncompressed = {size_BeforeCompression}; // set to 0 if NOT using the ZSTD compression.\n' | ||
| return c_array | ||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) != 3: | ||
| print(f"Usage: {sys.argv[0]} <input_binary_file> <output_header_file>") | ||
| if len(sys.argv) != 5: | ||
| print(f"Usage: {sys.argv[0]} <input_binary_file_before_compression> <input_binary_file_after_compression> <output_header_file> <compression_activated>") | ||
| sys.exit(1) | ||
|
|
||
| bin_file = sys.argv[1] | ||
| header_file_path = sys.argv[2] | ||
| bin_file_beforeCompression = sys.argv[1] | ||
| bin_file_afterCompression = sys.argv[2] # not used if 'compression_activated' is OFF | ||
| header_file_path = sys.argv[3] | ||
| compression_activated = sys.argv[4].lower() == "on" # sys.argv[4] should be "ON" or "OFF" | ||
|
|
||
| header_file = Path(header_file_path).name | ||
| array_name = header_file.replace('.', '_') | ||
|
|
||
| c_array = binary_to_c_array(bin_file, array_name) | ||
| if not compression_activated: | ||
| bin_file_afterCompression = bin_file_beforeCompression | ||
|
|
||
| c_array = binary_to_c_array(bin_file_afterCompression, array_name, Path(bin_file_beforeCompression).stat().st_size, compression_activated ) | ||
| with open(header_file_path, 'w') as f: | ||
| f.write("// generated by convert_binary_to_header.py\n") | ||
| f.write("// generated by convert_binary_to_array.py\n") | ||
| if compression_activated: | ||
| f.write(f"// Data is compressed.\n") | ||
| f.write(c_array) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
|
|
||
| # create_archive.cmake | ||
| # Create a raw Zstd-compressed "archive" from a single file. | ||
|
|
||
| # Variables expected: | ||
| # INPUT_FILE – path to the file to compress | ||
| # OUTPUT_FILE – path to the compressed file to generate | ||
| # DO_COMPRESS: ON/OFF | ||
|
|
||
|
|
||
| if(DO_COMPRESS) | ||
| message("Compress ${INPUT_FILE} ...") | ||
| file(ARCHIVE_CREATE | ||
| OUTPUT "${OUTPUT_FILE}" | ||
| PATHS "${INPUT_FILE}" | ||
| FORMAT raw | ||
| COMPRESSION Zstd | ||
| COMPRESSION_LEVEL 9 # 0-9 for cmake >= 3.19 or 0-19 for cmake >= 3.26 | ||
| ) | ||
| endif() | ||
|
|
||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API relies on the special value (uncompressed_sizeByte > 0) to decide which action to take, which would be error-prone and not very user-friendly.
I suggest to change it to one of the followings:
Create another function to handle the "NOT compressed" case, and let this function to solely handle the data that are "compressed". Or you can rename the function and utilize function overloading.
std::optional( e.g.,std::optional<size_t> uncompressed_sizeByte = std::nullopt)and then you can check it like this:
Wrap the input into different structures:
And then you use function overloading such as: