001    /*
002     * PromotionPaletted8
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.color.promotion;
009    
010    import net.sourceforge.jiu.data.BilevelImage;
011    import net.sourceforge.jiu.data.Gray8Image;
012    import net.sourceforge.jiu.data.MemoryPaletted8Image;
013    import net.sourceforge.jiu.data.Palette;
014    import net.sourceforge.jiu.data.Paletted8Image;
015    import net.sourceforge.jiu.data.PixelImage;
016    import net.sourceforge.jiu.ops.ImageToImageOperation;
017    import net.sourceforge.jiu.ops.MissingParameterException;
018    import net.sourceforge.jiu.ops.WrongParameterException;
019    
020    /**
021     * Converts {@link BilevelImage} and {@link Gray8Image} objects to 
022     * {@link Paletted8Image} objects.
023     * This lossless operation will only lead to an output image
024     * that holds the input image in a way that demands more memory.
025     *
026     * @author Marco Schmidt
027     * @since 0.8.0
028     */
029    public class PromotionPaletted8 extends ImageToImageOperation
030    {
031            private void prepare(PixelImage in) throws
032                    MissingParameterException,
033                    WrongParameterException
034            {
035                    if (in == null)
036                    {
037                            throw new MissingParameterException("Missing input image.");
038                    }
039                    Palette palette = null;
040                    if (in instanceof BilevelImage)
041                    {
042                            palette = new Palette(2, 255);
043                            palette.put(0, 0, 0, 0);
044                            palette.put(1, 255, 255, 255);
045                    }
046                    else
047                    if (in instanceof Gray8Image)
048                    {
049                            palette = new Palette(256, 255);
050                            for (int i = 0; i < 256; i++)
051                            {
052                                    palette.put(i, i, i, i);
053                            }
054                    }
055                    else
056                    {
057                            throw new WrongParameterException("Unsupported input image type: " + in.getClass().getName());
058                    }
059                    PixelImage out = getOutputImage();
060                    if (out == null)
061                    {
062                            setOutputImage(new MemoryPaletted8Image(in.getWidth(), in.getHeight(), palette));
063                    }
064                    else
065                    {
066                            if (!(out instanceof Paletted8Image))
067                            {
068                                    throw new WrongParameterException("Specified output image type must be of class Paletted8Image; got " + in.getClass().getName());
069                            }
070                            if (in.getWidth() != out.getWidth())
071                            {
072                                    throw new WrongParameterException("Specified output image must have same width as input image.");
073                            }
074                            if (in.getHeight() != out.getHeight())
075                            {
076                                    throw new WrongParameterException("Specified output image must have same height as input image.");
077                            }
078                    }
079            }
080    
081            private void process(BilevelImage in, Paletted8Image out)
082            {
083                    final int WIDTH = in.getWidth();
084                    final int HEIGHT = in.getHeight();
085                    for (int y = 0; y < HEIGHT; y++)
086                    {
087                            for (int x = 0; x < WIDTH; x++)
088                            {
089                                    if (in.isBlack(x, y))
090                                    {
091                                            out.putByteSample(0, x, y, (byte)0);
092                                    }
093                                    else
094                                    {
095                                            out.putByteSample(0, x, y, (byte)1);
096                                    }
097                            }
098                            setProgress(y, HEIGHT);
099                    }
100            }
101    
102            private void process(Gray8Image in, Paletted8Image out)
103            {
104                    final int WIDTH = in.getWidth();
105                    final int HEIGHT = in.getHeight();
106                    // simple copy
107                    for (int y = 0; y < HEIGHT; y++)
108                    {
109                            for (int x = 0; x < WIDTH; x++)
110                            {
111                                    out.putSample(0, x, y, in.getSample(0, x, y));
112                            }
113                            setProgress(y, HEIGHT);
114                    }
115            }
116    
117            public void process() throws
118                    MissingParameterException,
119                    WrongParameterException
120            {
121                    PixelImage in = getInputImage();
122                    prepare(in);
123                    Paletted8Image out = (Paletted8Image)getOutputImage();
124                    if (in instanceof BilevelImage)
125                    {
126                            process((BilevelImage)in, out);
127                    }
128                    else
129                    if (in instanceof Gray8Image)
130                    {
131                            process((Gray8Image)in, out);
132                    }
133            }
134    }