Make JPG and PNG library detection and usage more robust