f | import sys | f | import sys |
| bmp_data = sys.stdin.buffer.read() | | bmp_data = sys.stdin.buffer.read() |
| if len(bmp_data) < 14: | | if len(bmp_data) < 14: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| signature = bmp_data[0:2] | | signature = bmp_data[0:2] |
| if signature != b'BM': | | if signature != b'BM': |
| print('Not a Windows BMP') | | print('Not a Windows BMP') |
| sys.exit(0) | | sys.exit(0) |
| bmp_file_size = int.from_bytes(bmp_data[2:6], byteorder='little') | | bmp_file_size = int.from_bytes(bmp_data[2:6], byteorder='little') |
| if bmp_file_size != len(bmp_data): | | if bmp_file_size != len(bmp_data): |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| if len(bmp_data) < 18: | | if len(bmp_data) < 18: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| dib_header_size = int.from_bytes(bmp_data[14:18], byteorder='little') | | dib_header_size = int.from_bytes(bmp_data[14:18], byteorder='little') |
| if dib_header_size not in [12, 16, 40, 52, 56, 64, 108, 124]: | | if dib_header_size not in [12, 16, 40, 52, 56, 64, 108, 124]: |
| print('Incorrect header size') | | print('Incorrect header size') |
| sys.exit(0) | | sys.exit(0) |
| offset = 14 | | offset = 14 |
| if len(bmp_data) < offset + dib_header_size: | | if len(bmp_data) < offset + dib_header_size: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| if dib_header_size == 12: | | if dib_header_size == 12: |
| if len(bmp_data) < offset + 12: | | if len(bmp_data) < offset + 12: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| width = int.from_bytes(bmp_data[offset + 4:offset + 6], byteorder='l | | width = int.from_bytes(bmp_data[offset + 4:offset + 6], byteorder='l |
| ittle', signed=False) | | ittle', signed=False) |
| height = int.from_bytes(bmp_data[offset + 6:offset + 8], byteorder=' | | height = int.from_bytes(bmp_data[offset + 6:offset + 8], byteorder=' |
| little', signed=False) | | little', signed=False) |
| bits_per_pixel = int.from_bytes(bmp_data[offset + 10:offset + 12], b | | bits_per_pixel = int.from_bytes(bmp_data[offset + 10:offset + 12], b |
| yteorder='little', signed=False) | | yteorder='little', signed=False) |
| compression_method = 0 | | compression_method = 0 |
| image_size = 0 | | image_size = 0 |
| elif dib_header_size == 16: | | elif dib_header_size == 16: |
| if len(bmp_data) < offset + 16: | | if len(bmp_data) < offset + 16: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| width = int.from_bytes(bmp_data[offset + 4:offset + 6], byteorder='l | | width = int.from_bytes(bmp_data[offset + 4:offset + 6], byteorder='l |
| ittle', signed=True) | | ittle', signed=True) |
| height = int.from_bytes(bmp_data[offset + 6:offset + 8], byteorder=' | | height = int.from_bytes(bmp_data[offset + 6:offset + 8], byteorder=' |
| little', signed=True) | | little', signed=True) |
| bits_per_pixel = int.from_bytes(bmp_data[offset + 10:offset + 12], b | | bits_per_pixel = int.from_bytes(bmp_data[offset + 10:offset + 12], b |
| yteorder='little', signed=False) | | yteorder='little', signed=False) |
| compression_method = int.from_bytes(bmp_data[offset + 12:offset + 16 | | compression_method = int.from_bytes(bmp_data[offset + 12:offset + 16 |
| ], byteorder='little') | | ], byteorder='little') |
| image_size = 0 | | image_size = 0 |
| else: | | else: |
| if len(bmp_data) < offset + 24: | | if len(bmp_data) < offset + 24: |
| print('Incorrect size') | | print('Incorrect size') |
| sys.exit(0) | | sys.exit(0) |
| width = int.from_bytes(bmp_data[offset + 4:offset + 8], byteorder='l | | width = int.from_bytes(bmp_data[offset + 4:offset + 8], byteorder='l |
| ittle', signed=True) | | ittle', signed=True) |
| height = int.from_bytes(bmp_data[offset + 8:offset + 12], byteorder= | | height = int.from_bytes(bmp_data[offset + 8:offset + 12], byteorder= |
| 'little', signed=True) | | 'little', signed=True) |
| bits_per_pixel = int.from_bytes(bmp_data[offset + 14:offset + 16], b | | bits_per_pixel = int.from_bytes(bmp_data[offset + 14:offset + 16], b |
| yteorder='little', signed=False) | | yteorder='little', signed=False) |
| compression_method = int.from_bytes(bmp_data[offset + 16:offset + 20 | | compression_method = int.from_bytes(bmp_data[offset + 16:offset + 20 |
| ], byteorder='little') | | ], byteorder='little') |
| image_size = int.from_bytes(bmp_data[offset + 20:offset + 24], byteo | | image_size = int.from_bytes(bmp_data[offset + 20:offset + 24], byteo |
| rder='little') | | rder='little') |
| width_abs = abs(width) | | width_abs = abs(width) |
| height_abs = abs(height) | | height_abs = abs(height) |
| bits_per_row = width_abs * bits_per_pixel | | bits_per_row = width_abs * bits_per_pixel |
| bytes_per_row = (bits_per_row + 7) // 8 | | bytes_per_row = (bits_per_row + 7) // 8 |
| padding = (4 - bytes_per_row % 4) % 4 | | padding = (4 - bytes_per_row % 4) % 4 |
| bytes_per_row_padded = bytes_per_row + padding | | bytes_per_row_padded = bytes_per_row + padding |
| calculated_image_size = bytes_per_row_padded * height_abs | | calculated_image_size = bytes_per_row_padded * height_abs |
| if image_size == calculated_image_size + 2: | | if image_size == calculated_image_size + 2: |
| placeholder_size = 2 | | placeholder_size = 2 |
| elif image_size == calculated_image_size or image_size == 0: | | elif image_size == calculated_image_size or image_size == 0: |
| placeholder_size = 0 | | placeholder_size = 0 |
t | elif image_size == calculated_image_size + 2: | t | |
| placeholder_size = 2 | | |
| else: | | else: |
| print('Incorrect image size') | | print('Incorrect image size') |
| sys.exit(0) | | sys.exit(0) |
| print(f'{width_abs} {height_abs} {bits_per_pixel} {compression_method} { | | print(f'{width_abs} {height_abs} {bits_per_pixel} {compression_method} { |
| placeholder_size}') | | placeholder_size}') |