]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/CompositionImageDescriptor.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / CompositionImageDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 27.12.2006
14  */
15 package org.simantics.utils.ui.gfx;
16
17 import java.util.Collection;
18
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.graphics.PaletteData;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.RGB;
26 import org.simantics.utils.datastructures.Array;
27
28 /**
29  * CompositionImageDescriptor is image description that composes
30  * multiple images into one image.
31  * <p> 
32  * 
33  * @author Toni Kalajainen
34  */
35 @SuppressWarnings("deprecation")
36 public class CompositionImageDescriptor extends ImageDescriptor {
37
38     public static final PaletteData DEFAULT_PALETTEDATA = 
39         new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
40     
41     public static ImageDescriptor compose(ImageDescriptor ... imageDescriptors) {
42         if (imageDescriptors.length == 1)
43             return imageDescriptors[0];
44         return new CompositionImageDescriptor(imageDescriptors);
45     }
46     
47     public static ImageDescriptor compose(Collection<ImageDescriptor> imageDescriptors) {
48         int size = imageDescriptors.size();
49         if (size == 1)
50             return imageDescriptors.iterator().next();
51         return new CompositionImageDescriptor(imageDescriptors.toArray(new ImageDescriptor[size]));
52     }    
53     
54     ImageCache cache = ImageCache.getInstance();
55     final Array<ImageDescriptor> ids;
56     Point size;
57
58     public CompositionImageDescriptor(ImageDescriptor[] ids) {
59         this.ids = new Array<ImageDescriptor>(ids); 
60         // Make sure that the argument is valid
61         assert (this.ids.size() > 0);
62         for (ImageDescriptor id : ids)
63             assert (id != null);
64     }
65     
66     public CompositionImageDescriptor(Array<ImageDescriptor> ids) {
67         this.ids = ids;
68         // Make sure that the argument is valid
69         assert (this.ids.size() > 0);
70         for (ImageDescriptor id : ids.toArray())
71             assert (id != null);
72     }
73     
74     @Override
75     public ImageData getImageData() {
76         ImageDescriptor [] _ids = ids.toArray();
77         if (_ids.length==1)
78             return cache.getImage(_ids[0]).getImageData();
79             
80         Point s = getSize();        
81         PaletteData palette = DEFAULT_PALETTEDATA;
82         ImageData id = new ImageData(s.x, s.y, 24, palette);
83         id.setAlpha(0,0,0);
84         for (int i=0; i<_ids.length; i++)
85         {
86             ImageData layer = ImageCache.getInstance().getImage(_ids[i]).getImageData();
87             int width = Math.min(s.x, layer.width);
88             int height = Math.min(s.y, layer.height);
89             PaletteData layerPaletteData = layer.palette;
90             if (layer.getTransparencyType()==SWT.TRANSPARENCY_MASK ||
91                 layer.getTransparencyType()==SWT.TRANSPARENCY_PIXEL)
92             {
93                 ImageData mask = layer.getTransparencyMask();
94                 for (int y=0; y<height; y++)
95                     for (int x=0; x<width; x++)
96                         if (mask.getPixel(x, y)==1) {
97                             RGB rgb = layerPaletteData.getRGB(layer.getPixel(x, y));
98                             id.setPixel(x, y, palette.getPixel(rgb));
99                             id.setAlpha(x, y, 255);
100                         }
101             } else {
102                 for (int y=0; y<height; y++)
103                     for (int x=0; x<width; x++)
104                     {                                                
105                         int layerAlpha = layer.getAlpha(x, y);
106                         int origAlpha = id.getAlpha(x,y);
107                         RGB layerRGB = layerPaletteData.getRGB(layer.getPixel(x, y));
108                         RGB origRGB = palette.getRGB(id.getPixel(x, y));
109                         int newR = ( ( origRGB.red * (255-layerAlpha) ) +
110                                 ( layerRGB.red * (layerAlpha) ) ) / 255;
111                         int newG = ( ( origRGB.green * (255-layerAlpha) ) +
112                                 ( layerRGB.green * (layerAlpha) ) ) / 255;
113                         int newB = ( ( origRGB.blue * (255-layerAlpha) ) +
114                                 ( layerRGB.blue * (layerAlpha) ) ) / 255;                        
115                         int newAlpha = origAlpha + ((255-origAlpha)*layerAlpha)/255;
116                         id.setPixel(x, y, palette.getPixel(new RGB(newR, newG, newB)));
117                         id.setAlpha(x, y, newAlpha);
118                     }                
119             }
120                 
121         }
122         //ImageData orig = ImageCache.getInstance().getImage(desc).getImageData();
123         
124         return id;
125     }
126     
127     private Point _countSize()
128     {
129         int width = 0;
130         int height = 0;
131         for (ImageDescriptor id : ids.toArray())
132         {
133             Image i = cache.getImage(id);                
134             int w = i.getImageData().width;
135             if (w>width) width = w;
136             int h = i.getImageData().height;
137             if (h>height) height = h;
138         }
139         return new Point(width, height);
140     }
141     
142     protected Point getSize() {
143         if (size==null) size = _countSize(); 
144         return size;
145     }  
146     
147     @Override
148     public boolean equals(Object obj) {
149         if (!(obj instanceof CompositionImageDescriptor))
150             return false;
151         CompositionImageDescriptor other = (CompositionImageDescriptor) obj;
152         return other.ids.equals(ids);
153     }
154     
155     @Override
156     public int hashCode() {
157         return ids.hashCode();
158     }    
159
160 }