- PCX File Format
- PCX Reader Plug-ins Architecture
- PCX Reader Plug-in Workshop
- Conclusion
- Resources
PCX Reader Plug-in Workshop
This section shows you how to build and test the PCX reader plug-in. You learn how to organize this plug-in’s directories and files, how to compile the source code, how to package the resulting classfiles into a special JAR file, and how to deploy this JAR file. You also discover an application for testing this plug-in. This application enables you to configure the plug-in and read/display PCX images according to this configuration.
Build the Reader Plug-in
Prior to compiling the plug-in’s source code, you must establish an appropriate directory and file structure. Assuming that Windows is your operating system, and assuming that c:\pcx is your project’s home directory, the following directory and file structure is required:
c:\pcx ca mb javajeff pcx PCXImageReaderSpi.java PCXImageReader.java META-INF services javax.imageio.spi.ImageReaderSpi
The PCXImageReaderSpi.java and PCXImageReader.java files contain the plug-in’s Java source code. Because of their package ca.mb.javajeff.pcx; statements, they must be stored in the ca\mb\javajeff\pcx directory.
The javax.imageio.spi.ImageReaderSpi file contains a single line of text: ca.mb.javajeff.pcx.PCXImageReaderSpi. This text identifies the PCX reader plug-in to the JVM. The jar command requires that this file be stored in the META-INF\services directory.
Assuming that c:\pcx is the current directory, the following commands compile the source files, create a JAR file, and deploy this JAR file to the JRE’s extensions directory: c:\Program Files\Java\jdk1.5.0\jre\lib\ext on my platforms:
- javac ca/mb/javajeff/pcx/*.java
- jar cf pcx.jar -C META-INF/ services ca/mb/javajeff/pcx/*.class
- copy pcx.jar \progra~1\java\jdk1.5.0\jre\lib\ext
The next time you launch Java, pcx.jar will be examined and detected as a reader plug-in. An instance of its SPI will be created and stored in a run-time registry class, waiting to be accessed from Java programs needing to read PCX images.
Test the Reader Plug-in
Now that you’ve built the plug-in, you’ll want to make sure that it works correctly. You could use JUnit to create unit tests that exercise the plug-in by invoking Image I/O methods. Or you could work with PCX Reader Plug-in Tester (PCXRPT).
PCXRPT is a Swing-based application for testing the PCX reader plug-in. This application displays a PCX image in its scrollable main window. It also reports the image’s width and height, and the path and name of the originating PCX file via a status bar. Check out Figure 1.
Figure 1 PCXRPT’s GUI lets you read and display PCX images.
The File menu presents a Configure menu item that launches a dialog box for configuring the plug-in prior to reading an image. As shown in Figure 2, this dialog box initially presents default configuration settings.
Figure 2 The configuration dialog box lets you configure the PCX reader plug-in.
The configuration dialog box organizes configuration information into four sections:
- Method: Determines how PCXRPT obtains the PCX reader. It calls javax.imageio.ImageIO’s public static Iterator getImageReadersByFormatName(String formatName) method by default, but can also call public static Iterator getImageReaders(Object input), public static Iterator getImageReadersByMIMEType(String MIMEType), or public static Iterator getImageReadersBySuffix(String fileSuffix). Of these four methods, only getImageReaders() calls PCXImageReaderSpi’s canDecodeInput() method.
- Destination Offset: Identifies the location of the displayed image’s upper-left corner. By default, this location is (0, 0). For any other location, PCXRPT invokes javax.imageio.ImageReadParam’s inherited public void setDestinationOffset(Point destinationOffset) method. PCXRPT displays black pixels in columns to the left of and rows above the upper-left corner.
- Source Region: Identifies the region of the image that is to be displayed. By default, this region’s upper-left corner is (0, 0), its width is 0, and its height is 1. Setting the width to 0 causes PCXRPT to use the entire image as the source region. Otherwise, PCXRPT invokes ImageReadParam’s inherited public void setSourceRegion(Rectangle sourceRegion) method to set a smaller region.
- Source Subsampling: Identifies the image columns and rows that will be displayed (in a periodic fashion), resulting in an image scaled smaller than the original. By default, the X and Y subsampling factors are 1. Setting either or both factors to values greater than 1 causes PCXRPT to invoke ImageReadParam’s inherited public void setSourceSubsampling(int sourceXSubsampling, int sourceYSubsampling, int subsamplingXOffset, int subsamplingYOffset) method. The first two parameters specify the subsampling period; the last two parameters specify an offset (I chose 0) from the source region origin to the first subsampled pixel.
Let’s assume that the destination offset is set to (50, 50), the source region’s upper-left corner is set to (100, 100), the source region’s width and height are each set to 400, and the subsampling factors are set to 2. Figure 3 reveals the displayed image.
Figure 3 The width and height apply to the original image.
Figure 3 shows that the PCX reader plug-has passed several tests. However, additional tests are needed to be certain that the plug-in is 100 percent correct. For example, you could base a test on ImageReadParam’s inherited public void setSourceBands(int[] sourceBands) and non-inherited public void setDestinationBands(int[] destinationBands) methods.