solution_248c.py

#!/usr/bin/python3
# ===============================================================
# GMP image row padding calculator
# ===============================================================

import user_interface as ui

msg = '''\
+--------------------------------------------------+
|           GMP Image padding Calculator           |
|                                                  |
| The pixel format is defined by the DIB header or |
| extra bit masks. Each row in the Pixel array is  |
| padded to a multiple of 4 bytes in size.         |
+--------------------------------------------------+'''

# -------------------------------------------------------
# ---- calculate row padding
# -------------------------------------------------------

def calculate_row_padding(width:int) -> int:

    match (width * 3) % 4:
        case 3:
            return 1
        case 2:
            return 2
        case 1:
            return 3

    return 0

# -------------------------------------------------------
# ---- main
# -------------------------------------------------------

if __name__ == '__main__':

    print(msg)

    while True:

        print()
        s = ui.get_user_input('Enter number of pixels: ')

        if not s: break

        tf,i = ui.is_int(s)

        if not tf:
            print()
            print(f' Error: bad entry ({s})')
            continue

        p = calculate_row_padding(i)

        print(f'Pixels={i:<3} padding={p}')