Back to Repositories

Testing Image Color Conversion Utilities in Stirling-PDF

This test suite validates the image processing utilities in Stirling-PDF, focusing on color conversion functionality. The tests verify different color transformation methods including greyscale, black/white, and full color conversions with proper image type validation.

Test Coverage Overview

The test suite provides comprehensive coverage of image color conversion operations.

Key areas tested include:
  • Greyscale conversion with pixel value validation
  • Black and white binary conversion
  • Full color preservation
  • Invalid color type handling
Edge cases cover input validation and color space integrity verification.

Implementation Analysis

The testing approach utilizes JUnit 5 framework with systematic verification of image properties and pixel-level assertions. The implementation follows a pattern of creating source images, applying conversions, and validating both image attributes and color values using BufferedImage manipulation.

Framework features leverage @Test annotations and assertion methods for precise validation.

Technical Details

Testing tools and components:
  • JUnit Jupiter test framework
  • Java AWT and BufferedImage classes
  • Custom utility methods for image manipulation
  • RGB color space validation
  • Image type constants (TYPE_BYTE_GRAY, TYPE_BYTE_BINARY)

Best Practices Demonstrated

The test suite exemplifies robust testing practices through isolated test methods, comprehensive assertions, and proper setup helpers.

Notable practices include:
  • Systematic validation of image properties
  • Pixel-level verification
  • Helper method extraction for common operations
  • Clear test method naming conventions
  • Proper error case handling

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/utils/ImageProcessingUtilsTest.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());
            }
        }
    }
}