-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from sam-wmd/feature/resize_augmentation_aspe…
…ct_ratio Added a constructor to ResizeAugmentation.java with scaleFactor
- Loading branch information
Showing
3 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
lib/src/test/java/de/edux/augmentation/effects/AbstractAugmentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package de.edux.augmentation.effects; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public abstract class AbstractAugmentationTest { | ||
protected BufferedImage originalImage; | ||
protected BufferedImage augmentedImage; | ||
|
||
public void shouldHaveSameWidth() { | ||
assertEquals( | ||
originalImage.getWidth(), | ||
augmentedImage.getWidth(), | ||
"Augmented image width should match the specified width."); | ||
} | ||
|
||
public void shouldHaveSameHeight() { | ||
assertEquals( | ||
originalImage.getHeight(), | ||
augmentedImage.getHeight(), | ||
"Augmented image height should match the specified height."); | ||
} | ||
|
||
public void shouldHaveSamePixels() { | ||
int[] originalPixels = | ||
originalImage.getRGB( | ||
0, | ||
0, | ||
originalImage.getWidth(), | ||
originalImage.getHeight(), | ||
null, | ||
0, | ||
originalImage.getWidth()); | ||
int[] augmentedPixels = | ||
augmentedImage.getRGB( | ||
0, | ||
0, | ||
augmentedImage.getWidth(), | ||
augmentedImage.getHeight(), | ||
null, | ||
0, | ||
augmentedImage.getWidth()); | ||
assertFalse( | ||
Arrays.equals(originalPixels, augmentedPixels), | ||
"The augmented image should differ from the original."); | ||
} | ||
|
||
public void shouldNotBeNull() { | ||
assertNotNull(augmentedImage, "Augmented image should not be null."); | ||
} | ||
|
||
public void outputFileShouldExistAndNotBeEmpty() throws IOException { | ||
Path outputPath = Paths.get("augmented.png"); | ||
ImageIO.write(augmentedImage, "png", outputPath.toFile()); | ||
assertTrue(Files.exists(outputPath), "Output image file should exist."); | ||
assertTrue(Files.size(outputPath) > 0, "Output image file should not be empty."); | ||
} | ||
|
||
@AfterEach | ||
public void checkConformity() throws IOException { | ||
shouldNotBeNull(); | ||
shouldHaveSameWidth(); | ||
shouldHaveSameHeight(); | ||
shouldHaveSamePixels(); | ||
outputFileShouldExistAndNotBeEmpty(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
lib/src/test/java/de/edux/augmentation/effects/ResizeAugmentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package de.edux.augmentation.effects; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static de.edux.augmentation.AugmentationTestUtils.openImageInPreview; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static de.edux.augmentation.AugmentationTestUtils.loadTestImage; | ||
|
||
public class ResizeAugmentationTest extends AbstractAugmentationTest { | ||
|
||
private double originalAspectRatio; | ||
|
||
@Override | ||
@AfterEach | ||
public void checkConformity() throws IOException { | ||
shouldNotBeNull(); | ||
shouldHaveSamePixels(); | ||
outputFileShouldExistAndNotBeEmpty(); | ||
} | ||
|
||
@AfterEach | ||
public void shouldMaintainAspectRatio() { | ||
double newAspectRatio = (double) augmentedImage.getWidth() / augmentedImage.getHeight(); | ||
assertEquals( | ||
originalAspectRatio, | ||
newAspectRatio, | ||
"Augmented image should have the same aspect ratio as the original image"); | ||
} | ||
|
||
@AfterEach | ||
public void openImagesInPreview() throws InterruptedException { | ||
openImageInPreview(originalImage); | ||
openImageInPreview(augmentedImage); | ||
} | ||
|
||
@Test | ||
public void shouldIncreaseImageSize() throws IOException { | ||
originalImage = loadTestImage("augmentation" + File.separator + "cyborg-cyberpunk.png"); | ||
originalAspectRatio = (double) originalImage.getWidth() / originalImage.getHeight(); | ||
|
||
double testScaleFactor = 2; | ||
ResizeAugmentation ResizeAugmentation = new ResizeAugmentation(testScaleFactor); | ||
augmentedImage = ResizeAugmentation.apply(originalImage); | ||
|
||
assertEquals(augmentedImage.getHeight(), originalImage.getHeight() * testScaleFactor); | ||
assertEquals(augmentedImage.getWidth(), originalImage.getWidth() * testScaleFactor); | ||
} | ||
|
||
@Test | ||
public void shouldDecreaseImageSize() throws IOException { | ||
originalImage = loadTestImage("augmentation" + File.separator + "neo-tokyo.png"); | ||
originalAspectRatio = (double) originalImage.getWidth() / originalImage.getHeight(); | ||
|
||
double testScaleFactor = 0.5; | ||
ResizeAugmentation ResizeAugmentation = new ResizeAugmentation(testScaleFactor); | ||
augmentedImage = ResizeAugmentation.apply(originalImage); | ||
|
||
assertEquals(augmentedImage.getHeight(), originalImage.getHeight() * testScaleFactor); | ||
assertEquals(augmentedImage.getWidth(), originalImage.getWidth() * testScaleFactor); | ||
} | ||
} |