/* 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.net.URISyntaxException;
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, testImagePNG, testImageGIF, testImageBMP, testImagePNGtr, testImageGIFtr;
	private int imageSize = 32;

	private ImagePHash iph;

	@BeforeClass
	public static void setUpClass() throws Exception {
		testImageJPG = findFilePath("testImage.jpg");
		testImageSmallJPG = findFilePath("testImage_small.jpg");
		testImagePNG = findFilePath("testImage.png");

		testImageGIF = findFilePath("testImage.gif");
		testImageBMP = findFilePath("testImage.bmp");

		testImagePNGtr = findFilePath("testImage-with-transparency.png");
		testImageGIFtr = findFilePath("testImage-with-transparency.gif");
	}

	@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 testSourceImageHashPNG() throws Exception {
		long normal = hashImage(testImagePNG);

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

	@Test
	public void testSourceImageHashGIF() throws Exception {
		long normal = hashImage(testImageGIF);

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

	@Test
	public void testSourceImageHashBMP() throws Exception {
		long normal = hashImage(testImageBMP);

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

	@Test
	public void testSourceImageHashPNGtr() throws Exception {
		long normal = hashImage(testImagePNGtr);

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

	@Test
	public void testSourceImageHashGIFtr() throws Exception {
		long normal = hashImage(testImageGIFtr);

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

	@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);
	}

	private static Path findFilePath(String fileName) throws URISyntaxException {
		return Paths.get(Thread.currentThread().getContextClassLoader().getResource(fileName).toURI());
	}
}

@@ -896,23 +896,18 @@
 estImage
-SmallJ
 P
+N
 G = find
@@ -929,17 +929,11 @@
 mage
-_small.jp
+.pn
 g%22);
@@ -936,35 +936,35 @@
 g%22);%0A%09%09testImage
-PNG
+GIF
  = findFilePath(
@@ -978,16 +978,15 @@
 age.
-png
+gif
 %22);%0A
-%0A
 %09%09te
@@ -984,35 +984,35 @@
 f%22);%0A%09%09testImage
-GIF
+BMP
  = findFilePath(
@@ -1014,39 +1014,40 @@
 Path(%22testImage.
-gif
+bmp
 %22);%0A
+%0A
 %09%09testImageBMP =
@@ -1033,35 +1033,40 @@
 %22);%0A%0A%09%09testImage
-BMP
+SmallJPG
  = findFilePath(
@@ -1067,36 +1067,42 @@
 ePath(%22testImage
-.bmp
+_small.jpg
 %22);%0A%0A%09%09testImage

REFACTOR setup cleanup