Image Retrieval and Classification Using Local Distance Functions