Fixed crash issue, improved flattening

Crash issue caused by running off the end of the array when most pixels were
black.
Flattening was pushing onto the vector rather than preallocating. Should save some computation, no longer need to resize the vector.