/* The MIT License (MIT)
 * Copyright (c) 2014 Nicholas Wright
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.hash;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ImagePHashTest {
	private static Path testImageJPG, testImageSmallJPG;
	private int imageSize = 32;

	private ImagePHash iph;

	@BeforeClass
	public static void setUpClass() throws Exception {
		testImageJPG = Paths.get(Thread.currentThread().getContextClassLoader().getResource("testImage.jpg").toURI());
		testImageSmallJPG = Paths.get(Thread.currentThread().getContextClassLoader().getResource("testImage_small.jpg").toURI());
	}

	@Before
	public void setUp() throws Exception {
		iph = new ImagePHash();
	}

	@Test
	public void testgetLongHashCompareScaledandUnscaled() throws Exception {
		long normal = hashWithNoScale();
		long scaled = hashWithScale();

		assertThat(scaled, is(normal));
	}

	@Test
	public void testCompareScaledSourceImage() throws Exception {
		long normal = hashImage(testImageJPG);
		long scaled = hashImage(testImageSmallJPG);

		assertThat(getHammingDistance(normal, scaled), is(4));
	}

	@Test
	public void testSourceImageHash() throws Exception {
		long normal = hashImage(testImageJPG);

		assertThat(normal, is(-6261023631344080448L));
	}

	@Test
	public void testScaledSourceImageHash() throws Exception {
		long scaled = hashImage(testImageSmallJPG);

		assertThat(scaled, is(-6261023624918439488L));
	}

	private int getHammingDistance(long a, long b) {
		long xor = a ^ b;
		int distance = Long.bitCount(xor);
		return distance;
	}

	private long hashImage(Path path) throws IOException, Exception {
		return iph.getLongHash(new BufferedInputStream(Files.newInputStream(path)));
	}

	private long hashWithNoScale() throws Exception {
		return hashImage(testImageJPG);
	}

	private long hashWithScale() throws Exception {
		BufferedImage bi = ImageIO.read(testImageJPG.toFile());
		bi = ImagePHash.resize(bi, imageSize, imageSize);

		return iph.getLongHashScaledImage(bi);
	}
}

@@ -327,16 +327,52 @@
 eption;%0A
+import java.net.URISyntaxException;%0A
 import j
@@ -784,76 +784,20 @@
 G = 
-Paths.get(Thread.currentThread().getContextClassLoader().getResource
+findFilePath
 (%22te
@@ -809,25 +809,16 @@
 ge.jpg%22)
-.toURI())
 ;%0A%09%09test
@@ -837,76 +837,20 @@
 G = 
-Paths.get(Thread.currentThread().getContextClassLoader().getResource
+findFilePath
 (%22te
@@ -872,17 +872,8 @@
 pg%22)
-.toURI())
 ;%0A%09%7D
@@ -2255,11 +2255,192 @@
 (bi);%0A%09%7D
+%0A%0A%09private static Path findFilePath(String fileName) throws URISyntaxException %7B%0A%09%09return Paths.get(Thread.currentThread().getContextClassLoader().getResource(fileName).toURI());%0A%09%7D
 %0A%7D%0A

REFACTOR function for finding file paths