001    /*
002     * Rotate180
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     * Rotates images by 180 degrees.
018     * The result is the same as a a {@link Flip} operation followed by a {@link Mirror} operation (or vice versa).
019     * Input image must implement {@link net.sourceforge.jiu.data.IntegerImage}.
020     * <h3>Usage example</h3>
021     * <pre>
022     * Rotate180 rotate = new Rotate180();
023     * rotate.setInputImage(image); // something implementing IntegerImage
024     * rotate.process();
025     * PixelImage rotatedImage = rotate.getOutputImage();
026     * </pre>
027     * @author Marco Schmidt
028     */
029    public class Rotate180 extends ImageToImageOperation
030    {
031            private void process(IntegerImage in, IntegerImage out)
032            {
033                    final int WIDTH = in.getWidth();
034                    final int HEIGHT = in.getHeight();
035                    if (out == null)
036                    {
037                            out = (IntegerImage)in.createCompatibleImage(WIDTH, HEIGHT);
038                            setOutputImage(out);
039                    }
040                    int totalItems = in.getNumChannels() * HEIGHT;
041                    int processedItems = 0;
042                    for (int c = 0; c < in.getNumChannels(); c++)
043                    {
044                            for (int y1 = 0, y2 = HEIGHT - 1; y1 < HEIGHT; y1++, y2--)
045                            {
046                                    for (int x1 = 0, x2 = WIDTH - 1; x1 < WIDTH; x1++, x2--)
047                                    {
048                                            out.putSample(c, x2, y2, in.getSample(c, x1, y1));
049                                    }
050                                    setProgress(processedItems++, totalItems);
051                            }
052                    }
053            }
054    
055            public void process() throws
056                    MissingParameterException,
057                    WrongParameterException
058            {
059                    ensureInputImageIsAvailable();
060                    ensureImagesHaveSameResolution();
061                    PixelImage image = getInputImage();
062                    if (image instanceof IntegerImage)
063                    {
064                            process((IntegerImage)image, (IntegerImage)getOutputImage());
065                    }
066                    else
067                    {
068                            throw new WrongParameterException("Input image must implement IntegerImage.");
069                    }
070            }
071    }