Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/resource/CompositeImageDescriptor.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.jface.resource.CompositeImageDescriptor; | |
14 | |
15 import org.eclipse.jface.resource.ImageDescriptor; | |
16 | |
17 import org.eclipse.swt.graphics.ImageData; | |
18 import org.eclipse.swt.graphics.PaletteData; | |
19 import org.eclipse.swt.graphics.Point; | |
20 import org.eclipse.swt.graphics.RGB; | |
21 | |
22 import java.lang.all; | |
23 import java.util.Set; | |
24 | |
25 /** | |
26 * Abstract base class for image descriptors that synthesize an image from other | |
27 * images in order to simulate the effect of custom drawing. For example, this | |
28 * could be used to superimpose a red bar dexter symbol across an image to | |
29 * indicate that something was disallowed. | |
30 * <p> | |
31 * Subclasses must implement the <code>getSize</code> and <code>fill</code> | |
32 * methods. Little or no work happens until the image descriptor's image is | |
33 * actually requested by a call to <code>createImage</code> (or to | |
34 * <code>getImageData</code> directly). | |
35 * </p> | |
36 */ | |
37 public abstract class CompositeImageDescriptor : ImageDescriptor { | |
38 | |
39 /** | |
40 * The image data for this composite image. | |
41 */ | |
42 private ImageData imageData; | |
43 | |
44 /** | |
45 * Constructs an uninitialized composite image. | |
46 */ | |
47 protected this() { | |
48 } | |
49 | |
50 /** | |
51 * Draw the composite images. | |
52 * <p> | |
53 * Subclasses must implement this framework method to paint images within | |
54 * the given bounds using one or more calls to the <code>drawImage</code> | |
55 * framework method. | |
56 * </p> | |
57 * | |
58 * @param width | |
59 * the width | |
60 * @param height | |
61 * the height | |
62 */ | |
63 protected abstract void drawCompositeImage(int width, int height); | |
64 | |
65 /** | |
66 * Draws the given source image data into this composite image at the given | |
67 * position. | |
68 * <p> | |
69 * Call this internal framework method to superimpose another image atop | |
70 * this composite image. | |
71 * </p> | |
72 * | |
73 * @param src | |
74 * the source image data | |
75 * @param ox | |
76 * the x position | |
77 * @param oy | |
78 * the y position | |
79 */ | |
80 final protected void drawImage(ImageData src, int ox, int oy) { | |
81 ImageData dst = imageData; | |
82 PaletteData srcPalette = src.palette; | |
83 ImageData srcMask = null; | |
84 int alphaMask = 0, alphaShift = 0; | |
85 if (src.maskData !is null) { | |
86 srcMask = src.getTransparencyMask (); | |
87 if (src.depth is 32) { | |
88 alphaMask = ~(srcPalette.redMask | srcPalette.greenMask | srcPalette.blueMask); | |
89 while (alphaMask !is 0 && ((alphaMask >>> alphaShift) & 1) is 0) alphaShift++; | |
90 } | |
91 } | |
92 for (int srcY = 0, dstY = srcY + oy; srcY < src.height; srcY++, dstY++) { | |
93 for (int srcX = 0, dstX = srcX + ox; srcX < src.width; srcX++, dstX++) { | |
94 if (!(0 <= dstX && dstX < dst.width && 0 <= dstY && dstY < dst.height)) continue; | |
95 int srcPixel = src.getPixel(srcX, srcY); | |
96 int srcAlpha = 255; | |
97 if (src.maskData !is null) { | |
98 if (src.depth is 32) { | |
99 srcAlpha = (srcPixel & alphaMask) >>> alphaShift; | |
100 if (srcAlpha is 0) { | |
101 srcAlpha = srcMask.getPixel(srcX, srcY) !is 0 ? 255 : 0; | |
102 } | |
103 } else { | |
104 if (srcMask.getPixel(srcX, srcY) is 0) srcAlpha = 0; | |
105 } | |
106 } else if (src.transparentPixel !is -1) { | |
107 if (src.transparentPixel is srcPixel) srcAlpha = 0; | |
108 } else if (src.alpha !is -1) { | |
109 srcAlpha = src.alpha; | |
110 } else if (src.alphaData !is null) { | |
111 srcAlpha = src.getAlpha(srcX, srcY); | |
112 } | |
113 if (srcAlpha is 0) continue; | |
114 int srcRed, srcGreen, srcBlue; | |
115 if (srcPalette.isDirect) { | |
116 srcRed = srcPixel & srcPalette.redMask; | |
117 srcRed = (srcPalette.redShift < 0) ? srcRed >>> -srcPalette.redShift : srcRed << srcPalette.redShift; | |
118 srcGreen = srcPixel & srcPalette.greenMask; | |
119 srcGreen = (srcPalette.greenShift < 0) ? srcGreen >>> -srcPalette.greenShift : srcGreen << srcPalette.greenShift; | |
120 srcBlue = srcPixel & srcPalette.blueMask; | |
121 srcBlue = (srcPalette.blueShift < 0) ? srcBlue >>> -srcPalette.blueShift : srcBlue << srcPalette.blueShift; | |
122 } else { | |
123 RGB rgb = srcPalette.getRGB(srcPixel); | |
124 srcRed = rgb.red; | |
125 srcGreen = rgb.green; | |
126 srcBlue = rgb.blue; | |
127 } | |
128 int dstRed, dstGreen, dstBlue, dstAlpha; | |
129 if (srcAlpha is 255) { | |
130 dstRed = srcRed; | |
131 dstGreen = srcGreen; | |
132 dstBlue= srcBlue; | |
133 dstAlpha = srcAlpha; | |
134 } else { | |
135 int dstPixel = dst.getPixel(dstX, dstY); | |
136 dstAlpha = dst.getAlpha(dstX, dstY); | |
137 dstRed = (dstPixel & 0xFF) >>> 0; | |
138 dstGreen = (dstPixel & 0xFF00) >>> 8; | |
139 dstBlue = (dstPixel & 0xFF0000) >>> 16; | |
140 dstRed += (srcRed - dstRed) * srcAlpha / 255; | |
141 dstGreen += (srcGreen - dstGreen) * srcAlpha / 255; | |
142 dstBlue += (srcBlue - dstBlue) * srcAlpha / 255; | |
143 dstAlpha += (srcAlpha - dstAlpha) * srcAlpha / 255; | |
144 } | |
145 dst.setPixel(dstX, dstY, ((dstRed & 0xFF) << 0) | ((dstGreen & 0xFF) << 8) | ((dstBlue & 0xFF) << 16)); | |
146 dst.setAlpha(dstX, dstY, dstAlpha); | |
147 } | |
148 } | |
149 } | |
150 | |
151 /* | |
152 * (non-Javadoc) Method declared on ImageDesciptor. | |
153 */ | |
154 public override ImageData getImageData() { | |
155 Point size = getSize(); | |
156 | |
157 /* Create a 24 bit image data with alpha channel */ | |
158 imageData = new ImageData(size.x, size.y, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000)); | |
159 imageData.alphaData = new byte[imageData.width * imageData.height]; | |
160 | |
161 drawCompositeImage(size.x, size.y); | |
162 | |
163 /* Detect minimum transparency */ | |
164 bool transparency = false; | |
165 byte[] alphaData = imageData.alphaData; | |
166 for (int i = 0; i < alphaData.length; i++) { | |
167 int alpha = alphaData[i] & 0xFF; | |
168 if (!(alpha is 0 || alpha is 255)) { | |
169 /* Full alpha channel transparency */ | |
170 return imageData; | |
171 } | |
172 if (!transparency && alpha is 0) transparency = true; | |
173 } | |
174 if (transparency) { | |
175 /* Reduce to 1-bit alpha channel transparency */ | |
176 PaletteData palette = new PaletteData([new RGB(0, 0, 0), new RGB(255, 255, 255)]); | |
177 ImageData mask = new ImageData(imageData.width, imageData.height, 1, palette); | |
178 for (int y = 0; y < mask.height; y++) { | |
179 for (int x = 0; x < mask.width; x++) { | |
180 mask.setPixel(x, y, imageData.getAlpha(x, y) is 255 ? 1 : 0); | |
181 } | |
182 } | |
183 } else { | |
184 /* no transparency */ | |
185 imageData.alphaData = null; | |
186 } | |
187 return imageData; | |
188 } | |
189 | |
190 | |
191 /** | |
192 * Return the transparent pixel for the receiver. | |
193 * <strong>NOTE</strong> This value is not currently in use in the | |
194 * default implementation. | |
195 * @return int | |
196 * @since 3.3 | |
197 */ | |
198 protected int getTransparentPixel() { | |
199 return 0; | |
200 } | |
201 | |
202 /** | |
203 * Return the size of this composite image. | |
204 * <p> | |
205 * Subclasses must implement this framework method. | |
206 * </p> | |
207 * | |
208 * @return the x and y size of the image expressed as a point object | |
209 */ | |
210 protected abstract Point getSize(); | |
211 | |
212 /** | |
213 * @param imageData The imageData to set. | |
214 * @since 3.3 | |
215 */ | |
216 protected void setImageData(ImageData imageData) { | |
217 this.imageData = imageData; | |
218 } | |
219 } |