Encode function now takes margin and module sizes as inputs