-
Notifications
You must be signed in to change notification settings - Fork 1
First-pass charmap generator script #16
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
Open
GCC-Cardoso
wants to merge
1
commit into
main
Choose a base branch
from
custom-charmap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,119 @@ | ||
| #!/usr/bin/env python3 | ||
| from PIL import Image, UnidentifiedImageError | ||
|
|
||
| CHAR_SIZE = 8 | ||
| CHARS_COL = 32 | ||
| CHARS_ROW = 32 | ||
|
|
||
| STD_IMG_W = CHARS_COL * CHAR_SIZE | ||
| STD_IMG_H = CHARS_ROW * CHAR_SIZE | ||
|
|
||
| HEADER = f""" | ||
| -- File defining the character to pixel mapping | ||
|
|
||
| WIDTH={CHAR_SIZE}; | ||
| DEPTH={CHARS_COL * CHARS_ROW * CHAR_SIZE}; | ||
|
|
||
| ADDRESS_RADIX=UNS; | ||
| DATA_RADIX=BIN; | ||
|
|
||
| CONTENT BEGIN | ||
|
|
||
| """ | ||
|
|
||
| def generate_from_img(f_name): | ||
| ''' | ||
| Main routine for the program's function flow, taking in a file name | ||
| corresponding to a 256x256 image as input (32x32 characters of 8x8 each) | ||
|
|
||
| The standard dimensions can be customized via the constants above | ||
| ''' | ||
|
|
||
| try: | ||
| bit_img = Image.open(f_name) | ||
| except FileNotFoundError as e: | ||
| print(e) | ||
| return 1 | ||
| except UnidentifiedImageError as e: | ||
| print(f"The file {f_name} does not correspond to an image!") | ||
| return 1 | ||
|
|
||
| img_h, img_w = bit_img.size | ||
|
|
||
| if img_h < STD_IMG_W or img_w < STD_IMG_H: | ||
| print(f"Image dimensions are too small (min {img_h}x{img_w})") | ||
| return 1 | ||
|
|
||
| bit_img = bit_img.crop((0, 0, STD_IMG_W, STD_IMG_H)) | ||
|
|
||
| if bit_img.mode != '1': | ||
| bit_img = bit_img.convert('1') | ||
|
|
||
| full_matrix = [] | ||
|
|
||
| last_distinct_line = -1 | ||
| last_line_bits = 'xxxxxxxx' | ||
|
|
||
| for char_y in range(CHARS_ROW): | ||
| full_matrix.append([]) | ||
|
|
||
| for char_x in range(CHARS_COL): | ||
| full_matrix[-1].append("") | ||
|
|
||
| for bit_y in range(8): | ||
| line_bits = "" | ||
|
|
||
| py = char_y * 8 + bit_y | ||
|
|
||
| line_n = char_y * CHARS_COL * 8 + char_x * 8 + bit_y | ||
|
|
||
| for bit_x in range(8): | ||
| px = char_x * 8 + bit_x | ||
| line_bits += str(bit_img.getpixel((px, py)) // 255) | ||
|
|
||
| distinct = last_line_bits != line_bits | ||
|
|
||
| if distinct: | ||
| if last_distinct_line != -1: | ||
| if last_distinct_line == line_n - 1: | ||
| full_matrix[-1][-1] += ( | ||
| f"\t{line_n - 1} : {last_line_bits}\n") | ||
|
|
||
| else: | ||
| c_range = f"[{last_distinct_line}..{line_n-1}]" | ||
| full_matrix[-1][-1] += ( | ||
| f"\t{c_range} : {last_line_bits}\n") | ||
|
|
||
| last_line_bits = line_bits | ||
| last_distinct_line = line_n | ||
|
|
||
| if bit_y == 7: | ||
| if distinct: | ||
| full_matrix[-1][-1] += ( | ||
| f"\t{line_n} : {last_line_bits}\n") | ||
|
|
||
| else: | ||
| c_range = f"[{last_distinct_line}..{line_n}]" | ||
| full_matrix[-1][-1] += ( | ||
| f"\t{c_range} : {last_line_bits}\n") | ||
|
|
||
| last_distinct_line = -1 | ||
| last_line_bits = 'xxxxxxxx' | ||
|
|
||
| full_matrix = "\n".join(["\n".join(row) for row in full_matrix]) | ||
|
|
||
| with open('res/charmap.mif', 'w') as f: | ||
| f.write(HEADER + full_matrix + "\nEND") | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| f_name = input("File name for the image to use for charmap generation: ") | ||
|
|
||
| error_code = generate_from_img(f_name) | ||
|
|
||
| if error_code: | ||
| quit() | ||
|
|
||
| print(f"Saved a charmap with {CHARS_COL * CHARS_ROW} characters") | ||
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.
Could you use sys.argv to get the file name for inputs and outputs and link them in the Makefile? That is the standart way we are developing the tools.