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