1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
15 package org.simantics.utils.ui.gfx;
17 import java.util.Collection;
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;
29 * CompositionImageDescriptor is image description that composes
30 * multiple images into one image.
33 * @author Toni Kalajainen
35 @SuppressWarnings("deprecation")
36 public class CompositionImageDescriptor extends ImageDescriptor {
38 public static final PaletteData DEFAULT_PALETTEDATA =
39 new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
41 public static ImageDescriptor compose(ImageDescriptor ... imageDescriptors) {
42 if (imageDescriptors.length == 1)
43 return imageDescriptors[0];
44 return new CompositionImageDescriptor(imageDescriptors);
47 public static ImageDescriptor compose(Collection<ImageDescriptor> imageDescriptors) {
48 int size = imageDescriptors.size();
50 return imageDescriptors.iterator().next();
51 return new CompositionImageDescriptor(imageDescriptors.toArray(new ImageDescriptor[size]));
54 ImageCache cache = ImageCache.getInstance();
55 final Array<ImageDescriptor> ids;
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)
66 public CompositionImageDescriptor(Array<ImageDescriptor> ids) {
68 // Make sure that the argument is valid
69 assert (this.ids.size() > 0);
70 for (ImageDescriptor id : ids.toArray())
75 public ImageData getImageData() {
76 ImageDescriptor [] _ids = ids.toArray();
78 return cache.getImage(_ids[0]).getImageData();
81 PaletteData palette = DEFAULT_PALETTEDATA;
82 ImageData id = new ImageData(s.x, s.y, 24, palette);
84 for (int i=0; i<_ids.length; i++)
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)
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);
102 for (int y=0; y<height; y++)
103 for (int x=0; x<width; x++)
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);
122 //ImageData orig = ImageCache.getInstance().getImage(desc).getImageData();
127 private Point _countSize()
131 for (ImageDescriptor id : ids.toArray())
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;
139 return new Point(width, height);
142 protected Point getSize() {
143 if (size==null) size = _countSize();
148 public boolean equals(Object obj) {
149 if (!(obj instanceof CompositionImageDescriptor))
151 CompositionImageDescriptor other = (CompositionImageDescriptor) obj;
152 return other.ids.equals(ids);
156 public int hashCode() {
157 return ids.hashCode();