Fix reference images being lost when image is cloned/flattened
We shouldn't store a reference layer pointer in the document,
instead, we should fetch it from the image dynamically.
We should also ensure that fake layers are kept in the tree when
the image is flattened.