Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for issue#190 #229

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,34 @@
</plugin>
</plugins>
</build>

<reporting>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tell me please, for what reason you've added it?

<plugins>
<!-- 1. Checkstyle -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<!-- 2. PMD -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.16.0</version>
<configuration>
<rulesets>
<ruleset>/rulesets/java/quickstart.xml</ruleset>
</rulesets>
<printFailingErrors>true</printFailingErrors>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove extra lines.



/**
* Tools for the {@link ImageComparison} object.
*/
public final class ImageComparisonUtil {

public static int OFFSET = 0xff;
public static int EIGHT = 8;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the difference between EIGHT and TENSIX?

public static int TENSIX = 8;

/**
* Make a copy of the {@link BufferedImage} object.
*
Expand Down Expand Up @@ -142,11 +149,13 @@ public static float getDifferencePercent(BufferedImage img1, BufferedImage img2)
long diff = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
diff += pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y));
if(isDiffPixel(img1.getRGB(x, y), img2.getRGB(x, y))) {
diff += 1;
}
}
}
long maxDiff = 3L * 255 * width * height;

//CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190
long maxDiff = (long) width * height;
return (float) (100.0 * diff / maxDiff);
}

Expand All @@ -166,4 +175,22 @@ public static int pixelDiff(int rgb1, int rgb2) {
int b2 = rgb2 & 0xff;
return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
}

/**
* Compare two pixels.
*
* @param rgb1 the first rgb
* @param rgb2 the second rgb
* @return true If they are the same RGB pixel.
*/
//CS304 Issue link: https://github.com/romankh3/image-comparison/issues/190
public static boolean isDiffPixel(final int rgb1, final int rgb2) {
int r1 = (rgb1 >> TENSIX) & OFFSET;
int g1 = (rgb1 >> EIGHT) & OFFSET;
int b1 = rgb1 & OFFSET;
int r2 = (rgb2 >> TENSIX) & OFFSET;
int g2 = (rgb2 >> EIGHT) & OFFSET;
int b2 = rgb2 & OFFSET;
return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2) != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@
@DisplayName("Unit-level testing for {@link ImageComparison} object.")
public class ImageComparisonUnitTest {

//CS304 (manually written) Issue link: https://github.com/romankh3/image-comparison/issues/190
@DisplayName("For issue#190, test the Percentage of Different Pixels of different images")
@Test
public void testDifferencePercent1() {
//given
BufferedImage expectedResultImage = readImageFromResources("result.png");
File file = new File("build/test-images/result.png");
float actualPercent = (float) (100.0 * 695 / (985 * 701));
//when
ImageComparison imageComparison = new ImageComparison("expected.png", "actual.png");
ImageComparisonResult imageComparisonResult = imageComparison.compareImages().writeResultTo(file);

//then
assertNotNull(imageComparison.getActual());
assertNotNull(imageComparison.getExpected());
assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState());
assertEquals(actualPercent, imageComparisonResult.getDifferencePercent());
}

//CS304 (manually written) Issue link: https://github.com/romankh3/image-comparison/issues/190
@DisplayName("For issue#190, test the Percentage of Different Pixels of the same images")
@Test
public void testDifferencePercent2() {
//given
BufferedImage expectedResultImage = readImageFromResources("result.png");
File file = new File("build/test-images/result.png");
float actualPercent = 0.0f;
//when
ImageComparison imageComparison = new ImageComparison("actual.png", "actual.png");
ImageComparisonResult imageComparisonResult = imageComparison.compareImages().writeResultTo(file);

//then
assertNotNull(imageComparison.getActual());
assertNotNull(imageComparison.getExpected());
assertEquals(MATCH, imageComparisonResult.getImageComparisonState());
assertEquals(actualPercent, imageComparisonResult.getDifferencePercent());
}

@DisplayName("The most important test. Shown, that the changes in algorithm, "
+ "don't break the main behaviour and result as expected")
@Test
Expand Down
Loading