001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Rustem V. Rafikov
019: * @version $Revision: 1.3 $
020: */package javax.imageio;
021:
022: import java.util.Locale;
023: import java.awt.*;
024:
025: public class ImageWriteParam extends IIOParam {
026:
027: public static final int MODE_DISABLED = 0;
028: public static final int MODE_DEFAULT = 1;
029: public static final int MODE_EXPLICIT = 2;
030: public static final int MODE_COPY_FROM_METADATA = 3;
031: protected boolean canWriteTiles = false;
032: protected int tilingMode = MODE_COPY_FROM_METADATA;
033: protected Dimension[] preferredTileSizes = null;
034: protected boolean tilingSet = false;
035: protected int tileWidth = 0;
036: protected int tileHeight = 0;
037: protected boolean canOffsetTiles = false;
038: protected int tileGridXOffset = 0;
039: protected int tileGridYOffset = 0;
040: protected boolean canWriteProgressive = false;
041: protected int progressiveMode = MODE_COPY_FROM_METADATA;
042: protected boolean canWriteCompressed = false;
043: protected int compressionMode = MODE_COPY_FROM_METADATA;
044: protected String[] compressionTypes = null;
045: protected String compressionType = null;
046: protected float compressionQuality = 1.0f;
047: protected Locale locale = null;
048:
049: protected ImageWriteParam() {
050: }
051:
052: public ImageWriteParam(Locale locale) {
053: this .locale = locale;
054:
055: }
056:
057: public int getProgressiveMode() {
058: if (canWriteProgressive()) {
059: return progressiveMode;
060: }
061: throw new UnsupportedOperationException(
062: "progressive mode is not supported");
063: }
064:
065: public boolean canWriteProgressive() {
066: return canWriteProgressive;
067: }
068:
069: public void setProgressiveMode(int mode) {
070: if (canWriteProgressive()) {
071: if (mode < MODE_DISABLED || mode > MODE_COPY_FROM_METADATA
072: || mode == MODE_EXPLICIT) {
073: throw new IllegalArgumentException(
074: "mode is not supported");
075: }
076: this .progressiveMode = mode;
077: }
078: throw new UnsupportedOperationException(
079: "progressive mode is not supported");
080: }
081:
082: public boolean canOffsetTiles() {
083: return canOffsetTiles;
084: }
085:
086: public boolean canWriteCompressed() {
087: return canWriteCompressed;
088: }
089:
090: public boolean canWriteTiles() {
091: return canWriteTiles;
092: }
093:
094: private final void checkWriteCompressed() {
095: if (!canWriteCompressed()) {
096: throw new UnsupportedOperationException(
097: "Compression not supported.");
098: }
099: }
100:
101: private final void checkCompressionMode() {
102: if (getCompressionMode() != MODE_EXPLICIT) {
103: throw new IllegalStateException(
104: "Compression mode not MODE_EXPLICIT!");
105: }
106: }
107:
108: private final void checkCompressionType() {
109: if (getCompressionTypes() != null
110: && getCompressionType() == null) {
111: throw new IllegalStateException("No compression type set!");
112: }
113: }
114:
115: public int getCompressionMode() {
116: checkWriteCompressed();
117: return compressionMode;
118: }
119:
120: public String[] getCompressionTypes() {
121: checkWriteCompressed();
122: if (compressionTypes != null) {
123: return compressionTypes.clone();
124: }
125: return null;
126: }
127:
128: public String getCompressionType() {
129: checkWriteCompressed();
130: checkCompressionMode();
131: return compressionType;
132: }
133:
134: public float getBitRate(float quality) {
135: checkWriteCompressed();
136: checkCompressionMode();
137: checkCompressionType();
138: if (quality < 0 || quality > 1) {
139: throw new IllegalArgumentException("Quality out-of-bounds!");
140: }
141: return -1.0f;
142: }
143:
144: public float getCompressionQuality() {
145: checkWriteCompressed();
146: checkCompressionMode();
147: checkCompressionType();
148: return compressionQuality;
149: }
150:
151: public String[] getCompressionQualityDescriptions() {
152: checkWriteCompressed();
153: checkCompressionMode();
154: checkCompressionType();
155: return null;
156: }
157:
158: public float[] getCompressionQualityValues() {
159: checkWriteCompressed();
160: checkCompressionMode();
161: checkCompressionType();
162: return null;
163: }
164:
165: public Locale getLocale() {
166: return locale;
167: }
168:
169: public String getLocalizedCompressionTypeName() {
170: checkWriteCompressed();
171: checkCompressionMode();
172:
173: String compressionType = getCompressionType();
174: if (compressionType == null) {
175: throw new IllegalStateException("No compression type set!");
176: }
177: return compressionType;
178:
179: }
180:
181: private final void checkTiling() {
182: if (!canWriteTiles()) {
183: throw new UnsupportedOperationException(
184: "Tiling not supported!");
185: }
186: }
187:
188: private final void checkTilingMode() {
189: if (getTilingMode() != MODE_EXPLICIT) {
190: throw new IllegalStateException(
191: "Tiling mode not MODE_EXPLICIT!");
192: }
193: }
194:
195: private final void checkTilingParams() {
196: if (!tilingSet) {
197: throw new IllegalStateException(
198: "Tiling parameters not set!");
199: }
200: }
201:
202: public int getTilingMode() {
203: checkTiling();
204: return tilingMode;
205: }
206:
207: public Dimension[] getPreferredTileSizes() {
208: checkTiling();
209: if (preferredTileSizes == null) {
210: return null;
211: }
212:
213: Dimension[] retval = new Dimension[preferredTileSizes.length];
214: for (int i = 0; i < preferredTileSizes.length; i++) {
215: retval[i] = new Dimension(retval[i]);
216: }
217: return retval;
218: }
219:
220: public int getTileGridXOffset() {
221: checkTiling();
222: checkTilingMode();
223: checkTilingParams();
224: return tileGridXOffset;
225: }
226:
227: public int getTileGridYOffset() {
228: checkTiling();
229: checkTilingMode();
230: checkTilingParams();
231: return tileGridYOffset;
232: }
233:
234: public int getTileHeight() {
235: checkTiling();
236: checkTilingMode();
237: checkTilingParams();
238: return tileHeight;
239: }
240:
241: public int getTileWidth() {
242: checkTiling();
243: checkTilingMode();
244: checkTilingParams();
245: return tileWidth;
246: }
247:
248: public boolean isCompressionLossless() {
249: checkWriteCompressed();
250: checkCompressionMode();
251: checkCompressionType();
252: return true;
253: }
254:
255: public void unsetCompression() {
256: checkWriteCompressed();
257: checkCompressionMode();
258: compressionType = null;
259: compressionQuality = 1;
260: }
261:
262: public void setCompressionMode(int mode) {
263: checkWriteCompressed();
264: switch (mode) {
265: case MODE_EXPLICIT: {
266: compressionMode = mode;
267: unsetCompression();
268: break;
269: }
270: case MODE_COPY_FROM_METADATA:
271: case MODE_DISABLED:
272: case MODE_DEFAULT: {
273: compressionMode = mode;
274: break;
275: }
276: default: {
277: throw new IllegalArgumentException(
278: "Illegal value for mode!");
279: }
280: }
281: }
282:
283: public void setCompressionQuality(float quality) {
284: checkWriteCompressed();
285: checkCompressionMode();
286: checkCompressionType();
287: if (quality < 0 || quality > 1) {
288: throw new IllegalArgumentException("Quality out-of-bounds!");
289: }
290: compressionQuality = quality;
291: }
292:
293: public void setCompressionType(String compressionType) {
294: checkWriteCompressed();
295: checkCompressionMode();
296:
297: if (compressionType == null) { // Don't check anything
298: this .compressionType = null;
299: } else {
300: String[] compressionTypes = getCompressionTypes();
301: if (compressionTypes == null) {
302: throw new UnsupportedOperationException(
303: "No settable compression types");
304: }
305:
306: for (int i = 0; i < compressionTypes.length; i++) {
307: if (compressionTypes[i].equals(compressionType)) {
308: this .compressionType = compressionType;
309: return;
310: }
311: }
312:
313: // Compression type is not in the list.
314: throw new IllegalArgumentException(
315: "Unknown compression type!");
316: }
317: }
318:
319: public void setTiling(int tileWidth, int tileHeight,
320: int tileGridXOffset, int tileGridYOffset) {
321: checkTiling();
322: checkTilingMode();
323:
324: if (!canOffsetTiles()
325: && (tileGridXOffset != 0 || tileGridYOffset != 0)) {
326: throw new UnsupportedOperationException(
327: "Can't offset tiles!");
328: }
329:
330: if (tileWidth <= 0 || tileHeight <= 0) {
331: throw new IllegalArgumentException(
332: "tile dimensions are non-positive!");
333: }
334:
335: Dimension preferredTileSizes[] = getPreferredTileSizes();
336: if (preferredTileSizes != null) {
337: for (int i = 0; i < preferredTileSizes.length; i += 2) {
338: Dimension minSize = preferredTileSizes[i];
339: Dimension maxSize = preferredTileSizes[i + 1];
340: if (tileWidth < minSize.width
341: || tileWidth > maxSize.width
342: || tileHeight < minSize.height
343: || tileHeight > maxSize.height) {
344: throw new IllegalArgumentException(
345: "Illegal tile size!");
346: }
347: }
348: }
349:
350: tilingSet = true;
351: this .tileWidth = tileWidth;
352: this .tileHeight = tileHeight;
353: this .tileGridXOffset = tileGridXOffset;
354: this .tileGridYOffset = tileGridYOffset;
355: }
356:
357: public void unsetTiling() {
358: checkTiling();
359: checkTilingMode();
360:
361: tilingSet = false;
362: tileWidth = 0;
363: tileHeight = 0;
364: tileGridXOffset = 0;
365: tileGridYOffset = 0;
366: }
367:
368: public void setTilingMode(int mode) {
369: checkTiling();
370:
371: switch (mode) {
372: case MODE_EXPLICIT: {
373: tilingMode = mode;
374: unsetTiling();
375: break;
376: }
377: case MODE_COPY_FROM_METADATA:
378: case MODE_DISABLED:
379: case MODE_DEFAULT: {
380: tilingMode = mode;
381: break;
382: }
383: default: {
384: throw new IllegalArgumentException(
385: "Illegal value for mode!");
386: }
387: }
388: }
389: }
|