001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.internal.provider;
018:
019: import java.awt.Dimension;
020: import java.awt.Rectangle;
021: import java.awt.geom.AffineTransform;
022: import java.awt.image.BufferedImage;
023: import java.awt.image.Raster;
024: import java.awt.image.RenderedImage;
025: import java.io.IOException;
026: import java.util.Arrays;
027: import java.util.List;
028: import java.util.NoSuchElementException;
029:
030: import net.refractions.udig.project.internal.Layer;
031: import net.refractions.udig.ui.Drawing;
032: import net.refractions.udig.ui.graphics.ViewportGraphics;
033:
034: import org.eclipse.jface.resource.ImageDescriptor;
035: import org.eclipse.swt.graphics.GC;
036: import org.eclipse.swt.graphics.Image;
037: import org.eclipse.swt.graphics.ImageData;
038: import org.eclipse.swt.graphics.PaletteData;
039: import org.eclipse.swt.widgets.Display;
040: import org.geotools.data.FeatureReader;
041: import org.geotools.data.FeatureSource;
042: import org.geotools.data.wms.WebMapServer;
043: import org.geotools.data.wms.request.GetLegendGraphicRequest;
044: import org.geotools.feature.Feature;
045: import org.geotools.feature.IllegalAttributeException;
046: import org.geotools.styling.Style;
047:
048: /**
049: * Builds SWT images for to represent layers.
050: *
051: * @author jeichar
052: * @since 0.6.0
053: */
054: public class GlyphBuilder {
055:
056: public static ImageDescriptor createImageDescriptor(
057: final RenderedImage image) {
058: return new ImageDescriptor() {
059: public ImageData getImageData() {
060: return createImageData(image);
061: }
062: };
063: }
064:
065: /** Create a buffered image that can be be coverted to SWTland later */
066: public static BufferedImage createBufferedImage(int w, int h) {
067: return new BufferedImage(w, h,
068: BufferedImage.TYPE_4BYTE_ABGR_PRE);
069: }
070:
071: public static Image createSWTImage(RenderedImage image) {
072: // Rectangle size = new Rectangle(0, 0, image.getWidth(), image.getHeight());
073: ImageData data = createImageData(image);
074:
075: return new org.eclipse.swt.graphics.Image(Display.getDefault(),
076: data);
077: }
078:
079: public final static int TRANSPARENT = 0x220000 | 0x2200 | 0x22;
080:
081: public static ImageData createImageData(RenderedImage image) {
082: ImageData swtdata = null;
083: int width = image.getWidth();
084: int height = image.getHeight();
085: PaletteData palette;
086: int depth;
087:
088: depth = 24;
089: palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
090: swtdata = new ImageData(width, height, depth, palette);
091: swtdata.transparentPixel = TRANSPARENT;
092:
093: byte blueT = (byte) ((TRANSPARENT) & 0xFF);
094: byte greenT = (byte) ((TRANSPARENT >> 8) & 0xFF);
095: byte redT = (byte) ((TRANSPARENT >> 16) & 0xFF);
096: // System.out.println("red="+redT+"blue"+blueT+"green"+greenT);
097: // System.out.println("Transparent"+TRANSPARENT);
098:
099: // awtImage2.getRGB();
100: Raster raster = image.getData();
101: int[] awtdata = raster.getPixels(0, 0, width, height,
102: new int[width * height * 3]);// raster.getNumBands()]);
103: int step = swtdata.depth / 8;
104:
105: byte[] data = swtdata.data;
106: int baseindex = 0;
107: // System.out.println( "AWT size:" + awtdata.length );
108: for (int y = 0; y < height; y++) {
109: int idx = ((0 + y) * swtdata.bytesPerLine) + (0 * step);
110:
111: for (int x = 0; x < width; x++) {
112: baseindex = (x + (y * width)) * 4;
113:
114: if (awtdata[baseindex + 3] == 0) {
115: data[idx++] = blueT;
116: data[idx++] = greenT;
117: data[idx++] = redT;
118: } else {
119: data[idx++] = (byte) awtdata[baseindex];
120: data[idx++] = (byte) awtdata[baseindex + 1];
121: data[idx++] = (byte) awtdata[baseindex + 2];
122: }
123: }
124: }
125: return swtdata;
126: }
127:
128: public ImageDescriptor createWMSGylph(Layer target) {
129: if (target.isType(WebMapServer.class))
130: return null;
131: try {
132: WebMapServer wms = target.getResource(WebMapServer.class,
133: null);
134: org.geotools.data.ows.Layer layer = target.getResource(
135: org.geotools.data.ows.Layer.class, null);
136:
137: if (wms.getCapabilities().getRequest()
138: .getGetLegendGraphic() != null) {
139:
140: GetLegendGraphicRequest request = wms
141: .createGetLegendGraphicRequest();
142: request.setLayer(layer.getName());
143:
144: String desiredFormat = null;
145: List formats = Arrays.asList(wms.getCapabilities()
146: .getRequest().getGetLegendGraphic()
147: .getFormatStrings());
148: if (formats.contains("image/png")) { //$NON-NLS-1$
149: desiredFormat = "image/png"; //$NON-NLS-1$
150: }
151: if (desiredFormat == null
152: && formats.contains("image/gif")) { //$NON-NLS-1$
153: desiredFormat = "image/gif"; //$NON-NLS-1$
154: }
155: if (desiredFormat == null) {
156: return null;
157: }
158: request.setFormat(desiredFormat);
159:
160: return ImageDescriptor.createFromURL(request
161: .getFinalURL());
162: }
163: } catch (Exception e) {
164: // darn
165: }
166: return null;
167: /*
168: * BufferedImage image = createBufferedImage( target, 16, 16); Graphics2D g2 = (Graphics2D)
169: * image.getGraphics(); g2.setColor(Color.GREEN); g2.fillRect(1, 1, 14, 14);
170: * g2.setColor(Color.BLACK); g2.drawRect(0, 0, 15, 15); return createImageDescriptor(image);
171: */
172: }
173:
174: private Feature sampleFeature(Layer layer) {
175: FeatureReader reader = null;
176: try {
177: reader = layer.getResource(FeatureSource.class, null)
178: .getFeatures().reader();
179: } catch (Throwable ignore) {
180: return null;
181: }
182: try {
183: return reader.next();
184: } catch (NoSuchElementException e) {
185: return null;
186: } catch (IOException e) {
187: return null;
188: } catch (IllegalAttributeException e) {
189: return null;
190: } finally {
191: try {
192: reader.close();
193: } catch (IOException e1) {
194: return null;
195: }
196: }
197: }
198:
199: public Image createGlyph(Layer layer, Style styleObject) {
200: int width = 16;
201: int height = 16;
202:
203: Image image = new Image(Display.getDefault(), width, height);
204: Feature feature = sampleFeature(layer);
205: ViewportGraphics graphics = Drawing.createGraphics(
206: new GC(image), Display.getDefault(), new Dimension(
207: width - 1, width - 1));
208: graphics.clearRect(0, 0, width, height);
209: // graphics.clearRect(0,0,16,16);
210: AffineTransform transform = Drawing.worldToScreenTransform(
211: feature.getBounds(), new Rectangle(1, 0, width - 1,
212: width - 1));
213: // Drawing.createGraphics(image.createGraphics());
214: Drawing.create().drawFeature(graphics, feature, transform,
215: styleObject);
216: // return createSWTImage(image);
217: return image;
218: // StyleImpl imp = (StyleImpl) styleObject;
219: // FeatureTypeStyle style = imp.getFeatureTypeStyles()[0];
220: // Rule rule = style.getRules()[0];
221: // Symbolizer symbolizer = rule.getSymbolizers()[0];
222: // Feature feature = sampleFeature( layer );
223: //
224: // if (symbolizer instanceof LineSymbolizer) {
225: // try {
226: // LineSymbolizer line = (LineSymbolizer) symbolizer;
227: // Stroke stroke = line.getStroke();
228: // Color color = stroke.getColor(feature);
229: //
230: // BufferedImage image = createBufferedImage( layer, 16, 16);
231: // Graphics2D g2 = (Graphics2D) image.getGraphics();
232: // g2.setColor(color);
233: // g2.drawArc(4, 4, 24, 24, 90, 90);
234: // g2.drawLine(0, 2, 11, 11);
235: // g2.drawLine(11, 11, 15, 8);
236: //
237: // g2.setColor(Color.BLACK);
238: // g2.drawRect(0, 0, 15, 15);
239: // return createImageDescriptor(image);
240: // } catch (Exception e) {
241: // BufferedImage image = createBufferedImage(16, 16);
242: // Graphics2D g2 = (Graphics2D) image.getGraphics();
243: // g2.setColor(Color.BLACK);
244: // g2.drawArc(4, 4, 24, 24, 90, 90);
245: // g2.drawLine(0, 2, 11, 11);
246: // g2.drawLine(11, 11, 15, 8);
247: //
248: // g2.setColor(Color.BLACK);
249: // g2.drawRect(0, 0, 15, 15);
250: // return createImageDescriptor(image);
251: // }
252: // } else if (symbolizer instanceof PolygonSymbolizer) {
253: // try {
254: // PolygonSymbolizer poly = (PolygonSymbolizer) symbolizer;
255: // Stroke stroke = poly.getStroke();
256: // Color color = stroke.getColor(feature);
257: // Fill fill = poly.getFill();
258: // Paint fillColor = (Paint) fill.getColor().getValue(feature);
259: //
260: // BufferedImage image = createBufferedImage( layer, 16, 16);
261: // Graphics2D g2 = (Graphics2D) image.getGraphics();
262: // g2.setPaint(fillColor);
263: // g2.fillArc(4, 4, 24, 24, 90, 90);
264: // g2.setPaint(color);
265: // g2.drawArc(4, 4, 24, 24, 90, 90);
266: //
267: // g2.setColor(Color.BLACK);
268: // g2.drawRect(0, 0, 16, 16);
269: // return createImageDescriptor(image);
270: // } catch (Exception e) {
271: // BufferedImage image = createBufferedImage( layer, 16, 16);
272: // Graphics2D g2 = (Graphics2D) image.getGraphics();
273: //
274: // g2.setColor(Color.BLACK);
275: // g2.drawRect(2, 2, 11, 11);
276: // g2.setColor(Color.BLUE);
277: // g2.fillRect(3, 3, 10, 10);
278: //
279: // g2.setColor(Color.BLACK);
280: // g2.drawRect(0, 0, 15, 15);
281: // return createImageDescriptor(image);
282: // }
283: // }
284: // return null;
285: }
286:
287: public Object createGlyph(Layer layer) {
288: try {
289: ImageDescriptor glyph;
290: if (layer.isType(WebMapServer.class)) {
291: glyph = createWMSGylph(layer);
292: if (glyph != null)
293: return glyph;
294: }
295: /*
296: * // This so does not work right now if (layer.getStyle() != null) { glyph =
297: * createGylph( layer, layer.getStyle() ); if( glyph != null ) return glyph; }
298: */
299: } catch (Throwable t) {
300: t.printStackTrace();
301: }
302: return ProjectEditPlugin.INSTANCE.getImage("full/obj16/Layer"); //$NON-NLS-1$
303: }
304:
305: }
|