mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-15 02:54:06 +02:00
# Description This pull request includes several changes aimed at improving the code structure and removing redundant code. The most significant changes involve reordering methods, removing unnecessary annotations, and refactoring constructors to use dependency injection. Autowired now comes via constructor (which also doesn't need autowired annotation as its done by default for configuration) ## Checklist - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [ ] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only)
77 lines
2.8 KiB
Java
77 lines
2.8 KiB
Java
package stirling.software.SPDF.utils;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
public class ImageProcessingUtilsTest {
|
|
|
|
@Test
|
|
void testConvertColorTypeToGreyscale() {
|
|
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
|
fillImageWithColor(sourceImage, Color.RED);
|
|
|
|
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "greyscale");
|
|
|
|
assertNotNull(convertedImage);
|
|
assertEquals(BufferedImage.TYPE_BYTE_GRAY, convertedImage.getType());
|
|
assertEquals(sourceImage.getWidth(), convertedImage.getWidth());
|
|
assertEquals(sourceImage.getHeight(), convertedImage.getHeight());
|
|
|
|
// Check if a pixel is correctly converted to greyscale
|
|
Color grey = new Color(convertedImage.getRGB(0, 0));
|
|
assertEquals(grey.getRed(), grey.getGreen());
|
|
assertEquals(grey.getGreen(), grey.getBlue());
|
|
}
|
|
|
|
@Test
|
|
void testConvertColorTypeToBlackWhite() {
|
|
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
|
fillImageWithColor(sourceImage, Color.RED);
|
|
|
|
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "blackwhite");
|
|
|
|
assertNotNull(convertedImage);
|
|
assertEquals(BufferedImage.TYPE_BYTE_BINARY, convertedImage.getType());
|
|
assertEquals(sourceImage.getWidth(), convertedImage.getWidth());
|
|
assertEquals(sourceImage.getHeight(), convertedImage.getHeight());
|
|
|
|
// Check if a pixel is converted correctly (binary image will be either black or white)
|
|
int rgb = convertedImage.getRGB(0, 0);
|
|
assertTrue(rgb == Color.BLACK.getRGB() || rgb == Color.WHITE.getRGB());
|
|
}
|
|
|
|
@Test
|
|
void testConvertColorTypeToFullColor() {
|
|
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
|
fillImageWithColor(sourceImage, Color.RED);
|
|
|
|
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "fullcolor");
|
|
|
|
assertNotNull(convertedImage);
|
|
assertEquals(sourceImage, convertedImage);
|
|
}
|
|
|
|
@Test
|
|
void testConvertColorTypeInvalid() {
|
|
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
|
|
fillImageWithColor(sourceImage, Color.RED);
|
|
|
|
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "invalidtype");
|
|
|
|
assertNotNull(convertedImage);
|
|
assertEquals(sourceImage, convertedImage);
|
|
}
|
|
|
|
private void fillImageWithColor(BufferedImage image, Color color) {
|
|
for (int y = 0; y < image.getHeight(); y++) {
|
|
for (int x = 0; x < image.getWidth(); x++) {
|
|
image.setRGB(x, y, color.getRGB());
|
|
}
|
|
}
|
|
}
|
|
}
|