It seems the court's argument is that using a slightly changed work for a different purpose does not present the same copyright issues as using it for the same purpose. It is one thing to use the transformed photo for a gallery painting; it is quite another to use it for license revenue from a magazine. This is at least realistic: the gallery painting does not compete with the original photographer to nearly the same degree as the licensing does.
It does however create a new problem that whether a work is infringing or not depends on the context of its use.
So if Warhol put his Prince artwork in a gallery it’s not infringing, but then if you take a photograph of that artwork in the gallery and use it in a magazine, that photograph of an artwork is magically infringing again?
It means that artworks can harbor zombie copyright claims that come back to life when you ‘hold it wrong’.
This is a point that deserves closer attention -- thank you for mentioning it. The notion of an affirmative defense is an important part of how our legal system balances rights.
When the work of art is context itself that makes perfect sense. In „Black square“ by Malevich it was not the black square that was the artistic invention, but where he placed it at exhibition. Copyright must not prevent such uses of images, so it is perfectly normal for Warhol to transform the images the way he did. The copyright was infringed by the foundation which was supposed to preserve the works, not to alter them by licensing for the use not intended by author.