JPEG: check for arithmetic encoding more memory-efficiently (fixes issue 2577)