LosslessFactory uses ByteArrayOutputStreams when creating PDF image data. First, it creates a BAOS in which to store the data, then a BAOS in which to store the flate encoded data. Finally the flate encoded data is written to the PDImageXObject's stream.
We could instead create an empty PDStream, give it a filter, and write the image data directly into the stream. We then instantiate a PDImageXObject giving it the already created stream.
This would dramatically reduce RAM requirement if a scratchfile is in play.