001:
002: /*
003: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
004: * for visualizing and manipulating spatial features with geometry and attributes.
005: *
006: * Copyright (C) 2003 Vivid Solutions
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033:
034: package com.vividsolutions.wms;
035:
036: import java.util.Arrays;
037:
038: /**
039: * A Utility class to select the optimal Map Format based on some preferences.
040: * @author Chris Hodgson chodgson@refractions.net
041: */
042: public class MapImageFormatChooser {
043:
044: private boolean transparencyRequired;
045: private boolean useLossy;
046: //[UT]
047: public static final String[][] IMAGE_FORMATS = {
048: { "GIF", "PNG", "JPEG" }, // for WMS 1.0.0
049: { "image/gif", "image/png", "image/jpeg" } // for WMS 1.1.1
050: };
051:
052: // set image formats to the default WMS (1.0.0)
053: private String[] imageFormats = IMAGE_FORMATS[0];
054:
055: /**
056: * Creates a new instance of MapImageFormatChooser.
057: */
058: public MapImageFormatChooser() {
059: //[UT]
060: this (WMService.WMS_1_0_0); // 1.0.0 is the default WMS, anyway...
061: }
062:
063: /**
064: * Creates a new instance of MapImageFormatChooser.
065: */
066: public MapImageFormatChooser(String wmsVersion) {
067: this .transparencyRequired = false;
068: this .useLossy = false;
069: if (WMService.WMS_1_1_1.equals(wmsVersion)
070: || WMService.WMS_1_1_0.equals(wmsVersion)) {
071: imageFormats = IMAGE_FORMATS[1];
072: }
073: }
074:
075: /**
076: * Returns true if the specified format is known by the MapFormatChooser, false
077: * otherwise. The MapFormatChooser can only reliably select between formats
078: * which it knows; it will only return an unknown format if there are no known
079: * formats to select from. [UT] changed to accept WMS 1.0 and 1.1.1 image formats
080: * @param format the format which is in question
081: * @return true if the specified format is known by the MapFormatChooser, false
082: * otherwise
083: */
084: static public boolean isKnownFormat(String format) {
085: for (int i = 0; i < IMAGE_FORMATS.length; i++) {
086: for (int j = 0; j < IMAGE_FORMATS[i].length; j++) {
087: if (format.equals(IMAGE_FORMATS[i][j])) {
088: return true;
089: }
090: }
091: }
092: return false;
093: }
094:
095: /*
096: * Sets whether tranparency is required in the image format.
097: * If transparency is required it takes priority over lossy compression.
098: * However, if no format that supports transparency is available, the next best
099: * format will be selected.
100: * @param transparencyRequired true if the image format chosen needs to support
101: * transparency
102: */
103: public void setTransparencyRequired(boolean transparencyRequired) {
104: this .transparencyRequired = transparencyRequired;
105: }
106:
107: /*
108: * Sets whether lossy compression is preferred over non-lossy compression.
109: * Lossy compression would generally only be preferred over a slow connection,
110: * and also note that image formats which use lossy compression generally don't
111: * support transparency - in this case the transparency requirement takes priority.
112: * @param useLossy true if lossy compression is preferrable
113: */
114: public void setPreferLossyCompression(boolean useLossy) {
115: this .useLossy = useLossy;
116: }
117:
118: /*
119: * Returns a format String from the Array of available formats which best
120: * matches the requirements and preferences specified. If there are no image
121: * formats available which can be used by the WMS image handling code, then
122: * null is returned.
123: * @param formats the array of available formats to choose from
124: * @return the chosen format string from Array of available formats, or null
125: * if none of the available formats are known
126: */
127: public String chooseFormat(String[] formats) {
128: if (formats.length == 0) {
129: throw new IllegalArgumentException();
130: }
131: String[] order = new String[3];
132: if (transparencyRequired) {
133: order[0] = imageFormats[1]; //png
134: order[1] = imageFormats[0]; //gif
135: order[2] = imageFormats[2]; //jpg
136:
137: } else if (useLossy) {
138:
139: order[0] = imageFormats[2]; //jpg
140: order[1] = imageFormats[1]; //png
141: order[2] = imageFormats[0];
142: } else {
143:
144: order[0] = imageFormats[1]; //png
145: order[1] = imageFormats[2]; //jpg
146: order[2] = imageFormats[0];
147: }
148: Arrays.sort(formats);
149: for (int i = 0; i < order.length; i++) {
150: if (Arrays.binarySearch(formats, order[i]) >= 0) {
151: return order[i];
152: }
153: }
154: return null;
155: }
156: }
|