001/*
002 * $RCSfile: BMPImageWriteParam.java,v $
003 *
004 * 
005 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
006 * 
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions
009 * are met: 
010 * 
011 * - Redistribution of source code must retain the above copyright 
012 *   notice, this  list of conditions and the following disclaimer.
013 * 
014 * - Redistribution in binary form must reproduce the above copyright
015 *   notice, this list of conditions and the following disclaimer in 
016 *   the documentation and/or other materials provided with the
017 *   distribution.
018 * 
019 * Neither the name of Sun Microsystems, Inc. or the names of 
020 * contributors may be used to endorse or promote products derived 
021 * from this software without specific prior written permission.
022 * 
023 * This software is provided "AS IS," without a warranty of any 
024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035 * POSSIBILITY OF SUCH DAMAGES. 
036 * 
037 * You acknowledge that this software is not designed or intended for 
038 * use in the design, construction, operation or maintenance of any 
039 * nuclear facility. 
040 *
041 * $Revision: 1.2 $
042 * $Date: 2006/04/14 21:32:04 $
043 * $State: Exp $
044 */
045package com.github.jaiimageio.plugins.bmp;
046
047import java.util.Locale;
048
049import javax.imageio.ImageWriteParam;
050
051import com.github.jaiimageio.impl.plugins.bmp.BMPConstants;
052
053/**
054 * A subclass of <code>ImageWriteParam</code> for encoding images in
055 * the BMP format.
056 *
057 * <p> This class allows for the specification of various parameters
058 * while writing a BMP format image file.  By default, the data layout 
059 * is bottom-up, such that the pixels are stored in bottom-up order, 
060 * the first scanline being stored last.
061 *
062 * <p>The particular compression scheme to be used can be specified by using
063 * the <code>setCompressionType()</code> method with the appropriate type
064 * string.  The compression scheme specified will be honored if and only if it
065 * is compatible with the type of image being written. If the specified 
066 * compression scheme is not compatible with the type of image being written
067 * then the <code>IOException</code> will be thrown by the BMP image writer.
068 * If the compression type is not set explicitly then <code>getCompressionType()</code>
069 * will return <code>null</code>. In this case the BMP image writer will select 
070 * a compression type that supports encoding of the given image without loss
071 * of the color resolution.
072 * <p>The compression type strings and the image type(s) each supports are
073 * listed in the following
074 * table:
075 *
076 * <p><table border=1>
077 * <caption><b>Compression Types</b></caption>
078 * <tr><th>Type String</th> <th>Description</th>  <th>Image Types</th></tr>
079 * <tr><td>BI_RGB</td>  <td>Uncompressed RLE</td> <td><= 8-bits/sample</td></tr>
080 * <tr><td>BI_RLE8</td> <td>8-bit Run Length Encoding</td> <td><= 8-bits/sample</td></tr>
081 * <tr><td>BI_RLE4</td> <td>4-bit Run Length Encoding</td> <td><= 4-bits/sample</td></tr>
082 * <tr><td>BI_BITFIELDS</td> <td>Packed data</td> <td> 16 or 32 bits/sample</td></tr>
083 * <tr><td>BI_JPEG</td> <td>JPEG encoded</td> <td>grayscale or RGB image</td></tr>
084 * </table>
085 *
086 * <p> When <code>BI_BITFIELDS</code> is used, if the image encoded has a
087 * <code>DirectColorModel</code>, the bit mask in the color model will be
088 * written into the stream.  Otherwise, only 5-5-5 16-bit image or 8-8-8 
089 * 32-bit images are supported.
090 *
091 */
092public class BMPImageWriteParam extends ImageWriteParam {
093
094    // deprecated version constants
095
096    /** 
097     * Constant for BMP version 2. 
098     *
099     * @deprecated
100     */
101    public static final int VERSION_2 = 0;
102
103    /** 
104     * Constant for BMP version 3. 
105     *
106     * @deprecated
107     */
108    public static final int VERSION_3 = 1;
109
110    /** 
111     * Constant for BMP version 4. 
112     *
113     * @deprecated
114     */
115    public static final int VERSION_4 = 2;
116
117    /** 
118     * Constant for BMP version 5. 
119     *
120     * @deprecated
121     */
122    public static final int VERSION_5 = 3;
123
124    private boolean topDown = false;
125
126    /**
127     * Constructs a <code>BMPImageWriteParam</code> set to use a given
128     * <code>Locale</code> and with default values for all parameters.
129     *
130     * @param locale a <code>Locale</code> to be used to localize
131     * compression type names and quality descriptions, or
132     * <code>null</code>.
133     */
134    public BMPImageWriteParam(Locale locale) {
135        super(locale);
136
137        // Set compression types ("BI_RGB" denotes uncompressed).
138        compressionTypes = BMPConstants.compressionTypeNames;
139
140        // Set compression flag.
141        canWriteCompressed = true;
142        compressionMode = MODE_COPY_FROM_METADATA;
143        compressionType = compressionTypes[BMPConstants.BI_RGB];
144    }
145
146    /**
147     * Constructs an <code>BMPImageWriteParam</code> object with default
148     * values for all parameters and a <code>null</code> <code>Locale</code>.
149     */
150    public BMPImageWriteParam() {
151        this(null);
152    }
153
154    /**
155     * Returns the BMP version to be used.  The default is
156     * <code>VERSION_3</code>.
157     *
158     * @deprecated
159     * @return the BMP version number.
160     */
161    public int getVersion() {
162        return VERSION_3;
163    }
164
165    /**
166     * If set, the data will be written out in a top-down manner, the first
167     * scanline being written first.
168     *
169     * Any compression other than <code>BI_RGB</code> or 
170     * <code>BI_BITFIELDS</code> is incompatible with the data being 
171     * written in top-down order. Setting the <code>topDown</code> argument
172     * to <code>true</code> will be honored only when the compression 
173     * type at the time of writing the image is one of the two mentioned
174     * above. Otherwise, the <code>topDown</code> setting will be ignored.
175     * 
176     * @param topDown whether the data are written in top-down order.
177     */
178    public void setTopDown(boolean topDown) {
179        this.topDown = topDown;
180    }
181
182    /**
183     * Returns the value of the <code>topDown</code> parameter.
184     * The default is <code>false</code>.
185     *
186     * @return whether the data are written in top-down order.
187     */
188    public boolean isTopDown() {
189        return topDown;
190    }
191
192    // Override superclass implementation to add a new check that compression 
193    // is not being set when image has been specified to be encoded in a top
194    // down fashion
195
196    /**
197     * Sets the compression type to one of the values indicated by
198     * <code>getCompressionTypes</code>.  If a value of
199     * <code>null</code> is passed in, any previous setting is
200     * removed.
201     *
202     * <p>The method first invokes
203     * {@link javax.imageio.ImageWriteParam.#setCompressionType(String) 
204     * <code>setCompressionType()</code>}
205     *  with the supplied value of <code>compressionType</code>. Next, 
206     * if {@link #isTopDown()} returns <code>true</code> and the
207     * value of <code>compressionType</code> is incompatible with top-down
208     * order, {@link #setTopDown(boolean)} is invoked with parameter
209     * <code>topDown</code> set to <code>false</code>. The image will
210     * then be written in bottom-up order with the specified
211     * <code>compressionType</code>.</p>
212     *
213     * @param compressionType one of the <code>String</code>s returned
214     * by <code>getCompressionTypes</code>, or <code>null</code> to
215     * remove any previous setting.
216     *
217     * @exception UnsupportedOperationException if the writer does not
218     * support compression.
219     * @exception IllegalStateException if the compression mode is not
220     * <code>MODE_EXPLICIT</code>.
221     * @exception UnsupportedOperationException if there are no
222     * settable compression types.
223     * @exception IllegalArgumentException if
224     * <code>compressionType</code> is non-<code>null</code> but is not
225     * one of the values returned by <code>getCompressionTypes</code>.
226     *
227     * @see #isTopDown
228     * @see #setTopDown
229     * @see #getCompressionTypes
230     * @see #getCompressionType
231     * @see #unsetCompression
232     */
233    public void setCompressionType(String compressionType) {
234        super.setCompressionType(compressionType);
235        if (!(compressionType.equals("BI_RGB")) &&
236            !(compressionType.equals("BI_BITFIELDS")) && topDown == true) {
237            topDown = false;
238        }
239    }
240
241}