Improve image mask handling again

The first patch fixes a what I believe is a bug in the handling of image masks when the amount of data falls short of height*width. This causes the test for issue2984.pdf to fail, but that's because the previous rendering was incorrect, featuring lots of thin horizontal lines at the tops of many letters. This patch causes those lines to disappear, which is the same result given by evince and Mac Preview.

The second patch optimizates mask handling by transferring image mask arrays where possible, instead of copying. I have two test files involving image masks, and the reduction in peak RSS was as follows:
- bach:  ~310 MiB to ~280 MiB
- 37335: ~255 MiB to ~230 MiB