f | import struct | f | import struct |
| import sys | | import sys |
n | bmp_content = sys.stdin.buffer.read() | n | file_data = sys.stdin.buffer.read() |
| if bmp_content[:2] != b'BM': | | if file_data[:2] != b'BM': |
| print('Not a Windows BMP') | | print('Not a Windows BMP') |
| exit() | | exit() |
n | file_length, = struct.unpack_from('<I', bmp_content, 2) | n | bmp_size, = struct.unpack_from('<I', file_data, 2) |
| if file_length != len(bmp_content): | | if bmp_size != len(file_data): |
| print('Incorrect size') | | print('Incorrect size') |
| exit() | | exit() |
n | dib_size, = struct.unpack_from('<I', bmp_content, 14) | n | dib_header_size, = struct.unpack_from('<I', file_data, 14) |
| valid_dib_sizes = [12, 40, 52, 56, 108, 124] | | known_dib_sizes = [12, 40, 52, 56, 108, 124] |
| if dib_size not in valid_dib_sizes: | | if dib_header_size not in known_dib_sizes: |
| print('Incorrect header size') | | print('Incorrect header size') |
| exit() | | exit() |
n | if dib_size == 12: | n | if dib_header_size == 12: |
| width, height, planes, bpp = struct.unpack_from('<HHHH', bmp_content | | width, height, planes, bpp = struct.unpack_from('<HHHH', file_data, |
| , 18) | | 18) |
| else: | | else: |
n | width, height = struct.unpack_from('<ii', bmp_content, 18) | n | width, height = struct.unpack_from('<ii', file_data, 18) |
| planes, bpp = struct.unpack_from('<HH', bmp_content, 26) | | planes, bpp = struct.unpack_from('<HH', file_data, 26) |
| abs_height = abs(height) | | abs_height = abs(height) |
n | if dib_size >= 40: | n | if dib_header_size >= 40: |
| compression, = struct.unpack_from('<I', bmp_content, 30) | | compression, = struct.unpack_from('<I', file_data, 30) |
| else: | | else: |
| compression = 0 | | compression = 0 |
t | if dib_size >= 40: | t | if dib_header_size >= 40: |
| image_size, = struct.unpack_from('<I', bmp_content, 34) | | image_size, = struct.unpack_from('<I', file_data, 34) |
| else: | | else: |
| image_size = 0 | | image_size = 0 |
| row_size = (width * bpp + 31) // 32 * 4 | | row_size = (width * bpp + 31) // 32 * 4 |
| calculated_image_size = row_size * abs_height | | calculated_image_size = row_size * abs_height |
| if image_size == 0: | | if image_size == 0: |
| image_size = calculated_image_size | | image_size = calculated_image_size |
| elif image_size not in (calculated_image_size, calculated_image_size + 2 | | elif image_size not in (calculated_image_size, calculated_image_size + 2 |
| ): | | ): |
| print('Incorrect image size') | | print('Incorrect image size') |
| exit() | | exit() |
| filler_size = 0 if image_size == calculated_image_size else 2 | | filler_size = 0 if image_size == calculated_image_size else 2 |
| print(width, abs_height, bpp, compression, filler_size) | | print(width, abs_height, bpp, compression, filler_size) |