import struct


def encode_bmp(w, h, pixels):
	"""
	Encode a standard Windows BMP Image File

	https://en.wikipedia.org/wiki/BMP_file_format
	"""
	enc = b''
	for y in range(h):
		y = h-y-1 # Bitmap encodes the image "bottom-up"
		for x in range(w):
			r, g, b, a = pixels[y*w + x]
			enc += struct.pack('<BBBB',
				int(255*b), int(255*g), int(255*r), int(255*a))

	# Encode BITMAPV5HEADER
	# https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapv5header
	hdr = b'BM' + struct.pack('<3I 3I2H11I48x I12x',
		# Bitmap File Header
		14+124+len(enc), # Total size
		0,               # Reserved
		14+124,          # Offset to pixel array
		# DIB Header
		124,             # sizeof(BITMAPV5INFOHEADER)
		w, h,            # Image dimensions
		1,               # No. color planes
		32,              # BPP
		3,               # BI_BITFIELDS
		len(enc),        # Image size
		2835, 2835,      # Horizontal, Vertical Resolution (72DPI)
		0,               # Colors in palette (0=2^n)
		0,               # Important colors (0=all colors)
		0x00ff0000,      # Red channel bitmask
		0x0000ff00,      # Green channel bitmask
		0x000000ff,      # Blue channel bitmask
		0xff000000,      # Alpha channel bitmask
		0x73524742,      # sRGB color space
		4                # LCS_GM_IMAGES
		)

	return hdr + enc