001    /*
002     * ImagesToImageOperation
003     * 
004     * Copyright (c) 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.ops;
009    
010    import java.util.Vector;
011    import net.sourceforge.jiu.data.PixelImage;
012    import net.sourceforge.jiu.ops.Operation;
013    import net.sourceforge.jiu.ops.WrongParameterException;
014    
015    /**
016     * An operation that takes several input images and produces one output image.
017     *
018     * @author Marco Schmidt
019     * @since 0.11.0
020     */
021    public abstract class ImagesToImageOperation extends Operation
022    {
023            private Vector inputImages = new Vector();
024            private PixelImage outputImage;
025    
026            /**
027             * Constructs a new ImagesToImageOperation and initializes
028             * input images and output image to null.
029             */
030            public ImagesToImageOperation()
031            {
032                    this(null, null);
033            }
034    
035            /**
036             * Constructs a new ImagesToImageOperation and initializes
037             * input images and output image to the arguments.
038             */
039            public ImagesToImageOperation(Vector in, PixelImage out)
040            {
041                    if (in != null)
042                    {
043                            for (int i = 0; i < in.size(); i++)
044                            {
045                                    addInputImage((PixelImage)in.elementAt(i));
046                            }
047                    }
048                    setOutputImage(out);
049            }
050    
051            /**
052             * Adds an image to the end of the internal list of 
053             * input images.
054             */
055            public void addInputImage(PixelImage in)
056            {
057                    inputImages.addElement(in);
058            }
059    
060            /**
061             * Checks if all images have the same resolution as given by their
062             * getWidth and getHeight methods.
063             * This method will not complain if input and / or output images are not 
064             * available.
065             * @throws WrongParameterException if input and output images exist and their 
066             *  resolutions differ
067             */
068            public void ensureImagesHaveSameResolution() throws WrongParameterException
069            {
070                    if (inputImages == null || inputImages.size() < 1)
071                    {
072                            return;
073                    }
074                    PixelImage in = getInputImage(0);
075                    int width = in.getWidth();
076                    int height = in.getHeight();
077                    int index = 1;
078                    while (index < inputImages.size())
079                    {
080                            in = getInputImage(index);
081                            if (in.getWidth() != width)
082                            {
083                                    throw new WrongParameterException("Width of images #0 and #" + index + " are not equal.");
084                            }
085                            if (in.getHeight() != height)
086                            {
087                                    throw new WrongParameterException("Height of images #0 and #" + index + " are not equal.");
088                            }
089                            index++;
090                    }               
091                    PixelImage out = getOutputImage();
092                    if (out != null)
093                    {
094                            if (out.getWidth() != width)
095                            {
096                                    throw new WrongParameterException("Width of input images #0 and output image are not equal.");
097                            }
098                            if (out.getHeight() != height)
099                            {
100                                    throw new WrongParameterException("Height of input images #0 and output image are not equal.");
101                            }
102                    }
103            }
104    
105            /**
106             * If an output image has been specified this method will compare
107             * its resolution with the argument resolution and throw an exception if the
108             * resolutions differ.
109             * If no output image has been specified nothing happens.
110             * @param width the horizontal pixel resolution that the output image must have
111             * @param height the vertical pixel resolution that the output image must have
112             * @throws WrongParameterException if the resolutions differ
113             */
114            public void ensureOutputImageResolution(int width, int height) throws WrongParameterException
115            {
116                    PixelImage out = getOutputImage();
117                    if (out != null)
118                    {
119                            if (out.getWidth() != width)
120                            {
121                                    throw new WrongParameterException("Output image must have width " + width + " (got: " + out.getWidth() + ").");
122                            }
123                            if (out.getHeight() != height)
124                            {
125                                    throw new WrongParameterException("Output image must have height " + height + " (got: " + out.getHeight() + ").");
126                            }
127                    }
128            }
129    
130            /**
131             * Returns the input image stored in this object.
132             * @return input image, possibly <code>null</code>
133             */
134            public PixelImage getInputImage(int index)
135            {
136                    return (PixelImage)inputImages.elementAt(index);
137            }
138    
139            /**
140             * Return the number of input images currently stored in this operation.
141             * @return number of images
142             */
143            public int getNumInputImages()
144            {
145                    return inputImages.size();
146            }
147    
148            /**
149             * Returns the output image stored in this object.
150             * @return output image, possibly <code>null</code>
151             */
152            public PixelImage getOutputImage()
153            {
154                    return outputImage;
155            }
156    
157            /**
158             * Sets the output image stored in this object to the argument.
159             * Argument can be <code>null</code>.
160             * @param out the new output image of this object
161             */
162            public void setOutputImage(PixelImage out)
163            {
164                    outputImage = out;
165            }
166    }