001    /*
002     * Flip
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.geometry;
009    
010    import net.sourceforge.jiu.data.PixelImage;
011    import net.sourceforge.jiu.data.IntegerImage;
012    import net.sourceforge.jiu.ops.ImageToImageOperation;
013    import net.sourceforge.jiu.ops.MissingParameterException;
014    import net.sourceforge.jiu.ops.WrongParameterException;
015    
016    /**
017     * Flips images (top row becomes bottom row and vice versa, and so on).
018     * <p>
019     * Supported image types: {@link IntegerImage}.
020     * <p>
021     * Usage example:
022     * <pre>
023     * Flip flip = new Flip();
024     * flip.setInputImage(image); // image is some IntegerImage object
025     * flip.process();
026     * PixelImage flippedImage = flip.getOutputImage();
027     * </pre>
028     * @author Marco Schmidt
029     */
030    public class Flip extends ImageToImageOperation
031    {
032            private void process(IntegerImage in, IntegerImage out)
033            {
034                    final int WIDTH = in.getWidth();
035                    final int HEIGHT = in.getHeight();
036                    final int TOTAL_ITEMS = in.getNumChannels() * HEIGHT;
037                    if (out == null)
038                    {
039                            out = (IntegerImage)in.createCompatibleImage(WIDTH, HEIGHT);
040                            setOutputImage(out);
041                    }
042                    int processedItems = 0;
043                    for (int c = 0; c < in.getNumChannels(); c++)
044                    {
045                            for (int y1 = 0, y2 = HEIGHT - 1; y1 < HEIGHT; y1++, y2--)
046                            {
047                                    for (int x = 0; x < WIDTH; x++)
048                                    {
049                                            out.putSample(c, x, y2, in.getSample(c, x, y1));
050                                    }
051                                    setProgress(processedItems++, TOTAL_ITEMS);
052                            }
053                    }
054            }
055    
056            public void process() throws
057                    MissingParameterException,
058                    WrongParameterException
059            {
060                    ensureInputImageIsAvailable();
061                    ensureImagesHaveSameResolution();
062                    PixelImage in = getInputImage();
063                    if (in instanceof IntegerImage)
064                    {
065                            process((IntegerImage)in, (IntegerImage)getOutputImage());
066                    }
067                    else
068                    {
069                            throw new WrongParameterException("Input image must be of type IntegerImage.");
070                    }
071            }
072    }