001    /*
002     * Paletted8Image
003     *
004     * Copyright (c) 2000, 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.data;
009    
010    import net.sourceforge.jiu.data.Palette;
011    import net.sourceforge.jiu.data.PixelImage;
012    
013    /**
014     * This class stores a paletted image with one byte per sample in memory.
015     *
016     * @author Marco Schmidt
017     * @see net.sourceforge.jiu.data.ByteChannelImage
018     * @see net.sourceforge.jiu.data.IntegerImage
019     * @see net.sourceforge.jiu.data.Palette
020     */
021    public class MemoryPaletted8Image extends MemoryByteChannelImage implements Paletted8Image
022    {
023            /**
024             * This image's palette.
025             */
026            private Palette palette;
027            private int maxSampleValue;
028    
029            /**
030             * Create an image of byte channels.
031             * Image data will be completely in memory, so memory requirements are 
032             * <code>width * height * numChannels</code> bytes.
033             * Note that the data will not be initialized, so you should not assume
034             * anything about its content.
035             * @param width the horizontal resolution, must be non-zero and positive
036             * @param height the vertical resolution, must be non-zero and positive
037             * @throws IllegalArgumentException if any of the parameters are smaller than 1
038             */
039            public MemoryPaletted8Image(int width, int height)
040            {
041                    super(1, width, height);
042                    palette = null;
043                    maxSampleValue = 255;
044            }
045    
046            public MemoryPaletted8Image(int width, int height, Palette palette)
047            {
048                    this(width, height);
049                    setPalette(palette);
050            }
051    
052            public static void checkPalette(Palette palette)
053            {
054                    if (palette == null)
055                    {
056                            throw new IllegalArgumentException("Palette must be non-null.");
057                    }
058                    else
059                    {
060                            int numEntries = palette.getNumEntries();
061                            if (numEntries < 1 || numEntries > 256)
062                            {
063                                    throw new IllegalArgumentException("Number of entries must " +
064                                            "be from 1..256 for a Paletted8Image; got: " + numEntries);
065                            }
066                    }
067            }
068    
069            public PixelImage createCompatibleImage(int width, int height)
070            {
071                    Palette newPalette = null;
072                    Palette myPalette = getPalette();
073                    if (myPalette != null)
074                    {
075                            newPalette = (Palette)myPalette.clone();
076                    }
077                    return new MemoryPaletted8Image(width, height, newPalette);
078            }
079    
080            public long getAllocatedMemory()
081            {
082                    long result = super.getAllocatedMemory();
083                    Palette myPalette = getPalette();
084                    if (myPalette != null)
085                    {
086                            result += myPalette.getAllocatedMemory();
087                    }
088                    return result;
089            }
090    
091            public Class getImageType()
092            {
093                    return Paletted8Image.class;
094            }
095    
096            public int getMaxSample(int channel)
097            {
098                    return maxSampleValue;
099            }
100    
101            /**
102             * Returns this image's palette.
103             * @see #setPalette
104             */
105            public Palette getPalette()
106            {
107                    return palette;
108            }
109    
110            public String getTypeDescription()
111            {
112                    return "Paletted image, 8 bits per pixel";
113            }
114    
115            /**
116             * Sets this image's palette to a new value.
117             * @see #getPalette
118             */
119            public void setPalette(Palette palette)
120            {
121                    if (palette != null && palette.getNumEntries() > 256)
122                    {
123                            throw new IllegalArgumentException("Cannot use palette with more " +
124                                    "than 256 entries in a Paletted8Image.");
125                    }
126                    this.palette = palette;
127                    if (palette == null)
128                    {
129                            maxSampleValue = 255;
130                    }
131                    else
132                    {
133                            maxSampleValue = palette.getNumEntries() - 1;
134                    }
135            }
136    }