Алёна Алексеева, 321 группа BmpParser 15536
Александр Мозжухин Васильевич, 321 группа BmpParser 15393
f1import structf1import struct
2import sys2import sys
n3bmp_content = sys.stdin.buffer.read()n3file_data = sys.stdin.buffer.read()
4if bmp_content[:2] != b'BM':4if file_data[:2] != b'BM':
5    print('Not a Windows BMP')5    print('Not a Windows BMP')
6    exit()6    exit()
n7file_length, = struct.unpack_from('<I', bmp_content, 2)n7bmp_size, = struct.unpack_from('<I', file_data, 2)
8if file_length != len(bmp_content):8if bmp_size != len(file_data):
9    print('Incorrect size')9    print('Incorrect size')
10    exit()10    exit()
n11dib_size, = struct.unpack_from('<I', bmp_content, 14)n11dib_header_size, = struct.unpack_from('<I', file_data, 14)
12valid_dib_sizes = [12, 40, 52, 56, 108, 124]12known_dib_sizes = [12, 40, 52, 56, 108, 124]
13if dib_size not in valid_dib_sizes:13if dib_header_size not in known_dib_sizes:
14    print('Incorrect header size')14    print('Incorrect header size')
15    exit()15    exit()
n16if dib_size == 12:n16if dib_header_size == 12:
17    width, height, planes, bpp = struct.unpack_from('<HHHH', bmp_content17    width, height, planes, bpp = struct.unpack_from('<HHHH', file_data
>, 18)>18)
18else:18else:
n19    width, height = struct.unpack_from('<ii', bmp_content, 18)n19    width, height = struct.unpack_from('<ii', file_data, 18)
20    planes, bpp = struct.unpack_from('<HH', bmp_content, 26)20    planes, bpp = struct.unpack_from('<HH', file_data, 26)
21abs_height = abs(height)21abs_height = abs(height)
n22if dib_size >= 40:n22if dib_header_size >= 40:
23    compression, = struct.unpack_from('<I', bmp_content, 30)23    compression, = struct.unpack_from('<I', file_data, 30)
24else:24else:
25    compression = 025    compression = 0
t26if dib_size >= 40:t26if dib_header_size >= 40:
27    image_size, = struct.unpack_from('<I', bmp_content, 34)27    image_size, = struct.unpack_from('<I', file_data, 34)
28else:28else:
29    image_size = 029    image_size = 0
30row_size = (width * bpp + 31) // 32 * 430row_size = (width * bpp + 31) // 32 * 4
31calculated_image_size = row_size * abs_height31calculated_image_size = row_size * abs_height
32if image_size == 0:32if image_size == 0:
33    image_size = calculated_image_size33    image_size = calculated_image_size
34elif image_size not in (calculated_image_size, calculated_image_size + 234elif image_size not in (calculated_image_size, calculated_image_size + 2
>):>):
35    print('Incorrect image size')35    print('Incorrect image size')
36    exit()36    exit()
37filler_size = 0 if image_size == calculated_image_size else 237filler_size = 0 if image_size == calculated_image_size else 2
38print(width, abs_height, bpp, compression, filler_size)38print(width, abs_height, bpp, compression, filler_size)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op