|
28 | 28 | * Author(s): Matt Land, Brooke Storm, Sam McGahan |
29 | 29 |
|
30 | 30 | """ |
31 | | - |
| 31 | +#import logging |
32 | 32 |
|
33 | 33 | def load(file, magic_number, header, *, bitmap=None, palette=None): |
34 | | - # TODO: remove unused variables later |
| 34 | + if header[2] > 256: |
| 35 | + raise NotImplementedError("16 bit files are not supported") |
35 | 36 | width = header[0] |
36 | 37 | height = header[1] |
37 | | - max_colors = 6 #header[2] |
38 | | - bitmap = bitmap(width, height, max_colors) |
39 | | - colors = set() |
40 | | - |
41 | 38 |
|
42 | | - if max_colors > 256: |
43 | | - # raise exception |
44 | | - raise NotImplementedError("16 bit grayscale not supported") |
| 39 | + data_start = file.tell() # keep this so we can rewind |
| 40 | + #bitmap = bitmap(width, height, max_colors) |
| 41 | + palette_colors = set() |
45 | 42 |
|
46 | | - if magic_number == b'P2': # To handle ascii PGM files. |
| 43 | + if magic_number == b'P2': # To handle ascii PGM files. |
47 | 44 | # Handle ascii |
48 | | - for y in range(height): |
49 | | - for x in range(width): |
| 45 | + |
| 46 | + pixel = bytearray() |
| 47 | + byte = True |
| 48 | + # scan all colors present in the file |
| 49 | + while byte: |
| 50 | + byte = file.read(1) # type: byte |
| 51 | + if not byte.isdigit(): |
| 52 | + int_pixel = int("".join(["%c" % char for char in pixel])) |
| 53 | + #logging.info(f'color {pixel} {int_pixel}') |
| 54 | + #bitmap[x, y] = int_pixel |
| 55 | + palette_colors.add(int_pixel) |
50 | 56 | pixel = bytearray() |
51 | | - # Takes int and converts to an 8 bit |
52 | | - while True: |
53 | | - byte = file.read(1) # type: byte |
54 | | - if not byte.isdigit(): |
55 | | - break |
56 | | - pixel += byte |
57 | | - # convert b'255' to b'\xff' |
58 | | - int_pixel = bytes([int("".join(["%c" % char for char in pixel]))]) |
59 | | - bitmap[x, y] = int_pixel |
60 | | - colors.add(int_pixel) |
61 | | - # logging.info(f'{x}, {y}, {byte}, {int_pixel}') |
| 57 | + pixel += byte |
| 58 | + |
| 59 | + # logging.info(f'{x}, {y}, {byte}, {int_pixel}') |
62 | 60 | if palette: |
63 | | - palette = palette(len(colors)) |
64 | | - for counter, color in enumerate(colors): |
65 | | - palette[counter] = color |
| 61 | + palette = palette(len(palette_colors)) |
| 62 | + for counter, color in enumerate(palette_colors): |
| 63 | + palette[counter] = bytes([color, color, color]) |
| 64 | + if bitmap: |
| 65 | + bitmap = bitmap(width, height, len(palette_colors)) |
| 66 | + palette_colors = list(palette_colors) |
| 67 | + file.seek(data_start) |
| 68 | + for y in range(height): |
| 69 | + for x in range(width): |
| 70 | + pixel = bytearray() |
| 71 | + # Takes int and converts to an 8 bit |
| 72 | + while True: |
| 73 | + byte = file.read(1) # type: byte |
| 74 | + if not byte.isdigit(): |
| 75 | + break |
| 76 | + pixel += byte |
| 77 | + # convert b'255' to b'\xff' |
| 78 | + color = int("".join(["%c" % char for char in pixel])) |
| 79 | + #logging.info(f'found color {color} in palette at {palette_colors.index(color)}') |
| 80 | + bitmap[x, y] = palette_colors.index(color) |
| 81 | + |
66 | 82 | return bitmap, palette |
67 | 83 |
|
68 | | - if magic_number == b'P5': # To handle binary PGM files. |
| 84 | + if magic_number == b'P5': # To handle binary PGM files. |
69 | 85 | for y in range(height): |
70 | 86 | for x in range(width): |
71 | 87 | byte = file.read(1) |
72 | 88 | if byte == b"": |
73 | 89 | raise ValueError("ran out of file unexpectedly") |
74 | 90 | int_pixel = int.from_bytes(byte, "little") |
75 | 91 | bitmap[x, y] = int_pixel |
76 | | - colors.add(int_pixel) |
| 92 | + palette_colors.add(int_pixel) |
77 | 93 |
|
78 | 94 | if palette: |
79 | | - palette = palette(len(colors)) |
80 | | - for counter, color in enumerate(colors): |
| 95 | + palette = palette(len(palette_colors)) |
| 96 | + for counter, color in enumerate(palette_colors): |
81 | 97 | color_bytearray = bytearray() |
82 | 98 | for i in range(3): |
83 | 99 | color_bytearray += bytes([color]) |
|
0 commit comments