Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @(#)ImageFormatProvider.java
*
* Copyright (c) 2025 The authors and contributors of JHotDraw.
* You may not use, copy or modify this file, except in compliance with the
* accompanying license terms.
*/
package org.jhotdraw.draw.io;

import org.jhotdraw.draw.figure.ImageHolderFigure;

/**
* A provider interface for image format support.
*
* <p>Implementations of this interface define how to create input and output
* formats for specific image types (PNG, JPEG, GIF, etc.).</p>
*
* <p>To add support for a new image format:</p>
* <ol>
* <li>Implement this interface</li>
* <li>Register with {@link ImageFormatRegistry#registerProvider(ImageFormatProvider)}</li>
* <li>Or add to META-INF/services/org.jhotdraw.draw.io.ImageFormatProvider for SPI</li>
* </ol>
*
* @author JHotDraw
* @version $Id$
*/
public interface ImageFormatProvider {

/**
* Returns the format name (e.g., "PNG", "JPEG").
*
* @return the format name
*/
String getFormatName();

/**
* Returns a human-readable description of the format.
*
* @return the format description
*/
String getDescription();

/**
* Returns the file extensions supported by this format.
*
* @return array of file extensions (without dots)
*/
String[] getFileExtensions();

/**
* Returns the MIME types supported by this format.
*
* @return array of MIME types
*/
String[] getMimeTypes();

/**
* Creates an input format for reading images of this type.
*
* @param prototype the prototype figure for holding the image
* @return an InputFormat instance
*/
InputFormat createInputFormat(ImageHolderFigure prototype);

/**
* Creates an output format for writing images of this type.
*
* @return an OutputFormat instance, or null if output is not supported
*/
OutputFormat createOutputFormat();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* @(#)ImageFormatRegistry.java
*
* Copyright (c) 2025 The authors and contributors of JHotDraw.
* You may not use, copy or modify this file, except in compliance with the
* accompanying license terms.
*/
package org.jhotdraw.draw.io;

import org.jhotdraw.draw.figure.ImageHolderFigure;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

/**
* A registry for image format handlers that provides a modular way to
* add support for various image formats (PNG, JPEG, GIF, etc.).
*
* <p>This class uses the Service Provider Interface (SPI) pattern to allow
* dynamic registration of image format handlers.</p>
*
* <p><b>Usage:</b></p>
* <pre>
* // Get all registered input formats for a prototype figure
* List&lt;InputFormat&gt; formats = ImageFormatRegistry.getInputFormats(myImageFigure);
* drawing.getInputFormats().addAll(formats);
* </pre>
*
* @author JHotDraw
* @version $Id$
*/
public final class ImageFormatRegistry {

private static final List<ImageFormatProvider> providers = new ArrayList<>();

static {
// Register default providers
registerProvider(new PngFormatProvider());
registerProvider(new JpegFormatProvider());

// Load additional providers via SPI
ServiceLoader<ImageFormatProvider> loader = ServiceLoader.load(ImageFormatProvider.class);
for (ImageFormatProvider provider : loader) {
registerProvider(provider);
}
}

private ImageFormatRegistry() {
// Prevent instantiation
}

/**
* Registers a new image format provider.
*
* @param provider the provider to register
*/
public static void registerProvider(ImageFormatProvider provider) {
if (provider != null && !providers.contains(provider)) {
providers.add(provider);
}
}

/**
* Unregisters an image format provider.
*
* @param provider the provider to unregister
*/
public static void unregisterProvider(ImageFormatProvider provider) {
providers.remove(provider);
}

/**
* Returns all registered providers.
*
* @return an unmodifiable list of providers
*/
public static List<ImageFormatProvider> getProviders() {
return Collections.unmodifiableList(providers);
}

/**
* Creates input formats for all registered image format providers.
*
* @param prototype the prototype figure to use for creating image holders
* @return a list of input formats
*/
public static List<InputFormat> createInputFormats(ImageHolderFigure prototype) {
List<InputFormat> formats = new ArrayList<>();
for (ImageFormatProvider provider : providers) {
formats.add(provider.createInputFormat(prototype));
}
return formats;
}

/**
* Creates output formats for all registered image format providers.
*
* @return a list of output formats
*/
public static List<OutputFormat> createOutputFormats() {
List<OutputFormat> formats = new ArrayList<>();
for (ImageFormatProvider provider : providers) {
OutputFormat format = provider.createOutputFormat();
if (format != null) {
formats.add(format);
}
}
return formats;
}

/**
* Checks if a specific format is supported.
*
* @param extension the file extension (e.g., "png", "jpg")
* @return true if the format is supported
*/
public static boolean isFormatSupported(String extension) {
String ext = extension.toLowerCase();
for (ImageFormatProvider provider : providers) {
for (String supported : provider.getFileExtensions()) {
if (supported.equalsIgnoreCase(ext)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @(#)JpegFormatProvider.java
*
* Copyright (c) 2025 The authors and contributors of JHotDraw.
* You may not use, copy or modify this file, except in compliance with the
* accompanying license terms.
*/
package org.jhotdraw.draw.io;

import org.jhotdraw.draw.figure.ImageHolderFigure;
import java.awt.image.BufferedImage;

/**
* Provider for JPEG (Joint Photographic Experts Group) image format.
*
* <p>JPEG uses lossy compression, making it ideal for photographs
* and images with smooth color gradients. Supports both .jpg and .jpeg extensions.</p>
*
* @author JHotDraw
* @version $Id$
*/
public class JpegFormatProvider implements ImageFormatProvider {

@Override
public String getFormatName() {
return "JPEG";
}

@Override
public String getDescription() {
return "Joint Photographic Experts Group (JPEG)";
}

@Override
public String[] getFileExtensions() {
return new String[]{"jpg", "jpeg"};
}

@Override
public String[] getMimeTypes() {
return new String[]{"image/jpeg", "image/jpg"};
}

@Override
public InputFormat createInputFormat(ImageHolderFigure prototype) {
return new ImageInputFormat(
prototype,
getFormatName(),
getDescription(),
getFileExtensions(),
getMimeTypes()
);
}

@Override
public OutputFormat createOutputFormat() {
return new ImageOutputFormat(
"JPG",
getDescription(),
"jpg",
BufferedImage.TYPE_INT_RGB
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @(#)PngFormatProvider.java
*
* Copyright (c) 2025 The authors and contributors of JHotDraw.
* You may not use, copy or modify this file, except in compliance with the
* accompanying license terms.
*/
package org.jhotdraw.draw.io;

import org.jhotdraw.draw.figure.ImageHolderFigure;
import java.awt.image.BufferedImage;

/**
* Provider for PNG (Portable Network Graphics) image format.
*
* <p>PNG supports lossless compression and transparency (alpha channel),
* making it ideal for graphics with sharp edges or text.</p>
*
* @author JHotDraw
* @version $Id$
*/
public class PngFormatProvider implements ImageFormatProvider {

@Override
public String getFormatName() {
return "PNG";
}

@Override
public String getDescription() {
return "Portable Network Graphics (PNG)";
}

@Override
public String[] getFileExtensions() {
return new String[]{"png"};
}

@Override
public String[] getMimeTypes() {
return new String[]{"image/png"};
}

@Override
public InputFormat createInputFormat(ImageHolderFigure prototype) {
return new ImageInputFormat(
prototype,
getFormatName(),
getDescription(),
getFileExtensions(),
getMimeTypes()
);
}

@Override
public OutputFormat createOutputFormat() {
return new ImageOutputFormat(
getFormatName(),
getDescription(),
"png",
BufferedImage.TYPE_INT_ARGB
);
}
}
Loading
Loading